Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tests: add end to end test for proof generation and verify #17

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added zk_prover/proofs/proof_0
Binary file not shown.
Binary file added zk_prover/proofs/proof_1
Binary file not shown.
Binary file added zk_prover/proofs/proof_10
Binary file not shown.
Binary file added zk_prover/proofs/proof_11
Binary file not shown.
Binary file added zk_prover/proofs/proof_12
Binary file not shown.
Binary file added zk_prover/proofs/proof_13
Binary file not shown.
Binary file added zk_prover/proofs/proof_14
Binary file not shown.
Binary file added zk_prover/proofs/proof_15
Binary file not shown.
Binary file added zk_prover/proofs/proof_2
Binary file not shown.
Binary file added zk_prover/proofs/proof_3
Binary file not shown.
Binary file added zk_prover/proofs/proof_4
Binary file not shown.
Binary file added zk_prover/proofs/proof_5
Binary file not shown.
Binary file added zk_prover/proofs/proof_6
Binary file not shown.
Binary file added zk_prover/proofs/proof_7
Binary file not shown.
Binary file added zk_prover/proofs/proof_8
Binary file not shown.
Binary file added zk_prover/proofs/proof_9
Binary file not shown.
68 changes: 67 additions & 1 deletion zk_prover/src/circuits/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,17 @@ mod test {
use halo2_proofs::{
dev::{FailureLocation, MockProver, VerifyFailure},
halo2curves::bn256::Fr as Fp,
plonk::Any,
plonk::{create_proof, verify_proof, Any},
poly::kzg::{
multiopen::{ProverSHPLONK, VerifierSHPLONK},
strategy::SingleStrategy,
},
transcript::TranscriptWriterBuffer,
};
use halo2_solidity_verifier::Keccak256Transcript;
use num_bigint::ToBigUint;
use rand::rngs::OsRng;
use std::{fs::File, io::Write, path::Path};

const N_CURRENCIES: usize = 2;
const LEVELS: usize = 4;
Expand Down Expand Up @@ -458,4 +466,62 @@ mod test {
.render(K, &circuit, &root)
.unwrap();
}

#[test]
fn test_end_to_end_proof_and_verify() {
let merkle_sum_tree =
MerkleSumTree::<N_CURRENCIES, N_BYTES>::from_csv("../csv/entry_16.csv").unwrap();

for user_index in 0..16 {
// get merkle proof for entry ˆuser_indexˆ
// let user_index = 0;
let merkle_proof = merkle_sum_tree.generate_proof(user_index).unwrap();

let circuit = MstInclusionCircuit::<LEVELS, N_CURRENCIES, N_BYTES>::init(merkle_proof);
// generate a universal trusted setup for testing, along with the verification key (vk) and the proving key (pk).
let (params, pk, _) = generate_setup_artifacts(
11,
Some("../backend/ptau/hermez-raw-11"),
circuit.clone(),
)
.unwrap();
let instances_clone = circuit.instances().clone();

// generate proof
let proof = {
let mut transcript = Keccak256Transcript::new(Vec::new());
let proof_creation_result = create_proof::<_, ProverSHPLONK<_>, _, _, _, _>(
&params,
&pk,
&[circuit],
&[&[&instances_clone[0]]],
&mut OsRng,
&mut transcript,
);
assert!(proof_creation_result.is_ok());
transcript.finalize()
};

// write proof to file
let proof_path = format!("{}_{}", "./proofs/proof", user_index);
File::create(Path::new(&proof_path))
.expect("Failed to create proof file")
.write_all(&proof[..])
.expect("Failed to write proof");
println!("Proof written to: {}", proof_path);

// verify proof
let result = {
let mut transcript = Keccak256Transcript::new(proof.as_slice());
verify_proof::<_, VerifierSHPLONK<_>, _, _, SingleStrategy<_>>(
&params,
pk.get_vk(),
SingleStrategy::new(&params),
&[&[&instances_clone[0]]],
&mut transcript,
)
};
assert!(result.is_ok());
}
}
}
Loading