From c583af075551311e677ed010b1eb608995bab5d3 Mon Sep 17 00:00:00 2001 From: Dmitriy Ryajov Date: Mon, 13 Nov 2023 11:25:36 -0600 Subject: [PATCH] move things around and add results support --- example/example.nim | 21 +++++++++++-------- groth16.nim | 29 ++++++++++++++++++++++++++ tests/test_proof.nim | 49 -------------------------------------------- 3 files changed, 42 insertions(+), 57 deletions(-) create mode 100644 groth16.nim delete mode 100644 tests/test_proof.nim diff --git a/example/example.nim b/example/example.nim index 7112fcf..a6376b6 100644 --- a/example/example.nim +++ b/example/example.nim @@ -1,11 +1,16 @@ -import pkg/groth16 -import ../tests/test_proof -import ../src/export_json +import pkg/results +import ../groth16 -let zkey_fname : string = "./build/product.zkey" -let wtns_fname : string = "./build/product.wtns" -let proof = testProveAndVerify( zkey_fname, wtns_fname) +proc main(): Result[void, cstring] = + let zkey_fname : string = "./build/product.zkey" + let wtns_fname : string = "./build/product.wtns" + let proof = ? proveAndVerify( zkey_fname, wtns_fname) -exportPublicIO( "./build/nim_public.json" , proof ) -exportProof( "./build/nim_proof.json" , proof ) + exportPublicIO( "./build/nim_public.json" , proof ) + exportProof( "./build/nim_proof.json" , proof ) + + ok() + +if main().isErr: + raiseAssert "Error verifying proof" diff --git a/groth16.nim b/groth16.nim new file mode 100644 index 0000000..ccccf16 --- /dev/null +++ b/groth16.nim @@ -0,0 +1,29 @@ +import pkg/results + +import pkg/groth16 +import pkg/witness +import pkg/zkey +import pkg/zkey_types +import pkg/export_json + +export groth16, witness, zkey, zkey_types, export_json + +#------------------------------------------------------------------------------- + +proc proveAndVerify*( zkey_fname, wtns_fname: string): Result[Proof, cstring] = + debugEcho("parsing witness & zkey files...") + let witness = parseWitness( wtns_fname) + let zkey = parseZKey( zkey_fname) + + debugEcho("generating proof...") + let vkey = extractVKey( zkey) + let proof = generateProof( zkey, witness ) + + debugEcho("verifying the proof...") + if verifyProof( vkey, proof): + debugEcho("verification succeeded") + ok proof + else: + err "verification failed" + +#------------------------------------------------------------------------------- diff --git a/tests/test_proof.nim b/tests/test_proof.nim deleted file mode 100644 index 01b847b..0000000 --- a/tests/test_proof.nim +++ /dev/null @@ -1,49 +0,0 @@ - -import pkg/groth16 -import pkg/witness -import pkg/zkey -import pkg/zkey_types - -#------------------------------------------------------------------------------- - -proc testProveAndVerify*( zkey_fname, wtns_fname: string): Proof = - - echo("parsing witness & zkey files...") - let witness = parseWitness( wtns_fname) - let zkey = parseZKey( zkey_fname) - - # printCoeffs(zkey.coeffs) - - echo("generating proof...") - let vkey = extractVKey( zkey) - let proof = generateProof( zkey, witness ) - - echo("verifying the proof...") - let ok = verifyProof( vkey, proof ) - echo("verification succeeded = ",ok) - - return proof - -#------------------------------------------------------------------------------- - -proc testFakeSetupAndVerify*( r1cs_fname, wtns_fname: string, flavour=Snarkjs): Proof = - echo("trusted setup flavour = ",flavour) - - echo("parsing witness & r1cs files...") - let witness = parseWitness( wtns_fname) - let r1cs = parseR1CS( r1cs_fname) - - echo("performing fake trusted setup...") - let zkey = createFakeCircuitSetup( r1cs, flavour=flavour ) - - # printCoeffs(zkey.coeffs) - - echo("generating proof...") - let vkey = extractVKey( zkey) - let proof = generateProof( zkey, witness ) - - echo("verifying the proof...") - let ok = verifyProof( vkey, proof ) - echo("verification succeeded = ",ok) - - return proof