diff --git a/rln/Cargo.toml b/rln/Cargo.toml index 49ee035..e0973ef 100644 --- a/rln/Cargo.toml +++ b/rln/Cargo.toml @@ -61,3 +61,4 @@ blake2 = "0.8.1" # TODO Remove this and use arkworks instead sapling-crypto = { package = "sapling-crypto_ce", version = "0.1.3", default-features = false } +bellman = { package = "bellman_ce", version = "0.3.4", default-features = false } diff --git a/rln/src/ffi.rs b/rln/src/ffi.rs index 4c04401..64d1833 100644 --- a/rln/src/ffi.rs +++ b/rln/src/ffi.rs @@ -28,6 +28,20 @@ impl<'a> From<&Buffer> for &'a [u8] { } } +#[allow(clippy::not_unsafe_ptr_arg_deref)] +#[no_mangle] +pub extern "C" fn get_root(ctx: *const RLN, output_buffer: *mut Buffer) -> bool { + let rln = unsafe { &*ctx }; + let mut output_data: Vec = Vec::new(); + match rln.get_root(&mut output_data) { + Ok(_) => true, + Err(_) => false, + }; + unsafe { *output_buffer = Buffer::from(&output_data[..]) }; + std::mem::forget(output_data); + true +} + #[allow(clippy::not_unsafe_ptr_arg_deref)] #[no_mangle] pub extern "C" fn new_circuit(ctx: *mut *mut RLN) -> bool { diff --git a/rln/src/public.rs b/rln/src/public.rs index da12198..09b04b6 100644 --- a/rln/src/public.rs +++ b/rln/src/public.rs @@ -1,3 +1,6 @@ +use crate::merkle::IncrementalMerkleTree; +use crate::poseidon::{Poseidon as PoseidonHasher, PoseidonParams}; + use ark_circom::{CircomBuilder, CircomCircuit, CircomConfig}; use ark_std::rand::thread_rng; @@ -17,9 +20,18 @@ use num_bigint::BigInt; use serde::Deserialize; use serde_json; +// XXX +use bellman::pairing::ff::{Field, PrimeField, PrimeFieldRepr, ScalarEngine}; +use sapling_crypto::bellman::pairing::bn256::Bn256; + +// TODO Add Engine here? i.e. not +// NOTE Bn254 vs Bn256 mismatch! Tree is originally Bn256 +// TODO Figure out Bn254 vs Bn256 mismatch pub struct RLN { circom: CircomCircuit, params: ProvingKey, + // TODO Replace Bn256 with Bn254 here + tree: IncrementalMerkleTree, } #[derive(Debug, Deserialize)] @@ -141,7 +153,26 @@ impl RLN { println!("Public inputs {:#?} ", inputs); - RLN { circom, params } + // TODO Add as parameter(s) + let merkle_depth: usize = 3; + // XXX + let poseidon_params = PoseidonParams::::new(8, 55, 3, None, None, None); + let hasher = PoseidonHasher::new(poseidon_params.clone()); + let tree = IncrementalMerkleTree::empty(hasher, merkle_depth); + + RLN { + circom, + params, + tree, + } + } + + /// returns current membership root + /// * `root` is a scalar field element in 32 bytes + pub fn get_root(&self, mut result_data: W) -> io::Result<()> { + let root = self.tree.get_root(); + root.into_repr().write_le(&mut result_data)?; + Ok(()) } // TODO Input Read