Skip to content
This repository has been archived by the owner on Jul 5, 2024. It is now read-only.

Feat/challenge evm circuit #1020

Merged
merged 24 commits into from
Jan 12, 2023
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
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
6 changes: 0 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ members = [
]

[patch.crates-io]
halo2_proofs = { git = "https://github.com/privacy-scaling-explorations/halo2.git", tag = "v2022_09_10" }
halo2_proofs = { path = "../halo2/halo2_proofs" }
ChihChengLiang marked this conversation as resolved.
Show resolved Hide resolved

# Definition of benchmarks profile to use.
[profile.bench]
Expand Down
2 changes: 1 addition & 1 deletion bus-mapping/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ mock = { path = "../mock", optional = true }

ethers-core = "0.17.0"
ethers-providers = "0.17.0"
halo2_proofs = { git = "https://github.com/privacy-scaling-explorations/halo2.git", tag = "v2022_09_10" }
halo2_proofs = { path = "../../halo2/halo2_proofs" }
itertools = "0.10"
lazy_static = "1.4"
log = "0.4.14"
Expand Down
2 changes: 1 addition & 1 deletion circuit-benchmarks/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,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 = "v2022_09_10" }
halo2_proofs = { path = "../../halo2/halo2_proofs" }
ark-std = { version = "0.3", features = ["print-trace"] }
zkevm-circuits = { path = "../zkevm-circuits", features = ["test"]}
keccak256 = { path = "../keccak256" }
Expand Down
2 changes: 1 addition & 1 deletion eth-types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ ethers-core = "0.17.0"
ethers-signers = "0.17.0"
hex = "0.4"
lazy_static = "1.4"
halo2_proofs = { git = "https://github.com/privacy-scaling-explorations/halo2.git", tag = "v2022_09_10" }
halo2_proofs = { path = "../../halo2/halo2_proofs" }
regex = "1.5.4"
serde = {version = "1.0.130", features = ["derive"] }
serde_json = "1.0.66"
Expand Down
2 changes: 1 addition & 1 deletion gadgets/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ authors = ["The appliedzkp team"]
license = "MIT OR Apache-2.0"

[dependencies]
halo2_proofs = { git = "https://github.com/privacy-scaling-explorations/halo2.git", tag = "v2022_09_10" }
halo2_proofs = { path = "../../halo2/halo2_proofs" }
sha3 = "0.7.2"
eth-types = { path = "../eth-types" }
digest = "0.7.6"
Expand Down
2 changes: 1 addition & 1 deletion integration-tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ url = "2.2.2"
pretty_assertions = "1.0.0"
log = "0.4.14"
env_logger = "0.9"
halo2_proofs = { git = "https://github.com/privacy-scaling-explorations/halo2.git", tag = "v2022_09_10" }
halo2_proofs = { path = "../../halo2/halo2_proofs" }
rand_chacha = "0.3"
paste = "1.0"
rand_xorshift = "0.3.0"
Expand Down
7 changes: 3 additions & 4 deletions integration-tests/src/integration_test_circuits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use std::collections::HashMap;
use std::sync::Mutex;
use zkevm_circuits::bytecode_circuit::bytecode_unroller::BytecodeCircuit;
use zkevm_circuits::copy_circuit::CopyCircuit;
use zkevm_circuits::evm_circuit::test::{get_test_degree, get_test_instance};
use zkevm_circuits::evm_circuit::test::get_test_degree;
use zkevm_circuits::evm_circuit::{test::get_test_cicuit_from_block, witness::block_convert};
use zkevm_circuits::state_circuit::StateCircuit;
use zkevm_circuits::super_circuit::SuperCircuit;
Expand Down Expand Up @@ -245,13 +245,12 @@ pub async fn test_evm_circuit_block(block_num: u64, actual: bool) {
let block = block_convert(&builder.block, &builder.code_db).unwrap();

let degree = get_test_degree(&block);
let instance = get_test_instance(&block);
let circuit = get_test_cicuit_from_block(block);

if actual {
test_actual(degree, circuit, instance, None);
test_actual(degree, circuit, vec![], None);
} else {
test_mock(degree, &circuit, instance);
test_mock(degree, &circuit, vec![]);
}
}

Expand Down
2 changes: 1 addition & 1 deletion testool/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ yaml-rust = "0.4.5"
zkevm-circuits = { path="../zkevm-circuits", features=["test"] }
rand_chacha = "0.3"
rand = "0.8"
halo2_proofs = { git = "https://github.com/privacy-scaling-explorations/halo2.git", tag = "v2022_09_10" }
halo2_proofs = { path = "../../halo2/halo2_proofs" }


