Introducing Socket Firewall: Free, Proactive Protection for Your Software Supply Chain.Learn More
Socket
Book a DemoInstallSign in
Socket

@rarimo/bionetta-js-sdk-core

Package Overview
Dependencies
Maintainers
5
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@rarimo/bionetta-js-sdk-core

Bionetta core tools

latest
npmnpm
Version
0.1.2
Version published
Maintainers
5
Created
Source

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:

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

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

NameTypeDescription
zkeyFileBytesBufferLikeThe proving key file contents as a buffer (e.g., Uint8Array, ArrayBuffer).
wtnsBufferLikeThe 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

NameTypeDescription
vkVerifieranyThe verification key object (usually from .vkey.json).
publicSignalsPublicSignalsThe public signals generated during witness calculation.
proofGroth16ProofThe zk-SNARK proof previously generated by generateProof.
logger (optional)anyOptional 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

NameTypeDescription
dataWorker string URLAn 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

NameTypeDescription
wasmBytesBufferLikeA binary buffer (e.g., ArrayBuffer, Uint8Array) containing the WASM code of the circuit.
inputTInputThe input object to the circuit. This must match the input signals expected by the circuit.
workerMessageTypestringThe 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:

NameTypeDescription
videoElementHTMLVideoElementThe video DOM element displaying the camera feed.
videoCanvasHTMLCanvasElementCanvas used to render raw video frames for processing.
overlayCanvasHTMLCanvasElementCanvas to draw detection overlays (e.g., face boxes, landmarks).
faceCanvasHTMLCanvasElementCanvas where the resized detected face is drawn for later usage.
resizeWidthnumberThe 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

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

FAQs

Package last updated on 18 Jun 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts