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 datazkey(Buffer | Uint8Array): Required - The proving key binary datainputs(Record<string, unknown>): Required - All circuit inputs (public and private)options?(GenerateProofOptions): Optional configurationtimeout?(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 objectproof(Record<string, unknown>): Required - The proof object to verifypublicSignals(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 objectformat?(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 managementProof Verification: Uses
snarkjs.groth16.verify()with direct data passingFile 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
awaitThe 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