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

refactor state circuit #339

Merged
ed255 merged 22 commits into
privacy-ethereum:mainfrom
scroll-tech:refactor/state_circuit
Feb 22, 2022
Merged

refactor state circuit #339
ed255 merged 22 commits into
privacy-ethereum:mainfrom
scroll-tech:refactor/state_circuit

Conversation

@lispc
Copy link
Copy Markdown
Collaborator

@lispc lispc commented Feb 21, 2022

This pr contains some useful modification of state circuit, as the first step to implement the new state spec privacy-ethereum/zkevm-specs#118

  1. add RwTable and RwRow
  2. build state circuit witness from witness::RwMap.
  3. remove the dummy "write 0" row in memory circuit
  4. rename global_counter_xxx to rw_counter_xxx
  5. update many selector implementations in state circuit, change from a manually written (x-1) * (x-3) * (x-4) / 12 to generate_lagrange_base_polynomial(x, 2, 1..=4)

@github-actions github-actions Bot added T-opcode Type: opcode-related and focused PR/Issue crate-zkevm-circuits Issues related to the zkevm-circuits workspace member labels Feb 21, 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.

Overall looks good to me! I've just added a few notes for discussion, but regardless of the outcome I think they should be non-blocking because there are still many changes to be made in the state circuit to bring it up to date with the latest spec :)

The only comment which should really be resolved is about the generate_lagrange_base_polynomial which I think has a mistake.

Comment thread zkevm-circuits/src/evm_circuit/util/math_gadget.rs
}

impl RwMap {
/// These "sorted_xx" methods are used in state circuit
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.

Currently we have sorting methods in the bus-mapping (in OperationContainer). I think we should have storing methods in one place. So either we
a. take the bus-mapping Operations, sort them and convert them to RwMap
b. take the bus-mapping Operations, convert them to RwMap, sort them

Since we only should sort once, I think having the sorting implementation in two places is redundant. I don't know which is the best place to do the sorting though.


#[derive(Debug, Default)]
#[derive(Debug, Default, Clone)]
pub struct RwMap(pub HashMap<RwTableTag, Vec<Rw>>);
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.

NOTE: This could be a fixed length array instead of a HashMap (considering that in most cases we're using all the tags (minus value 0 and 1, and we have just a few of them).

Comment on lines +504 to +515
pub struct RwRow<F: FieldExt> {
pub rw_counter: F,
pub is_write: F,
pub tag: F,
pub key2: F,
pub key3: F,
pub key4: F,
pub value: F,
pub value_prev: F,
pub aux1: F,
pub aux2: F,
}
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.

Suggestion:

pub struct RwRow<F: FieldExt> {
    pub rw_counter: F,
    pub is_write: F,
    pub keys: [F; 4]
    pub value: F,
    pub value_prev: F,
    pub auxs: [F; 2],
}

I think making groups makes it more readable, but this is just a suggestion!


/// The rw table shared between evm circuit and state circuit
#[derive(Clone, Copy)]
pub struct RwTable {
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.

The same suggestion applies here to merge fields (all keys, and all auxs)

Comment thread zkevm-circuits/src/rw_table.rs Outdated
Comment thread zkevm-circuits/src/state_circuit/state.rs
Copy link
Copy Markdown
Collaborator

@miha-stopar miha-stopar left a comment

Choose a reason for hiding this comment

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

Looks cool to me, just a minor thing about generate_lagrange_base_polynomial that Edu mentioned is to be fixed.

@lispc
Copy link
Copy Markdown
Collaborator Author

lispc commented Feb 22, 2022

I made two small changes (1) update generate_lagrange_base_polynomial (2) fix a comment typo

Since a lot of updates needed for the new spec further, I think this PR can be merged first. Thank you!

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! Thanks for the contribution :)

@ed255
Copy link
Copy Markdown
Contributor

ed255 commented Feb 22, 2022

I made two small changes (1) update generate_lagrange_base_polynomial (2) fix a comment typo

Thanks

Since a lot of updates needed for the new spec further, I think this PR can be merged first. Thank you!

I agree :) Let's wait for the tests to pass and we can merge this PR

@ed255 ed255 merged commit 3d3820b into privacy-ethereum:main Feb 22, 2022
@lispc lispc deleted the refactor/state_circuit branch February 23, 2022 02:30
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

crate-zkevm-circuits Issues related to the zkevm-circuits workspace member T-opcode Type: opcode-related and focused PR/Issue

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants