@solarity/zkit 0.2.6 → 0.3.0-rc.1

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.
Files changed (79) hide show
  1. package/dist/core/CircuitZKit.d.ts +22 -21
  2. package/dist/core/CircuitZKit.d.ts.map +1 -1
  3. package/dist/core/CircuitZKit.js +28 -43
  4. package/dist/core/CircuitZKit.js.map +1 -1
  5. package/dist/core/protocols/AbstractImplementer.d.ts +15 -0
  6. package/dist/core/protocols/AbstractImplementer.d.ts.map +1 -0
  7. package/dist/core/protocols/AbstractImplementer.js +36 -0
  8. package/dist/core/protocols/AbstractImplementer.js.map +1 -0
  9. package/dist/core/protocols/Groth16Implementer.d.ts +10 -0
  10. package/dist/core/protocols/Groth16Implementer.d.ts.map +1 -0
  11. package/dist/core/protocols/Groth16Implementer.js +50 -0
  12. package/dist/core/protocols/Groth16Implementer.js.map +1 -0
  13. package/dist/core/protocols/PlonkImplementer.d.ts +10 -0
  14. package/dist/core/protocols/PlonkImplementer.d.ts.map +1 -0
  15. package/dist/core/protocols/PlonkImplementer.js +51 -0
  16. package/dist/core/protocols/PlonkImplementer.js.map +1 -0
  17. package/dist/core/protocols/index.d.ts +4 -0
  18. package/dist/core/protocols/index.d.ts.map +1 -0
  19. package/dist/core/protocols/index.js +10 -0
  20. package/dist/core/protocols/index.js.map +1 -0
  21. package/dist/core/templates/verifier_groth16.vy.ejs +3 -3
  22. package/dist/core/templates/verifier_plonk.sol.ejs +779 -0
  23. package/dist/core/templates/verifier_plonk.vy.ejs +650 -0
  24. package/dist/index.d.ts +4 -1
  25. package/dist/index.d.ts.map +1 -1
  26. package/dist/index.js +6 -1
  27. package/dist/index.js.map +1 -1
  28. package/dist/types/circuit-zkit.d.ts +0 -34
  29. package/dist/types/circuit-zkit.d.ts.map +1 -1
  30. package/dist/types/proof-utils.d.ts +7 -0
  31. package/dist/types/proof-utils.d.ts.map +1 -0
  32. package/dist/types/proof-utils.js +3 -0
  33. package/dist/types/proof-utils.js.map +1 -0
  34. package/dist/types/protocols/groth16.d.ts +28 -0
  35. package/dist/types/protocols/groth16.d.ts.map +1 -0
  36. package/dist/types/protocols/groth16.js +3 -0
  37. package/dist/types/protocols/groth16.js.map +1 -0
  38. package/dist/types/protocols/index.d.ts +31 -0
  39. package/dist/types/protocols/index.d.ts.map +1 -0
  40. package/dist/types/protocols/index.js +19 -0
  41. package/dist/types/protocols/index.js.map +1 -0
  42. package/dist/types/protocols/plonk.d.ts +26 -0
  43. package/dist/types/protocols/plonk.d.ts.map +1 -0
  44. package/dist/types/{types.js → protocols/plonk.js} +1 -1
  45. package/dist/types/protocols/plonk.js.map +1 -0
  46. package/package.json +1 -1
  47. package/src/core/CircuitZKit.ts +40 -63
  48. package/src/core/protocols/AbstractImplementer.ts +67 -0
  49. package/src/core/protocols/Groth16Implementer.ts +29 -0
  50. package/src/core/protocols/PlonkImplementer.ts +32 -0
  51. package/src/core/protocols/index.ts +3 -0
  52. package/src/core/templates/verifier_groth16.vy.ejs +3 -3
  53. package/src/core/templates/verifier_plonk.sol.ejs +779 -0
  54. package/src/core/templates/verifier_plonk.vy.ejs +650 -0
  55. package/src/index.ts +5 -1
  56. package/src/types/circuit-zkit.ts +0 -31
  57. package/src/types/proof-utils.ts +9 -0
  58. package/src/types/protocols/groth16.ts +21 -0
  59. package/src/types/protocols/index.ts +49 -0
  60. package/src/types/protocols/plonk.ts +28 -0
  61. package/dist/config/config.d.ts +0 -27
  62. package/dist/config/config.d.ts.map +0 -1
  63. package/dist/config/config.js +0 -19
  64. package/dist/config/config.js.map +0 -1
  65. package/dist/core/CircomZKit.d.ts +0 -39
  66. package/dist/core/CircomZKit.d.ts.map +0 -1
  67. package/dist/core/CircomZKit.js +0 -94
  68. package/dist/core/CircomZKit.js.map +0 -1
  69. package/dist/core/ManagerZKit.d.ts +0 -97
  70. package/dist/core/ManagerZKit.d.ts.map +0 -1
  71. package/dist/core/ManagerZKit.js +0 -222
  72. package/dist/core/ManagerZKit.js.map +0 -1
  73. package/dist/types/types.d.ts +0 -46
  74. package/dist/types/types.d.ts.map +0 -1
  75. package/dist/types/types.js.map +0 -1
  76. package/dist/utils/utils.d.ts +0 -18
  77. package/dist/utils/utils.d.ts.map +0 -1
  78. package/dist/utils/utils.js +0 -58
  79. 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
