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

In-circuit blockhash calculation #98

Closed
wants to merge 49 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
49 commits
Select commit Hold shift + click to select a range
9b43c24
feat: RLP circuit for block hash calculation
ggkitsas May 22, 2023
28ef033
finalized blockhash-check contraints; added blokchash-check witness g…
ggkitsas May 25, 2023
1ddefc6
blockhash: fixes errors, adds tests
ggkitsas May 31, 2023
a786df7
handles corner case of short RLP fields; fixes misc errors; minor ref…
ggkitsas Jun 5, 2023
31b6dd8
reduced the degree of pi circuit; adds RLP block header tests for cor…
ggkitsas Jun 5, 2023
c5d1298
adds rlc for blockhash calculation
ggkitsas Jun 6, 2023
2e898af
adds keccak checks for blockhash calculation
ggkitsas Jun 6, 2023
621590b
pi circuit cleanups
ggkitsas Jun 8, 2023
b823abd
pi circuit: adds checks for reconstructed values of block header fields
ggkitsas Jun 12, 2023
70529c0
pi circuit: adds parent hash checks
ggkitsas Jun 12, 2023
7f59e70
pi circuit: adds withdrawals root for blockhash calcs
ggkitsas Jun 12, 2023
b8e43bd
pi circuit: fixes handling RLP short values
ggkitsas Jun 13, 2023
19e5277
cleansup
ggkitsas Jun 14, 2023
9a2a854
cleaups, clippy, code quality
ggkitsas Jun 14, 2023
aae7300
rustfmt
ggkitsas Jun 14, 2023
ce2346b
clippy
ggkitsas Jun 14, 2023
17801f3
clippy
ggkitsas Jun 14, 2023
c305c8e
fix: wrong selectors when reconstructing block header values
ggkitsas Jun 15, 2023
1328b65
reduces lookups and columns of block hash circuit
ggkitsas Jun 16, 2023
35944ba
adds negative tests
ggkitsas Jun 19, 2023
6c4e630
optimized out some variable lenght field selectors
ggkitsas Jun 19, 2023
c64278a
rustfmt
ggkitsas Jun 19, 2023
bce8d27
ci fixes
ggkitsas Jun 19, 2023
4d16ea0
rustfmt
ggkitsas Jun 19, 2023
ea7b8e9
code imporvement
ggkitsas Jun 21, 2023
6edb468
refactoring
ggkitsas Jun 21, 2023
03979df
Merge branch 'blockhash' of github.com:taikoxyz/zkevm-circuits into b…
ggkitsas Jun 21, 2023
7c4f8b9
minor fix
ggkitsas Jun 21, 2023
a59e2e5
Some refactoring
Brechtpd Jun 21, 2023
207a5eb
Merge pull request #117 from taikoxyz/blockhash-brecht
ggkitsas Jun 22, 2023
8d1399e
minor refactoring
ggkitsas Jun 22, 2023
d53df04
test cleanups
ggkitsas Jun 22, 2023
a37a494
extends block table lookups to include tag and index
ggkitsas Jun 22, 2023
c625044
Some code refactoring
Brechtpd Jun 23, 2023
7e95c2f
Merge pull request #118 from taikoxyz/blockhash-brecht2
ggkitsas Jun 24, 2023
4de76ce
adds comments
ggkitsas Jun 24, 2023
718b923
simplifies code
ggkitsas Jun 27, 2023
d661601
fmt and clippy
ggkitsas Jun 27, 2023
8b53902
fmt + clippy
ggkitsas Jun 27, 2023
ac3c7b3
adds checks for correctly skipping leading zeros in rlc
ggkitsas Jun 27, 2023
2798ac5
fmt
ggkitsas Jun 27, 2023
ba93650
fixes constraint
ggkitsas Jun 27, 2023
5ca964e
skips artifical headers in rlc
ggkitsas Jun 27, 2023
9dd12e8
optimization: removes q_total_length column
ggkitsas Jun 28, 2023
872f182
make sure we rlc all the needed bytes
ggkitsas Jun 29, 2023
9796491
fmt
ggkitsas Jun 29, 2023
6ab8033
simplifies constraints; adds initial work on previous hashes
ggkitsas Jun 30, 2023
54d6f05
fmt + clippy
ggkitsas Jun 30, 2023
456f274
adds block hash checks for the previous 256 blocks
ggkitsas Jul 6, 2023
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
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ members = [
[patch."https://github.com/privacy-scaling-explorations/halo2.git"]
halo2_proofs = { git = "https://github.com/smtmfft/halo2.git", rev = "50ee8ad785c53232824e60b4ff6df32b69970358" }

[patch.crates-io]
ethers-core = {git = "https://github.com/ggkitsas/ethers-rs/", rev = "6059806c17fa1a1d7534044bcdbecfef869392e1"}

# Definition of benchmarks profile to use.
[profile.bench]
opt-level = 3
Expand Down
1 change: 1 addition & 0 deletions circuit-benchmarks/examples/pi_circuit_integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -561,6 +561,7 @@ async fn main() -> Result<(), Error> {
CIRCUIT_CONFIG.max_txs,
CIRCUIT_CONFIG.max_calldata,
public_data,
None,
),
);
assert!(block.txs.len() <= CIRCUIT_CONFIG.max_txs);
Expand Down
2 changes: 1 addition & 1 deletion circuit-benchmarks/examples/super_circuit_integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ fn bench_super_circuit_prover() {

block.sign(&wallets);

type TestSuperCircuit = SuperCircuit::<Fr, 4, 32, 512>;
type TestSuperCircuit = SuperCircuit<Fr, 4, 32, 512>;
let (_, circuit, instance, _) = TestSuperCircuit::build(block).unwrap();
let instance_refs: Vec<&[Fr]> = instance.iter().map(|v| &v[..]).collect();

Expand Down
9 changes: 4 additions & 5 deletions circuit-benchmarks/src/pi_circuit_with_verifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ use snark_verifier::{
verifier::{self, SnarkVerifier},
};
use std::fs::{self, File};
use std::{io::Cursor, io::Write, rc::Rc, time::Instant};
use std::{io::Write, rc::Rc};

type PlonkVerifier = verifier::plonk::PlonkVerifier<KzgAs<Bn256, Gwc19>>;

Expand Down Expand Up @@ -184,14 +184,13 @@ fn new_pi_circuit<const MAX_TXS: usize, const MAX_CALLDATA: usize>(
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(
PiTestCircuit::<Fr, MAX_TXS, MAX_CALLDATA>(PiCircuit::<Fr>::new(
MAX_TXS,
MAX_CALLDATA,
randomness,
rand_rpi,
public_data,
));
circuit
))
}

