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

Circuit for opcode BALANCE#683

Merged
lispc merged 12 commits into
privacy-ethereum:mainfrom
scroll-tech:feat/opcode-balance
Dec 21, 2022
Merged

Circuit for opcode BALANCE#683
lispc merged 12 commits into
privacy-ethereum:mainfrom
scroll-tech:feat/opcode-balance

Conversation

@silathdiir
Copy link
Copy Markdown
Contributor

@silathdiir silathdiir commented Aug 16, 2022

Spec: privacy-ethereum/zkevm-specs#248

Summary

  1. Update BALANCE bus-mapping and add circuit.
  2. Fix to lookup Balance if account exists, otherwise lookup NonExisting.
  3. Add a HashSet to save non-existent account addresses in both bus-mapping and circuit ExeSteps.

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.

Blocked by PR #907

@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 Aug 16, 2022
@silathdiir silathdiir marked this pull request as draft August 16, 2022 07:21
@CPerezz CPerezz self-requested a review August 16, 2022 11:40
@CPerezz
Copy link
Copy Markdown
Contributor

CPerezz commented Aug 19, 2022

Blocked by privacy-ethereum/zkevm-specs#248 which is at the same time blocked by privacy-ethereum/zkevm-specs#249

@silathdiir
Copy link
Copy Markdown
Contributor Author

Will try to update this PR since #907 has been merged.

@silathdiir silathdiir marked this pull request as ready for review December 12, 2022 23:00
@silathdiir
Copy link
Copy Markdown
Contributor Author

Hi @CPerezz , I updated this BALANCE circuit with non-existing proofs. Could you help review again when you have time? 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.

Just wondering the balance passed in the assignation of the Opcode circuit impl.

For the rest this looks good. If you cold provide feedback, I'll approve immediately!