+ SCALAR_SIZE: constant(uint256) = 21888242871839275222246405745257275088548364400416034343698204186575808495617
9
+ # Base field size
10
+ BASE_SIZE: 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], SCALAR_SIZE)
229
+
230
+ inv_total: uint256 = self._inverse(acc, SCALAR_SIZE)
231
+
232
+ for i: uint256 in range(<%=nPublic%>):
233
+ inverses[<%=nPublic%> - i] = uint256_mulmod(inv_total, pAux[<%=nPublic%> - i], SCALAR_SIZE)
234
+ inv_total = uint256_mulmod(inv_total, pVals[<%=nPublic%> - i], SCALAR_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] >= SCALAR_SIZE:
245
+ return False
246
+
247
+ if proof[P_EVAL_B] >= SCALAR_SIZE:
248
+ return False
249
+
250
+ if proof[P_EVAL_C] >= SCALAR_SIZE:
251
+ return False
252
+
253
+ if proof[P_EVAL_S1] >= SCALAR_SIZE:
254
+ return False
255
+
256
+ if proof[P_EVAL_S2] >= SCALAR_SIZE:
257
+ return False
258
+
259
+ if proof[P_EVAL_ZW] >= SCALAR_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) % SCALAR_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) % SCALAR_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) % SCALAR_SIZE
285
+ p[P_ALPHA] = aux
286
+ p[P_ALPHA2] = uint256_mulmod(aux, aux, SCALAR_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) % SCALAR_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) % SCALAR_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, SCALAR_SIZE)
302
+ p[P_V3] = uint256_mulmod(p[P_V2], v1, SCALAR_SIZE)
303
+ p[P_V4] = uint256_mulmod(p[P_V3], v1, SCALAR_SIZE)
304
+ p[P_V5] = uint256_mulmod(p[P_V4], v1, SCALAR_SIZE)
305
+
306
+ # challenges.beta * challenges.xi
307
+ p[P_BETA_XI] = uint256_mulmod(beta, aux, SCALAR_SIZE)
308
+
309
+ # challenges.xi^n
310
+ <%for (let i = 0; i < power; i++) {%>
311
+ aux = uint256_mulmod(aux, aux, SCALAR_SIZE)<% } %>
312
+ p[P_XIN] = aux
313
+
314
+ # Zh
315
+ aux = uint256_addmod(aux, SCALAR_SIZE - 1, SCALAR_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) % SCALAR_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, SCALAR_SIZE - w, SCALAR_SIZE), SCALAR_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, SCALAR_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, SCALAR_SIZE),
356
+ w,
357
+ SCALAR_SIZE
358
+ )
359
+ w = uint256_mulmod(w, W1, SCALAR_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
+ SCALAR_SIZE - uint256_mulmod(p[P_EVAL_L1 + i], pPub[i], SCALAR_SIZE),
373
+ SCALAR_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], SCALAR_SIZE)
385
+
386
+ e3a: uint256 = uint256_addmod(
387
+ proof[P_EVAL_A],
388
+ uint256_mulmod(p[P_BETA], proof[P_EVAL_S1], SCALAR_SIZE),
389
+ SCALAR_SIZE
390
+ )
391
+ e3a = uint256_addmod(e3a, p[P_GAMMA], SCALAR_SIZE)
392
+
393
+ e3b: uint256 = uint256_addmod(
394
+ proof[P_EVAL_B],
395
+ uint256_mulmod(p[P_BETA], proof[P_EVAL_S2], SCALAR_SIZE),
396
+ SCALAR_SIZE
397
+ )
398
+ e3b = uint256_addmod(e3b, p[P_GAMMA], SCALAR_SIZE)
399
+
400
+ e3c: uint256 = uint256_addmod(proof[P_EVAL_C], p[P_GAMMA], SCALAR_SIZE)
401
+
402
+ e3: uint256 = uint256_mulmod(uint256_mulmod(e3a, e3b, SCALAR_SIZE), e3c, SCALAR_SIZE)
403
+ e3 = uint256_mulmod(e3, proof[P_EVAL_ZW], SCALAR_SIZE)
404
+ e3 = uint256_mulmod(e3, p[P_ALPHA], SCALAR_SIZE)
405
+
406
+ r0: uint256 = uint256_addmod(e1, SCALAR_SIZE - e2, SCALAR_SIZE)
407
+ return uint256_addmod(r0, SCALAR_SIZE - e3, SCALAR_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], SCALAR_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], SCALAR_SIZE),
445
+ p[P_GAMMA],
446
+ SCALAR_SIZE
447
+ )
448
+
449
+ val2: uint256 = uint256_addmod(
450
+ uint256_addmod(proof[P_EVAL_B], uint256_mulmod(p[P_BETA_XI], K1, SCALAR_SIZE), SCALAR_SIZE),
451
+ p[P_GAMMA],
452
+ SCALAR_SIZE
453
+ )
454
+
455
+ val3: uint256 = uint256_addmod(
456
+ uint256_addmod(proof[P_EVAL_C], uint256_mulmod(p[P_BETA_XI], K2, SCALAR_SIZE), SCALAR_SIZE),
457
+ p[P_GAMMA],
458
+ SCALAR_SIZE
459
+ )
460
+
461
+ d2a: uint256 = uint256_mulmod(
462
+ uint256_mulmod(uint256_mulmod(val1, val2, SCALAR_SIZE), val3, SCALAR_SIZE),
463
+ p[P_ALPHA],
464
+ SCALAR_SIZE
465
+ )
466
+
467
+ d2b: uint256 = uint256_mulmod(p[P_EVAL_L1], p[P_ALPHA2], SCALAR_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, SCALAR_SIZE), p[P_U], SCALAR_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], SCALAR_SIZE), SCALAR_SIZE),
480
+ p[P_GAMMA],
481
+ SCALAR_SIZE
482
+ )
483
+
484
+ val2 = uint256_addmod(
485
+ uint256_addmod(proof[P_EVAL_B], uint256_mulmod(p[P_BETA], proof[P_EVAL_S2], SCALAR_SIZE), SCALAR_SIZE),
486
+ p[P_GAMMA],
487
+ SCALAR_SIZE
488
+ )
489
+
490
+ val3 = uint256_mulmod(
491
+ uint256_mulmod(p[P_ALPHA], p[P_BETA], SCALAR_SIZE),
492
+ proof[P_EVAL_ZW],
493
+ SCALAR_SIZE
494
+ )
495
+
496
+ success, mP = self._ecmul(
497
+ [proof[P_Z], proof[P_Z + 1]],
498
+ uint256_addmod(uint256_addmod(d2a, d2b, SCALAR_SIZE), p[P_U], SCALAR_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, SCALAR_SIZE), val3, SCALAR_SIZE)
510
+ )
511
+ mP[1] = (BASE_SIZE - mP[1]) % BASE_SIZE
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], SCALAR_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] = (BASE_SIZE - mP[1]) % BASE_SIZE
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 = (SCALAR_SIZE - p[P_EVAL_R0]) % SCALAR_SIZE
562
+
563
+ s = uint256_addmod(s, uint256_mulmod(proof[P_EVAL_A], p[P_V1], SCALAR_SIZE), SCALAR_SIZE)
564
+ s = uint256_addmod(s, uint256_mulmod(proof[P_EVAL_B], p[P_V2], SCALAR_SIZE), SCALAR_SIZE)
565
+ s = uint256_addmod(s, uint256_mulmod(proof[P_EVAL_C], p[P_V3], SCALAR_SIZE), SCALAR_SIZE)
566
+ s = uint256_addmod(s, uint256_mulmod(proof[P_EVAL_S1], p[P_V4], SCALAR_SIZE), SCALAR_SIZE)
567
+ s = uint256_addmod(s, uint256_mulmod(proof[P_EVAL_S2], p[P_V5], SCALAR_SIZE), SCALAR_SIZE)
568
+ s = uint256_addmod(s, uint256_mulmod(proof[P_EVAL_ZW], p[P_U], SCALAR_SIZE), SCALAR_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] = (BASE_SIZE - aP[1]) % BASE_SIZE
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], SCALAR_SIZE)
609
+ s = uint256_mulmod(s, W1, SCALAR_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] = (BASE_SIZE - p[P_E + 1]) % BASE_SIZE
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/dist/index.d.ts CHANGED
@@ -1,3 +1,6 @@
1
- export * from "./core/CircuitZKit";
1
+ export { CircuitZKit } from "./core/CircuitZKit";
2
+ export * from "./core/protocols";
2
3
  export * from "./types/circuit-zkit";
