Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 26 additions & 5 deletions src/poseidon2.nr
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,34 @@ impl Poseidon2 {
fn hash_internal<let N: u32>(input: [Field; N], in_len: u32) -> Field {
let two_pow_64 = 18446744073709551616;
let iv: Field = (in_len as Field) * two_pow_64;
let mut sponge = Poseidon2::new(iv);
for i in 0..input.len() {
if i < in_len {
sponge.absorb(input[i]);
let mut state = [0; 4];
state[RATE] = iv;

if std::runtime::is_unconstrained() {
for i in 0..in_len {
state[i % RATE] += input[i];
if (i + 1) % RATE == 0 {
state = crate::poseidon2_permutation(state, 4);
}
}
Comment on lines +69 to +74
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be done in a followup but I'd like to explore us partially unrolling this so that we writes into the state array 4 at a time to avoid needing to do so many modulos.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

} else {
for i in 0..input.len() {
if i < in_len {
state[i % RATE] += input[i];
if (i + 1) % RATE == 0 {
state = crate::poseidon2_permutation(state, 4);
}
}
}
}
sponge.squeeze()

// Always run final permutation unless we just completed a full chunk
// still need to permute once if in_len is 0
if (in_len == 0) | (in_len % RATE != 0) {
state = crate::poseidon2_permutation(state, 4)
};

state[0]
}
}

Expand Down