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

[WIP] update prover crate #1422

Draft
wants to merge 4 commits into
base: feat/migrate_compression
Choose a base branch
from
Draft
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
1 change: 1 addition & 0 deletions compression/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
/// Circuit implementation of compression circuit.
mod circuit;
mod params;
pub mod utils;

#[cfg(test)]
mod tests;
Expand Down
12 changes: 12 additions & 0 deletions compression/src/utils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
pub fn snark_to_ce_snark(snark: snark_verifier_sdk::Snark) -> ce_snark_verifier_sdk::Snark {
let s = serde_json::to_string(&snark).unwrap();
let mut inner_deserializer = serde_json::Deserializer::from_str(&s);
inner_deserializer.disable_recursion_limit();

let deserializer = serde_stacker::Deserializer::new(&mut inner_deserializer);
serde::Deserialize::deserialize(deserializer).unwrap()
}

pub fn ce_snark_to_snark(ce_snark: ce_snark_verifier_sdk::Snark) -> snark_verifier_sdk::Snark {
serde_json::from_str(&serde_json::to_string(&ce_snark).unwrap()).unwrap()
}
6 changes: 3 additions & 3 deletions prover/src/common/prover/compression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::{
utils::gen_rng,
};
use anyhow::{anyhow, Result};
use compression::CompressionCircuit;
use compression::{utils::ce_snark_to_snark, CompressionCircuit};
use rand::Rng;
use snark_verifier_sdk::Snark;
use ce_snark_verifier_sdk::halo2::aggregation::AggregationConfigParams;
Expand All @@ -21,11 +21,11 @@ impl<'params> Prover<'params> {
prev_snark: Snark,
) -> Result<Snark> {
env::set_var("COMPRESSION_CONFIG", layer_config_path(id));

let circuit =
CompressionCircuit::new(AggregationConfigParams::from_path(layer_config_path(id)), self.params(degree), prev_snark, has_accumulator, &mut rng)
.map_err(|err| anyhow!("Failed to construct compression circuit: {err:?}"))?;
self.gen_snark(id, degree, &mut rng, circuit, "gen_comp_snark")
let ce_snark = self.gen_snark_ce(id, degree, &mut rng, circuit, "gen_comp_snark")?;
Ok(ce_snark_to_snark(ce_snark))
}

