1
0
Fork 0
mirror of synced 2025-09-24 04:40:05 +00:00
ZoKrates/zokrates_js
2020-08-03 13:32:39 +02:00
..
node add metadata gulp task 2020-07-17 15:54:37 +02:00
src merge dev, add node support to zokrates-js, fix tests 2020-07-09 17:54:45 +02:00
tests wip 2020-07-16 11:31:19 +02:00
.dockerignore make imports public in core, remove ci steps for zokrates_js 2020-01-06 20:55:54 +01:00
.gitignore add metadata gulp task 2020-07-17 15:54:37 +02:00
build.sh Add dockerfile for building zokrates_js, modify ci steps 2020-01-14 02:51:29 +01:00
Cargo.lock wip 2020-07-16 11:31:19 +02:00
Cargo.toml merge dev, add node support to zokrates-js, fix tests 2020-07-09 17:54:45 +02:00
Dockerfile fix Dockerfile, disable wasm-opt 2020-02-04 18:05:44 +01:00
Dockerfile.env up zokrates_js version, update Dockerfile.env 2020-06-15 11:47:31 +02:00
gulpfile.js add metadata gulp task 2020-07-17 15:54:37 +02:00
index.d.ts update index.d.ts 2020-07-17 15:57:44 +02:00
index.js add metadata gulp task 2020-07-17 15:54:37 +02:00
package-lock.json Merge branch 'develop' into develop 2020-08-03 11:09:41 +02:00
package.json Merge branch 'develop' into develop 2020-08-03 11:09:41 +02:00
publish.sh Add dockerfile for building zokrates_js, modify ci steps 2020-01-14 02:51:29 +01:00
README.md update zokrates.js book 2020-08-03 13:32:39 +02:00
wrapper.js fix extension slicing 2020-07-16 11:54:00 +02:00

zokrates.js

JavaScript bindings for ZoKrates project.

npm install zokrates-js

Importing

Bundlers

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';
Node
const { initialize } = require('zokrates-js/node');

Example

function importResolver(currentLocation, importLocation) {
  console.log(currentLocation + ' is importing ' + importLocation);
  // implement your resolving logic here
  return {
    source: "def main() -> (): return",
    location: importLocation
  };
}

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

    // compilation
    const artifacts = zokratesProvider.compile(source, "main", importResolver);

    // 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");
});

API

initialize()

Loads binding wasm module and returns a promise with ZoKrates provider.

Returns: Promise<ZoKratesProvider>

compile(source, location, resolveCallback[, config])

Compiles source code into ZoKrates internal representation of arithmetic circuits.

Parameters:

  • source - Source code to compile
  • location - Root location of the module which is being compiled
  • resolveCallback - User-defined callback used to resolve imports
  • config - Compilation config

Returns: CompilationArtifacts

computeWitness(artifacts, args)

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

Parameters:

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

Returns: ComputationResult

setup(program)

Generates a trusted setup for the compiled program.

Parameters:

  • program - Compiled program

Returns: SetupKeypair

exportSolidityVerifier(verificationKey, abi)

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

Parameters:

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

Parameters:

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

Returns: Proof