From 075a755af85e2c1f9a2f3071dcc73aceee7704c9 Mon Sep 17 00:00:00 2001 From: Oskar Thoren Date: Wed, 1 Jun 2022 13:11:15 +0100 Subject: [PATCH] test(semaphore): Import basic end to end test --- semaphore/src/lib.rs | 38 +++++++++++++++++++++++++++++++++++--- 1 file changed, 35 insertions(+), 3 deletions(-) diff --git a/semaphore/src/lib.rs b/semaphore/src/lib.rs index 1b4a90c..19d56d3 100644 --- a/semaphore/src/lib.rs +++ b/semaphore/src/lib.rs @@ -1,8 +1,40 @@ #[cfg(test)] mod tests { + use semaphore::{ + hash_to_field, identity::Identity, poseidon_tree::PoseidonTree, protocol::*, Field, + }; + #[test] - fn it_works() { - let result = 2 + 2; - assert_eq!(result, 4); + fn test_semaphore() { + // generate identity + let id = Identity::from_seed(b"secret"); + + // generate merkle tree + let leaf = Field::from(0); + let mut tree = PoseidonTree::new(21, leaf); + tree.set(0, id.commitment()); + + let merkle_proof = tree.proof(0).expect("proof should exist"); + let root = tree.root().into(); + + // change signal and external_nullifier here + let signal_hash = hash_to_field(b"xxx"); + let external_nullifier_hash = hash_to_field(b"appId"); + + let nullifier_hash = generate_nullifier_hash(&id, external_nullifier_hash); + + let proof = + generate_proof(&id, &merkle_proof, external_nullifier_hash, signal_hash).unwrap(); + + let success = verify_proof( + root, + nullifier_hash, + signal_hash, + external_nullifier_hash, + &proof, + ) + .unwrap(); + + assert!(success); } }