@solarity/zkit 0.3.0-rc.0 → 0.3.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.
@@ -5,9 +5,9 @@
5
5
  # Omega
6
6
  W1: constant(uint256) = <%=w%>
7
7
  # Scalar field size
8
- BASE_FIELD_SIZE: constant(uint256) = 21888242871839275222246405745257275088548364400416034343698204186575808495617
8
+ SCALAR_SIZE: constant(uint256) = 21888242871839275222246405745257275088548364400416034343698204186575808495617
9
9
  # Base field size
10
- QF: constant(uint256) = 21888242871839275222246405745257275088696311157297823662689037894645226208583
10
+ BASE_SIZE: constant(uint256) = 21888242871839275222246405745257275088696311157297823662689037894645226208583
11
11
 
12
12
  # [1]_1
13
13
  G1_X: constant(uint256) = 1
@@ -94,7 +94,7 @@ P_ZH_INV: constant(uint256) = 24
94
94
 
95
95
  P_EVAL_L1: constant(uint256) = 25
96
96
 
97
- P_TOTAL_SIZE: constant(uint256) = <%=25+nPublic%>
97
+ P_TOTAL_SIZE: constant(uint256) = <%=25 + nPublic%>
98
98
 
99
99
  EC_ADD_PRECOMPILED_ADDRESS: constant(address) = 0x0000000000000000000000000000000000000006
100
100
  EC_MUL_PRECOMPILED_ADDRESS: constant(address) = 0x0000000000000000000000000000000000000007
@@ -218,20 +218,20 @@ def _inverse(a: uint256, q: uint256) -> uint256:
218
218
 
219
219
  @pure
220
220
  @internal
221
- def _inverseArray(pVals: uint256[<%=nPublic+1%>]) -> uint256[<%=nPublic+1%>]:
221
+ def _inverseArray(pVals: uint256[<%=nPublic + 1%>]) -> uint256[<%=nPublic + 1%>]:
222
222
  acc: uint256 = pVals[0]
223
- inverses: uint256[<%=nPublic+1%>] = empty(uint256[<%=nPublic+1%>])
224
- pAux: uint256[<%=nPublic+1%>] = empty(uint256[<%=nPublic+1%>])
223
+ inverses: uint256[<%=nPublic + 1%>] = empty(uint256[<%=nPublic + 1%>])
224
+ pAux: uint256[<%=nPublic + 1%>] = empty(uint256[<%=nPublic + 1%>])
225
225
 
226
- for i: uint256 in range(1, <%=nPublic+1%>):
226
+ for i: uint256 in range(1, <%=nPublic + 1%>):
227
227
  pAux[i] = acc
228
- acc = uint256_mulmod(acc, pVals[i], BASE_FIELD_SIZE)
228
+ acc = uint256_mulmod(acc, pVals[i], SCALAR_SIZE)
229
229
 
230
- inv_total: uint256 = self._inverse(acc, BASE_FIELD_SIZE)
230
+ inv_total: uint256 = self._inverse(acc, SCALAR_SIZE)
231
231
 
232
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)
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
235
 
236
236
  inverses[0] = inv_total
237
237
 
@@ -241,22 +241,22 @@ def _inverseArray(pVals: uint256[<%=nPublic+1%>]) -> uint256[<%=nPublic+1%>]:
241
241
  @pure
242
242
  @internal
243
243
  def _checkInput(proof: uint256[24]) -> bool:
244
- if proof[P_EVAL_A] >= BASE_FIELD_SIZE:
244
+ if proof[P_EVAL_A] >= SCALAR_SIZE:
245
245
  return False
246
246
 
247
- if proof[P_EVAL_B] >= BASE_FIELD_SIZE:
247
+ if proof[P_EVAL_B] >= SCALAR_SIZE:
248
248
  return False
249
249
 
250
- if proof[P_EVAL_C] >= BASE_FIELD_SIZE:
250
+ if proof[P_EVAL_C] >= SCALAR_SIZE:
251
251
  return False
