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

chore: verify inplace #270

Merged
merged 2 commits into from
Jul 26, 2024
Merged
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
22 changes: 20 additions & 2 deletions groth16/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,24 @@ pub fn groth16_verify(
Ok(())
}

#[cfg(not(any(feature = "cuda", feature = "opencl")))]
pub fn groth16_verify_inplace<E: Engine + crate::json_utils::Parser>(
vk: VerifyingKey<E>,
public_input_file: &str,
proof_file: &str,
) -> Result<()> {
let inputs: Vec<E::Fr> = read_public_input_from_file::<E::Fr>(public_input_file)?;
let proof = read_proof_from_file(proof_file)?;
let verification_result =
Groth16::<_, CircomCircuit<E>>::verify_with_processed_vk(&vk, &inputs[..], &proof);

if verification_result.is_err() || !verification_result.unwrap() {
bail!("verify failed");
}

Ok(())
}

#[cfg(any(feature = "cuda", feature = "opencl"))]
pub fn groth16_verify(
curve_type: &str,
Expand Down Expand Up @@ -553,7 +571,7 @@ pub fn create_circuit_add_witness(
}

#[cfg(not(any(feature = "cuda", feature = "opencl")))]
fn read_pk_from_file<E: Engine>(file_path: &str, checked: bool) -> Result<Parameters<E>> {
pub fn read_pk_from_file<E: Engine>(file_path: &str, checked: bool) -> Result<Parameters<E>> {
let file =
std::fs::File::open(file_path).map_err(|e| anyhow!("Open {}, {:?}", file_path, e))?;
let mut reader = std::io::BufReader::new(file);
Expand All @@ -574,7 +592,7 @@ where
Ok(Parameters::<E>::read(&mut reader, checked)?)
}

fn read_vk_from_file<P: Parser>(file_path: &str) -> Result<VerifyingKey<P>> {
pub fn read_vk_from_file<P: Parser>(file_path: &str) -> Result<VerifyingKey<P>> {
let json_data = std::fs::read_to_string(file_path)?;
Ok(to_verification_key::<P>(&json_data))
}
Expand Down
42 changes: 41 additions & 1 deletion groth16/src/groth16.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,13 +110,14 @@ impl<E: Engine, C: Circuit<E>> Groth16<E, C> {
#[cfg(test)]
#[cfg(not(any(feature = "cuda", feature = "opencl")))]
mod tests {
use anyhow::Ok;
use franklin_crypto::bellman::{Field, PrimeField};
use num_traits::Zero;

use super::*;
use crate::api::create_circuit_add_witness;
use crate::api::groth16_setup_inplace;
use crate::api::SetupResult;
use crate::api::{groth16_prove_inplace, groth16_setup_inplace, groth16_verify_inplace};
use crate::bellman_ce::bls12_381::Bls12;
use crate::bellman_ce::bn256::{Bn256, Fr};
use algebraic::circom_circuit::CircomCircuit;
Expand Down Expand Up @@ -224,6 +225,45 @@ mod tests {

Ok(())
}

#[test]
fn groth16_api_proof_inpace() -> Result<()> {
//1. SRS
let t = std::time::Instant::now();
let curve_type = "BLS12381";
let public_input_file = "/tmp/public_input.json";
let proof_file = "/tmp/proof.json";
let setup_result = groth16_setup_inplace(curve_type, CIRCUIT_FILE_BLS12)?;
let (circuit, pk, vk) = match setup_result {
SetupResult::BLS12381(circuit, pk, vk) => (circuit, pk, vk),
_ => panic!("Expected BLS12381 setup result"),
};
let elapsed = t.elapsed().as_secs_f64();
println!("1-groth16-bls12381 setup run time: {} secs", elapsed);

//2. Prove
let t1 = std::time::Instant::now();
let _ = groth16_prove_inplace(
curve_type,
circuit,
WASM_FILE_BLS12,
pk,
INPUT_FILE,
public_input_file,
proof_file,
false,
);
let elapsed1 = t1.elapsed().as_secs_f64();
println!("2-groth16-bls12381 prove run time: {} secs", elapsed1);

//3. Verify
let t2 = std::time::Instant::now();
let _ = groth16_verify_inplace(vk, public_input_file, proof_file);
let elapsed2 = t2.elapsed().as_secs_f64();
println!("3-groth16-bls12381 verify run time: {} secs", elapsed2);

Ok(())
}
}

#[cfg(test)]
Expand Down
Loading