Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EVM Circuit and trait OpGadget #26

Merged
merged 37 commits into from
Sep 17, 2021
Merged

EVM Circuit and trait OpGadget #26

merged 37 commits into from
Sep 17, 2021

Conversation

han0110
Copy link
Collaborator

@han0110 han0110 commented Aug 10, 2021

This PR aims to provides:

  1. A flexible design of evm circuit
  2. A trait for op gadget for us to implmenent without caring about the actual circuit layout
  3. An example AddGadget for reference (Closes [EVM] ADD example #9)

Todo:

  • Trait OpGadget
  • Check linear_combinations configured by OpGadget
  • Check lookups configured by OpGadget
    • Word 8bit lookups
    • Bus mapping lookups for stack and memory
  • AddGadget and PushGadget as examples.
  • Feed ExecutionStep by crate bus-mapping into circuit -> Next PR

Copy link
Collaborator

@ChihChengLiang ChihChengLiang left a comment

Choose a reason for hiding this comment

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

First round of review. Added a nitpick

zkevm-circuits/src/evm_circuit.rs Outdated Show resolved Hide resolved
}

#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
enum Case {
Copy link
Contributor

Choose a reason for hiding this comment

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

I think the name Case is a bit too general. Maybe use ErrorCode to be more specific?

zkevm-circuits/src/evm_circuit/op_execution/arithmetic.rs Outdated Show resolved Hide resolved
@han0110 han0110 force-pushed the evm_circuit branch 2 times, most recently from 35586b4 to 52a55d6 Compare September 1, 2021 08:13
@han0110 han0110 changed the title [WIP] EVM Circuit and trait for op gadget EVM Circuit and trait OpGadget Sep 1, 2021
Copy link
Member

@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.

Overall it looks good if what we plan is to have a Layout example.

But I have several questions:

  1. Seems that we still need to integrate bus-mapping in here. Do we plan do do it in a follow-up PR?
  2. The Opcode trait should (IMO) contain one or multiple ExecutionSteps so that we can basically have all of the CaseConfig data an all the other stuff extracted from there.
  3. Case and CaseConfig as well as CallContext should probably be migrated to bus-mappign and obtained during the parsing process.
  4. We should document more. Even if are not polished docs. But at least to help improve the general idea that the development path follows.
  5. CaseConfig as said in the comments should probably not be an associated const and instead be the result of a fn or similar. The reason is that multiple instances of the same Opode trait object can result in different CaseConfig.

zkevm-circuits/src/evm_circuit.rs Show resolved Hide resolved
zkevm-circuits/src/evm_circuit.rs Show resolved Hide resolved
zkevm-circuits/src/evm_circuit.rs Show resolved Hide resolved
zkevm-circuits/src/evm_circuit.rs Outdated Show resolved Hide resolved
zkevm-circuits/src/evm_circuit/op_execution.rs Outdated Show resolved Hide resolved
Comment on lines 35 to 84
const CASE_CONFIGS: &'static [CaseConfig] = &[
CaseConfig {
case: Case::Success,
num_word: 1,
num_cell: 32, // for PUSH selectors
will_resume: false,
},
CaseConfig {
case: Case::StackOverflow,
num_word: 0,
num_cell: 0,
will_resume: true,
},
CaseConfig {
case: Case::OutOfGas,
num_word: 0,
num_cell: 0,
will_resume: true,
},
];
Copy link
Member

Choose a reason for hiding this comment

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

This kinda reforces my previous argument that it doesn't seem to fit really well to have CaseConfigs as a const associated to the gadget since it can change depending on the instance.

@therealyingtong therealyingtong mentioned this pull request Sep 2, 2021
3 tasks
Copy link
Member

@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.

We will need to address all of the issues that have been raised here BTW. Not sure if before or after the first opcode impls. But the more we add the bigger the refactor will become.

Copy link
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.

The ideas used and the implementation both seem very nice to me. I was thinking about possible optimizations but nothing came to my mind that hasn't already been considered. I have one question though - would we need one additional constraint to make sure that execution_step.opcode and the opcode selector (talking about 80 cells) are in sync? Wouldn't be now possible to set execution_step.opcode to ADD and set the opcode selector to PUSH (the one of 80s that corresponds to PUSH) which would make some attacks possible?

@han0110
Copy link
Collaborator Author

han0110 commented Sep 14, 2021