252
252
 
253
- if proof[P_EVAL_S1] >= BASE_FIELD_SIZE:
253
+ if proof[P_EVAL_S1] >= SCALAR_SIZE:
254
254
  return False
255
255
 
256
- if proof[P_EVAL_S2] >= BASE_FIELD_SIZE:
256
+ if proof[P_EVAL_S2] >= SCALAR_SIZE:
257
257
  return False
258
258
 
259
- if proof[P_EVAL_ZW] >= BASE_FIELD_SIZE:
259
+ if proof[P_EVAL_ZW] >= SCALAR_SIZE:
260
260
  return False
261
261
 
262
262
  return True
@@ -265,60 +265,60 @@ def _checkInput(proof: uint256[24]) -> bool:
265
265
  @pure
266
266
  @internal
267
267
  def _calculateChallenges(proof: uint256[24], pubSignals: uint256[<%=nPublic%>]) -> uint256[P_TOTAL_SIZE]:
268
- mIn<%=22+nPublic%>: uint256[<%=22+nPublic%>] = [
268
+ mIn<%=22 + nPublic%>: uint256[<%=22 + nPublic%>] = [
269
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
270
  <% for (let i = 0; i < nPublic; i++) { %>pubSignals[<%=i%>], <% } %>
271
271
  proof[P_A], proof[P_A + 1], proof[P_B], proof[P_B + 1], proof[P_C], proof[P_C + 1],
272
272
  ]
273
273
 
274
- beta: uint256 = convert(keccak256(abi_encode(mIn<%=22+nPublic%>)), uint256) % BASE_FIELD_SIZE
274
+ beta: uint256 = convert(keccak256(abi_encode(mIn<%=22+nPublic%>)), uint256) % SCALAR_SIZE
275
275
 
276
276
  p: uint256[P_TOTAL_SIZE] = empty(uint256[P_TOTAL_SIZE])
277
277
  p[P_BETA] = beta
278
278
 
279
279
  # challenges.gamma
280
- p[P_GAMMA] = convert(keccak256(abi_encode(p[P_BETA])), uint256) % BASE_FIELD_SIZE
280
+ p[P_GAMMA] = convert(keccak256(abi_encode(p[P_BETA])), uint256) % SCALAR_SIZE
281
281
 
282
282
  # challenges.alpha
283
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
284
+ aux: uint256 = convert(keccak256(abi_encode(mIn4)), uint256) % SCALAR_SIZE
285
285
  p[P_ALPHA] = aux
286
- p[P_ALPHA2] = uint256_mulmod(aux, aux, BASE_FIELD_SIZE)
286
+ p[P_ALPHA2] = uint256_mulmod(aux, aux, SCALAR_SIZE)
287
287
 
288
288
  # challenges.xi
289
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
290
+ aux = convert(keccak256(abi_encode(mIn7)), uint256) % SCALAR_SIZE
291
291
  p[P_XI] = aux
292
292
 
293
293
  # challenges.v
294
294
  mIn7 = [
295
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
296
  ]
297
- v1: uint256 = convert(keccak256(abi_encode(mIn7)), uint256) % BASE_FIELD_SIZE
297
+ v1: uint256 = convert(keccak256(abi_encode(mIn7)), uint256) % SCALAR_SIZE
298
298
  p[P_V1] = v1
299
299
 
300
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)
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
305
 
306
306
  # challenges.beta * challenges.xi
307
- p[P_BETA_XI] = uint256_mulmod(beta, aux, BASE_FIELD_SIZE)
307
+ p[P_BETA_XI] = uint256_mulmod(beta, aux, SCALAR_SIZE)
308
308
 
309
309
  # challenges.xi^n
310
- <%for (let i=0; i<power;i++) {%>
311
- aux = uint256_mulmod(aux, aux, BASE_FIELD_SIZE)<% } %>
310
+ <%for (let i = 0; i < power; i++) {%>
311
+ aux = uint256_mulmod(aux, aux, SCALAR_SIZE)<% } %>
312
312
  p[P_XIN] = aux
