Skip to content

Commit

Permalink
s/id_commitments/rate_commitments (#205)
Browse files Browse the repository at this point in the history
* feat: rename public, protocol and update tests to use rate_commitments

* fix: into()
  • Loading branch information
rymnc authored Aug 18, 2023
1 parent c488185 commit ea1ac53
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 33 deletions.
12 changes: 7 additions & 5 deletions rln/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,18 +102,20 @@ let mut buffer = Cursor::new(Vec::<u8>::new());
rln.key_gen(&mut buffer).unwrap();

// We deserialize the keygen output to obtain
// the identiy_secret and id_commitment
// the identity_secret and id_commitment
let (identity_secret_hash, id_commitment) = deserialize_identity_pair(buffer.into_inner());
```

### Add ID commitment to the RLN Merkle tree
### Add Rate commitment to the RLN Merkle tree

```rust
// We define the tree index where id_commitment will be added
let id_index = 10;
let user_message_limit = 10;

// We serialize id_commitment and pass it to set_leaf
let mut buffer = Cursor::new(serialize_field_element(id_commitment));
let rate_commitment = poseidon_hash(&[id_commitment, user_message_limit]);
let mut buffer = Cursor::new(serialize_field_element(rate_commitment));
rln.set_leaf(id_index, &mut buffer).unwrap();
```

Expand Down Expand Up @@ -141,11 +143,11 @@ let signal = b"RLN is awesome";

We prepare the input to the proof generation routine.

Input buffer is serialized as `[ identity_key | id_index | epoch | signal_len | signal ]`.
Input buffer is serialized as `[ identity_key | id_index | epoch | rln_identifier | user_message_limit | message_id | signal_len | signal ]`.

```rust
// We prepare input to the proof generation routine
let proof_input = prepare_prove_input(identity_secret_hash, id_index, epoch, signal);
let proof_input = prepare_prove_input(identity_secret_hash, id_index, epoch, rln_identifier, user_message_limit, message_id, signal);
```

We are now ready to generate a RLN ZK proof along with the _public outputs_ of the ZK circuit evaluation.
Expand Down
52 changes: 33 additions & 19 deletions rln/src/public.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,11 +238,14 @@ impl RLN<'_> {
/// // We generate a random identity secret hash and commitment pair
/// let (identity_secret_hash, id_commitment) = keygen();
///
/// // We define the tree index where id_commitment will be added
/// // We define the tree index where rate_commitment will be added
/// let id_index = 10;
/// let user_message_limit = 1;
///
/// // We serialize id_commitment and pass it to set_leaf
/// let mut buffer = Cursor::new(serialize_field_element(id_commitment));
/// let rate_commitment = poseidon_hash(&[id_commitment, user_message_limit]);
///
/// // We serialize rate_commitment and pass it to set_leaf
/// let mut buffer = Cursor::new(serialize_field_element(rate_commitment));
/// rln.set_leaf(id_index, &mut buffer).unwrap();
/// ```
pub fn set_leaf<R: Read>(&mut self, index: usize, mut input_data: R) -> Result<()> {
Expand Down Expand Up @@ -270,7 +273,7 @@ impl RLN<'_> {
/// let id_index = 10;
/// let mut buffer = Cursor::new(Vec::<u8>::new());
/// rln.get_leaf(id_index, &mut buffer).unwrap();
/// let id_commitment = deserialize_field_element(&buffer.into_inner()).unwrap();
/// let rate_commitment = deserialize_field_element(&buffer.into_inner()).unwrap();
pub fn get_leaf<W: Write>(&self, index: usize, mut output_data: W) -> Result<()> {
// We get the leaf at input index
let leaf = self.tree.get(index)?;
Expand Down Expand Up @@ -305,7 +308,8 @@ impl RLN<'_> {
/// let mut rng = thread_rng();
/// for _ in 0..no_of_leaves {
/// let (_, id_commitment) = keygen();
/// leaves.push(id_commitment);
/// let rate_commitment = poseidon_hash(&[id_commitment, 1.into()]);
/// leaves.push(rate_commitment);
/// }
///
/// // We add leaves in a batch into the tree
Expand Down Expand Up @@ -366,7 +370,8 @@ impl RLN<'_> {
/// let mut rng = thread_rng();
/// for _ in 0..no_of_leaves {
/// let (_, id_commitment) = keygen();
/// leaves.push(id_commitment);
/// let rate_commitment = poseidon_hash(&[id_commitment, 1.into()]);
/// leaves.push(rate_commitment);
/// }
///
/// let mut indices: Vec<u8> = Vec::new();
Expand Down Expand Up @@ -432,7 +437,8 @@ impl RLN<'_> {
/// let mut rng = thread_rng();
/// for _ in 0..no_of_leaves {
/// let (_, id_commitment) = keygen();
/// leaves.push(id_commitment);
/// let rate_commitment = poseidon_hash(&[id_commitment, 1.into()]);
/// leaves.push(rate_commitment);
/// }
///
/// // We add leaves in a batch into the tree
Expand All @@ -442,9 +448,10 @@ impl RLN<'_> {
/// // We set 256 leaves starting from index 10: next_index value is now max(0, 256+10) = 266
///
/// // We set a leaf on next available index
/// // id_commitment will be set at index 266
/// // rate_commitment will be set at index 266
/// let (_, id_commitment) = keygen();
/// let mut buffer = Cursor::new(fr_to_bytes_le(&id_commitment));
/// let rate_commitment = poseidon_hash(&[id_commitment, 1.into()]);
/// let mut buffer = Cursor::new(fr_to_bytes_le(&rate_commitment));
/// rln.set_next_leaf(&mut buffer).unwrap();
/// ```
pub fn set_next_leaf<R: Read>(&mut self, mut input_data: R) -> Result<()> {
Expand Down Expand Up @@ -681,9 +688,10 @@ impl RLN<'_> {
/// // Generate identity pair
/// let (identity_secret_hash, id_commitment) = keygen();
///
/// // We set as leaf id_commitment after storing its index
/// // We set as leaf rate_commitment after storing its index
/// let identity_index = 10;
/// let mut buffer = Cursor::new(fr_to_bytes_le(&id_commitment));
/// let rate_commitment = poseidon_hash(&[id_commitment, 1.into()]);
/// let mut buffer = Cursor::new(fr_to_bytes_le(&rate_commitment));
/// rln.set_leaf(identity_index, &mut buffer).unwrap();
///
/// // We generate a random signal
Expand Down Expand Up @@ -1682,9 +1690,11 @@ mod test {
// Generate identity pair
let (identity_secret_hash, id_commitment) = keygen();

// We set as leaf id_commitment after storing its index
// We set as leaf rate_commitment after storing its index
let identity_index = rln.tree.leaves_set();
let mut buffer = Cursor::new(fr_to_bytes_le(&id_commitment));
let user_message_limit = Fr::from(100);
let rate_commitment = utils_poseidon_hash(&[id_commitment, user_message_limit]);
let mut buffer = Cursor::new(fr_to_bytes_le(&rate_commitment));
rln.set_next_leaf(&mut buffer).unwrap();

// We generate a random signal
Expand All @@ -1702,7 +1712,7 @@ mod test {
serialized.append(&mut normalize_usize(identity_index));
serialized.append(&mut fr_to_bytes_le(&epoch));
serialized.append(&mut fr_to_bytes_le(&rln_identifier));
serialized.append(&mut fr_to_bytes_le(&Fr::from(100)));
serialized.append(&mut fr_to_bytes_le(&user_message_limit));
serialized.append(&mut fr_to_bytes_le(&Fr::from(1)));
serialized.append(&mut normalize_usize(signal.len()));
serialized.append(&mut signal.to_vec());
Expand Down Expand Up @@ -1751,9 +1761,11 @@ mod test {
// Generate identity pair
let (identity_secret_hash, id_commitment) = keygen();

// We set as leaf id_commitment after storing its index
// We set as leaf rate_commitment after storing its index
let identity_index = rln.tree.leaves_set();
let mut buffer = Cursor::new(fr_to_bytes_le(&id_commitment));
let user_message_limit = Fr::from(100);
let rate_commitment = utils_poseidon_hash(&[id_commitment, user_message_limit]);
let mut buffer = Cursor::new(fr_to_bytes_le(&rate_commitment));
rln.set_next_leaf(&mut buffer).unwrap();

// We generate a random signal
Expand All @@ -1772,7 +1784,7 @@ mod test {
serialized.append(&mut normalize_usize(identity_index));
serialized.append(&mut fr_to_bytes_le(&epoch));
serialized.append(&mut fr_to_bytes_le(&rln_identifier));
serialized.append(&mut fr_to_bytes_le(&Fr::from(100)));
serialized.append(&mut fr_to_bytes_le(&user_message_limit));
serialized.append(&mut fr_to_bytes_le(&Fr::from(1)));
serialized.append(&mut normalize_usize(signal.len()));
serialized.append(&mut signal.to_vec());
Expand Down Expand Up @@ -1855,7 +1867,9 @@ mod test {

// We set as leaf id_commitment after storing its index
let identity_index = rln.tree.leaves_set();
let mut buffer = Cursor::new(fr_to_bytes_le(&id_commitment));
let user_message_limit = Fr::from(100);
let rate_commitment = utils_poseidon_hash(&[id_commitment, user_message_limit]);
let mut buffer = Cursor::new(fr_to_bytes_le(&rate_commitment));
rln.set_next_leaf(&mut buffer).unwrap();

// We generate a random signal
Expand All @@ -1874,7 +1888,7 @@ mod test {
serialized.append(&mut normalize_usize(identity_index));
serialized.append(&mut fr_to_bytes_le(&epoch));
serialized.append(&mut fr_to_bytes_le(&rln_identifier));
serialized.append(&mut fr_to_bytes_le(&Fr::from(100)));
serialized.append(&mut fr_to_bytes_le(&user_message_limit));
serialized.append(&mut fr_to_bytes_le(&Fr::from(1)));
serialized.append(&mut normalize_usize(signal.len()));
serialized.append(&mut signal.to_vec());
Expand Down
2 changes: 1 addition & 1 deletion rln/tests/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ mod test {

// We first add leaves one by one specifying the index
for (i, leaf) in leaves.iter().enumerate() {
// We prepare id_commitment and we set the leaf at provided index
// We prepare the rate_commitment and we set the leaf at provided index
let leaf_ser = fr_to_bytes_le(&leaf);
let input_buffer = &Buffer::from(leaf_ser.as_ref());
let success = set_leaf(rln_pointer, i, input_buffer);
Expand Down
11 changes: 7 additions & 4 deletions rln/tests/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ mod test {
// generate identity
let identity_secret_hash = hash_to_field(b"test-merkle-proof");
let id_commitment = poseidon_hash(&vec![identity_secret_hash]);
let rate_commitment = poseidon_hash(&[id_commitment, 1.into()]);

// generate merkle tree
let default_leaf = Fr::from(0);
Expand All @@ -186,7 +187,7 @@ mod test {
ConfigOf::<PoseidonTree>::default(),
)
.unwrap();
tree.set(leaf_index, id_commitment.into()).unwrap();
tree.set(leaf_index, rate_commitment.into()).unwrap();

// We check correct computation of the root
let root = tree.root();
Expand Down Expand Up @@ -347,7 +348,7 @@ mod test {
assert_eq!(identity_path_index, expected_identity_path_index);

// We check correct verification of the proof
assert!(tree.verify(&id_commitment, &merkle_proof).unwrap());
assert!(tree.verify(&rate_commitment, &merkle_proof).unwrap());
}

#[test]
Expand Down Expand Up @@ -392,6 +393,8 @@ mod test {

// Generate identity pair
let (identity_secret_hash, id_commitment) = keygen();
let user_message_limit = Fr::from(100);
let rate_commitment = poseidon_hash(&[id_commitment, user_message_limit]);

//// generate merkle tree
let default_leaf = Fr::from(0);
Expand All @@ -401,7 +404,7 @@ mod test {
ConfigOf::<PoseidonTree>::default(),
)
.unwrap();
tree.set(leaf_index, id_commitment.into()).unwrap();
tree.set(leaf_index, rate_commitment.into()).unwrap();

let merkle_proof = tree.proof(leaf_index).expect("proof should exist");

Expand All @@ -418,7 +421,7 @@ mod test {
x,
epoch,
rln_identifier,
Fr::from(100),
user_message_limit,
Fr::from(1),
);

Expand Down
14 changes: 10 additions & 4 deletions rln/tests/public.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ mod test {
fn test_merkle_proof() {
let tree_height = TEST_TREE_HEIGHT;
let leaf_index = 3;
let user_message_limit = 1;

let input_buffer =
Cursor::new(json!({ "resources_folder": TEST_RESOURCES_FOLDER }).to_string());
Expand All @@ -23,9 +24,10 @@ mod test {
// generate identity
let identity_secret_hash = hash_to_field(b"test-merkle-proof");
let id_commitment = utils_poseidon_hash(&vec![identity_secret_hash]);
let rate_commitment = utils_poseidon_hash(&[id_commitment, 1.into()]);

// We pass id_commitment as Read buffer to RLN's set_leaf
let mut buffer = Cursor::new(fr_to_bytes_le(&id_commitment));
// We pass rate_commitment as Read buffer to RLN's set_leaf
let mut buffer = Cursor::new(fr_to_bytes_le(&rate_commitment));
rln.set_leaf(leaf_index, &mut buffer).unwrap();

// We check correct computation of the root
Expand Down Expand Up @@ -192,8 +194,12 @@ mod test {
assert_eq!(identity_path_index, expected_identity_path_index);

// We double check that the proof computed from public API is correct
let root_from_proof =
compute_tree_root(&id_commitment, &path_elements, &identity_path_index, false);
let root_from_proof = compute_tree_root(
&rate_commitment,
&path_elements,
&identity_path_index,
false,
);

assert_eq!(root, root_from_proof);
}
Expand Down

0 comments on commit ea1ac53

Please sign in to comment.