Bionetta Core Tools is a foundational set of tools designed to integrate complex cryptographic computations, such as zk-SNARK proof generation and verification, into web applications or environments requiring high performance.
ProverWorker
class, which facilitates the generation and verification of zk-SNARK proofs using a Web Worker. By offloading these computationally intensive tasks to a separate thread, it helps prevent the main thread from blocking, ensuring a smoother user experience.
The ProverWorker
class acts as a wrapper around a Web Worker. It provides a convenient API for interacting with the worker to perform two primary operations:
-
Generating Zk-SNARK Proofs: Creating a cryptographic proof based on a pre-compiled ZK-SNARK circuit (a
.zkey
file) and witness data. - Verifying Zk-SNARK Proofs: Verifying the authenticity and correctness of a generated proof against a public verification key and public signals.
Initializes a new instance of the ProverWorker
class.
This constructor is flexible and allows you to either:
- Reuse an existing
Worker
instance (if already created elsewhere), - Or create a new
Worker
by providing a file path (as a string orURL
) to the worker script (usually a module that usessnarkjs
under the hood).
- If
worker
is an instance ofWorker
, it is used directly. - If
worker
is astring
or aURL
, the constructor internally creates a newWorker
using:
new Worker(worker, { type: 'module' })
generateProof<TProof = Groth16Proof>(zkeyFileBytes: BufferLike, wtns: BufferLike): Promise<ZkProof<TProof>>
Generates a zero-knowledge proof by sending data to the web worker.
This method communicates with the worker to execute the groth16.prove
function using the provided .zkey
proving key and the witness file.
Name | Type | Description |
---|---|---|
zkeyFileBytes |
BufferLike |
The proving key file contents as a buffer (e.g., Uint8Array, ArrayBuffer). |
wtns |
BufferLike |
The witness buffer generated from circuit inputs. |
-
Promise<ZkProof<TProof>>
A promise that resolves to a valid zk-SNARK proof object, typed asZkProof<TProof>
. By default, this is expected to conform to theGroth16Proof
structure fromsnarkjs
.
- Internally sets up message and error event listeners on the worker.
- Sends a
postMessage
with type'prove'
, including the.zkey
and.wtns
buffers. - Awaits a response from the worker, resolving to the proof or rejecting on error.
- Cleans up the event listeners after response is received.
import { ProverWorker } from '@rarimo/bionetta-js-sdk-core'
import proofWorkerUrl from './proofWorker?worker&inline'
// Load your .zkey and .wtns files (as ArrayBuffer or Uint8Array)
const zkeyBuffer = await fetch('/circuits/myCircuit.zkey').then(r => r.arrayBuffer())
const witnessBuffer = await fetch('/circuits/myCircuit.wtns').then(r => r.arrayBuffer())
const prover = new ProverWorker(proofWorkerUrl)
const proof = await prover.generateProof(zkeyBuffer, witnessBuffer)
generateVerification(vkVerifier: any, publicSignals: PublicSignals, proof: Groth16Proof, logger?: any): Promise<boolean>
Verifies a previously generated zk-SNARK proof using the worker thread.
This method sends verification data to the worker, which should use snarkjs.groth16.verify
internally to check the validity of the proof.
Name | Type | Description |
---|---|---|
vkVerifier |
any |
The verification key object (usually from .vkey.json ). |
publicSignals |
PublicSignals |
The public signals generated during witness calculation. |
proof |
Groth16Proof |
The zk-SNARK proof previously generated by generateProof . |
logger (optional)
|
any |
Optional logger or debug object to pass into the worker (if supported). |
-
Promise<boolean>
Resolves totrue
if the proof is valid,false
otherwise. Willthrow
if an error occurs in the worker.
- Sends a
postMessage
of type'verify'
to the worker. - Includes the verification key, proof, public signals, and optional logger.
- Resolves to
true
orfalse
based on verification result. - Rejects on any error or if the worker throws.
import { ProverWorker } from '@rarimo/bionetta-js-sdk-core'
import proofWorkerUrl from './proofWorker?worker&inline'
import vkey from './vkey.json'
const prover = new ProverWorker(proofWorkerUrl)
const isValid = await prover.generateVerification(vkey, publicSignals, proof)
The WitnessWorker
class is responsible for computing the witness for a zero-knowledge proof in a separate web worker thread. It isolates heavy computation (like executing a WASM circuit) from the main thread, improving performance and responsiveness.
Creates a new instance of the WitnessWorker
.
Name | Type | Description |
---|---|---|
data |
Worker string URL
|
An already created Worker instance or a URL/path to a worker script. |
If data
is a Worker
instance, it will be used directly. Otherwise, a new Worker
is created from the given URL with { type: 'module' }
.
generateWitness<TInput>(wasmBytes: BufferLike, input: TInput, workerMessageType: string): Promise<BufferLike>
Sends the circuit's WebAssembly and input signals to the worker to compute the witness.
Name | Type | Description |
---|---|---|
wasmBytes |
BufferLike |
A binary buffer (e.g., ArrayBuffer , Uint8Array ) containing the WASM code of the circuit. |
input |
TInput |
The input object to the circuit. This must match the input signals expected by the circuit. |
workerMessageType |
string |
The type identifier for the message to instruct the worker on what to do. |
-
Promise<BufferLike>
Resolves with the computed witness in binary format (usually aUint8Array
orArrayBuffer
).
- Posts a message to the worker with
type
,wasmBytes
, andinput
. - Listens for a successful
message
event with the resulting witness. - Handles errors via the
error
event. - Automatically cleans up event listeners after completion.
import { WitnessWorker } from '@rarimo/bionetta-js-sdk-core'
import witnessWorkerUrl from './witnessWorker?worker&inline'
const wasmBytes = await fetch('/circuits/myCircuit.wasm').then(r => r.arrayBuffer())
const input = { a: 3, b: 9 }
const witnessWorker = new WitnessWorker(witnessWorkerUrl)
const witness = await witnessWorker.generateWitness(wasmBytes, input, 'init')
Initializes the camera-based face detection pipeline using a video stream and canvas layers.
This function sets up a CameraProcessor
and a FaceDetectorService
, loads the face detection model, and starts processing frames to detect and extract faces.
Takes an options
object with the following properties:
Name | Type | Description |
---|---|---|
videoElement |
HTMLVideoElement |
The video DOM element displaying the camera feed. |
videoCanvas |
HTMLCanvasElement |
Canvas used to render raw video frames for processing. |
overlayCanvas |
HTMLCanvasElement |
Canvas to draw detection overlays (e.g., face boxes, landmarks). |
faceCanvas |
HTMLCanvasElement |
Canvas where the resized detected face is drawn for later usage. |
resizeWidth |
number |
The target width to which the detected face should be resized. |
onFaceResized |
(imageData: ImageData) => Promise<void> |
Callback called with resized face image data after each detection. |
- Initializes a
FaceDetectorService
and loads its model (TensorFlow.js). - Creates a
CameraProcessor
which:- Captures video frames from the
videoElement
. - Draws those frames to
videoCanvas
. - Detects faces using
faceDetector
. - Draws results to
overlayCanvas
. - Extracts and resizes the face to
resizeWidth
and draws it onfaceCanvas
. - Sends the resized face data to
onFaceResized
.
- Captures video frames from the
-
Promise<CameraProcessor>
Resolves with the activeCameraProcessor
instance that you can control (e.g., stop or pause).
import { initFace } from '@rarimo/bionetta-js-sdk-core'
const processor = await initFace({
videoElement: document.getElementById('video') as HTMLVideoElement,
videoCanvas: document.getElementById('video-canvas') as HTMLCanvasElement,
overlayCanvas: document.getElementById('overlay') as HTMLCanvasElement,
faceCanvas: document.getElementById('face') as HTMLCanvasElement,
resizeWidth: 224,
onFaceResized: async (imageData) => {
console.log('Received resized face data', imageData)
},
})