Skip to content

Commit

Permalink
chore: accept tree_config in new_with_params
Browse files Browse the repository at this point in the history
  • Loading branch information
richard-ramos committed Aug 4, 2023
1 parent ecb4d93 commit 1628a69
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 3 deletions.
2 changes: 2 additions & 0 deletions rln/src/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,13 +186,15 @@ pub extern "C" fn new_with_params(
circom_buffer: *const Buffer,
zkey_buffer: *const Buffer,
vk_buffer: *const Buffer,
tree_config: *const Buffer,
ctx: *mut *mut RLN,
) -> bool {
if let Ok(rln) = RLN::new_with_params(
tree_height,
circom_buffer.process().to_vec(),
zkey_buffer.process().to_vec(),
vk_buffer.process().to_vec(),
tree_config.process(),
) {
unsafe { *ctx = Box::into_raw(Box::new(rln)) };
true
Expand Down
30 changes: 27 additions & 3 deletions rln/src/public.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ impl RLN<'_> {
/// - `circom_vec`: a byte vector containing the ZK circuit (`rln.wasm`) as binary file
/// - `zkey_vec`: a byte vector containing to the proving key (`rln_final.zkey`) as binary file
/// - `vk_vec`: a byte vector containing to the verification key (`verification_key.json`) as binary file
/// - `tree_config`: a reader for a string containing a json with the merkle tree configuration
///
/// Example:
/// ```
Expand All @@ -134,29 +135,52 @@ impl RLN<'_> {
/// let mut buffer = vec![0; metadata.len() as usize];
/// file.read_exact(&mut buffer).expect("buffer overflow");
/// resources.push(buffer);
/// let tree_config = "{}".to_string();
/// let tree_config_buffer = &Buffer::from(tree_config.as_bytes());
/// }
///
/// let mut rln = RLN::new_with_params(
/// tree_height,
/// resources[0].clone(),
/// resources[1].clone(),
/// resources[2].clone(),
/// tree_config_buffer,
/// );
/// ```
pub fn new_with_params(
pub fn new_with_params<R: Read>(
tree_height: usize,
#[cfg(not(target_arch = "wasm32"))] circom_vec: Vec<u8>,
zkey_vec: Vec<u8>,
vk_vec: Vec<u8>,
#[cfg(not(target_arch = "wasm32"))] mut tree_config_input: R,
) -> Result<RLN<'static>> {
#[cfg(not(target_arch = "wasm32"))]
let witness_calculator = circom_from_raw(circom_vec)?;

let proving_key = zkey_from_raw(&zkey_vec)?;
let verification_key = vk_from_raw(&vk_vec, &zkey_vec)?;

// We compute a default empty tree
let tree = PoseidonTree::default(tree_height)?;
let tree = if cfg!(not(target_arch = "wasm32")) {
let mut tree_config_vec: Vec<u8> = Vec::new();
tree_config_input.read_to_end(&mut tree_config_vec)?;
let tree_config_str = String::from_utf8(tree_config_vec)?;
let tree_config: <PoseidonTree as ZerokitMerkleTree>::Config =
if tree_config_str.is_empty() {
<PoseidonTree as ZerokitMerkleTree>::Config::default()
} else {
<PoseidonTree as ZerokitMerkleTree>::Config::from_str(&tree_config_str)?
};

// We compute a default empty tree
PoseidonTree::new(
tree_height,
<PoseidonTree as ZerokitMerkleTree>::Hasher::default_leaf(),
tree_config,
)?
} else {
// We compute a default empty tree
PoseidonTree::default(tree_height)?
};

Ok(RLN {
#[cfg(not(target_arch = "wasm32"))]
Expand Down
3 changes: 3 additions & 0 deletions rln/tests/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -617,11 +617,14 @@ mod test {

// Creating a RLN instance passing the raw data
let mut rln_pointer_raw_bytes = MaybeUninit::<*mut RLN>::uninit();
let tree_config = "".to_string();
let tree_config_buffer = &Buffer::from(tree_config.as_bytes());
let success = new_with_params(
tree_height,
circom_data,
zkey_data,
vk_data,
tree_config_buffer,
rln_pointer_raw_bytes.as_mut_ptr(),
);
assert!(success, "RLN object creation failed");
Expand Down

0 comments on commit 1628a69

Please sign in to comment.