Skip to content

Commit f4df65a

Browse files
Eip 2537 precompile for bls12 381 curve operations (#654)
Signed-off-by: Olivier Bégassat <[email protected]> Co-authored-by: Olivier Bégassat <[email protected]>
1 parent c7825b7 commit f4df65a

File tree

59 files changed

+1443
-68
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+1443
-68
lines changed

Makefile

Lines changed: 73 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,49 @@ BLOCKDATA_CANCUN := blockdata/cancun
2323

2424
BLOCKHASH := blockhash
2525

26+
BLS_CANCUN := $(wildcard blsdata/cancun/*.lisp) \
27+
$(wildcard blsdata/cancun/generalities/cancun_restriction.lisp) \
28+
$(wildcard blsdata/cancun/generalities/constancy_conditions.lisp) \
29+
$(wildcard blsdata/cancun/generalities/constraining_address_sum.lisp) \
30+
$(wildcard blsdata/cancun/generalities/constraining_flag_sum.lisp) \
31+
$(wildcard blsdata/cancun/generalities/constraints_for_bls_stamp.lisp) \
32+
$(wildcard blsdata/cancun/generalities/constraints_for_ct.lisp) \
33+
$(wildcard blsdata/cancun/generalities/id_increment_constraints.lisp) \
34+
$(wildcard blsdata/cancun/generalities/legal_transition_constraints.lisp) \
35+
$(wildcard blsdata/cancun/generalities/setting_acc_inputs.lisp) \
36+
$(wildcard blsdata/cancun/generalities/setting_ct_max.lisp) \
37+
$(wildcard blsdata/cancun/generalities/setting_index_max.lisp) \
38+
$(wildcard blsdata/cancun/generalities/setting_index.lisp) \
39+
$(wildcard blsdata/cancun/generalities/setting_is_first_input_and_is_second_input.lisp) \
40+
$(wildcard blsdata/cancun/generalities/setting_phase.lisp) \
41+
$(wildcard blsdata/cancun/generalities/setting_total_size.lisp) \
42+
$(wildcard blsdata/cancun/generalities/shorthands.lisp) \
43+
$(wildcard blsdata/cancun/lookups/*.lisp) \
44+
$(wildcard blsdata/cancun/specialized_constraints/*.lisp) \
45+
$(wildcard blsdata/cancun/top_level_flags_mint_mext_wtrv_wnon/*.lisp) \
46+
$(wildcard blsdata/cancun/utilities/*.lisp) \
47+
48+
BLS_PRAGUE := $(wildcard blsdata/cancun/*.lisp) \
49+
$(wildcard blsdata/cancun/generalities/constancy_conditions.lisp) \
50+
$(wildcard blsdata/cancun/generalities/constraining_address_sum.lisp) \
51+
$(wildcard blsdata/cancun/generalities/constraining_flag_sum.lisp) \
52+
$(wildcard blsdata/cancun/generalities/constraints_for_bls_stamp.lisp) \
53+
$(wildcard blsdata/cancun/generalities/constraints_for_ct.lisp) \
54+
$(wildcard blsdata/cancun/generalities/id_increment_constraints.lisp) \
55+
$(wildcard blsdata/cancun/generalities/legal_transition_constraints.lisp) \
56+
$(wildcard blsdata/cancun/generalities/setting_acc_inputs.lisp) \
57+
$(wildcard blsdata/cancun/generalities/setting_ct_max.lisp) \
58+
$(wildcard blsdata/cancun/generalities/setting_index_max.lisp) \
59+
$(wildcard blsdata/cancun/generalities/setting_index.lisp) \
60+
$(wildcard blsdata/cancun/generalities/setting_is_first_input_and_is_second_input.lisp) \
61+
$(wildcard blsdata/cancun/generalities/setting_phase.lisp) \
62+
$(wildcard blsdata/cancun/generalities/setting_total_size.lisp) \
63+
$(wildcard blsdata/cancun/generalities/shorthands.lisp) \
64+
$(wildcard blsdata/cancun/lookups/*.lisp) \
65+
$(wildcard blsdata/cancun/specialized_constraints/*.lisp) \
66+
$(wildcard blsdata/cancun/top_level_flags_mint_mext_wtrv_wnon/*.lisp) \
67+
$(wildcard blsdata/cancun/utilities/*.lisp) \
68+
2669
CONSTANTS := constants/constants.lisp
2770

2871
CONSTANTS_LONDON := constants/london/constants.lisp
@@ -67,7 +110,30 @@ OOB_LONDON := oob/london
67110

68111
OOB_SHANGHAI := oob/shanghai
69112

70-
OOB_CANCUN := oob/cancun
113+
OOB_CANCUN := $(wildcard oob/cancun/lookups/*.lisp) \
114+
$(wildcard oob/cancun/opcodes/*.lisp) \
115+
$(wildcard oob/cancun/precompiles/*.lisp) \
116+
$(wildcard oob/cancun/binarities.lisp) \
117+
$(wildcard oob/cancun/cancun_restriction.lisp) \
118+
$(wildcard oob/cancun/columns.lisp) \
119+
$(wildcard oob/cancun/constancies.lisp) \
120+
$(wildcard oob/cancun/constants.lisp) \
121+
$(wildcard oob/cancun/decoding.lisp) \
122+
$(wildcard oob/cancun/heartbeat.lisp) \
123+
$(wildcard oob/cancun/shorthands.lisp) \
124+
$(wildcard oob/cancun/specialized.lisp) \
125+
126+
OOB_PRAGUE := $(wildcard oob/cancun/lookups/*.lisp) \
127+
$(wildcard oob/cancun/opcodes/*.lisp) \
128+
$(wildcard oob/cancun/precompiles/*.lisp) \
129+
$(wildcard oob/cancun/binarities.lisp) \
130+
$(wildcard oob/cancun/columns.lisp) \
131+
$(wildcard oob/cancun/constancies.lisp) \
132+
$(wildcard oob/cancun/constants.lisp) \
133+
$(wildcard oob/cancun/decoding.lisp) \
134+
$(wildcard oob/cancun/heartbeat.lisp) \
135+
$(wildcard oob/cancun/shorthands.lisp) \
136+
$(wildcard oob/cancun/specialized.lisp) \
71137

72138
RLP_ADDR := rlpaddr
73139

@@ -93,9 +159,12 @@ TABLES_LONDON := reftables/*.lisp \
93159
reftables/london/inst_decoder.lisp
94160

95161
TABLES_CANCUN := reftables/*.lisp \
162+
reftables/cancun/bls_reftable.lisp \
96163
reftables/cancun/inst_decoder.lisp \
97164
reftables/cancun/power.lisp
98165

166+
# reftables/cancun/bls_reftable.lisp is only used in PRAGUE, but adding it in CANCUN already allows to do not duplicate OOB
167+
99168
TRM := trm
100169

101170
TXN_DATA_LONDON := txndata/london
@@ -174,6 +243,7 @@ ZKEVM_MODULES_CANCUN := ${ZKEVM_MODULES_COMMON} \
174243
${CONSTANTS_CANCUN} \
175244
${TABLES_CANCUN} \
176245
${BLOCKDATA_CANCUN} \
246+
${BLS_CANCUN} \
177247
${HUB_CANCUN} \
178248
${LOG_INFO_CANCUN} \
179249
${MMIO_CANCUN} \
@@ -187,11 +257,12 @@ ZKEVM_MODULES_PRAGUE := ${ZKEVM_MODULES_COMMON} \
187257
${CONSTANTS_PRAGUE} \
188258
${TABLES_CANCUN} \
189259
${BLOCKDATA_CANCUN} \
260+
${BLS_PRAGUE} \
190261
${HUB_CANCUN} \
191262
${LOG_INFO_CANCUN} \
192263
${MMIO_CANCUN} \
193264
${MXP_CANCUN} \
194-
${OOB_CANCUN} \
265+
${OOB_PRAGUE} \
195266
${RLP_TXN_CANCUN} \
196267
${RLP_UTILS_CANCUN} \
197268
${TXN_DATA_CANCUN}

blsdata/cancun/circuit_selectors.lisp

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
(module blsdata)
2+
3+
(defun (cs_G1MT_for_g1_msm)
4+
(* DATA_BLS_G1_MSM_FLAG IS_FIRST_INPUT MEXT_BIT))
5+
6+
(defun (cs_G2MT_for_g2_msm)
7+
(* DATA_BLS_G2_MSM_FLAG IS_FIRST_INPUT MEXT_BIT))
8+
9+
(defun (cs_G1MT_for_pairing_malformed)
10+
(* DATA_BLS_PAIRING_CHECK_FLAG IS_FIRST_INPUT MEXT_BIT))
11+
12+
(defun (cs_G2MT_for_pairing_malformed)
13+
(* DATA_BLS_PAIRING_CHECK_FLAG IS_SECOND_INPUT MEXT_BIT))
14+
15+
(defun (cs_G1MT_for_pairing_wellformed)
16+
(* DATA_BLS_PAIRING_CHECK_FLAG IS_FIRST_INPUT (- 1 NONTRIVIAL_POP_BIT) (- 1 IS_INFINITY) (wellformed_data)))
17+
18+
(defun (cs_G2MT_for_pairing_wellformed)
19+
(* DATA_BLS_PAIRING_CHECK_FLAG IS_SECOND_INPUT (- 1 NONTRIVIAL_POP_BIT) (- 1 IS_INFINITY) (wellformed_data)))
20+
21+
(defun (is_nontrivial_pairing_data_or_result)
22+
(+ (* DATA_BLS_PAIRING_CHECK_FLAG NONTRIVIAL_POP_BIT) RSLT_BLS_PAIRING_CHECK_FLAG))
23+
24+
;; Circuit selector column definitions
25+
26+
(defcomputedcolumn (CIRCUIT_SELECTOR_C1_MEMBERSHIP :binary@prove)
27+
(* MEXT_BIT DATA_BLS_G1_ADD_FLAG))
28+
29+
(defcomputedcolumn (CIRCUIT_SELECTOR_C2_MEMBERSHIP :binary@prove)
30+
(* MEXT_BIT DATA_BLS_G2_MSM_FLAG))
31+
32+
(defcomputedcolumn (CIRCUIT_SELECTOR_G1_MEMBERSHIP :binary@prove)
33+
(+ (cs_G1MT_for_g1_msm)
34+
(cs_G1MT_for_pairing_malformed)
35+
(cs_G1MT_for_pairing_wellformed)))
36+
37+
(defcomputedcolumn (CIRCUIT_SELECTOR_G2_MEMBERSHIP :binary@prove)
38+
(+ (cs_G2MT_for_g2_msm)
39+
(cs_G2MT_for_pairing_malformed)
40+
(cs_G2MT_for_pairing_wellformed)))
41+
42+
(defcomputedcolumn (CIRCUIT_SELECTOR_POINT_EVALUATION :binary@prove)
43+
(* WNON (is_point_evaluation)))
44+
45+
(defcomputedcolumn (CIRCUIT_SELECTOR_POINT_EVALUATION_FAILURE :binary@prove)
46+
(* MEXT (is_point_evaluation)))
47+
48+
(defcomputedcolumn (CIRCUIT_SELECTOR_BLS_G1_ADD :binary@prove)
49+
(* WNON (is_g1_add)))
50+
51+
(defcomputedcolumn (CIRCUIT_SELECTOR_BLS_G1_MSM :binary@prove)
52+
(* WNON (is_g1_msm)))
53+
54+
(defcomputedcolumn (CIRCUIT_SELECTOR_BLS_G2_ADD :binary@prove)
55+
(* WNON (is_g2_add)))
56+
57+
(defcomputedcolumn (CIRCUIT_SELECTOR_BLS_G2_MSM :binary@prove)
58+
(* WNON (is_g2_msm)))
59+
60+
(defcomputedcolumn (CIRCUIT_SELECTOR_BLS_PAIRING_CHECK :binary@prove)
61+
(* WNON (is_nontrivial_pairing_data_or_result)))
62+
63+
(defcomputedcolumn (CIRCUIT_SELECTOR_BLS_MAP_FP_TO_G1 :binary@prove)
64+
(* WNON (is_map_fp_to_g1)))
65+
66+
(defcomputedcolumn (CIRCUIT_SELECTOR_BLS_MAP_FP2_TO_G2 :binary@prove)
67+
(* WNON (is_map_fp2_to_g2)))

blsdata/cancun/columns.lisp

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
(module blsdata)
2+
3+
(defcolumns
4+
(STAMP :i32)
5+
(ID :i32)
6+
(TOTAL_SIZE :i16)
7+
(INDEX :i16)
8+
(INDEX_MAX :i16)
9+
(LIMB :i128)
10+
(PHASE :i16)
11+
(SUCCESS_BIT :binary@prove)
12+
13+
(CT :i4)
14+
(CT_MAX :i4)
15+
16+
(DATA_POINT_EVALUATION_FLAG :binary@prove)
17+
(DATA_BLS_G1_ADD_FLAG :binary@prove)
18+
(DATA_BLS_G1_MSM_FLAG :binary@prove)
19+
(DATA_BLS_G2_ADD_FLAG :binary@prove)
20+
(DATA_BLS_G2_MSM_FLAG :binary@prove)
21+
(DATA_BLS_PAIRING_CHECK_FLAG :binary@prove)
22+
(DATA_BLS_MAP_FP_TO_G1_FLAG :binary@prove)
23+
(DATA_BLS_MAP_FP2_TO_G2_FLAG :binary@prove)
24+
25+
(RSLT_POINT_EVALUATION_FLAG :binary@prove)
26+
(RSLT_BLS_G1_ADD_FLAG :binary@prove)
27+
(RSLT_BLS_G1_MSM_FLAG :binary@prove)
28+
(RSLT_BLS_G2_ADD_FLAG :binary@prove)
29+
(RSLT_BLS_G2_MSM_FLAG :binary@prove)
30+
(RSLT_BLS_PAIRING_CHECK_FLAG :binary@prove)
31+
(RSLT_BLS_MAP_FP_TO_G1_FLAG :binary@prove)
32+
(RSLT_BLS_MAP_FP2_TO_G2_FLAG :binary@prove)
33+
34+
(ACC_INPUTS :i16)
35+
(BYTE_DELTA :byte@prove)
36+
37+
(MALFORMED_DATA_INTERNAL_BIT :binary@prove)
38+
(MALFORMED_DATA_INTERNAL_ACC :binary@prove)
39+
(MALFORMED_DATA_INTERNAL_ACC_TOT :binary@prove)
40+
(MALFORMED_DATA_EXTERNAL_BIT :binary@prove)
41+
(MALFORMED_DATA_EXTERNAL_ACC :binary@prove)
42+
(MALFORMED_DATA_EXTERNAL_ACC_TOT :binary@prove)
43+
(WELLFORMED_DATA_TRIVIAL :binary@prove)
44+
(WELLFORMED_DATA_NONTRIVIAL :binary@prove)
45+
46+
(IS_FIRST_INPUT :binary@prove)
47+
(IS_SECOND_INPUT :binary@prove)
48+
(IS_INFINITY :binary@prove)
49+
(NONTRIVIAL_PAIR_OF_POINTS_BIT :binary@prove)
50+
(NONTRIVIAL_PAIR_OF_POINTS_ACC :binary@prove)
51+
52+
;; Circuit selector columns are defined using defcomputedcolumn in circuit_selectors.lisp
53+
54+
(WCP_FLAG :binary@prove)
55+
(WCP_ARG1_HI :i128)
56+
(WCP_ARG1_LO :i128)
57+
(WCP_ARG2_HI :i128)
58+
(WCP_ARG2_LO :i128)
59+
(WCP_RES :binary)
60+
(WCP_INST :byte :display :opcode)
61+
)
62+
63+
;; aliases
64+
(defalias
65+
MINT_BIT MALFORMED_DATA_INTERNAL_BIT
66+
MINT_ACC MALFORMED_DATA_INTERNAL_ACC
67+
MINT MALFORMED_DATA_INTERNAL_ACC_TOT
68+
MEXT_BIT MALFORMED_DATA_EXTERNAL_BIT
69+
MEXT_ACC MALFORMED_DATA_EXTERNAL_ACC
70+
MEXT MALFORMED_DATA_EXTERNAL_ACC_TOT
71+
WTRV WELLFORMED_DATA_TRIVIAL
72+
WNON WELLFORMED_DATA_NONTRIVIAL
73+
NONTRIVIAL_POP_BIT NONTRIVIAL_PAIR_OF_POINTS_BIT
74+
NONTRIVIAL_POP_ACC NONTRIVIAL_PAIR_OF_POINTS_ACC
75+
CS_POINT_EVALUATION CIRCUIT_SELECTOR_POINT_EVALUATION
76+
CS_POINT_EVALUATION_FAILURE CIRCUIT_SELECTOR_POINT_EVALUATION_FAILURE
77+
CS_C1_MEMBERSHIP CIRCUIT_SELECTOR_C1_MEMBERSHIP
78+
CS_G1_MEMBERSHIP CIRCUIT_SELECTOR_G1_MEMBERSHIP
79+
CS_C2_MEMBERSHIP CIRCUIT_SELECTOR_C2_MEMBERSHIP
80+
CS_G2_MEMBERSHIP CIRCUIT_SELECTOR_G2_MEMBERSHIP
81+
CS_BLS_PAIRING_CHECK CIRCUIT_SELECTOR_BLS_PAIRING_CHECK
82+
CS_BLS_G1_ADD CIRCUIT_SELECTOR_BLS_G1_ADD
83+
CS_BLS_G2_ADD CIRCUIT_SELECTOR_BLS_G2_ADD
84+
CS_BLS_G1_MSM CIRCUIT_SELECTOR_BLS_G1_MSM
85+
CS_BLS_G2_MSM CIRCUIT_SELECTOR_BLS_G2_MSM
86+
CS_BLS_MAP_FP_TO_G1 CIRCUIT_SELECTOR_BLS_MAP_FP_TO_G1
87+
CS_BLS_MAP_FP2_TO_G2 CIRCUIT_SELECTOR_BLS_MAP_FP2_TO_G2
88+
)
89+
90+

blsdata/cancun/constants.lisp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
(module blsdata)
2+
3+
(defconst
4+
POINT_EVALUATION_PRIME_HI 0x73eda753299d7d483339d80809a1d805
5+
POINT_EVALUATION_PRIME_LO 0x53BDA402FFFE5BFEFFFFFFFF00000001
6+
BLS_PRIME_3 0x00000000000000000000000000000000
7+
BLS_PRIME_2 0x1a0111ea397fe69a4b1ba7b6434bacd7
8+
BLS_PRIME_1 0x64774b84f38512bf6730d2a0f6b0f624
9+
BLS_PRIME_0 0x1eabfffeb153ffffb9feffffffffaaab
10+
11+
INDEX_MAX_DATA_POINT_EVALUATION 11
12+
INDEX_MAX_RSLT_POINT_EVALUATION 3
13+
INDEX_MAX_DATA_G1_ADD 15
14+
INDEX_MAX_RSLT_G1_ADD 7
15+
INDEX_MAX_DATA_G1_MSM_MIN 9
16+
INDEX_MAX_RSLT_G1_MSM 7
17+
INDEX_MAX_DATA_G2_ADD 31
18+
INDEX_MAX_RSLT_G2_ADD 15
19+
INDEX_MAX_DATA_G2_MSM_MIN 17
20+
INDEX_MAX_RSLT_G2_MSM 15
21+
INDEX_MAX_DATA_PAIRING_CHECK_MIN 23
22+
INDEX_MAX_RSLT_PAIRING_CHECK 1
23+
INDEX_MAX_DATA_MAP_FP_TO_G1 3
24+
INDEX_MAX_RSLT_MAP_FP_TO_G1 7
25+
INDEX_MAX_DATA_MAP_FP2_TO_G2 7
26+
INDEX_MAX_RSLT_MAP_FP2_TO_G2 15
27+
28+
CT_MAX_POINT_EVALUATION 11
29+
CT_MAX_SMALL_POINT 7
30+
CT_MAX_LARGE_POINT 15
31+
CT_MAX_SCALAR 1
32+
CT_MAX_MAP_FP_TO_G1 3
33+
CT_MAX_MAP_FP2_TO_G2 7
34+
)
35+
36+
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
(module blsdata)
2+
3+
(defconstraint cancun-restriction ()
4+
(eq! (flag_sum) (is_point_evaluation)))
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
(module blsdata)
2+
3+
(defconstraint stamp-constancy ()
4+
(begin (stamp-constancy STAMP ID)
5+
(stamp-constancy STAMP SUCCESS_BIT)
6+
(stamp-constancy STAMP MINT)
7+
(stamp-constancy STAMP MEXT)
8+
(stamp-constancy STAMP WTRV)
9+
(stamp-constancy STAMP WNON)))
10+
11+
(defconstraint counter-constancy ()
12+
(begin (counter-constancy INDEX PHASE) ;; NOTE: PHASE and INDEX_MAX are said to be index-constant
13+
(counter-constancy INDEX INDEX_MAX)
14+
(counter-constancy CT CT_MAX)
15+
(counter-constancy CT IS_INFINITY)
16+
(counter-constancy CT ACC_INPUTS)
17+
(counter-constancy CT NONTRIVIAL_POP_ACC)
18+
(counter-constancy CT MEXT_BIT)
19+
(counter-constancy CT MEXT_ACC)))
20+
21+
(defconstraint pair-of-inputs-constancy ()
22+
(if-not-zero ACC_INPUTS
23+
(if (will-remain-constant! ACC_INPUTS)
24+
(will-remain-constant! NONTRIVIAL_POP_BIT))))
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
(module blsdata)
2+
3+
(defun (address_sum)
4+
(+ (* 10 (is_point_evaluation))
5+
(* 11 (is_g1_add))
6+
(* 12 (is_g1_msm))
7+
(* 13 (is_g2_add))
8+
(* 14 (is_g2_msm))
9+
(* 15 (is_pairing_check))
10+
(* 16 (is_map_fp_to_g1))
11+
(* 17 (is_map_fp2_to_g2))))
12+
13+
(defconstraint stamp-constancy ()
14+
(stamp-constancy STAMP (address_sum)))

0 commit comments

Comments
 (0)