ZK Functions

Synchronous Groth16 proof generation and verification using snarkjs under the hood.

Overview

The ZK module provides a clean, synchronous API for Groth16 zk-SNARK operations. It internally handles all snarkjs operations using the current Node.js process, keeping the library self-contained and easy to use.

Requirements

  • All inputs must be provided as binary data (Buffer/Uint8Array)

  • No external runner scripts or additional setup required

API Reference

generateProof(circuit, zkey, inputs, options?)

Generates a Groth16 proof.

Parameters:

  • circuit (Buffer | Uint8Array): Required - The circuit WASM binary data

  • zkey (Buffer | Uint8Array): Required - The proving key binary data

  • inputs (Record<string, unknown>): Required - All circuit inputs (public and private)

  • options? (GenerateProofOptions): Optional configuration

    • timeout? (number): Custom timeout in milliseconds (default: 300000)

Returns: Groth16ProveResult - Object containing proof and publicSignals

Example:

verifyProof(vkey, proof, publicSignals)

Verifies a Groth16 proof.

Parameters:

  • vkey (Record<string, unknown>): Required - The verification key object

  • proof (Record<string, unknown>): Required - The proof object to verify

  • publicSignals (unknown[]): Required - The public signals array

Returns: boolean - true if verification succeeds, false otherwise

Example:

serializeProof(data, format?)

Serializes a proof result into various formats.

Parameters:

  • data (Groth16ProveResult): The proof result object

  • format? (Groth16SerializeFormat): Output format (default: 'buffer')

Returns: Buffer | string - Serialized proof data

Formats:

  • 'buffer': Returns a Buffer

  • 'base64': Returns a base64-encoded string

  • 'hex': Returns a hex-encoded string

  • 'json': Returns a JSON string

Example:

deserializeProof(input)

Deserializes proof data back into a proof result object.

Parameters:

  • input (Buffer | string): Serialized proof data (automatically detects format)

Returns: Groth16ProveResult - The reconstructed proof result

Example:

Internal Implementation

The library internally handles all snarkjs operations using the current Node.js process:

  • Proof Generation: Uses snarkjs.groth16.fullProve() with temporary file management

  • Proof Verification: Uses snarkjs.groth16.verify() with direct data passing

  • File Handling: Automatically creates and cleans up temporary files

  • Error Handling: Comprehensive error handling with timeout protection

  • Memory Management: Efficient memory usage with automatic cleanup

Complete Example

Notes

  • All functions are synchronous and do not require await

  • The library handles temporary file creation and cleanup automatically

  • Timeout handling prevents hanging operations (default: 5 minutes)

  • Input validation ensures data integrity

  • No external runner scripts required - everything is self-contained

  • Automatic memory management with efficient cleanup

  • Support for both small (verify) and large (prove) data operations

Last updated