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

RETURN gadget for non-create calls#719

Merged
z2trillion merged 49 commits into
privacy-ethereum:mainfrom
scroll-tech:feat/return_cleanup
Oct 6, 2022
Merged

RETURN gadget for non-create calls#719
z2trillion merged 49 commits into
privacy-ethereum:mainfrom
scroll-tech:feat/return_cleanup

Conversation

@z2trillion
Copy link
Copy Markdown
Contributor

@z2trillion z2trillion commented Aug 29, 2022

This gadget implements the specs in https://github.com/scroll-tech/zkevm-specs/blob/master/specs/opcode/F3RETURN.md, except for "A. Returns the specified memory chunk as deployment code." I plan to add the copy lookup for code deployments in a follow up PR.

It also does handles the REVERT opcode, which is generally similar to RETURN.

TODO: add REVERT spec.


@z2trillion z2trillion marked this pull request as draft August 29, 2022 16:14
@z2trillion z2trillion mentioned this pull request Aug 29, 2022
@github-actions github-actions Bot added crate-bus-mapping Issues related to the bus-mapping workspace member crate-eth-types Issues related to the eth-types workspace member crate-zkevm-circuits Issues related to the zkevm-circuits workspace member labels Aug 29, 2022
@ed255 ed255 linked an issue Sep 7, 2022 that may be closed by this pull request
@github-actions github-actions Bot added the crate-mock Issues related to the mock workspace member label Sep 22, 2022
Copy link
Copy Markdown
Contributor

@ed255 ed255 left a comment

Choose a reason for hiding this comment

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

In general looks good! I noticed 2 things that may be missing (the memory expansion gas cost, and state transition when root), and I also added a few questions.

Comment thread zkevm-circuits/src/evm_circuit/util/common_gadget.rs Outdated
Comment thread zkevm-circuits/src/evm_circuit/execution/return.rs
Comment thread zkevm-circuits/src/evm_circuit/execution/return.rs
Comment thread zkevm-circuits/src/evm_circuit/execution/return.rs Outdated
@z2trillion z2trillion requested a review from ed255 September 30, 2022 19:27
Copy link
Copy Markdown
Contributor

@ed255 ed255 left a comment

Choose a reason for hiding this comment

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

LGTM! Very nice work! 😃

@ed255
Copy link
Copy Markdown
Contributor

ed255 commented Oct 4, 2022

@roynalnaruto could you help with a second review to this PR? Thanks!

@github-actions github-actions Bot added the crate-external-tracer Issues related to the external-tracer workspace member label Oct 5, 2022
@z2trillion
Copy link
Copy Markdown
Contributor Author

#817 broke the test here, but I don't know why.

@ed255
Copy link
Copy Markdown
Contributor

ed255 commented Oct 5, 2022

#817 broke the test here, but I don't know why.

Ah, the test that fails is evm_circuit::execution::r#return::test::test_return_nonroot. Maybe there's a bug in the memory reconstruction logic somewhere?

@ed255
Copy link
Copy Markdown
Contributor

ed255 commented Oct 5, 2022

I think I found the bug, we were reading from geth_step memory in the circuit input builder, but that's not populated when enable_memory = false, so we should always avoid it and rebuild the memory / memory.length. This should fix it:

diff --git a/bus-mapping/src/circuit_input_builder/input_state_ref.rs b/bus-mapping/src/circuit_input_builder/input_state_ref.rs
index f243f0cb..3fe96207 100644
--- a/bus-mapping/src/circuit_input_builder/input_state_ref.rs
+++ b/bus-mapping/src/circuit_input_builder/input_state_ref.rs
@@ -889,10 +889,7 @@ impl<'a> CircuitInputStateRef<'a> {
                 geth_step_next.stack.stack_pointer().0.into(),
             ),
             (CallContextField::GasLeft, caller_gas_left.into()),