[features]
Expand Down
10 changes: 5 additions & 5 deletions 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 = "v2022_09_10" }
halo2_proofs = { path = "../../halo2/halo2_proofs" }
num = "0.4"
sha3 = "0.10"
array-init = "2.0.0"
Expand All @@ -27,10 +27,10 @@ lazy_static = "1.4"
keccak256 = { path = "../keccak256"}
log = "0.4"
env_logger = "0.9"
ecdsa = { git = "https://github.com/privacy-scaling-explorations/halo2wrong", tag = "v2022_09_09" }
ecc = { git = "https://github.com/privacy-scaling-explorations/halo2wrong", tag = "v2022_09_09" }
maingate = { git = "https://github.com/privacy-scaling-explorations/halo2wrong", tag = "v2022_09_09" }
integer = { git = "https://github.com/privacy-scaling-explorations/halo2wrong", tag = "v2022_09_09" }
ecdsa = { path = "../../halo2wrong/ecdsa" }
ecc = { path = "../../halo2wrong/ecc" }
maingate = { path = "../../halo2wrong/maingate" }
integer = { path = "../../halo2wrong/integer" }
libsecp256k1 = "0.7"
num-bigint = { version = "0.4" }
subtle = "2.4"
Expand Down
2 changes: 1 addition & 1 deletion zkevm-circuits/src/copy_circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ impl<F: Field> SubCircuitConfig<F> for CopyCircuitConfig<F> {
cb.require_equal(
"rows[2].value == rows[0].value * r + rows[1].value",
meta.query_advice(value, Rotation(2)),
meta.query_advice(value, Rotation::cur()) * challenges.evm_word()
meta.query_advice(value, Rotation::cur()) * challenges.keccak_input()
+ meta.query_advice(value, Rotation::next()),
);

Expand Down
80 changes: 37 additions & 43 deletions zkevm-circuits/src/evm_circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ pub struct EvmCircuitConfig<F> {

/// Circuit configuration arguments
pub struct EvmCircuitConfigArgs<F: Field> {
/// Power of randomness
pub power_of_randomness: [Expression<F>; 31],
/// Challenge
pub challenges: Challenges<Expression<F>>,
/// TxTable
pub tx_table: TxTable,
/// RwTable
Expand All @@ -67,7 +67,7 @@ impl<F: Field> SubCircuitConfig<F> for EvmCircuitConfig<F> {
fn new(
meta: &mut ConstraintSystem<F>,
Self::ConfigArgs {
power_of_randomness,
challenges,
tx_table,
rw_table,
bytecode_table,
Expand All @@ -81,7 +81,7 @@ impl<F: Field> SubCircuitConfig<F> for EvmCircuitConfig<F> {
let byte_table = [(); 1].map(|_| meta.fixed_column());
let execution = Box::new(ExecutionConfig::configure(
meta,
power_of_randomness,
challenges,
&fixed_table,
&byte_table,
&tx_table,
Expand Down Expand Up @@ -215,14 +215,14 @@ impl<F: Field> SubCircuit<F> for EvmCircuit<F> {
fn synthesize_sub(
&self,
config: &Self::Config,
_challenges: &Challenges<Value<F>>,
challenges: &Challenges<Value<F>>,
layouter: &mut impl Layouter<F>,
) -> Result<(), Error> {
let block = self.block.as_ref().unwrap();

config.load_fixed_table(layouter, self.fixed_table_tags.clone())?;
config.load_byte_table(layouter)?;
config.execution.assign_block(layouter, block)
config.execution.assign_block(layouter, block, challenges)
}
}

Expand All @@ -233,13 +233,13 @@ pub mod test {
evm_circuit::{table::FixedTableTag, witness::Block, EvmCircuitConfig},
exp_circuit::OFFSET_INCREMENT,
table::{BlockTable, BytecodeTable, CopyTable, ExpTable, KeccakTable, RwTable, TxTable},
util::{power_of_randomness_from_instance, Challenges},
util::Challenges,
witness::block_convert,
};
use bus_mapping::{circuit_input_builder::CircuitsParams, evm::OpcodeId, mock::BlockData};
use eth_types::{geth_types::GethData, Field, Word};
use halo2_proofs::{
circuit::{Layouter, SimpleFloorPlanner, Value},
circuit::{Layouter, SimpleFloorPlanner},
dev::{MockProver, VerifyFailure},
plonk::{Circuit, ConstraintSystem, Error},
};
Expand Down Expand Up @@ -295,7 +295,7 @@ pub mod test {
}

impl<F: Field> Circuit<F> for EvmCircuit<F> {
type Config = EvmCircuitConfig<F>;
type Config = (EvmCircuitConfig<F>, Challenges);
type FloorPlanner = SimpleFloorPlanner;

fn without_witnesses(&self) -> Self {
Expand All @@ -311,20 +311,24 @@ pub mod test {
let copy_table = CopyTable::construct(meta, q_copy_table);
let keccak_table = KeccakTable::construct(meta);
let exp_table = ExpTable::construct(meta);

let power_of_randomness = power_of_randomness_from_instance(meta);
EvmCircuitConfig::new(
meta,
EvmCircuitConfigArgs {
power_of_randomness,
tx_table,
rw_table,
bytecode_table,
block_table,
copy_table,
keccak_table,
exp_table,
},
let challenges = Challenges::construct(meta);
let challenges_expr = challenges.exprs(meta);

(
EvmCircuitConfig::new(
meta,
EvmCircuitConfigArgs {
challenges: challenges_expr,
tx_table,
rw_table,
bytecode_table,
block_table,
copy_table,
keccak_table,
exp_table,
},
),
challenges,
)
}

Expand All @@ -334,10 +338,9 @@ pub mod test {
mut layouter: impl Layouter<F>,
) -> Result<(), Error> {
let block = self.block.as_ref().unwrap();
let challenges = Challenges::mock(
Value::known(block.randomness),
Value::known(block.randomness),
);

let (config, challenges) = config;
let challenges = challenges.values(&mut layouter);

config.tx_table.load(
&mut layouter,
Expand All @@ -350,14 +353,14 @@ pub mod test {
&mut layouter,
&block.rws.table_assignments(),
block.circuits_params.max_rws,
Value::known(block.randomness),
challenges.evm_word(),
)?;
config
.bytecode_table
.load(&mut layouter, block.bytecodes.values(), &challenges)?;
config
.block_table
.load(&mut layouter, &block.context, block.randomness)?;
.load(&mut layouter, &block.context, challenges.evm_word())?;
config.copy_table.load(&mut layouter, block, &challenges)?;
config
.keccak_table
Expand All @@ -372,13 +375,13 @@ pub mod test {
pub fn get_num_rows_required(block: &Block<F>) -> usize {
let mut cs = ConstraintSystem::default();
let config = EvmCircuit::<F>::configure(&mut cs);
config.get_num_rows_required(block)
config.0.get_num_rows_required(block)
}

pub fn get_active_rows(block: &Block<F>) -> (Vec<usize>, Vec<usize>) {
let mut cs = ConstraintSystem::default();
let config = EvmCircuit::<F>::configure(&mut cs);
config.get_active_rows(block)
config.0.get_active_rows(block)
}
}

Expand Down Expand Up @@ -468,25 +471,16 @@ pub mod test {
EvmCircuit::<F>::new_dev(block, fixed_table_tags)
}

pub fn get_test_instance<F: Field>(block: &Block<F>) -> Vec<Vec<F>> {
let k = get_test_degree(block);

(1..32)
.map(|exp| vec![block.randomness.pow(&[exp, 0, 0, 0]); (1 << k) - 64])
.collect()
}

pub fn run_test_circuit<F: Field>(block: Block<F>) -> Result<(), Vec<VerifyFailure>> {
let k = get_test_degree(&block);

let (active_gate_rows, active_lookup_rows) = EvmCircuit::<F>::get_active_rows(&block);

let power_of_randomness = get_test_instance(&block);

let circuit = get_test_cicuit_from_block(block);
let prover = MockProver::<F>::run(k, &circuit, power_of_randomness).unwrap();
let prover = MockProver::<F>::run(k, &circuit, vec![]).unwrap();
prover.verify_at_rows_par(active_gate_rows.into_iter(), active_lookup_rows.into_iter())
}

}

#[cfg(test)]
Expand Down Expand Up @@ -525,7 +519,7 @@ mod evm_circuit_stats {

let mut implemented_states = Vec::new();
for state in ExecutionState::iter() {
let height = circuit.execution.get_step_height_option(state);
let height = circuit.0.execution.get_step_height_option(state);
if let Some(h) = height {
implemented_states.push((state, h));
}
Expand Down
Loading