313
313
 
314
314
  # Zh
315
- aux = (aux - 1 + BASE_FIELD_SIZE) % BASE_FIELD_SIZE
315
+ aux = uint256_addmod(aux, SCALAR_SIZE - 1, SCALAR_SIZE)
316
316
  p[P_ZH] = aux
317
317
  p[P_ZH_INV] = aux
318
318
 
319
319
  # challenges.u
320
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
321
+ p[P_U] = convert(keccak256(abi_encode(mIn4)), uint256) % SCALAR_SIZE
322
322
 
323
323
  return p
324
324
 
@@ -326,7 +326,7 @@ def _calculateChallenges(proof: uint256[24], pubSignals: uint256[<%=nPublic%>])
326
326
  @pure
327
327
  @internal
328
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)
329
+ return uint256_mulmod(N, uint256_addmod(xi, SCALAR_SIZE - w, SCALAR_SIZE), SCALAR_SIZE)
330
330
 
331
331
 
332
332
  @pure
@@ -334,29 +334,29 @@ def _evaluateLagrange(w: uint256, xi: uint256) -> uint256:
334
334
  def _calculateLagrange(p: uint256[P_TOTAL_SIZE]) -> uint256[P_TOTAL_SIZE]:
335
335
  w: uint256 = 1
336
336
 
337
- for i: uint256 in range(1, <%=nPublic+1%>):
337
+ for i: uint256 in range(1, <%=nPublic + 1%>):
338
338
  p[P_EVAL_L1 + (i - 1)] = self._evaluateLagrange(w, p[P_XI])
339
- w = uint256_mulmod(w, W1, BASE_FIELD_SIZE)
339
+ w = uint256_mulmod(w, W1, SCALAR_SIZE)
340
340
 
341
- pointsToInverse: uint256[<%=nPublic+1%>] = empty(uint256[<%=nPublic+1%>])
342
- for i: uint256 in range(<%=nPublic+1%>):
341
+ pointsToInverse: uint256[<%=nPublic + 1%>] = empty(uint256[<%=nPublic + 1%>])
342
+ for i: uint256 in range(<%=nPublic + 1%>):
343
343
  pointsToInverse[i] = p[P_ZH_INV + i]
344
344
 
345
- inverses: uint256[<%=nPublic+1%>] = self._inverseArray(pointsToInverse)
345
+ inverses: uint256[<%=nPublic + 1%>] = self._inverseArray(pointsToInverse)
346
346
 
347
- for i: uint256 in range(<%=nPublic+1%>):
347
+ for i: uint256 in range(<%=nPublic + 1%>):
348
348
  p[P_ZH_INV + i] = inverses[i]
349
349
 
350
350
  zh: uint256 = p[P_ZH]
351
351
  w = 1
352
352
 
353
- for i: uint256 in range(1, <%=nPublic+1%>):
353
+ for i: uint256 in range(1, <%=nPublic + 1%>):
354
354
  p[P_EVAL_L1 + (i - 1)] = uint256_mulmod(
355
- uint256_mulmod(p[P_EVAL_L1 + (i - 1)], zh, BASE_FIELD_SIZE),
355
+ uint256_mulmod(p[P_EVAL_L1 + (i - 1)], zh, SCALAR_SIZE),
356
356
  w,
357
- BASE_FIELD_SIZE
357
+ SCALAR_SIZE
358
358
  )
359
- w = uint256_mulmod(w, W1, BASE_FIELD_SIZE)
359
+ w = uint256_mulmod(w, W1, SCALAR_SIZE)
360
360
 
361
361
  return p
362
362
 
@@ -369,8 +369,8 @@ def _calculatePI(p: uint256[P_TOTAL_SIZE], pPub: uint256[<%=nPublic%>]) -> uint2
369
369
  for i: uint256 in range(<%=nPublic%>):
