Skip to content

Commit 5b6371e

Browse files
committed
Remove poseidon_hash_mt
1 parent 6edeb34 commit 5b6371e

3 files changed

Lines changed: 21 additions & 17 deletions

File tree

rln/src/hashers.rs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ use crate::{circuit::Fr, utils::bytes_le_to_fr};
33
use once_cell::sync::Lazy;
44
use tiny_keccak::{Hasher, Keccak};
55
use utils::poseidon::Poseidon;
6-
use zeroize::Zeroize;
76

87
/// These indexed constants hardcode the supported round parameters tuples (t, RF, RN, SKIP_MATRICES) for the Bn254 scalar field.
98
/// SKIP_MATRICES is the index of the randomly generated secure MDS matrix.
@@ -28,14 +27,6 @@ pub fn poseidon_hash(input: &[Fr]) -> Fr {
2827
.expect("hash with fixed input size can't fail")
2928
}
3029

31-
pub fn poseidon_hash_mut(input: &mut [Fr]) -> Fr {
32-
let result = POSEIDON
33-
.hash(input)
34-
.expect("hash with fixed input size can't fail");
35-
input.into_iter().for_each(|i| i.zeroize());
36-
result
37-
}
38-
3930
/// The zerokit RLN Merkle tree Hasher.
4031
#[derive(Clone, Copy, PartialEq, Eq)]
4132
pub struct PoseidonHash;

rln/src/protocol.rs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
use crate::circuit::{calculate_rln_witness, qap::CircomReduction, Curve};
44
use crate::error::{ComputeIdSecretError, ConversionError, ProofError, ProtocolError};
5-
use crate::hashers::{hash_to_field, poseidon_hash, poseidon_hash_mut};
5+
use crate::hashers::{hash_to_field, poseidon_hash};
66
use crate::poseidon_tree::{MerkleProof, PoseidonTree};
77
use crate::public::RLN_IDENTIFIER;
88
use crate::utils::{
@@ -298,13 +298,14 @@ pub fn proof_values_from_witness(
298298
message_id_range_check(&rln_witness.message_id, &rln_witness.user_message_limit)?;
299299

300300
// y share
301-
let mut a_0: Fr = *rln_witness.identity_secret;
302-
let a_1 = poseidon_hash(&[a_0, rln_witness.external_nullifier, rln_witness.message_id]);
303-
let y = a_0 + rln_witness.x * a_1;
304-
a_0.zeroize();
301+
let a_0 = &rln_witness.identity_secret;
302+
let mut to_hash = [*(a_0.clone()), rln_witness.external_nullifier, rln_witness.message_id];
303+
let a_1 = poseidon_hash(&to_hash);
304+
let y = *(a_0.clone()) + rln_witness.x * a_1;
305305

306306
// Nullifier
307307
let nullifier = poseidon_hash(&[a_1]);
308+
to_hash[0].zeroize();
308309

309310
// Merkle tree root computations
310311
let root = compute_tree_root(
@@ -421,7 +422,10 @@ pub fn compute_tree_root(
421422
path_elements: &[Fr],
422423
identity_path_index: &[u8],
423424
) -> Fr {
424-
let id_commitment = poseidon_hash_mut(&mut [**identity_secret]);
425+
let mut to_hash = [*identity_secret.clone()];
426+
let id_commitment = poseidon_hash(&to_hash);
427+
to_hash[0].zeroize();
428+
425429
let mut root = poseidon_hash(&[id_commitment, *user_message_limit]);
426430

427431
for i in 0..identity_path_index.len() {
@@ -445,7 +449,9 @@ pub fn compute_tree_root(
445449
pub fn keygen() -> (IdSecret, Fr) {
446450
let mut rng = thread_rng();
447451
let identity_secret_hash = IdSecret::rand(&mut rng);
448-
let id_commitment = poseidon_hash_mut(&mut [*identity_secret_hash]);
452+
let mut to_hash = [*identity_secret_hash.clone()];
453+
let id_commitment = poseidon_hash(&to_hash);
454+
to_hash[0].zeroize();
449455
(identity_secret_hash, id_commitment)
450456
}
451457

rln/src/utils.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,11 +190,18 @@ impl IdSecret {
190190
pub(crate) fn to_bytes_le(&self) -> Zeroizing<Vec<u8>> {
191191
let input_biguint: BigUint = self.0.into();
192192
let mut res = input_biguint.to_bytes_le();
193-
//BigUint conversion ignores most significant zero bytes. We restore them otherwise serialization will fail (length % 8 != 0)
194193
res.resize(fr_byte_size(), 0);
195194
Zeroizing::new(res)
196195
}
197196

197+
/*
198+
pub(crate) fn to_fr(mut self) -> Zeroizing<Fr> {
199+
let result = Zeroizing::new(self.0);
200+
self.zeroize();
201+
result
202+
}
203+
*/
204+
198205
}
199206

200207
impl From<&mut Fr> for IdSecret {

0 commit comments

Comments
 (0)