Skip to content

Commit cfddefe

Browse files
committed
Fix unit tests compilation for relay & public api
1 parent 5b6371e commit cfddefe

5 files changed

Lines changed: 38 additions & 30 deletions

File tree

rln-cli/src/examples/relay.rs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@ use rln::{
1212
hashers::{hash_to_field, poseidon_hash},
1313
protocol::{keygen, prepare_prove_input, prepare_verify_input},
1414
public::RLN,
15-
utils::{bytes_le_to_fr, fr_to_bytes_le, generate_input_buffer},
15+
utils::{fr_to_bytes_le, generate_input_buffer},
1616
};
17+
use rln::utils::IdSecret;
1718

1819
const MESSAGE_LIMIT: u32 = 1;
1920

@@ -44,7 +45,7 @@ enum Commands {
4445

4546
#[derive(Debug, Clone)]
4647
struct Identity {
47-
identity_secret_hash: Fr,
48+
identity_secret_hash: IdSecret,
4849
id_commitment: Fr,
4950
}
5051

@@ -103,7 +104,7 @@ impl RLNSystem {
103104
println!("Registered users:");
104105
for (index, identity) in &self.local_identities {
105106
println!("User Index: {index}");
106-
println!("+ Identity Secret Hash: {}", identity.identity_secret_hash);
107+
println!("+ Identity Secret Hash: ********");
107108
println!("+ Identity Commitment: {}", identity.id_commitment);
108109
println!();
109110
}
@@ -118,7 +119,7 @@ impl RLNSystem {
118119
match self.rln.set_next_leaf(&mut buffer) {
119120
Ok(_) => {
120121
println!("Registered User Index: {index}");
121-
println!("+ Identity secret hash: {}", identity.identity_secret_hash);
122+
println!("+ Identity secret hash: ********");
122123
println!("+ Identity commitment: {},", identity.id_commitment);
123124
self.local_identities.insert(index, identity);
124125
}
@@ -143,7 +144,7 @@ impl RLNSystem {
143144
};
144145

145146
let serialized = prepare_prove_input(
146-
identity.identity_secret_hash,
147+
identity.identity_secret_hash.clone(),
147148
user_index,
148149
Fr::from(MESSAGE_LIMIT),
149150
Fr::from(message_id),
@@ -211,7 +212,7 @@ impl RLNSystem {
211212
{
212213
Ok(_) => {
213214
let output_data = output.into_inner();
214-
let (leaked_identity_secret_hash, _) = bytes_le_to_fr(&output_data);
215+
let (leaked_identity_secret_hash, _) = IdSecret::from_bytes_le(&output_data);
215216

216217
if let Some((user_index, identity)) = self
217218
.local_identities
@@ -221,19 +222,19 @@ impl RLNSystem {
221222
})
222223
.map(|(index, identity)| (*index, identity))
223224
{
224-
let real_identity_secret_hash = identity.identity_secret_hash;
225+
let real_identity_secret_hash = identity.identity_secret_hash.clone();
225226
if leaked_identity_secret_hash != real_identity_secret_hash {
226-
Err(eyre!("identity secret hash mismatch {leaked_identity_secret_hash} != {real_identity_secret_hash}"))
227+
Err(eyre!("identity secret hash mismatch: leaked_identity_secret_hash != real_identity_secret_hash"))
227228
} else {
228-
println!("DUPLICATE message ID detected! Reveal identity secret hash: {leaked_identity_secret_hash}");
229+
println!("DUPLICATE message ID detected! Reveal identity secret hash: ********");
229230
self.local_identities.remove(&user_index);
230231
self.rln.delete_leaf(user_index)?;
231232
println!("User index {user_index} has been SLASHED");
232233
Ok(())
233234
}
234235
} else {
235236
Err(eyre!(
236-
"user identity secret hash {leaked_identity_secret_hash} not found"
237+
"user identity secret hash ******** not found"
237238
))
238239
}
239240
}

rln/src/protocol.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,7 @@ pub fn deserialize_proof_values(serialized: &[u8]) -> (RLNProofValues, usize) {
373373

374374
// input_data is [ identity_secret<32> | id_index<8> | user_message_limit<32> | message_id<32> | external_nullifier<32> | signal_len<8> | signal<var> ]
375375
pub fn prepare_prove_input(
376-
identity_secret: Fr,
376+
identity_secret: IdSecret,
377377
id_index: usize,
378378
user_message_limit: Fr,
379379
message_id: Fr,
@@ -386,7 +386,7 @@ pub fn prepare_prove_input(
386386
// - variable length signal data
387387
let mut serialized = Vec::with_capacity(fr_byte_size() * 4 + 16 + signal.len()); // length of 4 fr elements + 16 bytes (id_index + len) + signal length
388388

389-
serialized.extend_from_slice(&fr_to_bytes_le(&identity_secret));
389+
serialized.extend_from_slice(&identity_secret.to_bytes_le());
390390
serialized.extend_from_slice(&normalize_usize(id_index));
391391
serialized.extend_from_slice(&fr_to_bytes_le(&user_message_limit));
392392
serialized.extend_from_slice(&fr_to_bytes_le(&message_id));

rln/src/public_api_tests.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -880,7 +880,7 @@ mod tree_test {
880880
// We prepare input for generate_rln_proof API
881881
// input_data is [ identity_secret<32> | id_index<8> | user_message_limit<32> | message_id<32> | external_nullifier<32> | signal_len<8> | signal<var> ]
882882
let prove_input1 = prepare_prove_input(
883-
identity_secret_hash,
883+
identity_secret_hash.clone(),
884884
identity_index,
885885
user_message_limit,
886886
message_id,
@@ -889,7 +889,7 @@ mod tree_test {
889889
);
890890

891891
let prove_input2 = prepare_prove_input(
892-
identity_secret_hash,
892+
identity_secret_hash.clone(),
893893
identity_index,
894894
user_message_limit,
895895
message_id,
@@ -932,7 +932,7 @@ mod tree_test {
932932

933933
// We check if the recovered identity secret hash corresponds to the original one
934934
let (recovered_identity_secret_hash, _) = bytes_le_to_fr(&serialized_identity_secret_hash);
935-
assert_eq!(recovered_identity_secret_hash, identity_secret_hash);
935+
assert_eq!(recovered_identity_secret_hash, *identity_secret_hash);
936936

937937
// We now test that computing identity_secret_hash is unsuccessful if shares computed from two different identity secret hashes but within same epoch are passed
938938

@@ -982,7 +982,7 @@ mod tree_test {
982982

983983
// ensure that the recovered secret does not match with either of the
984984
// used secrets in proof generation
985-
assert_ne!(recovered_identity_secret_hash_new, identity_secret_hash_new);
985+
assert_ne!(recovered_identity_secret_hash_new, *identity_secret_hash_new);
986986
}
987987
}
988988

rln/tests/ffi.rs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ mod test {
1414
use std::io::Read;
1515
use std::mem::MaybeUninit;
1616
use std::time::{Duration, Instant};
17+
use zeroize::Zeroize;
1718

1819
const NO_OF_LEAVES: usize = 256;
1920

@@ -49,13 +50,13 @@ mod test {
4950
root
5051
}
5152

52-
fn identity_pair_gen(rln_pointer: &mut RLN) -> (Fr, Fr) {
53+
fn identity_pair_gen(rln_pointer: &mut RLN) -> (IdSecret, Fr) {
5354
let mut output_buffer = MaybeUninit::<Buffer>::uninit();
5455
let success = key_gen(rln_pointer, output_buffer.as_mut_ptr());
5556
assert!(success, "key gen call failed");
5657
let output_buffer = unsafe { output_buffer.assume_init() };
5758
let result_data = <&[u8]>::from(&output_buffer).to_vec();
58-
let (identity_secret_hash, read) = bytes_le_to_fr(&result_data);
59+
let (identity_secret_hash, read) = IdSecret::from_bytes_le(&result_data);
5960
let (id_commitment, _) = bytes_le_to_fr(&result_data[read..].to_vec());
6061
(identity_secret_hash, id_commitment)
6162
}
@@ -271,8 +272,11 @@ mod test {
271272
let rln_pointer = create_rln_instance();
272273

273274
// generate identity
274-
let identity_secret_hash = hash_to_field(b"test-merkle-proof");
275-
let id_commitment = utils_poseidon_hash(&[identity_secret_hash]);
275+
let mut identity_secret_hash_ = hash_to_field(b"test-merkle-proof");
276+
let identity_secret_hash = IdSecret::from(&mut identity_secret_hash_);
277+
let mut to_hash = [*identity_secret_hash.clone()];
278+
let id_commitment = utils_poseidon_hash(&to_hash);
279+
to_hash[0].zeroize();
276280
let user_message_limit = Fr::from(100);
277281
let rate_commitment = utils_poseidon_hash(&[id_commitment, user_message_limit]);
278282

@@ -674,7 +678,7 @@ mod test {
674678
// We prepare input for generate_rln_proof API
675679
// input_data is [ identity_secret<32> | id_index<8> | user_message_limit<32> | message_id<32> | external_nullifier<32> | signal_len<8> | signal<var> ]
676680
let prove_input1 = prepare_prove_input(
677-
identity_secret_hash,
681+
identity_secret_hash.clone(),
678682
identity_index,
679683
user_message_limit,
680684
message_id,
@@ -683,7 +687,7 @@ mod test {
683687
);
684688

685689
let prove_input2 = prepare_prove_input(
686-
identity_secret_hash,
690+
identity_secret_hash.clone(),
687691
identity_index,
688692
user_message_limit,
689693
message_id,
@@ -718,7 +722,7 @@ mod test {
718722

719723
// We check if the recovered identity secret hash corresponds to the original one
720724
let (recovered_identity_secret_hash, _) = bytes_le_to_fr(&serialized_identity_secret_hash);
721-
assert_eq!(recovered_identity_secret_hash, identity_secret_hash);
725+
assert_eq!(recovered_identity_secret_hash, *identity_secret_hash);
722726

723727
// We now test that computing identity_secret_hash is unsuccessful if shares computed from two different identity secret hashes but within same epoch are passed
724728

@@ -772,7 +776,7 @@ mod test {
772776

773777
// ensure that the recovered secret does not match with either of the
774778
// used secrets in proof generation
775-
assert_ne!(recovered_identity_secret_hash_new, identity_secret_hash_new);
779+
assert_ne!(recovered_identity_secret_hash_new, *identity_secret_hash_new);
776780
}
777781

778782
#[test]

rln/tests/public.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,9 @@ mod test {
1212
use rln::hashers::{hash_to_field, poseidon_hash as utils_poseidon_hash, ROUND_PARAMS};
1313
use rln::protocol::deserialize_identity_tuple;
1414
use rln::public::{hash as public_hash, poseidon_hash as public_poseidon_hash, RLN};
15-
use rln::utils::{
16-
bytes_le_to_fr, bytes_le_to_vec_fr, bytes_le_to_vec_u8, bytes_le_to_vec_usize,
17-
fr_to_bytes_le, generate_input_buffer, str_to_fr, vec_fr_to_bytes_le,
18-
};
15+
use rln::utils::{bytes_le_to_fr, bytes_le_to_vec_fr, bytes_le_to_vec_u8, bytes_le_to_vec_usize, fr_to_bytes_le, generate_input_buffer, str_to_fr, vec_fr_to_bytes_le, IdSecret};
1916
use std::io::Cursor;
17+
use zeroize::Zeroize;
2018

2119
#[test]
2220
// This test is similar to the one in lib, but uses only public API
@@ -28,8 +26,13 @@ mod test {
2826
let mut rln = RLN::new(TEST_TREE_HEIGHT, generate_input_buffer()).unwrap();
2927

3028
// generate identity
31-
let identity_secret_hash = hash_to_field(b"test-merkle-proof");
32-
let id_commitment = utils_poseidon_hash(&vec![identity_secret_hash]);
29+
let mut identity_secret_hash_ = hash_to_field(b"test-merkle-proof");
30+
let identity_secret_hash = IdSecret::from(&mut identity_secret_hash_);
31+
32+
let mut to_hash = [*identity_secret_hash.clone()];
33+
let id_commitment = utils_poseidon_hash(&to_hash);
34+
to_hash[0].zeroize();
35+
3336
let rate_commitment = utils_poseidon_hash(&[id_commitment, user_message_limit.into()]);
3437

3538
// check that leaves indices is empty

0 commit comments

Comments
 (0)