370
370
  pl = uint256_addmod(
371
371
  pl,
372
- BASE_FIELD_SIZE - uint256_mulmod(p[P_EVAL_L1 + i], pPub[i], BASE_FIELD_SIZE),
373
- BASE_FIELD_SIZE
372
+ SCALAR_SIZE - uint256_mulmod(p[P_EVAL_L1 + i], pPub[i], SCALAR_SIZE),
373
+ SCALAR_SIZE
374
374
  )
375
375
 
376
376
  return pl
@@ -381,30 +381,30 @@ def _calculatePI(p: uint256[P_TOTAL_SIZE], pPub: uint256[<%=nPublic%>]) -> uint2
381
381
  def _calculateR0(p: uint256[P_TOTAL_SIZE], proof: uint256[24]) -> uint256:
382
382
  e1: uint256 = p[P_PI]
383
383
 
384
- e2: uint256 = uint256_mulmod(p[P_EVAL_L1], p[P_ALPHA2], BASE_FIELD_SIZE)
384
+ e2: uint256 = uint256_mulmod(p[P_EVAL_L1], p[P_ALPHA2], SCALAR_SIZE)
385
385
 
386
386
  e3a: uint256 = uint256_addmod(
387
387
  proof[P_EVAL_A],
388
- uint256_mulmod(p[P_BETA], proof[P_EVAL_S1], BASE_FIELD_SIZE),
389
- BASE_FIELD_SIZE
388
+ uint256_mulmod(p[P_BETA], proof[P_EVAL_S1], SCALAR_SIZE),
389
+ SCALAR_SIZE
390
390
  )
391
- e3a = uint256_addmod(e3a, p[P_GAMMA], BASE_FIELD_SIZE)
391
+ e3a = uint256_addmod(e3a, p[P_GAMMA], SCALAR_SIZE)
392
392
 
393
393
  e3b: uint256 = uint256_addmod(
394
394
  proof[P_EVAL_B],
395
- uint256_mulmod(p[P_BETA], proof[P_EVAL_S2], BASE_FIELD_SIZE),
396
- BASE_FIELD_SIZE
395
+ uint256_mulmod(p[P_BETA], proof[P_EVAL_S2], SCALAR_SIZE),
396
+ SCALAR_SIZE
397
397
  )
398
- e3b = uint256_addmod(e3b, p[P_GAMMA], BASE_FIELD_SIZE)
398
+ e3b = uint256_addmod(e3b, p[P_GAMMA], SCALAR_SIZE)
399
399
 
400
- e3c: uint256 = uint256_addmod(proof[P_EVAL_C], p[P_GAMMA], BASE_FIELD_SIZE)
400
+ e3c: uint256 = uint256_addmod(proof[P_EVAL_C], p[P_GAMMA], SCALAR_SIZE)
401
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)
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
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)
406
+ r0: uint256 = uint256_addmod(e1, SCALAR_SIZE - e2, SCALAR_SIZE)
407
+ return uint256_addmod(r0, SCALAR_SIZE - e3, SCALAR_SIZE)
408
408
 
409
409
 
410
410
  @pure
@@ -424,7 +424,7 @@ def _calculateD(p: uint256[P_TOTAL_SIZE], proof: uint256[24]) -> (bool, uint256[
424
424
  success: bool = True
425
425
  pd: uint256[2] = [QC_X, QC_Y]
426
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))
427
+ success, pd = self._g1_mulAccC(pd, [QM_X, QM_Y], uint256_mulmod(proof[P_EVAL_A], proof[P_EVAL_B], SCALAR_SIZE))
428
428
  if not success:
429
429
  return (False, [0, 0])
430
430
 
