Skip to content

Commit

Permalink
fix examples
Browse files Browse the repository at this point in the history
  • Loading branch information
zhenfeizhang committed Oct 23, 2023
1 parent cde1814 commit 4bac515
Show file tree
Hide file tree
Showing 15 changed files with 64 additions and 635 deletions.
7 changes: 6 additions & 1 deletion snark-verifier-sdk/examples/k_as_witness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,12 @@ mod application {
config.q_ab,
config.constant,
]) {
region.assign_fixed(|| "", column, 1, ||Value::known(Fr::from(idx as u64)));
region.assign_fixed(
|| "",
column,
1,
|| Value::known(Fr::from(idx as u64)),
);
}
let a = region.assign_advice(|| "", config.a, 2, || Value::known(Fr::one()))?;
a.copy_advice(|| "", &mut region, config.b, 3);
Expand Down
12 changes: 7 additions & 5 deletions snark-verifier-sdk/src/evm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use halo2_base::halo2_proofs::{
transcript::{TranscriptReadBuffer, TranscriptWriterBuffer},
};
use itertools::Itertools;
use rand::{rngs::StdRng, SeedableRng};
use rand::{rngs::StdRng, Rng, SeedableRng};
pub use snark_verifier::loader::evm::encode_calldata;
use snark_verifier::{
loader::evm::{compile_solidity, deploy_and_call, EvmLoader},
Expand All @@ -38,6 +38,7 @@ pub fn gen_evm_proof<'params, C, P, V>(
pk: &'params ProvingKey<G1Affine>,
circuit: C,
instances: Vec<Vec<Fr>>,
rng: &mut (impl Rng + Send),
) -> Vec<u8>
where
C: Circuit<Fr>,
Expand All @@ -53,7 +54,6 @@ where

#[cfg(feature = "display")]
let proof_time = start_timer!(|| "Create EVM proof");
let rng = StdRng::from_entropy();
let proof = {
let mut transcript = TranscriptWriterBuffer::<_, G1Affine, _>::init(Vec::new());
create_proof::<KZGCommitmentScheme<Bn256>, P, _, _, EvmTranscript<_, _, _, _>, _>(
Expand Down Expand Up @@ -93,17 +93,19 @@ pub fn gen_evm_proof_gwc<'params, C: Circuit<Fr>>(
pk: &'params ProvingKey<G1Affine>,
circuit: C,
instances: Vec<Vec<Fr>>,
rng: &mut (impl Rng + Send),
) -> Vec<u8> {
gen_evm_proof::<C, ProverGWC<_>, VerifierGWC<_>>(params, pk, circuit, instances)
gen_evm_proof::<C, ProverGWC<_>, VerifierGWC<_>>(params, pk, circuit, instances, rng)
}

pub fn gen_evm_proof_shplonk<'params, C: Circuit<Fr>>(
params: &'params ParamsKZG<Bn256>,
pk: &'params ProvingKey<G1Affine>,
circuit: C,
instances: Vec<Vec<Fr>>,
rng: &mut (impl Rng + Send),
) -> Vec<u8> {
gen_evm_proof::<C, ProverSHPLONK<_>, VerifierSHPLONK<_>>(params, pk, circuit, instances)
gen_evm_proof::<C, ProverSHPLONK<_>, VerifierSHPLONK<_>>(params, pk, circuit, instances, rng)
}

pub trait EvmKzgAccumulationScheme = PolynomialCommitmentScheme<
Expand Down Expand Up @@ -185,4 +187,4 @@ pub fn write_calldata(instances: &[Vec<Fr>], proof: &[u8], path: &Path) -> io::R
let calldata = hex::encode(calldata);
fs::write(path, &calldata)?;
Ok(calldata)
}
}
82 changes: 0 additions & 82 deletions snark-verifier-sdk/src/file_io.rs

This file was deleted.

23 changes: 16 additions & 7 deletions snark-verifier-sdk/src/halo2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use halo2_proofs::{
};
use itertools::Itertools;
use lazy_static::lazy_static;
use rand::{rngs::StdRng, SeedableRng};
use rand::{rngs::StdRng, Rng, SeedableRng};
use snark_verifier::{
cost::CostEstimation,
loader::native::NativeLoader,
Expand Down Expand Up @@ -81,6 +81,7 @@ pub fn gen_proof<'params, C, P, V>(
pk: &ProvingKey<G1Affine>,
circuit: C,
instances: Vec<Vec<Fr>>,
rng: &mut (impl Rng + Send),
path: Option<(&Path, &Path)>,
) -> Vec<u8>
where
Expand Down Expand Up @@ -114,7 +115,6 @@ where

