-
Notifications
You must be signed in to change notification settings - Fork 22
/
index.js
62 lines (45 loc) · 1.61 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
const express = require('express');
const snarkjs = require("snarkjs");
const ffjavascript = require('ffjavascript');
const app = express();
app.use(express.static('public'));
app.use(express.json());
const {stringifyBigInts: stringifyBigInts$3, unstringifyBigInts: unstringifyBigInts$1} = ffjavascript.utils;
// copy pasta p256 from snarkjs cli.cjs line 6726
function p256(n) {
let nstr = n.toString(16);
while (nstr.length < 64) nstr = "0"+nstr;
nstr = `0x${nstr}`;
return nstr;
}
async function genSolidityCalldata(publicName, proofName) {
const pub = unstringifyBigInts$1(publicName);
const proof = unstringifyBigInts$1(proofName);
let inputs = [];
for (let i=0; i<pub.length; i++) {
inputs.push(p256(pub[i]));
}
if ((typeof proof.protocol === "undefined") || (proof.protocol != "groth16")) {
throw new Error("InvalidProof");
}
let S = {
'pi_a': [p256(proof.pi_a[0]), p256(proof.pi_a[1])],
'pi_b': [[p256(proof.pi_b[0][1]), p256(proof.pi_b[0][0])], [p256(proof.pi_b[1][1]), p256(proof.pi_b[1][0])]],
'pi_c': [p256(proof.pi_c[0]), p256(proof.pi_c[1])],
'inputs': inputs,
};
return S;
}
app.get('/', (req, res) => res.send('Im a teapot'));
app.post('/generate_proof', async function (req, res) {
const input = req.body;
const { proof, publicSignals } = await snarkjs.groth16.fullProve(
input,
"./circuit.wasm",
"./circuit.zkey"
);
const genCalldata = await genSolidityCalldata(publicSignals, proof);
res.json(genCalldata);
})
const port = process.env.PORT || 3000;
app.listen(port, () => console.log(`Server running on ${port}, http://localhost:${port}`));