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

Spec for opcode BALANCE#248

Merged
ed255 merged 16 commits into
privacy-ethereum:masterfrom
scroll-tech:feat/opcode-balance
Nov 3, 2022
Merged

Spec for opcode BALANCE#248
ed255 merged 16 commits into
privacy-ethereum:masterfrom
scroll-tech:feat/opcode-balance

Conversation

@silathdiir
Copy link
Copy Markdown
Contributor

@silathdiir silathdiir commented Aug 16, 2022

Circuit: privacy-ethereum/zkevm-circuits#683

Not confirm if necessary to constrain "If the given account doesn't exist, then it will push 0 onto the stack instead." in the circuit.
And is there a method (could be used in circuit) to lookup if account is existing? Thanks.

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.

The implementation looks good.

Some questions regarding the markdown specs.

Comment thread specs/opcode/31BALANCE.md Outdated
Comment thread specs/opcode/31BALANCE.md Outdated
@CPerezz CPerezz requested a review from ed255 August 17, 2022 08:12
Co-authored-by: Carlos Pérez <37264926+CPerezz@users.noreply.github.com>
@CPerezz
Copy link
Copy Markdown
Contributor

CPerezz commented Aug 17, 2022

To be honest I'm not sure about:

necessary to constrain "If the given account doesn't exist, then it will push 0 onto the stack instead." in the circuit.
And is there a method (could be used in circuit) to lookup if account is existing?

