Skip to content

Commit 211b2d4

Browse files
authored
Add error for return type of compute_id_secret function (#316)
1 parent 5f4bcb7 commit 211b2d4

3 files changed

Lines changed: 23 additions & 13 deletions

File tree

rln/src/error.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,13 @@ pub enum ProtocolError {
4343
InvalidMessageId(Fr, Fr),
4444
}
4545

46+
#[derive(Debug, thiserror::Error)]
47+
pub enum ComputeIdSecretError {
48+
/// Usually it means that the same signal is used to recover the user secret hash
49+
#[error("Cannot recover secret: division by zero")]
50+
DivisionByZero,
51+
}
52+
4653
#[derive(Debug, thiserror::Error)]
4754
pub enum RLNError {
4855
#[error("I/O error: {0}")]
@@ -66,5 +73,5 @@ pub enum RLNError {
6673
#[error("Proof error: {0}")]
6774
Proof(#[from] ProofError),
6875
#[error("Unable to extract secret")]
69-
RecoverSecret,
76+
RecoverSecret(#[from] ComputeIdSecretError),
7077
}

rln/src/protocol.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// This crate collects all the underlying primitives used to implement RLN
22

33
use ark_bn254::Fr;
4+
use ark_ff::AdditiveGroup;
45
use ark_groth16::{prepare_verifying_key, Groth16, Proof as ArkProof, ProvingKey, VerifyingKey};
56
use ark_relations::r1cs::ConstraintMatrices;
67
use ark_serialize::{CanonicalDeserialize, CanonicalSerialize};
@@ -14,7 +15,7 @@ use std::time::Instant;
1415
use tiny_keccak::{Hasher as _, Keccak};
1516

1617
use crate::circuit::{calculate_rln_witness, qap::CircomReduction, Curve};
17-
use crate::error::{ConversionError, ProofError, ProtocolError};
18+
use crate::error::{ComputeIdSecretError, ConversionError, ProofError, ProtocolError};
1819
use crate::hashers::{hash_to_field, poseidon_hash};
1920
use crate::poseidon_tree::*;
2021
use crate::public::RLN_IDENTIFIER;
@@ -508,7 +509,7 @@ pub fn extended_seeded_keygen(signal: &[u8]) -> (Fr, Fr, Fr, Fr) {
508509
)
509510
}
510511

511-
pub fn compute_id_secret(share1: (Fr, Fr), share2: (Fr, Fr)) -> Result<Fr, String> {
512+
pub fn compute_id_secret(share1: (Fr, Fr), share2: (Fr, Fr)) -> Result<Fr, ComputeIdSecretError> {
512513
// Assuming a0 is the identity secret and a1 = poseidonHash([a0, external_nullifier]),
513514
// a (x,y) share satisfies the following relation
514515
// y = a_0 + x * a_1
@@ -518,11 +519,16 @@ pub fn compute_id_secret(share1: (Fr, Fr), share2: (Fr, Fr)) -> Result<Fr, Strin
518519
// If the two input shares were computed for the same external_nullifier and identity secret, we can recover the latter
519520
// y1 = a_0 + x1 * a_1
520521
// y2 = a_0 + x2 * a_1
521-
let a_1 = (y1 - y2) / (x1 - x2);
522-
let a_0 = y1 - x1 * a_1;
523522

524-
// If shares come from the same polynomial, a0 is correctly recovered and a1 = poseidonHash([a0, external_nullifier])
525-
Ok(a_0)
523+
if (x1 - x2) != Fr::ZERO {
524+
let a_1 = (y1 - y2) / (x1 - x2);
525+
let a_0 = y1 - x1 * a_1;
526+
527+
// If shares come from the same polynomial, a0 is correctly recovered and a1 = poseidonHash([a0, external_nullifier])
528+
Ok(a_0)
529+
} else {
530+
Err(ComputeIdSecretError::DivisionByZero)
531+
}
526532
}
527533

528534
///////////////////////////////////////////////////////

rln/src/public.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1310,14 +1310,11 @@ impl RLN {
13101310
let share2 = (proof_values_2.x, proof_values_2.y);
13111311

13121312
// We recover the secret
1313-
let recovered_identity_secret_hash = compute_id_secret(share1, share2);
1313+
let recovered_identity_secret_hash =
1314+
compute_id_secret(share1, share2).map_err(RLNError::RecoverSecret)?;
13141315

13151316
// If an identity secret hash is recovered, we write it to output_data, otherwise nothing will be written.
1316-
if let Ok(identity_secret_hash) = recovered_identity_secret_hash {
1317-
output_data.write_all(&fr_to_bytes_le(&identity_secret_hash))?;
1318-
} else {
1319-
return Err(RLNError::RecoverSecret);
1320-
}
1317+
output_data.write_all(&fr_to_bytes_le(&recovered_identity_secret_hash))?;
13211318
}
13221319

13231320
Ok(())

0 commit comments

Comments
 (0)