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

Circuit tools tests #18

Open
wants to merge 100 commits into
base: circuit-tools
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
100 commits
Select commit Hold shift + click to select a range
2654320
drifted RLP consistency checks
CeciliaZ030 Jun 11, 2023
79576ea
rlp limitations
CeciliaZ030 Jun 12, 2023
f8b7df2
update
CeciliaZ030 Jun 12, 2023
cba60ea
update
CeciliaZ030 Jun 12, 2023
484a775
below limit
CeciliaZ030 Jun 12, 2023
3b38805
MainData::store at wrong place
CeciliaZ030 Jun 12, 2023
dcdc8b0
update
CeciliaZ030 Jun 12, 2023
fb1fa7c
chaned main store loc
CeciliaZ030 Jun 12, 2023
f84e171
root changed issue
CeciliaZ030 Jun 13, 2023
e0ef76b
2**21 not enough
CeciliaZ030 Jun 13, 2023
0502662
Fix: support both s and c
Brechtpd Jun 13, 2023
94cc431
fixed
CeciliaZ030 Jun 13, 2023
aea95e0
fixed
CeciliaZ030 Jun 13, 2023
4c33368
fixed
CeciliaZ030 Jun 13, 2023
d403029
config with params
CeciliaZ030 Jun 13, 2023
86406b1
assign corner case
CeciliaZ030 Jun 13, 2023
62340db
assign corner case
CeciliaZ030 Jun 13, 2023
0319e4b
put limit in main rlp
CeciliaZ030 Jun 14, 2023
bc8c25c
require string
CeciliaZ030 Jun 14, 2023
a13a2df
clean up
CeciliaZ030 Jun 14, 2023
d0bde05
clippy
CeciliaZ030 Jun 14, 2023
cd6d992
Update zkevm-circuits/src/mpt_circuit.rs
CeciliaZ030 Jun 14, 2023
ac10dae
clean up
CeciliaZ030 Jun 14, 2023
92d7f63
Merge pull request #4 from CeciliaZ030/storage-below-non-existing-branch
CeciliaZ030 Jun 14, 2023
0557809
fix fmt
Brechtpd Jun 14, 2023
5944d34
MPT improvements (#1462)
Brechtpd Jun 14, 2023
d3b0176
params changed
CeciliaZ030 Jun 15, 2023
05e68a8
Merge commit '05578094826520b9609796034dfe0188641c8aad' into two-byte…
CeciliaZ030 Jun 15, 2023
a695217
cargo fmt
CeciliaZ030 Jun 15, 2023
78dafb6
clean up
CeciliaZ030 Jun 15, 2023
0955449
Merge commit '05578094826520b9609796034dfe0188641c8aad' into rlp-limi…
CeciliaZ030 Jun 15, 2023
608ce66
cargo fix & fmt
CeciliaZ030 Jun 15, 2023
5f1931d
Merge commit '05578094826520b9609796034dfe0188641c8aad' into drifted-…
CeciliaZ030 Jun 15, 2023
8d279ff
cargo fmt
CeciliaZ030 Jun 15, 2023
d256f6f
Update zkevm-circuits/src/mpt_circuit/account_leaf.rs
CeciliaZ030 Jun 15, 2023
b73e82b
Update zkevm-circuits/src/mpt_circuit/account_leaf.rs
CeciliaZ030 Jun 15, 2023
1e7a4f7
Merge pull request #2 from CeciliaZ030/drifted-rlp-cinsistency
CeciliaZ030 Jun 15, 2023
df75378
removed unused macros
CeciliaZ030 Jun 15, 2023
db0248c
Update zkevm-circuits/src/mpt_circuit.rs
CeciliaZ030 Jun 15, 2023
3ffc50c
Update account_leaf.rs
CeciliaZ030 Jun 15, 2023
a84ad21
Apply suggestions from code review
CeciliaZ030 Jun 15, 2023
51b28c9
remove
CeciliaZ030 Jun 15, 2023
fc0c61c
Update zkevm-circuits/src/mpt_circuit/lib.rs
CeciliaZ030 Jun 15, 2023
560afd4
Update zkevm-circuits/src/mpt_circuit/extension.rs
CeciliaZ030 Jun 15, 2023
7e8b7b2
Update zkevm-circuits/src/mpt_circuit/start.rs
CeciliaZ030 Jun 15, 2023
1a6f8c1
Update zkevm-circuits/src/mpt_circuit/helpers.rs
CeciliaZ030 Jun 15, 2023
ef7a604
Update zkevm-circuits/src/mpt_circuit/param.rs
CeciliaZ030 Jun 15, 2023
8518142
Update zkevm-circuits/src/mpt_circuit/helpers.rs
CeciliaZ030 Jun 15, 2023
30694a8
Update storage_leaf.rs
CeciliaZ030 Jun 15, 2023
f43f1d2
special case
CeciliaZ030 Jun 15, 2023
5dd8806
MAIN_RLP_STRING_MAX = 34
CeciliaZ030 Jun 15, 2023
a081205
fix second byte is zero case
CeciliaZ030 Jun 16, 2023
23fc883
update
CeciliaZ030 Jun 16, 2023
a2f93ff
cargo fix
CeciliaZ030 Jun 16, 2023
1ef9314
Some misc improvements
Brechtpd Jun 16, 2023
09414a3
Merge pull request #3 from CeciliaZ030/rlp-limitation
Brechtpd Jun 17, 2023
039a01e
Misc improvements
Brechtpd Jun 17, 2023
98911f3
Merge remote-tracking branch 'origin/circuit-tools-wip' into two-byte…
Brechtpd Jun 17, 2023
5d29b5b
Merge pull request #5 from CeciliaZ030/two-bytes-range-check-mainRLP
Brechtpd Jun 17, 2023
5143f0e
fails at some test case
CeciliaZ030 Jun 26, 2023
7bd5273
temp
CeciliaZ030 Jun 26, 2023
fb95240
empty long string doesn't need constain
CeciliaZ030 Jun 26, 2023
d341d7a
MPT new descriptors & benchmark (#1492)
miha-stopar Jun 26, 2023
cda78b6
root node value is hash, may has zero leading byte
CeciliaZ030 Jun 26, 2023
5ff1914
cargo fmt
CeciliaZ030 Jun 26, 2023
0b10a1d
clean
CeciliaZ030 Jun 27, 2023
29113b4
Misc improvements
Brechtpd Jul 3, 2023
b79f850
Merge branch 'circuit-tools-wip' into challenge-api
Brechtpd Jul 3, 2023
db4f8c3
Merge pull request #6 from CeciliaZ030/challenge-api
Brechtpd Jul 3, 2023
c0aa530
Merge branch 'circuit-tools-wip' into rlp-min-encodeing-switch
Brechtpd Jul 3, 2023
306c069
Fix an issue with rlp consistency check
Brechtpd Jul 3, 2023
6fa4881
Merge pull request #11 from CeciliaZ030/rlp-min-encodeing-switch
Brechtpd Jul 3, 2023
f99154f
Merge branch 'mpt2' into circuit-tools-wip
Brechtpd Jul 3, 2023
c73ffc1
Merge remote-tracking branch 'origin/circuit-tools-wip' into circuit-…
Brechtpd Jul 3, 2023
f0d95a5
Removed some old params + renamings from feedback
Brechtpd Jul 4, 2023
2addd74
cm with ctx
CeciliaZ030 Jun 29, 2023
7cbb5b9
update
CeciliaZ030 Jul 5, 2023
0da67be
try running benchmark
CeciliaZ030 Jul 5, 2023
e289a47
add test back
CeciliaZ030 Jun 16, 2023
3c21576
update
CeciliaZ030 Jul 5, 2023
70bb384
query and branch example
CeciliaZ030 Jul 5, 2023
839753e
query and branch
CeciliaZ030 Jul 5, 2023
469ee53
shuffle example for cm
CeciliaZ030 Jul 6, 2023
29c0476
shuffle
CeciliaZ030 Jul 6, 2023
2695500
shuffle done
CeciliaZ030 Jul 7, 2023
47dea2c
update
CeciliaZ030 Jul 7, 2023
25cdf70
store_expression with tartget cell
CeciliaZ030 Jul 9, 2023
8474d61
build lookups
CeciliaZ030 Jul 9, 2023
16dd874
need to fix memory key
CeciliaZ030 Jul 9, 2023
95afdad
fixed with mutable mpt ctx
CeciliaZ030 Jul 9, 2023
ee8b21c
TODO(Brecht): not written
CeciliaZ030 Jul 9, 2023
32a4519
cleanup prints
CeciliaZ030 Jul 9, 2023
1599b25
replaced memory
CeciliaZ030 Jul 9, 2023
2149cbc
clippy & fmt
CeciliaZ030 Jul 9, 2023
7ebfaf5
Merge pull request #20 from CeciliaZ030/memory-bank
CeciliaZ030 Jul 10, 2023
964e8b5
Merge branch 'circuit-tools-tests' of https://github.com/CeciliaZ030/…
CeciliaZ030 Jul 10, 2023
7e35d56
simple rlp
CeciliaZ030 Jul 10, 2023
5017353
database
CeciliaZ030 Jul 10, 2023
da7eb69
restart
CeciliaZ030 Aug 1, 2023
db9b3b9
query selector
CeciliaZ030 Aug 1, 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
2 changes: 2 additions & 0 deletions Cargo.lock

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

3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ evm_bench: ## Run Evm Circuit benchmarks
state_bench: ## Run State Circuit benchmarks
@cargo test --profile bench bench_state_circuit_prover -p circuit-benchmarks --features benches -- --nocapture

mpt_bench: ## Run MPT Circuit benchmarks
@cargo test --profile bench bench_mpt_circuit_prover -p circuit-benchmarks --features benches -- --nocapture

packed_multi_keccak_bench: ## Run Packed Multi Keccak Circuit benchmarks
@cargo test --profile bench bench_packed_multi_keccak_circuit_prover -p circuit-benchmarks --features benches -- --nocapture

Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ to use for your circuit in the bench process.
- Keccak Circuit prover benches. -> `DEGREE=16 make packed_multi_keccak_bench`
- EVM Circuit prover benches. -> `DEGREE=18 make evm_bench`.
- State Circuit prover benches. -> `DEGREE=18 make state_bench`
- MPT Circuit prover benches. -> `DEGREE=14 make mpt_bench`

You can also run all benchmarks by running: `make circuit_benches DEGREE=18`.

Expand Down
4 changes: 3 additions & 1 deletion circuit-benchmarks/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ env_logger = "0.9"
ethers-signers = "0.17.0"
mock = { path="../mock" }
rand_chacha = "0.3"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"

[features]
default = []
default = ["benches"]
benches = []
4 changes: 4 additions & 0 deletions circuit-benchmarks/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,7 @@ pub mod exp_circuit;
#[cfg(test)]
#[cfg(feature = "benches")]
pub mod constants;

#[cfg(test)]
#[cfg(feature = "benches")]
pub mod mpt_circuit;
127 changes: 127 additions & 0 deletions circuit-benchmarks/src/mpt_circuit.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
//! MPT circuit benchmarks

#[cfg(test)]
mod tests {
use ark_std::{end_timer, start_timer};
use halo2_proofs::{
halo2curves::bn256::{Bn256, Fr, G1Affine},
plonk::{create_proof, keygen_pk, keygen_vk, verify_proof},
poly::{
commitment::ParamsProver,
kzg::{
commitment::{KZGCommitmentScheme, ParamsKZG, ParamsVerifierKZG},
multiopen::{ProverSHPLONK, VerifierSHPLONK},
strategy::SingleStrategy,
},
},
transcript::{
Blake2bRead, Blake2bWrite, Challenge255, TranscriptReadBuffer, TranscriptWriterBuffer,
},
};
use rand::SeedableRng;
use rand_xorshift::XorShiftRng;
use std::env::var;
use zkevm_circuits::mpt_circuit::{witness_row::Node, MPTCircuit};

#[cfg_attr(not(feature = "benches"), ignore)]
#[test]
fn bench_mpt_circuit_prover() {
let setup_prfx = crate::constants::SETUP_PREFIX;
let proof_gen_prfx = crate::constants::PROOFGEN_PREFIX;
let proof_ver_prfx = crate::constants::PROOFVER_PREFIX;
// Unique string used by bench results module for parsing the result
const BENCHMARK_ID: &str = "MPT Circuit";

let degree: u32 = var("DEGREE")
.expect("No DEGREE env var was provided")
.parse()
.expect("Cannot parse DEGREE env var as u32");

let path = "../zkevm-circuits/src/mpt_circuit/tests/UpdateOneLevel.json";
let file = std::fs::File::open(path);
let reader = std::io::BufReader::new(file.unwrap());

let randomness = Fr::from(123456u64);
let nodes: Vec<Node> = serde_json::from_reader(reader).unwrap();

let mut keccak_data = vec![];
for node in nodes.iter() {
for k in node.keccak_data.iter() {
keccak_data.push(k.clone());
}
}

let circuit = MPTCircuit::<Fr> {
nodes,
keccak_data,
degree: degree as usize,
randomness,
};

// Initialize the polynomial commitment parameters
let mut rng = XorShiftRng::from_seed([
0x59, 0x62, 0xbe, 0x5d, 0x76, 0x3d, 0x31, 0x8d, 0x17, 0xdb, 0x37, 0x32, 0x54, 0x06,
0xbc, 0xe5,
]);

// Bench setup generation
let setup_message = format!("{} {} with degree = {}", BENCHMARK_ID, setup_prfx, degree);
let start1 = start_timer!(|| setup_message);
let general_params = ParamsKZG::<Bn256>::setup(degree, &mut rng);
let verifier_params: ParamsVerifierKZG<Bn256> = general_params.verifier_params().clone();
end_timer!(start1);

// Initialize the proving key
let vk = keygen_vk(&general_params, &circuit).expect("keygen_vk should not fail");
let pk = keygen_pk(&general_params, vk, &circuit).expect("keygen_pk should not fail");
// Create a proof
let mut transcript = Blake2bWrite::<_, G1Affine, Challenge255<_>>::init(vec![]);

// Bench proof generation time
let proof_message = format!(
"{} {} with degree = {}",
BENCHMARK_ID, proof_gen_prfx, degree
);
let start2 = start_timer!(|| proof_message);

create_proof::<
KZGCommitmentScheme<Bn256>,
ProverSHPLONK<'_, Bn256>,
Challenge255<G1Affine>,
XorShiftRng,
Blake2bWrite<Vec<u8>, G1Affine, Challenge255<G1Affine>>,
MPTCircuit<Fr>,
>(
&general_params,
&pk,
&[circuit],
&[&[]],
rng,
&mut transcript,
)
.expect("proof generation should not fail");
let proof = transcript.finalize();
end_timer!(start2);

// Bench verification time
let start3 = start_timer!(|| format!("{} {}", BENCHMARK_ID, proof_ver_prfx));
let mut verifier_transcript = Blake2bRead::<_, G1Affine, Challenge255<_>>::init(&proof[..]);
let strategy = SingleStrategy::new(&general_params);

verify_proof::<
KZGCommitmentScheme<Bn256>,
VerifierSHPLONK<'_, Bn256>,
Challenge255<G1Affine>,
Blake2bRead<&[u8], G1Affine, Challenge255<G1Affine>>,
SingleStrategy<'_, Bn256>,
>(
&verifier_params,
pk.get_vk(),
strategy,
&[&[]],
&mut verifier_transcript,
)
.expect("failed to verify bench circuit");
end_timer!(start3);
}
}
4 changes: 2 additions & 2 deletions zkevm-circuits/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ rand_chacha = "0.3"
snark-verifier = { git = "https://github.com/privacy-scaling-explorations/snark-verifier", tag = "v2023_04_20", default-features = false, features = ["loader_halo2", "system_halo2"] }
cli-table = { version = "0.4", optional = true }
num_enum = "0.5.7"
serde = { version = "1.0.130", features = ["derive"] }
serde_json = "1.0.78"

[dev-dependencies]
bus-mapping = { path = "../bus-mapping", features = ["test"] }
Expand All @@ -45,8 +47,6 @@ hex = "0.4.3"
itertools = "0.10.1"
mock = { path = "../mock" }
pretty_assertions = "1.0.0"
serde = { version = "1.0.130", features = ["derive"] }
serde_json = "1.0.78"

[features]
default = []
Expand Down
1 change: 1 addition & 0 deletions zkevm-circuits/src/circuit_tools.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ pub mod cached_region;
pub mod cell_manager;
pub mod gadgets;
pub mod memory;
mod test;
39 changes: 22 additions & 17 deletions zkevm-circuits/src/circuit_tools/cached_region.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,28 +22,26 @@ impl<F: Field, V: AsRef<[Value<F>]>> ChallengeSet<F> for V {
}
}

pub struct CachedRegion<'r, 'b, F: Field, S: ChallengeSet<F>> {
pub struct CachedRegion<'r, 'b, F: Field> {
region: &'r mut Region<'b, F>,
pub advice: HashMap<(usize, usize), F>,
pub fixed: HashMap<(usize, usize), F>,
challenges: &'r S,
disable_description: bool,
regions: Vec<(usize, usize)>,
pub le_r: F,
pub be_r: F,
pub r: F,
pub keccak_r: F,
}

impl<'r, 'b, F: Field, S: ChallengeSet<F>> CachedRegion<'r, 'b, F, S> {
pub(crate) fn new(region: &'r mut Region<'b, F>, challenges: &'r S, le_r: F, be_r: F) -> Self {
impl<'r, 'b, F: Field> CachedRegion<'r, 'b, F> {
pub(crate) fn new(region: &'r mut Region<'b, F>, r: F, keccak_r: F) -> Self {
Self {
region,
advice: HashMap::new(),
fixed: HashMap::new(),
challenges,
disable_description: false,
regions: Vec::new(),
le_r,
be_r,
r,
keccak_r,
}
}

Expand All @@ -59,14 +57,19 @@ impl<'r, 'b, F: Field, S: ChallengeSet<F>> CachedRegion<'r, 'b, F, S> {
// Nothing to do
}

pub(crate) fn assign_stored_expressions<C: CellType>(
pub(crate) fn lookup_challenge(&self) -> F {
self.r.clone()
}

pub(crate) fn assign_stored_expressions<C: CellType, S: ChallengeSet<F>>(
&mut self,
cb: &ConstraintBuilder<F, C>,
challenges: &S,
) -> Result<(), Error> {
for (offset, region_id) in self.regions.clone() {
for stored_expression in cb.get_stored_expressions(region_id).iter() {
// println!("stored expression: {}", stored_expression.name);
stored_expression.assign(self, offset)?;
stored_expression.assign(self, challenges, offset)?;
}
}
Ok(())
Expand Down Expand Up @@ -127,7 +130,12 @@ impl<'r, 'b, F: Field, S: ChallengeSet<F>> CachedRegion<'r, 'b, F, S> {
A: Fn() -> AR,
AR: Into<String>,
{
// Actually set the value
let res = self.region.assign_fixed(annotation, column, offset, &to);
// Cache the value
// Note that the `value_field` in `AssignedCell` might be `Value::unkonwn` if
// the column has different phase than current one, so we call to `to`
// again here to cache the value.
if res.is_ok() {
to().map(|f: VR| {
let existing = self
Expand Down Expand Up @@ -156,10 +164,6 @@ impl<'r, 'b, F: Field, S: ChallengeSet<F>> CachedRegion<'r, 'b, F, S> {
.unwrap_or(&zero)
}

pub fn challenges(&self) -> &S {
self.challenges
}

/// Constrains a cell to have a constant value.
///
/// Returns an error if the cell is in a column where equality has not been
Expand Down Expand Up @@ -195,7 +199,8 @@ impl<F, C: CellType> Hash for StoredExpression<F, C> {
impl<F: Field, C: CellType> StoredExpression<F, C> {
pub fn assign<S: ChallengeSet<F>>(
&self,
region: &mut CachedRegion<'_, '_, F, S>,
region: &mut CachedRegion<'_, '_, F>,
challenges: &S,
offset: usize,
) -> Result<Value<F>, Error> {
let value = self.expr.evaluate(
Expand All @@ -216,7 +221,7 @@ impl<F: Field, C: CellType> StoredExpression<F, C> {
))
},
&|_| unimplemented!("instance column"),
&|challenge| *region.challenges().indexed()[challenge.index()],
&|challenge| *challenges.indexed()[challenge.index()],
&|a| -a,
&|a, b| a + b,
&|a, b| a * b,
Expand Down
Loading
Loading