refactor(rln): Use PoseidonTree

Some LE stuff to sort
This commit is contained in:
Oskar Thoren 2022-03-17 19:15:11 +08:00
parent 6881a078d6
commit 3314aba6cc
No known key found for this signature in database
GPG Key ID: B2ECCFD3BC2EF77E
1 changed files with 22 additions and 9 deletions

View File

@ -1,5 +1,7 @@
use crate::merkle::IncrementalMerkleTree; use crate::merkle::IncrementalMerkleTree;
use crate::poseidon::{Poseidon as PoseidonHasher, PoseidonParams}; use crate::poseidon::{Poseidon as PoseidonHasher, PoseidonParams};
use crate::poseidon_tree::PoseidonTree;
use crate::hash::Hash;
use ark_circom::{CircomBuilder, CircomCircuit, CircomConfig}; use ark_circom::{CircomBuilder, CircomCircuit, CircomConfig};
use ark_std::rand::thread_rng; use ark_std::rand::thread_rng;
@ -31,7 +33,8 @@ pub struct RLN {
circom: CircomCircuit<Bn254>, circom: CircomCircuit<Bn254>,
params: ProvingKey<Bn254>, params: ProvingKey<Bn254>,
// TODO Replace Bn256 with Bn254 here // TODO Replace Bn256 with Bn254 here
tree: IncrementalMerkleTree<Bn256>, //tree: IncrementalMerkleTree<Bn256>,
tree: PoseidonTree,
} }
#[derive(Debug, Deserialize)] #[derive(Debug, Deserialize)]
@ -153,12 +156,15 @@ impl RLN {
println!("Public inputs {:#?} ", inputs); println!("Public inputs {:#?} ", inputs);
// TODO Add as parameter(s) // Sapling based tree
let merkle_depth: usize = 3; // // TODO Add as parameter(s)
// XXX // let merkle_depth: usize = 3;
let poseidon_params = PoseidonParams::<Bn256>::new(8, 55, 3, None, None, None); // let poseidon_params = PoseidonParams::<Bn256>::new(8, 55, 3, None, None, None);
let hasher = PoseidonHasher::new(poseidon_params.clone()); // let hasher = PoseidonHasher::new(poseidon_params.clone());
let tree = IncrementalMerkleTree::empty(hasher, merkle_depth); // let tree = IncrementalMerkleTree::empty(hasher, merkle_depth);
const LEAF: Hash = Hash::from_bytes_be([0u8; 32]);
let mut tree = PoseidonTree::new(21, LEAF);
RLN { RLN {
circom, circom,
@ -170,8 +176,15 @@ impl RLN {
/// returns current membership root /// returns current membership root
/// * `root` is a scalar field element in 32 bytes /// * `root` is a scalar field element in 32 bytes
pub fn get_root<W: Write>(&self, mut result_data: W) -> io::Result<()> { pub fn get_root<W: Write>(&self, mut result_data: W) -> io::Result<()> {
let root = self.tree.get_root(); //let root = self.tree.get_root();
root.into_repr().write_le(&mut result_data)?; // Converts PrimeFieldRepr into LE
//root.into_repr().write_le(&mut result_data)?;
//returns H::Hash, which is a 256 bit hash value
let root = self.tree.root();
// TODO Return root as LE here
//root.write_le(&mut result_data)?;
println!("NYI: root le write buffer {:#?}", root);
Ok(()) Ok(())
} }