test(semaphore): Import basic end to end test

This commit is contained in:
Oskar Thoren 2022-06-01 13:11:15 +01:00
parent bcc38c41e2
commit 075a755af8
No known key found for this signature in database
GPG Key ID: B2ECCFD3BC2EF77E
1 changed files with 35 additions and 3 deletions

View File

@ -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);
}
}