Skip to content

Commit

Permalink
Update to v0.2.2 and add halo2-ecc
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanpwang committed Jan 30, 2023
1 parent cf6ef26 commit 0b1c3e0
Show file tree
Hide file tree
Showing 84 changed files with 15,941 additions and 1,641 deletions.
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,10 @@ Cargo.lock

# These are backup files generated by rustfmt
**/*.rs.bk
=======
/target

*.png

/halo2_ecc/src/bn254/data/
/halo2_ecc/src/secp256k1/data/
39 changes: 37 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,10 +1,45 @@
[workspace]
members = [
"halo2-base",
"halo2-ecc",
"hashes/zkevm-keccak",
]

[patch.crates-io]
halo2_proofs = { git = "https://github.com/privacy-scaling-explorations/halo2.git", tag = "v2022_09_10" }
[profile.dev]
opt-level = 3
debug = 1 # change to 0 or 2 for more or less debug info
overflow-checks = true
incremental = true

# Local "release" mode, more optimized than dev but faster to compile than release
[profile.local]
inherits = "dev"
opt-level = 3
# Set this to 1 or 2 to get more useful backtraces
debug = 1
debug-assertions = false
panic = 'unwind'
# better recompile times
incremental = true
lto = "thin"
codegen-units = 16

[profile.release]
opt-level = 3
debug = false
debug-assertions = false
lto = "fat"
# `codegen-units = 1` can lead to WORSE performance - always bench to find best profile for your machine!
# codegen-units = 1
panic = "abort"
incremental = false

# For performance profiling
[profile.flamegraph]
inherits = "release"
debug = true

# patch so snark-verifier uses this crate's halo2-base
[patch."https://github.com/axiom-crypto/halo2-lib.git"]
halo2-base = { path = "./halo2-base" }
halo2-ecc = { path = "./halo2-ecc" }
10 changes: 0 additions & 10 deletions halo2-base/.gitignore

This file was deleted.

43 changes: 36 additions & 7 deletions halo2-base/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,22 +1,51 @@
[package]
name = "halo2_base"
version = "0.1.0"
name = "halo2-base"
version = "0.2.2"
edition = "2021"

[dependencies]
itertools = "0.10"
num-bigint = { version = "0.4", features = ["rand"] }
num-integer = "0.1"
num-traits = "0.2"
ff = "0.12.0"
rand_chacha = "0.3"
rustc-hash = "1.1"
ff = "0.12"

# halo2
halo2_proofs = { git = "https://github.com/privacy-scaling-explorations/halo2.git", tag = "v2022_09_10" }
# Use Axiom's custom halo2 monorepo for faster proving when feature = "halo2-axiom" is on
halo2_proofs_axiom = { git = "https://github.com/axiom-crypto/halo2.git", tag = "v2023_01_17", package = "halo2_proofs", optional = true }
# Use PSE halo2 and halo2curves for compatibility when feature = "halo2-pse" is on
halo2_proofs = { git = "https://github.com/privacy-scaling-explorations/halo2.git", tag = "v2023_01_20", optional = true }

# plotting circuit layout
plotters = { version = "0.3.0", optional = true }
tabbycat = { version = "0.1", features = ["attributes"], optional = true }

[dev-dependencies]
ark-std = { version = "0.3.0", features = ["print-trace"] }
rand = "0.8"
pprof = { version = "0.11", features = ["criterion", "flamegraph"] }
criterion = "0.4"
criterion-macro = "0.4"

# memory allocation
[target.'cfg(not(target_env = "msvc"))'.dependencies]
jemallocator = { version = "0.5", optional = true }

mimalloc = { version = "0.1", default-features = false, optional = true }

[features]
default = ["display"]
dev-graph = ["halo2_proofs/dev-graph", "plotters"]
default = ["halo2-axiom", "display"]
dev-graph = ["halo2_proofs?/dev-graph", "halo2_proofs_axiom?/dev-graph", "plotters"]
halo2-pse = ["halo2_proofs"]
halo2-axiom = ["halo2_proofs_axiom"]
display = []
profile = ["halo2_proofs_axiom?/profile"]

[[bench]]
name = "mul"
harness = false

[[bench]]
name = "inner_product"
harness = false
21 changes: 0 additions & 21 deletions halo2-base/LICENSE

This file was deleted.

15 changes: 0 additions & 15 deletions halo2-base/README.md

This file was deleted.

128 changes: 128 additions & 0 deletions halo2-base/benches/inner_product.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
#![allow(unused_imports)]
#![allow(unused_variables)]
use halo2_base::gates::{
flex_gate::{FlexGateConfig, GateStrategy},
GateInstructions,
};
use halo2_base::halo2_proofs::{
arithmetic::Field,
circuit::*,
dev::MockProver,
halo2curves::bn256::{Bn256, Fr, G1Affine},
plonk::*,
poly::kzg::{
commitment::{KZGCommitmentScheme, ParamsKZG},
multiopen::ProverSHPLONK,
},
transcript::{Blake2bWrite, Challenge255, TranscriptWriterBuffer},
};
use halo2_base::{Context, ContextParams, QuantumCell::Witness, SKIP_FIRST_PASS};
use itertools::Itertools;
use rand::rngs::OsRng;
use std::marker::PhantomData;

use criterion::{criterion_group, criterion_main};
use criterion::{BenchmarkId, Criterion};

use pprof::criterion::{Output, PProfProfiler};
// Thanks to the example provided by @jebbow in his article
// https://www.jibbow.com/posts/criterion-flamegraphs/

#[derive(Clone, Default)]
struct MyCircuit<F> {
_marker: PhantomData<F>,
}

const NUM_ADVICE: usize = 1;
const K: u32 = 19;

impl Circuit<Fr> for MyCircuit<Fr> {
type Config = FlexGateConfig<Fr>;
type FloorPlanner = SimpleFloorPlanner;

fn without_witnesses(&self) -> Self {
Self::default()
}

fn configure(meta: &mut ConstraintSystem<Fr>) -> Self::Config {
FlexGateConfig::configure(meta, GateStrategy::Vertical, &[NUM_ADVICE], 1, 0, K as usize)
}

fn synthesize(
&self,
config: Self::Config,
mut layouter: impl Layouter<Fr>,
) -> Result<(), Error> {
let mut first_pass = SKIP_FIRST_PASS;

layouter.assign_region(
|| "gate",
|region| {
if first_pass {
first_pass = false;
return Ok(());
}

let mut aux = Context::new(
region,
ContextParams {
max_rows: config.max_rows,
num_context_ids: 1,
fixed_columns: config.constants.clone(),
},
);
let ctx = &mut aux;

let a = (0..5).map(|_| Witness(Value::known(Fr::random(OsRng)))).collect_vec();
let b = (0..5).map(|_| Witness(Value::known(Fr::random(OsRng)))).collect_vec();

for _ in 0..(1 << K) / 16 - 10 {
config.inner_product(ctx, a.clone(), b.clone());
}

Ok(())
},
)
}
}

fn bench(c: &mut Criterion) {
let circuit = MyCircuit::<Fr> { _marker: PhantomData };

MockProver::run(K, &circuit, vec![]).unwrap().assert_satisfied();

let params = ParamsKZG::<Bn256>::setup(K, OsRng);
let vk = keygen_vk(&params, &circuit).expect("vk should not fail");
let pk = keygen_pk(&params, vk, &circuit).expect("pk should not fail");

let mut group = c.benchmark_group("plonk-prover");
group.sample_size(10);
group.bench_with_input(
BenchmarkId::new("inner_product", K),
&(&params, &pk),
|b, &(params, pk)| {
b.iter(|| {
let circuit = MyCircuit::<Fr> { _marker: PhantomData };
let rng = OsRng;
let mut transcript = Blake2bWrite::<_, _, Challenge255<_>>::init(vec![]);
create_proof::<
KZGCommitmentScheme<Bn256>,
ProverSHPLONK<'_, Bn256>,
Challenge255<G1Affine>,
_,
Blake2bWrite<Vec<u8>, G1Affine, Challenge255<_>>,
_,
>(params, pk, &[circuit], &[&[]], rng, &mut transcript)
.expect("prover should not fail");
})
},
);
group.finish();
}

criterion_group! {
name = benches;
config = Criterion::default().with_profiler(PProfProfiler::new(10, Output::Flamegraph(None)));
targets = bench
}
criterion_main!(benches);
Loading

0 comments on commit 0b1c3e0

Please sign in to comment.