@@ -441,61 +441,61 @@ def _calculateD(p: uint256[P_TOTAL_SIZE], proof: uint256[24]) -> (bool, uint256[
441
441
  return (False, [0, 0])
442
442
 
443
443
  val1: uint256 = uint256_addmod(
444
- uint256_addmod(proof[P_EVAL_A], p[P_BETA_XI], BASE_FIELD_SIZE),
444
+ uint256_addmod(proof[P_EVAL_A], p[P_BETA_XI], SCALAR_SIZE),
445
445
  p[P_GAMMA],
446
- BASE_FIELD_SIZE
446
+ SCALAR_SIZE
447
447
  )
448
448
 
449
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),
450
+ uint256_addmod(proof[P_EVAL_B], uint256_mulmod(p[P_BETA_XI], K1, SCALAR_SIZE), SCALAR_SIZE),
451
451
  p[P_GAMMA],
452
- BASE_FIELD_SIZE
452
+ SCALAR_SIZE
453
453
  )
454
454
 
455
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),
456
+ uint256_addmod(proof[P_EVAL_C], uint256_mulmod(p[P_BETA_XI], K2, SCALAR_SIZE), SCALAR_SIZE),
457
457
  p[P_GAMMA],
458
- BASE_FIELD_SIZE
458
+ SCALAR_SIZE
459
459
  )
460
460
 
461
461
  d2a: uint256 = uint256_mulmod(
462
- uint256_mulmod(uint256_mulmod(val1, val2, BASE_FIELD_SIZE), val3, BASE_FIELD_SIZE),
462
+ uint256_mulmod(uint256_mulmod(val1, val2, SCALAR_SIZE), val3, SCALAR_SIZE),
463
463
  p[P_ALPHA],
464
- BASE_FIELD_SIZE
464
+ SCALAR_SIZE
465
465
  )
466
466
 
467
- d2b: uint256 = uint256_mulmod(p[P_EVAL_L1], p[P_ALPHA2], BASE_FIELD_SIZE)
467
+ d2b: uint256 = uint256_mulmod(p[P_EVAL_L1], p[P_ALPHA2], SCALAR_SIZE)
468
468
 
469
469
  mP: uint256[2] = [0, 0]
470
470
 
471
471
  success, mP = self._ecmul(
472
472
  [proof[P_Z], proof[P_Z + 1]],
473
- uint256_addmod(uint256_addmod(d2a, d2b, BASE_FIELD_SIZE), p[P_U], BASE_FIELD_SIZE)
473
+ uint256_addmod(uint256_addmod(d2a, d2b, SCALAR_SIZE), p[P_U], SCALAR_SIZE)
474
474
  )
475
475
  if not success:
476
476
  return (False, [0, 0])
477
477
 
478
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),
479
+ uint256_addmod(proof[P_EVAL_A], uint256_mulmod(p[P_BETA], proof[P_EVAL_S1], SCALAR_SIZE), SCALAR_SIZE),
480
480
  p[P_GAMMA],
481
- BASE_FIELD_SIZE
481
+ SCALAR_SIZE
482
482
  )
483
483
 
484
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),
485
+ uint256_addmod(proof[P_EVAL_B], uint256_mulmod(p[P_BETA], proof[P_EVAL_S2], SCALAR_SIZE), SCALAR_SIZE),
486
486
  p[P_GAMMA],
487
- BASE_FIELD_SIZE
487
+ SCALAR_SIZE
488
488
  )
489
489
 
490
490
  val3 = uint256_mulmod(
491
- uint256_mulmod(p[P_ALPHA], p[P_BETA], BASE_FIELD_SIZE),
491
+ uint256_mulmod(p[P_ALPHA], p[P_BETA], SCALAR_SIZE),
492
492
  proof[P_EVAL_ZW],
493
- BASE_FIELD_SIZE
493
+ SCALAR_SIZE
494
494
  )
495
495
 
496
496
  success, mP = self._ecmul(
497
497
  [proof[P_Z], proof[P_Z + 1]],
498
- uint256_addmod(uint256_addmod(d2a, d2b, BASE_FIELD_SIZE), p[P_U], BASE_FIELD_SIZE)
498
+ uint256_addmod(uint256_addmod(d2a, d2b, SCALAR_SIZE), p[P_U], SCALAR_SIZE)
499
499
  )
