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

Handle contract creation tx#1140

Merged
CPerezz merged 26 commits into
privacy-ethereum:mainfrom
scroll-tech:feat/begintx-contract-deploy
Feb 23, 2023
Merged

Handle contract creation tx#1140
CPerezz merged 26 commits into
privacy-ethereum:mainfrom
scroll-tech:feat/begintx-contract-deploy

Conversation

@roynalnaruto
Copy link
Copy Markdown
Collaborator

This PR handles "case 1" in the BeginTx gadget, i.e. a contract deployment transaction.

Major changes:

  • Bus Mapping:
    • Add RLP([tx_caller, tx_nonce]) to the keccak table
    • Add a reversible account op for the deployed contract's nonce
  • BeginTx gadget:
    • Conditional lookups to the keccak table (depending on byte_size(tx.nonce))
    • Appropriate step transition for contract creation case
    • More tests
  • Others:
    • Add a keccak_rlc method in constraint builder that computes a random linear combination using challenge.keccak_input
    • Add a BinaryNumber gadget to evm_circuit::util::math_gadget

@roynalnaruto roynalnaruto requested a review from ed255 February 6, 2023 16:12
@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 Feb 6, 2023
@roynalnaruto roynalnaruto requested review from CPerezz and lispc February 6, 2023 16:12
@CPerezz
Copy link
Copy Markdown
Contributor

CPerezz commented Feb 7, 2023

Starting the review tomorrow as my first task. Will try to do it ASAP!

Copy link
Copy Markdown
Contributor

@CPerezz CPerezz left a comment

Choose a reason for hiding this comment

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

Nice work done here! It's a quite big PR.
Have done a 1st pass but will need to do a 2nd definitely and check deeper.

Comment thread bus-mapping/src/evm/opcodes.rs
Comment thread bus-mapping/src/evm/opcodes.rs
Comment thread bus-mapping/src/evm/opcodes.rs
Comment thread zkevm-circuits/src/evm_circuit/execution/begin_tx.rs Outdated
Comment thread zkevm-circuits/src/evm_circuit/execution/begin_tx.rs
Comment on lines +327 to +339
keccak_lookup!(0, tx_nonce_lt_128.expr());
keccak_lookup!(
1,
not::expr(tx_nonce_lt_128.expr()),
tx_nonce_byte_size_cmp.value_equals(1usize)
);
keccak_lookup!(2, tx_nonce_byte_size_cmp.value_equals(2usize));
keccak_lookup!(3, tx_nonce_byte_size_cmp.value_equals(3usize));
keccak_lookup!(4, tx_nonce_byte_size_cmp.value_equals(4usize));
keccak_lookup!(5, tx_nonce_byte_size_cmp.value_equals(5usize));
keccak_lookup!(6, tx_nonce_byte_size_cmp.value_equals(6usize));
keccak_lookup!(7, tx_nonce_byte_size_cmp.value_equals(7usize));
keccak_lookup!(8, tx_nonce_byte_size_cmp.value_equals(8usize));
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

There has to be a better way to do this I think. It's fine to land this now but we should file a tech-debt issue.

Copy link
Copy Markdown
Collaborator Author

@roynalnaruto roynalnaruto Feb 11, 2023

Choose a reason for hiding this comment

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

With the RLP gadget, I believe it will be possible to remove this. Before merging this PR, I'll make it a point to create that issue and define the scope. Again, as I said above, I'm happy to implement the RLP gadget for contract creation address (CREATE/CREATE2)

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

@CPerezz I've ported over @z2trillion 's work with tests, which includes the gadget to calculate CREATE address is here:
https://github.com/scroll-tech/zkevm-circuits/blob/feat/contract-create-rlp/zkevm-circuits/src/evm_circuit/util/math_gadget/rlp.rs#L182

I'd suggest I can do the refactoring as part of a separate PR (to make the reviewing process more convenient). But if you think it's better to be wrapped up in this very PR, happy to make the changes :)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Sure if we file a tech-debt PR before merging with all of the things like this.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

This part of multiple condition guarded keccak lookups has been replaced by a single keccak lookup, where the input rlc and input length is calculated from the ContractCreate gadget that we merged earlier.

