From cc06b41b4caf6233ec284f9291d409eea61cae13 Mon Sep 17 00:00:00 2001 From: Mohammad Jahanara Date: Sat, 17 Feb 2024 16:18:52 -0800 Subject: [PATCH] removed douplicate KeccakTable classes --- src/zkevm_specs/pi_circuit.py | 27 +------------------------- src/zkevm_specs/tx_circuit.py | 28 +-------------------------- src/zkevm_specs/util/tables.py | 7 ++++--- src/zkevm_specs/withdrawal_circuit.py | 28 +-------------------------- 4 files changed, 7 insertions(+), 83 deletions(-) diff --git a/src/zkevm_specs/pi_circuit.py b/src/zkevm_specs/pi_circuit.py index 508306a22..d9ca48cd8 100644 --- a/src/zkevm_specs/pi_circuit.py +++ b/src/zkevm_specs/pi_circuit.py @@ -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 @@ -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""" diff --git a/src/zkevm_specs/tx_circuit.py b/src/zkevm_specs/tx_circuit.py index b19cda2b6..0b616d0c7 100644 --- a/src/zkevm_specs/tx_circuit.py +++ b/src/zkevm_specs/tx_circuit.py @@ -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 @@ -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 diff --git a/src/zkevm_specs/util/tables.py b/src/zkevm_specs/util/tables.py index 0816bfb58..3b92214f3 100644 --- a/src/zkevm_specs/util/tables.py +++ b/src/zkevm_specs/util/tables.py @@ -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), ) ) diff --git a/src/zkevm_specs/withdrawal_circuit.py b/src/zkevm_specs/withdrawal_circuit.py index b39b7db86..47b1083d7 100644 --- a/src/zkevm_specs/withdrawal_circuit.py +++ b/src/zkevm_specs/withdrawal_circuit.py @@ -11,6 +11,7 @@ Word, Expression, is_circuit_code, + KeccakTable ) import rlp # type: ignore from .evm_circuit import lookup @@ -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