@solarity/zkit 0.3.2 → 0.3.4

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
- SCALAR_SIZE: constant(uint256) = 21888242871839275222246405745257275088548364400416034343698204186575808495617
8
+ SCALAR_FIELD_SIZE: constant(uint256) = 21888242871839275222246405745257275088548364400416034343698204186575808495617
9
9
  # Base field size
10
- BASE_SIZE: constant(uint256) = 21888242871839275222246405745257275088696311157297823662689037894645226208583
10
+ BASE_FIELD_SIZE: constant(uint256) = 21888242871839275222246405745257275088696311157297823662689037894645226208583
11
11
 
12
12
  # [1]_1
13
13
  G1_X: constant(uint256) = 1
@@ -227,13 +227,13 @@ def _inverseArray(pVals: uint256[<%=nPublic + 1%>]) -> uint256[<%=nPublic + 1%>]
227
227
 
228
228
  for i: uint256 in range(1, <%=nPublic + 1%>):
229
229
  pAux[i] = acc
230
- acc = uint256_mulmod(acc, pVals[i], SCALAR_SIZE)
230
+ acc = uint256_mulmod(acc, pVals[i], SCALAR_FIELD_SIZE)
231
231
 
232
- inv_total: uint256 = self._inverse(acc, SCALAR_SIZE)
232
+ inv_total: uint256 = self._inverse(acc, SCALAR_FIELD_SIZE)
233
233
 
234
234
  for i: uint256 in range(<%=nPublic%>):
235
- inverses[<%=nPublic%> - i] = uint256_mulmod(inv_total, pAux[<%=nPublic%> - i], SCALAR_SIZE)
236
- inv_total = uint256_mulmod(inv_total, pVals[<%=nPublic%> - i], SCALAR_SIZE)
235
+ inverses[<%=nPublic%> - i] = uint256_mulmod(inv_total, pAux[<%=nPublic%> - i], SCALAR_FIELD_SIZE)
236
+ inv_total = uint256_mulmod(inv_total, pVals[<%=nPublic%> - i], SCALAR_FIELD_SIZE)
237
237
 
238
238
  inverses[0] = inv_total
239
239
 
@@ -243,22 +243,22 @@ def _inverseArray(pVals: uint256[<%=nPublic + 1%>]) -> uint256[<%=nPublic + 1%>]
243
243
  @pure
244
244
  @internal
245
245
  def _checkInput(proof: uint256[24]) -> bool:
246
- if proof[P_EVAL_A] >= SCALAR_SIZE:
246
+ if proof[P_EVAL_A] >= SCALAR_FIELD_SIZE:
247
247
  return False
248
248
 
249
- if proof[P_EVAL_B] >= SCALAR_SIZE:
249
+ if proof[P_EVAL_B] >= SCALAR_FIELD_SIZE:
250
250
  return False
251
251
 
252
- if proof[P_EVAL_C] >= SCALAR_SIZE:
252
+ if proof[P_EVAL_C] >= SCALAR_FIELD_SIZE:
253
253
  return False
254
254
 
255
- if proof[P_EVAL_S1] >= SCALAR_SIZE:
255
+ if proof[P_EVAL_S1] >= SCALAR_FIELD_SIZE:
256
256
  return False
257
257
 
258
- if proof[P_EVAL_S2] >= SCALAR_SIZE:
258
+ if proof[P_EVAL_S2] >= SCALAR_FIELD_SIZE:
259
259
  return False
260
260
 
261
- if proof[P_EVAL_ZW] >= SCALAR_SIZE:
261
+ if proof[P_EVAL_ZW] >= SCALAR_FIELD_SIZE:
262
262
  return False
263
263
 
264
264
  return True
@@ -273,54 +273,54 @@ def _calculateChallenges(proof: uint256[24], pubSignals: uint256[<%=nPublic%>])
273
273
  proof[P_A], proof[P_A + 1], proof[P_B], proof[P_B + 1], proof[P_C], proof[P_C + 1],
274
274
  ]
275
275
 
276
- beta: uint256 = convert(keccak256(abi_encode(mIn<%=22+nPublic%>)), uint256) % SCALAR_SIZE
276
+ beta: uint256 = convert(keccak256(abi_encode(mIn<%=22+nPublic%>)), uint256) % SCALAR_FIELD_SIZE
277
277
 
