Embed circuit

This commit is contained in:
Remco Bloemen 2022-03-11 16:01:52 -08:00
parent 3de4debd55
commit 7501a56b4c
4 changed files with 36 additions and 16 deletions

View File

@ -44,6 +44,7 @@ proptest = { version = "1.0", optional = true }
rayon = "1.5.1"
serde = "1.0"
sha2 = "0.10.1"
tempfile = "3.3.0"
thiserror = "1.0.0"
tiny-keccak = { version = "2.0.2", optional = true }
zkp-u256 = { version = "0.2", optional = true }

26
src/circuit.rs Normal file
View File

@ -0,0 +1,26 @@
use ark_bn254::{Bn254, Fr};
use ark_circom::{read_zkey, WitnessCalculator};
use ark_groth16::ProvingKey;
use ark_relations::r1cs::ConstraintMatrices;
use core::include_bytes;
use once_cell::sync::Lazy;
use std::io::{Cursor, Write};
use tempfile::NamedTempFile;
const ZKEY_BYTES: &'static [u8] = include_bytes!("../semaphore/build/snark/semaphore_final.zkey");
const WASM: &'static [u8] = include_bytes!("../semaphore/build/snark/semaphore.wasm");
pub(crate) static ZKEY: Lazy<(ProvingKey<Bn254>, ConstraintMatrices<Fr>)> = Lazy::new(|| {
let mut reader = Cursor::new(ZKEY_BYTES);
read_zkey(&mut reader).expect("zkey should be valid")
});
pub(crate) static WITNESS_CALCULATOR: Lazy<WitnessCalculator> = Lazy::new(|| {
// HACK: ark-circom requires a file, so we make one!
let mut tmpfile = NamedTempFile::new().expect("Failed to create temp file");
tmpfile.write(WASM).expect("Failed to write to temp file");
let path = tmpfile.into_temp_path();
let result = WitnessCalculator::new(&path).expect("Failed to create witness calculator");
path.close().expect("Could not remove tempfile");
result
});

View File

@ -3,6 +3,7 @@
// TODO: ark-circom and ethers-core pull in a lot of deps, some duplicate.
#![allow(clippy::multiple_crate_versions)]
mod circuit;
pub mod hash;
pub mod identity;
pub mod merkle_tree;

View File

@ -1,4 +1,5 @@
use crate::{
circuit::{WITNESS_CALCULATOR, ZKEY},
identity::Identity,
merkle_tree::{self, Branch},
poseidon_tree::PoseidonHash,
@ -89,11 +90,6 @@ pub fn generate_proof(
external_nullifier: &[u8],
signal: &[u8],
) -> Result<Proof<Bn<Parameters>>, ProofError> {
let mut file = File::open(&config.zkey)?;
let (params, matrices) = read_zkey(&mut file)?;
let num_inputs = matrices.num_instance_variables;
let num_constraints = matrices.num_constraints;
let external_nullifier = hash_external_nullifier(external_nullifier);
let signal = hash_signal(signal);
let inputs = [
@ -117,9 +113,8 @@ pub fn generate_proof(
let now = Instant::now();
let mut witness = WitnessCalculator::new(&config.wasm).map_err(ProofError::WitnessError)?;
let full_assignment = witness
let full_assignment = WITNESS_CALCULATOR
.clone()
.calculate_witness_element::<Bn254, _>(inputs, false)
.map_err(ProofError::WitnessError)?;
@ -134,12 +129,12 @@ pub fn generate_proof(
let now = Instant::now();
let proof = create_proof_with_reduction_and_matrices::<_, CircomReduction>(
&params,
&ZKEY.0,
r,
s,
&matrices,
num_inputs,
num_constraints,
&ZKEY.1,
ZKEY.1.num_instance_variables,
ZKEY.1.num_constraints,
full_assignment.as_slice(),
)?;
@ -162,10 +157,7 @@ pub fn verify_proof(
external_nullifier: &[u8],
proof: &Proof<Bn<Parameters>>,
) -> Result<bool, ProofError> {
let mut file = File::open(&config.zkey)?;
let (params, _) = read_zkey(&mut file)?;
let pvk = prepare_verifying_key(&params.vk);
let pvk = prepare_verifying_key(&ZKEY.0.vk);
let public_inputs = vec![
root,