Skip to content

Commit

Permalink
move asEC*, randomFieldElement to utils
Browse files Browse the repository at this point in the history
  • Loading branch information
Vindaar committed Aug 13, 2024
1 parent f648d56 commit 030f53c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 16 deletions.
20 changes: 15 additions & 5 deletions constantine/proof_systems/constraint_systems/groth16_utils.nim
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,21 @@ proc toEcG2*[Name: static Algebra](s: seq[byte]): EC_ShortW_Aff[Fp2[Name], G2] =
doAssert isOnCurve(result.x, result.y, G2).bool, "Input point is not on curve!"

## Currently not used
proc randomFieldElement*[Name: static Algebra](): Fp[Name] =
## random element in ~Fp[T]~
let m = Fp[Name].getModulus()
var b: matchingBigInt(Name)
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:
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:
result[i] = toEcG1[Name](el)

proc asEC2*[Name: static Algebra](pts: seq[seq[byte]], _: typedesc[Fp2[Name]]): seq[EC_ShortW_Aff[Fp2[Name], G2]] =
result = newSeq[EC_ShortW_Aff[Fp2[Name], G2]](pts.len)
for i, el in pts:
result[i] = toEcG2[Name](el)
11 changes: 0 additions & 11 deletions constantine/proof_systems/constraint_systems/manual_groth16.nim
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,6 @@ proc getWitnesses[Name: static Algebra](ctx: Groth16Prover[Name]): seq[Fr[Name]]
for i, w in witnesses:
result[i] = toFr[Name](w.data, isMont = false) ## Improtant: Witness does *not* store numbers in Montgomery rep

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:
result[i] = toEcG1[Name](el)

proc asEC2[Name: static Algebra](pts: seq[seq[byte]], _: typedesc[Fp2[Name]]): seq[EC_ShortW_Aff[Fp2[Name], G2]] =
result = newSeq[EC_ShortW_Aff[Fp2[Name], G2]](pts.len)
for i, el in pts:
result[i] = toEcG2[Name](el)


proc calcAp[Name: static Algebra](ctx: Groth16Prover[Name], wt: seq[Fr[Name]]): EC_ShortW_Jac[Fp[Name], G1] =
# A_p is defined as
# A_p = α_1 + (Σ_i [W]_i · A_i) + [r] · δ_1
Expand Down

0 comments on commit 030f53c

Please sign in to comment.