Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions rln-cli/src/examples/multi_message_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ struct Identity {

impl Identity {
fn new() -> Self {
let (identity_secret, id_commitment) = keygen().unwrap();
let (identity_secret, id_commitment) = keygen();
Identity {
identity_secret,
id_commitment,
Expand Down Expand Up @@ -125,8 +125,7 @@ impl RLNSystem {
let index = self.rln.leaves_set();
let identity = Identity::new();

let rate_commitment =
poseidon_hash(&[identity.id_commitment, Fr::from(MESSAGE_LIMIT)]).unwrap();
let rate_commitment = poseidon_hash(&[identity.id_commitment, Fr::from(MESSAGE_LIMIT)]);
match self.rln.set_next_leaf(rate_commitment) {
Ok(_) => {
println!("Registered user: {index}");
Expand Down Expand Up @@ -195,7 +194,7 @@ impl RLNSystem {
};

let (path_elements, identity_path_index) = self.rln.get_merkle_proof(user_index)?;
let x = hash_to_field_le(signal.as_bytes())?;
let x = hash_to_field_le(signal.as_bytes());

let witness = RLNWitnessInput::new(
identity.identity_secret.clone(),
Expand Down Expand Up @@ -291,9 +290,9 @@ fn main() -> Result<()> {
println!("Initializing RLN multi-message-id instance...");
print!("\x1B[2J\x1B[1;1H");
let mut rln_system = RLNSystem::new()?;
let rln_epoch = hash_to_field_le(b"epoch")?;
let rln_identifier = hash_to_field_le(b"rln-identifier")?;
let external_nullifier = poseidon_hash(&[rln_epoch, rln_identifier]).unwrap();
let rln_epoch = hash_to_field_le(b"epoch");
let rln_identifier = hash_to_field_le(b"rln-identifier");
let external_nullifier = poseidon_hash(&[rln_epoch, rln_identifier]);
println!("RLN Multi-Message-ID Example:");
println!("Message Limit: {MESSAGE_LIMIT}");
println!("Message Slots: {} - MAX_OUT", rln_system.rln.max_out());
Expand Down
13 changes: 6 additions & 7 deletions rln-cli/src/examples/partial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ struct CachedPartialProof {

impl Identity {
fn new() -> Self {
let (identity_secret, id_commitment) = keygen().unwrap();
let (identity_secret, id_commitment) = keygen();
Identity {
identity_secret,
id_commitment,
Expand Down Expand Up @@ -167,8 +167,7 @@ impl RLNSystem {
let index = self.rln.leaves_set();
let identity = Identity::new();

let rate_commitment =
poseidon_hash(&[identity.id_commitment, Fr::from(MESSAGE_LIMIT)]).unwrap();
let rate_commitment = poseidon_hash(&[identity.id_commitment, Fr::from(MESSAGE_LIMIT)]);
match self.rln.set_next_leaf(rate_commitment) {
Ok(_) => {
println!("Registered user: {index}");
Expand Down Expand Up @@ -242,7 +241,7 @@ impl RLNSystem {
None => return Err(format!("User {user_index} not found").into()),
};

let x = hash_to_field_le(signal.as_bytes())?;
let x = hash_to_field_le(signal.as_bytes());
let current_root = self.rln.get_root();

let (witness, partial_proof) = match self.partial_proofs.get(&user_index) {
Expand Down Expand Up @@ -366,9 +365,9 @@ impl RLNSystem {
fn main() -> Result<()> {
println!("Initializing RLN instance...");
print!("\x1B[2J\x1B[1;1H");
let rln_epoch = hash_to_field_le(b"epoch")?;
let rln_identifier = hash_to_field_le(b"rln-identifier")?;
let external_nullifier = poseidon_hash(&[rln_epoch, rln_identifier]).unwrap();
let rln_epoch = hash_to_field_le(b"epoch");
let rln_identifier = hash_to_field_le(b"rln-identifier");
let external_nullifier = poseidon_hash(&[rln_epoch, rln_identifier]);
let mut rln_system = RLNSystem::new(external_nullifier)?;
println!("RLN Partial Proof Example:");
println!("Message Limit: {MESSAGE_LIMIT}");
Expand Down
13 changes: 6 additions & 7 deletions rln-cli/src/examples/relay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ struct Identity {

impl Identity {
fn new() -> Self {
let (identity_secret, id_commitment) = keygen().unwrap();
let (identity_secret, id_commitment) = keygen();
Identity {
identity_secret,
id_commitment,
Expand Down Expand Up @@ -117,8 +117,7 @@ impl RLNSystem {
let index = self.rln.leaves_set();
let identity = Identity::new();

let rate_commitment =
poseidon_hash(&[identity.id_commitment, Fr::from(MESSAGE_LIMIT)]).unwrap();
let rate_commitment = poseidon_hash(&[identity.id_commitment, Fr::from(MESSAGE_LIMIT)]);
match self.rln.set_next_leaf(rate_commitment) {
Ok(_) => {
println!("Registered user: {index}");
Expand Down Expand Up @@ -147,7 +146,7 @@ impl RLNSystem {
};

let (path_elements, identity_path_index) = self.rln.get_merkle_proof(user_index)?;
let x = hash_to_field_le(signal.as_bytes())?;
let x = hash_to_field_le(signal.as_bytes());

let witness = RLNWitnessInput::new(
identity.identity_secret.clone(),
Expand Down Expand Up @@ -230,9 +229,9 @@ fn main() -> Result<()> {
println!("Initializing RLN instance...");
print!("\x1B[2J\x1B[1;1H");
let mut rln_system = RLNSystem::new()?;
let rln_epoch = hash_to_field_le(b"epoch")?;
let rln_identifier = hash_to_field_le(b"rln-identifier")?;
let external_nullifier = poseidon_hash(&[rln_epoch, rln_identifier]).unwrap();
let rln_epoch = hash_to_field_le(b"epoch");
let rln_identifier = hash_to_field_le(b"rln-identifier");
let external_nullifier = poseidon_hash(&[rln_epoch, rln_identifier]);
println!("RLN Relay Example:");
println!("Message Limit: {MESSAGE_LIMIT}");
println!("----------------------------------");
Expand Down
13 changes: 6 additions & 7 deletions rln-cli/src/examples/stateless.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ struct Identity {

impl Identity {
fn new() -> Self {
let (identity_secret, id_commitment) = keygen().unwrap();
let (identity_secret, id_commitment) = keygen();
Identity {
identity_secret,
id_commitment,
Expand Down Expand Up @@ -101,8 +101,7 @@ impl RLNSystem {
let index = self.tree.leaves_set();
let identity = Identity::new();

let rate_commitment =
poseidon_hash(&[identity.id_commitment, Fr::from(MESSAGE_LIMIT)]).unwrap();
let rate_commitment = poseidon_hash(&[identity.id_commitment, Fr::from(MESSAGE_LIMIT)]);
self.tree.update_next(rate_commitment)?;

println!("Registered user: {index}");
Expand All @@ -126,7 +125,7 @@ impl RLNSystem {
};

let merkle_proof = self.tree.proof(user_index)?;
let x = hash_to_field_le(signal.as_bytes())?;
let x = hash_to_field_le(signal.as_bytes());

let witness = RLNWitnessInput::new(
identity.identity_secret.clone(),
Expand Down Expand Up @@ -219,9 +218,9 @@ fn main() -> Result<()> {
println!("Initializing RLN instance...");
print!("\x1B[2J\x1B[1;1H");
let mut rln_system = RLNSystem::new()?;
let rln_epoch = hash_to_field_le(b"epoch")?;
let rln_identifier = hash_to_field_le(b"rln-identifier")?;
let external_nullifier = poseidon_hash(&[rln_epoch, rln_identifier]).unwrap();
let rln_epoch = hash_to_field_le(b"epoch");
let rln_identifier = hash_to_field_le(b"rln-identifier");
let external_nullifier = poseidon_hash(&[rln_epoch, rln_identifier]);
println!("RLN Stateless Example:");
println!("Message Limit: {MESSAGE_LIMIT}");
println!("----------------------------------");
Expand Down
71 changes: 20 additions & 51 deletions rln-wasm/examples/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,7 @@ async function main() {
}

console.log("\nGenerating identity keys");
let identity;
try {
identity = rlnWasm.Identity.generate();
} catch (error) {
console.error("Key generation error:", error);
return;
}
let identity = rlnWasm.Identity.generate();
const identitySecret = identity.getSecretHash();
const idCommitment = identity.getCommitment();
console.log(" - identity generated successfully");
Expand All @@ -92,16 +86,10 @@ async function main() {
console.log(" - user_message_limit = " + userMessageLimit.debug());

console.log("\nComputing rate commitment");
let rateCommitment;
try {
rateCommitment = rlnWasm.Hasher.poseidonHashPair(
idCommitment,
userMessageLimit,
);
} catch (error) {
console.error("Rate commitment hash error:", error);
return;
}
let rateCommitment = rlnWasm.Hasher.poseidonHashPair(
idCommitment,
userMessageLimit,
);
console.log(" - rate_commitment = " + rateCommitment.debug());

console.log("\nWasmFr serialization: WasmFr <-> bytes");
Expand Down Expand Up @@ -150,20 +138,12 @@ async function main() {
const defaultLeaf = rlnWasm.WasmFr.zero();

const defaultHashes = [];
try {
defaultHashes[0] = rlnWasm.Hasher.poseidonHashPair(
defaultLeaf,
defaultLeaf,
defaultHashes[0] = rlnWasm.Hasher.poseidonHashPair(defaultLeaf, defaultLeaf);
for (let i = 1; i < treeDepth - 1; i++) {
defaultHashes[i] = rlnWasm.Hasher.poseidonHashPair(
defaultHashes[i - 1],
defaultHashes[i - 1],
);
for (let i = 1; i < treeDepth - 1; i++) {
defaultHashes[i] = rlnWasm.Hasher.poseidonHashPair(
defaultHashes[i - 1],
defaultHashes[i - 1],
);
}
} catch (error) {
console.error("Poseidon hash error:", error);
return;
}

const pathElements = new rlnWasm.VecWasmFr();
Expand Down Expand Up @@ -206,18 +186,15 @@ async function main() {
console.log("\nComputing Merkle root for stateless mode");
console.log(" - computing root for index 0 with rate_commitment");

let computedRoot;
try {
computedRoot = rlnWasm.Hasher.poseidonHashPair(rateCommitment, defaultLeaf);
for (let i = 1; i < treeDepth; i++) {
computedRoot = rlnWasm.Hasher.poseidonHashPair(
computedRoot,
defaultHashes[i - 1],
);
}
} catch (error) {
console.error("Poseidon hash error:", error);
return;
let computedRoot = rlnWasm.Hasher.poseidonHashPair(
rateCommitment,
defaultLeaf,
);
for (let i = 1; i < treeDepth; i++) {
computedRoot = rlnWasm.Hasher.poseidonHashPair(
computedRoot,
defaultHashes[i - 1],
);
}
console.log(" - computed_root = " + computedRoot.debug());

Expand Down Expand Up @@ -260,13 +237,7 @@ async function main() {
console.log(" - rln_identifier = " + rlnIdentifier.debug());

console.log("\nComputing Poseidon hash for external nullifier");
let externalNullifier;
try {
externalNullifier = rlnWasm.Hasher.poseidonHashPair(epoch, rlnIdentifier);
} catch (error) {
console.error("External nullifier hash error:", error);
return;
}
let externalNullifier = rlnWasm.Hasher.poseidonHashPair(epoch, rlnIdentifier);
console.log(" - external_nullifier = " + externalNullifier.debug());

if (MULTI_MESSAGE_ID) {
Expand Down Expand Up @@ -303,7 +274,6 @@ async function main() {
witness = new rlnWasm.WasmRLNWitnessInput(
identitySecret,
userMessageLimit,
null,
messageIds,
pathElements,
identityPathIndex,
Expand Down Expand Up @@ -511,7 +481,6 @@ async function main() {
witness2 = new rlnWasm.WasmRLNWitnessInput(
identitySecret,
userMessageLimit,
null,
messageIds2,
pathElements,
identityPathIndex,
Expand Down
51 changes: 22 additions & 29 deletions rln-wasm/src/wasm_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,24 +200,18 @@ pub struct Hasher;
#[wasm_bindgen]
impl Hasher {
#[wasm_bindgen(js_name = hashToFieldLE)]
pub fn hash_to_field_le(input: &Uint8Array) -> Result<WasmFr, String> {
hash_to_field_le(&input.to_vec())
.map(WasmFr)
.map_err(|err| err.to_string())
pub fn hash_to_field_le(input: &Uint8Array) -> WasmFr {
WasmFr(hash_to_field_le(&input.to_vec()))
}

#[wasm_bindgen(js_name = hashToFieldBE)]
pub fn hash_to_field_be(input: &Uint8Array) -> Result<WasmFr, String> {
hash_to_field_be(&input.to_vec())
.map(WasmFr)
.map_err(|err| err.to_string())
pub fn hash_to_field_be(input: &Uint8Array) -> WasmFr {
WasmFr(hash_to_field_be(&input.to_vec()))
}

#[wasm_bindgen(js_name = poseidonHashPair)]
pub fn poseidon_hash_pair(a: &WasmFr, b: &WasmFr) -> Result<WasmFr, String> {
poseidon_hash(&[a.0, b.0])
.map(WasmFr)
.map_err(|err| err.to_string())
pub fn poseidon_hash_pair(a: &WasmFr, b: &WasmFr) -> WasmFr {
WasmFr(poseidon_hash_pair(a.0, b.0))
}
}

Expand All @@ -230,23 +224,22 @@ pub struct Identity {
#[wasm_bindgen]
impl Identity {
#[wasm_bindgen(js_name = generate)]
pub fn generate() -> Result<Identity, String> {
let (identity_secret, id_commitment) = keygen().map_err(|err| err.to_string())?;
Ok(Identity {
pub fn generate() -> Identity {
let (identity_secret, id_commitment) = keygen();
Identity {
identity_secret: *identity_secret,
id_commitment,
})
}
}

#[wasm_bindgen(js_name = generateSeeded)]
pub fn generate_seeded(seed: &Uint8Array) -> Result<Identity, String> {
pub fn generate_seeded(seed: &Uint8Array) -> Identity {
let seed_vec = seed.to_vec();
let (identity_secret, id_commitment) =
seeded_keygen(&seed_vec).map_err(|err| err.to_string())?;
Ok(Identity {
let (identity_secret, id_commitment) = seeded_keygen(&seed_vec);
Identity {
identity_secret,
id_commitment,
})
}
}

#[wasm_bindgen(js_name = getSecretHash)]
Expand Down Expand Up @@ -316,28 +309,28 @@ pub struct ExtendedIdentity {
#[wasm_bindgen]
impl ExtendedIdentity {
#[wasm_bindgen(js_name = generate)]
pub fn generate() -> Result<ExtendedIdentity, String> {
pub fn generate() -> ExtendedIdentity {
let (identity_trapdoor, identity_nullifier, identity_secret, id_commitment) =
extended_keygen().map_err(|err| err.to_string())?;
Ok(ExtendedIdentity {
extended_keygen();
ExtendedIdentity {
identity_trapdoor,
identity_nullifier,
identity_secret,
id_commitment,
})
}
}

#[wasm_bindgen(js_name = generateSeeded)]
pub fn generate_seeded(seed: &Uint8Array) -> Result<ExtendedIdentity, String> {
pub fn generate_seeded(seed: &Uint8Array) -> ExtendedIdentity {
let seed_vec = seed.to_vec();
let (identity_trapdoor, identity_nullifier, identity_secret, id_commitment) =
extended_seeded_keygen(&seed_vec).map_err(|err| err.to_string())?;
Ok(ExtendedIdentity {
extended_seeded_keygen(&seed_vec);
ExtendedIdentity {
identity_trapdoor,
identity_nullifier,
identity_secret,
id_commitment,
})
}
}

#[wasm_bindgen(js_name = getTrapdoor)]
Expand Down
Loading
Loading