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

feat: panic on invalid account lookups#1092

Merged
ed255 merged 10 commits into
mainfrom
feature/detect-invalid-account-lookups
Jan 25, 2023
Merged

feat: panic on invalid account lookups#1092
ed255 merged 10 commits into
mainfrom
feature/detect-invalid-account-lookups

Conversation

@ed255
Copy link
Copy Markdown
Contributor

@ed255 ed255 commented Jan 19, 2023

After this was merged privacy-ethereum/zkevm-specs#354 the rule for opcodes that can access non-existing accounts is to first read the CodeHash, and only if it's not 0 (meaning that the account exists), proceed with other account field reads. If a field read is performed to a non-existing account, this may end up mapping to an MPT account field proof for a non-existing account (which are not supported); so the MPT proof would be impossible to generate. We can add some checks to make sure we don't implement any circuit that generates this situation; but we can even add more sanity checks to detect possible bugs early. In particular we can check for each account field lookup (either read or write):

  • That a READ doesn't change the field value
  • That the previous value matches the account field value in the StateDB
  • That no read is done to a field other than CodeHash to a non-existing
    account (only CodeHash reads with value=0 can be done to non-existing
    accounts, which the State Circuit translates to MPT
    AccountNonExisting proofs lookups).

If any of these two points don't hold, the corresponding chain of MPT proofs
can't be generated.

In order to perform this checks in a single place, I've moved the logic to update the accounts in the StateDB performed by the circuit input builder in a single place, after the check (instead of requiring each bus-mapping opcode implementation to do it by themselves).

With these checks I found some bugs in the callop and error_oog_call, which I have fixed.

As a bonus I added the method debug_print_txs_steps_rw_ops to the witness::Block which prints all the rw operations (with their content) of each step in each tx, which was very useful for debugging.

Resolve #1084

Depends on #1069

@github-actions github-actions Bot added crate-bus-mapping Issues related to the bus-mapping workspace member crate-zkevm-circuits Issues related to the zkevm-circuits workspace member labels Jan 19, 2023
@ed255 ed255 force-pushed the feature/detect-invalid-account-lookups branch from 0fabc6d to 2108aac Compare January 19, 2023 11:04
@ed255 ed255 force-pushed the feature/detect-invalid-account-lookups branch from 84f4b43 to c74896e Compare January 20, 2023 11:42
@ed255 ed255 force-pushed the feature/detect-invalid-account-lookups branch 2 times, most recently from 3edb370 to 89dd62d Compare January 23, 2023 15:29
Comment thread bus-mapping/src/circuit_input_builder/input_state_ref.rs
Copy link
Copy Markdown
Collaborator

@ChihChengLiang ChihChengLiang left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. I added some feedback.

Comment thread zkevm-circuits/src/evm_circuit/execution/begin_tx.rs
Comment thread zkevm-circuits/src/evm_circuit/execution/callop.rs Outdated
Comment thread zkevm-circuits/src/evm_circuit/execution/error_oog_call.rs Outdated
ed255 added 9 commits January 25, 2023 11:44
- Account field lookups for fields different than CodeHash are invalid if the
  account doesn't exist.
- All account field lookups should contain a value_prev that matches with the
  field value found in the StateDB for that account.

If any of these two points don't hold, the corresponding chain of MPT proofs
can't be generated.

Test with:
`cargo test --profile release evm_circuit --all-features -- --nocapture 2>&1 | tee /tmp/zkevm-tests.log` from `zkevm-circuits`.
@ed255 ed255 force-pushed the feature/detect-invalid-account-lookups branch from 89dd62d to 1ccc071 Compare January 25, 2023 11:02
@ed255 ed255 force-pushed the feature/detect-invalid-account-lookups branch from 1ccc071 to 4853f08 Compare January 25, 2023 11:22
@ed255 ed255 merged commit 0de9718 into main Jan 25, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

crate-bus-mapping Issues related to the bus-mapping workspace member crate-zkevm-circuits Issues related to the zkevm-circuits workspace member

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Review and add checks to verify account updates consistency

3 participants