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

Commit

Permalink
added padding advice column (#419)
Browse files Browse the repository at this point in the history
added python spec code and added is_final col

fixed lint errors

added padding 1^1

nit changes

convert steps -> rows and removed pad_rows

fixed typing issue

Co-authored-by: Richord <[email protected]>
  • Loading branch information
ed255 and rrzhang139 authored May 8, 2023
1 parent 1faf078 commit 0da1195
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 28 deletions.
50 changes: 26 additions & 24 deletions src/zkevm_specs/evm_circuit/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -783,23 +783,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, 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, 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 @@ -856,24 +854,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=Word(0),
exponent=Word(0),
exponentiation=Word(0),
a=Word(0),
b=Word(0),
c=Word(0),
d=Word(0),
q=Word(0),
r=FQ(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=Word(1),
exponent=Word(1),
exponentiation=Word(1),
a=Word(1),
b=Word(1),
c=Word(0),
d=Word(1),
q=Word(0),
r=FQ(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 @@ -28,9 +28,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
6 changes: 5 additions & 1 deletion tests/evm/test_exp.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,11 @@ def test_exp(base_int: int, exponent_int: int):
.stack_write(CALL_ID, 1023, exponentiation)
)

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

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

0 comments on commit 0da1195

Please sign in to comment.