Skip to content

Add witness calculation to the browser version #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7,664 changes: 4,408 additions & 3,256 deletions build/websnark.js

Large diffs are not rendered by default.

7,664 changes: 4,408 additions & 3,256 deletions example/websnark.js

Large diffs are not rendered by default.

28 changes: 20 additions & 8 deletions main.js
Original file line number Diff line number Diff line change
@@ -19,16 +19,17 @@

/* globals window */

const buildGroth16 = require("./src/groth16.js");
const buildGroth16 = require("./src/groth16");
const utils = require("./src/utils");

buildGroth16().then( (groth16) => {
buildGroth16().then((groth16) => {
window.groth16 = groth16;
window.genZKSnarkProof = function(witness, provingKey, cb) {

const p = groth16.proof(witness, provingKey);
window.zkSnarkProofToSolidityInput = utils.toSolidityInput;

window.genZKSnarkProofAndWitness = function (input, circuitJson, provingKey, cb) {
const p = utils.genWitnessAndProve(groth16, input, circuitJson, provingKey);
if (cb) {
p.then( (proof) => {
p.then((proof) => {
cb(null, proof);
}, (err) => {
cb(err);
@@ -37,6 +38,17 @@ buildGroth16().then( (groth16) => {
return p;
}
};
});


window.genZKSnarkProof = function (witness, provingKey, cb) {
const p = groth16.proof(witness, provingKey);
if (cb) {
p.then((proof) => {
cb(null, proof);
}, (err) => {
cb(err);
});
} else {
return p;
}
};
});
76 changes: 72 additions & 4 deletions src/utils.js
Original file line number Diff line number Diff line change
@@ -18,23 +18,91 @@
*/

const bigInt = require("big-integer");
const Circuit = require("snarkjs/src/circuit");
const bigInt2 = require("snarkjs/src/bigint");
const hexifyBigInts = require("../tools/stringifybigint").hexifyBigInts;
const unhexifyBigInts = require("../tools/stringifybigint").unhexifyBigInts;
const stringifyBigInts = require("../tools/stringifybigint").stringifyBigInts;
const unstringifyBigInts = require("../tools/stringifybigint").unstringifyBigInts;
const stringifyBigInts2 = require("snarkjs/src/stringifybigint").stringifyBigInts;
const unstringifyBigInts2 = require("snarkjs/src/stringifybigint").unstringifyBigInts;

exports.bigInt2BytesLE = function bigInt2BytesLE(_a, len) {
function bigInt2BytesLE(_a, len) {
const b = Array(len);
let v = bigInt(_a);
for (let i=0; i<len; i++) {
b[i] = v.and(0xFF).toJSNumber();
v = v.shiftRight(8);
}
return b;
};
}

exports.bigInt2U32LE = function bigInt2BytesLE(_a, len) {
function bigInt2U32LE(_a, len) {
const b = Array(len);
let v = bigInt(_a);
for (let i=0; i<len; i++) {
b[i] = v.and(0xFFFFFFFF).toJSNumber();
v = v.shiftRight(32);
}
return b;
};
}

function convertWitness(witness) {
const buffLen = witness.length * 32;
const buff = new ArrayBuffer(buffLen);
const h = {
dataView: new DataView(buff),
offset: 0
};
const mask = bigInt2(0xFFFFFFFF);
for (let i = 0; i < witness.length; i++) {
for (let j = 0; j < 8; j++) {
const v = Number(witness[i].shr(j * 32).and(mask));
h.dataView.setUint32(h.offset, v, true);
h.offset += 4;
}
}
return buff;
}

function toSolidityInput(proof) {
const result = {
pi_a: [proof.pi_a[0], proof.pi_a[1]],
pi_b: [[proof.pi_b[0][1], proof.pi_b[0][0]], [proof.pi_b[1][1], proof.pi_b[1][0]]],
pi_c: [proof.pi_c[0], proof.pi_c[1]],
};
if (proof.publicSignals) {
result.publicSignals = proof.publicSignals;
}
return hexifyBigInts(unstringifyBigInts(result));
}

function fromSolidityInput(proof) {
proof = unhexifyBigInts(proof);
const result = {
pi_a: [proof.pi_a[0], proof.pi_a[1], bigInt(1)],
pi_b: [[proof.pi_b[0][1], proof.pi_b[0][0]], [proof.pi_b[1][1], proof.pi_b[1][0]], [bigInt(1), bigInt(0)]],
pi_c: [proof.pi_c[0], proof.pi_c[1], bigInt(1)]
};
if (proof.publicSignals) {
result.publicSignals = proof.publicSignals;
}
return stringifyBigInts(proof);
}

function genWitness(input, circuitJson) {
const circuit = new Circuit(unstringifyBigInts2(circuitJson));
const witness = circuit.calculateWitness(unstringifyBigInts2(input));
const publicSignals = witness.slice(1, circuit.nPubInputs + circuit.nOutputs + 1);
return {witness, publicSignals};
}

async function genWitnessAndProve(groth16, input, circuitJson, provingKey) {
const witnessData = genWitness(input, circuitJson);
const witnessBin = convertWitness(witnessData.witness);
const result = await groth16.proof(witnessBin, provingKey);
result.publicSignals = stringifyBigInts2(witnessData.publicSignals);
return result;
}

module.exports = {bigInt2BytesLE, bigInt2U32LE, toSolidityInput, fromSolidityInput, genWitnessAndProve};
39 changes: 38 additions & 1 deletion tools/stringifybigint.js
Original file line number Diff line number Diff line change
@@ -21,6 +21,8 @@ const bigInt = require("big-integer");

module.exports.stringifyBigInts = stringifyBigInts;
module.exports.unstringifyBigInts = unstringifyBigInts;
module.exports.hexifyBigInts = hexifyBigInts;
module.exports.unhexifyBigInts = unhexifyBigInts;

function stringifyBigInts(o) {
if ((typeof(o) == "bigint") || (o instanceof bigInt)) {
@@ -43,7 +45,7 @@ function unstringifyBigInts(o) {
return bigInt(o);
} else if (Array.isArray(o)) {
return o.map(unstringifyBigInts);
} else if (typeof o == "object") {
} else if (typeof o == "object" && !(o instanceof bigInt)) {
const res = {};
for (let k in o) {
res[k] = unstringifyBigInts(o[k]);
@@ -53,3 +55,38 @@ function unstringifyBigInts(o) {
return o;
}
}

function hexifyBigInts(o) {
if (typeof (o) === "bigInt" || (o instanceof bigInt)) {
let str = o.toString(16);
while (str.length < 64) str = "0" + str;
str = "0x" + str;
return str;
} else if (Array.isArray(o)) {
return o.map(hexifyBigInts);
} else if (typeof o == "object") {
const res = {};
for (let k in o) {
res[k] = hexifyBigInts(o[k]);
}
return res;
} else {
return o;
}
}

function unhexifyBigInts(o) {
if ((typeof(o) == "string") && (/^0x[0-9a-fA-F]+$/.test(o))) {
return bigInt(o);
} else if (Array.isArray(o)) {
return o.map(unhexifyBigInts);
} else if (typeof o == "object") {
const res = {};
for (let k in o) {
res[k] = unhexifyBigInts(o[k]);
}
return res;
} else {
return o;
}
}