Skip to content
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

Configurable wasm memory allocation #9

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
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
Expand Up @@ -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);
Expand All @@ -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;
}
};
});
129 changes: 116 additions & 13 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"eslint-plugin-webassembly": "^1.8.4",
"mocha": "^6.1.4",
"package": "^1.0.1",
"snarkjs": "^0.1.12",
"snarkjs": "git+https://github.com/peppersec/snarkjs.git#869181cfaf7526fe8972073d31655493a04326d5",
"wasmbuilder": "0.0.3"
},
"dependencies": {
Expand Down
21 changes: 12 additions & 9 deletions src/groth16.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ function thread(self) {
const res = i32[0];
i32[0] += length;
while (i32[0] > memory.buffer.byteLength) {
memory.grow(100);
memory.grow(100);
}
i32 = new Uint32Array(memory.buffer);
return res;
Expand Down Expand Up @@ -169,8 +169,11 @@ function thread(self) {
};
}

async function build() {

// We use the Object.assign approach for the backwards compatibility
// @params Number wasmInitialMemory
async function build(params) {
const defaultParams = { wasmInitialMemory: 5000 };
Object.assign(defaultParams, params);
const groth16 = new Groth16();

groth16.q = bigInt("21888242871839275222246405745257275088696311157297823662689037894645226208583");
Expand All @@ -179,7 +182,7 @@ async function build() {
groth16.n32 = groth16.n64*2;
groth16.n8 = groth16.n64*8;

groth16.memory = new WebAssembly.Memory({initial:5000});
groth16.memory = new WebAssembly.Memory({initial:defaultParams.wasmInitialMemory});
groth16.i32 = new Uint32Array(groth16.memory.buffer);

const wasmModule = await WebAssembly.compile(groth16_wasm.code);
Expand Down Expand Up @@ -247,7 +250,7 @@ async function build() {
const copyCode = groth16_wasm.code.buffer.slice(0);
initPromises.push(groth16.postAction(i, {
command: "INIT",
init: 5000,
init: defaultParams.wasmInitialMemory,
code: copyCode

}, [copyCode]));
Expand Down Expand Up @@ -492,7 +495,7 @@ class Groth16 {


const pH = this.calcH(signals.slice(0), polsA, polsB, nSignals, domainSize).then( (h) => {
/* Debug code to print the result of h
/* Debug code to print the result of h
for (let i=0; i<domainSize; i++) {
const a = this.bin2int(h.slice(i*32, i*32+32));
console.log(i + " -> " + a.toString());
Expand Down Expand Up @@ -547,9 +550,9 @@ class Groth16 {
this.putBin(ps, bs);
}

/// Uncoment it to debug and check it works
// this.instance.exports.f1m_zero(pr);
// this.instance.exports.f1m_zero(ps);
/// Uncoment it to debug and check it works
// this.instance.exports.f1m_zero(pr);
// this.instance.exports.f1m_zero(ps);

// pi_a = pi_a + Alfa1 + r*Delta1
this.instance.exports.g1_add(pAlfa1, pi_a, pi_a);
Expand Down
73 changes: 69 additions & 4 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,88 @@
*/

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 toHex32(number) {
let str = number.toString(16);
while (str.length < 64) str = "0" + str;
return str;
}

function toSolidityInput(proof) {
const flatProof = unstringifyBigInts([
proof.pi_a[0], proof.pi_a[1],
proof.pi_b[0][1], proof.pi_b[0][0],
proof.pi_b[1][1], proof.pi_b[1][0],
proof.pi_c[0], proof.pi_c[1],
]);
const result = {
proof: "0x" + flatProof.map(x => toHex32(x)).join("")
};
if (proof.publicSignals) {
result.publicSignals = hexifyBigInts(unstringifyBigInts(proof.publicSignals));
}
return result;
}

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, genWitnessAndProve};
Loading