proof verification working

This commit is contained in:
psippl 2022-02-01 02:31:07 +01:00
parent 9cc93c98e3
commit e7958440ef
8 changed files with 67 additions and 30 deletions

3
Cargo.lock generated
View File

@ -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",

View File

@ -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"

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -1,5 +1,5 @@
mod identity;
mod proof;
mod protocol;
mod merkle_tree;
mod poseidon_tree;
mod hash;

View File

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

View File

@ -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<BigInt> {
fn merkle_proof_to_vec(proof: &merkle_tree::Proof<PoseidonHash>) -> Vec<BigInt> {
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<PoseidonHash>,
external_nullifier: BigInt,
signal: &[u8],
) -> Result<()> {
// ) -> Result<Proof<Bn<Parameters>>, 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(&params.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<Bn<Parameters>>) -> Result<()> {
// let mut file = File::open("./snarkfiles/semaphore.zkey").unwrap();
// let (params, matrices) = read_zkey(&mut file).unwrap();
// let pvk = prepare_verifying_key(&params.vk);
// // let inputs = &full_assignment[1..num_inputs];
// let verified = ark_groth16::verify_proof(&pvk, proof, inputs).unwrap();
// // assert!(verified);
// Ok(())
// }