This got me thinking. I'd say yes, we definitely need to do this check right? But how? How can we assert that an account exists? This would require a lookup and I think we don't have any mechanism in our tables (see https://github.com/privacy-scaling-explorations/zkevm-specs/blob/master/specs/tables.md) that allows us to gather this info.

We would need to have a table with all the accounts for which we need to check it's existance. Or at the very least, to add a tag for this thing in the rw table for example.
cc: @han0110 @ed255

@ed255
Copy link
Copy Markdown
Contributor

ed255 commented Aug 17, 2022

To be honest I'm not sure about:

necessary to constrain "If the given account doesn't exist, then it will push 0 onto the stack instead." in the circuit.
And is there a method (could be used in circuit) to lookup if account is existing?

This got me thinking. I'd say yes, we definitely need to do this check right? But how? How can we assert that an account exists? This would require a lookup and I think we don't have any mechanism in our tables (see https://github.com/privacy-scaling-explorations/zkevm-specs/blob/master/specs/tables.md) that allows us to gather this info.

We would need to have a table with all the accounts for which we need to check it's existance. Or at the very least, to add a tag for this thing in the rw table for example. cc: @han0110 @ed255

Here's a summary of a discussion we had about this via chat:

There are 2 options for the MPT table API.
A. One is that you just do a balance lookup, and the MPT circuit constraints the value to be 0 when the account doesn't exist (using an MPT proof of non-existence).
B. The other option is to have a tag for the MPT table for account exists, which would mean that the BALANCE opcode needs to do 2 lookups:

  1. Account exists
  2. Account balance
    I think option B is simpler for the MPT circuit, but I'm not sure if it already supports option A.

Some circuits require proving that an account doesn't exist. With option B that is done with a single lookup. With option A that would be done with the check nonce == 0 && balance == 0 && code_hash == empty_hash.

I'm more favorable towards option B.

For this PR I would say to go with one of the options (probably option A is simpler), and then we can revisit all lookups to non-existing accounts if necessary once we settle on which option we take.

@CPerezz
Copy link
Copy Markdown
Contributor

CPerezz commented Aug 18, 2022

Pending on #249 update

@silathdiir
Copy link
Copy Markdown
Contributor Author

Hi @CPerezz and @ed255, I updated the code to use option A to check account existence (via nonce == 0 && balance == 0 && code_hash == empty_hash), and add a TODO for updating later when the issue #249 is fixed.

Please help review again when you have time. Thanks.

@silathdiir silathdiir requested review from CPerezz and removed request for ed255 August 19, 2022 02:04
@CPerezz
Copy link
Copy Markdown
Contributor

CPerezz commented Aug 19, 2022

Hey @silathdiir I think that #249 is close enough to be merged that this will not be necessary. And we can wait, rebase this on the top of the new main and then resolve the TODO in this PR.

Note that this means that we'll need to go for option 2.
Hope you agree with that! :)

@silathdiir
Copy link
Copy Markdown
Contributor Author

silathdiir commented Aug 19, 2022

Hey @silathdiir I think that #249 is close enough to be merged that this will not be necessary. And we can wait, rebase this on the top of the new main and then resolve the TODO in this PR.

Note that this means that we'll need to go for option 2. Hope you agree with that! :)

Got it. Will try to update after #249 is closed. Thanks.

@ed255
Copy link
Copy Markdown
Contributor

ed255 commented Oct 3, 2022

#291 should unlock this PR

@ed255
Copy link
Copy Markdown
Contributor

ed255 commented Oct 5, 2022

@silathdiir #291 has just been merged, so this PR is now unlocked to be completed :)

@silathdiir
Copy link
Copy Markdown
Contributor Author

@silathdiir #291 has just been merged, so this PR is now unlocked to be completed :)

Thanks a lot. I updated this PR according to #291 (will update similar code of extcodehash in another PR).

@CPerezz @ed255 please help review again when you have time. Thanks.

Comment thread tests/evm/test_balance.py
address,
True,
is_warm,
rw_counter_of_reversion=rw_counter_end_of_reversion - reversible_write_counter,
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.

Is reversible_write_counter always zero?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I tried to review reversible-write-reversion doc. It seems that reversible_write_counter should be initialized to 0.

And I referenced the code of test_extcodecopy.py and test_extcodehash.py.

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.

I readed the document for first time, and, ups, it's tricky!

BTW I changed with random values this lines in SLOAD and test are passing

https://github.com/privacy-scaling-explorations/zkevm-specs/blob/947e012c6d8d1566b295f8bfd717a82eeb475e18/tests/evm/test_sload.py#L56-L57

so are we missing something here? (cc/ @ed255 @ChihChengLiang )

unsure about this revertion parameters, but for the rest, LGTM

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.

I think in this test the rw_counter_end_of_reversion and reversible_write_counter are not super relevant because we're only checking the constraints of the BALANCE step with the STOP/REVERT step as next. But we're not checking the constraints of the STOP/REVERT step.
The constraints related to rw_counter_end_of_reversion and reversible_write_counter are checked during the REVERT step, so even if we have wrong values here the test would pass.

As @silathdiir reversible_write_counter should start at 0 when a call begins. I think rw_counter_end_of_reversion should be 9 + the count of reversible operations that happened (which I think should be 1 for the account access list write).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed rw_counter_end_of_reversion to 10 with comment:

# 9 + 1 reversible operation (the account access list write)
rw_counter_end_of_reversion = 0 if is_persistent else 10

@silathdiir silathdiir removed the request for review from CPerezz October 14, 2022 01:56
@silathdiir silathdiir requested review from CPerezz and adria0 and removed request for CPerezz and adria0 October 14, 2022 01:56
Comment thread tests/evm/test_balance.py
address,
True,
is_warm,
rw_counter_of_reversion=rw_counter_end_of_reversion - reversible_write_counter,
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.

I readed the document for first time, and, ups, it's tricky!

BTW I changed with random values this lines in SLOAD and test are passing

https://github.com/privacy-scaling-explorations/zkevm-specs/blob/947e012c6d8d1566b295f8bfd717a82eeb475e18/tests/evm/test_sload.py#L56-L57

so are we missing something here? (cc/ @ed255 @ChihChengLiang )

unsure about this revertion parameters, but for the rest, LGTM

@ed255 ed255 assigned ed255 and unassigned ed255 Oct 28, 2022
@ed255 ed255 self-requested a review October 28, 2022 06:54
Comment thread src/zkevm_specs/evm/execution/balance.py Outdated
Comment thread tests/evm/test_balance.py
address,
True,
is_warm,
rw_counter_of_reversion=rw_counter_end_of_reversion - reversible_write_counter,
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.

I think in this test the rw_counter_end_of_reversion and reversible_write_counter are not super relevant because we're only checking the constraints of the BALANCE step with the STOP/REVERT step as next. But we're not checking the constraints of the STOP/REVERT step.
The constraints related to rw_counter_end_of_reversion and reversible_write_counter are checked during the REVERT step, so even if we have wrong values here the test would pass.

As @silathdiir reversible_write_counter should start at 0 when a call begins. I think rw_counter_end_of_reversion should be 9 + the count of reversible operations that happened (which I think should be 1 for the account access list write).

@silathdiir silathdiir requested review from CPerezz and ed255 and removed request for CPerezz and ed255 October 30, 2022 14:38
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.

I noticed that in a few places the rwc counter increase is outdated (check my suggested changes).
Other than that, everything looks good!

Comment thread tests/evm/test_balance.py Outdated
Comment thread src/zkevm_specs/evm/execution/balance.py Outdated
Comment thread specs/opcode/31BALANCE.md Outdated
silathdiir and others added 3 commits November 2, 2022 22:43
Co-authored-by: Eduard S. <eduardsanou@posteo.net>
Co-authored-by: Eduard S. <eduardsanou@posteo.net>
Co-authored-by: Eduard S. <eduardsanou@posteo.net>
@silathdiir
Copy link
Copy Markdown
Contributor Author

I noticed that in a few places the rwc counter increase is outdated (check my suggested changes).
Other than that, everything looks good!

I updated code according to the suggestions. Thanks a lot.

@silathdiir silathdiir requested a review from ed255 November 2, 2022 14:50
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! Great work!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants