@solarity/zkit 0.2.5 → 0.3.0-rc.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +8 -8
- package/dist/core/CircuitZKit.d.ts +24 -22
- package/dist/core/CircuitZKit.d.ts.map +1 -1
- package/dist/core/CircuitZKit.js +30 -44
- package/dist/core/CircuitZKit.js.map +1 -1
- package/dist/core/protocols/AbstractImplementer.d.ts +15 -0
- package/dist/core/protocols/AbstractImplementer.d.ts.map +1 -0
- package/dist/core/protocols/AbstractImplementer.js +36 -0
- package/dist/core/protocols/AbstractImplementer.js.map +1 -0
- package/dist/core/protocols/Groth16Implementer.d.ts +10 -0
- package/dist/core/protocols/Groth16Implementer.d.ts.map +1 -0
- package/dist/core/protocols/Groth16Implementer.js +50 -0
- package/dist/core/protocols/Groth16Implementer.js.map +1 -0
- package/dist/core/protocols/PlonkImplementer.d.ts +10 -0
- package/dist/core/protocols/PlonkImplementer.d.ts.map +1 -0
- package/dist/core/protocols/PlonkImplementer.js +51 -0
- package/dist/core/protocols/PlonkImplementer.js.map +1 -0
- package/dist/core/protocols/index.d.ts +4 -0
- package/dist/core/protocols/index.d.ts.map +1 -0
- package/dist/core/protocols/index.js +10 -0
- package/dist/core/protocols/index.js.map +1 -0
- package/dist/core/templates/verifier_groth16.vy.ejs +118 -0
- package/dist/core/templates/verifier_plonk.sol.ejs +681 -0
- package/dist/core/templates/verifier_plonk.vy.ejs +650 -0
- package/dist/index.d.ts +4 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +6 -1
- package/dist/index.js.map +1 -1
- package/dist/types/circuit-zkit.d.ts +1 -34
- package/dist/types/circuit-zkit.d.ts.map +1 -1
- package/dist/types/proof-utils.d.ts +7 -0
- package/dist/types/proof-utils.d.ts.map +1 -0
- package/dist/types/proof-utils.js +3 -0
- package/dist/types/proof-utils.js.map +1 -0
- package/dist/types/protocols/groth16.d.ts +28 -0
- package/dist/types/protocols/groth16.d.ts.map +1 -0
- package/dist/types/protocols/groth16.js +3 -0
- package/dist/types/protocols/groth16.js.map +1 -0
- package/dist/types/protocols/index.d.ts +31 -0
- package/dist/types/protocols/index.d.ts.map +1 -0
- package/dist/types/protocols/index.js +19 -0
- package/dist/types/protocols/index.js.map +1 -0
- package/dist/types/protocols/plonk.d.ts +26 -0
- package/dist/types/protocols/plonk.d.ts.map +1 -0
- package/dist/types/{types.js → protocols/plonk.js} +1 -1
- package/dist/types/protocols/plonk.js.map +1 -0
- package/package.json +7 -6
- package/src/core/CircuitZKit.ts +42 -63
- package/src/core/protocols/AbstractImplementer.ts +67 -0
- package/src/core/protocols/Groth16Implementer.ts +29 -0
- package/src/core/protocols/PlonkImplementer.ts +32 -0
- package/src/core/protocols/index.ts +3 -0
- package/src/core/templates/verifier_groth16.vy.ejs +118 -0
- package/src/core/templates/verifier_plonk.sol.ejs +681 -0
- package/src/core/templates/verifier_plonk.vy.ejs +650 -0
- package/src/index.ts +5 -1
- package/src/types/circuit-zkit.ts +1 -31
- package/src/types/proof-utils.ts +9 -0
- package/src/types/protocols/groth16.ts +21 -0
- package/src/types/protocols/index.ts +49 -0
- package/src/types/protocols/plonk.ts +28 -0
- package/dist/config/config.d.ts +0 -27
- package/dist/config/config.d.ts.map +0 -1
- package/dist/config/config.js +0 -19
- package/dist/config/config.js.map +0 -1
- package/dist/core/CircomZKit.d.ts +0 -39
- package/dist/core/CircomZKit.d.ts.map +0 -1
- package/dist/core/CircomZKit.js +0 -94
- package/dist/core/CircomZKit.js.map +0 -1
- package/dist/core/ManagerZKit.d.ts +0 -97
- package/dist/core/ManagerZKit.d.ts.map +0 -1
- package/dist/core/ManagerZKit.js +0 -222
- package/dist/core/ManagerZKit.js.map +0 -1
- package/dist/types/types.d.ts +0 -46
- package/dist/types/types.d.ts.map +0 -1
- package/dist/types/types.js.map +0 -1
- package/dist/utils/utils.d.ts +0 -18
- package/dist/utils/utils.d.ts.map +0 -1
- package/dist/utils/utils.js +0 -58
- package/dist/utils/utils.js.map +0 -1
|
@@ -0,0 +1,650 @@
|
|
|
1
|
+
# pragma version ~=0.4.0
|
|
2
|
+
|
|
3
|
+
# AUTOGENERATED FILE BY HARDHAT-ZKIT. DO NOT EDIT.
|
|
4
|
+
|
|
5
|
+
# Omega
|
|
6
|
+
W1: constant(uint256) = <%=w%>
|
|
7
|
+
# Scalar field size
|
|
8
|
+
BASE_FIELD_SIZE: constant(uint256) = 21888242871839275222246405745257275088548364400416034343698204186575808495617
|
|
9
|
+
# Base field size
|
|
10
|
+
QF: constant(uint256) = 21888242871839275222246405745257275088696311157297823662689037894645226208583
|
|
11
|
+
|
|
12
|
+
# [1]_1
|
|
13
|
+
G1_X: constant(uint256) = 1
|
|
14
|
+
G1_Y: constant(uint256) = 2
|
|
15
|
+
# [1]_2
|
|
16
|
+
G2_X1: constant(uint256) = 10857046999023057135944570762232829481370756359578518086990519993285655852781
|
|
17
|
+
G2_X2: constant(uint256) = 11559732032986387107991004021392285783925812861821192530917403151452391805634
|
|
18
|
+
G2_Y1: constant(uint256) = 8495653923123431417604973247489272438418190587263600148770280649306958101930
|
|
19
|
+
G2_Y2: constant(uint256) = 4082367875863433681332203403145435568316851327593401208105741076214120093531
|
|
20
|
+
|
|
21
|
+
# Verification Key data
|
|
22
|
+
N: constant(uint256) = <%=2**power%>
|
|
23
|
+
N_PUBLIC: constant(uint256) = <%=nPublic%>
|
|
24
|
+
N_LAGRANGE: constant(uint256) = <%=nPublic%>
|
|
25
|
+
|
|
26
|
+
QM_X: constant(uint256) = <%=Qm[0]%>
|
|
27
|
+
QM_Y: constant(uint256) = <%=Qm[0] == "0" ? "0" : Qm[1]%>
|
|
28
|
+
QL_X: constant(uint256) = <%=Ql[0]%>
|
|
29
|
+
QL_Y: constant(uint256) = <%=Ql[0] == "0" ? "0" : Ql[1]%>
|
|
30
|
+
QR_X: constant(uint256) = <%=Qr[0]%>
|
|
31
|
+
QR_Y: constant(uint256) = <%=Qr[0] == "0" ? "0" : Qr[1]%>
|
|
32
|
+
QO_X: constant(uint256) = <%=Qo[0]%>
|
|
33
|
+
QO_Y: constant(uint256) = <%=Qo[0] == "0" ? "0" : Qo[1]%>
|
|
34
|
+
QC_X: constant(uint256) = <%=Qc[0]%>
|
|
35
|
+
QC_Y: constant(uint256) = <%=Qc[0] == "0" ? "0" : Qc[1]%>
|
|
36
|
+
S1_X: constant(uint256) = <%=S1[0]%>
|
|
37
|
+
S1_Y: constant(uint256) = <%=S1[0] == "0" ? "0" : S1[1]%>
|
|
38
|
+
S2_X: constant(uint256) = <%=S2[0]%>
|
|
39
|
+
S2_Y: constant(uint256) = <%=S2[0] == "0" ? "0" : S2[1]%>
|
|
40
|
+
S3_X: constant(uint256) = <%=S3[0]%>
|
|
41
|
+
S3_Y: constant(uint256) = <%=S3[0] == "0" ? "0" : S3[1]%>
|
|
42
|
+
K1: constant(uint256) = <%=k1%>
|
|
43
|
+
K2: constant(uint256) = <%=k2%>
|
|
44
|
+
X2_X1: constant(uint256) = <%=X_2[0][0]%>
|
|
45
|
+
X2_X2: constant(uint256) = <%=X_2[0][1]%>
|
|
46
|
+
X2_Y1: constant(uint256) = <%=X_2[1][0]%>
|
|
47
|
+
X2_Y2: constant(uint256) = <%=X_2[1][1]%>
|
|
48
|
+
|
|
49
|
+
# Proof values offsets
|
|
50
|
+
# Byte offset of every parameter of the proof array
|
|
51
|
+
# Polynomial commitments
|
|
52
|
+
P_A: constant(uint256) = 0
|
|
53
|
+
P_B: constant(uint256) = 2
|
|
54
|
+
P_C: constant(uint256) = 4
|
|
55
|
+
P_Z: constant(uint256) = 6
|
|
56
|
+
P_T1: constant(uint256) = 8
|
|
57
|
+
P_T2: constant(uint256) = 10
|
|
58
|
+
P_T3: constant(uint256) = 12
|
|
59
|
+
P_WX_I: constant(uint256) = 14
|
|
60
|
+
P_WX_IW: constant(uint256) = 16
|
|
61
|
+
|
|
62
|
+
# Opening evaluations
|
|
63
|
+
P_EVAL_A: constant(uint256) = 18
|
|
64
|
+
P_EVAL_B: constant(uint256) = 19
|
|
65
|
+
P_EVAL_C: constant(uint256) = 20
|
|
66
|
+
P_EVAL_S1: constant(uint256) = 21
|
|
67
|
+
P_EVAL_S2: constant(uint256) = 22
|
|
68
|
+
P_EVAL_ZW: constant(uint256) = 23
|
|
69
|
+
|
|
70
|
+
# Memory data
|
|
71
|
+
# Challenges
|
|
72
|
+
P_ALPHA: constant(uint256) = 0
|
|
73
|
+
P_BETA: constant(uint256) = 1
|
|
74
|
+
P_GAMMA: constant(uint256) = 2
|
|
75
|
+
P_XI: constant(uint256) = 3
|
|
76
|
+
P_XIN: constant(uint256) = 4
|
|
77
|
+
P_BETA_XI: constant(uint256) = 5
|
|
78
|
+
P_V1: constant(uint256) = 6
|
|
79
|
+
P_V2: constant(uint256) = 7
|
|
80
|
+
P_V3: constant(uint256) = 8
|
|
81
|
+
P_V4: constant(uint256) = 9
|
|
82
|
+
P_V5: constant(uint256) = 10
|
|
83
|
+
P_U: constant(uint256) = 11
|
|
84
|
+
|
|
85
|
+
P_PI: constant(uint256) = 12
|
|
86
|
+
P_EVAL_R0: constant(uint256) = 13
|
|
87
|
+
P_D: constant(uint256) = 14
|
|
88
|
+
P_F: constant(uint256) = 16
|
|
89
|
+
P_E: constant(uint256) = 18
|
|
90
|
+
P_TMP: constant(uint256) = 20
|
|
91
|
+
P_ALPHA2: constant(uint256) = 22
|
|
92
|
+
P_ZH: constant(uint256) = 23
|
|
93
|
+
P_ZH_INV: constant(uint256) = 24
|
|
94
|
+
|
|
95
|
+
P_EVAL_L1: constant(uint256) = 25
|
|
96
|
+
|
|
97
|
+
P_TOTAL_SIZE: constant(uint256) = <%=25+nPublic%>
|
|
98
|
+
|
|
99
|
+
EC_ADD_PRECOMPILED_ADDRESS: constant(address) = 0x0000000000000000000000000000000000000006
|
|
100
|
+
EC_MUL_PRECOMPILED_ADDRESS: constant(address) = 0x0000000000000000000000000000000000000007
|
|
101
|
+
EC_PAIRING_PRECOMPILED_ADDRESS: constant(address) = 0x0000000000000000000000000000000000000008
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
@pure
|
|
105
|
+
@external
|
|
106
|
+
def verifyProof(proofArr: uint256[24], publicSignals: uint256[<%=nPublic%>]) -> bool:
|
|
107
|
+
if not self._checkInput(proofArr):
|
|
108
|
+
return False
|
|
109
|
+
|
|
110
|
+
p: uint256[P_TOTAL_SIZE] = self._calculateChallenges(proofArr, publicSignals)
|
|
111
|
+
|
|
112
|
+
p = self._calculateLagrange(p)
|
|
113
|
+
|
|
114
|
+
p[P_PI] = self._calculatePI(p, publicSignals)
|
|
115
|
+
p[P_EVAL_R0] = self._calculateR0(p, proofArr)
|
|
116
|
+
|
|
117
|
+
success: bool = True
|
|
118
|
+
tmpPoint: uint256[2] = [0, 0]
|
|
119
|
+
|
|
120
|
+
success, tmpPoint = self._calculateD(p, proofArr)
|
|
121
|
+
if not success:
|
|
122
|
+
return False
|
|
123
|
+
|
|
124
|
+
p[P_D] = tmpPoint[0]
|
|
125
|
+
p[P_D + 1] = tmpPoint[1]
|
|
126
|
+
|
|
127
|
+
success, tmpPoint = self._calculateF(p, proofArr)
|
|
128
|
+
if not success:
|
|
129
|
+
return False
|
|
130
|
+
|
|
131
|
+
p[P_F] = tmpPoint[0]
|
|
132
|
+
p[P_F + 1] = tmpPoint[1]
|
|
133
|
+
|
|
134
|
+
success, tmpPoint = self._calculateE(p, proofArr)
|
|
135
|
+
if not success:
|
|
136
|
+
return False
|
|
137
|
+
|
|
138
|
+
p[P_E] = tmpPoint[0]
|
|
139
|
+
p[P_E + 1] = tmpPoint[1]
|
|
140
|
+
|
|
141
|
+
return self._checkPairing(p, proofArr)
|
|
142
|
+
|
|
143
|
+
|
|
144
|
+
@pure
|
|
145
|
+
@internal
|
|
146
|
+
def _ecadd(a: uint256[2], b: uint256[2]) -> (bool, uint256[2]):
|
|
147
|
+
success: bool = True
|
|
148
|
+
response: Bytes[64] = b""
|
|
149
|
+
success, response = raw_call(
|
|
150
|
+
EC_ADD_PRECOMPILED_ADDRESS,
|
|
151
|
+
abi_encode(a, b),
|
|
152
|
+
max_outsize=64,
|
|
153
|
+
is_static_call=True,
|
|
154
|
+
revert_on_failure=False
|
|
155
|
+
)
|
|
156
|
+
|
|
157
|
+
if not success or len(response) != 64:
|
|
158
|
+
return (False, [0, 0])
|
|
159
|
+
|
|
160
|
+
x: uint256 = convert(slice(response, 0, 32), uint256)
|
|
161
|
+
y: uint256 = convert(slice(response, 32, 32), uint256)
|
|
162
|
+
|
|
163
|
+
return (True, [x, y])
|
|
164
|
+
|
|
165
|
+
|
|
166
|
+
@pure
|
|
167
|
+
@internal
|
|
168
|
+
def _ecmul(p: uint256[2], s: uint256) -> (bool, uint256[2]):
|
|
169
|
+
success: bool = True
|
|
170
|
+
response: Bytes[64] = b""
|
|
171
|
+
success, response = raw_call(
|
|
172
|
+
EC_MUL_PRECOMPILED_ADDRESS,
|
|
173
|
+
abi_encode(p, s),
|
|
174
|
+
max_outsize=64,
|
|
175
|
+
is_static_call=True,
|
|
176
|
+
revert_on_failure=False
|
|
177
|
+
)
|
|
178
|
+
|
|
179
|
+
if not success or len(response) != 64:
|
|
180
|
+
return (False, [0, 0])
|
|
181
|
+
|
|
182
|
+
x: uint256 = convert(slice(response, 0, 32), uint256)
|
|
183
|
+
y: uint256 = convert(slice(response, 32, 32), uint256)
|
|
184
|
+
|
|
185
|
+
return (True, [x, y])
|
|
186
|
+
|
|
187
|
+
|
|
188
|
+
@pure
|
|
189
|
+
@internal
|
|
190
|
+
def _inverse(a: uint256, q: uint256) -> uint256:
|
|
191
|
+
t: int256 = 0
|
|
192
|
+
newt: int256 = 1
|
|
193
|
+
r: uint256 = q
|
|
194
|
+
newr: uint256 = a
|
|
195
|
+
quotient: uint256 = 0
|
|
196
|
+
aux: int256 = 0
|
|
197
|
+
|
|
198
|
+
for _: uint256 in range(256):
|
|
199
|
+
if newr == 0:
|
|
200
|
+
break
|
|
201
|
+
|
|
202
|
+
quotient = r // newr
|
|
203
|
+
aux = t - convert(quotient, int256) * newt
|
|
204
|
+
t = newt
|
|
205
|
+
newt = aux
|
|
206
|
+
|
|
207
|
+
aux = convert(r - quotient * newr, int256)
|
|
208
|
+
r = newr
|
|
209
|
+
newr = convert(aux, uint256)
|
|
210
|
+
|
|
211
|
+
assert r <= 1, "Inverse does not exist."
|
|
212
|
+
|
|
213
|
+
if t < 0:
|
|
214
|
+
t += convert(q, int256)
|
|
215
|
+
|
|
216
|
+
return convert(t, uint256)
|
|
217
|
+
|
|
218
|
+
|
|
219
|
+
@pure
|
|
220
|
+
@internal
|
|
221
|
+
def _inverseArray(pVals: uint256[<%=nPublic+1%>]) -> uint256[<%=nPublic+1%>]:
|
|
222
|
+
acc: uint256 = pVals[0]
|
|
223
|
+
inverses: uint256[<%=nPublic+1%>] = empty(uint256[<%=nPublic+1%>])
|
|
224
|
+
pAux: uint256[<%=nPublic+1%>] = empty(uint256[<%=nPublic+1%>])
|
|
225
|
+
|
|
226
|
+
for i: uint256 in range(1, <%=nPublic+1%>):
|
|
227
|
+
pAux[i] = acc
|
|
228
|
+
acc = uint256_mulmod(acc, pVals[i], BASE_FIELD_SIZE)
|
|
229
|
+
|
|
230
|
+
inv_total: uint256 = self._inverse(acc, BASE_FIELD_SIZE)
|
|
231
|
+
|
|
232
|
+
for i: uint256 in range(<%=nPublic%>):
|
|
233
|
+
inverses[<%=nPublic%> - i] = uint256_mulmod(inv_total, pAux[<%=nPublic%> - i], BASE_FIELD_SIZE)
|
|
234
|
+
inv_total = uint256_mulmod(inv_total, pVals[<%=nPublic%> - i], BASE_FIELD_SIZE)
|
|
235
|
+
|
|
236
|
+
inverses[0] = inv_total
|
|
237
|
+
|
|
238
|
+
return inverses
|
|
239
|
+
|
|
240
|
+
|
|
241
|
+
@pure
|
|
242
|
+
@internal
|
|
243
|
+
def _checkInput(proof: uint256[24]) -> bool:
|
|
244
|
+
if proof[P_EVAL_A] >= BASE_FIELD_SIZE:
|
|
245
|
+
return False
|
|
246
|
+
|
|
247
|
+
if proof[P_EVAL_B] >= BASE_FIELD_SIZE:
|
|
248
|
+
return False
|
|
249
|
+
|
|
250
|
+
if proof[P_EVAL_C] >= BASE_FIELD_SIZE:
|
|
251
|
+
return False
|
|
252
|
+
|
|
253
|
+
if proof[P_EVAL_S1] >= BASE_FIELD_SIZE:
|
|
254
|
+
return False
|
|
255
|
+
|
|
256
|
+
if proof[P_EVAL_S2] >= BASE_FIELD_SIZE:
|
|
257
|
+
return False
|
|
258
|
+
|
|
259
|
+
if proof[P_EVAL_ZW] >= BASE_FIELD_SIZE:
|
|
260
|
+
return False
|
|
261
|
+
|
|
262
|
+
return True
|
|
263
|
+
|
|
264
|
+
|
|
265
|
+
@pure
|
|
266
|
+
@internal
|
|
267
|
+
def _calculateChallenges(proof: uint256[24], pubSignals: uint256[<%=nPublic%>]) -> uint256[P_TOTAL_SIZE]:
|
|
268
|
+
mIn<%=22+nPublic%>: uint256[<%=22+nPublic%>] = [
|
|
269
|
+
QM_X, QM_Y, QL_X, QL_Y, QR_X, QR_Y, QO_X, QO_Y, QC_X, QC_Y, S1_X, S1_Y, S2_X, S2_Y, S3_X, S3_Y,
|
|
270
|
+
<% for (let i = 0; i < nPublic; i++) { %>pubSignals[<%=i%>], <% } %>
|
|
271
|
+
proof[P_A], proof[P_A + 1], proof[P_B], proof[P_B + 1], proof[P_C], proof[P_C + 1],
|
|
272
|
+
]
|
|
273
|
+
|
|
274
|
+
beta: uint256 = convert(keccak256(abi_encode(mIn<%=22+nPublic%>)), uint256) % BASE_FIELD_SIZE
|
|
275
|
+
|
|
276
|
+
p: uint256[P_TOTAL_SIZE] = empty(uint256[P_TOTAL_SIZE])
|
|
277
|
+
p[P_BETA] = beta
|
|
278
|
+
|
|
279
|
+
# challenges.gamma
|
|
280
|
+
p[P_GAMMA] = convert(keccak256(abi_encode(p[P_BETA])), uint256) % BASE_FIELD_SIZE
|
|
281
|
+
|
|
282
|
+
# challenges.alpha
|
|
283
|
+
mIn4: uint256[4] = [beta, p[P_GAMMA], proof[P_Z], proof[P_Z + 1]]
|
|
284
|
+
aux: uint256 = convert(keccak256(abi_encode(mIn4)), uint256) % BASE_FIELD_SIZE
|
|
285
|
+
p[P_ALPHA] = aux
|
|
286
|
+
p[P_ALPHA2] = uint256_mulmod(aux, aux, BASE_FIELD_SIZE)
|
|
287
|
+
|
|
288
|
+
# challenges.xi
|
|
289
|
+
mIn7: uint256[7] = [aux, proof[P_T1], proof[P_T1 + 1], proof[P_T2], proof[P_T2 + 1], proof[P_T3], proof[P_T3 + 1]]
|
|
290
|
+
aux = convert(keccak256(abi_encode(mIn7)), uint256) % BASE_FIELD_SIZE
|
|
291
|
+
p[P_XI] = aux
|
|
292
|
+
|
|
293
|
+
# challenges.v
|
|
294
|
+
mIn7 = [
|
|
295
|
+
aux, proof[P_EVAL_A], proof[P_EVAL_B], proof[P_EVAL_C], proof[P_EVAL_S1], proof[P_EVAL_S2], proof[P_EVAL_ZW]
|
|
296
|
+
]
|
|
297
|
+
v1: uint256 = convert(keccak256(abi_encode(mIn7)), uint256) % BASE_FIELD_SIZE
|
|
298
|
+
p[P_V1] = v1
|
|
299
|
+
|
|
300
|
+
# challenges.v^2, challenges.v^3, challenges.v^4, challenges.v^5
|
|
301
|
+
p[P_V2] = uint256_mulmod(v1, v1, BASE_FIELD_SIZE)
|
|
302
|
+
p[P_V3] = uint256_mulmod(p[P_V2], v1, BASE_FIELD_SIZE)
|
|
303
|
+
p[P_V4] = uint256_mulmod(p[P_V3], v1, BASE_FIELD_SIZE)
|
|
304
|
+
p[P_V5] = uint256_mulmod(p[P_V4], v1, BASE_FIELD_SIZE)
|
|
305
|
+
|
|
306
|
+
# challenges.beta * challenges.xi
|
|
307
|
+
p[P_BETA_XI] = uint256_mulmod(beta, aux, BASE_FIELD_SIZE)
|
|
308
|
+
|
|
309
|
+
# challenges.xi^n
|
|
310
|
+
<%for (let i=0; i<power;i++) {%>
|
|
311
|
+
aux = uint256_mulmod(aux, aux, BASE_FIELD_SIZE)<% } %>
|
|
312
|
+
p[P_XIN] = aux
|
|
313
|
+
|
|
314
|
+
# Zh
|
|
315
|
+
aux = (aux - 1 + BASE_FIELD_SIZE) % BASE_FIELD_SIZE
|
|
316
|
+
p[P_ZH] = aux
|
|
317
|
+
p[P_ZH_INV] = aux
|
|
318
|
+
|
|
319
|
+
# challenges.u
|
|
320
|
+
mIn4 = [proof[P_WX_I], proof[P_WX_I + 1], proof[P_WX_IW], proof[P_WX_IW + 1]]
|
|
321
|
+
p[P_U] = convert(keccak256(abi_encode(mIn4)), uint256) % BASE_FIELD_SIZE
|
|
322
|
+
|
|
323
|
+
return p
|
|
324
|
+
|
|
325
|
+
|
|
326
|
+
@pure
|
|
327
|
+
@internal
|
|
328
|
+
def _evaluateLagrange(w: uint256, xi: uint256) -> uint256:
|
|
329
|
+
return uint256_mulmod(N, uint256_addmod(xi, BASE_FIELD_SIZE - w, BASE_FIELD_SIZE), BASE_FIELD_SIZE)
|
|
330
|
+
|
|
331
|
+
|
|
332
|
+
@pure
|
|
333
|
+
@internal
|
|
334
|
+
def _calculateLagrange(p: uint256[P_TOTAL_SIZE]) -> uint256[P_TOTAL_SIZE]:
|
|
335
|
+
w: uint256 = 1
|
|
336
|
+
|
|
337
|
+
for i: uint256 in range(1, <%=nPublic+1%>):
|
|
338
|
+
p[P_EVAL_L1 + (i - 1)] = self._evaluateLagrange(w, p[P_XI])
|
|
339
|
+
w = uint256_mulmod(w, W1, BASE_FIELD_SIZE)
|
|
340
|
+
|
|
341
|
+
pointsToInverse: uint256[<%=nPublic+1%>] = empty(uint256[<%=nPublic+1%>])
|
|
342
|
+
for i: uint256 in range(<%=nPublic+1%>):
|
|
343
|
+
pointsToInverse[i] = p[P_ZH_INV + i]
|
|
344
|
+
|
|
345
|
+
inverses: uint256[<%=nPublic+1%>] = self._inverseArray(pointsToInverse)
|
|
346
|
+
|
|
347
|
+
for i: uint256 in range(<%=nPublic+1%>):
|
|
348
|
+
p[P_ZH_INV + i] = inverses[i]
|
|
349
|
+
|
|
350
|
+
zh: uint256 = p[P_ZH]
|
|
351
|
+
w = 1
|
|
352
|
+
|
|
353
|
+
for i: uint256 in range(1, <%=nPublic+1%>):
|
|
354
|
+
p[P_EVAL_L1 + (i - 1)] = uint256_mulmod(
|
|
355
|
+
uint256_mulmod(p[P_EVAL_L1 + (i - 1)], zh, BASE_FIELD_SIZE),
|
|
356
|
+
w,
|
|
357
|
+
BASE_FIELD_SIZE
|
|
358
|
+
)
|
|
359
|
+
w = uint256_mulmod(w, W1, BASE_FIELD_SIZE)
|
|
360
|
+
|
|
361
|
+
return p
|
|
362
|
+
|
|
363
|
+
|
|
364
|
+
@pure
|
|
365
|
+
@internal
|
|
366
|
+
def _calculatePI(p: uint256[P_TOTAL_SIZE], pPub: uint256[<%=nPublic%>]) -> uint256:
|
|
367
|
+
pl: uint256 = 0
|
|
368
|
+
|
|
369
|
+
for i: uint256 in range(<%=nPublic%>):
|
|
370
|
+
pl = uint256_addmod(
|
|
371
|
+
pl,
|
|
372
|
+
BASE_FIELD_SIZE - uint256_mulmod(p[P_EVAL_L1 + i], pPub[i], BASE_FIELD_SIZE),
|
|
373
|
+
BASE_FIELD_SIZE
|
|
374
|
+
)
|
|
375
|
+
|
|
376
|
+
return pl
|
|
377
|
+
|
|
378
|
+
|
|
379
|
+
@pure
|
|
380
|
+
@internal
|
|
381
|
+
def _calculateR0(p: uint256[P_TOTAL_SIZE], proof: uint256[24]) -> uint256:
|
|
382
|
+
e1: uint256 = p[P_PI]
|
|
383
|
+
|
|
384
|
+
e2: uint256 = uint256_mulmod(p[P_EVAL_L1], p[P_ALPHA2], BASE_FIELD_SIZE)
|
|
385
|
+
|
|
386
|
+
e3a: uint256 = uint256_addmod(
|
|
387
|
+
proof[P_EVAL_A],
|
|
388
|
+
uint256_mulmod(p[P_BETA], proof[P_EVAL_S1], BASE_FIELD_SIZE),
|
|
389
|
+
BASE_FIELD_SIZE
|
|
390
|
+
)
|
|
391
|
+
e3a = uint256_addmod(e3a, p[P_GAMMA], BASE_FIELD_SIZE)
|
|
392
|
+
|
|
393
|
+
e3b: uint256 = uint256_addmod(
|
|
394
|
+
proof[P_EVAL_B],
|
|
395
|
+
uint256_mulmod(p[P_BETA], proof[P_EVAL_S2], BASE_FIELD_SIZE),
|
|
396
|
+
BASE_FIELD_SIZE
|
|
397
|
+
)
|
|
398
|
+
e3b = uint256_addmod(e3b, p[P_GAMMA], BASE_FIELD_SIZE)
|
|
399
|
+
|
|
400
|
+
e3c: uint256 = uint256_addmod(proof[P_EVAL_C], p[P_GAMMA], BASE_FIELD_SIZE)
|
|
401
|
+
|
|
402
|
+
e3: uint256 = uint256_mulmod(uint256_mulmod(e3a, e3b, BASE_FIELD_SIZE), e3c, BASE_FIELD_SIZE)
|
|
403
|
+
e3 = uint256_mulmod(e3, proof[P_EVAL_ZW], BASE_FIELD_SIZE)
|
|
404
|
+
e3 = uint256_mulmod(e3, p[P_ALPHA], BASE_FIELD_SIZE)
|
|
405
|
+
|
|
406
|
+
r0: uint256 = uint256_addmod(e1, BASE_FIELD_SIZE - e2, BASE_FIELD_SIZE)
|
|
407
|
+
return uint256_addmod(r0, BASE_FIELD_SIZE - e3, BASE_FIELD_SIZE)
|
|
408
|
+
|
|
409
|
+
|
|
410
|
+
@pure
|
|
411
|
+
@internal
|
|
412
|
+
def _g1_mulAccC(pR: uint256[2], point: uint256[2], s: uint256) -> (bool, uint256[2]):
|
|
413
|
+
success: bool = True
|
|
414
|
+
mP: uint256[2] = [0, 0]
|
|
415
|
+
|
|
416
|
+
success, mP = self._ecmul(point, s)
|
|
417
|
+
|
|
418
|
+
return self._ecadd(mP, pR)
|
|
419
|
+
|
|
420
|
+
|
|
421
|
+
@pure
|
|
422
|
+
@internal
|
|
423
|
+
def _calculateD(p: uint256[P_TOTAL_SIZE], proof: uint256[24]) -> (bool, uint256[2]):
|
|
424
|
+
success: bool = True
|
|
425
|
+
pd: uint256[2] = [QC_X, QC_Y]
|
|
426
|
+
|
|
427
|
+
success, pd = self._g1_mulAccC(pd, [QM_X, QM_Y], uint256_mulmod(proof[P_EVAL_A], proof[P_EVAL_B], BASE_FIELD_SIZE))
|
|
428
|
+
if not success:
|
|
429
|
+
return (False, [0, 0])
|
|
430
|
+
|
|
431
|
+
success, pd = self._g1_mulAccC(pd, [QL_X, QL_Y], proof[P_EVAL_A])
|
|
432
|
+
if not success:
|
|
433
|
+
return (False, [0, 0])
|
|
434
|
+
|
|
435
|
+
success, pd = self._g1_mulAccC(pd, [QR_X, QR_Y], proof[P_EVAL_B])
|
|
436
|
+
if not success:
|
|
437
|
+
return (False, [0, 0])
|
|
438
|
+
|
|
439
|
+
success, pd = self._g1_mulAccC(pd, [QO_X, QO_Y], proof[P_EVAL_C])
|
|
440
|
+
if not success:
|
|
441
|
+
return (False, [0, 0])
|
|
442
|
+
|
|
443
|
+
val1: uint256 = uint256_addmod(
|
|
444
|
+
uint256_addmod(proof[P_EVAL_A], p[P_BETA_XI], BASE_FIELD_SIZE),
|
|
445
|
+
p[P_GAMMA],
|
|
446
|
+
BASE_FIELD_SIZE
|
|
447
|
+
)
|
|
448
|
+
|
|
449
|
+
val2: uint256 = uint256_addmod(
|
|
450
|
+
uint256_addmod(proof[P_EVAL_B], uint256_mulmod(p[P_BETA_XI], K1, BASE_FIELD_SIZE), BASE_FIELD_SIZE),
|
|
451
|
+
p[P_GAMMA],
|
|
452
|
+
BASE_FIELD_SIZE
|
|
453
|
+
)
|
|
454
|
+
|
|
455
|
+
val3: uint256 = uint256_addmod(
|
|
456
|
+
uint256_addmod(proof[P_EVAL_C], uint256_mulmod(p[P_BETA_XI], K2, BASE_FIELD_SIZE), BASE_FIELD_SIZE),
|
|
457
|
+
p[P_GAMMA],
|
|
458
|
+
BASE_FIELD_SIZE
|
|
459
|
+
)
|
|
460
|
+
|
|
461
|
+
d2a: uint256 = uint256_mulmod(
|
|
462
|
+
uint256_mulmod(uint256_mulmod(val1, val2, BASE_FIELD_SIZE), val3, BASE_FIELD_SIZE),
|
|
463
|
+
p[P_ALPHA],
|
|
464
|
+
BASE_FIELD_SIZE
|
|
465
|
+
)
|
|
466
|
+
|
|
467
|
+
d2b: uint256 = uint256_mulmod(p[P_EVAL_L1], p[P_ALPHA2], BASE_FIELD_SIZE)
|
|
468
|
+
|
|
469
|
+
mP: uint256[2] = [0, 0]
|
|
470
|
+
|
|
471
|
+
success, mP = self._ecmul(
|
|
472
|
+
[proof[P_Z], proof[P_Z + 1]],
|
|
473
|
+
uint256_addmod(uint256_addmod(d2a, d2b, BASE_FIELD_SIZE), p[P_U], BASE_FIELD_SIZE)
|
|
474
|
+
)
|
|
475
|
+
if not success:
|
|
476
|
+
return (False, [0, 0])
|
|
477
|
+
|
|
478
|
+
val1 = uint256_addmod(
|
|
479
|
+
uint256_addmod(proof[P_EVAL_A], uint256_mulmod(p[P_BETA], proof[P_EVAL_S1], BASE_FIELD_SIZE), BASE_FIELD_SIZE),
|
|
480
|
+
p[P_GAMMA],
|
|
481
|
+
BASE_FIELD_SIZE
|
|
482
|
+
)
|
|
483
|
+
|
|
484
|
+
val2 = uint256_addmod(
|
|
485
|
+
uint256_addmod(proof[P_EVAL_B], uint256_mulmod(p[P_BETA], proof[P_EVAL_S2], BASE_FIELD_SIZE), BASE_FIELD_SIZE),
|
|
486
|
+
p[P_GAMMA],
|
|
487
|
+
BASE_FIELD_SIZE
|
|
488
|
+
)
|
|
489
|
+
|
|
490
|
+
val3 = uint256_mulmod(
|
|
491
|
+
uint256_mulmod(p[P_ALPHA], p[P_BETA], BASE_FIELD_SIZE),
|
|
492
|
+
proof[P_EVAL_ZW],
|
|
493
|
+
BASE_FIELD_SIZE
|
|
494
|
+
)
|
|
495
|
+
|
|
496
|
+
success, mP = self._ecmul(
|
|
497
|
+
[proof[P_Z], proof[P_Z + 1]],
|
|
498
|
+
uint256_addmod(uint256_addmod(d2a, d2b, BASE_FIELD_SIZE), p[P_U], BASE_FIELD_SIZE)
|
|
499
|
+
)
|
|
500
|
+
if not success:
|
|
501
|
+
return (False, [0, 0])
|
|
502
|
+
|
|
503
|
+
success, pd = self._ecadd(pd, mP)
|
|
504
|
+
if not success:
|
|
505
|
+
return (False, [0, 0])
|
|
506
|
+
|
|
507
|
+
success, mP = self._ecmul(
|
|
508
|
+
[S3_X, S3_Y],
|
|
509
|
+
uint256_mulmod(uint256_mulmod(val1, val2, BASE_FIELD_SIZE), val3, BASE_FIELD_SIZE)
|
|
510
|
+
)
|
|
511
|
+
mP[1] = (QF - mP[1]) % QF
|
|
512
|
+
|
|
513
|
+
success, pd = self._ecadd(pd, mP)
|
|
514
|
+
if not success:
|
|
515
|
+
return (False, [0, 0])
|
|
516
|
+
|
|
517
|
+
pd2: uint256[2] = [proof[P_T1], proof[P_T1 + 1]]
|
|
518
|
+
success, pd2 = self._g1_mulAccC(pd2, [proof[P_T2], proof[P_T2 + 1]], p[P_XIN])
|
|
519
|
+
if not success:
|
|
520
|
+
return (False, [0, 0])
|
|
521
|
+
|
|
522
|
+
xin2: uint256 = uint256_mulmod(p[P_XIN], p[P_XIN], BASE_FIELD_SIZE)
|
|
523
|
+
success, pd2 = self._g1_mulAccC(pd2, [proof[P_T3], proof[P_T3 + 1]], xin2)
|
|
524
|
+
if not success:
|
|
525
|
+
return (False, [0, 0])
|
|
526
|
+
|
|
527
|
+
success, mP = self._ecmul(pd2, p[P_ZH])
|
|
528
|
+
mP[1] = (QF - mP[1]) % QF
|
|
529
|
+
|
|
530
|
+
return self._ecadd(pd, mP)
|
|
531
|
+
|
|
532
|
+
|
|
533
|
+
@pure
|
|
534
|
+
@internal
|
|
535
|
+
def _calculateF(p: uint256[P_TOTAL_SIZE], proof: uint256[24]) -> (bool, uint256[2]):
|
|
536
|
+
success: bool = True
|
|
537
|
+
pf: uint256[2] = [p[P_D], p[P_D + 1]]
|
|
538
|
+
|
|
539
|
+
success, pf = self._g1_mulAccC(pf, [proof[P_A], proof[P_A + 1]], p[P_V1])
|
|
540
|
+
if not success:
|
|
541
|
+
return (False, [0, 0])
|
|
542
|
+
|
|
543
|
+
success, pf = self._g1_mulAccC(pf, [proof[P_B], proof[P_B + 1]], p[P_V2])
|
|
544
|
+
if not success:
|
|
545
|
+
return (False, [0, 0])
|
|
546
|
+
|
|
547
|
+
success, pf = self._g1_mulAccC(pf, [proof[P_C], proof[P_C + 1]], p[P_V3])
|
|
548
|
+
if not success:
|
|
549
|
+
return (False, [0, 0])
|
|
550
|
+
|
|
551
|
+
success, pf = self._g1_mulAccC(pf, [S1_X, S1_Y], p[P_V4])
|
|
552
|
+
if not success:
|
|
553
|
+
return (False, [0, 0])
|
|
554
|
+
|
|
555
|
+
return self._g1_mulAccC(pf, [S2_X, S2_Y], p[P_V5])
|
|
556
|
+
|
|
557
|
+
|
|
558
|
+
@pure
|
|
559
|
+
@internal
|
|
560
|
+
def _calculateE(p: uint256[P_TOTAL_SIZE], proof: uint256[24]) -> (bool, uint256[2]):
|
|
561
|
+
s: uint256 = (BASE_FIELD_SIZE - p[P_EVAL_R0]) % BASE_FIELD_SIZE
|
|
562
|
+
|
|
563
|
+
s = uint256_addmod(s, uint256_mulmod(proof[P_EVAL_A], p[P_V1], BASE_FIELD_SIZE), BASE_FIELD_SIZE)
|
|
564
|
+
s = uint256_addmod(s, uint256_mulmod(proof[P_EVAL_B], p[P_V2], BASE_FIELD_SIZE), BASE_FIELD_SIZE)
|
|
565
|
+
s = uint256_addmod(s, uint256_mulmod(proof[P_EVAL_C], p[P_V3], BASE_FIELD_SIZE), BASE_FIELD_SIZE)
|
|
566
|
+
s = uint256_addmod(s, uint256_mulmod(proof[P_EVAL_S1], p[P_V4], BASE_FIELD_SIZE), BASE_FIELD_SIZE)
|
|
567
|
+
s = uint256_addmod(s, uint256_mulmod(proof[P_EVAL_S2], p[P_V5], BASE_FIELD_SIZE), BASE_FIELD_SIZE)
|
|
568
|
+
s = uint256_addmod(s, uint256_mulmod(proof[P_EVAL_ZW], p[P_U], BASE_FIELD_SIZE), BASE_FIELD_SIZE)
|
|
569
|
+
|
|
570
|
+
return self._ecmul([G1_X, G1_Y], s)
|
|
571
|
+
|
|
572
|
+
|
|
573
|
+
@pure
|
|
574
|
+
@internal
|
|
575
|
+
def _checkPairing(p: uint256[P_TOTAL_SIZE], proof: uint256[24]) -> bool:
|
|
576
|
+
mIn: uint256[12] = empty(uint256[12])
|
|
577
|
+
|
|
578
|
+
success: bool = True
|
|
579
|
+
mP: uint256[2] = [0, 0]
|
|
580
|
+
|
|
581
|
+
# A1
|
|
582
|
+
success, mP = self._ecmul([proof[P_WX_IW], proof[P_WX_IW + 1]], p[P_U])
|
|
583
|
+
if not success:
|
|
584
|
+
return False
|
|
585
|
+
|
|
586
|
+
aP: uint256[2] = [0, 0]
|
|
587
|
+
success, aP = self._ecadd([proof[P_WX_I], proof[P_WX_I + 1]], mP)
|
|
588
|
+
if not success:
|
|
589
|
+
return False
|
|
590
|
+
|
|
591
|
+
mIn[0] = aP[0]
|
|
592
|
+
mIn[1] = (QF - aP[1]) % QF
|
|
593
|
+
|
|
594
|
+
# [X]_2
|
|
595
|
+
mIn[2] = X2_X2
|
|
596
|
+
mIn[3] = X2_X1
|
|
597
|
+
mIn[4] = X2_Y2
|
|
598
|
+
mIn[5] = X2_Y1
|
|
599
|
+
|
|
600
|
+
# B1
|
|
601
|
+
success, mP = self._ecmul([proof[P_WX_I], proof[P_WX_I + 1]], p[P_XI])
|
|
602
|
+
if not success:
|
|
603
|
+
return False
|
|
604
|
+
|
|
605
|
+
mIn[6] = mP[0]
|
|
606
|
+
mIn[7] = mP[1]
|
|
607
|
+
|
|
608
|
+
s: uint256 = uint256_mulmod(p[P_U], p[P_XI], BASE_FIELD_SIZE)
|
|
609
|
+
s = uint256_mulmod(s, W1, BASE_FIELD_SIZE)
|
|
610
|
+
|
|
611
|
+
success, mP = self._ecmul([proof[P_WX_IW], proof[P_WX_IW + 1]], s)
|
|
612
|
+
if not success:
|
|
613
|
+
return False
|
|
614
|
+
|
|
615
|
+
success, aP = self._ecadd([mIn[6], mIn[7]], mP)
|
|
616
|
+
if not success:
|
|
617
|
+
return False
|
|
618
|
+
|
|
619
|
+
success, aP = self._ecadd(aP, [p[P_F], p[P_F + 1]])
|
|
620
|
+
if not success:
|
|
621
|
+
return False
|
|
622
|
+
|
|
623
|
+
p[P_E + 1] = (QF - p[P_E + 1]) % QF
|
|
624
|
+
|
|
625
|
+
success, aP = self._ecadd(aP, [p[P_E], p[P_E + 1]])
|
|
626
|
+
if not success:
|
|
627
|
+
return False
|
|
628
|
+
|
|
629
|
+
mIn[6] = aP[0]
|
|
630
|
+
mIn[7] = aP[1]
|
|
631
|
+
|
|
632
|
+
# [1]_2
|
|
633
|
+
mIn[8] = G2_X2
|
|
634
|
+
mIn[9] = G2_X1
|
|
635
|
+
mIn[10] = G2_Y2
|
|
636
|
+
mIn[11] = G2_Y1
|
|
637
|
+
|
|
638
|
+
response: Bytes[32] = b""
|
|
639
|
+
success, response = raw_call(
|
|
640
|
+
EC_PAIRING_PRECOMPILED_ADDRESS,
|
|
641
|
+
abi_encode(mIn),
|
|
642
|
+
max_outsize=32,
|
|
643
|
+
is_static_call=True,
|
|
644
|
+
revert_on_failure=False
|
|
645
|
+
)
|
|
646
|
+
|
|
647
|
+
if not success:
|
|
648
|
+
return False
|
|
649
|
+
|
|
650
|
+
return convert(response, bool)
|
package/src/index.ts
CHANGED
|
@@ -1,38 +1,8 @@
|
|
|
1
|
-
export type NumericString = `${number}` | string;
|
|
2
|
-
|
|
3
|
-
export type PublicSignals = NumericString[];
|
|
4
|
-
|
|
5
|
-
export type Groth16Proof = {
|
|
6
|
-
pi_a: [NumericString, NumericString];
|
|
7
|
-
pi_b: [[NumericString, NumericString], [NumericString, NumericString]];
|
|
8
|
-
pi_c: [NumericString, NumericString];
|
|
9
|
-
protocol: string;
|
|
10
|
-
curve: string;
|
|
11
|
-
};
|
|
12
|
-
|
|
13
|
-
export type Calldata = [
|
|
14
|
-
[NumericString, NumericString],
|
|
15
|
-
[[NumericString, NumericString], [NumericString, NumericString]],
|
|
16
|
-
[NumericString, NumericString],
|
|
17
|
-
PublicSignals,
|
|
18
|
-
];
|
|
19
|
-
|
|
20
|
-
export type ProofStruct = {
|
|
21
|
-
proof: Groth16Proof;
|
|
22
|
-
publicSignals: PublicSignals;
|
|
23
|
-
};
|
|
24
|
-
|
|
25
|
-
export type NumberLike = number | bigint | `${number}`;
|
|
26
|
-
export type ArrayLike = NumberLike[] | ArrayLike[];
|
|
27
|
-
export type Signal = NumberLike | ArrayLike;
|
|
28
|
-
export type Signals = Record<string, Signal>;
|
|
29
|
-
|
|
30
1
|
export type ArtifactsFileType = "r1cs" | "zkey" | "vkey" | "sym" | "json" | "wasm";
|
|
31
|
-
export type
|
|
2
|
+
export type VerifierLanguageType = "sol" | "vy";
|
|
32
3
|
|
|
33
4
|
export type CircuitZKitConfig = {
|
|
34
5
|
circuitName: string;
|
|
35
6
|
circuitArtifactsPath: string;
|
|
36
7
|
verifierDirPath: string;
|
|
37
|
-
templateType?: VerifierTemplateType;
|
|
38
8
|
};
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export type NumericString = `${number}` | string;
|
|
2
|
+
|
|
3
|
+
export type PublicSignals = NumericString[];
|
|
4
|
+
|
|
5
|
+
export type NumberLike = number | bigint | `${number}`;
|
|
6
|
+
export type ArrayLike = NumberLike[] | ArrayLike[];
|
|
7
|
+
|
|
8
|
+
export type Signal = NumberLike | ArrayLike;
|
|
9
|
+
export type Signals = Record<string, Signal>;
|