4
+ export * from "./types/proof-utils";
5
+ export * from "./types/protocols";
3
6
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,oBAAoB,CAAC;AACnC,cAAc,sBAAsB,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,oBAAoB,CAAC;AACjD,cAAc,kBAAkB,CAAC;AAEjC,cAAc,sBAAsB,CAAC;AACrC,cAAc,qBAAqB,CAAC;AACpC,cAAc,mBAAmB,CAAC"}
package/dist/index.js CHANGED
@@ -14,6 +14,11 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
- __exportStar(require("./core/CircuitZKit"), exports);
17
+ exports.CircuitZKit = void 0;
18
+ var CircuitZKit_1 = require("./core/CircuitZKit");
19
+ Object.defineProperty(exports, "CircuitZKit", { enumerable: true, get: function () { return CircuitZKit_1.CircuitZKit; } });
20
+ __exportStar(require("./core/protocols"), exports);
18
21
  __exportStar(require("./types/circuit-zkit"), exports);
22
+ __exportStar(require("./types/proof-utils"), exports);
23
+ __exportStar(require("./types/protocols"), exports);
19
24
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,qDAAmC;AACnC,uDAAqC"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;AAAA,kDAAiD;AAAxC,0GAAA,WAAW,OAAA;AACpB,mDAAiC;AAEjC,uDAAqC;AACrC,sDAAoC;AACpC,oDAAkC"}