Skip to content

Commit 678300e

Browse files
chore: unroll poseidon2 hash loop by RATE chunks (#30)
Co-authored-by: Tom French <[email protected]>
1 parent 596d59a commit 678300e

File tree

2 files changed

+11
-5
lines changed

2 files changed

+11
-5
lines changed

.github/workflows/benchmark.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ jobs:
1515
deployments: write
1616
# contents permission to update benchmark contents in gh-pages branch
1717
contents: write
18+
# pull-requests permission to comment on PRs with benchmark results
1819
pull-requests: write
1920

2021
steps:

src/poseidon2.nr

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,16 @@ impl Poseidon2 {
6363
state[RATE] = iv;
6464

6565
if std::runtime::is_unconstrained() {
66-
for i in 0..in_len {
67-
state[i % RATE] += input[i];
68-
if (i + 1) % RATE == 0 {
69-
state = crate::poseidon2_permutation(state, 4);
70-
}
66+
for i in 0..(in_len / RATE) {
67+
state[0] += input[i * RATE];
68+
state[1] += input[i * RATE + 1];
69+
state[2] += input[i * RATE + 2];
70+
state = crate::poseidon2_permutation(state, 4);
71+
}
72+
// handle remaining (<3) elements
73+
let remainder_start = (in_len / RATE) * RATE;
74+
for j in remainder_start..in_len {
75+
state[j - remainder_start] += input[j];
7176
}
7277
} else {
7378
for i in 0..input.len() {

0 commit comments

Comments
 (0)