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

Commit

Permalink
removed douplicate KeccakTable classes
Browse files Browse the repository at this point in the history
  • Loading branch information
Mohammad Jahanara authored and Mohammad Jahanara committed Feb 18, 2024
1 parent b871e6b commit cc06b41
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 83 deletions.
27 changes: 1 addition & 26 deletions src/zkevm_specs/pi_circuit.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from .util import PUBLIC_INPUTS_BLOCK_LEN as BLOCK_LEN
from .util import PUBLIC_INPUTS_TX_LEN as TX_LEN
from .util import RLC, U8, U64, U160, U256, Expression, Word, WordOrValue, is_circuit_code
from .util import KeccakTable


@dataclass
Expand Down Expand Up @@ -75,32 +76,6 @@ class FixedU16Row(TableRow):
value: FQ


class KeccakTable:
# The columns are: (is_enabled, input_rlc, input_len, output)
table: Set[Tuple[FQ, FQ, FQ, Word]]

def __init__(self):
self.table = set()
self.table.add((FQ.zero(), FQ.zero(), FQ.zero(), Word(0))) # Add all 0s row

def add(self, input: bytes, keccak_randomness: FQ):
output = keccak(input)
self.table.add(
(
FQ.one(),
RLC(bytes(reversed(input)), keccak_randomness, n_bytes=len(input)).expr(),
FQ(len(input)),
Word(output),
)
)

def lookup(self, is_enabled: FQ, input_rlc: FQ, input_len: FQ, output: Word, assert_msg: str):
assert (is_enabled, input_rlc, input_len, output) in self.table, (
f"{assert_msg}: {(is_enabled, input_rlc, input_len, output)} "
+ "not found in the lookup table"
)


@dataclass
class Row:
"""PublicInputs circuit row"""
Expand Down
28 changes: 1 addition & 27 deletions src/zkevm_specs/tx_circuit.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@
GAS_COST_TX_CALL_DATA_PER_NON_ZERO_BYTE,
GAS_COST_TX_CALL_DATA_PER_ZERO_BYTE,
is_circuit_code,
KeccakTable,
)
from eth_keys import KeyAPI # type: ignore
import rlp # type: ignore
from eth_utils import keccak
from .evm_circuit import TxContextFieldTag as Tag


class Row:
"""
Tx circuit row
Expand All @@ -35,32 +35,6 @@ def __init__(self, tx_id: FQ, tag: FQ, index: FQ, value: Union[FQ, Word]):
self.value = WordOrValue(value)


class KeccakTable:
# The columns are: (is_enabled, input_rlc, input_len, output)
table: Set[Tuple[FQ, FQ, FQ, Word]]

def __init__(self):
self.table = set()
self.table.add((FQ(0), FQ(0), FQ(0), Word(0))) # Add all 0s row

def add(self, input: bytes, keccak_randomness: FQ):
output = keccak(input)
self.table.add(
(
FQ(1),
RLC(bytes(reversed(input)), keccak_randomness, n_bytes=64).expr(),
FQ(len(input)),
Word(output),
)
)

def lookup(self, is_enabled: FQ, input_rlc: FQ, input_len: FQ, output: Word, assert_msg: str):
assert (is_enabled, input_rlc, input_len, output) in self.table, (
f"{assert_msg}: {(is_enabled, input_rlc, input_len, output)} "
+ "not found in the lookup table"
)


class WrongFieldInteger:
"""
Wrong Field arithmetic Integer, representing the implementation at
Expand Down
7 changes: 4 additions & 3 deletions src/zkevm_specs/util/tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@ def __init__(self):

def add(self, input: bytes, keccak_randomness: FQ):
output = keccak(input)
length = len(input)
self.table.add(
(
FQ(1),
RLC(bytes(reversed(input)), keccak_randomness, n_bytes=64).expr(),
FQ(len(input)),
FQ.one(),
RLC(bytes(reversed(input)), keccak_randomness, n_bytes=length).expr(),
FQ(length),
Word(output),
)
)
Expand Down
28 changes: 1 addition & 27 deletions src/zkevm_specs/withdrawal_circuit.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
Word,
Expression,
is_circuit_code,
KeccakTable
)
import rlp # type: ignore
from .evm_circuit import lookup
Expand Down Expand Up @@ -90,33 +91,6 @@ def block_lookup(self, field_tag: Expression, value: Word) -> BlockTableRow:
return lookup(BlockTableRow, self.table, query)


class KeccakTable:
# The columns are: (is_enabled, input_rlc, input_len, output)
table: Set[Tuple[FQ, FQ, FQ, Word]]

def __init__(self):
self.table = set()
self.table.add((FQ(0), FQ(0), FQ(0), Word(0))) # Add all 0s row

def add(self, input: bytes, keccak_randomness: FQ):
output = keccak(input)
length = len(input)
self.table.add(
(
FQ(1),
RLC(bytes(reversed(input)), keccak_randomness, n_bytes=length).expr(),
FQ(length),
Word(output),
)
)

def lookup(self, is_enabled: FQ, input_rlc: FQ, input_len: FQ, output: Word, assert_msg: str):
assert (is_enabled, input_rlc, input_len, output) in self.table, (
f"{assert_msg}: {(is_enabled, input_rlc, input_len, output)} "
+ "not found in the lookup table"
)


class Witness(NamedTuple):
rows: List[Row] # Withdrawal table rows
mpt_table: MPTTable
Expand Down

0 comments on commit cc06b41

Please sign in to comment.