278
278
  p: uint256[P_TOTAL_SIZE] = empty(uint256[P_TOTAL_SIZE])
279
279
  p[P_BETA] = beta
280
280
 
281
281
  # challenges.gamma
282
- p[P_GAMMA] = convert(keccak256(abi_encode(p[P_BETA])), uint256) % SCALAR_SIZE
282
+ p[P_GAMMA] = convert(keccak256(abi_encode(p[P_BETA])), uint256) % SCALAR_FIELD_SIZE
283
283
 
284
284
  # challenges.alpha
285
285
  mIn4: uint256[4] = [beta, p[P_GAMMA], proof[P_Z], proof[P_Z + 1]]
286
- aux: uint256 = convert(keccak256(abi_encode(mIn4)), uint256) % SCALAR_SIZE
286
+ aux: uint256 = convert(keccak256(abi_encode(mIn4)), uint256) % SCALAR_FIELD_SIZE
287
287
  p[P_ALPHA] = aux
288
- p[P_ALPHA2] = uint256_mulmod(aux, aux, SCALAR_SIZE)
288
+ p[P_ALPHA2] = uint256_mulmod(aux, aux, SCALAR_FIELD_SIZE)
289
289
 
290
290
  # challenges.xi
291
291
  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]]
292
- aux = convert(keccak256(abi_encode(mIn7)), uint256) % SCALAR_SIZE
292
+ aux = convert(keccak256(abi_encode(mIn7)), uint256) % SCALAR_FIELD_SIZE
293
293
  p[P_XI] = aux
294
294
 
295
295
  # challenges.v
296
296
  mIn7 = [
297
297
  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]
298
298
  ]
299
- v1: uint256 = convert(keccak256(abi_encode(mIn7)), uint256) % SCALAR_SIZE
299
+ v1: uint256 = convert(keccak256(abi_encode(mIn7)), uint256) % SCALAR_FIELD_SIZE
300
300
  p[P_V1] = v1
301
301
 
302
302
  # challenges.v^2, challenges.v^3, challenges.v^4, challenges.v^5
303
- p[P_V2] = uint256_mulmod(v1, v1, SCALAR_SIZE)
304
- p[P_V3] = uint256_mulmod(p[P_V2], v1, SCALAR_SIZE)
305
- p[P_V4] = uint256_mulmod(p[P_V3], v1, SCALAR_SIZE)
306
- p[P_V5] = uint256_mulmod(p[P_V4], v1, SCALAR_SIZE)
303
+ p[P_V2] = uint256_mulmod(v1, v1, SCALAR_FIELD_SIZE)
304
+ p[P_V3] = uint256_mulmod(p[P_V2], v1, SCALAR_FIELD_SIZE)
305
+ p[P_V4] = uint256_mulmod(p[P_V3], v1, SCALAR_FIELD_SIZE)
306
+ p[P_V5] = uint256_mulmod(p[P_V4], v1, SCALAR_FIELD_SIZE)
307
307
 
308
308
  # challenges.beta * challenges.xi
309
- p[P_BETA_XI] = uint256_mulmod(beta, aux, SCALAR_SIZE)
309
+ p[P_BETA_XI] = uint256_mulmod(beta, aux, SCALAR_FIELD_SIZE)
310
310
 
311
311
  # challenges.xi^n
312
312
  <%for (let i = 0; i < power; i++) {%>
313
- aux = uint256_mulmod(aux, aux, SCALAR_SIZE)<% } %>
313
+ aux = uint256_mulmod(aux, aux, SCALAR_FIELD_SIZE)<% } %>
314
314
  p[P_XIN] = aux
315
315
 
316
316
  # Zh
317
- aux = uint256_addmod(aux, SCALAR_SIZE - 1, SCALAR_SIZE)
317
+ aux = uint256_addmod(aux, SCALAR_FIELD_SIZE - 1, SCALAR_FIELD_SIZE)
318
318
  p[P_ZH] = aux
319
319
  p[P_ZH_INV] = aux
320
320
 
321
321
  # challenges.u
322
322
  mIn4 = [proof[P_WX_I], proof[P_WX_I + 1], proof[P_WX_IW], proof[P_WX_IW + 1]]
