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

Commit 496829a

Browse files
authored
Merge branch 'main' into fix/begintx-missingconstraint
2 parents 757dbf1 + 3bbc757 commit 496829a

File tree

98 files changed

+1162
-1604
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

98 files changed

+1162
-1604
lines changed

.github/workflows/geth-utils.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ jobs:
3030
- name: Set up Go
3131
uses: actions/setup-go@v4
3232
with:
33-
go-version: '1.20'
33+
go-version: '1.21'
3434

3535
- name: Format
3636
uses: Jerome1337/[email protected]

.github/workflows/integration.yml

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -70,19 +70,9 @@ jobs:
7070
with:
7171
override: false
7272
- name: Setup golang
73-
uses: actions/setup-go@v3
73+
uses: actions/setup-go@v4
7474
with:
75-
go-version: ~1.19
76-
# Go cache for building geth-utils
77-
- name: Go cache
78-
uses: actions/cache@v3
79-
with:
80-
path: |
81-
~/.cache/go-build
82-
~/go/pkg/mod
83-
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
84-
restore-keys: |
85-
${{ runner.os }}-go-
75+
go-version: ~1.21
8676
- name: Cargo cache
8777
uses: actions/cache@v3
8878
with:

.github/workflows/lints.yml

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,10 @@ jobs:
3838
with:
3939
components: rustfmt, clippy
4040
override: false
41-
# Go cache for building geth-utils
42-
- name: Go cache
43-
uses: actions/cache@v3
41+
- name: Setup golang
42+
uses: actions/setup-go@v4
4443
with:
45-
path: |
46-
~/.cache/go-build
47-
~/go/pkg/mod
48-
key: lint-${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
49-
restore-keys: |
50-
${{ runner.os }}-go-
44+
go-version: ~1.21
5145
- name: Cargo cache
5246
uses: actions/cache@v3
5347
with:

.github/workflows/main-tests.yml

Lines changed: 7 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -46,19 +46,9 @@ jobs:
4646
with:
4747
override: false
4848
- name: Setup golang
49-
uses: actions/setup-go@v3
49+
uses: actions/setup-go@v4
5050
with:
51-
go-version: ~1.19
52-
# Go cache for building geth-utils
53-
- name: Go cache
54-
uses: actions/cache@v3
55-
with:
56-
path: |
57-
~/.cache/go-build
58-
~/go/pkg/mod
59-
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
60-
restore-keys: |
61-
${{ runner.os }}-go-
51+
go-version: ~1.21
6252
- name: Cargo cache
6353
uses: actions/cache@v3
6454
with:
@@ -104,19 +94,9 @@ jobs:
10494
with:
10595
override: false
10696
- name: Setup golang
107-
uses: actions/setup-go@v3
108-
with:
109-
go-version: ~1.19
110-
# Go cache for building geth-utils
111-
- name: Go cache
112-
uses: actions/cache@v3
97+
uses: actions/setup-go@v4
11398
with:
114-
path: |
115-
~/.cache/go-build
116-
~/go/pkg/mod
117-
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
118-
restore-keys: |
119-
${{ runner.os }}-go-
99+
go-version: ~1.21
120100
- name: Cargo cache
121101
uses: actions/cache@v3
122102
with:
@@ -155,16 +135,10 @@ jobs:
155135
override: false
156136
- name: Add target
157137
run: rustup target add x86_64-unknown-linux-gnu
158-
# Go cache for building geth-utils
159-
- name: Go cache
160-
uses: actions/cache@v3
138+
- name: Setup golang
139+
uses: actions/setup-go@v4
161140
with:
162-
path: |
163-
~/.cache/go-build
164-
~/go/pkg/mod
165-
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
166-
restore-keys: |
167-
${{ runner.os }}-go-
141+
go-version: ~1.21
168142
- name: Cargo cache
169143
uses: actions/cache@v3
170144
with:

.github/workflows/test-features.yml

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,10 @@ jobs:
3636
- uses: actions-rs/toolchain@v1
3737
with:
3838
override: false
39-
40-
# Go cache for building geth-utils
41-
- name: Go cache
42-
uses: actions/cache@v3
39+
- name: Setup golang
40+
uses: actions/setup-go@v4
4341
with:
44-
path: |
45-
~/.cache/go-build
46-
~/go/pkg/mod
47-
key: ${{ github.workflow }}-${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
48-
42+
go-version: ~1.21
4943
- name: Cargo cache
5044
uses: actions/cache@v3
5145
with:

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ stats_evm_circuit: # Print a table with EVM Circuit stats by ExecState/opcode
7777
stats_copy_circuit: # Print a table with Copy Circuit stats by ExecState/opcode
7878
@cargo run --bin stats --features stats -- copy
7979

80-
evm_exec_steps_occupancy: # Print a table for each EVM-CellManager CellType with the top 10 occupancy ExecutionSteps associated
80+
evm_exec_steps_occupancy: # Print a table for each EVM-CellManager CellType with the top 10 occupancy ExecSteps associated
8181
@cargo run --bin stats --features stats -- exec
8282

8383
.PHONY: clippy doc fmt test test_benches test-all evm_bench state_bench circuit_benches evm_exec_steps_occupancy stats_state_circuit stats_evm_circuit stats_copy_circuit help

bin/mpt-test/src/circuit/state_update.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,13 @@ pub struct StateUpdateCircuitConfig<F: Field> {
5757
/// The MPT table, where the state updates are stored
5858
pub pi_mpt: MptTable,
5959

60-
/// Intance column, used to check public inputs
60+
/// Instance column, used to check public inputs
6161
pub instance: Column<Instance>,
6262

6363
/// ONE if the first row, ZERO otherwise
6464
pub is_first: Column<Fixed>,
6565

66-
/// ONE if row is paddding, ZERO otherwise
66+
/// ONE if row is padding, ZERO otherwise
6767
pub is_padding: IsZeroConfig<F>,
6868

6969
/// ONE is the last used row, ZERO otherwise
@@ -465,7 +465,7 @@ impl<F: Field> Circuit<F> for StateUpdateCircuit<F> {
465465
).unwrap()
466466
);
467467

468-
// at beggining, set the old root and number of proofs
468+
// at beginning, set the old root and number of proofs
469469

470470
if offset == 0 {
471471
pi.push(Some(old_root_lo));

bus-mapping/README.md

Lines changed: 78 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,36 @@
11
# ZKEVM Bus-Mapping
22

3-
![GitHub Workflow Status (branch)](https://img.shields.io/github/workflow/status/appliedzkp/zkevm-circuits/CI%20checks/main?style=for-the-badge)
3+
The `Bus-Mapping` crate is designed to parse EVM execution traces and manipulate the data they provide to obtain structured witness inputs for the EVM Proof and the State Proof.
44

5-
`Bus-Mapping` is a crate designed to parse EVM execution traces and manipulate
6-
all of the data they provide in order to obtain structured witness inputs
7-
for the EVM Proof and the State Proof.
5+
## Introduction
86

9-
### Introduction
10-
At the moment every node on ethereum has to validate every transaction in
11-
the ethereum virtual machine. This means that every transaction adds work
12-
that everyone needs to do to verify Ethereum’s history. Worse still is that
13-
each transaction needs to be verified by every new node. Which means the
14-
amount of work a new node needs to do the sync the network is growing
15-
constantly. We want to build a proof of validity for the Ethereum blocks to
16-
avoid this.
7+
Currently, every node on Ethereum must validate every transaction in the Ethereum Virtual Machine (EVM). This means that each transaction adds work that everyone must do to verify Ethereum’s history. Furthermore, each transaction needs to be verified by every new node, leading to a growing amount of work for new nodes to sync the network. To address this, we aim to build a proof of validity for Ethereum blocks.
178

18-
This means making a proof of validity for the EVM + state reads / writes +
19-
signatures.
20-
To simplify we separate our proofs into two components.
9+
This involves creating a proof of validity for the EVM, including state reads/writes and signatures. To simplify, we divide our proofs into two components:
2110

22-
- State proof: State/memory/stack ops have been performed correctly. This
23-
does not check if the correct location has been read/written. We allow our
24-
prover to pick any location here and in the EVM proof confirm it is correct.
11+
- State Proof: Validates that state/memory/stack operations have been performed correctly. However, it does not verify if the correct location has been read/written. Our prover can select any location here and in the EVM proof confirm its correctness.
12+
- EVM Proof: Validates that the correct opcode is called at the correct time, checking the validity of these opcodes. It also confirms that for each of these opcodes, the state proof performed the correct operation.
2513

26-
- EVM proof: This checks that the correct opcode is called at the correct
27-
time. It checks the validity of these opcodes. It also confirms that for
28-
each of these opcodes the state proof performed the correct operation.
14+
Only after verifying both proofs can we be confident that the Ethereum block is executed correctly.
2915

30-
Only after verifying both proofs are we confident that that Ethereum block
31-
is executed correctly.
16+
## Bus Mapping
3217

33-
### Bus Mapping
3418
The goal of this crate is to serve as:
35-
- A parsing lib for EVM execution traces.
36-
- A way to infer some witness data that can only be constructed once we've
37-
analyzed the full exec trace.
38-
- An easy interface to collect all of the data to witness into the circuits
39-
and witness it with few function calls.
40-
41-
### Parsing
42-
Provided a JSON file or a JSON as a stream of bytes, which contains an
43-
execution trace from an EVM, you can parse it and construct an
44-
`ExecutionTrace` instance from it. That will automatically fill all of the
45-
bus-mapping instances of each
46-
[`ExecutionStep`](crate::exec_trace::ExecutionStep) plus fill in an
47-
[`OperationContainer`](crate::operation::container::OperationContainer) with
48-
all of the Memory, Stack and Storage ops performed by the provided trace.
19+
20+
- A parsing library for EVM execution traces.
21+
- A way to infer some witness data that can only be constructed once we've analyzed the full exec trace.
22+
- An easy interface to collect all of the data to witness into the circuits and witness it with few function calls.
23+
24+
## Parsing
25+
26+
Given a JSON file or a JSON stream containing an execution trace from an EVM, you can parse it and construct a [`GethExecTrace`](eth_types::GethExecTrace) instance from it. This will automatically populate all of the bus-mapping instances of each [`ExecStep`](crate::circuit_input_builder::ExecStep) and fill an [`OperationContainer`](crate::operation::OperationContainer) with all of the memory, stack, and storage operations performed by the provided trace.
4927

5028
```rust
51-
use bus_mapping::{ExecutionTrace, ExecutionStep, BlockConstants, Error, evm::EvmWord};
29+
use bus_mapping::mock::BlockData;
30+
use eth_types::{
31+
GethExecTrace, GethExecStep, geth_types::GethData, bytecode
32+
};
33+
use mock::test_ctx::{TestContext, helpers::*};
5234

5335
let input_trace = r#"
5436
[
@@ -98,31 +80,48 @@ let input_trace = r#"
9880
]
9981
"#;
10082

101-
let block_ctants = BlockConstants::new(
102-
Word::from(0),
103-
pasta_curves::Fp::zero(),
104-
pasta_curves::Fp::zero(),
105-
pasta_curves::Fp::zero(),
106-
pasta_curves::Fp::zero(),
107-
pasta_curves::Fp::zero(),
108-
pasta_curves::Fp::zero(),
109-
pasta_curves::Fp::zero(),
110-
);
111-
112-
// Here we have the ExecutionTrace completely formed with all of the data to witness structured.
113-
let obtained_exec_trace = ExecutionTrace::from_trace_bytes(
114-
input_trace.as_bytes(),
115-
block_ctants,
116-
).expect("Error on trace generation");
83+
// We use the [`TestContext`] struct to mock a block.
84+
let code = bytecode! {
85+
// Write 0x6f to storage slot 0
86+
PUSH1(0x6fu64)
87+
PUSH1(0x00u64)
88+
SSTORE
89+
// Load storage slot 0
90+
PUSH1(0x00u64)
91+
SLOAD
92+
STOP
93+
};
94+
// Get the execution steps from the external tracer
95+
let block: GethData = TestContext::<2, 1>::new(
96+
None,
97+
account_0_code_account_1_no_code(code),
98+
tx_from_1_to_0,
99+
|block, _tx| block.number(0xcafeu64),
100+
)
101+
.unwrap()
102+
.into();
103+
// Here we update the circuit input with the data from the transaction trace.
104+
let builder = BlockData::new_from_geth_data(block.clone()).new_circuit_input_builder();
105+
let builder = builder
106+
.handle_block(&block.eth_block, &block.geth_traces)
107+
.unwrap();
108+
let geth_steps: Vec<GethExecStep> = serde_json::from_str(input_trace).unwrap();
109+
let _geth_trace = GethExecTrace {
110+
return_value: "".to_string(),
111+
gas: block.eth_block.transactions[0].gas.as_u64(),
112+
invalid: false,
113+
failed: false,
114+
struct_logs: geth_steps,
115+
};
117116

118117
// Get an ordered vector with all of the Stack operations of this trace.
119-
let stack_ops = obtained_exec_trace.sorted_stack_ops();
120-
118+
let _stack_ops = builder.block.container.sorted_stack();
121119
// You can also iterate over the steps of the trace and witness the EVM Proof.
122-
obtained_exec_trace.steps().iter();
120+
let _ = builder.block.txs()[0].steps().iter();
123121
```
124122

125-
Assume we have the following trace:
123+
Assuming we have the following trace:
124+
126125
```text,ignore
127126
pc op stack (top -> down) memory
128127
-- -------------- ---------------------------------- ---------------------------------------
@@ -148,37 +147,28 @@ pc op stack (top -> down) memory
148147
...
149148
```
150149

151-
Once you have the trace built (following the code found above) you can
152-
basically:
153-
- Get an iterator/vector over the `Stack`, `Memory` or `Storage` operations
154-
ordered on the way the State Proof needs.
150+
Once you have built the trace (following the code above), you can:
151+
152+
- Get an iterator/vector over the `Stack`, `Memory`, or `Storage` operations ordered in the way the State Proof needs.
153+
154+
For the Memory operations, it might look like this:
155155

156-
On that way, we would get something like this for the Memory ops:
157156
```text,ignore
158-
| `key` | `val` | `rw` | `gc` | Note |
159-
|:------:| ------------- | ------- | ---- | ---------------------------------------- |
160-
| `0x40` | `0` | `Write` | | Init |
161-
| `0x40` | `0x80` | `Write` | 0 | Assume written at the begining of `code` |
162-
| `0x40` | `0x80` | `Read` | 4 | `56 MLOAD` |
163-
| - | | | | |
164-
| `0x80` | `0` | `Write` | | Init |
165-
| `0x80` | `0xdeadbeef` | `Write` | 10 | `63 MSTORE` |
166-
| `0x80` | `0xdeadbeef` | `Read` | 16 | `70 MLOAD` |
167-
| `0x80` | `0x1d97c6efb` | `Write` | 24 | `73 MSTORE` |
168-
| - | | | | |
169-
| `0xa0` | `0` | `Write` | | Init |
170-
| `0xa0` | `0xcafeb0ba` | `Write` | 34 | `83 MSTORE`
157+
| `key` | `val` | `rw` | `gc` | Note |
158+
| :----: | ------------- | ------- | ---- | ----------------------------------------- |
159+
| `0x40` | `0` | `Write` | | Init |
160+
| `0x40` | `0x80` | `Write` | 0 | Assume written at the beginning of `code` |
161+
| `0x40` | `0x80` | `Read` | 4 | `56 MLOAD` |
162+
| - | | | | |
163+
| `0x80` | `0` | `Write` | | Init |
164+
| `0x80` | `0xdeadbeef` | `Write` | 10 | `63 MSTORE` |
165+
| `0x80` | `0xdeadbeef` | `Read` | 16 | `70 MLOAD` |
166+
| `0x80` | `0x1d97c6efb` | `Write` | 24 | `73 MSTORE` |
167+
| - | | | | |
168+
| `0xa0` | `0` | `Write` | | Init |
169+
| `0xa0` | `0xcafeb0ba` | `Write` | 34 | `83 MSTORE` |
171170
```
172171

173-
Where as you see, we group by `memory_address` and then order by
174-
`global_counter`.
175-
176-
- Iterate over the `ExecutionTrace` itself over
177-
each `ExecutionStep`'s is formed by and check which Stack/Memory&Storage operations are linked to each step.
178-
This is also automatically done via the
179-
[`Opcode`](crate::evm::opcodes::Opcode) trait defined in this crate.
172+
Here, we group by `memory_address` and then order by `global_counter`.
180173

181-
### Documentation
182-
For extra documentation, check the book with the specs written for the
183-
entire ZK-EVM solution.
184-
See: <https://hackmd.io/@liangcc/zkvmbook/https%3A%2F%2Fhackmd.io%2FAmhZ2ryITxicmhYFyQ0DEw#Bus-Mapping>
174+
- Iterate over the [`GethExecTrace`](eth_types::GethExecTrace) itself, over each [`ExecStep`](crate::circuit_input_builder::ExecStep) formed by, and check which Stack/Memory&Storage operations are linked to each step. This is also automatically done via the [`Opcode`](crate::evm::opcodes::Opcode) trait defined in this crate.

bus-mapping/src/circuit_input_builder.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ impl FeatureConfig {
8484
#[derive(Debug, Clone, Copy)]
8585
pub struct FixedCParams {
8686
/// Maximum number of rw operations in the state circuit (RwTable length /
87-
/// nummber of rows). This must be at least the number of rw operations
87+
/// number of rows). This must be at least the number of rw operations
8888
/// + 1, in order to allocate at least a Start row.
8989
pub max_rws: usize,
9090
// TODO: evm_rows: Maximum number of rows in the EVM Circuit
@@ -190,7 +190,7 @@ pub struct CircuitInputBuilder<C: CircuitsParams> {
190190
pub code_db: CodeDB,
191191
/// Block
192192
pub block: Block,
193-
/// Circuits Setup Paramteres
193+
/// Circuits Setup Parameters
194194
pub circuits_params: C,
195195
/// Block Context
196196
pub block_ctx: BlockContext,
@@ -544,7 +544,7 @@ pub fn keccak_inputs(block: &Block, code_db: &CodeDB) -> Result<Vec<Vec<u8>>, Er
544544
}
545545

546546
/// Generate the keccak inputs required by the SignVerify Chip from the
547-
/// signature datas.
547+
/// signature data.
548548
pub fn keccak_inputs_sign_verify(sigs: &[SignData]) -> Vec<Vec<u8>> {
549549
let mut inputs = Vec::new();
550550
for sig in sigs {

0 commit comments

Comments
 (0)