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

added padding advice column in Exp Circuit #359

Closed
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
50 changes: 26 additions & 24 deletions src/zkevm_specs/evm/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -742,23 +742,21 @@ def add(self, data: bytes, r: FQ) -> KeccakCircuit:

class ExpCircuit:
rows: List[ExpCircuitRow]
pad_rows: List[ExpCircuitRow]
max_exp_steps: int
OFFSET_INCREMENT = 7

def __init__(self, pad_rows: Optional[List[ExpCircuitRow]] = None) -> None:
def __init__(self, max_exp_steps: int = 100) -> None:
self.rows = []
self.pad_rows = []
if pad_rows is not None:
self.pad_rows = pad_rows
self.max_exp_steps = max_exp_steps

def table(self) -> Sequence[ExpCircuitRow]:
return self.rows + self.pad_rows
return self.rows

def add_event(self, base: int, exponent: int, randomness: FQ, identifier: IntOrFQ):
steps: List[Tuple[int, int, int]] = []
exponentiation = self._exp_by_squaring(base, exponent, steps)
steps.reverse()
self._append_steps(base, exponent, exponentiation, steps, randomness, identifier)
self._append_padding_row(identifier)
return self

def _exp_by_squaring(self, base: int, exponent: int, steps: List[Tuple[int, int, int]]):
Expand Down Expand Up @@ -816,24 +814,28 @@ def _append_steps(
# exponent is odd
exponent = exponent - 1

def _append_padding_row(self, identifier: IntOrFQ):
self.rows.append(
ExpCircuitRow(
q_usable=FQ.zero(),
is_step=FQ.zero(),
identifier=FQ(identifier),
is_last=FQ.zero(),
base=RLC(0),
exponent=RLC(0),
exponentiation=RLC(0),
a=RLC(0),
b=RLC(0),
c=RLC(0),
d=RLC(0),
q=RLC(0),
r=RLC(0),
def fill_dummy_events(self):
max_exp_rows = self.max_exp_steps * self.OFFSET_INCREMENT
rows_left = max_exp_rows - len(self.rows)
for i in range(rows_left):
self.rows.append(
ExpCircuitRow(
q_usable=FQ.one(),
is_step=FQ.zero(),
identifier=FQ.zero(),
is_last=FQ.zero(),
base=RLC(1),
exponent=RLC(1),
exponentiation=RLC(1),
a=RLC(1),
b=RLC(1),
c=RLC(0),
d=RLC(1),
q=RLC(0),
r=RLC(1),
)
)
)
return self

def _append_step(
self,
Expand Down
3 changes: 0 additions & 3 deletions src/zkevm_specs/exp_circuit.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,6 @@ def verify_step(cs: ConstraintSystem, rows: List[ExpCircuitRow]):
cs.constrain_bool(rows[0].is_last)
# remainder (r), i.e. odd/even parity of exponent is boolean.
cs.constrain_bool(rows[0].r)
# is_last == 1 is followed by unusable row.
# is_last == 0 is following by usable row.
cs.constrain_equal(rows[0].is_last, (1 - rows[1].q_usable))
# multiplication is assigned correctly
_overflow, carry_lo_hi, additional_constraints = mul_add_words(
rows[0].a, rows[0].b, rows[0].c, rows[0].d
Expand Down
4 changes: 3 additions & 1 deletion tests/evm/test_exp.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,9 @@ def test_exp(base: int, exponent: int):
.stack_write(CALL_ID, 1023, exponentiation_rlc)
)

exp_circuit = ExpCircuit().add_event(base, exponent, randomness, rw_dict.rw_counter)
exp_circuit = (
ExpCircuit().add_event(base, exponent, randomness, rw_dict.rw_counter).fill_dummy_events()
)

tables = Tables(
block_table=set(Block().table_assignments(randomness)),
Expand Down