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

feat: remove use of env vars for circuit configuration #22

Merged
merged 2 commits into from
Jul 20, 2023
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
4 changes: 0 additions & 4 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ jobs:
- name: Run test
run: cargo test --all -- --nocapture


lint:
name: Lint
runs-on: ubuntu-latest
Expand All @@ -47,8 +46,5 @@ jobs:
with:
cache-on-failure: true

- name: Run fmt
run: cargo fmt --all -- --check

- name: Run clippy
run: cargo clippy --all --all-targets -- -D warnings
2 changes: 1 addition & 1 deletion snark-verifier-sdk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ serde_json = "1.0"
serde_with = { version = "2.2", optional = true }
bincode = "1.3.3"
ark-std = { version = "0.3.0", features = ["print-trace"], optional = true }
halo2-base = { git = "https://github.com/axiom-crypto/halo2-lib.git", branch = "develop", default-features = false }
halo2-base = { git = "https://github.com/axiom-crypto/halo2-lib.git", branch = "feat/test_suite", default-features = false }
snark-verifier = { path = "../snark-verifier", default-features = false }

# loader_evm
Expand Down
13 changes: 7 additions & 6 deletions snark-verifier-sdk/benches/standard_plonk.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
use ark_std::{end_timer, start_timer};
use criterion::{criterion_group, criterion_main};
use criterion::{BenchmarkId, Criterion};
use halo2_base::gates::builder::CircuitBuilderStage;
use halo2_base::utils::fs::gen_srs;
use pprof::criterion::{Output, PProfProfiler};
use rand::rngs::OsRng;
use std::path::Path;
use ark_std::{end_timer, start_timer};
use halo2_base::gates::builder::{CircuitBuilderStage, BASE_CONFIG_PARAMS};
use halo2_base::halo2_proofs;
use halo2_base::utils::fs::gen_srs;
use halo2_proofs::halo2curves as halo2_curves;
use halo2_proofs::{
halo2curves::bn256::Bn256,
poly::{commitment::Params, kzg::commitment::ParamsKZG},
};
use pprof::criterion::{Output, PProfProfiler};
use rand::rngs::OsRng;
use snark_verifier_sdk::evm::{evm_verify, gen_evm_proof_shplonk, gen_evm_verifier_shplonk};
use snark_verifier_sdk::halo2::aggregation::AggregationConfigParams;
use snark_verifier_sdk::{
Expand All @@ -20,6 +19,7 @@ use snark_verifier_sdk::{
Snark,
};
use snark_verifier_sdk::{CircuitExt, SHPLONK};
use std::path::Path;

mod application {
use super::halo2_curves::bn256::Fr;
Expand Down Expand Up @@ -185,6 +185,7 @@ fn bench(c: &mut Criterion) {

let snarks = [(); 3].map(|_| gen_application_snark(&params_app));
let agg_config = AggregationConfigParams::from_path(path);
BASE_CONFIG_PARAMS.with(|params| *params.borrow_mut() = agg_config.into());
let params = gen_srs(agg_config.degree);
let lookup_bits = params.k() as usize - 1;

Expand Down
52 changes: 30 additions & 22 deletions snark-verifier-sdk/src/halo2/aggregation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ use crate::{BITS, LIMBS};
use halo2_base::{
gates::{
builder::{
CircuitBuilderStage, FlexGateConfigParams, GateThreadBuilder,
MultiPhaseThreadBreakPoints, RangeCircuitBuilder, RangeWithInstanceCircuitBuilder,
RangeWithInstanceConfig,
BaseConfigParams, CircuitBuilderStage, GateThreadBuilder, MultiPhaseThreadBreakPoints,
PublicBaseConfig, RangeCircuitBuilder, RangeWithInstanceCircuitBuilder,
BASE_CONFIG_PARAMS,
},
flex_gate::GateStrategy,
RangeChip,
},
halo2_proofs::{
Expand Down Expand Up @@ -38,12 +39,7 @@ use snark_verifier::{
},
verifier::SnarkVerifier,
};
use std::{
env::{set_var, var},
fs::File,
path::Path,
rc::Rc,
};
use std::{fs::File, path::Path, rc::Rc};

use super::{CircuitExt, PoseidonTranscript, Snark, POSEIDON_SPEC};

Expand Down Expand Up @@ -143,7 +139,7 @@ where

/// Same as `FlexGateConfigParams` except we assume a single Phase and default 'Vertical' strategy.
/// Also adds `lookup_bits` field.
#[derive(Clone, Debug, Serialize, Deserialize)]
#[derive(Clone, Copy, Debug, Serialize, Deserialize)]
pub struct AggregationConfigParams {
pub degree: u32,
pub num_advice: usize,
Expand All @@ -159,6 +155,19 @@ impl AggregationConfigParams {
}
}

impl From<AggregationConfigParams> for BaseConfigParams {
fn from(params: AggregationConfigParams) -> Self {
BaseConfigParams {
strategy: GateStrategy::Vertical,
k: params.degree as usize,
num_advice_per_phase: vec![params.num_advice],
num_lookup_advice_per_phase: vec![params.num_lookup_advice],
num_fixed: params.num_fixed,
lookup_bits: Some(params.lookup_bits),
}
}
}

#[derive(Clone, Debug)]
pub struct AggregationCircuit {
pub inner: RangeWithInstanceCircuitBuilder<Fr>,
Expand Down Expand Up @@ -314,11 +323,12 @@ impl AggregationCircuit {
where
AS: for<'a> Halo2KzgAccumulationScheme<'a>,
{
let lookup_bits = params.k() as usize - 1; // almost always we just use the max lookup bits possible, which is k - 1 because of blinding factors
let lookup_bits = BASE_CONFIG_PARAMS
.with(|conf| conf.borrow().lookup_bits)
.unwrap_or(params.k() as usize - 1);
let circuit =
Self::new::<AS>(CircuitBuilderStage::Keygen, None, lookup_bits, params, snarks);
circuit.config(params.k(), Some(10));
set_var("LOOKUP_BITS", lookup_bits.to_string());
circuit
}

Expand All @@ -331,18 +341,16 @@ impl AggregationCircuit {
where
AS: for<'a> Halo2KzgAccumulationScheme<'a>,
{
let lookup_bits: usize = var("LOOKUP_BITS").expect("LOOKUP_BITS not set").parse().unwrap();
let circuit = Self::new::<AS>(
let lookup_bits = BASE_CONFIG_PARAMS
.with(|conf| conf.borrow().lookup_bits)
.unwrap_or(params.k() as usize - 1);
Self::new::<AS>(
CircuitBuilderStage::Prover,
Some(break_points),
lookup_bits,
params,
snarks,
);
let minimum_rows = var("MINIMUM_ROWS").map(|s| s.parse().unwrap_or(10)).unwrap_or(10);
circuit.config(params.k(), Some(minimum_rows));
set_var("LOOKUP_BITS", lookup_bits.to_string());
circuit
)
}

/// Re-expose the previous public instances of aggregated snarks again.
Expand All @@ -359,7 +367,7 @@ impl AggregationCircuit {
&self.as_proof[..]
}

pub fn config(&self, k: u32, minimum_rows: Option<usize>) -> FlexGateConfigParams {
pub fn config(&self, k: u32, minimum_rows: Option<usize>) -> BaseConfigParams {
self.inner.config(k, minimum_rows)
}

Expand All @@ -386,12 +394,12 @@ impl<F: ScalarField> CircuitExt<F> for RangeWithInstanceCircuitBuilder<F> {
}

fn selectors(config: &Self::Config) -> Vec<Selector> {
config.range.gate.basic_gates[0].iter().map(|gate| gate.q_enable).collect()
config.base.gate().basic_gates[0].iter().map(|gate| gate.q_enable).collect()
}
}

impl Circuit<Fr> for AggregationCircuit {
type Config = RangeWithInstanceConfig<Fr>;
type Config = PublicBaseConfig<Fr>;
type FloorPlanner = SimpleFloorPlanner;

fn without_witnesses(&self) -> Self {
Expand Down
4 changes: 2 additions & 2 deletions snark-verifier/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ rand = "0.8"
serde = { version = "1.0", features = ["derive"] }

# Use halo2-base as non-optional dependency because it re-exports halo2_proofs, halo2curves, and poseidon, using different repos based on feature flag "halo2-axiom" or "halo2-pse"
halo2-base = { git = "https://github.com/axiom-crypto/halo2-lib.git", branch = "develop", default-features = false }
halo2-base = { git = "https://github.com/axiom-crypto/halo2-lib.git", branch = "feat/test_suite", default-features = false }
# This is Scroll's audited poseidon circuit. We only use it for the Native Poseidon spec. We do not use the halo2 circuit at all (and it wouldn't even work because the halo2_proofs tag is not compatbile).
poseidon-circuit = { git = "https://github.com/scroll-tech/poseidon-circuit.git", rev = "50015b7" }

Expand All @@ -29,7 +29,7 @@ rlp = { version = "0.5.2", default-features = false, features = ["std"], optiona
revm = { version = "2.3.1", optional = true }

# loader_halo2
halo2-ecc = { git = "https://github.com/axiom-crypto/halo2-lib.git", branch = "develop", default-features = false, optional = true }
halo2-ecc = { git = "https://github.com/axiom-crypto/halo2-lib.git", branch = "feat/test_suite", default-features = false, optional = true }

[dev-dependencies]
ark-std = { version = "0.3.0", features = ["print-trace"] }
Expand Down
Loading