JavaScript bindings for ZoKrates.

npm install zokrates-js



Note: As this library uses a model where the wasm module itself is natively an ES module, you will need a bundler of some form. Currently the only known bundler known to be fully compatible with zokrates-js is Webpack. The choice of this default was done to reflect the trends of the JS ecosystem.

import { initialize } from 'zokrates-js';
const { initialize } = require('zokrates-js/node');


initialize().then((zokratesProvider) => {
    const source = "def main(private field a) -> field: return a * a";

    // compilation
    const artifacts = zokratesProvider.compile(source);

    // computation
    const { witness, output } = zokratesProvider.computeWitness(artifacts, ["2"]);

    // run setup
    const keypair = zokratesProvider.setup(artifacts.program);

    // generate proof
    const proof = zokratesProvider.generateProof(artifacts.program, witness, keypair.pk);

    // export solidity verifier
    const verifier = zokratesProvider.exportSolidityVerifier(keypair.vk, "v1");



Returns an initialized ZoKratesProvider as a promise.

initialize().then(zokratesProvider => { 
    // call api functions here

Returns: Promise<ZoKratesProvider>

compile(source[, options])

Compiles source code into ZoKrates internal representation of arithmetic circuits.


  • source - Source code to compile
  • options - Compilation options

Returns: CompilationArtifacts



const artifacts = zokratesProvider.compile("def main() -> (): return");

Compilation with custom options:

const source = "...";
const options = {
    location: "main.zok", // location of the root module
    resolveCallback: (currentLocation, importLocation) => {
        console.log(currentLocation + ' is importing ' + importLocation);
        return { 
            source: "def main() -> (): return", 
            location: importLocation 
const artifacts = zokratesProvider.compile(source, options);

Note: The resolveCallback function is used to resolve dependencies. This callback receives the current module location and the import location of the module which is being imported. The callback must synchronously return either an error, null or a valid ResolverResult object like shown in the example above.

computeWitness(artifacts, args)

Computes a valid assignment of the variables, which include the results of the computation.


  • artifacts - Compilation artifacts
  • args - Array of arguments (eg. ["1", "2", true])

Returns: ComputationResult


const code = 'def main(private field a) -> (field): return a * a';
const artifacts = zokratesProvider.compile(code);

const { witness, output } = zokratesProvider.computeWitness(artifacts, ["2"]);

console.log(witness); // Resulting witness which can be used to generate a proof
console.log(output); // Computation output: ["4"]

Generates a trusted setup for the compiled program.


  • program - Compiled program

Returns: SetupKeypair

exportSolidityVerifier(verificationKey, abi)

Generates a Solidity contract which contains the generated verification key and a public function to verify proofs of computation of the compiled program.


  • verificationKey - Verification key from the setup keypair
  • abi - Abi version ("v1" | "v2")

Returns: string

generateProof(program, witness, provingKey)

Generates a proof for a computation of the compiled program.


  • program - Compiled program
  • witness - Witness (valid assignment of the variables) from the computation result
  • provingKey - Proving key from the setup keypair

Returns: Proof

verify(verificationKey, proof)

Verifies the generated proof.


  • verificationKey - Verification key from the setup keypair
  • proof - Generated proof

Returns: boolean