323
- p[P_U] = convert(keccak256(abi_encode(mIn4)), uint256) % SCALAR_SIZE
323
+ p[P_U] = convert(keccak256(abi_encode(mIn4)), uint256) % SCALAR_FIELD_SIZE
324
324
 
325
325
  return p
326
326
 
@@ -328,7 +328,7 @@ def _calculateChallenges(proof: uint256[24], pubSignals: uint256[<%=nPublic%>])
328
328
  @pure
329
329
  @internal
330
330
  def _evaluateLagrange(w: uint256, xi: uint256) -> uint256:
331
- return uint256_mulmod(N, uint256_addmod(xi, SCALAR_SIZE - w, SCALAR_SIZE), SCALAR_SIZE)
331
+ return uint256_mulmod(N, uint256_addmod(xi, SCALAR_FIELD_SIZE - w, SCALAR_FIELD_SIZE), SCALAR_FIELD_SIZE)
332
332
 
333
333
 
334
334
  @pure
@@ -338,7 +338,7 @@ def _calculateLagrange(p: uint256[P_TOTAL_SIZE]) -> uint256[P_TOTAL_SIZE]:
338
338
 
339
339
  for i: uint256 in range(1, <%=nPublic + 1%>):
340
340
  p[P_EVAL_L1 + (i - 1)] = self._evaluateLagrange(w, p[P_XI])
341
- w = uint256_mulmod(w, W1, SCALAR_SIZE)
341
+ w = uint256_mulmod(w, W1, SCALAR_FIELD_SIZE)
342
342
 
343
343
  pointsToInverse: uint256[<%=nPublic + 1%>] = empty(uint256[<%=nPublic + 1%>])
344
344
  for i: uint256 in range(<%=nPublic + 1%>):
@@ -354,11 +354,11 @@ def _calculateLagrange(p: uint256[P_TOTAL_SIZE]) -> uint256[P_TOTAL_SIZE]:
354
354
 
355
355
  for i: uint256 in range(1, <%=nPublic + 1%>):
356
356
  p[P_EVAL_L1 + (i - 1)] = uint256_mulmod(
357
- uint256_mulmod(p[P_EVAL_L1 + (i - 1)], zh, SCALAR_SIZE),
357
+ uint256_mulmod(p[P_EVAL_L1 + (i - 1)], zh, SCALAR_FIELD_SIZE),
358
358
  w,
359
- SCALAR_SIZE
359
+ SCALAR_FIELD_SIZE
360
360
  )
361
- w = uint256_mulmod(w, W1, SCALAR_SIZE)
361
+ w = uint256_mulmod(w, W1, SCALAR_FIELD_SIZE)
362
362
 
363
363
  return p
364
364
 
@@ -371,8 +371,8 @@ def _calculatePI(p: uint256[P_TOTAL_SIZE], pPub: uint256[<%=nPublic%>]) -> uint2
371
371
  for i: uint256 in range(<%=nPublic%>):
372
372
  pl = uint256_addmod(
373
373
  pl,
374
- SCALAR_SIZE - uint256_mulmod(p[P_EVAL_L1 + i], pPub[i], SCALAR_SIZE),
375
- SCALAR_SIZE
374
+ SCALAR_FIELD_SIZE - uint256_mulmod(p[P_EVAL_L1 + i], pPub[i], SCALAR_FIELD_SIZE),
375
+ SCALAR_FIELD_SIZE
376
376
  )
377
377
 
378
378
  return pl
@@ -383,30 +383,30 @@ def _calculatePI(p: uint256[P_TOTAL_SIZE], pPub: uint256[<%=nPublic%>]) -> uint2
383
383
  def _calculateR0(p: uint256[P_TOTAL_SIZE], proof: uint256[24]) -> uint256:
384
384
  e1: uint256 = p[P_PI]
385
385
 
386
- e2: uint256 = uint256_mulmod(p[P_EVAL_L1], p[P_ALPHA2], SCALAR_SIZE)
386
+ e2: uint256 = uint256_mulmod(p[P_EVAL_L1], p[P_ALPHA2], SCALAR_FIELD_SIZE)
387
387
 
388
388
  e3a: uint256 = uint256_addmod(
389
389
  proof[P_EVAL_A],
390
- uint256_mulmod(p[P_BETA], proof[P_EVAL_S1], SCALAR_SIZE),
391
- SCALAR_SIZE
390
+ uint256_mulmod(p[P_BETA], proof[P_EVAL_S1], SCALAR_FIELD_SIZE),
391
+ SCALAR_FIELD_SIZE
392
392
  )
