Skip to content

Commit

Permalink
fixup code for changed path, minor cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Vindaar committed Aug 14, 2024
1 parent 282c138 commit fdca197
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 36 deletions.
31 changes: 15 additions & 16 deletions constantine/proof_systems/groth16_utils.nim
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import ../../math/[arithmetic, extension_fields]
import ../../math/io/[io_bigints, io_fields, io_ec, io_extfields]
import ../../platforms/abstractions
import ../../named/[algebras, properties_fields, properties_curves]
import ../../math/elliptic/[ec_shortweierstrass_affine, ec_shortweierstrass_jacobian, ec_scalar_mul, ec_multi_scalar_mul, ec_scalar_mul_vartime]
import ../math/[arithmetic, extension_fields],
../math/io/[io_bigints, io_fields, io_ec, io_extfields],
../platforms/abstractions,
../named/[algebras, properties_fields, properties_curves],
../math/elliptic/[ec_shortweierstrass_affine, ec_shortweierstrass_jacobian, ec_scalar_mul, ec_multi_scalar_mul, ec_scalar_mul_vartime]

## NOTE: These constructors for ...
## Helper constructors for Fp / Fr elements used in Groth16 binary file parsers.
proc toFp*[Name: static Algebra](x: seq[byte], isMont = true): Fp[Name] =
let b = matchingBigInt(Name).unmarshal(x.toOpenArray(0, x.len - 1), littleEndian)
if isMont:
Expand Down Expand Up @@ -46,16 +46,6 @@ proc toEcG2*[Name: static Algebra](s: seq[byte]): EC_ShortW_Aff[Fp2[Name], G2] =
if not bool(result.isNeutral()):
doAssert isOnCurve(result.x, result.y, G2).bool, "Input point is not on curve!"

## Currently not used
proc randomFieldElement*[Name: static Algebra](_: typedesc[Fr[Name]]): Fr[Name] =
## random element in ~Fr[Name]~
let m = Fr[Name].getModulus()
var b: matchingOrderBigInt(Name)

while b.isZero().bool or (b > m).bool: ## XXX: or just truncate?
assert b.limbs.sysrand()
result.fromBig(b)

proc asEC*[Name: static Algebra](pts: seq[seq[byte]], _: typedesc[Fp[Name]]): seq[EC_ShortW_Aff[Fp[Name], G1]] =
result = newSeq[EC_ShortW_Aff[Fp[Name], G1]](pts.len)
for i, el in pts:
Expand All @@ -65,3 +55,12 @@ proc asEC2*[Name: static Algebra](pts: seq[seq[byte]], _: typedesc[Fp2[Name]]):
result = newSeq[EC_ShortW_Aff[Fp2[Name], G2]](pts.len)
for i, el in pts:
result[i] = toEcG2[Name](el)

proc randomFieldElement*[Name: static Algebra](_: typedesc[Fr[Name]]): Fr[Name] =
## random element in ~Fr[Name]~
let m = Fr[Name].getModulus()
var b: matchingOrderBigInt(Name)

while b.isZero().bool or (b > m).bool: ## XXX: or just truncate?
assert b.limbs.sysrand()
result.fromBig(b)
31 changes: 11 additions & 20 deletions constantine/proof_systems/manual_groth16.nim
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import ./r1cs_circom_parser,
./zkey_binary_parser,
./wtns_binary_parser
import ./constraint_systems/r1cs_circom_parser,
./constraint_systems/zkey_binary_parser,
./constraint_systems/wtns_binary_parser

import ../../math/[arithmetic, extension_fields]
import ../../math/io/[io_bigints, io_fields, io_ec, io_extfields]
import ../../platforms/abstractions
import ../../named/[algebras, properties_fields, properties_curves]
import ../../math/elliptic/[ec_shortweierstrass_affine, ec_shortweierstrass_jacobian, ec_scalar_mul, ec_multi_scalar_mul, ec_scalar_mul_vartime]
import ../../named/zoo_generators
import ../../csprngs/sysrand
import ../math/[arithmetic, extension_fields],
../math/io/[io_bigints, io_fields, io_ec, io_extfields],
../platforms/abstractions,
../named/[algebras, properties_fields, properties_curves],
../math/elliptic/[ec_shortweierstrass_affine, ec_shortweierstrass_jacobian, ec_scalar_mul, ec_multi_scalar_mul, ec_scalar_mul_vartime],
../named/zoo_generators,
../csprngs/sysrand

import ../../math/polynomials/[fft_fields, fft_lut]
import ../math/polynomials/[fft_fields, fft_lut]

from std / math import log2

Expand All @@ -26,15 +26,6 @@ type
r: Fr[Name]
s: Fr[Name]

proc randomFieldElement[Name: static Algebra](_: typedesc[Fr[Name]]): Fr[Name] =
## random element in ~Fp[Name]~
let m = Fr[Name].getModulus()
var b: matchingOrderBigInt(Name)

while b.isZero().bool or (b > m).bool: ## XXX: or just truncate?
assert b.limbs.sysrand()
result.fromBig(b)

proc init*[Name: static Algebra](G: typedesc[Groth16Prover[Name]], zkey: Zkey[Name], wtns: Wtns[Name], r1cs: R1CS): Groth16Prover[Name] =
result = Groth16Prover[Name](
zkey: zkey,
Expand Down

0 comments on commit fdca197

Please sign in to comment.