500
500
  if not success:
501
501
  return (False, [0, 0])
@@ -506,9 +506,9 @@ def _calculateD(p: uint256[P_TOTAL_SIZE], proof: uint256[24]) -> (bool, uint256[
506
506
 
507
507
  success, mP = self._ecmul(
508
508
  [S3_X, S3_Y],
509
- uint256_mulmod(uint256_mulmod(val1, val2, BASE_FIELD_SIZE), val3, BASE_FIELD_SIZE)
509
+ uint256_mulmod(uint256_mulmod(val1, val2, SCALAR_SIZE), val3, SCALAR_SIZE)
510
510
  )
511
- mP[1] = (QF - mP[1]) % QF
511
+ mP[1] = (BASE_SIZE - mP[1]) % BASE_SIZE
512
512
 
513
513
  success, pd = self._ecadd(pd, mP)
514
514
  if not success:
@@ -519,13 +519,13 @@ def _calculateD(p: uint256[P_TOTAL_SIZE], proof: uint256[24]) -> (bool, uint256[
519
519
  if not success:
520
520
  return (False, [0, 0])
521
521
 
522
- xin2: uint256 = uint256_mulmod(p[P_XIN], p[P_XIN], BASE_FIELD_SIZE)
522
+ xin2: uint256 = uint256_mulmod(p[P_XIN], p[P_XIN], SCALAR_SIZE)
523
523
  success, pd2 = self._g1_mulAccC(pd2, [proof[P_T3], proof[P_T3 + 1]], xin2)
524
524
  if not success:
525
525
  return (False, [0, 0])
526
526
 
527
527
  success, mP = self._ecmul(pd2, p[P_ZH])
528
- mP[1] = (QF - mP[1]) % QF
528
+ mP[1] = (BASE_SIZE - mP[1]) % BASE_SIZE
529
529
 
530
530
  return self._ecadd(pd, mP)
531
531
 
@@ -558,14 +558,14 @@ def _calculateF(p: uint256[P_TOTAL_SIZE], proof: uint256[24]) -> (bool, uint256[
558
558
  @pure
559
559
  @internal
560
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
561
+ s: uint256 = (SCALAR_SIZE - p[P_EVAL_R0]) % SCALAR_SIZE
562
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)
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
569
 
570
570
  return self._ecmul([G1_X, G1_Y], s)
571
571
 
@@ -589,7 +589,7 @@ def _checkPairing(p: uint256[P_TOTAL_SIZE], proof: uint256[24]) -> bool:
589
589
  return False
590
590
 
591
591
  mIn[0] = aP[0]
592
- mIn[1] = (QF - aP[1]) % QF
592
+ mIn[1] = (BASE_SIZE - aP[1]) % BASE_SIZE
593
593
 
594
594
  # [X]_2
595
595
  mIn[2] = X2_X2
@@ -605,8 +605,8 @@ def _checkPairing(p: uint256[P_TOTAL_SIZE], proof: uint256[24]) -> bool:
605
605
  mIn[6] = mP[0]
606
606
  mIn[7] = mP[1]
607
607
 
608
- s: uint256 = uint256_mulmod(p[P_U], p[P_XI], BASE_FIELD_SIZE)
609
- s = uint256_mulmod(s, W1, BASE_FIELD_SIZE)
608
+ s: uint256 = uint256_mulmod(p[P_U], p[P_XI], SCALAR_SIZE)
609
+ s = uint256_mulmod(s, W1, SCALAR_SIZE)
610
610
 
611
611
  success, mP = self._ecmul([proof[P_WX_IW], proof[P_WX_IW + 1]], s)
612
612
  if not success:
@@ -620,7 +620,7 @@ def _checkPairing(p: uint256[P_TOTAL_SIZE], proof: uint256[24]) -> bool:
620
620
  if not success:
621
621
  return False
622
622
 
623
- p[P_E + 1] = (QF - p[P_E + 1]) % QF
623
+ p[P_E + 1] = (BASE_SIZE - p[P_E + 1]) % BASE_SIZE
624
624
 
625
625
  success, aP = self._ecadd(aP, [p[P_E], p[P_E + 1]])
626
626
  if not success:
@@ -0,0 +1,2 @@
1
+ export declare function terminateCurve(): Promise<void>;
2
+ //# sourceMappingURL=utils.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAGA,wBAAsB,cAAc,kBAEnC"}
package/dist/utils.js ADDED
@@ -0,0 +1,32 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ exports.terminateCurve = terminateCurve;
27
+ const snarkjs = __importStar(require("snarkjs"));
28
+ const constants_1 = require("./constants");
29
+ async function terminateCurve() {
30
+ await (await snarkjs.curves.getCurveFromName(constants_1.BN128_CURVE_NAME)).terminate();
31
+ }
32
+ //# sourceMappingURL=utils.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils.js","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;AAGA,wCAEC;AALD,iDAAmC;AACnC,2CAA+C;AAExC,KAAK,UAAU,cAAc;IAClC,MAAM,CAAC,MAAO,OAAe,CAAC,MAAM,CAAC,gBAAgB,CAAC,4BAAgB,CAAC,CAAC,CAAC,SAAS,EAAE,CAAC;AACvF,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@solarity/zkit",
3
- "version": "0.3.0-rc.0",
3
+ "version": "0.3.0",
4
4
  "license": "MIT",
5
5
  "author": "Distributed Lab",
6
6
  "readme": "README.md",
@@ -39,7 +39,7 @@
39
39
  },
40
40
  "dependencies": {
41
41
  "ejs": "3.1.10",
42
- "snarkjs": "0.7.3"
42
+ "snarkjs": "0.7.5"
43
43
  },
44
44
  "devDependencies": {
45
45
  "@nomicfoundation/hardhat-ethers": "3.0.5",
@@ -0,0 +1 @@
1
+ export const BN128_CURVE_NAME = "bn128";
@@ -47,7 +47,9 @@ export class CircuitZKit<Type extends ProvingSystemType> {
47
47
 
48
48
  await snarkjs.wtns.calculate(inputs, wasmFile, wtnsFile);
49
49
 
50
- return (await snarkjs.wtns.exportJson(wtnsFile)) as bigint[];
50
+ const wtnsJson = await snarkjs.wtns.exportJson(wtnsFile);
51
+
52
+ return wtnsJson as bigint[];
51
53
  }
52
54
 
53
55
  /**
@@ -6,15 +6,25 @@ import { AbstractProtocolImplementer } from "./AbstractImplementer";
6
6
  import { Signals } from "../../types/proof-utils";
7
7
  import { Groth16ProofStruct, ProvingSystemType, Groth16Calldata } from "../../types/protocols";
8
8
 
9
+ import { terminateCurve } from "../../utils";
10
+
9
11
  export class Groth16Implementer extends AbstractProtocolImplementer<"groth16"> {
10
12
  public async generateProof(inputs: Signals, zKeyFilePath: string, wasmFilePath: string): Promise<Groth16ProofStruct> {
11
- return (await snarkjs.groth16.fullProve(inputs, wasmFilePath, zKeyFilePath)) as Groth16ProofStruct;
13
+ const fullProof = await snarkjs.groth16.fullProve(inputs, wasmFilePath, zKeyFilePath);
14
+
15
+ await terminateCurve();
16
+
17
+ return fullProof as Groth16ProofStruct;
12
18
  }
13
19
 
14
20
  public async verifyProof(proof: Groth16ProofStruct, vKeyFilePath: string): Promise<boolean> {
15
21
  const verifier = JSON.parse(fs.readFileSync(vKeyFilePath).toString());
16
22
 
17
- return await snarkjs.groth16.verify(verifier, proof.publicSignals, proof.proof);
23
+ const proofVerification = await snarkjs.groth16.verify(verifier, proof.publicSignals, proof.proof);
24
+
25
+ await terminateCurve();
26
+
27
+ return proofVerification;
18
28
  }
19
29
 
20
30
  public async generateCalldata(proof: Groth16ProofStruct): Promise<Groth16Calldata> {
@@ -6,15 +6,25 @@ import { AbstractProtocolImplementer } from "./AbstractImplementer";
6
6
  import { Signals } from "../../types/proof-utils";
7
7
  import { PlonkCalldata, PlonkProofStruct, ProvingSystemType } from "../../types/protocols";
8
8
 
9
+ import { terminateCurve } from "../../utils";
10
+
9
11
  export class PlonkImplementer extends AbstractProtocolImplementer<"plonk"> {
10
12
  public async generateProof(inputs: Signals, zKeyFilePath: string, wasmFilePath: string): Promise<PlonkProofStruct> {
11
- return (await snarkjs.plonk.fullProve(inputs, wasmFilePath, zKeyFilePath)) as PlonkProofStruct;
13
+ const fullProof = await snarkjs.plonk.fullProve(inputs, wasmFilePath, zKeyFilePath);
14
+
15
+ await terminateCurve();
16
+
17
+ return fullProof as PlonkProofStruct;
12
18
  }
13
19
 
14
20
  public async verifyProof(proof: PlonkProofStruct, vKeyFilePath: string): Promise<boolean> {
15
21
  const verifier = JSON.parse(fs.readFileSync(vKeyFilePath).toString());
16
22
 
17
- return await snarkjs.plonk.verify(verifier, proof.publicSignals, proof.proof);
23
+ const proofVerification = await snarkjs.plonk.verify(verifier, proof.publicSignals, proof.proof);
24
+
25
+ await terminateCurve();
26
+
27
+ return proofVerification;
18
28
  }
19
29
 
20
30
  public async generateCalldata(proof: PlonkProofStruct): Promise<PlonkCalldata> {
@@ -22,7 +22,7 @@ DELTA_Y1: constant(uint256) = <%=vk_delta_2[1][1]%>
22
22
  DELTA_Y2: constant(uint256) = <%=vk_delta_2[1][0] -%>
23
23
 
24
24
 
25
- IC: constant(uint256[<%=IC.length%>][2]) = [
25
+ IC: constant(uint256[2][<%=IC.length%>]) = [
26
26
  <% IC.forEach(function(innerArray, index) { %> [
27
27
  <%= innerArray[0] %>,
28
28
  <%= innerArray[1] %>
@@ -36,7 +36,7 @@ EC_PAIRING_PRECOMPILED_ADDRESS: constant(address) = 0x00000000000000000000000000
36
36
 
37
37
  @view
38
38
  @external
39
- def verifyProof(pointA: uint256[2], pointB: uint256[2][2], pointC: uint256[2], publicSignals: uint256[<%=IC.length-1%>]) -> bool:
39
+ def verifyProof(pointA: uint256[2], pointB: uint256[2][2], pointC: uint256[2], publicSignals: uint256[<%=IC.length - 1%>]) -> bool:
40
40
  # @dev check that all public signals are in F
41
41
  for signal: uint256 in publicSignals:
42
42
  if signal >= BASE_FIELD_SIZE:
@@ -84,7 +84,7 @@ def _g1MulAdd(pR: uint256[2], pP: uint256[2], s: uint256) -> (bool, uint256[2]):
84
84
 
85
85
  @view
86
86
  @internal
87
- def _checkPairing(pA: uint256[2], pB: uint256[2][2], pC: uint256[2], pubSignals: uint256[<%=IC.length-1%>]) -> bool:
87
+ def _checkPairing(pA: uint256[2], pB: uint256[2][2], pC: uint256[2], pubSignals: uint256[<%=IC.length - 1%>]) -> bool:
88
88
  success: bool = True
89
89
  mulAddResult: uint256[2] = IC[0]
90
90