@rarimo/bionetta-js-sdk-core
TypeScript icon, indicating that this package has built-in type declarations

0.1.2 • Public • Published

Bionetta core tools

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.

Overview


ProverWorker

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:

  1. Generating Zk-SNARK Proofs: Creating a cryptographic proof based on a pre-compiled ZK-SNARK circuit (a .zkey file) and witness data.
  2. Verifying Zk-SNARK Proofs: Verifying the authenticity and correctness of a generated proof against a public verification key and public signals.

new ProverWorker(worker: Worker | string | URL)

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 or URL) to the worker script (usually a module that uses snarkjs under the hood).

Behavior

  • If worker is an instance of Worker, it is used directly.
  • If worker is a string or a URL, the constructor internally creates a new Worker using:
new Worker(worker, { type: 'module' })

generateProof

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.


Parameters

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.

Returns

  • Promise<ZkProof<TProof>>
    A promise that resolves to a valid zk-SNARK proof object, typed as ZkProof<TProof>. By default, this is expected to conform to the Groth16Proof structure from snarkjs.

Behavior

  • 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.

Example

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

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.


Parameters

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).

Returns

  • Promise<boolean>
    Resolves to true if the proof is valid, false otherwise. Will throw if an error occurs in the worker.

Behavior

  • Sends a postMessage of type 'verify' to the worker.
  • Includes the verification key, proof, public signals, and optional logger.
  • Resolves to true or false based on verification result.
  • Rejects on any error or if the worker throws.

Example

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)

WitnessWorker

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.


Constructor

new WitnessWorker(data: Worker | string | URL)

Creates a new instance of the WitnessWorker.

Parameters

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

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.


Parameters

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.

Returns

  • Promise<BufferLike>
    Resolves with the computed witness in binary format (usually a Uint8Array or ArrayBuffer).

Behavior

  • Posts a message to the worker with type, wasmBytes, and input.
  • Listens for a successful message event with the resulting witness.
  • Handles errors via the error event.
  • Automatically cleans up event listeners after completion.

Example

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')

initFace

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.


Parameters

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.

Behavior

  1. Initializes a FaceDetectorService and loads its model (TensorFlow.js).
  2. 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 on faceCanvas.
    • Sends the resized face data to onFaceResized.

Returns

  • Promise<CameraProcessor>
    Resolves with the active CameraProcessor instance that you can control (e.g., stop or pause).

Example

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)
  },
})

Readme

Keywords

none

Package Sidebar

Install

npm i @rarimo/bionetta-js-sdk-core

Weekly Downloads

1

Version

0.1.2

License

MIT

Unpacked Size

190 kB

Total Files

43

Last publish

Collaborators

  • volendi
  • lukachi
  • ihor.diachenko
  • arvolear
  • ardier16