let mut transcript =
PoseidonTranscript::<NativeLoader, Vec<u8>>::from_spec(vec![], POSEIDON_SPEC.clone());
let rng = StdRng::from_entropy();
create_proof::<_, P, _, _, _, _>(params, pk, &[circuit], &[&instances], rng, &mut transcript)
.unwrap();
let proof = transcript.finalize();
Expand Down Expand Up @@ -159,9 +159,11 @@ pub fn gen_proof_gwc<C: Circuit<Fr>>(
pk: &ProvingKey<G1Affine>,
circuit: C,
instances: Vec<Vec<Fr>>,

rng: &mut (impl Rng + Send),
path: Option<(&Path, &Path)>,
) -> Vec<u8> {
gen_proof::<C, ProverGWC<_>, VerifierGWC<_>>(params, pk, circuit, instances, path)
gen_proof::<C, ProverGWC<_>, VerifierGWC<_>>(params, pk, circuit, instances, rng, path)
}

/// Generates a native proof using SHPLONK multi-open scheme. Uses Poseidon for Fiat-Shamir.
Expand All @@ -172,9 +174,11 @@ pub fn gen_proof_shplonk<C: Circuit<Fr>>(
pk: &ProvingKey<G1Affine>,
circuit: C,
instances: Vec<Vec<Fr>>,

rng: &mut (impl Rng + Send),
path: Option<(&Path, &Path)>,
) -> Vec<u8> {
gen_proof::<C, ProverSHPLONK<_>, VerifierSHPLONK<_>>(params, pk, circuit, instances, path)
gen_proof::<C, ProverSHPLONK<_>, VerifierSHPLONK<_>>(params, pk, circuit, instances, rng, path)
}

/// Generates a SNARK using either SHPLONK or GWC multi-open scheme. Uses Poseidon for Fiat-Shamir.
Expand All @@ -185,6 +189,7 @@ pub fn gen_snark<'params, ConcreteCircuit, P, V>(
params: &'params ParamsKZG<Bn256>,
pk: &ProvingKey<G1Affine>,
circuit: ConcreteCircuit,
rng: &mut (impl Rng + Send),
path: Option<impl AsRef<Path>>,
) -> Snark
where
Expand Down Expand Up @@ -222,7 +227,7 @@ where
(format!("{path}.instances"), format!("{path}.proof"))
});
let paths = path.as_ref().map(|path| (Path::new(&path.0), Path::new(&path.1)));
gen_proof::<ConcreteCircuit, P, V>(params, pk, circuit, instances.clone(), paths)
gen_proof::<ConcreteCircuit, P, V>(params, pk, circuit, instances.clone(), rng, paths)
};

let snark = Snark::new(protocol, instances, proof);
Expand All @@ -247,9 +252,10 @@ pub fn gen_snark_gwc<ConcreteCircuit: CircuitExt<Fr>>(
params: &ParamsKZG<Bn256>,
pk: &ProvingKey<G1Affine>,
circuit: ConcreteCircuit,
rng: &mut (impl Rng + Send),
path: Option<impl AsRef<Path>>,
) -> Snark {
gen_snark::<ConcreteCircuit, ProverGWC<_>, VerifierGWC<_>>(params, pk, circuit, path)
gen_snark::<ConcreteCircuit, ProverGWC<_>, VerifierGWC<_>>(params, pk, circuit, rng, path)
}

/// Generates a SNARK using SHPLONK multi-open scheme. Uses Poseidon for Fiat-Shamir.
Expand All @@ -260,9 +266,12 @@ pub fn gen_snark_shplonk<ConcreteCircuit: CircuitExt<Fr>>(
params: &ParamsKZG<Bn256>,
pk: &ProvingKey<G1Affine>,
circuit: ConcreteCircuit,
rng: &mut (impl Rng + Send),
path: Option<impl AsRef<Path>>,
) -> Snark {
gen_snark::<ConcreteCircuit, ProverSHPLONK<_>, VerifierSHPLONK<_>>(params, pk, circuit, path)
gen_snark::<ConcreteCircuit, ProverSHPLONK<_>, VerifierSHPLONK<_>>(
params, pk, circuit, rng, path,
)
}

/// Tries to deserialize a SNARK from the specified `path` using `bincode`.
Expand Down
Loading

0 comments on commit 4bac515

Please sign in to comment.