-            (
-                CallContextField::MemorySize,
-                geth_step_next.memory.word_size().into(),
-            ),
+            (CallContextField::MemorySize, next_memory_word_size.into()),
             (
                 CallContextField::ReversibleWriteCounter,
                 self.caller_ctx()?.reversible_write_counter.into(),

@z2trillion can you try this change, along with enable_memory: false in LoggerConfig::default()?

@github-actions github-actions Bot removed the crate-external-tracer Issues related to the external-tracer workspace member label Oct 5, 2022
@z2trillion
Copy link
Copy Markdown
Contributor Author

I think I found the bug, we were reading from geth_step memory in the circuit input builder, but that's not populated when enable_memory = false, so we should always avoid it and rebuild the memory / memory.length. This should fix it:

diff --git a/bus-mapping/src/circuit_input_builder/input_state_ref.rs b/bus-mapping/src/circuit_input_builder/input_state_ref.rs
index f243f0cb..3fe96207 100644
--- a/bus-mapping/src/circuit_input_builder/input_state_ref.rs
+++ b/bus-mapping/src/circuit_input_builder/input_state_ref.rs
@@ -889,10 +889,7 @@ impl<'a> CircuitInputStateRef<'a> {
                 geth_step_next.stack.stack_pointer().0.into(),
             ),
             (CallContextField::GasLeft, caller_gas_left.into()),
-            (
-                CallContextField::MemorySize,
-                geth_step_next.memory.word_size().into(),
-            ),
+            (CallContextField::MemorySize, next_memory_word_size.into()),
             (
                 CallContextField::ReversibleWriteCounter,
                 self.caller_ctx()?.reversible_write_counter.into(),

@z2trillion can you try this change, along with enable_memory: false in LoggerConfig::default()?

Thanks for finding the spot with the error. The correct new value is self.caller_ctx()?.memory.word_size().into(),

Copy link
Copy Markdown
Collaborator

@roynalnaruto roynalnaruto left a comment

Choose a reason for hiding this comment

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

Everything looks good to me! I have a comment though, with regards to is_success not being constrained as boolean (in the ReturnGadget)

Comment thread zkevm-circuits/src/evm_circuit/execution/return.rs
@z2trillion z2trillion merged commit 809895b into privacy-ethereum:main Oct 6, 2022
@z2trillion z2trillion deleted the feat/return_cleanup branch October 6, 2022 03:15
lispc added a commit that referenced this pull request Aug 28, 2023
…S auditors (#572)

* fix finding 3 (#575)

* Fix zellic finding 4 (#576)

* fix finding 3 (#575)

* fix finding 4

---------

Co-authored-by: Rohit Narurkar <rohit.narurkar@protonmail.com>

* add range check on diffs (#586)

* Fix finding 10 (#578)

* fix finding 3 (#575)

* fix finding 10

* Fix finding 13 (#579)

* fix finding 3 (#575)

* fix finding 13

* Fix zellic finding 14 (#580)

* fix finding 3 (#575)

* fix finding 14

* Fix zellic finding 5 (#584)

* fix finding 3 (#575)

* fix finding 5

* refine comments

* fmt

* Fix finding 17 (#602)

* add q_last

* fix

* add more diff range check

* fix finding 7 (#625)

* tx_id = 1 when sm starts

* Fix finding 11 : use length for rlc in rlp table (#719)

* fix: use tag_bytes_rlc and tag_length to copy tag's bytes around

* fix lookup input for Len & RLC & GasCost fields in tx circuit

* refactor

* fix

* refactor

* fix col phase issue

* refactor bytes_rlc type

* Fix the bugs in Tx & PI circuits reported by Zellic & KALOS auditors (#612)

* lookup chain_id to RLP table

* fix finding 22 (#614)

* fix finding 21 (#613)

* fix finding 23 (#618)

* fix finding 26 (#622)

* fix finding 28 (#624)

Co-authored-by: Rohit Narurkar <rohit.narurkar@protonmail.com>

* fix finding 29 (#623)

Co-authored-by: Rohit Narurkar <rohit.narurkar@protonmail.com>

* enforce is_final is true at the last row and fix RLC related vul (#735)

* Fix finding 30  (#733)

* enforce all txs in a block are included in the tx table

* clippy

---------

Co-authored-by: Rohit Narurkar <rohit.narurkar@protonmail.com>

* Fix Zellic / Kalos finding25 (#619)

* fix finding 25

* add comment

---------

Co-authored-by: Rohit Narurkar <rohit.narurkar@protonmail.com>

* fix conflicts

---------

Co-authored-by: Rohit Narurkar <rohit.narurkar@protonmail.com>
Co-authored-by: Zhang Zhuo <mycinbrin@gmail.com>

* use q_first instead

* fmt

---------

Co-authored-by: Rohit Narurkar <rohit.narurkar@protonmail.com>
Co-authored-by: Zhang Zhuo <mycinbrin@gmail.com>
lispc added a commit that referenced this pull request Aug 28, 2023
* add row counting interface for keccak

* add class level capacity calculator for keccak

* remove f capacity from core

* remove capacity calculator in aggregator util

* remove unnecessary imports

* replace max keccak round in core

* replace reference for max keccak

* remove unnecessary keccak imports and constants

* remove max keccak constant

* remove constants in hash cell parsing

* remove constant column sanity check

* add state column usage log

* adjust input bytes column

* add long column padding

* correct fmt

* fix fmt

* minor fixes

* fix

* Fix: allow skipping of L1Msg tx part 2 (calculate num_all_txs in tx circuit) (#778)

* calculate num_l1_msgs and num_l2_txs in tx circuit

* fix

* fmt and clippy

* fix: non-last tx requires next is calldata

* add NumAllTxs in block table and copy it from pi to block table

* add lookup for NumAllTxs in tx circuit

* clippy

* add block num diff check to avoid two real block have same num

* clippy

* address comments

* Fix the bugs in RLP/Tx/PI circuit which are reported by Zellic & KALOS auditors (#572)

* fix finding 3 (#575)

* Fix zellic finding 4 (#576)

* fix finding 3 (#575)

* fix finding 4

---------

Co-authored-by: Rohit Narurkar <rohit.narurkar@protonmail.com>

* add range check on diffs (#586)

* Fix finding 10 (#578)

* fix finding 3 (#575)

* fix finding 10

* Fix finding 13 (#579)

* fix finding 3 (#575)

* fix finding 13

* Fix zellic finding 14 (#580)

* fix finding 3 (#575)

* fix finding 14

* Fix zellic finding 5 (#584)

* fix finding 3 (#575)

* fix finding 5

* refine comments

* fmt

* Fix finding 17 (#602)

* add q_last

* fix

* add more diff range check

* fix finding 7 (#625)

* tx_id = 1 when sm starts

* Fix finding 11 : use length for rlc in rlp table (#719)

* fix: use tag_bytes_rlc and tag_length to copy tag's bytes around

* fix lookup input for Len & RLC & GasCost fields in tx circuit

* refactor

* fix

* refactor

* fix col phase issue

* refactor bytes_rlc type

* Fix the bugs in Tx & PI circuits reported by Zellic & KALOS auditors (#612)

* lookup chain_id to RLP table

* fix finding 22 (#614)

* fix finding 21 (#613)

* fix finding 23 (#618)

* fix finding 26 (#622)

* fix finding 28 (#624)

Co-authored-by: Rohit Narurkar <rohit.narurkar@protonmail.com>

* fix finding 29 (#623)

Co-authored-by: Rohit Narurkar <rohit.narurkar@protonmail.com>

* enforce is_final is true at the last row and fix RLC related vul (#735)

* Fix finding 30  (#733)

* enforce all txs in a block are included in the tx table

* clippy

---------

Co-authored-by: Rohit Narurkar <rohit.narurkar@protonmail.com>

* Fix Zellic / Kalos finding25 (#619)

* fix finding 25

* add comment

---------

Co-authored-by: Rohit Narurkar <rohit.narurkar@protonmail.com>

* fix conflicts

---------

Co-authored-by: Rohit Narurkar <rohit.narurkar@protonmail.com>
Co-authored-by: Zhang Zhuo <mycinbrin@gmail.com>

* use q_first instead

* fmt

---------

Co-authored-by: Rohit Narurkar <rohit.narurkar@protonmail.com>
Co-authored-by: Zhang Zhuo <mycinbrin@gmail.com>

* add pi comments

* rename preimage col idx

* add keccak rows check

* rename input bytes col finder fn

* modify keccak row env constaint

* modify keccak row env constaint

* add named constant setup vars

* modify keccak row check

* clippy advised

* add comments on chunk hash

* fmt

* avoid constant lookup table

* avoid repetitive computation of input_bytes_col_idx

---------

Co-authored-by: Zhuo Zhang <mycinbrin@gmail.com>
Co-authored-by: xkx <xiakunxian130@gmail.com>
Co-authored-by: Rohit Narurkar <rohit.narurkar@protonmail.com>
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-eth-types Issues related to the eth-types workspace member crate-mock Issues related to the mock 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.

RETURN opcode

3 participants