websnark/README.md
README.mdRaw
1# websnark
2
3A fast zkSnark proof generator written in native Web Assembly.
4
5websnark is used to generate zkSnark Proofs from the browser.
6
7This module generates highly optimized Web Assembly modules for the low level
8cryptographic primitives.
9
10It also makes use of the Web Workers feature to parallelize the generation
11of the zero knoledge proofs.
12
13The result is a fast library with times close to libsnarks but fully compatible for
14browsers.
15
16## Usage
17
18You just need to import the websnark.js found in the build directory.
19
20```html
21<script src="websnark.js" />
22```
23
24This library has a single javascript function:
25
26genZKSnarkProof(witness, provingKey, cb)
27
28cb is the callback. If cb is undefined, then the function will return a promise.
29
30witness is a binary buffer with all the signals in binnary format. The buffer is packt in 32bytes Little Endian Encoded Field Elements.
31
32You can use the tool to build the binary file from the witness.json file generated by [snarkjs](https://github.com/iden3/snarkjs).
33
34### IMPORTANT: Please be sure you run your setup with `--protocol groth` websnark only generates groth16 proofs!
35
36```
37node ../tools/buildwitness_cli.js -i witness.json -o witness.bin
38```
39
40provingKey is the binary buffer with the binary representation of the proving key.
41
42Check the tool tools/buildpkey_cli.js to convert a proving_key.json file generated
43in [snarkjs](https://github.com/iden3/snarkjs) to a proving_key.bin file that can
44be used directly with this library.
45
46```
47node ../tools/buildpkey_cli.js -i proving_key.json -o proving_key.bin
48```
49
50The result is a JSON object with pi_a, pi_b and pi_c points.
51
52You can use the stringified version of this JSON as a proof.json in [snarkjs](https://github.com/iden3/snarkjs)
53
54
55Here is a simple example of a web page that loads a key and a witness and generates the proof when the button is pressed.
56
57```html
58<html>
59<header>
60</header>
61<script src="websnark.js"></script>
62<script>
63
64var witness;
65var proving_key;
66
67function onLoad() {
68
69 fetch("proving_key.bin").then( (response) => {
70 return response.arrayBuffer();
71 }).then( (b) => {
72 provingKey = b;
73 });
74
75 fetch("witness.bin").then( (response) => {
76 return response.arrayBuffer();
77 }).then( (b) => {
78 witness = b;
79 });
80}
81
82function calcProof() {
83 const start = new Date().getTime();
84 document.getElementById("time").innerHTML = "processing....";
85 document.getElementById("proof").innerHTML = "";
86
87 window.genZKSnarkProof(witness, provingKey).then((p)=> {
88 const end = new Date().getTime();
89 const time = end - start;
90 document.getElementById("time").innerHTML = `Time to compute: ${time}ms`;
91 document.getElementById("proof").innerHTML = JSON.stringify(p, null, 1);
92 });
93}
94
95</script>
96<body onLoad="onLoad()">
97<h1>iden3</h1>
98<h2>Zero knowledge proof generator</h2>
99<button onClick="calcProof()">Test</button>
100<div id="time"></div>
101<pre id="proof"></pre>
102
103</body>
104</html>
105```
106
107You can test it by running a web server on the example directory
108
109```
110npm -g install http-server
111cd example
112http-server .
113```
114
115And then navegate to [http://127.0.0.1:8080](http://127.0.0.1:8080)
116
117The generated proof can be cut and pasted to `example/proof` and tested with snarkjs
118
119```
120snarkjs verify
121``
122
123## Building wasm.js
124
125```
126npm run build
127```
128
129## Testing
130
131```
132npm test
133```
134
135## License
136
137websnark is part of the iden3 project copyright 2019 0KIMS association and published with GPL-3 license. Please check the COPYING file for more details.