Comment thread zkevm-circuits/src/evm_circuit/execution/begin_tx.rs Outdated
Comment thread zkevm-circuits/src/evm_circuit/execution/begin_tx.rs
Comment thread zkevm-circuits/src/evm_circuit/execution/begin_tx.rs
tx_call_data_length.expr(),
),
(CallContextFieldTag::Value, tx_value.expr()),
(CallContextFieldTag::IsStatic, 0.expr()),
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

How do we know that IsStatic = false? We aren't in a create Tx but we should still be able to be in a non-static call.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Nevermind. The root call always needs to be non-static right? @ed255 do you happen to know?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Yes, I forgot to respond to this. My understanding was that a contract creation transaction (is_create * is_root == true) MUST be non-static.

@roynalnaruto roynalnaruto requested a review from CPerezz February 11, 2023 06:01
@roynalnaruto
Copy link
Copy Markdown
Collaborator Author

@CPerezz I have added this PR: #1164

We can review/merge the above, then I can update this PR to refactor it and use the ContractCreateGadget (and remove the macros and unnecessary extra conditional guards). Sounds good?

@CPerezz
Copy link
Copy Markdown
Contributor

CPerezz commented Feb 13, 2023

@roynalnaruto perfect! I'll take a look to the other one after 2 other pending reviews I have! :)

@roynalnaruto
Copy link
Copy Markdown
Collaborator Author

@CPerezz This PR is now ready for review after refactoring on #1164 . The keccak lookup looks much neater now.

lispc pushed a commit that referenced this pull request Feb 21, 2023
### Description

The `CREATE2` input length and `RLC(input)` were not tested during the
#1164 merge. This PR adds and enables a test for CREATE2 specific
`input_len` and `input_rlc` calculation.

### Type of change

- [ ] Bug fix (non-breaking change which fixes an issue)
- [x] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing
functionality to not work as expected)
- [ ] This change requires a documentation update

### How Has This Been Tested?

The helper container can test both `CREATE` and `CREATE2` inputs (length
and `RLC(input_bytes)`). These inputs are then used as inputs to do a
lookup to the Keccak table.

### Related PRs
- #1164 
- #1140 (needs rebasing after this is merged)

---------

Co-authored-by: adria0.eth <5526331+adria0@users.noreply.github.com>
@CPerezz
Copy link
Copy Markdown
Contributor

CPerezz commented Feb 21, 2023

Is this ready for review now that the other related PRs have been merged @roynalnaruto ?

@roynalnaruto
Copy link
Copy Markdown
Collaborator Author

@CPerezz Yes it's ready for review. I've also answered your questions and resolved most of the review comments!

Copy link
Copy Markdown
Contributor

@CPerezz CPerezz left a comment

Choose a reason for hiding this comment

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

Just one change required.
Aside from a couple nits, the rest looks good!

Nice work @roynalnaruto

Comment thread zkevm-circuits/src/evm_circuit/util/math_gadget.rs Outdated
Comment thread zkevm-circuits/src/evm_circuit/util/math_gadget/binary_number.rs Outdated
tx_call_data_length.expr(),
),
(CallContextFieldTag::Value, tx_value.expr()),
(CallContextFieldTag::IsStatic, 0.expr()),
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Nevermind. The root call always needs to be non-static right? @ed255 do you happen to know?

@roynalnaruto roynalnaruto requested a review from CPerezz February 23, 2023 10:03
Copy link
Copy Markdown
Contributor

@CPerezz CPerezz left a comment

Choose a reason for hiding this comment

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

LGTM!!!

@CPerezz CPerezz added this pull request to the merge queue Feb 23, 2023
Merged via the queue into privacy-ethereum:main with commit 646cd32 Feb 23, 2023
@roynalnaruto roynalnaruto deleted the feat/begintx-contract-deploy branch February 23, 2023 18:36
lispc pushed a commit that referenced this pull request Mar 30, 2024
* fix: Stale code comment

* fix: Inconsistent structure constructor
enables clippy::inconsistent_struct_constructor lint

* fix: Incorrect documentation on the MinMaxGadget

* fix: cargo audit

* fix clippy
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

Status: ✅ Done

Development

Successfully merging this pull request may close these issues.

3 participants