This repository has been archived by the owner on Jul 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 272
/
pi_circuit.py
932 lines (801 loc) · 33.8 KB
/
pi_circuit.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
from dataclasses import dataclass
from typing import List, Set, Tuple, Union
from eth_utils import keccak
from zkevm_specs.util.arithmetic import bytes_to_fq
from zkevm_specs.util.param import N_BYTES_WORD
from .evm_circuit.table import TableRow, lookup
from .tx_circuit import Tag as TxTag
from .util import FQ, GAS_COST_TX_CALL_DATA_PER_NON_ZERO_BYTE, GAS_COST_TX_CALL_DATA_PER_ZERO_BYTE
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
@dataclass
class BlockTable:
table: List[WordOrValue]
def __init__(self):
self.table = []
def add(self, value: WordOrValue):
self.table.append(value)
@dataclass
class TxTableRow:
tx_id: FQ
tag: FQ # Fixed Column
index: FQ
value: WordOrValue
@dataclass
class TxTable:
table: List[TxTableRow]
def __init__(self):
self.table = []
def add(self, tx_id: FQ, tag: FQ, index: FQ, value: WordOrValue):
self.table.append(TxTableRow(tx_id, tag, index, value))
@dataclass(frozen=True)
class TxCallDataGasCostAccRow(TableRow):
tx_id: Expression
is_final: FQ
gas_cost_acc: FQ
@dataclass(frozen=True)
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"""
q_bytes_last: FQ
q_tx_table: FQ
q_tx_calldata: FQ
q_tx_calldata_start: FQ
q_rpi_keccak_lookup: FQ
q_rpi_value_start: FQ # Fixed Column
tx_id_inv: FQ # (tx_tag - CallDataLength)^(-1) when q_tx_table = 1
# tx_id^(-1) when q_tx_calldata = 1
tx_value_lo_inv: FQ
tx_id_diff_inv: FQ
calldata_gas_cost: FQ
is_final: FQ
rpi_bytes: FQ
rpi_bytes_keccakrlc: FQ
rpi_value_lc: FQ
rpi_digest_word: Word
q_rpi_byte_enable: FQ
keccak_table: KeccakTable
tx_table: TxTableRow
@dataclass
class PublicInputs:
"""Public Inputs of the PublicInputs circuit"""
pi_keccak: Word
# FIXME temporarily put block_hash state_root and pre_state_root here, since no table refer to those
# just temporarily place to carry data for copy constraints
block_hash: Word
state_root: Word
state_root_prev: Word
@is_circuit_code
def check_row(
row: Row,
row_next: Row,
calldata_gas_cost_table: Set[TxCallDataGasCostAccRow],
fixed_u16_table: Set[FixedU16Row],
keccak_table: KeccakTable,
circuit_len: FQ,
):
q_bytes_last = row.q_bytes_last
q_rpi_byte_enable = row.q_rpi_byte_enable
# gate 1 and gate 2 are compensation branch
# 1: rpi_bytes_keccakrlc[last] = rpi_bytes[last]
assert q_rpi_byte_enable * q_bytes_last * (row.rpi_bytes_keccakrlc - row.rpi_bytes) == FQ.zero()
# 2: rpi_bytes_keccakrlc[i] = keccak_rand * rpi_bytes_keccakrlc[i+1] + rpi_bytes[i]
assert (
q_rpi_byte_enable
* (FQ.one() - q_bytes_last)
* (row.rpi_bytes_keccakrlc - (row_next.rpi_bytes_keccakrlc * keccak_rand + row.rpi_bytes))
) == FQ.zero()
## gate 3 and gate 4 are compensation branch
# | q_rpi_value_start | rpi_bytes | rpi_value_lc | gate |
# | ----------------- | --------- | ------------------ |------------|
# | ... | ..... | ... ... | |
# | | ee | ddee |gate 3 |
# | 1 | dd | dd |gate 4 |
# | | cc | aabbcc |gate 3 |
# | | bb | aabb |gate 3 |
# | 1 | aa | aa |gate 4 |
# 3: rpi_value_lc[i] = rpi_value_lc[i+1] * byte_pow_base + rpi_bytes[i]
assert (
row.q_rpi_byte_enable
* (FQ.one() - row.q_rpi_value_start)
* (row.rpi_value_lc - (row_next.rpi_value_lc * byte_pow_base + row.rpi_bytes))
== FQ.zero()
)
# 4. rpi_value_lc[i] = rpi_bytes[i]
assert (
row.q_rpi_byte_enable * row.q_rpi_value_start * (row.rpi_value_lc - row.rpi_bytes)
== FQ.zero()
)
# 5. lookup rpi_bytes_keccakrlc against rpi_digest_word
keccak_table.lookup(
row.q_rpi_keccak_lookup,
row.q_rpi_keccak_lookup * row.rpi_bytes_keccakrlc,
row.q_rpi_keccak_lookup * circuit_len,
row.rpi_digest_word.select(row.q_rpi_keccak_lookup),
"lookup not found",
)
zero = FQ.zero()
one = FQ.one()
if row.q_tx_calldata != zero:
assert row.tx_table.tx_id * (one - row.tx_id_inv * row.tx_table.tx_id) == zero
assert (
row.tx_table.value.lo.expr()
* (one - row.tx_value_lo_inv * row.tx_table.value.lo.expr())
== zero
)
assert (row_next.tx_table.tx_id - row.tx_table.tx_id) * (
one - row.tx_id_diff_inv * (row_next.tx_table.tx_id - row.tx_table.tx_id)
) == zero
is_tx_id_nonzero = row.tx_table.tx_id * row.tx_id_inv
is_tx_id_next_nonzero = row_next.tx_table.tx_id * row_next.tx_id_inv
is_tx_id_zero = one - is_tx_id_nonzero
is_tx_id_next_zero = one - is_tx_id_next_nonzero
tx_id_not_equal_to_next = (
row_next.tx_table.tx_id - row.tx_table.tx_id
) * row.tx_id_diff_inv
tx_id_equal_to_next = one - tx_id_not_equal_to_next
is_byte_nonzero = row.tx_table.value.lo.expr() * row.tx_value_lo_inv
is_byte_next_nonzero = row_next.tx_table.value.lo.expr() * row_next.tx_value_lo_inv
is_byte_zero = one - is_byte_nonzero
is_byte_next_zero = one - is_byte_next_nonzero
default_calldata_row_constraints = [
is_tx_id_zero * row.tx_table.tx_id,
is_tx_id_zero * row_next.tx_table.tx_id,
is_tx_id_zero * row.is_final,
is_tx_id_zero * row.calldata_gas_cost,
]
for cons in default_calldata_row_constraints:
assert cons == zero
gas_cost = (
FQ(GAS_COST_TX_CALL_DATA_PER_NON_ZERO_BYTE) * is_byte_nonzero
+ FQ(GAS_COST_TX_CALL_DATA_PER_ZERO_BYTE) * is_byte_zero
)
gas_cost_next = (
FQ(GAS_COST_TX_CALL_DATA_PER_NON_ZERO_BYTE) * is_byte_next_nonzero
+ FQ(GAS_COST_TX_CALL_DATA_PER_ZERO_BYTE) * is_byte_next_zero
)
tx_id_diff_minus_one = row_next.tx_table.tx_id - row.tx_table.tx_id - one
tx_id_diff_minus_one_query = {
"value": tx_id_not_equal_to_next * is_tx_id_next_nonzero * tx_id_diff_minus_one
}
lookup(FixedU16Row, fixed_u16_table, tx_id_diff_minus_one_query)
idx_of_same_tx_constraint = tx_id_equal_to_next * (
row_next.tx_table.index - row.tx_table.index - one
)
idx_of_next_tx_constraint = (
row_next.tx_table.tx_id - row.tx_table.tx_id
) * row_next.tx_table.index
gas_cost_of_same_tx_constraint = tx_id_equal_to_next * (
row_next.calldata_gas_cost - row.calldata_gas_cost - gas_cost_next
)
gas_cost_of_next_tx_constraint = (
is_tx_id_next_nonzero
* (row_next.tx_table.tx_id - row.tx_table.tx_id)
* (row_next.calldata_gas_cost - gas_cost_next)
)
gas_cost_of_last_tx_constraint = is_tx_id_next_zero * row_next.calldata_gas_cost
is_final_of_same_tx_constraint = tx_id_equal_to_next * row.is_final
is_final_of_next_tx_constraint = (row_next.tx_table.tx_id - row.tx_table.tx_id) * (
row.is_final - one
)
constraints = [
is_tx_id_nonzero * idx_of_same_tx_constraint,
is_tx_id_nonzero * idx_of_next_tx_constraint,
is_tx_id_nonzero * gas_cost_of_same_tx_constraint,
is_tx_id_nonzero * gas_cost_of_next_tx_constraint,
is_tx_id_nonzero * gas_cost_of_last_tx_constraint,
is_tx_id_nonzero * is_final_of_same_tx_constraint,
is_tx_id_nonzero * is_final_of_next_tx_constraint,
]
for cons_id, cons in enumerate(constraints):
assert cons == zero
assert row.q_tx_calldata_start * is_tx_id_nonzero * row.tx_table.index == zero
assert (
row.q_tx_calldata_start * is_tx_id_nonzero * (row.calldata_gas_cost - gas_cost) == zero
)
if row.q_tx_table != zero:
row_is_cdl = row.tx_table.tag - FQ(TxTag.CallDataLength)
assert row_is_cdl * (one - row.tx_id_inv * row_is_cdl) == zero
assert (
row.tx_table.value.lo.expr()
* (one - row.tx_value_lo_inv * row.tx_table.value.lo.expr())
== zero
)
is_calldata_length_row = one - row_is_cdl * row.tx_id_inv
is_calldata_length_nonzero = row.tx_table.value.lo.expr() * row.tx_value_lo_inv
is_calldata_length_zero = one - is_calldata_length_nonzero
calldata_cost = row_next.tx_table.value.lo.expr()
assert is_calldata_length_row * is_calldata_length_zero * calldata_cost == zero
query_condition = is_calldata_length_row * is_calldata_length_nonzero
query = {
"tx_id": row.tx_table.tx_id * query_condition,
"is_final": one * query_condition,
"gas_cost_acc": calldata_cost * query_condition,
}
lookup(TxCallDataGasCostAccRow, calldata_gas_cost_table, query)
@dataclass
class Witness:
rows: List[Row] # PublicInputs rows
public_inputs: PublicInputs # Public Inputs of the PublicInputs circuit
calldata_gas_cost_table: Set[TxCallDataGasCostAccRow]
keccak_table: KeccakTable
block_table: BlockTable
tx_table: TxTable
circuit_len: int
copy_constrains: List[bytes]
@is_circuit_code
def verify_circuit(
witness: Witness,
MAX_TXS: int,
MAX_CALLDATA_BYTES: int,
) -> None:
"""
Entry level circuit verification function
"""
rows = witness.rows
calldata_gas_cost_table = witness.calldata_gas_cost_table
public_inputs = witness.public_inputs
keccak_table = witness.keccak_table
block_table = witness.block_table
tx_table = witness.tx_table
copy_constrains = witness.copy_constrains
fixed_u16_table = set([FixedU16Row(FQ(i)) for i in range(1 << 16)])
# copy constraint from public input to advice column
# must copy constrain `hi` part to zero for non_word value, otherwise `hi` can be anything
# constrain witness rpi digest lo/hi equals pi input keccak lo/hi
assert rows[0].rpi_digest_word == public_inputs.pi_keccak
# constrain block table word_or_value equals witness rpi bytes in vertical order
for i in range(BLOCK_LEN // 2 + 1):
block_row = block_table.table[i]
lo = copy_constrains.pop(0)
if block_row.is_word:
hi = copy_constrains.pop(0)
else:
hi = bytes(0)
(lo_expr, hi_expr) = block_row.to_lo_hi()
assert lo_expr == bytes_to_fq(lo)
assert hi_expr == bytes_to_fq(hi)
# constrain block_hash and state_root lo/hi.
# TODO layout block_hash in proper table
lo = copy_constrains.pop(0)
hi = copy_constrains.pop(0)
assert public_inputs.block_hash.lo.expr() == bytes_to_fq(lo)
assert public_inputs.block_hash.hi.expr() == bytes_to_fq(hi)
# TODO layout state_root in proper table
lo = copy_constrains.pop(0)
hi = copy_constrains.pop(0)
assert public_inputs.state_root.lo.expr() == bytes_to_fq(lo)
assert public_inputs.state_root.hi.expr() == bytes_to_fq(hi)
# TODO layout state_root_prev in proper table
lo = copy_constrains.pop(0)
hi = copy_constrains.pop(0)
assert public_inputs.state_root_prev.lo.expr() == bytes_to_fq(lo)
assert public_inputs.state_root_prev.hi.expr() == bytes_to_fq(hi)
# constrain tx table `id``, `index`, value lo/hi per row, and all rows equals witness rpi bytes in vertical order
tx_len = TX_LEN * MAX_TXS + 1
for i in range(tx_len):
tx_row: TxTableRow = tx_table.table[i]
tx_id, index, value = tx_row.tx_id, tx_row.index, tx_row.value
lo = copy_constrains.pop(0)
assert tx_id == bytes_to_fq(lo)
lo = copy_constrains.pop(0)
assert index == bytes_to_fq(lo)
lo = copy_constrains.pop(0)
if value.is_word:
hi = copy_constrains.pop(0)
else:
hi = bytes(0)
assert value.lo.expr() == bytes_to_fq(lo)
assert value.hi.expr() == bytes_to_fq(hi)
# constrain tx calldata value lo/hi euqal to equals witness rpi bytes in vertical order
calldata_len = MAX_CALLDATA_BYTES
for i in range(calldata_len):
value = tx_table.table[tx_len + i].value
lo = copy_constrains.pop(0)
if value.is_word:
hi = copy_constrains.pop(0)
else:
hi = bytes(0)
assert value.lo.expr() == bytes_to_fq(lo)
assert value.hi.expr() == bytes_to_fq(hi)
# check gates constrains
for i in range(len(rows)):
row: Row = rows[i]
row_next = rows[(i + 1) % len(rows)]
check_row(
row,
row_next,
calldata_gas_cost_table,
fixed_u16_table,
keccak_table,
witness.circuit_len,
)
@dataclass
class Block:
"""Block header"""
hash: U256
parent_hash: U256
uncle_hash: U256
coinbase: U160
state_root: U256 # State Trie Root
tx_hash: U256 # Txs Trie Root
receipt_hash: U256 # Receipts Trie Root
bloom: bytes # 256 bytes
difficulty: U256
number: U64
gas_limit: U64
gas_used: U64
time: U64
extra: bytes # NOTE: We assume this is always an empty byte array
mix_digest: U256
nonce: U64
base_fee: U256 # NOTE: BaseFee was added by EIP-1559 and is ignored in legacy headers.
@dataclass
class Transaction:
nonce: U64
gas_price: U256
gas: U64
from_addr: U160
to_addr: Union[None, U160]
value: U256
data: bytes
tx_sign_hash: U256
@classmethod
def default(cls):
return Transaction(U64(0), U256(0), U64(0), U160(0), U160(0), U256(0), bytes([]), U256(0))
def tx_table_value_column(self) -> List[WordOrValue]:
"""Return the tx table value column corresponding to this tx. Contains fields and no calldata"""
column = []
column.append(WordOrValue(FQ(self.nonce))) # Nonce
column.append(WordOrValue(FQ(self.gas))) # Gas
column.append(WordOrValue(Word(self.gas_price))) # GasPrice
column.append(WordOrValue(FQ(self.from_addr))) # CallerAddress
column.append(WordOrValue(FQ(self.to_addr or 0))) # CalleeAddress
column.append(WordOrValue(FQ(1 if self.to_addr is None else 0))) # IsCreate
column.append(WordOrValue(Word(self.value))) # Value
column.append(WordOrValue(FQ(len(self.data)))) # CallDataLength
call_data_gas_cost = sum(
[
(
GAS_COST_TX_CALL_DATA_PER_ZERO_BYTE
if byte == 0
else GAS_COST_TX_CALL_DATA_PER_NON_ZERO_BYTE
)
for byte in self.data
]
)
column.append(WordOrValue(FQ(call_data_gas_cost))) # CallDataCost
column.append(WordOrValue(Word(self.tx_sign_hash))) # TxSignHash
return column
def tx_raw_bytes(self, tx_id: int) -> List[bytes]:
tx_raw_byte: List[bytes] = []
self.append_raw_byte_with_id_index(
tx_raw_byte, tx_id, self.nonce.to_bytes(8, "little")
) # Nonce
self.append_raw_byte_with_id_index(
tx_raw_byte, tx_id, self.gas.to_bytes(8, "little")
) # Gas Limit
gas_price_lo, gas_price_hi = Word(self.gas_price).to_lo_hi()
self.append_raw_byte_with_id_index(
tx_raw_byte,
tx_id,
gas_price_lo.n.to_bytes(16, "little"),
gas_price_hi.n.to_bytes(16, "little"),
) # GasPrice
self.append_raw_byte_with_id_index(
tx_raw_byte, tx_id, self.from_addr.to_bytes(20, "little")
) # CallerAddress
self.append_raw_byte_with_id_index(
tx_raw_byte, tx_id, (self.to_addr or U160(0)).to_bytes(20, "little")
) # CalleeAddress
self.append_raw_byte_with_id_index(
tx_raw_byte,
tx_id,
(U64(1) if self.to_addr is None else U64(0)).to_bytes(8, "little"),
) # IsCreate
value_lo, value_hi = Word(self.value).to_lo_hi()
self.append_raw_byte_with_id_index(
tx_raw_byte,
tx_id,
value_lo.n.to_bytes(16, "little"),
value_hi.n.to_bytes(16, "little"),
) # Value
self.append_raw_byte_with_id_index(
tx_raw_byte, tx_id, U64(len(self.data)).to_bytes(8, "little")
) # CallDataLength
call_data_gas_cost = sum(
[
(
GAS_COST_TX_CALL_DATA_PER_ZERO_BYTE
if byte == 0
else GAS_COST_TX_CALL_DATA_PER_NON_ZERO_BYTE
)
for byte in self.data
]
)
self.append_raw_byte_with_id_index(
tx_raw_byte, tx_id, U64(call_data_gas_cost).to_bytes(8, "little")
) # CallDataCost
tx_sign_hash_lo, tx_sign_hash_hi = Word(self.tx_sign_hash).to_lo_hi()
self.append_raw_byte_with_id_index(
tx_raw_byte,
tx_id,
tx_sign_hash_lo.n.to_bytes(16, "little"),
tx_sign_hash_hi.n.to_bytes(16, "little"),
) # TxSignHash
return tx_raw_byte
def append_raw_byte_with_id_index(
self,
raw_byte_value_col: List[bytes],
tx_id: int,
value_lo: bytes,
value_hi: bytes = bytes(0),
):
raw_byte_value_col.append(U64(tx_id).to_bytes(8, "little"))
raw_byte_value_col.append(U64(0).to_bytes(8, "little"))
raw_byte_value_col.append(value_lo)
if value_hi != bytes(0):
raw_byte_value_col.append(value_hi)
def tx_table_tx_fields(self, tx_id: int) -> Tuple[List[FQ], List[FQ], List[WordOrValue]]:
"""Return the tx table contents corresponding to this tx. Contains fields and no calldata"""
tx_id_col = [FQ(tx_id + 1)] * TX_LEN
index_col = [FQ.zero()] * TX_LEN
value_col = self.tx_table_value_column()
assert len(value_col) == TX_LEN
return (tx_id_col, index_col, value_col)
@dataclass
class PublicData:
chain_id: U64
block: Block
state_root_prev: U256
block_hashes: List[U256] # 256 previous block hashes
txs: List[Transaction]
def block_table_value_column(self) -> List[WordOrValue]:
"""Return the block table value column including the first 0 row"""
column = []
column.append(WordOrValue(FQ.zero())) # offset = 0
column.append(WordOrValue(FQ(self.block.coinbase)))
column.append(WordOrValue(FQ(self.block.gas_limit)))
column.append(WordOrValue(FQ(self.block.number)))
column.append(WordOrValue(FQ(self.block.time)))
column.append(WordOrValue(Word(self.block.difficulty)))
column.append(WordOrValue(Word(self.block.base_fee)))
column.append(WordOrValue(FQ(self.chain_id)))
assert len(self.block_hashes) == 256
for block_hash in self.block_hashes:
column.append(WordOrValue(Word(block_hash))) # offset = 8
return column
def block_table_raw_byte_values(self) -> List[bytes]:
"""Return the block table bytes, including first 0 row"""
raw_block_value = []
raw_block_value.append(U8(0).to_bytes(1, "little")) # offset = 0
raw_block_value.append(self.block.coinbase.to_bytes(20, "little"))
raw_block_value.append(self.block.gas_limit.to_bytes(8, "little"))
raw_block_value.append(self.block.number.to_bytes(8, "little"))
raw_block_value.append(self.block.time.to_bytes(8, "little"))
difficulty_lo, difficulty_hi = Word(self.block.difficulty).to_lo_hi()
raw_block_value.append(difficulty_lo.n.to_bytes(16, "little"))
raw_block_value.append(difficulty_hi.n.to_bytes(16, "little"))
base_fee_lo, base_fee_hi = Word(self.block.base_fee).to_lo_hi()
raw_block_value.append(base_fee_lo.n.to_bytes(16, "little"))
raw_block_value.append(base_fee_hi.n.to_bytes(16, "little"))
raw_block_value.append(self.chain_id.to_bytes(8, "little"))
assert len(self.block_hashes) == 256
for block_hash in self.block_hashes:
block_hash_lo, block_hash_hi = Word(block_hash).to_lo_hi()
raw_block_value.append(block_hash_lo.n.to_bytes(16, "little"))
raw_block_value.append(block_hash_hi.n.to_bytes(16, "little"))
return raw_block_value
def tx_table_raw_bytes(self, MAX_TXS: int) -> List[bytes]:
"""Return the tx table bytes, traverse in row oriented and including first 0 row"""
table_raw_bytes = []
assert len(self.txs) > 0
assert len(self.txs) <= MAX_TXS
table_raw_bytes.append(U64(0).to_bytes(8, "little")) # empty id
table_raw_bytes.append(U64(0).to_bytes(8, "little")) # empty index
table_raw_bytes.append(U8(0).to_bytes(1, "little")) # empty value lo
for i in range(MAX_TXS):
tx = Transaction.default()
if i < len(self.txs):
tx = self.txs[i]
table_raw_bytes.extend([tx_bytes for tx_bytes in tx.tx_raw_bytes(i + 1)])
return table_raw_bytes
def tx_table_calldata_raw_bytes(self, MAX_CALLDATA_BYTES: int) -> List[bytes]:
tx_calldata_raw_bytes = []
calldata_count = 0
for i, tx in enumerate(self.txs):
for byte_index, byte in enumerate(tx.data):
tx_calldata_raw_bytes.append(U8(byte).to_bytes(1, "little"))
calldata_count += 1
assert calldata_count <= MAX_CALLDATA_BYTES
for _ in range(MAX_CALLDATA_BYTES - calldata_count):
tx_calldata_raw_bytes.append(U8(0).to_bytes(1, "little"))
return tx_calldata_raw_bytes
def tx_table_tx_cols(self, MAX_TXS: int) -> Tuple[List[FQ], List[FQ], List[WordOrValue]]:
"""Return the tx table, static section with tx fields (no calldata)"""
tx_id_col = []
index_col = []
value_col = []
assert len(self.txs) <= MAX_TXS
for i in range(MAX_TXS):
tx = Transaction.default()
if i < len(self.txs):
tx = self.txs[i]
(tx_id_col_i, index_col_i, value_col_i) = tx.tx_table_tx_fields(i)
tx_id_col.extend(tx_id_col_i)
index_col.extend(index_col_i)
value_col.extend(value_col_i)
return (tx_id_col, index_col, value_col)
def tx_table_tx_calldata_cols(
self, MAX_CALLDATA_BYTES: int
) -> Tuple[List[FQ], List[FQ], List[WordOrValue], List[FQ], List[FQ]]:
"""Return the tx table, dynamic section with calldata"""
tx_id_col = []
index_col = []
value_col = []
gas_cost_col = []
is_final_col = []
for i, tx in enumerate(self.txs):
gas_cost_acc = 0
for byte_index, byte in enumerate(tx.data):
tx_id_col.append(FQ(i + 1))
index_col.append(FQ(byte_index))
value_col.append(WordOrValue(FQ(byte)))
if byte == 0:
gas_cost_acc += GAS_COST_TX_CALL_DATA_PER_ZERO_BYTE
else:
gas_cost_acc += GAS_COST_TX_CALL_DATA_PER_NON_ZERO_BYTE
if byte_index == len(tx.data) - 1:
is_final = 1
else:
is_final = 0
gas_cost_col.append(FQ(gas_cost_acc))
is_final_col.append(FQ(is_final))
assert len(value_col) <= MAX_CALLDATA_BYTES
calldata_padding = [FQ.zero()] * (MAX_CALLDATA_BYTES - len(value_col))
tx_id_col.extend(calldata_padding)
index_col.extend(calldata_padding)
value_col.extend([WordOrValue(v) for v in calldata_padding])
gas_cost_col.extend(calldata_padding)
is_final_col.extend(calldata_padding)
return (tx_id_col, index_col, value_col, gas_cost_col, is_final_col)
def tx_table_cols(
self, MAX_TXS: int, MAX_CALLDATA_BYTES: int
) -> Tuple[List[FQ], List[FQ], List[WordOrValue]]:
"""Return the complete tx table including the initial 0 row"""
tx_cols = self.tx_table_tx_cols(MAX_TXS)
tx_calldata = self.tx_table_tx_calldata_cols(MAX_CALLDATA_BYTES)
return (
[FQ.zero()] + tx_cols[0] + tx_calldata[0],
[FQ.zero()] + tx_cols[1] + tx_calldata[1],
[WordOrValue(FQ.zero())] + tx_cols[2] + tx_calldata[2],
)
N_BYTES_ONE = 1
N_BYTES_U64 = 8
N_BYTES_TX = 176
N_BYTES_BLOCK = (
+20 # coinbase
+ 8 # gas limit
+ 8 # block number
+ 8 # timestamp
+ 32 # difficulty
+ 32 # base_fee
+ 8 # chain_id
+ 32 * 256 # pre block hashes
)
N_BYTES_EXTRA_VALUE = N_BYTES_WORD * 3
byte_pow_base = FQ(255)
evm_rand = FQ(255)
keccak_rand = FQ(255)
def public_data2witness(
public_data: PublicData, MAX_TXS: int, MAX_CALLDATA_BYTES: int, rand_rpi: FQ
) -> Witness:
# Layout of raw_public_inputs:
# # Block Table. `value.hi` is optional dependes on the original value bits size.
# [0] + [block_table.value.lo, (block_table.value.hi)]...
# # Extra Fields
# [state_root.lo, state_root.hi] # 2
# [state_root_prev.lo, state_root_prev.hi] # 2
# # Tx Table, `value.hi` is optional dependes on the original value bits size.
# [0, 0, 0] // empty row
# + [tx_table.id, tx_table.index, tx_table.value.lo, (tx_table.value.hi)]... # TX_LEN * MAX_TXS + 1
# [tx_table.calldata.lo] # MAX_CALLDATA_BYTES
rpi_byte_values: List[bytes] = []
# Block table
block_table_value_col = public_data.block_table_value_column()
block_table_raw_byte_values = public_data.block_table_raw_byte_values()
rpi_byte_values.extend(block_table_raw_byte_values)
# Extra fields
hash_lo, hash_hi = Word(public_data.block.hash).to_lo_hi()
rpi_byte_values.append(hash_lo.n.to_bytes(16, "little"))
rpi_byte_values.append(hash_hi.n.to_bytes(16, "little"))
state_root_lo, state_root_hi = Word(public_data.block.state_root).to_lo_hi()
rpi_byte_values.append(state_root_lo.n.to_bytes(16, "little"))
rpi_byte_values.append(state_root_hi.n.to_bytes(16, "little"))
state_root_prev_lo, state_root_prev_hi = Word(public_data.state_root_prev).to_lo_hi()
rpi_byte_values.append(state_root_prev_lo.n.to_bytes(16, "little"))
rpi_byte_values.append(state_root_prev_hi.n.to_bytes(16, "little"))
assert flatten_len(rpi_byte_values) == N_BYTES_ONE + N_BYTES_BLOCK + N_BYTES_EXTRA_VALUE
# Tx Table
tx_table_cols = public_data.tx_table_cols(MAX_TXS, MAX_CALLDATA_BYTES)
tx_table_tx_calldata = public_data.tx_table_tx_calldata_cols(MAX_CALLDATA_BYTES)
# traverse column tuple in row order
tx_table_raw_bytes = public_data.tx_table_raw_bytes(MAX_TXS)
rpi_byte_values.extend(tx_table_raw_bytes)
keccak_table = KeccakTable()
block_table = BlockTable()
tx_table = TxTable()
assert flatten_len(rpi_byte_values) == (
N_BYTES_ONE # empty block row
+ N_BYTES_BLOCK # block
+ N_BYTES_EXTRA_VALUE # extra value
+ N_BYTES_U64 * TX_LEN * MAX_TXS
+ N_BYTES_U64 # tx_id + first empty
+ N_BYTES_U64 * TX_LEN * MAX_TXS
+ N_BYTES_U64 # txindex + first empty
+ N_BYTES_TX * MAX_TXS
+ N_BYTES_ONE # tx value
)
# Tx Calldata
tx_table_calldata_raw_bytes = public_data.tx_table_calldata_raw_bytes(MAX_CALLDATA_BYTES)
rpi_byte_values.extend(tx_table_calldata_raw_bytes)
circuit_len = (
N_BYTES_ONE # empty block row
+ N_BYTES_BLOCK # block
+ N_BYTES_EXTRA_VALUE # extra value
+ N_BYTES_U64 * TX_LEN * MAX_TXS
+ N_BYTES_U64 # tx_id + first empty
+ N_BYTES_U64 * TX_LEN * MAX_TXS
+ N_BYTES_U64 # txindex + first empty
+ N_BYTES_TX * MAX_TXS
+ N_BYTES_ONE # tx value
+ MAX_CALLDATA_BYTES
)
assert flatten_len(rpi_byte_values) == circuit_len
rows: List[Row] = []
calldata_gas_cost_table = [TxCallDataGasCostAccRow(FQ.zero(), FQ.zero(), FQ.zero())]
i = circuit_len - 1
rpi_bytes_keccakrlc = []
rpi_value_lc = []
rpi_bytes = []
for value in reversed(rpi_byte_values): # acc from big endian
for byte_index, byte in enumerate(reversed(value)):
rpi_bytes.append(byte)
q_rpi_byte_enable = FQ.one()
q_bytes_last = FQ.one() if len(rpi_bytes) == 1 else FQ.zero()
q_rpi_keccak_lookup = (
FQ.one() if i == 0 else FQ.zero()
) # keccak lookup happened in first row
q_rpi_value_start = FQ.zero()
if i == circuit_len - 1:
rpi_bytes_keccakrlc = [FQ(byte)]
else:
rpi_bytes_keccakrlc.append(FQ(rpi_bytes_keccakrlc[-1] * keccak_rand + byte))
if byte_index == 0:
q_rpi_value_start = FQ.one()
rpi_value_lc.append(FQ(byte))
else:
rpi_value_lc.append(FQ(rpi_value_lc[-1] * byte_pow_base + byte))
if i < BLOCK_LEN // 2 + 1:
assert i < len(block_table_value_col)
block_table.add(block_table_value_col[i])
# FIXME: extra value not used in any place. Here add 2 copy constraint in block table just for aligment
if i == BLOCK_LEN // 2 + 1:
block_table.add(WordOrValue(Word(public_data.block.state_root)))
if i == BLOCK_LEN // 2 + 2:
block_table.add(WordOrValue(Word(public_data.state_root_prev)))
q_tx_table = FQ.zero()
q_tx_calldata = FQ.zero()
q_tx_calldata_start = FQ.zero()
tx_id_inv = FQ.zero()
tx_value_lo_inv = FQ.zero()
tx_id_diff_inv = FQ.zero()
calldata_gas_cost = FQ.zero()
is_final = FQ.zero()
tx_row = TxTableRow(FQ.zero(), FQ.zero(), FQ.zero(), WordOrValue(FQ.zero()))
tx_table_len = TX_LEN * MAX_TXS + 1
if i < tx_table_len + MAX_CALLDATA_BYTES:
tx_id = tx_table_cols[0][i]
index = tx_table_cols[1][i]
value = tx_table_cols[2][i]
tag = FQ(TxTag.CallData)
if i == 0:
tag = FQ.zero()
elif i < tx_table_len:
# Iterate over TxTag values (until TxTag.TxSignHash) in a cycle
tag = FQ((i % TX_LEN))
if i % TX_LEN == 0:
tag = FQ(TX_LEN)
if i < tx_table_len:
q_tx_table = FQ.one()
tx_id_inv = (tag - FQ(TxTag.CallDataLength)).inv()
tx_value_lo_inv = value.lo.expr().inv()
if i >= tx_table_len:
q_tx_calldata = FQ.one()
tx_id_inv = tx_id.inv()
tx_value_lo_inv = value.lo.expr().inv()
tx_id_next = FQ.zero()
if i < tx_table_len + MAX_CALLDATA_BYTES - 1:
tx_id_next = tx_table_cols[0][i + 1]
tx_id_diff_inv = (tx_id_next - tx_id).inv()
calldata_gas_cost = tx_table_tx_calldata[3][i - tx_table_len]
is_final = tx_table_tx_calldata[4][i - tx_table_len]
calldata_gas_cost_table.append(
TxCallDataGasCostAccRow(tx_id, is_final, calldata_gas_cost)
)
if i == tx_table_len:
q_tx_calldata_start = FQ.one()
tx_row = TxTableRow(tx_id, tag, index, value)
tx_table.add(tx_id, tag, index, value)
row = Row(
q_bytes_last,
q_tx_table,
q_tx_calldata,
q_tx_calldata_start,
q_rpi_keccak_lookup,
q_rpi_value_start,
tx_id_inv,
tx_value_lo_inv,
tx_id_diff_inv,
calldata_gas_cost,
is_final,
FQ(rpi_bytes[-1]),
rpi_bytes_keccakrlc[-1],
rpi_value_lc[-1],
Word(0), # rpi_digest_word will be set below
q_rpi_byte_enable,
keccak_table,
tx_row,
)
rows.append(row)
i -= 1
rows.reverse()
output_digest = keccak(bytes(rpi_bytes))
assert len(output_digest) == 32
# keccak lookup happened on 0 row
rows[0].rpi_digest_word = Word(output_digest)
public_inputs = PublicInputs(
pi_keccak=Word(output_digest),
block_hash=Word(public_data.block.hash),
state_root=Word(public_data.block.state_root),
state_root_prev=Word(public_data.state_root_prev),
)
keccak_table.add(bytes(rpi_bytes), keccak_rand)
block_table.table.reverse()
tx_table.table.reverse()
return Witness(
rows,
public_inputs,
set(calldata_gas_cost_table),
keccak_table,
block_table,
tx_table,
circuit_len,
copy_constrains=rpi_byte_values,
)
def flatten_len(a: List[bytes]):
return len([c for b in a for c in b])