Skip to content

Commit

Permalink
improve p2 time by 2x
Browse files Browse the repository at this point in the history
  • Loading branch information
alxiong committed Jan 14, 2025
1 parent 9a781de commit d7a8321
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 5 deletions.
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,7 @@ sha3 = { version = "0.10", default-features = false }
itertools = { version = "0.12", default-features = false }
tagged-base64 = "0.4"
zeroize = { version = "^1.8" }

[profile.profiling]
inherits = "release"
debug = true
7 changes: 3 additions & 4 deletions poseidon2/src/external.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,8 @@ pub(crate) fn permute_state<F: PrimeField, const T: usize>(
rc: &'static [F; T],
d: usize,
) {
state
.iter_mut()
.zip(rc.iter())
.for_each(|(s, &rc)| add_rc_and_sbox(s, rc, d));
for i in 0..T {
add_rc_and_sbox(&mut state[i], rc[i], d)
}
matmul_external(state);
}
1 change: 1 addition & 0 deletions poseidon2/src/internal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ fn matmul_internal<F: PrimeField, const T: usize>(

/// One internal round
// @credit `internal_permute_state()` in plonky3
#[inline(always)]
pub(crate) fn permute_state<F: PrimeField, const T: usize>(
state: &mut [F; T],
rc: F,
Expand Down
10 changes: 9 additions & 1 deletion poseidon2/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,15 @@ impl<F: PrimeField> Poseidon2<F> {
#[inline(always)]
pub(crate) fn add_rc_and_sbox<F: PrimeField>(val: &mut F, rc: F, d: usize) {
*val += rc;
*val = val.pow([d as u64]);
if d == 5 {
// Perform unrolled computation for val^5, faster
let original = *val;
val.square_in_place();
val.square_in_place();
*val *= &original;
} else {
*val = val.pow([d as u64]);
}
}

/// Poseidon2 Error type
Expand Down

0 comments on commit d7a8321

Please sign in to comment.