Skip to content

Commit 65f53e3

Browse files
authored
Initial impl for IdSecret (#320)
1 parent 042f8a9 commit 65f53e3

14 files changed

Lines changed: 274 additions & 109 deletions

File tree

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rln-cli/src/examples/relay.rs

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,13 @@ use std::{
77

88
use clap::{Parser, Subcommand};
99
use color_eyre::{eyre::eyre, Report, Result};
10+
use rln::utils::IdSecret;
1011
use rln::{
1112
circuit::Fr,
1213
hashers::{hash_to_field, poseidon_hash},
1314
protocol::{keygen, prepare_prove_input, prepare_verify_input},
1415
public::RLN,
15-
utils::{bytes_le_to_fr, fr_to_bytes_le, generate_input_buffer},
16+
utils::{fr_to_bytes_le, generate_input_buffer},
1617
};
1718

1819
const MESSAGE_LIMIT: u32 = 1;
@@ -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: {}", *identity.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: {}", *identity.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,20 +222,21 @@ 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!(
230+
"DUPLICATE message ID detected! Reveal identity secret hash: {}",
231+
*leaked_identity_secret_hash
232+
);
229233
self.local_identities.remove(&user_index);
230234
self.rln.delete_leaf(user_index)?;
231235
println!("User index {user_index} has been SLASHED");
232236
Ok(())
233237
}
234238
} else {
235-
Err(eyre!(
236-
"user identity secret hash {leaked_identity_secret_hash} not found"
237-
))
239+
Err(eyre!("user identity secret hash ******** not found"))
238240
}
239241
}
240242
Err(err) => Err(eyre!("Failed to recover identity secret: {err}")),