393
- e3a = uint256_addmod(e3a, p[P_GAMMA], SCALAR_SIZE)
393
+ e3a = uint256_addmod(e3a, p[P_GAMMA], SCALAR_FIELD_SIZE)
394
394
 
395
395
  e3b: uint256 = uint256_addmod(
396
396
  proof[P_EVAL_B],
397
- uint256_mulmod(p[P_BETA], proof[P_EVAL_S2], SCALAR_SIZE),
398
- SCALAR_SIZE
397
+ uint256_mulmod(p[P_BETA], proof[P_EVAL_S2], SCALAR_FIELD_SIZE),
398
+ SCALAR_FIELD_SIZE
399
399
  )
400
- e3b = uint256_addmod(e3b, p[P_GAMMA], SCALAR_SIZE)
400
+ e3b = uint256_addmod(e3b, p[P_GAMMA], SCALAR_FIELD_SIZE)
401
401
 
402
- e3c: uint256 = uint256_addmod(proof[P_EVAL_C], p[P_GAMMA], SCALAR_SIZE)
402
+ e3c: uint256 = uint256_addmod(proof[P_EVAL_C], p[P_GAMMA], SCALAR_FIELD_SIZE)
403
403
 
404
- e3: uint256 = uint256_mulmod(uint256_mulmod(e3a, e3b, SCALAR_SIZE), e3c, SCALAR_SIZE)
405
- e3 = uint256_mulmod(e3, proof[P_EVAL_ZW], SCALAR_SIZE)
406
- e3 = uint256_mulmod(e3, p[P_ALPHA], SCALAR_SIZE)
404
+ e3: uint256 = uint256_mulmod(uint256_mulmod(e3a, e3b, SCALAR_FIELD_SIZE), e3c, SCALAR_FIELD_SIZE)
405
+ e3 = uint256_mulmod(e3, proof[P_EVAL_ZW], SCALAR_FIELD_SIZE)
406
+ e3 = uint256_mulmod(e3, p[P_ALPHA], SCALAR_FIELD_SIZE)
407
407
 
408
- r0: uint256 = uint256_addmod(e1, SCALAR_SIZE - e2, SCALAR_SIZE)
409
- return uint256_addmod(r0, SCALAR_SIZE - e3, SCALAR_SIZE)
408
+ r0: uint256 = uint256_addmod(e1, SCALAR_FIELD_SIZE - e2, SCALAR_FIELD_SIZE)
409
+ return uint256_addmod(r0, SCALAR_FIELD_SIZE - e3, SCALAR_FIELD_SIZE)
410
410
 
411
411
 
412
412
  @pure
