Skip to content

Commit

Permalink
rm kzg verify
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-camuto committed Dec 14, 2022
1 parent a14623d commit aff8070
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 133 deletions.
27 changes: 5 additions & 22 deletions src/bin/ezkl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use ezkl::pfsys::kzg::aggregation::{
gen_application_snark, gen_kzg_proof, gen_pk, gen_srs,
};
#[cfg(not(feature = "evm"))]
use ezkl::pfsys::kzg::single::{create_kzg_proof, verify_kzg_proof};
use ezkl::pfsys::kzg::single::create_kzg_proof;
use ezkl::pfsys::Proof;
use ezkl::pfsys::{parse_prover_errors, prepare_circuit_and_public_input, prepare_data};
#[cfg(feature = "evm")]
Expand Down Expand Up @@ -220,24 +220,8 @@ pub fn main() {
}
}
ProofSystem::KZG => {
let (circuit, public_inputs) = prepare_circuit_and_public_input(&data);
info!("proof with {}", pfsys);
let params: ParamsKZG<Bn256> = ParamsKZG::new(args.logrows);
trace!("params computed");

let (_pk, proof, _input_dims) =
create_kzg_proof(circuit.clone(), public_inputs.clone(), &params);

let pi: Vec<_> = public_inputs
.into_iter()
.map(|i| i.into_iter().collect())
.collect();

Proof {
input_shapes: circuit.inputs.iter().map(|i| i.dims().to_vec()).collect(),
public_inputs: pi,
proof,
}
// https://github.com/zcash/halo2/pull/661
todo!("we need verification key serialization to be implemented");
}
};
let serialized = match serde_json::to_string(&checkable_pf) {
Expand Down Expand Up @@ -276,9 +260,8 @@ pub fn main() {
assert!(result);
}
ProofSystem::KZG => {
let result = verify_kzg_proof(proof);
info!("verified: {}", result);
assert!(result);
// https://github.com/zcash/halo2/pull/661
todo!("we need verification key serialization to be implemented");
}
}
}
Expand Down
76 changes: 6 additions & 70 deletions src/pfsys/kzg/single.rs
Original file line number Diff line number Diff line change
@@ -1,30 +1,17 @@
use super::super::Proof;
use crate::abort;
use crate::commands::Cli;
use crate::fieldutils::i32_to_felt;
use crate::graph::ModelCircuit;
use crate::tensor::Tensor;
use clap::Parser;
use halo2_proofs::{
// arithmetic::FieldExt,
// dev::{MockProver, VerifyFailure},
plonk::{create_proof, keygen_pk, keygen_vk, verify_proof, Circuit, ProvingKey},
poly::{
commitment::ParamsProver,
kzg::{
commitment::{KZGCommitmentScheme, ParamsKZG},
multiopen::{ProverGWC, VerifierGWC},
strategy::SingleStrategy,
},
},
transcript::{
Blake2bRead, Blake2bWrite, Challenge255, TranscriptReadBuffer, TranscriptWriterBuffer,
plonk::{create_proof, keygen_pk, keygen_vk, Circuit, ProvingKey},
poly::kzg::{
commitment::{KZGCommitmentScheme, ParamsKZG},
multiopen::ProverGWC,
},
transcript::{Blake2bWrite, Challenge255, TranscriptWriterBuffer},
};
use halo2curves::bn256::{Bn256, Fr as F, G1Affine};
use log::{error, info, trace};
use log::{info, trace};
use rand::rngs::OsRng;
use std::marker::PhantomData;
use std::ops::Deref;
use std::time::Instant;

Expand Down Expand Up @@ -72,54 +59,3 @@ pub fn create_kzg_proof(

(pk, proof, dims)
}

pub fn verify_kzg_proof(proof: Proof) -> bool {
let args = Cli::parse();
let params: ParamsKZG<Bn256> = ParamsKZG::new(args.logrows);

let inputs = proof
.input_shapes
.iter()
.map(
|s| match Tensor::new(Some(&vec![0; s.iter().product()]), s) {
Ok(t) => t,
Err(e) => {
abort!("failed to initialize tensor {:?}", e);
}
},
)
.collect();
let circuit = ModelCircuit::<F> {
inputs,
_marker: PhantomData,
};
let empty_circuit = circuit.without_witnesses();
let vk = keygen_vk(&params, &empty_circuit).expect("keygen_vk should not fail");
let pk = keygen_pk(&params, vk, &empty_circuit).expect("keygen_pk should not fail");

let pi_inner: Vec<Vec<F>> = proof
.public_inputs
.iter()
.map(|i| i.iter().map(|e| i32_to_felt::<F>(*e)).collect::<Vec<F>>())
.collect::<Vec<Vec<F>>>();
let pi_inner = pi_inner.iter().map(|e| e.deref()).collect::<Vec<&[F]>>();
let pi_for_real_prover: &[&[&[F]]] = &[&pi_inner];
trace!("pi for real prover {:?}", pi_for_real_prover);

let now = Instant::now();
let strategy = SingleStrategy::new(&params);
let mut transcript = Blake2bRead::<_, _, Challenge255<_>>::init(&proof.proof[..]);

trace!("params computed");

let result = verify_proof::<_, VerifierGWC<_>, _, _, _>(
&params,
pk.get_vk(),
strategy,
pi_for_real_prover,
&mut transcript,
)
.is_ok();
info!("verify took {}", now.elapsed().as_secs());
result
}
41 changes: 0 additions & 41 deletions tests/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ macro_rules! test_func {
use crate::ipa_fullprove;
use crate::ipa_prove_and_verify;
use crate::kzg_fullprove;
use crate::kzg_prove_and_verify;
use crate::kzg_evm_fullprove;

seq!(N in 0..=11 {
Expand Down Expand Up @@ -68,12 +67,6 @@ macro_rules! test_func {
kzg_fullprove(test.to_string());
}

#(#[test_case(TESTS[N])])*
#[ignore]
fn kzg_prove_and_verify_(test: &str) {
kzg_prove_and_verify(test.to_string());
}

// these take a particularly long time to run
#(#[test_case(TESTS[N])])*
#[ignore]
Expand Down Expand Up @@ -197,40 +190,6 @@ fn ipa_prove_and_verify(example_name: String) {
assert!(status.success());
}

// prove-serialize-verify, the usual full path
fn kzg_prove_and_verify(example_name: String) {
let status = Command::new("target/release/ezkl")
.args([
"--bits=16",
"-K=17",
"prove",
"-D",
format!("./examples/onnx/examples/{}/input.json", example_name).as_str(),
"-M",
format!("./examples/onnx/examples/{}/network.onnx", example_name).as_str(),
"-O",
format!("pav_{}.pf", example_name).as_str(),
"--pfsys=kzg",
])
.status()
.expect("failed to execute process");
assert!(status.success());
let status = Command::new("target/release/ezkl")
.args([
"--bits=16",
"-K=17",
"verify",
"--pfsys=kzg",
"-M",
format!("./examples/onnx/examples/{}/network.onnx", example_name).as_str(),
"-P",
format!("pav_{}.pf", example_name).as_str(),
])
.status()
.expect("failed to execute process");
assert!(status.success());
}

// KZG tests
// full prove (slower, covers more, but still reuses the pk)
fn kzg_fullprove(example_name: String) {
Expand Down

0 comments on commit aff8070

Please sign in to comment.