feat(rln): Add IncrementalMerkleTree to RLN

- Sapling-based version
- Also Update ffi with get_root
This commit is contained in:
Oskar Thoren 2022-03-17 16:00:50 +08:00
parent a0d35de625
commit 6881a078d6
No known key found for this signature in database
GPG Key ID: B2ECCFD3BC2EF77E
3 changed files with 47 additions and 1 deletions

View File

@ -61,3 +61,4 @@ blake2 = "0.8.1"
# TODO Remove this and use arkworks instead # TODO Remove this and use arkworks instead
sapling-crypto = { package = "sapling-crypto_ce", version = "0.1.3", default-features = false } sapling-crypto = { package = "sapling-crypto_ce", version = "0.1.3", default-features = false }
bellman = { package = "bellman_ce", version = "0.3.4", default-features = false }

View File

@ -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<u8> = 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)] #[allow(clippy::not_unsafe_ptr_arg_deref)]
#[no_mangle] #[no_mangle]
pub extern "C" fn new_circuit(ctx: *mut *mut RLN) -> bool { pub extern "C" fn new_circuit(ctx: *mut *mut RLN) -> bool {

View File

@ -1,3 +1,6 @@
use crate::merkle::IncrementalMerkleTree;
use crate::poseidon::{Poseidon as PoseidonHasher, PoseidonParams};
use ark_circom::{CircomBuilder, CircomCircuit, CircomConfig}; use ark_circom::{CircomBuilder, CircomCircuit, CircomConfig};
use ark_std::rand::thread_rng; use ark_std::rand::thread_rng;
@ -17,9 +20,18 @@ use num_bigint::BigInt;
use serde::Deserialize; use serde::Deserialize;
use serde_json; 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. <E: Engine> not <Bn254>
// NOTE Bn254 vs Bn256 mismatch! Tree is originally Bn256
// TODO Figure out Bn254 vs Bn256 mismatch
pub struct RLN { pub struct RLN {
circom: CircomCircuit<Bn254>, circom: CircomCircuit<Bn254>,
params: ProvingKey<Bn254>, params: ProvingKey<Bn254>,
// TODO Replace Bn256 with Bn254 here
tree: IncrementalMerkleTree<Bn256>,
} }
#[derive(Debug, Deserialize)] #[derive(Debug, Deserialize)]
@ -141,7 +153,26 @@ impl RLN {
println!("Public inputs {:#?} ", inputs); println!("Public inputs {:#?} ", inputs);
RLN { circom, params } // TODO Add as parameter(s)
let merkle_depth: usize = 3;
// XXX
let poseidon_params = PoseidonParams::<Bn256>::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<W: Write>(&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 // TODO Input Read