@@ -426,7 +426,7 @@ def _calculateD(p: uint256[P_TOTAL_SIZE], proof: uint256[24]) -> (bool, uint256[
426
426
  success: bool = True
427
427
  pd: uint256[2] = [QC_X, QC_Y]
428
428
 
429
- success, pd = self._g1_mulAccC(pd, [QM_X, QM_Y], uint256_mulmod(proof[P_EVAL_A], proof[P_EVAL_B], SCALAR_SIZE))
429
+ success, pd = self._g1_mulAccC(pd, [QM_X, QM_Y], uint256_mulmod(proof[P_EVAL_A], proof[P_EVAL_B], SCALAR_FIELD_SIZE))
430
430
  if not success:
431
431
  return (False, [0, 0])
432
432
 
@@ -443,61 +443,61 @@ def _calculateD(p: uint256[P_TOTAL_SIZE], proof: uint256[24]) -> (bool, uint256[
443
443
  return (False, [0, 0])
444
444
 
445
445
  val1: uint256 = uint256_addmod(
446
- uint256_addmod(proof[P_EVAL_A], p[P_BETA_XI], SCALAR_SIZE),
446
+ uint256_addmod(proof[P_EVAL_A], p[P_BETA_XI], SCALAR_FIELD_SIZE),
447
447
  p[P_GAMMA],
448
- SCALAR_SIZE
448
+ SCALAR_FIELD_SIZE
449
449
  )
450
450
 
451
451
  val2: uint256 = uint256_addmod(
452
- uint256_addmod(proof[P_EVAL_B], uint256_mulmod(p[P_BETA_XI], K1, SCALAR_SIZE), SCALAR_SIZE),
452
+ uint256_addmod(proof[P_EVAL_B], uint256_mulmod(p[P_BETA_XI], K1, SCALAR_FIELD_SIZE), SCALAR_FIELD_SIZE),
453
453
  p[P_GAMMA],
454
- SCALAR_SIZE
454
+ SCALAR_FIELD_SIZE
455
455
  )
456
456
 
457
457
  val3: uint256 = uint256_addmod(
458
- uint256_addmod(proof[P_EVAL_C], uint256_mulmod(p[P_BETA_XI], K2, SCALAR_SIZE), SCALAR_SIZE),
458
+ uint256_addmod(proof[P_EVAL_C], uint256_mulmod(p[P_BETA_XI], K2, SCALAR_FIELD_SIZE), SCALAR_FIELD_SIZE),
459
459
  p[P_GAMMA],
460
- SCALAR_SIZE
460
+ SCALAR_FIELD_SIZE
461
461
  )
462
462
 
463
463
  d2a: uint256 = uint256_mulmod(
464
- uint256_mulmod(uint256_mulmod(val1, val2, SCALAR_SIZE), val3, SCALAR_SIZE),
464
+ uint256_mulmod(uint256_mulmod(val1, val2, SCALAR_FIELD_SIZE), val3, SCALAR_FIELD_SIZE),
465
465
  p[P_ALPHA],
466
- SCALAR_SIZE
466
+ SCALAR_FIELD_SIZE
467
467
  )
468
468
 
469
- d2b: uint256 = uint256_mulmod(p[P_EVAL_L1], p[P_ALPHA2], SCALAR_SIZE)
469
+ d2b: uint256 = uint256_mulmod(p[P_EVAL_L1], p[P_ALPHA2], SCALAR_FIELD_SIZE)
470
470
 
471
471
  mP: uint256[2] = [0, 0]
472
472
 
473
473
  success, mP = self._ecmul(
474
474
  [proof[P_Z], proof[P_Z + 1]],
475
- uint256_addmod(uint256_addmod(d2a, d2b, SCALAR_SIZE), p[P_U], SCALAR_SIZE)
475
+ uint256_addmod(uint256_addmod(d2a, d2b, SCALAR_FIELD_SIZE), p[P_U], SCALAR_FIELD_SIZE)
476
476
  )
477
477
  if not success:
478
478
  return (False, [0, 0])
479
479
 
480
480
  val1 = uint256_addmod(
481
- uint256_addmod(proof[P_EVAL_A], uint256_mulmod(p[P_BETA], proof[P_EVAL_S1], SCALAR_SIZE), SCALAR_SIZE),
481
+ uint256_addmod(proof[P_EVAL_A], uint256_mulmod(p[P_BETA], proof[P_EVAL_S1], SCALAR_FIELD_SIZE), SCALAR_FIELD_SIZE),
482
482
  p[P_GAMMA],
483
- SCALAR_SIZE
483
+ SCALAR_FIELD_SIZE
484
484
  )
485
485
 
486
486
  val2 = uint256_addmod(
487
- uint256_addmod(proof[P_EVAL_B], uint256_mulmod(p[P_BETA], proof[P_EVAL_S2], SCALAR_SIZE), SCALAR_SIZE),
487
+ uint256_addmod(proof[P_EVAL_B], uint256_mulmod(p[P_BETA], proof[P_EVAL_S2], SCALAR_FIELD_SIZE), SCALAR_FIELD_SIZE),
488
488
  p[P_GAMMA],
489
- SCALAR_SIZE
489
+ SCALAR_FIELD_SIZE
490
490
  )
491
491
 
492
492
  val3 = uint256_mulmod(
493
- uint256_mulmod(p[P_ALPHA], p[P_BETA], SCALAR_SIZE),
493
+ uint256_mulmod(p[P_ALPHA], p[P_BETA], SCALAR_FIELD_SIZE),
494
494
  proof[P_EVAL_ZW],
495
- SCALAR_SIZE
495
+ SCALAR_FIELD_SIZE
496
496
  )
497
497
 
498
498
  success, mP = self._ecmul(
499
499
  [proof[P_Z], proof[P_Z + 1]],
500
- uint256_addmod(uint256_addmod(d2a, d2b, SCALAR_SIZE), p[P_U], SCALAR_SIZE)
500
+ uint256_addmod(uint256_addmod(d2a, d2b, SCALAR_FIELD_SIZE), p[P_U], SCALAR_FIELD_SIZE)
501
501
  )
502
502
  if not success:
503
503
  return (False, [0, 0])
@@ -508,9 +508,9 @@ def _calculateD(p: uint256[P_TOTAL_SIZE], proof: uint256[24]) -> (bool, uint256[
508
508
 
509
509
  success, mP = self._ecmul(
510
510
  [S3_X, S3_Y],
511
- uint256_mulmod(uint256_mulmod(val1, val2, SCALAR_SIZE), val3, SCALAR_SIZE)
511
+ uint256_mulmod(uint256_mulmod(val1, val2, SCALAR_FIELD_SIZE), val3, SCALAR_FIELD_SIZE)
512
512
  )
513
- mP[1] = (BASE_SIZE - mP[1]) % BASE_SIZE
513
+ mP[1] = (BASE_FIELD_SIZE - mP[1]) % BASE_FIELD_SIZE
514
514
 
515
515
  success, pd = self._ecadd(pd, mP)
516
516
  if not success:
@@ -521,13 +521,13 @@ def _calculateD(p: uint256[P_TOTAL_SIZE], proof: uint256[24]) -> (bool, uint256[
521
521
  if not success:
522
522
  return (False, [0, 0])
523
523
 
524
- xin2: uint256 = uint256_mulmod(p[P_XIN], p[P_XIN], SCALAR_SIZE)
524
+ xin2: uint256 = uint256_mulmod(p[P_XIN], p[P_XIN], SCALAR_FIELD_SIZE)
525
525
  success, pd2 = self._g1_mulAccC(pd2, [proof[P_T3], proof[P_T3 + 1]], xin2)
526
526
  if not success:
527
527
  return (False, [0, 0])
528
528
 
529
529
  success, mP = self._ecmul(pd2, p[P_ZH])
530
- mP[1] = (BASE_SIZE - mP[1]) % BASE_SIZE
530
+ mP[1] = (BASE_FIELD_SIZE - mP[1]) % BASE_FIELD_SIZE
531
531
 
532
532
  return self._ecadd(pd, mP)
533
533
 
@@ -560,14 +560,14 @@ def _calculateF(p: uint256[P_TOTAL_SIZE], proof: uint256[24]) -> (bool, uint256[
560
560
  @pure
561
561
  @internal
562
562
  def _calculateE(p: uint256[P_TOTAL_SIZE], proof: uint256[24]) -> (bool, uint256[2]):
563
- s: uint256 = (SCALAR_SIZE - p[P_EVAL_R0]) % SCALAR_SIZE
563
+ s: uint256 = (SCALAR_FIELD_SIZE - p[P_EVAL_R0]) % SCALAR_FIELD_SIZE
564
564
 
565
- s = uint256_addmod(s, uint256_mulmod(proof[P_EVAL_A], p[P_V1], SCALAR_SIZE), SCALAR_SIZE)
566
- s = uint256_addmod(s, uint256_mulmod(proof[P_EVAL_B], p[P_V2], SCALAR_SIZE), SCALAR_SIZE)
567
- s = uint256_addmod(s, uint256_mulmod(proof[P_EVAL_C], p[P_V3], SCALAR_SIZE), SCALAR_SIZE)
568
- s = uint256_addmod(s, uint256_mulmod(proof[P_EVAL_S1], p[P_V4], SCALAR_SIZE), SCALAR_SIZE)
569
- s = uint256_addmod(s, uint256_mulmod(proof[P_EVAL_S2], p[P_V5], SCALAR_SIZE), SCALAR_SIZE)
570
- s = uint256_addmod(s, uint256_mulmod(proof[P_EVAL_ZW], p[P_U], SCALAR_SIZE), SCALAR_SIZE)
565
+ s = uint256_addmod(s, uint256_mulmod(proof[P_EVAL_A], p[P_V1], SCALAR_FIELD_SIZE), SCALAR_FIELD_SIZE)
566
+ s = uint256_addmod(s, uint256_mulmod(proof[P_EVAL_B], p[P_V2], SCALAR_FIELD_SIZE), SCALAR_FIELD_SIZE)
567
+ s = uint256_addmod(s, uint256_mulmod(proof[P_EVAL_C], p[P_V3], SCALAR_FIELD_SIZE), SCALAR_FIELD_SIZE)
568
+ s = uint256_addmod(s, uint256_mulmod(proof[P_EVAL_S1], p[P_V4], SCALAR_FIELD_SIZE), SCALAR_FIELD_SIZE)
569
+ s = uint256_addmod(s, uint256_mulmod(proof[P_EVAL_S2], p[P_V5], SCALAR_FIELD_SIZE), SCALAR_FIELD_SIZE)
570
+ s = uint256_addmod(s, uint256_mulmod(proof[P_EVAL_ZW], p[P_U], SCALAR_FIELD_SIZE), SCALAR_FIELD_SIZE)
571
571
 
572
572
  return self._ecmul([G1_X, G1_Y], s)
573
573
 
@@ -591,7 +591,7 @@ def _checkPairing(p: uint256[P_TOTAL_SIZE], proof: uint256[24]) -> bool:
591
591
  return False
592
592
 
593
593
  mIn[0] = aP[0]
594
- mIn[1] = (BASE_SIZE - aP[1]) % BASE_SIZE
594
+ mIn[1] = (BASE_FIELD_SIZE - aP[1]) % BASE_FIELD_SIZE
595
595
 
596
596
  # [X]_2
597
597
  mIn[2] = X2_X2
@@ -607,8 +607,8 @@ def _checkPairing(p: uint256[P_TOTAL_SIZE], proof: uint256[24]) -> bool:
607
607
  mIn[6] = mP[0]
608
608
  mIn[7] = mP[1]
609
609
 
610
- s: uint256 = uint256_mulmod(p[P_U], p[P_XI], SCALAR_SIZE)
611
- s = uint256_mulmod(s, W1, SCALAR_SIZE)
610
+ s: uint256 = uint256_mulmod(p[P_U], p[P_XI], SCALAR_FIELD_SIZE)
611
+ s = uint256_mulmod(s, W1, SCALAR_FIELD_SIZE)
612
612
 
613
613
  success, mP = self._ecmul([proof[P_WX_IW], proof[P_WX_IW + 1]], s)
614
614
  if not success:
@@ -622,7 +622,7 @@ def _checkPairing(p: uint256[P_TOTAL_SIZE], proof: uint256[24]) -> bool:
622
622
  if not success:
623
623
  return False
624
624
 
625
- p[P_E + 1] = (BASE_SIZE - p[P_E + 1]) % BASE_SIZE
625
+ p[P_E + 1] = (BASE_FIELD_SIZE - p[P_E + 1]) % BASE_FIELD_SIZE
626
626
 
627
627
  success, aP = self._ecadd(aP, [p[P_E], p[P_E + 1]])
628
628
  if not success:
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@solarity/zkit",
3
- "version": "0.3.2",
3
+ "version": "0.3.4",
4
4
  "license": "MIT",
5
5
  "author": "Distributed Lab",
6
6
  "readme": "README.md",
package/src/constants.ts CHANGED
@@ -1 +1,3 @@
1
1
  export const BN128_CURVE_NAME = "bn128";
2
+
3
+ export const MAX_FILE_NAME_LENGTH = 255;
@@ -2,11 +2,14 @@ import fs from "fs";
2
2
  import path from "path";
3
3
  import * as os from "os";
4
4
  import * as snarkjs from "snarkjs";
5
+ import { createHash } from "crypto";
5
6
 
6
7
  import { ArtifactsFileType, CircuitZKitConfig, VerifierLanguageType } from "../types/circuit-zkit";
7
8
  import { Signals } from "../types/proof-utils";
8
9
  import { CalldataByProtocol, IProtocolImplementer, ProofStructByProtocol, ProvingSystemType } from "../types/protocols";
9
10
 
11
+ import { MAX_FILE_NAME_LENGTH } from "../constants";
12
+
10
13
  /**
11
14
  * `CircuitZKit` represents a single circuit and provides a high-level API to work with it.
12
15
  */
@@ -20,15 +23,32 @@ export class CircuitZKit<Type extends ProvingSystemType> {
20
23
  * Creates a verifier contract for the specified contract language with optional name suffix.
21
24
  * For more details regarding the structure of the contract verifier name, see {@link getVerifierName} description.
22
25
  *
26
+ * In case the length of the verifier filename exceeds the {@link MAX_FILE_NAME_LENGTH},
27
+ * the `verifierNameSuffix` will be replaced by the first four bytes of its `sha1` hash.
28
+ *
29
+ * If no suffix was passed, but the verifier's filename still exceeds {@link MAX_FILE_NAME_LENGTH}, an error will be thrown.
30
+ *
23
31
  * @param {VerifierLanguageType} languageExtension - The verifier contract language extension.
24
32
  * @param {string} verifierNameSuffix - The optional verifier name suffix.
25
33
  */
26
34
  public async createVerifier(languageExtension: VerifierLanguageType, verifierNameSuffix?: string): Promise<void> {
27
35
  const vKeyFilePath: string = this.mustGetArtifactsFilePath("vkey");
28
- const verifierFilePath = path.join(
29
- this._config.verifierDirPath,
30
- `${this.getVerifierName(verifierNameSuffix)}.${languageExtension}`,
31
- );
36
+
37
+ let verifierFileName: string = `${this.getVerifierName(verifierNameSuffix)}.${languageExtension}`;
38
+
39
+ if (verifierFileName.length >= MAX_FILE_NAME_LENGTH) {
40
+ const modifiedSuffix: string = verifierNameSuffix
41
+ ? `_0x${createHash("sha1").update(verifierNameSuffix).digest("hex").slice(0, 8)}_`
42
+ : "";
43
+
44
+ verifierFileName = `${this.getVerifierName(modifiedSuffix)}.${languageExtension}`;
45
+
46
+ if (verifierFileName.length >= MAX_FILE_NAME_LENGTH) {
47
+ throw new Error(`Verifier file name "${verifierFileName}" exceeds the maximum file name length`);
48
+ }
49
+ }
50
+
51
+ const verifierFilePath = path.join(this._config.verifierDirPath, verifierFileName);
32
52
 
33
53
  this._implementer.createVerifier(vKeyFilePath, verifierFilePath, languageExtension);
34
54
  }
@@ -5,6 +5,9 @@
5
5
  pragma solidity >=0.7.0 <0.9.0;
6
6
 
7
7
  contract <%=verifier_id%> {
8
+ // @dev scalar field size
9
+ uint256 public constant SCALAR_FIELD_SIZE =
10
+ 21888242871839275222246405745257275088548364400416034343698204186575808495617;
8
11
  /// @dev base field size
9
12
  uint256 public constant BASE_FIELD_SIZE =
10
13
  21888242871839275222246405745257275088696311157297823662689037894645226208583;
@@ -57,7 +60,7 @@ contract <%=verifier_id%> {
57
60
  ) public view returns (bool verified_) {
58
61
  assembly {
59
62
  function checkField(signal_) -> res_ {
60
- res_ := lt(signal_, BASE_FIELD_SIZE)
63
+ res_ := lt(signal_, SCALAR_FIELD_SIZE)
61
64
  }
62
65
 
63
66
  function g1MulAdd(pR_, x_, y_, s_) -> res_ {
@@ -2,6 +2,8 @@
2
2
 
3
3
  # AUTOGENERATED FILE BY HARDHAT-ZKIT. DO NOT EDIT.
4
4
 
5
+ # @dev scalar field size
6
+ SCALAR_FIELD_SIZE: constant(uint256) = 21888242871839275222246405745257275088548364400416034343698204186575808495617
5
7
  # @dev base field size
6
8
  BASE_FIELD_SIZE: constant(uint256) = 21888242871839275222246405745257275088696311157297823662689037894645226208583
7
9
 
@@ -39,7 +41,7 @@ EC_PAIRING_PRECOMPILED_ADDRESS: constant(address) = 0x00000000000000000000000000
39
41
  def verifyProof(pointA: uint256[2], pointB: uint256[2][2], pointC: uint256[2], publicSignals: uint256[<%=IC.length - 1%>]) -> bool:
40
42
  # @dev check that all public signals are in F
41
43
  for signal: uint256 in publicSignals:
42
- if signal >= BASE_FIELD_SIZE:
44
+ if signal >= SCALAR_FIELD_SIZE:
43
45
  return False
44
46
 
45
47
  return self._checkPairing(pointA, pointB, pointC, publicSignals)