A fast zkSnark proof generator written in native Web Assembly.
Go to file
Jordi Baylina 664c98e41a
0.0.8
2020-03-25 19:35:53 +01:00
build renamed from websnark to wasmsnark 2020-03-25 19:35:35 +01:00
example renamed from websnark to wasmsnark 2020-03-25 19:35:35 +01:00
src renamed from websnark to wasmsnark 2020-03-25 19:35:35 +01:00
test Full zkSnark test fixed 2020-03-21 17:55:51 +01:00
tools mnt6753 verification and bn128 pairings 2019-08-02 11:20:07 +02:00
.eslintrc.js renamed from websnark to wasmsnark 2020-03-25 19:35:35 +01:00
.gitignore Initial commit 2019-04-09 12:37:39 -07:00
COPYING Initial commit 2019-04-09 12:37:39 -07:00
README.md renamed from websnark to wasmsnark 2020-03-25 19:35:35 +01:00
TODO Initial commit 2019-04-09 12:37:39 -07:00
index.js renamed from websnark to wasmsnark 2020-03-25 19:35:35 +01:00
main_bn128.js renamed from websnark to wasmsnark 2020-03-25 19:35:35 +01:00
main_mnt6753.js renamed from websnark to wasmsnark 2020-03-25 19:35:35 +01:00
package-lock.json 0.0.8 2020-03-25 19:35:53 +01:00
package.json 0.0.8 2020-03-25 19:35:53 +01:00

README.md

wasmsnark

A fast zkSnark proof and verifier and proof generator written in native Web Assembly.

wasmsnark is used to generate zkSnark Proofs and verify the from the browser.

This module generates highly optimized Web Assembly modules for the low level cryptographic primitives.

It also makes use of the Web Workers feature to parallelize the generation and verification of the zero knoledge proofs.

The result is a fast library with times close to libsnarks but fully compatible for browsers.

Usage

BN128

You just need to import the wasmsnark_bn128.js found in the build directory.

<script src="wasmsnark_bn128.js" />

This library has a single javascript function:

genZKSnarkProof(witness, provingKey, cb)

cb is the callback. If cb is undefined, then the function will return a promise.

witness is a binary buffer with all the signals in binnary format. The buffer is packt in 32bytes Little Endian Encoded Field Elements.

You can use the tool to build the binary file from the witness.json file generated by snarkjs.

IMPORTANT: Please be sure you run your setup with --protocol groth wasmsnark only generates groth16 proofs!

node ../tools/buildwitness.js -i witness.json -o witness.bin

provingKey is the binary buffer with the binary representation of the proving key.

Check the tool tools/buildpkey.js to convert a proving_key.json file generated in snarkjs to a proving_key.bin file that can be used directly with this library.

node ../tools/buildpkey.js -i proving_key.json -o proving_key.bin

The result is a JSON object with pi_a, pi_b and pi_c points.

You can use the stringified version of this JSON as a proof.json in snarkjs

Here is a simple example of a web page that loads a key and a witness and generates the proof when the button is pressed.

<html>
<header>
</header>
<script src="wasmsnark_bn128.js"></script>
<script>

var witness;
var proving_key;

function onLoad() {

    fetch("proving_key.bin").then( (response) => {
        return response.arrayBuffer();
    }).then( (b) => {
        provingKey = b;
    });

    fetch("witness.bin").then( (response) => {
        return response.arrayBuffer();
    }).then( (b) => {
        witness = b;
    });
}

function calcProof() {
    const start = new Date().getTime();
    document.getElementById("time").innerHTML = "processing....";
    document.getElementById("proof").innerHTML = "";

    window.genZKSnarkProof(witness, provingKey).then((p)=> {
        const end = new Date().getTime();
        const time = end - start;
        document.getElementById("time").innerHTML = `Time to compute: ${time}ms`;
        document.getElementById("proof").innerHTML = JSON.stringify(p, null, 1);
    });
}

</script>
<body onLoad="onLoad()">
<h1>iden3</h1>
<h2>Zero knowledge proof generator</h2>
<button onClick="calcProof()">Test</button>
<div id="time"></div>
<pre id="proof"></pre>

</body>
</html>

You can test it by running a web server on the example directory

npm -g install http-server
cd example/bn128
http-server .

And then navegate to http://127.0.0.1:8080

The generated proof can be cut and pasted to example/bn128/proof.json and tested with snarkjs

snarkjs verify

MNT6753

The directory example/mnt6753 contains an example of the verifier.

Building wasm.js

npm run build

Testing

npm test

License

wasmsnark is part of the iden3 project copyright 2019 0KIMS association and published with GPL-3 license. Please check the COPYING file for more details.