Comment thread bus-mapping/src/circuit_input_builder/execution.rs Outdated
Comment on lines +126 to +129
let balance = if exists {
block.rws[step.rw_indices[5]]
.table_assignment_aux(block.randomness)
.value
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'm unsure why do we need block.randomness to set the balance. Is it passed directly as an RLC??

Copy link
Copy Markdown
Contributor Author

@silathdiir silathdiir Dec 17, 2022

Choose a reason for hiding this comment

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

Hi @CPerezz , I checked function table_assignment_aux. But it seems that only value is needed here, so I replaced it with value_assignment(block.randomness).

And reference function value_assignment, it seems that the argument randomness is used in function random_linear_combine as below:

AccountFieldTag::CodeHash | AccountFieldTag::Balance => {
    RandomLinearCombination::random_linear_combine(value.to_le_bytes(), randomness)
}

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.

Hi @CPerezz , could you help review this again when you have time? Thanks.

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.

Ye sorry @silathdiir I missed this!!!

@silathdiir silathdiir requested a review from CPerezz December 17, 2022 02:59
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! I'd want another review from someone at Scroll before merging this! @icemelon or @silathdiir could you ping someone? 😄

@silathdiir
Copy link
Copy Markdown
Contributor Author

LGTM! I'd want another review from someone at Scroll before merging this! @icemelon or @silathdiir could you ping someone? 😄

Yes. I will ping others to help review. Thanks a lot.

+ (1.expr() - is_warm.expr()) * GasCost::COLD_ACCOUNT_ACCESS.expr();

let step_state_transition = StepStateTransition {
rw_counter: Delta(cb.rw_counter_offset()),
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

rw_counter: Delta(cb.rw_counter_offset()), not good style since we don't know exactly rw counter change, suggest to use normal way rw_counter: 7.expr()

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.

Replaced cb.rw_counter_offset() with 7.expr().

/// Error generated by this step
pub error: Option<ExecError>,
/// Non existent account addresses
pub non_existent_accounts: HashSet<Address>,
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

most probably do not need this, for circuit side, existing can be check by reading buss mapping record, for AccountFieldTag::NonExisting, no need additional step info.

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.

Yes exactly 🙏, I fixed to get both balance and exists in circuit function assign_exec_step as below:

let (balance, exists) = match block.rws[step.rw_indices[5]] {
    Rw::Account {
        field_tag: AccountFieldTag::Balance,
        value,
        ..
    } => (
        RandomLinearCombination::random_linear_combine(
            value.to_le_bytes(),
            block.randomness,
        ),
        true,
    ),
    Rw::Account {
        field_tag: AccountFieldTag::NonExisting,
        ..
    } => (F::zero(), false),
    _ => unreachable!(),
};

Comment thread zkevm-circuits/src/witness/step.rs Outdated
/// The opcode corresponds to the step
pub opcode: Option<OpcodeId>,
/// Non existent account addresses
pub non_existent_accounts: HashSet<Address>,
Copy link
Copy Markdown
Collaborator

@DreamWuGit DreamWuGit Dec 20, 2022

Choose a reason for hiding this comment

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

same as above comment, suggest to remove

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.

Deleted.


let address = block.rws[step.rw_indices[0]].stack_value().to_address();
let mut address_bytes = address.0;
address_bytes.reverse();
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

want to know what reason for reverse it

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.

Try to investigate ethers_core::types::Address and code in eth-types, it seems that the Address is big endian.

https://github.com/privacy-scaling-explorations/zkevm-circuits/blob/main/eth-types/src/lib.rs#L167


let exists = cb.query_bool();
let balance = cb.condition(exists.expr(), |cb| {
let balance = cb.query_cell();
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

can the "query_cell" be moved out of the closure? It is some "structure/allocation" info, better not be guarded with "if" branch.

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.

Moved out query_cell as:

let balance = cb.query_cell();
let exists = cb.query_bool();
cb.condition(exists.expr(), |cb| {
    cb.account_read(
        from_bytes::expr(&address.cells),
        AccountFieldTag::Balance,
        balance.expr(),
    );
});

cb.stack_push(select::expr(exists.expr(), balance.expr(), 0.expr()));

let gas_cost = is_warm.expr() * GasCost::WARM_ACCESS.expr()
+ (1.expr() - is_warm.expr()) * GasCost::COLD_ACCOUNT_ACCESS.expr();
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

use select::expr?

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 to:

let gas_cost = select::expr(
    is_warm.expr(),
    GasCost::WARM_ACCESS.expr(),
    GasCost::COLD_ACCOUNT_ACCESS.expr(),
);

@lispc
Copy link
Copy Markdown
Collaborator

lispc commented Dec 21, 2022

@silathdiir
Copy link
Copy Markdown
Contributor Author

need to modify

https://github.com/privacy-scaling-explorations/zkevm-circuits/blob/3da1888cfdea1f05062477279d9c5088e01fac19/zkevm-circuits/src/witness/step.rs#L184

Sorry for the wrong revert. Fixed to OpcodeId::BALANCE => ExecutionState::BALANCE and moved to next one of ADDRESS.

@silathdiir silathdiir requested review from lispc and removed request for DreamWuGit December 21, 2022 06:07
@lispc lispc merged commit 0b8f7de into privacy-ethereum:main Dec 21, 2022
@lispc lispc deleted the feat/opcode-balance branch December 21, 2022 06:16
jonathanpwang pushed a commit to axiom-crypto/zkevm-circuits that referenced this pull request Aug 1, 2023
* implement RLC chip

* keccak table interfaces

* minor

* mock chunk tests

* aggregation tests

* fixes

* supporing empty hash preimage

* minor

* remove tmp data

* clean up and address comments

* update docs

* partial fix clippy

* fix chunk construction bug

* fix compiling bug

* fix padded chunk's data hash

* fix bug in number of valid chunks

* handle the case of all valid ones

* add fixed columns to rlc config

* enforce the chunks are orderred

* fix bugs in challenges

* add missing selectors for aggregation circuit

* fix the fixed cells (privacy-ethereum#677)

* try to reenable skip first pass

* tr to fix skip first pass again

* try to fix again

* clean up

* fixed skip first pass

* aggregation parameter with k = 19

* removing unused features; partial address comments

* fix clippy

* fix clippy

* address comment on fixed cells

* fix dynamic hash test

* try to fix new fixed cells

* reverting last 3 commits

* Add conversion from witness `Block` to `ChunkHash`. (privacy-ethereum#683)

---------

Co-authored-by: Steven <asongala@163.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-zkevm-circuits Issues related to the zkevm-circuits workspace member

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants