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

[WIP] Refactor/#499 clean up duplicated KeccakTable #513

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
27 changes: 1 addition & 26 deletions src/zkevm_specs/tx_circuit.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
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
Expand All @@ -35,32 +36,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
35 changes: 1 addition & 34 deletions src/zkevm_specs/withdrawal_circuit.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,7 @@
BlockTableRow,
BlockContextFieldTag,
)
from .util import (
FQ,
RLC,
Word,
Expression,
is_circuit_code,
)
from .util import FQ, RLC, Word, Expression, is_circuit_code, KeccakTable
import rlp # type: ignore
from .evm_circuit import lookup
from eth_utils import keccak
Expand Down Expand Up @@ -90,33 +84,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