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```
35node ../tools/buildwitness.js -i witness.json -o witness.bin
36```
37
38provingKey is the binary buffer with the binary representation of the proving key.
39
40Check the tool tools/buildpkey.js to convert a proving_key.json file generated
41in [snarkjs](https://github.com/iden3/snarkjs) to a proving_key.bin file that can
42be used directly with this library.
43
44```
45node ../tools/buildpkey.js -i proving_key.json -o proving_key.bin
46```
47
48The result is a JSON object with pi_a, pi_b and pi_c points.
49
50You can use the stringified version of this JSON as a proof.json in [snarkjs](https://github.com/iden3/snarkjs)
51
52
53Here is a simple example of a web page that loads a key and a witness and generates the proof when the button is pressed.
54
55```html
56<html>
57<header>
58</header>
59<script src="websnark.js"></script>
60<script>
61
62var witness;
63var proving_key;
64
65function onLoad() {
66
67 fetch("proving_key.bin").then( (response) => {
68 return response.arrayBuffer();
69 }).then( (b) => {
70 provingKey = b;
71 });
72
73 fetch("witness.bin").then( (response) => {
74 return response.arrayBuffer();
75 }).then( (b) => {
76 witness = b;
77 });
78}
79
80function calcProof() {
81 const start = new Date().getTime();
82 document.getElementById("time").innerHTML = "processing....";
83 document.getElementById("proof").innerHTML = "";
84
85 window.genZKSnarkProof(witness, provingKey).then((p)=> {
86 const end = new Date().getTime();
87 const time = end - start;
88 document.getElementById("time").innerHTML = `Time to compute: ${time}ms`;
89 document.getElementById("proof").innerHTML = JSON.stringify(p, null, 1);
90 });
91}
92
93</script>
94<body onLoad="onLoad()">
95<h1>iden3</h1>
96<h2>Zero knowledge proof generator</h2>
97<button onClick="calcProof()">Test</button>
98<div id="time"></div>
99<pre id="proof"></pre>
100
101</body>
102</html>
103```
104
105## Building wasm.js
106
107```
108npm run build
109```
110
111## Testing
112
113```
114npm test
115```
116
117## License
118
119websnark is part of the iden3 project copyright 2019 0KIMS association and published with GPL-3 license. Please check the COPYING file for more details.