Skip to content

Commit

Permalink
feat(perf): Simplify poseidon2 cache zero-pad (#5869)
Browse files Browse the repository at this point in the history
# Description

## Problem\*

Resolves <!-- Link to GitHub Issue -->

Works towards general Brillig bytecode size reductions

## Summary\*

When actually setting up the state for a poseidon2 permutation, we have
a loop to zero-pad the cache and then another loop to add the cache to
the state. If we just have a single loop that only adds to the state
when the rate index is below the cache size we achieve the same goal
with less code.

## Additional Context



## Documentation\*

Check one:
- [X] No documentation needed.
- [ ] Documentation included in this PR.
- [ ] **[For Experimental Features]** Documentation to be submitted in a
separate PR.

# PR Checklist\*

- [X] I have tested the changes locally.
- [X] I have formatted the changes with [Prettier](https://prettier.io/)
and/or `cargo fmt` on default settings.
  • Loading branch information
vezenovm authored Aug 30, 2024
1 parent 5c4f19f commit 31e9be6
Showing 1 changed file with 5 additions and 7 deletions.
12 changes: 5 additions & 7 deletions noir_stdlib/src/hash/poseidon2.nr
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,13 @@ impl Poseidon2 {
}

fn perform_duplex(&mut self) {
// zero-pad the cache
for i in 0..RATE {
if i >= self.cache_size {
self.cache[i] = 0;
}
}
// add the cache into sponge state
for i in 0..RATE {
self.state[i] += self.cache[i];
// We effectively zero-pad the cache by only adding to the state
// cache that is less than the specified `cache_size`
if i < self.cache_size {
self.state[i] += self.cache[i];
}
}
self.state = crate::hash::poseidon2_permutation(self.state, 4);
}
Expand Down

0 comments on commit 31e9be6

Please sign in to comment.