pub fn load_or_gen_comp_snark(
Expand Down
10 changes: 5 additions & 5 deletions prover/src/common/prover/evm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crate::{
EvmProof,
};
use anyhow::{anyhow, Result};
use ce_snark_verifier_sdk::{evm::gen_evm_proof_shplonk, CircuitExt as CeCircuitExt};
use compression::CompressionCircuit;
use halo2_proofs::halo2curves::bn256::Fr;
use rand::Rng;
Expand Down Expand Up @@ -39,7 +40,7 @@ impl<'params> Prover<'params> {
)
.map_err(|err| anyhow!("Failed to construct compression circuit: {err:?}"))?;

let result = self.gen_evm_proof(id, degree, &mut rng, circuit, output_dir);
let result = self.gen_evm_proof(id, degree, circuit, output_dir);

if let (Some(output_dir), Ok(proof)) = (output_dir, &result) {
proof.dump(output_dir, &name)?;
Expand All @@ -50,15 +51,14 @@ impl<'params> Prover<'params> {
}
}

fn gen_evm_proof<C: CircuitExt<Fr>>(
fn gen_evm_proof<C: CeCircuitExt<Fr>>(
&mut self,
id: &str,
degree: u32,
rng: &mut (impl Rng + Send),
circuit: C,
output_dir: Option<&str>,
) -> Result<EvmProof> {
Self::assert_if_mock_prover(id, degree, &circuit);
// Self::assert_if_mock_prover(id, degree, &circuit);

let (params, pk) = self.params_and_pk(id, degree, &circuit)?;
log::info!(
Expand All @@ -67,7 +67,7 @@ impl<'params> Prover<'params> {
);
let instances = circuit.instances();
let num_instance = circuit.num_instance();
let proof = gen_evm_proof_shplonk(params, pk, circuit, instances.clone(), rng);
let proof = gen_evm_proof_shplonk(params, pk, circuit, instances.clone());
let evm_proof = EvmProof::new(proof, &instances, num_instance, Some(pk))?;

if read_env_var("SCROLL_PROVER_DUMP_YUL", false) {
Expand Down
26 changes: 26 additions & 0 deletions prover/src/common/prover/utils.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use super::Prover;
use crate::io::serialize_vk;
use anyhow::Result;
use ce_snark_verifier_sdk::CircuitExt as CeCircuitExt;
use halo2_proofs::{
halo2curves::bn256::{Bn256, Fr, G1Affine},
plonk::{keygen_pk2, Circuit, ProvingKey},
Expand Down Expand Up @@ -32,6 +33,31 @@ impl<'params> Prover<'params> {
Ok(snark)
}

pub fn gen_snark_ce<C: CeCircuitExt<Fr>>(
&mut self,
id: &str,
degree: u32,
_rng: &mut (impl Rng + Send),
circuit: C,
desc: &str,
) -> Result<ce_snark_verifier_sdk::Snark> {
let (params, pk) = self.params_and_pk(id, degree, &circuit)?;

log::info!(
"gen_snark_ce id {} desc {} vk transcript_repr {:?}",
id,
desc,
pk.get_vk().transcript_repr()
);

Ok(ce_snark_verifier_sdk::halo2::gen_snark_shplonk(
params,
pk,
circuit,
None::<String>,
))
}

pub fn params(&self, degree: u32) -> &ParamsKZG<Bn256> {
&self.params_map[&degree]
}
Expand Down
8 changes: 8 additions & 0 deletions prover/src/common/verifier.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use crate::io::deserialize_vk;
use ce_snark_verifier_sdk::snark_verifier::pcs::kzg::KzgSuccinctVerifyingKey;
use ce_snark_verifier_sdk::{CircuitExt as CeCircuitExt, PlonkVerifier, SHPLONK};
use halo2_proofs::{
halo2curves::bn256::{Bn256, Fr, G1Affine},
plonk::VerifyingKey,
Expand Down Expand Up @@ -35,3 +37,9 @@ impl<'params, C: CircuitExt<Fr, Params = ()>> Verifier<'params, C> {
verify_snark_shplonk::<C>(self.params.verifier_params(), snark, &self.vk)
}
}

pub struct Verifier2<C: CeCircuitExt<Fr>> {
svk: KzgSuccinctVerifyingKey<G1Affine>,
inner: PlonkVerifier<SHPLONK>,
phantom: PhantomData<C>,
}
4 changes: 2 additions & 2 deletions prover/src/common/verifier/evm.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use super::Verifier;
use crate::EvmProof;
use ce_snark_verifier_sdk::CircuitExt as CeCircuitExt;
use halo2_proofs::halo2curves::bn256::Fr;
use snark_verifier_sdk::CircuitExt;

impl<'params, C: CircuitExt<Fr>> Verifier<'params, C> {
impl<'params, C: CeCircuitExt<Fr>> Verifier<'params, C> {
pub fn gen_evm_verifier(&self, evm_proof: &EvmProof, output_dir: Option<&str>) {
crate::evm::gen_evm_verifier::<C>(self.params, &self.vk, evm_proof, output_dir)
}
Expand Down
7 changes: 3 additions & 4 deletions prover/src/evm.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
use crate::{io::write_file, EvmProof};
use ce_snark_verifier_sdk::CircuitExt as CeCircuitExt;
use halo2_proofs::{
halo2curves::bn256::{Bn256, Fr, G1Affine},
plonk::VerifyingKey,
poly::kzg::commitment::ParamsKZG,
};
use snark_verifier::pcs::kzg::{Bdfg21, Kzg};
use snark_verifier_sdk::CircuitExt;
use std::{path::PathBuf, str::FromStr};

/// Dump YUL and binary bytecode(use `solc` in PATH) to output_dir.
/// Panic if error encountered.
pub fn gen_evm_verifier<C: CircuitExt<Fr>>(
pub fn gen_evm_verifier<C: CeCircuitExt<Fr>>(
params: &ParamsKZG<Bn256>,
vk: &VerifyingKey<G1Affine>,
evm_proof: &EvmProof,
Expand All @@ -23,7 +22,7 @@ pub fn gen_evm_verifier<C: CircuitExt<Fr>>(
});

// Generate deployment code and dump YUL file.
let deployment_code = snark_verifier_sdk::gen_evm_verifier::<C, Kzg<Bn256, Bdfg21>>(
let deployment_code = ce_snark_verifier_sdk::evm::gen_evm_verifier_shplonk::<C>(
params,
vk,
evm_proof.num_instance.clone(),
Expand Down
Loading