diff --git a/Cargo.lock b/Cargo.lock index 82d15f1..2cc63a1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2161,7 +2161,10 @@ version = "0.1.0" dependencies = [ "ark-bn254", "ark-circom", + "ark-ec", + "ark-ff", "ark-groth16", + "ark-relations", "ark-std", "color-eyre", "ethers", diff --git a/Cargo.toml b/Cargo.toml index 647f152..a48170a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,6 +10,9 @@ ark-circom = { git = "https://github.com/philsippl/ark-circom", features=["circo ark-std = { version = "0.3.0", default-features = false, features = ["parallel"] } ark-bn254 = { version = "0.3.0" } ark-groth16 = { git = "https://github.com/arkworks-rs/groth16", rev = "765817f", features = ["parallel"] } +ark-ec = { version = "0.3.0", default-features = false, features = ["parallel"] } +ark-ff = { version = "0.3.0", default-features = false, features = ["parallel", "asm"] } +ark-relations = { version = "0.3.0", default-features = false } num-bigint = { version = "0.4", default-features = false, features = ["rand"] } ff = { package="ff_ce", version="0.11"} poseidon-rs = "0.0.8" diff --git a/snarkfiles/semaphore.r1cs b/snarkfiles/semaphore.r1cs deleted file mode 100644 index 39748df..0000000 Binary files a/snarkfiles/semaphore.r1cs and /dev/null differ diff --git a/snarkfiles/semaphore.wasm b/snarkfiles/semaphore.wasm index db56da8..495dbf0 100644 Binary files a/snarkfiles/semaphore.wasm and b/snarkfiles/semaphore.wasm differ diff --git a/snarkfiles/semaphore.zkey b/snarkfiles/semaphore.zkey index 76a9de4..df83946 100644 Binary files a/snarkfiles/semaphore.zkey and b/snarkfiles/semaphore.zkey differ diff --git a/src/lib.rs b/src/lib.rs index 820090f..ed16c34 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,5 +1,5 @@ mod identity; -mod proof; +mod protocol; mod merkle_tree; mod poseidon_tree; mod hash; diff --git a/src/main.rs b/src/main.rs index 7fb40d8..8f3134a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,4 @@ -mod proof; +mod protocol; mod identity; mod merkle_tree; mod poseidon_tree; @@ -7,7 +7,7 @@ mod hash; use num_bigint::BigInt; use poseidon_rs::Poseidon; use hex_literal::hex; -use {identity::*, poseidon_tree::*, hash::*, proof::*}; +use {identity::*, poseidon_tree::*, hash::*, protocol::*}; fn main() { @@ -21,29 +21,22 @@ fn main() { "0000000000000000000000000000000000000000000000000000000000000000" )); - let mut tree = PoseidonTree::new(3, LEAF); + let mut tree = PoseidonTree::new(21, LEAF); let (_, leaf) = id.identity_commitment().to_bytes_be(); dbg!(&leaf); - tree.set(2, leaf.into()); + tree.set(0, leaf.into()); let root: BigInt = tree.root().into(); dbg!(root); - let proof = tree.proof(2).expect("proof should exist"); + let proof = tree.proof(0).expect("proof should exist"); - dbg!(proof.path_index()); + dbg!(&proof); - // let proof: Vec = proof.0.iter().map(|x| { - // match x { - // Branch::Left(value) => value.into(), - // Branch::Right(value) => value.into(), - // } - // }).collect(); + dbg!(&proof.path_index()); - // dbg!(proof); - - generate(&id, &proof, BigInt::from(123), b"xxx"); + generate_proof(&id, &proof, BigInt::from(123), b"xxx"); } diff --git a/src/proof.rs b/src/protocol.rs similarity index 59% rename from src/proof.rs rename to src/protocol.rs index 5e04459..3cf9c7a 100644 --- a/src/proof.rs +++ b/src/protocol.rs @@ -1,18 +1,18 @@ -use ark_bn254::Bn254; +use ark_bn254::{Bn254}; use ark_circom::{read_zkey, CircomReduction, WitnessCalculator}; +use ark_ff::{Fp256}; +use ark_relations::r1cs::SynthesisError; use ark_std::rand::thread_rng; use color_eyre::Result; use ethers::utils::keccak256; use num_bigint::{BigInt, Sign}; - use std::{collections::HashMap, fs::File, ops::Shr}; +use ark_groth16::{create_proof_with_reduction_and_matrices, prepare_verifying_key, Proof}; -use crate::{identity::*, merkle_tree::Branch, poseidon_tree::Proof}; - -use ark_groth16::{create_proof_with_reduction_and_matrices, prepare_verifying_key, verify_proof}; +use crate::{identity::*, merkle_tree::{Branch, self}, poseidon_tree::{PoseidonHash}}; // TODO: we should create a From trait for this -fn proof_to_vec(proof: &Proof) -> Vec { +fn merkle_proof_to_vec(proof: &merkle_tree::Proof) -> Vec { proof .0 .iter() @@ -28,12 +28,13 @@ fn hash_signal(signal: &[u8]) -> BigInt { } // WIP: uses dummy proofs for now -pub fn generate( +pub fn generate_proof( identity: &Identity, - merkle_proof: &Proof, + merkle_proof: &merkle_tree::Proof, external_nullifier: BigInt, signal: &[u8], ) -> Result<()> { +// ) -> Result>, SynthesisError> { let mut file = File::open("./snarkfiles/semaphore.zkey").unwrap(); let (params, matrices) = read_zkey(&mut file).unwrap(); let num_inputs = matrices.num_instance_variables; @@ -51,13 +52,29 @@ pub fn generate( vec![identity.trapdoor.clone()], ); inputs.insert("identity_path_index".to_string(), merkle_proof.path_index()); - inputs.insert("path_elements".to_string(), proof_to_vec(merkle_proof)); + inputs.insert("path_elements".to_string(), merkle_proof_to_vec(merkle_proof)); inputs.insert("external_nullifier".to_string(), vec![external_nullifier]); inputs.insert("signal_hash".to_string(), vec![hash_signal(signal)]); inputs }; + dbg!(&inputs); + + let nullifier = BigInt::parse_bytes( + b"2073423254391230197488930967618194527029511360562414420050239137722181518699", + 10, + ) + .unwrap(); + + let root = BigInt::parse_bytes( + b"9194628565321423830640339892337438998798131617576196335312343809896770847079", + 10, + ) + .unwrap(); + + dbg!(nullifier.sign(), root.sign()); + let mut wtns = WitnessCalculator::new("./snarkfiles/semaphore.wasm").unwrap(); let full_assignment = wtns @@ -82,8 +99,7 @@ pub fn generate( num_inputs, num_constraints, full_assignment.as_slice(), - ) - .unwrap(); + ); let elapsed = now.elapsed(); println!("proof generation took: {:.2?}", elapsed); @@ -91,10 +107,32 @@ pub fn generate( dbg!(&proof); let pvk = prepare_verifying_key(¶ms.vk); - let inputs = &full_assignment[1..num_inputs]; - let verified = verify_proof(&pvk, &proof, inputs).unwrap(); - assert!(verified); + let public_inputs = vec![ + Fp256::from(root.to_biguint().unwrap()), + Fp256::from(nullifier.to_biguint().unwrap()), + full_assignment[3], + full_assignment[4] + ]; + dbg!(&public_inputs); + + let verified = ark_groth16::verify_proof(&pvk, &proof.unwrap(), &public_inputs).unwrap(); + + dbg!(verified); + + // proof Ok(()) } + +// fn verify_proof(nullifier_hash: BigInt, root: BigInt, proof: &Proof>) -> Result<()> { +// let mut file = File::open("./snarkfiles/semaphore.zkey").unwrap(); +// let (params, matrices) = read_zkey(&mut file).unwrap(); + +// let pvk = prepare_verifying_key(¶ms.vk); +// // let inputs = &full_assignment[1..num_inputs]; +// let verified = ark_groth16::verify_proof(&pvk, proof, inputs).unwrap(); + +// // assert!(verified); +// Ok(()) +// }