Skip to content

Commit

Permalink
EVM Circuit and trait OpGadget (privacy-scaling-explorations#26)
Browse files Browse the repository at this point in the history
* feat: draft on evm circuit and trait for op gadget

* feat: draft more on evm circuit with minimal working example

* fix: improve comment about EIP3541 (EOF)

Co-authored-by: Chih Cheng Liang <[email protected]>

* feat: add fixed table and Range256 lookup support

* feat: refactor with param.rs to place layout parameters

* feat: implement qs_byte_lookup to reduce byte lookup

* fix: avoid non-independent lookup by requiring selector to be only a queried cell

* fix: rename exp to expr to avoid ambiguity and make vars shorter

* fix: update rotation of cells_next

* feat: add stack and memory BusMappingLookup support

* Implement PushGadget responsible for opcode PUSH* (privacy-scaling-explorations#45)

* PUSH first version

* Push gadget constraint added

* fix: fix PushGadget op execution transition constraint

* feat: add BusMappingLookup in PUSH and handle negative offset in EvmCircuit

* feat: add some PUSH before AddGadget's test to make it reasonable

Co-authored-by: han0110 <[email protected]>

* chore: rename will_resume to will_halt to be consistent to geth

* feat: replace u8 with OpcodeId and fix PushGadget's stack_overflow constraint

* chore: simplify macro construct_op_gadget and add comment

* fix: fix Range32 table to correct range 0..32

* fix: fix bus mapping inconsistency and fix SUB's minuend to second popped item

* feat: refactor with trait Expr and add struct GasCost for convenient usage

* lt opcode updated

* fix rebase

* update lt && gt opcode

* accept result 0 for lt && gt opcode ,add more tests

* fixed some explanation

* fix

* update test

* fix lt gadget

* update add/sub test case

* eol

* lint

* fix clippy

* fix: add AddGadget's carry range check (should be 0 or 1)

* fix: add constraint on qs_byte_lookups for cases to enable byte lookup

* fix clippy and address comments

* fix more comments

* fix: fix typo and comment

* fix: remove witness selector boolean constraint cause OpGadget enforces it

* chore: remove commented implementation on PushGadget and move to issue privacy-scaling-explorations#73

Co-authored-by: Chih Cheng Liang <[email protected]>
Co-authored-by: Miha Stopar <[email protected]>
Co-authored-by: ghostfly23333 <[email protected]>
Co-authored-by: gaswhat <[email protected]>
  • Loading branch information
5 people authored Sep 17, 2021
1 parent 20c7dd2 commit 1e26882
Show file tree
Hide file tree
Showing 15 changed files with 3,369 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ members = [
"zkevm-circuits",
"bus-mapping",
"keccak256"
]
]

[patch.crates-io]
halo2 = { git = "https://github.com/zcash/halo2.git", rev = "4283713ec76051eaf21a06d0279fa7d3497cafb6" }
2 changes: 2 additions & 0 deletions bus-mapping/src/evm.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! Evm types needed for parsing instruction sets as well
pub(crate) mod gas;
pub(crate) mod instruction;
pub(crate) mod opcodes;

Expand All @@ -9,6 +10,7 @@ use lazy_static::lazy_static;
use num::{BigUint, Num, Zero};
use serde::{Deserialize, Serialize};
pub use {
gas::GasCost,
instruction::Instruction,
opcodes::{ids::OpcodeId, Opcode},
};
Expand Down
31 changes: 31 additions & 0 deletions bus-mapping/src/evm/gas.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/// Gas cost enum.
pub struct GasCost(pub u8);

impl GasCost {
/// Constant cost for quick step
pub const QUICK: Self = Self(2);
/// Constant cost for fastest step
pub const FASTEST: Self = Self(3);
/// Constant cost for fast step
pub const FAST: Self = Self(5);
/// Constant cost for mid step
pub const MID: Self = Self(8);
/// Constant cost for slow step
pub const SLOW: Self = Self(10);
/// Constant cost for ext step
pub const EXT: Self = Self(20);
}

impl GasCost {
/// Returns the `GasCost` as a `u8`.
#[inline]
pub const fn as_u8(&self) -> u8 {
self.0
}

/// Returns the `GasCost` as a `usize`.
#[inline]
pub const fn as_usize(&self) -> usize {
self.0 as usize
}
}
2 changes: 1 addition & 1 deletion bus-mapping/src/evm/opcodes/ids.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use serde::{Deserialize, Serialize};
use std::str::FromStr;

/// Opcode enum. One-to-one corresponding to an `u8` value.
#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash, Serialize, Deserialize)]
pub struct OpcodeId(pub u8);

// Core opcodes.
Expand Down
1 change: 1 addition & 0 deletions bus-mapping/src/exec_trace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,7 @@ impl From<(Target, usize)> for OperationRef {
Target::Memory => Self(Target::Memory, op_ref_data.1),
Target::Stack => Self(Target::Stack, op_ref_data.1),
Target::Storage => Self(Target::Storage, op_ref_data.1),
_ => unreachable!(),
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions bus-mapping/src/operation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ impl RW {
/// Enum used to differenciate between EVM Stack, Memory and Storage operations.
#[derive(Debug, Clone, PartialEq, Eq, Copy)]
pub enum Target {
/// Dummy target to pad meaningful targets
Noop,
/// Means the target of the operation is the Memory.
Memory,
/// Means the target of the operation is the Stack.
Expand Down
2 changes: 2 additions & 0 deletions zkevm-circuits/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,7 @@ ff = "0.10"
halo2 = "0.0"
pasta_curves = "0.1"
bigint = "4"
num = "0.4"
sha3 = "0.7.2"
digest = "0.7.6"
bus-mapping = { path = "../bus-mapping" }
Loading

0 comments on commit 1e26882

Please sign in to comment.