Skip to content

Commit 1db3048

Browse files
committed
refactor: continue address PR13's comments
1 parent 89bad1a commit 1db3048

15 files changed

Lines changed: 111 additions & 44 deletions

File tree

rln/src/circuit/backend.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,17 @@ use crate::circuit::Fr;
88

99
/// The arkworks Groth16 proving backend over the RLN circuit with a specific hash function `H`.
1010
#[derive(Debug, Clone)]
11-
pub struct ArkGroth16Backend<H: ZerokitHasher<Scalar = Fr>> {
11+
pub struct ArkGroth16Backend<H> {
1212
pub(crate) zkey: Arc<Zkey>,
1313
pub(crate) graph: Arc<Graph>,
1414
pub(crate) pvk: ArkPreparedVerifyingKey<Curve>,
1515
_hasher: PhantomData<H>,
1616
}
1717

18-
impl<H: ZerokitHasher<Scalar = Fr>> ArkGroth16Backend<H> {
18+
impl<H> ArkGroth16Backend<H>
19+
where
20+
H: ZerokitHasher<Scalar = Fr>,
21+
{
1922
pub fn new(zkey: impl Into<Arc<Zkey>>, graph: impl Into<Arc<Graph>>) -> Self {
2023
let zkey = zkey.into();
2124
let graph = graph.into();

rln/src/circuit/iden3calc/graph.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ pub(crate) fn u256_to_fr(x: &U256) -> Result<Fr, String> {
4242
.ok_or_else(|| "Failed to convert U256 to Fr".to_string())
4343
}
4444

45-
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
45+
#[derive(Debug, Clone, Copy, PartialEq, Hash, Serialize, Deserialize)]
4646
pub(crate) enum Operation {
4747
Mul,
4848
Div,
@@ -168,7 +168,7 @@ impl From<&Operation> for proto::DuoOp {
168168
}
169169
}
170170

171-
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
171+
#[derive(Debug, Clone, Copy, PartialEq, Hash, Serialize, Deserialize)]
172172
pub(crate) enum UnoOperation {
173173
Neg,
174174
Id, // identity - just return self
@@ -203,7 +203,7 @@ impl From<&UnoOperation> for proto::UnoOp {
203203
}
204204
}
205205

206-
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
206+
#[derive(Debug, Clone, Copy, PartialEq, Hash, Serialize, Deserialize)]
207207
pub(crate) enum TresOperation {
208208
TernCond,
209209
}
@@ -230,7 +230,7 @@ impl From<&TresOperation> for proto::TresOp {
230230
}
231231
}
232232

233-
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
233+
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
234234
pub(crate) enum Node {
235235
Input(usize),
236236
Constant(U256),

rln/src/circuit/iden3calc/storage.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,10 @@ struct WriteBackReader<R: Read> {
306306
buffer: Vec<u8>,
307307
}
308308

309-
impl<R: Read> WriteBackReader<R> {
309+
impl<R> WriteBackReader<R>
310+
where
311+
R: Read,
312+
{
310313
fn new(reader: R) -> Self {
311314
WriteBackReader {
312315
reader,
@@ -315,7 +318,10 @@ impl<R: Read> WriteBackReader<R> {
315318
}
316319
}
317320

318-
impl<R: Read> Read for WriteBackReader<R> {
321+
impl<R> Read for WriteBackReader<R>
322+
where
323+
R: Read,
324+
{
319325
fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
320326
if buf.is_empty() {
321327
return Ok(0);
@@ -347,7 +353,10 @@ impl<R: Read> Read for WriteBackReader<R> {
347353
}
348354
}
349355

350-
impl<R: Read> Write for WriteBackReader<R> {
356+
impl<R> Write for WriteBackReader<R>
357+
where
358+
R: Read,
359+
{
351360
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
352361
self.buffer.reserve(buf.len());
353362
self.buffer.extend(buf.iter().rev());

rln/src/hashers.rs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ const ROUND_PARAMS: [(usize, usize, usize, usize); 8] = [
2626
static POSEIDON: LazyLock<Poseidon<Fr>> = LazyLock::new(|| Poseidon::from(&ROUND_PARAMS));
2727

2828
/// The Poseidon hash function over the Bn254 scalar field.
29-
#[derive(Clone, Copy, PartialEq, Eq)]
29+
#[derive(Clone, Copy, PartialEq)]
3030
pub struct PoseidonHash;
3131

3232
impl ZerokitHasher for PoseidonHash {
@@ -44,7 +44,10 @@ impl ZerokitHasher for PoseidonHash {
4444
/// For example, `Hasher::<PoseidonHash>::hash_pair(left, right)`.
4545
pub struct Hasher<H>(PhantomData<H>);
4646

47-
impl<H: ZerokitHasher<Scalar = Fr>> Hasher<H> {
47+
impl<H> Hasher<H>
48+
where
49+
H: ZerokitHasher<Scalar = Fr>,
50+
{
4851
/// Hashes a single field element.
4952
pub fn hash_single(input: Fr) -> Fr {
5053
H::hash(&[input])
@@ -61,9 +64,12 @@ impl<H: ZerokitHasher<Scalar = Fr>> Hasher<H> {
6164
}
6265
}
6366

64-
/// Hashes arbitrary signal to the underlying prime field.
67+
/// Hashes an arbitrary-length signal to the prime field.
68+
/// Keccak-256 digest reduced little-endian modulo the field order.
69+
///
70+
/// Keccak-256 is used because this mapping runs outside the circuit: the circuit only
71+
/// consumes the resulting field elements (signal `x`, `epoch`, `rln_identifier`).
6572
pub fn hash_to_field_le(signal: &[u8]) -> Fr {
66-
// We hash the input signal using Keccak256
6773
let mut hash = [0; 32];
6874
let mut hasher = Keccak::v256();
6975
hasher.update(signal);
@@ -72,9 +78,12 @@ pub fn hash_to_field_le(signal: &[u8]) -> Fr {
7278
Fr::from_le_bytes_mod_order(&hash)
7379
}
7480

75-
/// Hashes arbitrary signal to the underlying prime field.
81+
/// Hashes an arbitrary-length signal to the prime field.
82+
/// Keccak-256 digest reduced big-endian modulo the field order.
83+
///
84+
/// Keccak-256 is used because this mapping runs outside the circuit: the circuit only
85+
/// consumes the resulting field elements (signal `x`, `epoch`, `rln_identifier`).
7686
pub fn hash_to_field_be(signal: &[u8]) -> Fr {
77-
// We hash the input signal using Keccak256
7887
let mut hash = [0; 32];
7988
let mut hasher = Keccak::v256();
8089
hasher.update(signal);

rln/src/partial_proof.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@ pub struct PartialAssignment<F: PrimeField> {
2020
pub values: Vec<Option<F>>,
2121
}
2222

23-
impl<F: PrimeField> PartialAssignment<F> {
23+
impl<F> PartialAssignment<F>
24+
where
25+
F: PrimeField,
26+
{
2427
/// Creates a new partial assignment.
2528
pub fn new(values: Vec<Option<F>>) -> Self {
2629
Self { values }
@@ -47,7 +50,11 @@ pub struct Groth16Partial<E: Pairing, QAP: R1CSToQAP = LibsnarkReduction> {
4750
_p: PhantomData<(E, QAP)>,
4851
}
4952

50-
impl<E: Pairing, QAP: R1CSToQAP> Groth16Partial<E, QAP> {
53+
impl<E, QAP> Groth16Partial<E, QAP>
54+
where
55+
E: Pairing,
56+
QAP: R1CSToQAP,
57+
{
5158
/// Precompute a partial proof from a partial assignment.
5259
pub fn prove_partial(
5360
pk: &ProvingKey<E>,

rln/src/protocol/zk.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,10 @@ pub trait RLNPartialZkProof: RLNZkProof {
6969
) -> Result<(Self::Proof, Self::Values), Self::FinishProofError>;
7070
}
7171

72-
impl<H: ZerokitHasher<Scalar = Fr>> RLNZkProof for ArkGroth16Backend<H> {
72+
impl<H> RLNZkProof for ArkGroth16Backend<H>
73+
where
74+
H: ZerokitHasher<Scalar = Fr>,
75+
{
7376
type Hasher = H;
7477
type Witness = RLNWitnessInput;
7578
type Values = RLNProofValues;
@@ -132,7 +135,10 @@ impl<H: ZerokitHasher<Scalar = Fr>> RLNZkProof for ArkGroth16Backend<H> {
132135
}
133136
}
134137

135-
impl<H: ZerokitHasher<Scalar = Fr>> RLNPartialZkProof for ArkGroth16Backend<H> {
138+
impl<H> RLNPartialZkProof for ArkGroth16Backend<H>
139+
where
140+
H: ZerokitHasher<Scalar = Fr>,
141+
{
136142
type PartialWitness = RLNPartialWitnessInput;
137143
type PartialProof = PartialProof;
138144
type GeneratePartialProofError = GenerateProofError;

rln/src/public.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,10 @@ where
160160
}
161161
}
162162

163-
impl<State, ZkProof: RLNZkProof> RLN<State, ZkProof> {
163+
impl<State, ZkProof> RLN<State, ZkProof>
164+
where
165+
ZkProof: RLNZkProof,
166+
{
164167
pub fn generate_proof(
165168
&self,
166169
witness: &ZkProof::Witness,
@@ -177,7 +180,10 @@ impl<State, ZkProof: RLNZkProof> RLN<State, ZkProof> {
177180
}
178181
}
179182

180-
impl<State, ZkProof: RLNPartialZkProof> RLN<State, ZkProof> {
183+
impl<State, ZkProof> RLN<State, ZkProof>
184+
where
185+
ZkProof: RLNPartialZkProof,
186+
{
181187
pub fn generate_partial_proof(
182188
&self,
183189
partial_witness: &ZkProof::PartialWitness,

rln/tests/partial_proof.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@ struct MulCircuit<F: Field> {
1818
b: Option<F>,
1919
}
2020

21-
impl<ConstraintF: Field> ConstraintSynthesizer<ConstraintF> for MulCircuit<ConstraintF> {
21+
impl<ConstraintF> ConstraintSynthesizer<ConstraintF> for MulCircuit<ConstraintF>
22+
where
23+
ConstraintF: Field,
24+
{
2225
fn generate_constraints(
2326
self,
2427
cs: ConstraintSystemRef<ConstraintF>,

utils/benches/merkle_tree_benchmark.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ use zerokit_utils::merkle_tree::{
77
ZerokitMerkleProof, ZerokitMerkleTree,
88
};
99

10-
#[derive(Clone, Copy, PartialEq, Eq)]
10+
#[derive(Clone, Copy, PartialEq)]
1111
struct Keccak256;
1212

13-
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
13+
#[derive(Debug, Clone, Copy, PartialEq, Default)]
1414
struct TestFr([u8; 32]);
1515

1616
impl ZerokitHasher for Keccak256 {

utils/src/hasher.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::fmt::Debug;
55
/// Defines the interface for a hash function over a prime field, used by all zerokit modules.
66
pub trait ZerokitHasher {
77
/// Type of the hashed elements, also used as the Merkle tree node type.
8-
type Scalar: Debug + Copy + Eq + Default + Send + Sync;
8+
type Scalar: Debug + Copy + PartialEq + Default + Send + Sync;
99

1010
/// Hashes an arbitrary-length slice of field elements to a single field element.
1111
fn hash(input: &[Self::Scalar]) -> Self::Scalar;

0 commit comments

Comments
 (0)