Compare commits

..

2 Commits

10 changed files with 5342 additions and 2400 deletions

1
.npmignore Normal file

@ -0,0 +1 @@
test

1
.npmrc Normal file

@ -0,0 +1 @@
@tornado:registry=https://git.tornado.ws/api/packages/tornado-packages/npm/

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

467
package-lock.json generated

File diff suppressed because it is too large Load Diff

@ -1,12 +1,12 @@
{ {
"name": "websnark", "name": "@tornado/websnark",
"version": "0.0.4", "version": "0.0.4",
"description": "big integer library to work in Zq", "description": "big integer library to work in Zq",
"main": "index.js", "main": "index.js",
"scripts": { "scripts": {
"test": "mocha --experimental-worker", "test": "mocha --experimental-worker",
"buildwasm": "node tools/buildwasm.js", "buildwasm": "node tools/buildwasm.js",
"build": "node tools/buildwasm.js; browserify main.js -o build/websnark.js --exclude worker_threads --exclude crypto; cp build/websnark.js example/websnark.js" "build": "node tools/buildwasm.js && browserify main.js -o build/websnark.js --exclude worker_threads --exclude crypto && cp build/websnark.js example/websnark.js"
}, },
"keywords": [ "keywords": [
"bigint", "bigint",
@ -22,7 +22,7 @@
"license": "GPL-3.0", "license": "GPL-3.0",
"repository": { "repository": {
"type": "git", "type": "git",
"url": "https://github.com/iden3/websnark.git" "url": "https://git.tornado.ws/tornado-packages/websnark"
}, },
"devDependencies": { "devDependencies": {
"browserify": "^16.2.3", "browserify": "^16.2.3",
@ -31,10 +31,10 @@
"eslint-plugin-webassembly": "^1.8.4", "eslint-plugin-webassembly": "^1.8.4",
"mocha": "^6.1.4", "mocha": "^6.1.4",
"package": "^1.0.1", "package": "^1.0.1",
"snarkjs": "git+https://development.tornadocash.community/tornadocash/snarkjs.git#869181cfaf7526fe8972073d31655493a04326d5",
"wasmbuilder": "0.0.3" "wasmbuilder": "0.0.3"
}, },
"dependencies": { "dependencies": {
"@tornado/snarkjs": "^0.1.20",
"big-integer": "1.6.42" "big-integer": "1.6.42"
} }
} }

@ -18,20 +18,20 @@
*/ */
const bigInt = require("big-integer"); const bigInt = require("big-integer");
const Circuit = require("snarkjs/src/circuit"); const Circuit = require("@tornado/snarkjs/src/circuit");
const bigInt2 = require("snarkjs/src/bigint"); const bigInt2 = require("@tornado/snarkjs/src/bigint");
const hexifyBigInts = require("../tools/stringifybigint").hexifyBigInts; const hexifyBigInts = require("../tools/stringifybigint").hexifyBigInts;
const unhexifyBigInts = require("../tools/stringifybigint").unhexifyBigInts; const unhexifyBigInts = require("../tools/stringifybigint").unhexifyBigInts;
const stringifyBigInts = require("../tools/stringifybigint").stringifyBigInts; const stringifyBigInts = require("../tools/stringifybigint").stringifyBigInts;
const unstringifyBigInts = require("../tools/stringifybigint").unstringifyBigInts; const unstringifyBigInts = require("../tools/stringifybigint").unstringifyBigInts;
const stringifyBigInts2 = require("snarkjs/src/stringifybigint").stringifyBigInts; const stringifyBigInts2 = require("@tornado/snarkjs/src/stringifybigint").stringifyBigInts;
const unstringifyBigInts2 = require("snarkjs/src/stringifybigint").unstringifyBigInts; const unstringifyBigInts2 = require("@tornado/snarkjs/src/stringifybigint").unstringifyBigInts;
function bigInt2BytesLE(_a, len) { function bigInt2BytesLE(_a, len) {
const b = Array(len); const b = Array(len);
let v = bigInt(_a); let v = bigInt(_a);
for (let i = 0; i < len; i++) { for (let i = 0; i < len; i++) {
b[i] = v.and(0xFF).toJSNumber(); b[i] = v.and(0xff).toJSNumber();
v = v.shiftRight(8); v = v.shiftRight(8);
} }
return b; return b;
@ -41,7 +41,7 @@ function bigInt2U32LE(_a, len) {
const b = Array(len); const b = Array(len);
let v = bigInt(_a); let v = bigInt(_a);
for (let i = 0; i < len; i++) { for (let i = 0; i < len; i++) {
b[i] = v.and(0xFFFFFFFF).toJSNumber(); b[i] = v.and(0xffffffff).toJSNumber();
v = v.shiftRight(32); v = v.shiftRight(32);
} }
return b; return b;
@ -52,9 +52,9 @@ function convertWitness(witness) {
const buff = new ArrayBuffer(buffLen); const buff = new ArrayBuffer(buffLen);
const h = { const h = {
dataView: new DataView(buff), dataView: new DataView(buff),
offset: 0 offset: 0,
}; };
const mask = bigInt2(0xFFFFFFFF); const mask = bigInt2(0xffffffff);
for (let i = 0; i < witness.length; i++) { for (let i = 0; i < witness.length; i++) {
for (let j = 0; j < 8; j++) { for (let j = 0; j < 8; j++) {
const v = Number(witness[i].shr(j * 32).and(mask)); const v = Number(witness[i].shr(j * 32).and(mask));
@ -73,13 +73,17 @@ function toHex32(number) {
function toSolidityInput(proof) { function toSolidityInput(proof) {
const flatProof = unstringifyBigInts([ const flatProof = unstringifyBigInts([
proof.pi_a[0], proof.pi_a[1], proof.pi_a[0],
proof.pi_b[0][1], proof.pi_b[0][0], proof.pi_a[1],
proof.pi_b[1][1], proof.pi_b[1][0], proof.pi_b[0][1],
proof.pi_c[0], proof.pi_c[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 = { const result = {
proof: "0x" + flatProof.map(x => toHex32(x)).join("") proof: "0x" + flatProof.map((x) => toHex32(x)).join(""),
}; };
if (proof.publicSignals) { if (proof.publicSignals) {
result.publicSignals = hexifyBigInts(unstringifyBigInts(proof.publicSignals)); result.publicSignals = hexifyBigInts(unstringifyBigInts(proof.publicSignals));

@ -1,6 +1,6 @@
const assert = require("assert"); const assert = require("assert");
const refBn128 = require("snarkjs").bn128; const refBn128 = require("@tornado/snarkjs").bn128;
const refBigInt = require("snarkjs").bigInt; const refBigInt = require("@tornado/snarkjs").bigInt;
const buildBn128 = require("../index.js").buildBn128; const buildBn128 = require("../index.js").buildBn128;
@ -39,7 +39,6 @@ describe("Basic tests for g1 in bn128", () => {
d[1] = refBigInt(d[1].toString()); d[1] = refBigInt(d[1].toString());
d[2] = refBigInt(d[2].toString()); d[2] = refBigInt(d[2].toString());
assert(d[0].equals(refD[0])); assert(d[0].equals(refD[0]));
assert(d[1].equals(refD[1])); assert(d[1].equals(refD[1]));
assert(d[2].equals(1)); assert(d[2].equals(1));
@ -101,7 +100,6 @@ describe("Basic tests for g1 in bn128", () => {
} }
assert(refBn128.G1.equals(d, refD)); assert(refBn128.G1.equals(d, refD));
}).timeout(10000000); }).timeout(10000000);
it("It should do a basic point doubling in G2", async () => { it("It should do a basic point doubling in G2", async () => {
const bn128 = await buildBn128(); const bn128 = await buildBn128();

@ -1,6 +1,6 @@
const assert = require("assert"); const assert = require("assert");
const refBn128 = require("snarkjs").bn128; const refBn128 = require("@tornado/snarkjs").bn128;
const refBigInt = require("snarkjs").bigInt; const refBigInt = require("@tornado/snarkjs").bigInt;
const buildBn128 = require("../index.js").buildBn128; const buildBn128 = require("../index.js").buildBn128;
@ -53,7 +53,7 @@ describe("FFT tests", () => {
const p = bn128.alloc(32 * N); const p = bn128.alloc(32 * N);
for (let i = 0; i < N; i++) { for (let i = 0; i < N; i++) {
bn128.putInt(p+i*32, (i%2 == 0)? 0 : 1); bn128.putInt(p + i * 32, i % 2 == 0 ? 0 : 1);
} }
bn128.fft_toMontgomeryN(p, p, N); bn128.fft_toMontgomeryN(p, p, N);
@ -63,7 +63,7 @@ describe("FFT tests", () => {
for (let i = 0; i < N; i++) { for (let i = 0; i < N; i++) {
const a = bn128.getInt(p + i * 32); const a = bn128.getInt(p + i * 32);
assert.equal(a,(i%2 == 0)? 0 : 1); assert.equal(a, i % 2 == 0 ? 0 : 1);
} }
}); });
it("test interleaved", async () => { it("test interleaved", async () => {
@ -117,7 +117,5 @@ describe("FFT tests", () => {
const a = bn128.getInt(pr1 + i * 32, 1); const a = bn128.getInt(pr1 + i * 32, 1);
assert.equal(a, 0); assert.equal(a, 0);
} }
}); });
}); });

@ -1,8 +1,7 @@
const assert = require("assert"); const assert = require("assert");
const fs = require("fs"); const fs = require("fs");
const path = require("path"); const path = require("path");
const snarkjs = require("snarkjs"); const snarkjs = require("@tornado/snarkjs");
const buildGroth16 = require("../index.js").buildGroth16; const buildGroth16 = require("../index.js").buildGroth16;
@ -46,7 +45,6 @@ describe("Basic tests for groth16 proof generator", () => {
assert.equal(r1[1], r2[1]); assert.equal(r1[1], r2[1]);
groth16.terminate(); groth16.terminate();
}); });
it("It should do a basic point doubling G1", async () => { it("It should do a basic point doubling G1", async () => {
@ -57,12 +55,13 @@ describe("Basic tests for groth16 proof generator", () => {
const proofS = await groth16.proof(signals.buffer, provingKey.buffer); const proofS = await groth16.proof(signals.buffer, provingKey.buffer);
const proof = snarkjs.unstringifyBigInts(proofS); const proof = snarkjs.unstringifyBigInts(proofS);
const verifierKey = snarkjs.unstringifyBigInts(JSON.parse(fs.readFileSync(path.join(__dirname, "data", "verification_key.json"), "utf8"))); const verifierKey = snarkjs.unstringifyBigInts(
JSON.parse(fs.readFileSync(path.join(__dirname, "data", "verification_key.json"), "utf8"))
);
const pub = snarkjs.unstringifyBigInts(JSON.parse(fs.readFileSync(path.join(__dirname, "data", "public.json"), "utf8"))); const pub = snarkjs.unstringifyBigInts(JSON.parse(fs.readFileSync(path.join(__dirname, "data", "public.json"), "utf8")));
assert(snarkjs.groth.isValid(verifierKey, proof, pub)); assert(snarkjs.groth.isValid(verifierKey, proof, pub));
groth16.terminate(); groth16.terminate();
}).timeout(10000000); }).timeout(10000000);
}); });