From 0d087e3f0a4373c25dd9187418e5670c5b77e2d7 Mon Sep 17 00:00:00 2001 From: Oskar Thoren Date: Fri, 18 Mar 2022 14:32:54 +0800 Subject: [PATCH] test(rln): end to end test wip --- rln/src/lib.rs | 59 ++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 48 insertions(+), 11 deletions(-) diff --git a/rln/src/lib.rs b/rln/src/lib.rs index b206498..8ceaed1 100644 --- a/rln/src/lib.rs +++ b/rln/src/lib.rs @@ -7,6 +7,9 @@ pub mod public; use ark_bn254::{Fr, Parameters}; use ark_ec::bn::Bn; +pub mod protocol; +pub mod circuit; + pub type Field = Fr; pub type Groth16Proof = ark_groth16::Proof>; pub type EthereumGroth16Proof = ark_circom::ethereum::Proof; @@ -20,7 +23,8 @@ mod test { use super::*; use hex_literal::hex; use num_bigint::BigInt; - use semaphore::{hash::Hash, identity::Identity, poseidon_tree::PoseidonTree, protocol::*}; + use semaphore::{hash::Hash, identity::Identity, poseidon_tree::PoseidonTree}; + use crate::protocol::*; #[test] fn test_merkle_proof() { @@ -36,23 +40,56 @@ mod test { tree.set(0, id.commitment().into()); let merkle_proof = tree.proof(0).expect("proof should exist"); - let root: Hash = tree.root().into(); + let root: Field = tree.root().into(); + + println!("Root: {:#}", root); + println!("Merkle proof: {:#?}", merkle_proof); + } + + #[ignore] + #[test] + fn test_end_to_end() { + const LEAF: Hash = Hash::from_bytes_be(hex!( + "0000000000000000000000000000000000000000000000000000000000000000" + )); + + // generate identity + let id = Identity::new(b"hello"); + + // generate merkle tree + let mut tree = PoseidonTree::new(21, LEAF); + tree.set(0, id.commitment().into()); + + let merkle_proof = tree.proof(0).expect("proof should exist"); + let root = tree.root().into(); println!("Root: {:#}", root); println!("Merkle proof: {:#?}", merkle_proof); - // TODO Expand this test to cover RLN end to end + // change signal and external_nullifier here + let signal = b"xxx"; + let external_nullifier = b"appId"; - // // change signal and external_nullifier here - // let signal = b"xxx"; - // let external_nullifier = b"appId"; + let external_nullifier_hash = hash_external_nullifier(external_nullifier); + let nullifier_hash = generate_nullifier_hash(&id, external_nullifier_hash); - // let external_nullifier_hash = hash_external_nullifier(external_nullifier); - // let nullifier_hash = generate_nullifier_hash(&id, external_nullifier_hash); + let proof = generate_proof(&id, &merkle_proof, external_nullifier, signal).unwrap(); - // let proof = generate_proof(&id, &merkle_proof, external_nullifier, signal).unwrap(); + println!("Proof: {:#?}", proof); - // let success = - // verify_proof(root, nullifier_hash, signal, external_nullifier, &proof).unwrap(); + // TODO Make this test pass + // + // Currently fails at: + // thread 'test::test_end_to_end' panicked at 'called `Result::unwrap()` + // on an `Err` value: SynthesisError(MalformedVerifyingKey)', + // rln/src/lib.rs:62:84 + // + // Not sure why this is MalformedVerifyingKey, though the proof is + // likely incorrect with wrong fields in protocol.rs + // + // Indeed: + // if (public_inputs.len() + 1) != pvk.vk.gamma_abc_g1.len() { + let success = + verify_proof(root, nullifier_hash, signal, external_nullifier, &proof).unwrap(); } }