rln-cli/src/examples/stateless.rs

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use std::{
66

77
use clap::{Parser, Subcommand};
88
use color_eyre::{eyre::eyre, Result};
9+
use rln::utils::IdSecret;
910
use rln::{
1011
circuit::{Fr, TEST_TREE_HEIGHT},
1112
hashers::{hash_to_field, poseidon_hash},
@@ -45,7 +46,7 @@ enum Commands {
4546

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

@@ -93,7 +94,7 @@ impl RLNSystem {
9394
println!("Registered users:");
9495
for (index, identity) in &self.local_identities {
9596
println!("User Index: {index}");
96-
println!("+ Identity Secret Hash: {}", identity.identity_secret_hash);
97+
println!("+ Identity Secret Hash: {}", *identity.identity_secret_hash);
9798
println!("+ Identity Commitment: {}", identity.id_commitment);
9899
println!();
99100
}
@@ -107,7 +108,7 @@ impl RLNSystem {
107108
self.tree.update_next(rate_commitment)?;
108109

109110
println!("Registered User Index: {index}");
110-
println!("+ Identity secret hash: {}", identity.identity_secret_hash);
111+
println!("+ Identity secret hash: {}", *identity.identity_secret_hash);
111112
println!("+ Identity commitment: {}", identity.id_commitment);
112113

113114
self.local_identities.insert(index, identity);
@@ -130,7 +131,7 @@ impl RLNSystem {
130131
let x = hash_to_field(signal.as_bytes());
131132

132133
let rln_witness = rln_witness_from_values(
133-
identity.identity_secret_hash,
134+
identity.identity_secret_hash.clone(),
134135
&merkle_proof,
135136
x,
136137
external_nullifier,
@@ -208,7 +209,7 @@ impl RLNSystem {
208209
{
209210
Ok(_) => {
210211
let output_data = output.into_inner();
211-
let (leaked_identity_secret_hash, _) = bytes_le_to_fr(&output_data);
212+
let (leaked_identity_secret_hash, _) = IdSecret::from_bytes_le(&output_data);
212213

213214
if let Some((user_index, identity)) = self
214215
.local_identities
@@ -218,19 +219,19 @@ impl RLNSystem {
218219
})
219220
.map(|(index, identity)| (*index, identity))
220221
{
221-
let real_identity_secret_hash = identity.identity_secret_hash;
222+
let real_identity_secret_hash = identity.identity_secret_hash.clone();
222223
if leaked_identity_secret_hash != real_identity_secret_hash {
223-
Err(eyre!("identity secret hash mismatch {leaked_identity_secret_hash} != {real_identity_secret_hash}"))
224+
Err(eyre!("identity secret hash mismatch: leaked_identity_secret_hash != real_identity_secret_hash"))
224225
} else {
225-
println!("DUPLICATE message ID detected! Reveal identity secret hash: {leaked_identity_secret_hash}");
226+
println!(
227+
"DUPLICATE message ID detected! Reveal identity secret hash: ********"
228+
);
226229
self.local_identities.remove(&user_index);
227230
println!("User index {user_index} has been SLASHED");
228231
Ok(())
229232
}
230233
} else {
231-
Err(eyre!(
232-
"user identity secret hash {leaked_identity_secret_hash} not found"
233-
))
234+
Err(eyre!("user identity secret hash ******** not found"))
234235
}
235236
}
236237
Err(err) => Err(eyre!("Failed to recover identity secret: {err}")),

rln-wasm/tests/browser.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ mod tests {
77
use rln::hashers::{hash_to_field, poseidon_hash};
88
use rln::poseidon_tree::PoseidonTree;
99
use rln::protocol::{prepare_verify_input, rln_witness_from_values, serialize_witness};
10-
use rln::utils::{bytes_le_to_fr, fr_to_bytes_le};
10+
use rln::utils::{bytes_le_to_fr, fr_to_bytes_le, IdSecret};
1111
use rln_wasm::{
1212
wasm_generate_rln_proof_with_witness, wasm_key_gen, wasm_new, wasm_rln_witness_to_json,
1313
wasm_verify_with_roots,
@@ -121,7 +121,7 @@ mod tests {
121121
// Generate identity pair for other benchmarks
122122
let mem_keys = wasm_key_gen(rln_instance).expect("Failed to generate keys");
123123
let id_key = mem_keys.subarray(0, 32);
124-
let (identity_secret_hash, _) = bytes_le_to_fr(&id_key.to_vec());
124+
let (identity_secret_hash, _) = IdSecret::from_bytes_le(&id_key.to_vec());
125125
let (id_commitment, _) = bytes_le_to_fr(&mem_keys.subarray(32, 64).to_vec());
126126

127127
let epoch = hash_to_field(b"test-epoch");

rln-wasm/tests/node.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ mod tests {
88
use rln::hashers::{hash_to_field, poseidon_hash};
99
use rln::poseidon_tree::PoseidonTree;
1010
use rln::protocol::{prepare_verify_input, rln_witness_from_values, serialize_witness};
11-
use rln::utils::{bytes_le_to_fr, fr_to_bytes_le};
11+
use rln::utils::{bytes_le_to_fr, fr_to_bytes_le, IdSecret};
1212
use rln_wasm::{
1313
wasm_generate_rln_proof_with_witness, wasm_key_gen, wasm_new, wasm_rln_witness_to_json,
1414
wasm_verify_with_roots,
@@ -86,7 +86,7 @@ mod tests {
8686
// Generate identity pair for other benchmarks
8787
let mem_keys = wasm_key_gen(rln_instance).expect("Failed to generate keys");
8888
let id_key = mem_keys.subarray(0, 32);
89-
let (identity_secret_hash, _) = bytes_le_to_fr(&id_key.to_vec());
89+
let (identity_secret_hash, _) = IdSecret::from_bytes_le(&id_key.to_vec());
9090
let (id_commitment, _) = bytes_le_to_fr(&mem_keys.subarray(32, 64).to_vec());
9191

9292
let epoch = hash_to_field(b"test-epoch");

rln/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ rand = "0.8.5"
5353
rand_chacha = "0.3.1"
5454
ruint = { version = "1.15.0", features = ["rand", "serde", "ark-ff-04"] }
5555
tiny-keccak = { version = "2.0.2", features = ["keccak"] }
56-
utils = { path = "../utils", package = "zerokit_utils", version = "0.6.0", default-features = false }
56+
zeroize = "1.8"
57+
utils = { package = "zerokit_utils", version = "0.6.0", path = "../utils", default-features = false }
5758

5859
# serialization
5960
prost = "0.13.5"

rln/src/circuit/iden3calc.rs

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,28 +8,49 @@ pub mod storage;
88
use ruint::aliases::U256;
99
use std::collections::HashMap;
1010
use storage::deserialize_witnesscalc_graph;
11+
use zeroize::zeroize_flat_type;
1112

13+
use crate::circuit::iden3calc::graph::fr_to_u256;
1214
use crate::circuit::Fr;
13-
use graph::{fr_to_u256, Node};
15+
use crate::utils::FrOrSecret;
16+
use graph::Node;
1417

1518
pub type InputSignalsInfo = HashMap<String, (usize, usize)>;
1619

17-
pub fn calc_witness<I: IntoIterator<Item = (String, Vec<Fr>)>>(
20+
pub fn calc_witness<I: IntoIterator<Item = (String, Vec<FrOrSecret>)>>(
1821
inputs: I,
1922
graph_data: &[u8],
2023
) -> Vec<Fr> {
21-
let inputs: HashMap<String, Vec<U256>> = inputs
24+
let mut inputs: HashMap<String, Vec<U256>> = inputs
2225
.into_iter()
23-
.map(|(key, value)| (key, value.iter().map(fr_to_u256).collect()))
26+
.map(|(key, value)| {
27+
(
28+
key,
29+
value
30+
.iter()
31+
.map(|f_| match f_ {
32+
FrOrSecret::IdSecret(s) => s.to_u256(),
33+
FrOrSecret::Fr(f) => fr_to_u256(f),
34+
})
35+
.collect(),
36+
)
37+
})
2438
.collect();
2539

2640
let (nodes, signals, input_mapping): (Vec<Node>, Vec<usize>, InputSignalsInfo) =
2741
deserialize_witnesscalc_graph(std::io::Cursor::new(graph_data)).unwrap();
2842

2943
let mut inputs_buffer = get_inputs_buffer(get_inputs_size(&nodes));
3044
populate_inputs(&inputs, &input_mapping, &mut inputs_buffer);
31-
32-
graph::evaluate(&nodes, inputs_buffer.as_slice(), &signals)
45+
if let Some(v) = inputs.get_mut("identitySecret") {
46+
// ~== v[0] = U256::ZERO;
47+
unsafe { zeroize_flat_type(v) };
48+
}
49+
let res = graph::evaluate(&nodes, inputs_buffer.as_slice(), &signals);
50+
inputs_buffer.iter_mut().for_each(|i| {
51+
unsafe { zeroize_flat_type(i) };
52+
});
53+
res
3354
}
3455

3556
fn get_inputs_size(nodes: &[Node]) -> usize {

rln/src/circuit/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ use crate::circuit::iden3calc::calc_witness;
2020
#[cfg(feature = "arkzkey")]
2121
use {ark_ff::Field, ark_serialize::CanonicalDeserialize, ark_serialize::CanonicalSerialize};
2222

23+
use crate::utils::FrOrSecret;
2324
#[cfg(not(feature = "arkzkey"))]
2425
use {crate::circuit::zkey::read_zkey, std::io::Cursor};
2526

@@ -84,7 +85,7 @@ pub fn zkey_from_folder() -> &'static (ProvingKey<Curve>, ConstraintMatrices<Fr>
8485
&ZKEY
8586
}
8687

87-
pub fn calculate_rln_witness<I: IntoIterator<Item = (String, Vec<Fr>)>>(
88+
pub fn calculate_rln_witness<I: IntoIterator<Item = (String, Vec<FrOrSecret>)>>(
8889
inputs: I,
8990
graph_data: &[u8],
9091
) -> Vec<Fr> {

0 commit comments

Comments
 (0)