test(rln): end to end test wip

This commit is contained in:
Oskar Thoren 2022-03-18 14:32:54 +08:00
parent 1e8e834887
commit 0d087e3f0a
No known key found for this signature in database
GPG Key ID: B2ECCFD3BC2EF77E
1 changed files with 48 additions and 11 deletions

View File

@ -7,6 +7,9 @@ pub mod public;
use ark_bn254::{Fr, Parameters}; use ark_bn254::{Fr, Parameters};
use ark_ec::bn::Bn; use ark_ec::bn::Bn;
pub mod protocol;
pub mod circuit;
pub type Field = Fr; pub type Field = Fr;
pub type Groth16Proof = ark_groth16::Proof<Bn<Parameters>>; pub type Groth16Proof = ark_groth16::Proof<Bn<Parameters>>;
pub type EthereumGroth16Proof = ark_circom::ethereum::Proof; pub type EthereumGroth16Proof = ark_circom::ethereum::Proof;
@ -20,7 +23,8 @@ mod test {
use super::*; use super::*;
use hex_literal::hex; use hex_literal::hex;
use num_bigint::BigInt; 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] #[test]
fn test_merkle_proof() { fn test_merkle_proof() {
@ -36,23 +40,56 @@ mod test {
tree.set(0, id.commitment().into()); tree.set(0, id.commitment().into());
let merkle_proof = tree.proof(0).expect("proof should exist"); 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!("Root: {:#}", root);
println!("Merkle proof: {:#?}", merkle_proof); 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 external_nullifier_hash = hash_external_nullifier(external_nullifier);
// let signal = b"xxx"; let nullifier_hash = generate_nullifier_hash(&id, external_nullifier_hash);
// let external_nullifier = b"appId";
// let external_nullifier_hash = hash_external_nullifier(external_nullifier); let proof = generate_proof(&id, &merkle_proof, external_nullifier, signal).unwrap();
// let nullifier_hash = generate_nullifier_hash(&id, external_nullifier_hash);
// let proof = generate_proof(&id, &merkle_proof, external_nullifier, signal).unwrap(); println!("Proof: {:#?}", proof);
// let success = // TODO Make this test pass
// verify_proof(root, nullifier_hash, signal, external_nullifier, &proof).unwrap(); //
// 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();
} }
} }