mirror of
https://github.com/vacp2p/zerokit.git
synced 2026-01-10 06:08:09 -05:00
Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b903d8d740 | ||
|
|
f73c83b571 | ||
|
|
a86b859b75 | ||
|
|
f8fc455d08 |
2
Cargo.lock
generated
2
Cargo.lock
generated
@@ -2695,7 +2695,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "rln"
|
||||
version = "0.3.2"
|
||||
version = "0.3.4"
|
||||
dependencies = [
|
||||
"ark-bn254 0.4.0",
|
||||
"ark-circom 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "rln"
|
||||
version = "0.3.2"
|
||||
version = "0.3.4"
|
||||
edition = "2021"
|
||||
license = "MIT OR Apache-2.0"
|
||||
description = "APIs to manage, compute and verify zkSNARK proofs and RLN primitives"
|
||||
|
||||
@@ -261,6 +261,12 @@ pub extern "C" fn get_leaf(ctx: *mut RLN, index: usize, output_buffer: *mut Buff
|
||||
call_with_output_arg!(ctx, get_leaf, output_buffer, index)
|
||||
}
|
||||
|
||||
#[allow(clippy::not_unsafe_ptr_arg_deref)]
|
||||
#[no_mangle]
|
||||
pub extern "C" fn leaves_set(ctx: *mut RLN) -> usize {
|
||||
ctx.process().leaves_set()
|
||||
}
|
||||
|
||||
#[allow(clippy::not_unsafe_ptr_arg_deref)]
|
||||
#[no_mangle]
|
||||
pub extern "C" fn set_next_leaf(ctx: *mut RLN, input_buffer: *const Buffer) -> bool {
|
||||
|
||||
@@ -197,11 +197,13 @@ impl ZerokitMerkleTree for PmTree {
|
||||
let mut indices = indices.into_iter().collect::<Vec<_>>();
|
||||
indices.sort();
|
||||
|
||||
match (leaves.is_empty(), indices.is_empty()) {
|
||||
(true, true) => Err(Report::msg("no leaves or indices to be removed")),
|
||||
(false, true) => self.set_range_with_leaves(start, leaves),
|
||||
(true, false) => self.remove_indices(indices),
|
||||
(false, false) => self.remove_indices_and_set_leaves(start, leaves, indices),
|
||||
match (leaves.len(), indices.len()) {
|
||||
(0, 0) => Err(Report::msg("no leaves or indices to be removed")),
|
||||
(1, 0) => self.set(start, leaves[0]),
|
||||
(0, 1) => self.delete(indices[0]),
|
||||
(_, 0) => self.set_range_with_leaves(start, leaves),
|
||||
(0, _) => self.remove_indices(&indices),
|
||||
(_, _) => self.remove_indices_and_set_leaves(start, leaves, &indices),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -264,18 +266,11 @@ impl PmTree {
|
||||
.map_err(|e| Report::msg(e.to_string()))
|
||||
}
|
||||
|
||||
fn remove_indices(&mut self, indices: Vec<usize>) -> Result<()> {
|
||||
fn remove_indices(&mut self, indices: &[usize]) -> Result<()> {
|
||||
let start = indices[0];
|
||||
let end = indices.last().unwrap() + 1;
|
||||
|
||||
let mut new_leaves: Vec<_> = (start..end)
|
||||
.map(|i| self.tree.get(i))
|
||||
.collect::<Result<_, _>>()?;
|
||||
|
||||
new_leaves
|
||||
.iter_mut()
|
||||
.take(indices.len())
|
||||
.for_each(|leaf| *leaf = PmTreeHasher::default_leaf());
|
||||
let new_leaves = (start..end).map(|_| PmTreeHasher::default_leaf());
|
||||
|
||||
self.tree
|
||||
.set_range(start, new_leaves)
|
||||
@@ -286,16 +281,13 @@ impl PmTree {
|
||||
&mut self,
|
||||
start: usize,
|
||||
leaves: Vec<FrOfPmTreeHasher>,
|
||||
indices: Vec<usize>,
|
||||
indices: &[usize],
|
||||
) -> Result<()> {
|
||||
let min_index = *indices.first().unwrap();
|
||||
let max_index = start + leaves.len();
|
||||
|
||||
// Generated a placeholder with the exact size needed,
|
||||
// Initiated with default values to be overridden throughout the method
|
||||
let mut set_values = vec![PmTreeHasher::default_leaf(); max_index - min_index];
|
||||
|
||||
// If the index is not in indices list, keep the original value
|
||||
for i in min_index..start {
|
||||
if !indices.contains(&i) {
|
||||
let value = self.tree.get(i)?;
|
||||
@@ -303,7 +295,6 @@ impl PmTree {
|
||||
}
|
||||
}
|
||||
|
||||
// Insert new leaves after 'start' position
|
||||
for (i, &leaf) in leaves.iter().enumerate() {
|
||||
set_values[start - min_index + i] = leaf;
|
||||
}
|
||||
|
||||
@@ -143,6 +143,8 @@ mod test {
|
||||
assert!(success, "RLN object creation failed");
|
||||
let rln_pointer = unsafe { &mut *rln_pointer.assume_init() };
|
||||
|
||||
assert_eq!(rln_pointer.leaves_set(), 0);
|
||||
|
||||
// We generate a vector of random leaves
|
||||
let mut leaves: Vec<Fr> = Vec::new();
|
||||
let mut rng = thread_rng();
|
||||
@@ -159,6 +161,7 @@ mod test {
|
||||
let input_buffer = &Buffer::from(leaves_ser.as_ref());
|
||||
let success = init_tree_with_leaves(rln_pointer, input_buffer);
|
||||
assert!(success, "init tree with leaves call failed");
|
||||
assert_eq!(rln_pointer.leaves_set(), no_of_leaves);
|
||||
|
||||
// We get the root of the tree obtained adding leaves in batch
|
||||
let mut output_buffer = MaybeUninit::<Buffer>::uninit();
|
||||
|
||||
Reference in New Issue
Block a user