A snippet of SnarkJS code for verifying and generating Groth16 proofs only.
[!WARNING]
This package is no longer maintained as SnarkJS has integrated most of the above optimizations. Please, consider installing it instead.
This package contains SnarkJS functions for generating and verifying zero knowledge proofs with Groth16 specifically. In addition to the original code it also uses the cached bn128 curve if it already exists, making verification and generation of consecutive proofs faster. |
---|
Some advantages of using this package instead of snarkjs
directly are:
buildBn128
function if a bn128
cached curve already exists, making verification and generation of consecutive proofs much faster (e.g. verification seems to be ~9 times faster after the first one).Install the @zk-kit/groth16
package and its peer dependencies with npm:
npm i @zk-kit/groth16
or yarn:
yarn add @zk-kit/groth16
[!WARNING]
You will need to provide your own circuits here in your specified path. Remember to define your circuit input and rename the files accordingly.
import { prove, verify, buildBn128 } from "@zk-kit/groth16"
// Build the BN128 curve for Groth16.
// https://github.com/iden3/ffjavascript/blob/master/src/wasm_field1.js
await buildBn128() // WasmField1
// Define your circuit input.
// const input = {
// input1: 1,
// input2: 2,
// inputN: "N"
// }
// Compute the proof.
const proof = await prove(input, "<YOUR-PATH>/circuit.zkey", "<YOUR-PATH>/circuit.wasm")
/*
{
proof: {
pi_a: [
'8259885706934172848141475422209230656096448508815982888010519325096632035723',
'3142099172052192611205205328157407975469005554072266974009053708782134081166',
'1'
],
pi_b: [ [Array], [Array], [Array] ],
pi_c: [
'13863804425308906943736719856399634046638544298517159271373916818387594277305',
'21340646707244019956779928177502771923632450548108204371058275686712196195969',
'1'
],
protocol: 'groth16',
curve: 'bn128'
},
publicSignals: [
'527758365153958423212195330785598453331596731388181860789801455413116800554',
'19104626566001952573667666924569656871967113105870778077087237826253896482830',
'122'
]
}
*/
console.log(proof)
// Verify the proof.
const response = await verify("<YOUR-PATH>/circuit_verification_key.json", proof)
// true or false.
console.log(response)