Skip to content

Commit

Permalink
Use halo2 circuit-params feature (privacy-scaling-explorations#1399)
Browse files Browse the repository at this point in the history
Refactor SuperCircuit and PiCircuit to use circuit parameters instead of
associated constants.

Resolve
privacy-scaling-explorations#1393

### Type of change

- [x] Breaking change (fix or feature that would cause existing
functionality to not work as expected)

### Contents

Using the `circuit-params` feature recently introduced in our fork of
halo2 allows simplifying the code for circuits that require runtime
parameters at configuration time. It removes the annoying associated
const of the circuit types, and also allows having a single circuit type
(no need for an extra circuit test type).
  • Loading branch information
ed255 authored May 11, 2023
1 parent a807754 commit 14bd7c9
Show file tree
Hide file tree
Showing 37 changed files with 174 additions and 207 deletions.
21 changes: 8 additions & 13 deletions circuit-benchmarks/src/pi_circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ mod tests {
use rand_xorshift::XorShiftRng;
use std::env::var;
use zkevm_circuits::{
pi_circuit::{PiCircuit, PiTestCircuit, PublicData},
pi_circuit::{PiCircuit, PublicData},
util::SubCircuit,
};

Expand All @@ -48,15 +48,10 @@ mod tests {
let mut rng = ChaCha20Rng::seed_from_u64(2);
let randomness = Fr::random(&mut rng);
let rand_rpi = Fr::random(&mut rng);
let public_data = generate_publicdata::<MAX_TXS, MAX_CALLDATA>();
let circuit = PiTestCircuit::<Fr, MAX_TXS, MAX_CALLDATA>(PiCircuit::<Fr>::new(
MAX_TXS,
MAX_CALLDATA,
randomness,
rand_rpi,
public_data,
));
let public_inputs = circuit.0.instance();
let public_data = generate_publicdata(MAX_TXS);
let circuit =
PiCircuit::<Fr>::new(MAX_TXS, MAX_CALLDATA, randomness, rand_rpi, public_data);
let public_inputs = circuit.instance();
let instance: Vec<&[Fr]> = public_inputs.iter().map(|input| &input[..]).collect();
let instances = &[&instance[..]];

Expand Down Expand Up @@ -90,7 +85,7 @@ mod tests {
Challenge255<G1Affine>,
XorShiftRng,
Blake2bWrite<Vec<u8>, G1Affine, Challenge255<G1Affine>>,
PiTestCircuit<Fr, MAX_TXS, MAX_CALLDATA>,
PiCircuit<Fr>,
>(
&general_params,
&pk,
Expand Down Expand Up @@ -125,12 +120,12 @@ mod tests {
end_timer!(start3);
}

fn generate_publicdata<const MAX_TXS: usize, const MAX_CALLDATA: usize>() -> PublicData {
fn generate_publicdata(max_txs: usize) -> PublicData {
let mut public_data = PublicData::default();
let chain_id = 1337u64;
public_data.chain_id = Word::from(chain_id);

let n_tx = MAX_TXS;
let n_tx = max_txs;
for _ in 0..n_tx {
let eth_tx = eth_types::Transaction::from(mock::CORRECT_MOCK_TXS[0].clone());
public_data.transactions.push(eth_tx);
Expand Down
10 changes: 4 additions & 6 deletions circuit-benchmarks/src/super_circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,9 @@ mod tests {

block.sign(&wallets);

const MAX_TXS: usize = 1;
const MAX_CALLDATA: usize = 32;
let circuits_params = CircuitsParams {
max_txs: MAX_TXS,
max_calldata: MAX_CALLDATA,
max_txs: 1,
max_calldata: 32,
max_rws: 256,
max_copy_rows: 256,
max_exp_steps: 256,
Expand All @@ -92,7 +90,7 @@ mod tests {
max_keccak_rows: 0,
};
let (_, circuit, instance, _) =
SuperCircuit::<_, MAX_TXS, MAX_CALLDATA, 0x100>::build(block, circuits_params).unwrap();
SuperCircuit::build(block, circuits_params, Fr::from(0x100)).unwrap();
let instance_refs: Vec<&[Fr]> = instance.iter().map(|v| &v[..]).collect();

// Bench setup generation
Expand Down Expand Up @@ -120,7 +118,7 @@ mod tests {
Challenge255<G1Affine>,
ChaChaRng,
Blake2bWrite<Vec<u8>, G1Affine, Challenge255<G1Affine>>,
SuperCircuit<Fr, MAX_TXS, MAX_CALLDATA, 0x100>,
SuperCircuit<Fr>,
>(
&general_params,
&pk,
Expand Down
1 change: 1 addition & 0 deletions gadgets/src/batched_is_zero.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ mod test {
impl<F: Field, const N: usize> Circuit<F> for TestCircuit<F, N> {
type Config = TestCircuitConfig<N>;
type FloorPlanner = SimpleFloorPlanner;
type Params = ();

fn without_witnesses(&self) -> Self {
Self::default()
Expand Down
1 change: 1 addition & 0 deletions gadgets/src/evm_word.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ mod tests {
// commitment which will be provided as public inputs.
type Config = (WordConfig<F>, Column<Instance>);
type FloorPlanner = SimpleFloorPlanner;
type Params = ();

fn without_witnesses(&self) -> Self {
Self::default()
Expand Down
2 changes: 2 additions & 0 deletions gadgets/src/is_zero.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ mod test {
impl<F: Field> Circuit<F> for TestCircuit<F> {
type Config = TestCircuitConfig<F>;
type FloorPlanner = SimpleFloorPlanner;
type Params = ();

fn without_witnesses(&self) -> Self {
Self::default()
Expand Down Expand Up @@ -340,6 +341,7 @@ mod test {
impl<F: Field> Circuit<F> for TestCircuit<F> {
type Config = TestCircuitConfig<F>;
type FloorPlanner = SimpleFloorPlanner;
type Params = ();

fn without_witnesses(&self) -> Self {
Self::default()
Expand Down
2 changes: 2 additions & 0 deletions gadgets/src/less_than.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ mod test {
impl<F: Field> Circuit<F> for TestCircuit<F> {
type Config = TestCircuitConfig<F>;
type FloorPlanner = SimpleFloorPlanner;
type Params = ();

fn without_witnesses(&self) -> Self {
Self::default()
Expand Down Expand Up @@ -363,6 +364,7 @@ mod test {
impl<F: Field> Circuit<F> for TestCircuit<F> {
type Config = TestCircuitConfig<F>;
type FloorPlanner = SimpleFloorPlanner;
type Params = ();

fn without_witnesses(&self) -> Self {
Self::default()
Expand Down
1 change: 1 addition & 0 deletions gadgets/src/monotone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ mod test {
{
type Config = TestCircuitConfig;
type FloorPlanner = SimpleFloorPlanner;
type Params = ();

fn without_witnesses(&self) -> Self {
Self::default()
Expand Down
1 change: 1 addition & 0 deletions gadgets/src/mul_add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,7 @@ mod test {
impl<F: Field> Circuit<F> for TestCircuit<F> {
type Config = TestCircuitConfig<F>;
type FloorPlanner = SimpleFloorPlanner;
type Params = ();

fn configure(meta: &mut halo2_proofs::plonk::ConstraintSystem<F>) -> Self::Config {
let q_enable = meta.complex_selector();
Expand Down
2 changes: 1 addition & 1 deletion integration-tests/src/integration_test_circuits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ lazy_static! {
TokioMutex::new(IntegrationTest::new("Keccak", KECCAK_CIRCUIT_DEGREE));

/// Integration test for Copy circuit
pub static ref SUPER_CIRCUIT_TEST: TokioMutex<IntegrationTest<SuperCircuit::<Fr, MAX_TXS, MAX_CALLDATA, TEST_MOCK_RANDOMNESS>>> =
pub static ref SUPER_CIRCUIT_TEST: TokioMutex<IntegrationTest<SuperCircuit::<Fr>>> =
TokioMutex::new(IntegrationTest::new("Super", SUPER_CIRCUIT_DEGREE));

/// Integration test for Exp circuit
Expand Down
10 changes: 3 additions & 7 deletions testool/src/statetest/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@ use std::{collections::HashMap, str::FromStr};
use thiserror::Error;
use zkevm_circuits::{super_circuit::SuperCircuit, test_util::CircuitTestBuilder, witness::Block};

const MAX_TXS: usize = 1;
const MAX_CALLDATA: usize = 32;

#[derive(PartialEq, Eq, Error, Debug)]
pub enum StateTestError {
#[error("CannotGenerateCircuitInput({0})")]
Expand Down Expand Up @@ -277,8 +274,8 @@ pub fn run_test(
geth_data.sign(&wallets);

let circuits_params = CircuitsParams {
max_txs: MAX_TXS,
max_calldata: MAX_CALLDATA,
max_txs: 1,
max_calldata: 32,
max_rws: 256,
max_copy_rows: 256,
max_exp_steps: 256,
Expand All @@ -287,8 +284,7 @@ pub fn run_test(
max_keccak_rows: 0,
};
let (k, circuit, instance, _builder) =
SuperCircuit::<Fr, MAX_TXS, MAX_CALLDATA, 0x100>::build(geth_data, circuits_params)
.unwrap();
SuperCircuit::<Fr>::build(geth_data, circuits_params, Fr::from(0x100)).unwrap();
builder = _builder;

let prover = MockProver::run(k, &circuit, instance).unwrap();
Expand Down
2 changes: 1 addition & 1 deletion zkevm-circuits/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ license = "MIT OR Apache-2.0"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
halo2_proofs = { git = "https://github.com/privacy-scaling-explorations/halo2.git", tag = "v2023_04_20" }
halo2_proofs = { git = "https://github.com/privacy-scaling-explorations/halo2.git", features = ["circuit-params"], tag = "v2023_04_20" }
num = "0.4"
sha3 = "0.10"
array-init = "2.0.0"
Expand Down
1 change: 1 addition & 0 deletions zkevm-circuits/src/bytecode_circuit/dev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use halo2_proofs::{
impl<F: Field> Circuit<F> for BytecodeCircuit<F> {
type Config = (BytecodeCircuitConfig<F>, Challenges);
type FloorPlanner = SimpleFloorPlanner;
type Params = ();

fn without_witnesses(&self) -> Self {
Self::default()
Expand Down
2 changes: 1 addition & 1 deletion zkevm-circuits/src/bytecode_circuit/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use log::error;
fn bytecode_circuit_unusable_rows() {
assert_eq!(
BytecodeCircuit::<Fr>::unusable_rows(),
unusable_rows::<Fr, BytecodeCircuit::<Fr>>(),
unusable_rows::<Fr, BytecodeCircuit::<Fr>>(()),
)
}

Expand Down
1 change: 1 addition & 0 deletions zkevm-circuits/src/copy_circuit/dev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use halo2_proofs::{
impl<F: Field> Circuit<F> for CopyCircuit<F> {
type Config = (CopyCircuitConfig<F>, Challenges<Challenge>);
type FloorPlanner = SimpleFloorPlanner;
type Params = ();

fn without_witnesses(&self) -> Self {
Self::default()
Expand Down
2 changes: 1 addition & 1 deletion zkevm-circuits/src/copy_circuit/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use mock::{test_ctx::helpers::account_0_code_account_1_no_code, TestContext, MOC
fn copy_circuit_unusable_rows() {
assert_eq!(
CopyCircuit::<Fr>::unusable_rows(),
unusable_rows::<Fr, CopyCircuit::<Fr>>(),
unusable_rows::<Fr, CopyCircuit::<Fr>>(()),
)
}

Expand Down
4 changes: 3 additions & 1 deletion zkevm-circuits/src/evm_circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,7 @@ pub(crate) mod cached {
impl Circuit<Fr> for EvmCircuitCached {
type Config = (EvmCircuitConfig<Fr>, Challenges);
type FloorPlanner = SimpleFloorPlanner;
type Params = ();

fn without_witnesses(&self) -> Self {
Self(self.0.without_witnesses())
Expand Down Expand Up @@ -359,6 +360,7 @@ pub(crate) mod cached {
impl<F: Field> Circuit<F> for EvmCircuit<F> {
type Config = (EvmCircuitConfig<F>, Challenges);
type FloorPlanner = SimpleFloorPlanner;
type Params = ();

fn without_witnesses(&self) -> Self {
Self::default()
Expand Down Expand Up @@ -470,7 +472,7 @@ mod evm_circuit_stats {
fn evm_circuit_unusable_rows() {
assert_eq!(
EvmCircuit::<Fr>::unusable_rows(),
unusable_rows::<Fr, EvmCircuit::<Fr>>(),
unusable_rows::<Fr, EvmCircuit::<Fr>>(()),
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ impl<G> UnitTestMathGadgetBaseCircuit<G> {
impl<F: Field, G: MathGadgetContainer<F>> Circuit<F> for UnitTestMathGadgetBaseCircuit<G> {
type Config = (UnitTestMathGadgetBaseCircuitConfig<F, G>, Challenges);
type FloorPlanner = SimpleFloorPlanner;
type Params = ();

fn without_witnesses(&self) -> Self {
UnitTestMathGadgetBaseCircuit {
Expand Down
1 change: 1 addition & 0 deletions zkevm-circuits/src/exp_circuit/dev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use halo2_proofs::{
impl<F: Field> Circuit<F> for ExpCircuit<F> {
type Config = (ExpCircuitConfig<F>, Challenges);
type FloorPlanner = SimpleFloorPlanner;
type Params = ();

fn without_witnesses(&self) -> Self {
Self::default()
Expand Down
2 changes: 1 addition & 1 deletion zkevm-circuits/src/exp_circuit/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use mock::TestContext;
fn exp_circuit_unusable_rows() {
assert_eq!(
ExpCircuit::<Fr>::unusable_rows(),
unusable_rows::<Fr, ExpCircuit::<Fr>>(),
unusable_rows::<Fr, ExpCircuit::<Fr>>(()),
)
}

Expand Down
1 change: 1 addition & 0 deletions zkevm-circuits/src/keccak_circuit/dev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use halo2_proofs::{
impl<F: Field> Circuit<F> for KeccakCircuit<F> {
type Config = (KeccakCircuitConfig<F>, Challenges);
type FloorPlanner = SimpleFloorPlanner;
type Params = ();

fn without_witnesses(&self) -> Self {
Self::default()
Expand Down
1 change: 1 addition & 0 deletions zkevm-circuits/src/keccak_circuit/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@ mod tests {
impl Circuit<F> for TableTestCircuit {
type Config = [TableColumn; 2];
type FloorPlanner = SimpleFloorPlanner;
type Params = ();

fn without_witnesses(&self) -> Self {
self.clone()
Expand Down
2 changes: 1 addition & 1 deletion zkevm-circuits/src/keccak_circuit/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ fn serial_keccak_circuit_unusable_rows() {
std::env::set_var("KECCAK_ROWS", format!("{keccak_rows}"));
assert_eq!(
KeccakCircuit::<Fr>::unusable_rows(),
unusable_rows::<Fr, KeccakCircuit::<Fr>>(),
unusable_rows::<Fr, KeccakCircuit::<Fr>>(()),
)
}
std::env::set_var("KECCAK_ROWS", format!("{DEFAULT_KECCAK_ROWS}"));
Expand Down
2 changes: 0 additions & 2 deletions zkevm-circuits/src/pi_circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ mod param;
mod dev;
#[cfg(any(feature = "test", test))]
mod test;
#[cfg(any(feature = "test", test, feature = "test-circuits"))]
pub use dev::PiTestCircuit;

use eth_types::{
geth_types::{BlockConstants, Transaction},
Expand Down
Loading

0 comments on commit 14bd7c9

Please sign in to comment.