trait InstancesExport {
Expand Down Expand Up @@ -235,7 +234,7 @@ fn gen_evm_verifier(

File::create("./PlonkEvmVerifier.sol")
.expect("PlonkEvmVerifier.sol")
.write_all(&loader.yul_code().as_bytes())
.write_all(loader.yul_code().as_bytes())
.expect("PlonkEvmVerifier.sol");

evm::compile_yul(&loader.yul_code())
Expand Down
6 changes: 5 additions & 1 deletion mock/src/block.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Mock Block definition and builder related methods.

use crate::{MockTransaction, MOCK_BASEFEE, MOCK_CHAIN_ID, MOCK_DIFFICULTY, MOCK_GASLIMIT};
use eth_types::{Address, Block, Bytes, Hash, Transaction, Word, H64, U64};
use eth_types::{Address, Block, Bytes, Hash, Transaction, Word, H256, H64, U64};
use ethers_core::types::Bloom;
use ethers_core::types::OtherFields;

Expand Down Expand Up @@ -32,6 +32,7 @@ pub struct MockBlock {
size: Word,
mix_hash: Hash,
nonce: H64,
withdrawals_root: H256,
// This field is handled here as we assume that all block txs have the same ChainId.
// Also, the field is stored in the block_table since we don't have a chain_config
// structure/table.
Expand Down Expand Up @@ -64,6 +65,7 @@ impl Default for MockBlock {
mix_hash: Hash::zero(),
nonce: H64::zero(),
chain_id: *MOCK_CHAIN_ID,
withdrawals_root: H256::zero(),
}
}
}
Expand Down Expand Up @@ -97,6 +99,7 @@ impl From<MockBlock> for Block<Transaction> {
mix_hash: Some(mock.mix_hash),
nonce: Some(mock.nonce),
base_fee_per_gas: Some(mock.base_fee_per_gas),
withdrawals_root: Some(mock.withdrawals_root),
other: OtherFields::default(),
}
}
Expand Down Expand Up @@ -127,6 +130,7 @@ impl From<MockBlock> for Block<()> {
mix_hash: Some(mock.mix_hash),
nonce: Some(mock.nonce),
base_fee_per_gas: Some(mock.base_fee_per_gas),
withdrawals_root: Some(mock.withdrawals_root),
other: OtherFields::default(),
}
}
Expand Down
29 changes: 17 additions & 12 deletions zkevm-circuits/src/evm_circuit/util/constraint_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,15 +142,15 @@ impl<F: Field> ReversionInfo<F> {
pub struct BaseConstraintBuilder<F> {
pub constraints: Vec<(&'static str, Expression<F>)>,
pub max_degree: usize,
pub condition: Option<Expression<F>>,
pub conditions: Vec<Expression<F>>,
}

impl<F: Field> BaseConstraintBuilder<F> {
pub(crate) fn new(max_degree: usize) -> Self {
BaseConstraintBuilder {
constraints: Vec::new(),
max_degree,
condition: None,
conditions: Vec::new(),
}
}

Expand Down Expand Up @@ -189,27 +189,32 @@ impl<F: Field> BaseConstraintBuilder<F> {
condition: Expression<F>,
constraint: impl FnOnce(&mut Self) -> R,
) -> R {
debug_assert!(
self.condition.is_none(),
"Nested condition is not supported"
);
self.condition = Some(condition);
self.conditions.push(condition);
let ret = constraint(self);
self.condition = None;
self.conditions.pop();
ret
}

pub(crate) fn get_condition(&self) -> Option<Expression<F>> {
if self.conditions.is_empty() {
None
} else {
Some(and::expr(self.conditions.iter()))
}
}

pub(crate) fn get_condition_expr(&self) -> Expression<F> {
self.get_condition().unwrap_or_else(|| 1.expr())
}

pub(crate) fn add_constraints(&mut self, constraints: Vec<(&'static str, Expression<F>)>) {
for (name, constraint) in constraints {
self.add_constraint(name, constraint);
}
}

pub(crate) fn add_constraint(&mut self, name: &'static str, constraint: Expression<F>) {
let constraint = match &self.condition {
Some(condition) => condition.clone() * constraint,
None => constraint,
};
let constraint = self.get_condition_expr() * constraint;
self.validate_degree(constraint.degree(), name);
self.constraints.push((name, constraint));
}
Expand Down
Loading