From b3205e6cd61d455aa64d0a7b829c89119101a42b Mon Sep 17 00:00:00 2001 From: Thomas Coratger Date: Thu, 14 Mar 2024 19:04:07 +0100 Subject: [PATCH] Improve poseidon hash perf by using assign operators --- starknet-crypto/src/poseidon_hash.rs | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/starknet-crypto/src/poseidon_hash.rs b/starknet-crypto/src/poseidon_hash.rs index 235cc829..8603d65f 100644 --- a/starknet-crypto/src/poseidon_hash.rs +++ b/starknet-crypto/src/poseidon_hash.rs @@ -119,7 +119,7 @@ pub fn poseidon_permute_comp(state: &mut [FieldElement; 3]) { #[inline(always)] fn mix(state: &mut [FieldElement; 3]) { let t = state[0] + state[1] + state[2]; - state[0] = t + state[0].double(); + state[0] += state[0] + t; state[1] = t - state[1].double(); state[2] = t - FieldElement::THREE * state[2]; } @@ -130,12 +130,13 @@ fn round_comp(state: &mut [FieldElement; 3], idx: usize, full: bool) { state[0] += POSEIDON_COMP_CONSTS[idx]; state[1] += POSEIDON_COMP_CONSTS[idx + 1]; state[2] += POSEIDON_COMP_CONSTS[idx + 2]; - state[0] = state[0] * state[0] * state[0]; - state[1] = state[1] * state[1] * state[1]; - state[2] = state[2] * state[2] * state[2]; + + state[0] *= state[0] * state[0]; + state[1] *= state[1] * state[1]; + state[2] *= state[2] * state[2]; } else { state[2] += POSEIDON_COMP_CONSTS[idx]; - state[2] = state[2] * state[2] * state[2]; + state[2] *= state[2] * state[2]; } mix(state); }