@zk-kit/groth16
v0.5.0
Published
A snippet of SnarkJS code for verifying and generating Groth16 proofs only.
Downloads
4,538
Readme
| 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:
- It only includes code to verify and generate Groth16 proofs, making your final bundle lighter.
- It doesn't call the ffjavascript
buildBn128
function if abn128
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). - It includes TS types.
- It provides an ESM bundle that is compatible with browsers. So there is no need to add any polyfill or additional configuration.
🛠 Install
npm or yarn
Install the @zk-kit/groth16
package and its peer dependencies with npm:
npm i @zk-kit/groth16
or yarn:
yarn add @zk-kit/groth16
📜 Usage
# prove(input: CircuitSignals, wasmFile: ZKArtifact, zkeyFile: ZKArtifact): Promise<{ proof: Groth16Proof publicSignals: PublicSignals }>
import { prove } from "@zk-kit/groth16"
const input = {
message: 12,
scope: 122
}
const proof = await prove(input, "./circuit.zkey", "./circuit.wasm")
console.log(proof)
/*
{
proof: {
pi_a: [
'8259885706934172848141475422209230656096448508815982888010519325096632035723',
'3142099172052192611205205328157407975469005554072266974009053708782134081166',
'1'
],
pi_b: [ [Array], [Array], [Array] ],
pi_c: [
'13863804425308906943736719856399634046638544298517159271373916818387594277305',
'21340646707244019956779928177502771923632450548108204371058275686712196195969',
'1'
],
protocol: 'groth16',
curve: 'bn128'
},
publicSignals: [
'527758365153958423212195330785598453331596731388181860789801455413116800554',
'19104626566001952573667666924569656871967113105870778077087237826253896482830',
'122'
]
}
*/
# verify(verificationKey: any, proof: { proof: Groth16Proof publicSignals: PublicSignals }): Promise<boolean>
import { verify } from "@zk-kit/groth16"
import verificationKey from "./circuit.json"
const response = await verify(verificationKey, proof)
console.log(response) // true
# buildBn128(): Promise<any>
import { buildBn128 } from "@zk-kit/groth16"
const curve = await buildBn128() // WasmField1
// https://github.com/iden3/ffjavascript/blob/master/src/wasm_field1.js