Skip to content

Commit 47ba6f9

Browse files
committed
[tests] clean up Groth16 test case, adjust for correct API
1 parent 92108bf commit 47ba6f9

File tree

1 file changed

+30
-15
lines changed

1 file changed

+30
-15
lines changed

tests/proof_systems/t_groth16_prover.nim

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,39 @@
1-
import std/[os, unittest, strutils],
2-
constantine/proof_systems/manual_groth16,
1+
import std/[os, unittest, strutils, importutils],
2+
constantine/proof_systems/groth16 {.all.}, # to call `calc*` procs
33
constantine/named/algebras
44

5+
6+
7+
58
#[
69
For information about the data files used in this test case, see
710
`examples/groth16_prover.org`.
811
]#
912

13+
proc proveManual[Name: static Algebra](ctx: var Groth16Prover[Name],
14+
r, s: Fr[Name]): tuple[A: EC_ShortW_Aff[Fp[Name], G1],
15+
B: EC_ShortW_Aff[Fp2[Name], G2],
16+
C: EC_ShortW_Aff[Fp[Name], G1]] {.noinit.} =
17+
## Helper function for a "manual" Groth16 proof so that we can overwrite
18+
## the `r` and `s` parameters to compare with a SnarkJS proof.
19+
##
20+
## Identical implementation to `groth16.prove`, but sets `r` and `s` to inputs.
21+
22+
# 1. Sample the random field elements `r` and `s` for the proof
23+
privateAccess(ctx.type)
24+
ctx.r = r
25+
ctx.s = s
26+
# 2. get the witness data needed for all proof elements
27+
let wt = ctx.wtns.witnesses
28+
# 3. compute the individual proof elements
29+
let A_p = ctx.calcAp(wt)
30+
let B2_p = ctx.calcBp(wt)
31+
let B1_p = ctx.calcB1(wt)
32+
let C_p = ctx.calcCp(A_p, B1_p, wt)
33+
34+
result = (A: A_p.getAffine(), B: B2_p.getAffine(), C: C_p.getAffine())
35+
36+
1037
suite "Groth16 prover":
1138
test "Proving 3-factorization example":
1239
const T = BN254_Snarks
@@ -34,12 +61,6 @@ suite "Groth16 prover":
3461
let r = toFr[BN254_Snarks](rSJ)
3562
# and `s`
3663
let s = toFr[BN254_Snarks](sSJ)
37-
# overwrite context's random values
38-
ctx.r = r
39-
ctx.s = s
40-
41-
echo "r = ", ctx.r.toHex()
42-
echo "s = ", ctx.s.toHex()
4364

4465
# expected values produced by SnarkJS with these `r`, `s` values
4566
# x/y coordinates of Fp point on G1 subgroup of EC, corresponding to `g^A_1`
@@ -77,7 +98,7 @@ suite "Groth16 prover":
7798
let cExp = toECG1(cx, cy)
7899

79100
# call the proof and...
80-
let (A_p, B2_p, C_p) = ctx.prove()
101+
let (A_p, B2_p, C_p) = ctx.proveManual(r, s)
81102

82103
echo aExp.toDecimal()
83104
echo bExp.toDecimal()
@@ -93,12 +114,6 @@ suite "Groth16 prover":
93114
echo "C_p#16 = ", C_p.toHex()
94115
echo "C_p#10 = ", C_p.toDecimal()
95116

96-
#check (A_p == aExp.getJacobian).bool
97-
#check (B2_p == bExp.getJacobian).bool
98-
### XXX: C currently fails!
99-
#check (C_p == cExp.getJacobian).bool
100-
101117
check (A_p == aExp).bool
102118
check (B2_p == bExp).bool
103-
## XXX: C currently fails!
104119
check (C_p == cExp).bool

0 commit comments

Comments
 (0)