I have one question though - would we need one additional constraint to make sure that execution_step.opcode and the opcode selector (talking about 80 cells) are in sync? Wouldn't be now possible to set execution_step.opcode to ADD and set the opcode selector to PUSH (the one of 80s that corresponds to PUSH) which would make some attacks possible?

@miha-stopar According to your question, currently in AddGadget and PushGadget we check if opcode == 1 or opcode == 3 and 0 <= opcode - PUSH1 <= 31 respectively to keep them in sync. If we set opcode to ADD, the constraint in PushGadget should not pass, right?

@miha-stopar
Copy link
Collaborator

@han0110 yes, that's ok. What I meant are these assignments: https://github.com/appliedzkp/zkevm-circuits/blob/f0bd8afe2607abfb428c5fb171730625448ddd25/zkevm-circuits/src/evm_circuit/op_execution.rs#L484. Not sure if there is a check that these assignments correspond to the actual opcode?

@miha-stopar
Copy link
Collaborator

@han0110 I can see it now - opcode number cell is used only as kind of a hint and the 80 cells determine the branching. Sorry, all good!

Copy link
Contributor

@gaswhat gaswhat left a comment

Choose a reason for hiding this comment

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

LGTM. Just a few minor comments

meta.advice_column(), // global_counter
meta.advice_column(), // target
meta.advice_column(), // is_write
meta.advice_column(), // val1
Copy link
Contributor

Choose a reason for hiding this comment

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

Add comment for actual meaning.

Suggested change
meta.advice_column(), // val1
meta.advice_column(), // val1, call_id

Copy link
Collaborator Author

@han0110 han0110 Sep 15, 2021

Choose a reason for hiding this comment

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

These val* mean different things depends on the target, so I left it arbitrary instead of concrete meaning. For stack or memroy it would be call_id but for storage it would be account address cause the storage update is not scoped in a single call but in a block. Will try to document it in a better way to clarify.

zkevm-circuits/src/evm_circuit.rs Show resolved Hide resolved
zkevm-circuits/src/evm_circuit.rs Show resolved Hide resolved
zkevm-circuits/src/evm_circuit.rs Show resolved Hide resolved
zkevm-circuits/src/evm_circuit/op_execution.rs Outdated Show resolved Hide resolved
zkevm-circuits/src/evm_circuit/op_execution.rs Outdated Show resolved Hide resolved
zkevm-circuits/src/evm_circuit/op_execution/arithmetic.rs Outdated Show resolved Hide resolved
Copy link
Collaborator

@therealyingtong therealyingtong left a comment

Choose a reason for hiding this comment

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

I've not looked through this whole PR and am just submitting a few nits -- please don't block on my review.

// Value in wei of call.
Value,
// If call succeeds or not in the future.
IsPersistant,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
IsPersistant,
IsPersistent,

Comment on lines 174 to 176
// relative position to selector for synthesis
column: Column<Advice>,
rotation: usize,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
// relative position to selector for synthesis
column: Column<Advice>,
rotation: usize,
column: Column<Advice>,
// relative position to selector for synthesis
rotation: usize,


// Number of cells used for each purpose
// TODO: pub const NUM_CELL_CALL_INITIALIZATION_STATE: usize = ;
pub const NUM_CELL_OP_EXECTION_STATE: usize = 7;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
pub const NUM_CELL_OP_EXECTION_STATE: usize = 7;
pub const NUM_CELL_OP_EXECUTION_STATE: usize = 7;

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Fixed with all above suggestions in 702039d.


construct_op_gadget!(add_gadget);
construct_op_gadget!(push_gadget);
let _ = qs_op_idx;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
let _ = qs_op_idx;

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This is intended for escaping the clippy warning since the macro increases qs_op_idx even it's the last one.

Copy link
Collaborator

@ChihChengLiang ChihChengLiang left a comment

Choose a reason for hiding this comment

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

LGTM. Added some nitpicks

zkevm-circuits/src/evm_circuit.rs Outdated Show resolved Hide resolved
zkevm-circuits/src/evm_circuit/op_execution/push.rs Outdated Show resolved Hide resolved
@han0110 han0110 merged commit 1e26882 into main Sep 17, 2021
@han0110 han0110 deleted the evm_circuit branch September 17, 2021 06:27
@ChihChengLiang ChihChengLiang added crate-zkevm-circuits Issues related to the zkevm-circuits workspace member T-opcode Type: opcode-related and focused PR/Issue labels Oct 6, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
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.

[EVM] ADD example
8 participants