Bump version & use snarkjs dependency from self-hosted tornado git registry

This commit is contained in:
Theo 2023-07-21 03:10:01 -07:00
parent 3b492f9801
commit 14314b6bbb
45 changed files with 1056 additions and 1188 deletions

1
.npmrc Normal file

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

@ -1,5 +1,5 @@
const bn128 = require("snarkjs").bn128;
const bigInt = require("snarkjs").bigInt;
const bn128 = require("@tornado/snarkjs").bn128;
const bigInt = require("@tornado/snarkjs").bigInt;
const createBlakeHash = require("blake-hash");
const babyJub = require("../src/babyjub");
@ -8,12 +8,12 @@ function getPoint(S) {
const h = createBlakeHash("blake256").update(S).digest();
if (h.length != 32) {
throw new Error("Invalid length")
throw new Error("Invalid length");
}
let sign = false;
if (h[31] & 0x80) {
h[31] = h[31] & 0x7F;
h[31] = h[31] & 0x7f;
sign = true;
}
@ -28,9 +28,7 @@ function getPoint(S) {
const y2 = F.square(y);
let x = F.sqrt(F.div(
F.sub(F.one, y2),
F.sub(a, F.mul(d, y2))));
let x = F.sqrt(F.div(F.sub(F.one, y2), F.sub(a, F.mul(d, y2))));
if (x == null) return null;
@ -43,7 +41,6 @@ function getPoint(S) {
return p8;
}
function generatePoint(S) {
let p = null;
let idx = 0;
@ -59,13 +56,10 @@ function generatePoint(S) {
return p;
}
const g = [
bigInt("5299619240641551281634865583518297030282874472190772894086521144482721001553"),
bigInt("16950150798460657717958625567821834550301663161624707787222815936182638968203")];
bigInt("16950150798460657717958625567821834550301663161624707787222815936182638968203"),
];
// Sanity check
if (!babyJub.inCurve(g)) {
@ -78,6 +72,3 @@ for (let i=0; i<25; i++) {
const P = generatePoint("Iden3_PedersenGenerator_" + S);
console.log(`[${P[0].toString()}, ${P[1].toString()}]`);
}

@ -1,6 +1,6 @@
{
"name": "circomlib",
"version": "0.0.20",
"name": "@tornado/circomlib",
"version": "0.0.21",
"description": "Basic circuits library for Circom",
"main": "index.js",
"directories": {
@ -19,14 +19,14 @@
],
"repository": {
"type": "git",
"url": "https://github.com/iden3/circomlib.git"
"url": "https://git.tornado.ws/tornado-packages/circomlib.git"
},
"author": "0Kims",
"license": "GPL-3.0",
"dependencies": {
"blake-hash": "^1.1.0",
"blake2b": "^2.1.3",
"snarkjs": "git+https://github.com/tornadocash/snarkjs.git#869181cfaf7526fe8972073d31655493a04326d5",
"@tornado/snarkjs": "0.1.20",
"typedarray-to-buffer": "^3.1.5",
"web3": "^1.2.11"
},

@ -1,5 +1,5 @@
const bn128 = require("snarkjs").bn128;
const bigInt = require("snarkjs").bigInt;
const bn128 = require("@tornado/snarkjs").bn128;
const bigInt = require("@tornado/snarkjs").bigInt;
exports.addPoint = addPoint;
exports.mulPointEscalar = mulPointEscalar;
@ -9,11 +9,11 @@ exports.packPoint = packPoint;
exports.unpackPoint = unpackPoint;
exports.Generator = [
bigInt("995203441582195749578291179787384436505546430278305826713579947235728471134"),
bigInt("5472060717959818805561601436314318772137091100104008585924551046643952123905")
bigInt("5472060717959818805561601436314318772137091100104008585924551046643952123905"),
];
exports.Base8 = [
bigInt("5299619240641551281634865583518297030282874472190772894086521144482721001553"),
bigInt("16950150798460657717958625567821834550301663161624707787222815936182638968203")
bigInt("16950150798460657717958625567821834550301663161624707787222815936182638968203"),
];
exports.order = bigInt("21888242871839275222246405745257275088614511777268538073601725287587578984328");
exports.subOrder = exports.order.shr(3);
@ -21,7 +21,6 @@ exports.p = bn128.r;
exports.A = bigInt("168700");
exports.D = bigInt("168696");
function addPoint(a, b) {
const q = bn128.r;
@ -31,8 +30,18 @@ function addPoint(a,b) {
res[0] = bigInt((a[0]*b[1] + b[0]*a[1]) * bigInt(bigInt("1") + d*a[0]*b[0]*a[1]*b[1]).inverse(q)).affine(q);
res[1] = bigInt((a[1]*b[1] - cta*a[0]*b[0]) * bigInt(bigInt("1") - d*a[0]*b[0]*a[1]*b[1]).inverse(q)).affine(q);
*/
res[0] = bigInt((bigInt(a[0]).mul(b[1]).add(bigInt(b[0]).mul(a[1]))).mul(bigInt(bigInt("1").add(exports.D.mul(a[0]).mul(b[0]).mul(a[1]).mul(b[1]))).inverse(q))).affine(q);
res[1] = bigInt((bigInt(a[1]).mul(b[1]).sub(exports.A.mul(a[0]).mul(b[0]))).mul(bigInt(bigInt("1").sub(exports.D.mul(a[0]).mul(b[0]).mul(a[1]).mul(b[1]))).inverse(q))).affine(q);
res[0] = bigInt(
bigInt(a[0])
.mul(b[1])
.add(bigInt(b[0]).mul(a[1]))
.mul(bigInt(bigInt("1").add(exports.D.mul(a[0]).mul(b[0]).mul(a[1]).mul(b[1]))).inverse(q))
).affine(q);
res[1] = bigInt(
bigInt(a[1])
.mul(b[1])
.sub(exports.A.mul(a[0]).mul(b[0]))
.mul(bigInt(bigInt("1").sub(exports.D.mul(a[0]).mul(b[0]).mul(a[1]).mul(b[1]))).inverse(q))
).affine(q);
return res;
}
@ -56,7 +65,7 @@ function mulPointEscalar(base, e) {
function inSubgroup(P) {
if (!inCurve(P)) return false;
const res = mulPointEscalar(P, exports.subOrder);
return (res[0].equals(bigInt(0))) && (res[1].equals(bigInt(1)));
return res[0].equals(bigInt(0)) && res[1].equals(bigInt(1));
}
function inCurve(P) {
@ -65,9 +74,7 @@ function inCurve(P) {
const x2 = F.square(P[0]);
const y2 = F.square(P[1]);
if (!F.equals(
F.add(F.mul(exports.A, x2), y2),
F.add(F.one, F.mul(F.mul(x2, y2), exports.D)))) return false;
if (!F.equals(F.add(F.mul(exports.A, x2), y2), F.add(F.one, F.mul(F.mul(x2, y2), exports.D)))) return false;
return true;
}
@ -88,16 +95,14 @@ function unpackPoint(_buff) {
const P = new Array(2);
if (buff[31] & 0x80) {
sign = true;
buff[31] = buff[31] & 0x7F;
buff[31] = buff[31] & 0x7f;
}
P[1] = bigInt.leBuff2int(buff);
if (P[1].greaterOrEquals(exports.p)) return null;
const y2 = F.square(P[1]);
let x = F.sqrt(F.div(
F.sub(F.one, y2),
F.sub(exports.A, F.mul(exports.D, y2))));
let x = F.sqrt(F.div(F.sub(F.one, y2), F.sub(exports.A, F.mul(exports.D, y2))));
if (x == null) return null;

@ -1,5 +1,5 @@
const createBlakeHash = require("blake-hash");
const bigInt = require("snarkjs").bigInt;
const bigInt = require("@tornado/snarkjs").bigInt;
const babyJub = require("./babyjub");
const pedersenHash = require("./pedersenHash").hash;
const mimc7 = require("./mimc7");
@ -19,11 +19,10 @@ exports.packSignature = packSignature;
exports.unpackSignature = unpackSignature;
exports.pruneBuffer = pruneBuffer;
function pruneBuffer(_buff) {
const buff = Buffer.from(_buff);
buff[0] = buff[0] & 0xF8;
buff[31] = buff[31] & 0x7F;
buff[0] = buff[0] & 0xf8;
buff[31] = buff[31] & 0x7f;
buff[31] = buff[31] | 0x40;
return buff;
}
@ -41,7 +40,9 @@ function sign(prv, msg) {
const s = bigInt.leBuff2int(sBuff);
const A = babyJub.mulPointEscalar(babyJub.Base8, s.shr(3));
const rBuff = createBlakeHash("blake512").update(Buffer.concat([h1.slice(32,64), msg])).digest();
const rBuff = createBlakeHash("blake512")
.update(Buffer.concat([h1.slice(32, 64), msg]))
.digest();
let r = bigInt.leBuff2int(rBuff);
r = r.mod(babyJub.subOrder);
const R8 = babyJub.mulPointEscalar(babyJub.Base8, r);
@ -52,7 +53,7 @@ function sign(prv, msg) {
const S = r.add(hm.mul(s)).mod(babyJub.subOrder);
return {
R8: R8,
S: S
S: S,
};
}
@ -63,7 +64,9 @@ function signMiMC(prv, msg) {
const A = babyJub.mulPointEscalar(babyJub.Base8, s.shr(3));
const msgBuff = bigInt.leInt2Buff(msg, 32);
const rBuff = createBlakeHash("blake512").update(Buffer.concat([h1.slice(32,64), msgBuff])).digest();
const rBuff = createBlakeHash("blake512")
.update(Buffer.concat([h1.slice(32, 64), msgBuff]))
.digest();
let r = bigInt.leBuff2int(rBuff);
r = r.mod(babyJub.subOrder);
const R8 = babyJub.mulPointEscalar(babyJub.Base8, r);
@ -71,7 +74,7 @@ function signMiMC(prv, msg) {
const S = r.add(hm.mul(s)).mod(babyJub.subOrder);
return {
R8: R8,
S: S
S: S,
};
}
@ -82,7 +85,9 @@ function signMiMCSponge(prv, msg) {
const A = babyJub.mulPointEscalar(babyJub.Base8, s.shr(3));
const msgBuff = bigInt.leInt2Buff(msg, 32);
const rBuff = createBlakeHash("blake512").update(Buffer.concat([h1.slice(32,64), msgBuff])).digest();
const rBuff = createBlakeHash("blake512")
.update(Buffer.concat([h1.slice(32, 64), msgBuff]))
.digest();
let r = bigInt.leBuff2int(rBuff);
r = r.mod(babyJub.subOrder);
const R8 = babyJub.mulPointEscalar(babyJub.Base8, r);
@ -90,7 +95,7 @@ function signMiMCSponge(prv, msg) {
const S = r.add(hm.mul(s)).mod(babyJub.subOrder);
return {
R8: R8,
S: S
S: S,
};
}
@ -101,7 +106,9 @@ function signPoseidon(prv, msg) {
const A = babyJub.mulPointEscalar(babyJub.Base8, s.shr(3));
const msgBuff = bigInt.leInt2Buff(msg, 32);
const rBuff = createBlakeHash("blake512").update(Buffer.concat([h1.slice(32,64), msgBuff])).digest();
const rBuff = createBlakeHash("blake512")
.update(Buffer.concat([h1.slice(32, 64), msgBuff]))
.digest();
let r = bigInt.leBuff2int(rBuff);
r = r.mod(babyJub.subOrder);
const R8 = babyJub.mulPointEscalar(babyJub.Base8, r);
@ -109,7 +116,7 @@ function signPoseidon(prv, msg) {
const S = r.add(hm.mul(s)).mod(babyJub.subOrder);
return {
R8: R8,
S: S
S: S,
};
}
@ -160,7 +167,6 @@ function verifyMiMC(msg, sig, A) {
return true;
}
function verifyPoseidon(msg, sig, A) {
// Check parameters
if (typeof sig != "object") return false;
@ -214,8 +220,6 @@ function packSignature(sig) {
function unpackSignature(sigBuff) {
return {
R8: babyJub.unpackPoint(sigBuff.slice(0, 32)),
S: bigInt.leBuff2int(sigBuff.slice(32,64))
S: bigInt.leBuff2int(sigBuff.slice(32, 64)),
};
}

@ -3,9 +3,8 @@
//
const Contract = require("./evmasm");
const G2 = require("snarkjs").bn128.G2;
const bigInt = require("snarkjs").bigInt;
const G2 = require("@tornado/snarkjs").bn128.G2;
const bigInt = require("@tornado/snarkjs").bigInt;
function toHex256(a) {
let S = a.toString(16);
@ -14,13 +13,12 @@ function toHex256(a) {
}
function createCode(P, w) {
const C = new Contract();
const NPOINTS = 1 << (w - 1);
const VAR_POS = C.allocMem(32);
const VAR_POINTS = C.allocMem( (NPOINTS)*4*32);
const VAR_POINTS = C.allocMem(NPOINTS * 4 * 32);
const savedP = C.allocMem(32);
const savedZ3 = C.allocMem(32);
@ -96,7 +94,6 @@ function createCode(P, w) {
C.push("0x00");
C.return();
double();
addPoint();
affine();
@ -210,7 +207,6 @@ function createCode(P, w) {
C.and();
C.jumpi("enddouble"); // X Y Z q
// Z3 = 2*Y*Z // Remove Z
mul(2, 4, 6); // yz X Y Z q
rm(6); // X Y yz q
@ -279,8 +275,8 @@ function createCode(P, w) {
C.returnCall();
}
function addPoint() { // p, xR, xI, yR, yI, zR zI, q
function addPoint() {
// p, xR, xI, yR, yI, zR zI, q
C.dup(0); // p p X2 Y2 Z2 q
@ -290,7 +286,6 @@ function createCode(P, w) {
C.iszero(); // X2 Y2 Z2 q
C.jumpi("endpadd");
C.dup(4);
C.iszero();
C.dup(6);
@ -298,15 +293,12 @@ function createCode(P, w) {
C.and();
C.jumpi("returnP"); // X2 Y2 Z2 q
// lastZ3 = (Z2+1)^2 - Z2^2
add1(4, 6); // Z2+1 X2 Y2 Z2 q
square(0, 8); // (Z2+1)^2 Z2+1 X2 Y2 Z2 q
rm(2); // (Z2+1)^2 X2 Y2 Z2 q
square(6, 8); // Z2^2 (Z2+1)^2 X2 Y2 Z2 q
sub(2, 0, 10); // (Z2+1)^2-Z2^2 Z2^2 (Z2+1)^2 X2 Y2 Z2 q
saveZ3(); // Z2^2 (Z2+1)^2 X2 Y2 Z2 q
@ -315,13 +307,11 @@ function createCode(P, w) {
// U2 = X2
// S2 = Y2 // Z2^2 U2 S2 Z2 q
// U1 = X1 * Z2^2
loadX(); // X1 Z2^2 U2 S2 Z2 q
mul(0, 2, 10); // X1*Z2^2 X1 Z2^2 U2 S2 Z2 q
rm(2); // X1*Z2^2 Z2^2 U2 S2 Z2 q
mul(2, 8, 10); // Z2^3 U1 Z2^2 U2 S2 Z2 q
rm(4); // U1 Z2^3 U2 S2 Z2 q
rm(8); // Z2^3 U2 S2 U1 q
@ -337,7 +327,6 @@ function createCode(P, w) {
C.and(); // c2&c1 S1 U2 S2 U1 q
C.jumpi("double1"); // S1 U2 S2 U1 q
// Returns the double
// H = U2-U1 // Remove U2
@ -473,7 +462,8 @@ function createCode(P, w) {
}
}
function affine() { // X Y Z q
function affine() {
// X Y Z q
// If Z2=0 return 0
C.label("affine");
C.dup(4);
@ -551,33 +541,32 @@ function createCode(P, w) {
}
}
}
}
module.exports.abi = [
{
"constant": true,
"inputs": [
constant: true,
inputs: [
{
"name": "escalar",
"type": "uint256"
}
name: "escalar",
type: "uint256",
},
],
"name": "mulexp",
"outputs": [
name: "mulexp",
outputs: [
{
"name": "",
"type": "uint256"
name: "",
type: "uint256",
},
{
"name": "",
"type": "uint256"
}
name: "",
type: "uint256",
},
],
"payable": false,
"stateMutability": "pure",
"type": "function"
}
payable: false,
stateMutability: "pure",
type: "function",
},
];
module.exports.createCode = createCode;

@ -1,5 +1,5 @@
const bn128 = require("snarkjs").bn128;
const bigInt = require("snarkjs").bigInt;
const bn128 = require("@tornado/snarkjs").bn128;
const bigInt = require("@tornado/snarkjs").bigInt;
const Web3Utils = require("web3-utils");
const F = bn128.Fr;
@ -38,7 +38,7 @@ exports.hash = (_x_in, _k) =>{
let r;
for (let i = 0; i < NROUNDS; i++) {
const c = cts[i];
const t = (i==0) ? F.add(x_in, k) : F.add(F.add(r, k), c);
const t = i == 0 ? F.add(x_in, k) : F.add(F.add(r, k), c);
r = F.exp(t, 7);
}
return F.affine(F.add(r, k));
@ -46,19 +46,13 @@ exports.hash = (_x_in, _k) =>{
exports.multiHash = (arr, key) => {
let r;
if (typeof(key) === "undefined") {
if (typeof key === "undefined") {
r = F.zero;
} else {
r = key;
}
for (let i = 0; i < arr.length; i++) {
r = F.add(
F.add(
r,
arr[i]
),
exports.hash(bigInt(arr[i]), r)
);
r = F.add(F.add(r, arr[i]), exports.hash(bigInt(arr[i]), r));
}
return F.affine(r);
};

@ -1,5 +1,5 @@
const bn128 = require("snarkjs").bn128;
const bigInt = require("snarkjs").bigInt;
const bn128 = require("@tornado/snarkjs").bn128;
const bigInt = require("@tornado/snarkjs").bigInt;
const Web3Utils = require("web3-utils");
const F = bn128.Fr;
@ -39,9 +39,9 @@ exports.hash = (_xL_in, _xR_in, _k) =>{
const k = bigInt(_k);
for (let i = 0; i < NROUNDS; i++) {
const c = cts[i];
const t = (i==0) ? F.add(xL, k) : F.add(F.add(xL, k), c);
const t = i == 0 ? F.add(xL, k) : F.add(F.add(xL, k), c);
const xR_tmp = bigInt(xR);
if (i < (NROUNDS - 1)) {
if (i < NROUNDS - 1) {
xR = xL;
xL = F.add(xR_tmp, F.exp(t, 5));
} else {
@ -55,10 +55,10 @@ exports.hash = (_xL_in, _xR_in, _k) =>{
};
exports.multiHash = (arr, key, numOutputs) => {
if (typeof(numOutputs) === "undefined") {
if (typeof numOutputs === "undefined") {
numOutputs = 1;
}
if (typeof(key) === "undefined") {
if (typeof key === "undefined") {
key = F.zero;
}
@ -81,6 +81,6 @@ exports.multiHash = (arr, key, numOutputs) => {
if (numOutputs == 1) {
return F.affine(outputs[0]);
} else {
return outputs.map(x => F.affine(x));
return outputs.map((x) => F.affine(x));
}
};

@ -1,5 +1,5 @@
const bn128 = require("snarkjs").bn128;
const bigInt = require("snarkjs").bigInt;
const bn128 = require("@tornado/snarkjs").bn128;
const bigInt = require("@tornado/snarkjs").bigInt;
const babyJub = require("./babyjub");
const createBlakeHash = require("blake-hash");
@ -21,7 +21,7 @@ function pedersenHash(msg) {
for (let s = 0; s < nSegments; s++) {
let nWindows;
if (s == nSegments - 1) {
nWindows = Math.floor(((bits.length - (nSegments - 1)*bitsPerSegment) - 1) / windowSize) +1;
nWindows = Math.floor((bits.length - (nSegments - 1) * bitsPerSegment - 1) / windowSize) + 1;
} else {
nWindows = nWindowsPerSegment;
}
@ -30,7 +30,7 @@ function pedersenHash(msg) {
for (let w = 0; w < nWindows; w++) {
let o = s * bitsPerSegment + w * windowSize;
let acc = bigInt.one;
for (let b=0; ((b<windowSize-1)&&(o<bits.length)) ; b++) {
for (let b = 0; b < windowSize - 1 && o < bits.length; b++) {
if (bits[o]) {
acc = acc.add(bigInt.one.shl(b));
}
@ -65,7 +65,7 @@ function getBasePoint(pointIdx) {
while (p == null) {
const S = GENPOINT_PREFIX + "_" + padLeftZeros(pointIdx, 32) + "_" + padLeftZeros(tryIdx, 32);
const h = createBlakeHash("blake256").update(S).digest();
h[31] = h[31] & 0xBF; // Set 255th bit to 0 (256th is the signal and 254th is the last possible bit to 1)
h[31] = h[31] & 0xbf; // Set 255th bit to 0 (256th is the signal and 254th is the last possible bit to 1)
p = babyJub.unpackPoint(h);
tryIdx++;
}
@ -105,7 +105,3 @@ function buffer2bits(buff) {
}
return res;
}

@ -1,8 +1,8 @@
const assert = require("assert");
const bn128 = require("snarkjs").bn128;
const bigInt = require("snarkjs").bigInt;
const bn128 = require("@tornado/snarkjs").bn128;
const bigInt = require("@tornado/snarkjs").bigInt;
const F = bn128.Fr;
const { unstringifyBigInts } = require("snarkjs");
const { unstringifyBigInts } = require("@tornado/snarkjs");
// Prime 0x30644e72e131a029b85045b68181585d2833e84879b9709143e1f593f0000001
// const F = new ZqField(Scalar.fromString("21888242871839275222246405745257275088548364400416034343698204186575808495617"));
@ -17,7 +17,7 @@ const { C, M } = unstringifyBigInts(require("./poseidon_constants.json"));
const N_ROUNDS_F = 8;
const N_ROUNDS_P = 35;
const pow5 = a => F.mul(a, F.square(F.square(a, a)));
const pow5 = (a) => F.mul(a, F.square(F.square(a, a)));
function poseidon(inputs) {
assert(inputs.length > 0);
@ -27,21 +27,19 @@ function poseidon(inputs) {
const nRoundsF = N_ROUNDS_F;
const nRoundsP = N_ROUNDS_P;
let state = [...inputs.map(a => bigInt(a)), F.zero];
let state = [...inputs.map((a) => bigInt(a)), F.zero];
for (let r = 0; r < nRoundsF + nRoundsP; r++) {
state = state.map((a, i) => F.add(a, bigInt(C[t - 2][r * t + i])));
if (r < nRoundsF / 2 || r >= nRoundsF / 2 + nRoundsP) {
state = state.map(a => pow5(a));
state = state.map((a) => pow5(a));
} else {
state[0] = pow5(state[0]);
}
// no matrix multiplication in the last round
if (r < nRoundsF + nRoundsP - 1) {
state = state.map((_, i) =>
state.reduce((acc, a, j) => F.add(acc, F.mul(bigInt(M[t - 2][j][i]), a)), F.zero)
);
state = state.map((_, i) => state.reduce((acc, a, j) => F.add(acc, F.mul(bigInt(M[t - 2][j][i]), a)), F.zero));
}
}
return F.affine(state[0]);

@ -3,7 +3,7 @@
//
const Contract = require("./evmasm");
const { unstringifyBigInts } = require("snarkjs");
const { unstringifyBigInts } = require("@tornado/snarkjs");
const Web3Utils = require("web3-utils");
const { C: K, M } = unstringifyBigInts(require("./poseidon_constants.json"));
@ -21,8 +21,7 @@ function toHex256(a) {
}
function createCode(nInputs) {
if (( nInputs<1) || (nInputs>4)) throw new Error("Invalid number of inputs. Must be 1<=nInputs<=8");
if (nInputs < 1 || nInputs > 4) throw new Error("Invalid number of inputs. Must be 1<=nInputs<=8");
const t = nInputs + 1;
const nRoundsF = N_ROUNDS_F;
const nRoundsP = N_ROUNDS_P;
@ -39,7 +38,8 @@ function createCode(nInputs) {
}
}
function ark(r) { // st, q
function ark(r) {
// st, q
for (let i = 0; i < t; i++) {
C.dup(t); // q, st, q
C.push(toHex256(K[t - 2][r * t + i])); // K, q, st, q
@ -89,7 +89,7 @@ function createCode(nInputs) {
}
}
for (let i = 0; i < t; i++) {
C.swap((t -i) + (t -i-1));
C.swap(t - i + (t - i - 1));
C.pop();
}
C.push(0);
@ -97,7 +97,6 @@ function createCode(nInputs) {
C.jmp();
}
// Check selector
C.push("0x0100000000000000000000000000000000000000000000000000000000");
C.push(0);
@ -124,13 +123,13 @@ function createCode(nInputs) {
// [Selector (4)] [item1 (32)] [item2 (32)] ....
// Stack positions 0-nInputs.
for (let i = 0; i < t; i++) {
C.push(0x04+(0x20*(nInputs-i)));
C.push(0x04 + 0x20 * (nInputs - i));
C.calldataload();
}
for (let i = 0; i < nRoundsF + nRoundsP - 1; i++) {
ark(i);
if ((i<nRoundsF/2) || (i>=nRoundsP+nRoundsF/2)) {
if (i < nRoundsF / 2 || i >= nRoundsP + nRoundsF / 2) {
for (let j = 0; j < t; j++) {
sigma(j);
}
@ -166,51 +165,49 @@ function createCode(nInputs) {
function generateABI(nInputs) {
return [
{
"constant": true,
"inputs": [
constant: true,
inputs: [
{
"internalType": `bytes32[${nInputs}]`,
"name": "input",
"type": `bytes32[${nInputs}]`
}
internalType: `bytes32[${nInputs}]`,
name: "input",
type: `bytes32[${nInputs}]`,
},
],
"name": "poseidon",
"outputs": [
name: "poseidon",
outputs: [
{
"internalType": "bytes32",
"name": "",
"type": "bytes32"
}
internalType: "bytes32",
name: "",
type: "bytes32",
},
],
"payable": false,
"stateMutability": "pure",
"type": "function"
payable: false,
stateMutability: "pure",
type: "function",
},
{
"constant": true,
"inputs": [
constant: true,
inputs: [
{
"internalType": `uint256[${nInputs}]`,
"name": "input",
"type": `uint256[${nInputs}]`
}
internalType: `uint256[${nInputs}]`,
name: "input",
type: `uint256[${nInputs}]`,
},
],
"name": "poseidon",
"outputs": [
name: "poseidon",
outputs: [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
internalType: "uint256",
name: "",
type: "uint256",
},
],
"payable": false,
"stateMutability": "pure",
"type": "function"
}
payable: false,
stateMutability: "pure",
type: "function",
},
];
}
module.exports.generateABI = generateABI;
module.exports.createCode = createCode;

@ -1,17 +1,15 @@
const bigInt = require("snarkjs").bigInt;
const bigInt = require("@tornado/snarkjs").bigInt;
const SMTMemDB = require("./smt_memdb");
const { hash0, hash1 } = require("./smt_hashes_poseidon");
class SMT {
constructor(db, root) {
this.db = db;
this.root = root;
}
_splitBits(_key) {
let k = bigInt(_key);
const res = [];
@ -33,7 +31,6 @@ class SMT {
const key = bigInt(_key);
const newValue = bigInt(_newValue);
const resFind = await this.find(key);
const res = {};
res.oldRoot = this.root;
@ -87,7 +84,7 @@ class SMT {
const res = {
siblings: [],
delKey: key,
delValue: resFind.foundValue
delValue: resFind.foundValue,
};
const dels = [];
@ -99,7 +96,7 @@ class SMT {
let mixed;
if (resFind.siblings.length > 0) {
const record = await this.db.get(resFind.siblings[resFind.siblings.length - 1]);
if ((record.length == 3)&&(record[0].equals(bigInt.one))) {
if (record.length == 3 && record[0].equals(bigInt.one)) {
mixed = false;
res.oldKey = record[1];
res.oldValue = record[2];
@ -125,7 +122,7 @@ class SMT {
for (let level = resFind.siblings.length - 1; level >= 0; level--) {
let newSibling = resFind.siblings[level];
if ((level == resFind.siblings.length-1)&&(!res.isOld0)) {
if (level == resFind.siblings.length - 1 && !res.isOld0) {
newSibling = bigInt.zero;
}
const oldSibling = resFind.siblings[level];
@ -201,7 +198,7 @@ class SMT {
inserts.push([rt, [1, key, value]]);
for (let i = res.siblings.length - 1; i >= 0; i--) {
if ((i<res.siblings.length-1)&&(!res.siblings[i].isZero())) {
if (i < res.siblings.length - 1 && !res.siblings[i].isZero()) {
mixed = true;
}
if (mixed) {
@ -214,7 +211,6 @@ class SMT {
dels.push(rtOld);
}
let newRt;
if (newKeyBits[i]) {
newRt = hash0(res.siblings[i], rt);
@ -227,7 +223,7 @@ class SMT {
}
if (addedOne) res.siblings.pop();
while ((res.siblings.length>0) && (res.siblings[res.siblings.length-1].isZero())) {
while (res.siblings.length > 0 && res.siblings[res.siblings.length - 1].isZero()) {
res.siblings.pop();
}
res.oldKey = resFind.notFoundKey;
@ -235,7 +231,6 @@ class SMT {
res.newRoot = rt;
res.isOld0 = resFind.isOld0;
await this.db.multiIns(inserts);
await this.db.setRoot(rt);
this.root = rt;
@ -259,20 +254,20 @@ class SMT {
siblings: [],
notFoundKey: key,
notFoundValue: bigInt.zero,
isOld0: true
isOld0: true,
};
return res;
}
const record = await this.db.get(root);
if ((record.length==3)&&(record[0].equals(bigInt.one))) {
if (record.length == 3 && record[0].equals(bigInt.one)) {
if (record[1].equals(key)) {
res = {
found: true,
siblings: [],
foundValue: record[2],
isOld0: false
isOld0: false,
};
} else {
res = {
@ -280,7 +275,7 @@ class SMT {
siblings: [],
notFoundKey: record[1],
notFoundValue: record[2],
isOld0: false
isOld0: false,
};
}
} else {
@ -296,9 +291,7 @@ class SMT {
}
}
async function loadFromFile(fileName) {
}
async function loadFromFile(fileName) {}
async function newMemEmptyTrie() {
const db = new SMTMemDB();

@ -1,5 +1,5 @@
const mimc7 = require("./mimc7");
const bigInt = require("snarkjs").bigInt;
const bigInt = require("@tornado/snarkjs").bigInt;
exports.hash0 = function (left, right) {
return mimc7.multiHash(left, right);

@ -1,5 +1,5 @@
const poseidon = require("./poseidon");
const bigInt = require("snarkjs").bigInt;
const bigInt = require("@tornado/snarkjs").bigInt;
exports.hash0 = function (left, right) {
return poseidon([left, right]);

@ -1,4 +1,4 @@
const bigInt = require("snarkjs").bigInt;
const bigInt = require("@tornado/snarkjs").bigInt;
class SMTMemDb {
constructor() {

@ -1,6 +1,6 @@
const chai = require("chai");
const path = require("path");
const snarkjs = require("snarkjs");
const snarkjs = require("@tornado/snarkjs");
const compiler = require("circom");
const assert = chai.assert;
@ -62,7 +62,6 @@ describe("Aliascheck test", () => {
});
it("Nhot not satisfy all ones", async () => {
const inp = getBits(bigInt(1).shl(254).sub(bigInt(1)), 254);
try {
circuit.calculateWitness({ in: inp });
@ -72,5 +71,4 @@ describe("Aliascheck test", () => {
assert(err.message.indexOf("1 != 0") >= 0);
}
});
});

@ -1,6 +1,6 @@
const chai = require("chai");
const path = require("path");
const snarkjs = require("snarkjs");
const snarkjs = require("@tornado/snarkjs");
const compiler = require("circom");
const assert = chai.assert;
@ -71,5 +71,4 @@ describe("Aliascheck test", () => {
assert(err.message.indexOf("1 != 0") >= 0);
}
});
});

@ -1,6 +1,6 @@
const chai = require("chai");
const path = require("path");
const snarkjs = require("snarkjs");
const snarkjs = require("@tornado/snarkjs");
const compiler = require("circom");
const createBlakeHash = require("blake-hash");
@ -8,7 +8,7 @@ const eddsa = require("../src/eddsa.js");
const assert = chai.assert;
const bigInt = require("snarkjs").bigInt;
const bigInt = require("@tornado/snarkjs").bigInt;
describe("Baby Jub test", function () {
let circuitAdd;
@ -28,16 +28,14 @@ describe("Baby Jub test", function () {
const cirDefPbk = await compiler(path.join(__dirname, "circuits", "babypbk_test.circom"));
circuitPbk = new snarkjs.Circuit(cirDefPbk);
console.log("NConstrains BabyPbk: " + circuitPbk.nConstraints);
});
it("Should add point (0,1) and (0,1)", async () => {
const input = {
x1: snarkjs.bigInt(0),
y1: snarkjs.bigInt(1),
x2: snarkjs.bigInt(0),
y2: snarkjs.bigInt(1)
y2: snarkjs.bigInt(1),
};
const w = circuitAdd.calculateWitness(input);
@ -50,12 +48,11 @@ describe("Baby Jub test", function () {
});
it("Should add 2 same numbers", async () => {
const input = {
x1: snarkjs.bigInt("17777552123799933955779906779655732241715742912184938656739573121738514868268"),
y1: snarkjs.bigInt("2626589144620713026669568689430873010625803728049924121243784502389097019475"),
x2: snarkjs.bigInt("17777552123799933955779906779655732241715742912184938656739573121738514868268"),
y2: snarkjs.bigInt("2626589144620713026669568689430873010625803728049924121243784502389097019475")
y2: snarkjs.bigInt("2626589144620713026669568689430873010625803728049924121243784502389097019475"),
};
const w = circuitAdd.calculateWitness(input);
@ -68,12 +65,11 @@ describe("Baby Jub test", function () {
});
it("Should add 2 different numbers", async () => {
const input = {
x1: snarkjs.bigInt("17777552123799933955779906779655732241715742912184938656739573121738514868268"),
y1: snarkjs.bigInt("2626589144620713026669568689430873010625803728049924121243784502389097019475"),
x2: snarkjs.bigInt("16540640123574156134436876038791482806971768689494387082833631921987005038935"),
y2: snarkjs.bigInt("20819045374670962167435360035096875258406992893633759881276124905556507972311")
y2: snarkjs.bigInt("20819045374670962167435360035096875258406992893633759881276124905556507972311"),
};
const w = circuitAdd.calculateWitness(input);
@ -106,7 +102,6 @@ describe("Baby Jub test", function () {
});
it("Should extract the public key from the private one", async () => {
const rawpvk = Buffer.from("0001020304050607080900010203040506070809000102030405060708090021", "hex");
const pvk = eddsa.pruneBuffer(createBlakeHash("blake512").update(rawpvk).digest().slice(0, 32));
const S = bigInt.leBuff2int(pvk).shr(3);
@ -116,11 +111,10 @@ describe("Baby Jub test", function () {
const input = {
in: S,
Ax: A[0],
Ay : A[1]
}
Ay: A[1],
};
const w = circuitPbk.calculateWitness(input);
assert(circuitPbk.checkWitness(w));
});
});

@ -1,6 +1,6 @@
const chai = require("chai");
const path = require("path");
const snarkjs = require("snarkjs");
const snarkjs = require("@tornado/snarkjs");
const compiler = require("circom");
const babyjub = require("../src/babyjub.js");
@ -8,22 +8,14 @@ const assert = chai.assert;
// const bigInt = require("big-integer");
describe("Baby Jub js test", function () {
this.timeout(100000);
it("Should add point (0,1) and (0,1)", () => {
const p1 = [snarkjs.bigInt(0), snarkjs.bigInt(1)];
const p2 = [snarkjs.bigInt(0), snarkjs.bigInt(1)];
const p1 = [
snarkjs.bigInt(0),
snarkjs.bigInt(1)];
const p2 = [
snarkjs.bigInt(0),
snarkjs.bigInt(1)
];
const out = babyjub.addPoint(p1, p2)
const out = babyjub.addPoint(p1, p2);
assert(out[0].equals(0));
assert(out[1].equals(1));
});
@ -39,7 +31,6 @@ describe("Baby Jub js test", function () {
});
it("Should add 2 same numbers", () => {
const p1 = [
snarkjs.bigInt("17777552123799933955779906779655732241715742912184938656739573121738514868268"),
snarkjs.bigInt("2626589144620713026669568689430873010625803728049924121243784502389097019475"),
@ -49,13 +40,12 @@ describe("Baby Jub js test", function () {
snarkjs.bigInt("2626589144620713026669568689430873010625803728049924121243784502389097019475"),
];
const out = babyjub.addPoint(p1, p2)
const out = babyjub.addPoint(p1, p2);
assert(out[0].equals(snarkjs.bigInt("6890855772600357754907169075114257697580319025794532037257385534741338397365")));
assert(out[1].equals(snarkjs.bigInt("4338620300185947561074059802482547481416142213883829469920100239455078257889")));
});
it("Should add 2 different numbers", () => {
const p1 = [
snarkjs.bigInt("17777552123799933955779906779655732241715742912184938656739573121738514868268"),
snarkjs.bigInt("2626589144620713026669568689430873010625803728049924121243784502389097019475"),
@ -65,7 +55,7 @@ describe("Baby Jub js test", function () {
snarkjs.bigInt("20819045374670962167435360035096875258406992893633759881276124905556507972311"),
];
const out = babyjub.addPoint(p1, p2)
const out = babyjub.addPoint(p1, p2);
assert(out[0].equals(snarkjs.bigInt("7916061937171219682591368294088513039687205273691143098332585753343424131937")));
assert(out[1].equals(snarkjs.bigInt("14035240266687799601661095864649209771790948434046947201833777492504781204499")));
@ -92,7 +82,10 @@ describe("Baby Jub js test", function () {
snarkjs.bigInt("2626589144620713026669568689430873010625803728049924121243784502389097019475"),
];
const r = babyjub.mulPointEscalar(p, snarkjs.bigInt("14035240266687799601661095864649209771790948434046947201833777492504781204499"));
const r = babyjub.mulPointEscalar(
p,
snarkjs.bigInt("14035240266687799601661095864649209771790948434046947201833777492504781204499")
);
assert.equal(r[0].toString(), "17070357974431721403481313912716834497662307308519659060910483826664480189605");
assert.equal(r[1].toString(), "4014745322800118607127020275658861516666525056516280575712425373174125159339");
});
@ -103,7 +96,10 @@ describe("Baby Jub js test", function () {
snarkjs.bigInt("4338620300185947561074059802482547481416142213883829469920100239455078257889"),
];
const r = babyjub.mulPointEscalar(p, snarkjs.bigInt("20819045374670962167435360035096875258406992893633759881276124905556507972311"));
const r = babyjub.mulPointEscalar(
p,
snarkjs.bigInt("20819045374670962167435360035096875258406992893633759881276124905556507972311")
);
assert.equal(r[0].toString(), "13563888653650925984868671744672725781658357821216877865297235725727006259983");
assert.equal(r[1].toString(), "8442587202676550862664528699803615547505326611544120184665036919364004251662");
});
@ -146,7 +142,7 @@ describe("Baby Jub js test", function () {
snarkjs.bigInt("2626589144620713026669568689430873010625803728049924121243784502389097019475"),
];
const buf = babyjub.packPoint(p);
assert.equal(buf.toString('hex'), '53b81ed5bffe9545b54016234682e7b2f699bd42a5e9eae27ff4051bc698ce85');
assert.equal(buf.toString("hex"), "53b81ed5bffe9545b54016234682e7b2f699bd42a5e9eae27ff4051bc698ce85");
const p2 = babyjub.unpackPoint(buf);
assert.equal(p2[0].toString(), "17777552123799933955779906779655732241715742912184938656739573121738514868268");
assert.equal(p2[1].toString(), "2626589144620713026669568689430873010625803728049924121243784502389097019475");
@ -158,7 +154,7 @@ describe("Baby Jub js test", function () {
snarkjs.bigInt("4338620300185947561074059802482547481416142213883829469920100239455078257889"),
];
const buf = babyjub.packPoint(p);
assert.equal(buf.toString('hex'), 'e114eb17eddf794f063a68fecac515e3620e131976108555735c8b0773929709');
assert.equal(buf.toString("hex"), "e114eb17eddf794f063a68fecac515e3620e131976108555735c8b0773929709");
const p2 = babyjub.unpackPoint(buf);
assert.equal(p2[0].toString(), "6890855772600357754907169075114257697580319025794532037257385534741338397365");
assert.equal(p2[1].toString(), "4338620300185947561074059802482547481416142213883829469920100239455078257889");

@ -1,6 +1,6 @@
const chai = require("chai");
const path = require("path");
const snarkjs = require("snarkjs");
const snarkjs = require("@tornado/snarkjs");
const compiler = require("circom");
const assert = chai.assert;
@ -51,6 +51,4 @@ describe("BinSub test", () => {
checkSub(-2, 2, circuit);
checkSub(-2, 3, circuit);
});
});

@ -1,6 +1,6 @@
const chai = require("chai");
const path = require("path");
const snarkjs = require("snarkjs");
const snarkjs = require("@tornado/snarkjs");
const crypto = require("crypto");
const compiler = require("circom");
@ -9,25 +9,23 @@ const assert = chai.assert;
describe("Sum test", () => {
it("Should create a constant circuit", async () => {
const cirDef = await compiler(path.join(__dirname, "circuits", "constants_test.circom"));
assert.equal(cirDef.nVars, 2);
const circuit = new snarkjs.Circuit(cirDef);
const witness = circuit.calculateWitness({ "in": "0xd807aa98" });
const witness = circuit.calculateWitness({ in: "0xd807aa98" });
assert(witness[0].equals(snarkjs.bigInt(1)));
assert(witness[1].equals(snarkjs.bigInt("0xd807aa98")));
});
it("Should create a sum circuit", async () => {
const cirDef = await compiler(path.join(__dirname, "circuits", "sum_test.circom"));
assert.equal(cirDef.nVars, 97); // 32 (in1) + 32(in2) + 32(out) + 1 (carry)
const circuit = new snarkjs.Circuit(cirDef);
const witness = circuit.calculateWitness({ "a": "111", "b": "222" });
const witness = circuit.calculateWitness({ a: "111", b: "222" });
assert(witness[0].equals(snarkjs.bigInt(1)));
assert(witness[1].equals(snarkjs.bigInt("333")));

@ -1,6 +1,6 @@
const chai = require("chai");
const path = require("path");
const snarkjs = require("snarkjs");
const snarkjs = require("@tornado/snarkjs");
const crypto = require("crypto");
const compiler = require("circom");
@ -14,11 +14,11 @@ describe("Sum test", () => {
const circuit = new snarkjs.Circuit(cirDef);
let witness;
witness = circuit.calculateWitness({ "in": 111});
witness = circuit.calculateWitness({ in: 111 });
assert(witness[0].equals(snarkjs.bigInt(1)));
assert(witness[1].equals(snarkjs.bigInt(0)));
witness = circuit.calculateWitness({ "in": 0 });
witness = circuit.calculateWitness({ in: 0 });
assert(witness[0].equals(snarkjs.bigInt(1)));
assert(witness[1].equals(snarkjs.bigInt(1)));
});

@ -1,6 +1,6 @@
const chai = require("chai");
const path = require("path");
const snarkjs = require("snarkjs");
const snarkjs = require("@tornado/snarkjs");
const compiler = require("circom");
// const crypto = require("crypto");
@ -29,7 +29,6 @@ function buffer2bits(buff) {
return res;
}
describe("EdDSA test", function () {
let circuit;
@ -69,6 +68,5 @@ describe("EdDSA test", function () {
const w = circuit.calculateWitness({ A: aBits, R8: r8Bits, S: sBits, msg: msgBits });
assert(circuit.checkWitness(w));
});
});

@ -1,5 +1,5 @@
const chai = require("chai");
const snarkjs = require("snarkjs");
const snarkjs = require("@tornado/snarkjs");
const eddsa = require("../src/eddsa.js");
const babyJub = require("../src/babyjub.js");
@ -9,7 +9,6 @@ const assert = chai.assert;
const bigInt = snarkjs.bigInt;
describe("EdDSA js test", function () {
this.timeout(100000);
it("Sign (using Mimc7) a single 10 bytes from 0 to 9", () => {
@ -22,29 +21,26 @@ describe("EdDSA js test", function () {
const pubKey = eddsa.prv2pub(prvKey);
assert.equal(pubKey[0].toString(),
"13277427435165878497778222415993513565335242147425444199013288855685581939618");
assert.equal(pubKey[1].toString(),
"13622229784656158136036771217484571176836296686641868549125388198837476602820");
assert.equal(pubKey[0].toString(), "13277427435165878497778222415993513565335242147425444199013288855685581939618");
assert.equal(pubKey[1].toString(), "13622229784656158136036771217484571176836296686641868549125388198837476602820");
const pPubKey = babyJub.packPoint(pubKey);
const signature = eddsa.signMiMC(prvKey, msg);
assert.equal(signature.R8[0].toString(),
"11384336176656855268977457483345535180380036354188103142384839473266348197733");
assert.equal(signature.R8[1].toString(),
"15383486972088797283337779941324724402501462225528836549661220478783371668959");
assert.equal(signature.S.toString(),
"2523202440825208709475937830811065542425109372212752003460238913256192595070");
assert.equal(signature.R8[0].toString(), "11384336176656855268977457483345535180380036354188103142384839473266348197733");
assert.equal(signature.R8[1].toString(), "15383486972088797283337779941324724402501462225528836549661220478783371668959");
assert.equal(signature.S.toString(), "2523202440825208709475937830811065542425109372212752003460238913256192595070");
const pSignature = eddsa.packSignature(signature);
assert.equal(pSignature.toString("hex"), ""+
assert.equal(
pSignature.toString("hex"),
"" +
"dfedb4315d3f2eb4de2d3c510d7a987dcab67089c8ace06308827bf5bcbe02a2" +
"7ed40dab29bf993c928e789d007387998901a24913d44fddb64b1f21fc149405");
"7ed40dab29bf993c928e789d007387998901a24913d44fddb64b1f21fc149405"
);
const uSignature = eddsa.unpackSignature(pSignature);
assert(eddsa.verifyMiMC(msg, uSignature, pubKey));
});
it("Sign (using Poseidon) a single 10 bytes from 0 to 9", () => {
@ -55,28 +51,25 @@ describe("EdDSA js test", function () {
const pubKey = eddsa.prv2pub(prvKey);
assert.equal(pubKey[0].toString(),
"13277427435165878497778222415993513565335242147425444199013288855685581939618");
assert.equal(pubKey[1].toString(),
"13622229784656158136036771217484571176836296686641868549125388198837476602820");
assert.equal(pubKey[0].toString(), "13277427435165878497778222415993513565335242147425444199013288855685581939618");
assert.equal(pubKey[1].toString(), "13622229784656158136036771217484571176836296686641868549125388198837476602820");
const pPubKey = babyJub.packPoint(pubKey);
const signature = eddsa.signPoseidon(prvKey, msg);
assert.equal(signature.R8[0].toString(),
"11384336176656855268977457483345535180380036354188103142384839473266348197733");
assert.equal(signature.R8[1].toString(),
"15383486972088797283337779941324724402501462225528836549661220478783371668959");
assert.equal(signature.S.toString(),
"1398758333392199195742243841591064350253744445503462896781493968760929513778");
assert.equal(signature.R8[0].toString(), "11384336176656855268977457483345535180380036354188103142384839473266348197733");
assert.equal(signature.R8[1].toString(), "15383486972088797283337779941324724402501462225528836549661220478783371668959");
assert.equal(signature.S.toString(), "1398758333392199195742243841591064350253744445503462896781493968760929513778");
const pSignature = eddsa.packSignature(signature);
assert.equal(pSignature.toString("hex"), ""+
assert.equal(
pSignature.toString("hex"),
"" +
"dfedb4315d3f2eb4de2d3c510d7a987dcab67089c8ace06308827bf5bcbe02a2" +
"32f16b0f2f4c4e1169aa59685637e1429b6581a9531d058d65f4ab224eab1703");
"32f16b0f2f4c4e1169aa59685637e1429b6581a9531d058d65f4ab224eab1703"
);
const uSignature = eddsa.unpackSignature(pSignature);
assert(eddsa.verifyPoseidon(msg, uSignature, pubKey));
});
});

@ -1,6 +1,6 @@
const chai = require("chai");
const path = require("path");
const snarkjs = require("snarkjs");
const snarkjs = require("@tornado/snarkjs");
const compiler = require("circom");
const eddsa = require("../src/eddsa.js");
@ -40,7 +40,8 @@ describe("EdDSA MiMC test", function () {
R8x: signature.R8[0],
R8y: signature.R8[1],
S: signature.S,
M: msg});
M: msg,
});
assert(circuit.checkWitness(w));
});
@ -52,7 +53,6 @@ describe("EdDSA MiMC test", function () {
const pubKey = eddsa.prv2pub(prvKey);
const signature = eddsa.signMiMC(prvKey, msg);
assert(eddsa.verifyMiMC(msg, signature, pubKey));
@ -64,14 +64,14 @@ describe("EdDSA MiMC test", function () {
R8x: signature.R8[0].add(bigInt(1)),
R8y: signature.R8[1],
S: signature.S,
M: msg});
M: msg,
});
assert(false);
} catch (err) {
assert(/Constraint\sdoesn't\smatch(.*)1\s!=\s0/.test(err.message));
}
});
it("Test a dissabled circuit with a bad signature", async () => {
const msg = bigInt(1234);
@ -79,7 +79,6 @@ describe("EdDSA MiMC test", function () {
const pubKey = eddsa.prv2pub(prvKey);
const signature = eddsa.signMiMC(prvKey, msg);
assert(eddsa.verifyMiMC(msg, signature, pubKey));
@ -91,7 +90,8 @@ describe("EdDSA MiMC test", function () {
R8x: signature.R8[0].add(bigInt(1)),
R8y: signature.R8[1],
S: signature.S,
M: msg});
M: msg,
});
assert(circuit.checkWitness(w));
});

@ -1,6 +1,6 @@
const chai = require("chai");
const path = require("path");
const snarkjs = require("snarkjs");
const snarkjs = require("@tornado/snarkjs");
const compiler = require("circom");
const eddsa = require("../src/eddsa.js");
@ -40,7 +40,8 @@ describe("EdDSA MiMCSponge test", function () {
R8x: signature.R8[0],
R8y: signature.R8[1],
S: signature.S,
M: msg});
M: msg,
});
assert(circuit.checkWitness(w));
});
@ -52,7 +53,6 @@ describe("EdDSA MiMCSponge test", function () {
const pubKey = eddsa.prv2pub(prvKey);
const signature = eddsa.signMiMCSponge(prvKey, msg);
assert(eddsa.verifyMiMCSponge(msg, signature, pubKey));
@ -64,7 +64,8 @@ describe("EdDSA MiMCSponge test", function () {
R8x: signature.R8[0].add(bigInt(1)),
R8y: signature.R8[1],
S: signature.S,
M: msg});
M: msg,
});
assert(false);
} catch (err) {
assert(err.message.indexOf("Constraint doesn't match") >= 0);
@ -72,7 +73,6 @@ describe("EdDSA MiMCSponge test", function () {
}
});
it("Test a dissabled circuit with a bad signature", async () => {
const msg = bigInt(1234);
@ -80,7 +80,6 @@ describe("EdDSA MiMCSponge test", function () {
const pubKey = eddsa.prv2pub(prvKey);
const signature = eddsa.signMiMCSponge(prvKey, msg);
assert(eddsa.verifyMiMCSponge(msg, signature, pubKey));
@ -92,7 +91,8 @@ describe("EdDSA MiMCSponge test", function () {
R8x: signature.R8[0].add(bigInt(1)),
R8y: signature.R8[1],
S: signature.S,
M: msg});
M: msg,
});
assert(circuit.checkWitness(w));
});

@ -1,6 +1,6 @@
const chai = require("chai");
const path = require("path");
const snarkjs = require("snarkjs");
const snarkjs = require("@tornado/snarkjs");
const compiler = require("circom");
const eddsa = require("../src/eddsa.js");
@ -40,7 +40,8 @@ describe("EdDSA Poseidon test", function () {
R8x: signature.R8[0],
R8y: signature.R8[1],
S: signature.S,
M: msg});
M: msg,
});
assert(circuit.checkWitness(w));
});
@ -52,7 +53,6 @@ describe("EdDSA Poseidon test", function () {
const pubKey = eddsa.prv2pub(prvKey);
const signature = eddsa.signPoseidon(prvKey, msg);
assert(eddsa.verifyPoseidon(msg, signature, pubKey));
@ -64,14 +64,14 @@ describe("EdDSA Poseidon test", function () {
R8x: signature.R8[0].add(bigInt(1)),
R8y: signature.R8[1],
S: signature.S,
M: msg});
M: msg,
});
assert(false);
} catch (err) {
assert(/Constraint\sdoesn't\smatch(.*)1\s!=\s0/.test(err.message));
}
});
it("Test a dissabled circuit with a bad signature", async () => {
const msg = bigInt(1234);
@ -79,7 +79,6 @@ describe("EdDSA Poseidon test", function () {
const pubKey = eddsa.prv2pub(prvKey);
const signature = eddsa.signPoseidon(prvKey, msg);
assert(eddsa.verifyPoseidon(msg, signature, pubKey));
@ -91,7 +90,8 @@ describe("EdDSA Poseidon test", function () {
R8x: signature.R8[0].add(bigInt(1)),
R8y: signature.R8[1],
S: signature.S,
M: msg});
M: msg,
});
assert(circuit.checkWitness(w));
});

@ -1,13 +1,12 @@
const chai = require("chai");
const path = require("path");
const snarkjs = require("snarkjs");
const snarkjs = require("@tornado/snarkjs");
const compiler = require("circom");
const assert = chai.assert;
const bigInt = snarkjs.bigInt;
const q = bigInt("21888242871839275222246405745257275088548364400416034343698204186575808495617");
function addPoint(a, b) {
const cta = bigInt("168700");
@ -25,7 +24,6 @@ function print(circuit, w, s) {
describe("Exponentioation test", () => {
it("Should generate the Exponentiation table in k=0", async () => {
const cirDef = await compiler(path.join(__dirname, "circuits", "escalarmulw4table_test.circom"));
// console.log(JSON.stringify(cirDef, null, 1));
@ -40,8 +38,10 @@ describe("Exponentioation test", () => {
assert(circuit.checkWitness(w));
let g = [bigInt("5299619240641551281634865583518297030282874472190772894086521144482721001553"),
bigInt("16950150798460657717958625567821834550301663161624707787222815936182638968203")]
let g = [
bigInt("5299619240641551281634865583518297030282874472190772894086521144482721001553"),
bigInt("16950150798460657717958625567821834550301663161624707787222815936182638968203"),
];
dbl = [bigInt("0"), snarkjs.bigInt("1")];
@ -59,11 +59,9 @@ describe("Exponentioation test", () => {
dbl = addPoint([xout1, yout1], g);
}
});
it("Should generate the Exponentiation table in k=3", async () => {
const cirDef = await compiler(path.join(__dirname, "circuits", "escalarmulw4table_test3.circom"));
// console.log(JSON.stringify(cirDef, null, 1));
@ -78,8 +76,10 @@ describe("Exponentioation test", () => {
assert(circuit.checkWitness(w));
let g = [snarkjs.bigInt("5299619240641551281634865583518297030282874472190772894086521144482721001553"),
snarkjs.bigInt("16950150798460657717958625567821834550301663161624707787222815936182638968203")]
let g = [
snarkjs.bigInt("5299619240641551281634865583518297030282874472190772894086521144482721001553"),
snarkjs.bigInt("16950150798460657717958625567821834550301663161624707787222815936182638968203"),
];
for (let i = 0; i < 12; i++) {
g = addPoint(g, g);
@ -91,7 +91,6 @@ describe("Exponentioation test", () => {
const xout1 = w[circuit.getSignalIdx(`main.out[${i}][0]`)];
const yout1 = w[circuit.getSignalIdx(`main.out[${i}][1]`)];
// console.log(xout1.toString());
// console.log(yout1.toString());
// console.log(dbl[0]);
@ -102,7 +101,6 @@ describe("Exponentioation test", () => {
dbl = addPoint([xout1, yout1], g);
}
});
it("Should exponentiate g^31", async () => {
@ -116,12 +114,14 @@ describe("Exponentioation test", () => {
console.log("NConstrains: " + circuit.nConstraints);
const w = circuit.calculateWitness({"in": 31});
const w = circuit.calculateWitness({ in: 31 });
assert(circuit.checkWitness(w));
let g = [snarkjs.bigInt("5299619240641551281634865583518297030282874472190772894086521144482721001553"),
snarkjs.bigInt("16950150798460657717958625567821834550301663161624707787222815936182638968203")]
let g = [
snarkjs.bigInt("5299619240641551281634865583518297030282874472190772894086521144482721001553"),
snarkjs.bigInt("16950150798460657717958625567821834550301663161624707787222815936182638968203"),
];
let c = [0n, 1n];
@ -139,8 +139,8 @@ describe("Exponentioation test", () => {
assert(xout.equals(c[0]));
assert(yout.equals(c[1]));
console.log("-------")
const w2 = circuit.calculateWitness({"in": (1n<<252n)+1n});
console.log("-------");
const w2 = circuit.calculateWitness({ in: (1n << 252n) + 1n });
const xout2 = w2[circuit.getSignalIdx(`main.out[0]`)];
const yout2 = w2[circuit.getSignalIdx(`main.out[1]`)];
@ -158,7 +158,6 @@ describe("Exponentioation test", () => {
assert(xout2.equals(c[0]));
assert(yout2.equals(c[1]));
}).timeout(10000000);
it("Number of constrains for 256 bits", async () => {
@ -168,5 +167,4 @@ describe("Exponentioation test", () => {
console.log("NConstrains: " + circuit.nConstraints);
}).timeout(10000000);
});

@ -1,13 +1,12 @@
const chai = require("chai");
const path = require("path");
const snarkjs = require("snarkjs");
const snarkjs = require("@tornado/snarkjs");
const compiler = require("circom");
const assert = chai.assert;
const bigInt = snarkjs.bigInt;
function print(circuit, w, s) {
console.log(s + ": " + w[circuit.getSignalIdx(s)]);
}
@ -19,7 +18,7 @@ describe("Escalarmul test", function () {
let g = [
snarkjs.bigInt("5299619240641551281634865583518297030282874472190772894086521144482721001553"),
snarkjs.bigInt("16950150798460657717958625567821834550301663161624707787222815936182638968203")
snarkjs.bigInt("16950150798460657717958625567821834550301663161624707787222815936182638968203"),
];
before(async () => {
@ -29,8 +28,7 @@ describe("Escalarmul test", function () {
});
it("Should generate Same escalar mul", async () => {
const w = circuitEMulAny.calculateWitness({"e": 1, "p": g});
const w = circuitEMulAny.calculateWitness({ e: 1, p: g });
assert(circuitEMulAny.checkWitness(w));
@ -42,9 +40,8 @@ describe("Escalarmul test", function () {
});
it("If multiply by order should return 0", async () => {
const r = bigInt("2736030358979909402780800718157159386076813972158567259200215660948447373041");
const w = circuitEMulAny.calculateWitness({"e": r, "p": g});
const w = circuitEMulAny.calculateWitness({ e: r, p: g });
assert(circuitEMulAny.checkWitness(w));
@ -54,6 +51,4 @@ describe("Escalarmul test", function () {
assert(xout.equals(bigInt.zero));
assert(yout.equals(bigInt.one));
});
});

@ -1,6 +1,6 @@
const chai = require("chai");
const path = require("path");
const snarkjs = require("snarkjs");
const snarkjs = require("@tornado/snarkjs");
const compiler = require("circom");
const babyjub = require("../src/babyjub");
@ -8,7 +8,6 @@ const assert = chai.assert;
const bigInt = snarkjs.bigInt;
function print(circuit, w, s) {
console.log(s + ": " + w[circuit.getSignalIdx(s)]);
}
@ -25,8 +24,7 @@ describe("Escalarmul test", function () {
});
it("Should generate Same escalar mul", async () => {
const w = circuit.calculateWitness({"e": 0});
const w = circuit.calculateWitness({ e: 0 });
assert(circuit.checkWitness(w));
@ -38,8 +36,7 @@ describe("Escalarmul test", function () {
});
it("Should generate Same escalar mul", async () => {
const w = circuit.calculateWitness({"e": 1});
const w = circuit.calculateWitness({ e: 1 });
assert(circuit.checkWitness(w));
@ -51,14 +48,13 @@ describe("Escalarmul test", function () {
});
it("Should generate scalar mul of a specific constant", async () => {
const s = bigInt("2351960337287830298912035165133676222414898052661454064215017316447594616519");
const base8 = [
bigInt("5299619240641551281634865583518297030282874472190772894086521144482721001553"),
bigInt("16950150798460657717958625567821834550301663161624707787222815936182638968203")
bigInt("16950150798460657717958625567821834550301663161624707787222815936182638968203"),
];
const w = circuit.calculateWitness({"e": s});
const w = circuit.calculateWitness({ e: s });
assert(circuit.checkWitness(w));
@ -72,16 +68,15 @@ describe("Escalarmul test", function () {
});
it("Should generate scalar mul of the firsts 50 elements", async () => {
const base8 = [
bigInt("5299619240641551281634865583518297030282874472190772894086521144482721001553"),
bigInt("16950150798460657717958625567821834550301663161624707787222815936182638968203")
bigInt("16950150798460657717958625567821834550301663161624707787222815936182638968203"),
];
for (let i = 0; i < 50; i++) {
const s = bigInt(i);
const w = circuit.calculateWitness({"e": s});
const w = circuit.calculateWitness({ e: s });
assert(circuit.checkWitness(w));
@ -96,8 +91,7 @@ describe("Escalarmul test", function () {
});
it("If multiply by order should return 0", async () => {
const w = circuit.calculateWitness({"e": babyjub.subOrder });
const w = circuit.calculateWitness({ e: babyjub.subOrder });
assert(circuit.checkWitness(w));
@ -107,6 +101,4 @@ describe("Escalarmul test", function () {
assert(xout.equals(bigInt.zero));
assert(yout.equals(bigInt.one));
});
});

@ -1,5 +1,4 @@
const snarkjs = require("snarkjs");
const snarkjs = require("@tornado/snarkjs");
const bigInt = snarkjs.bigInt;

@ -1,6 +1,6 @@
const chai = require("chai");
const path = require("path");
const snarkjs = require("snarkjs");
const snarkjs = require("@tornado/snarkjs");
const compiler = require("circom");
const mimcjs = require("../src/mimc7.js");
@ -30,6 +30,5 @@ describe("MiMC Circuit test", function () {
assert.equal(res.toString(), res2.toString());
assert(circuit.checkWitness(w));
});
});

@ -1,6 +1,6 @@
const chai = require("chai");
const path = require("path");
const snarkjs = require("snarkjs");
const snarkjs = require("@tornado/snarkjs");
const compiler = require("circom");
const mimcjs = require("../src/mimcsponge.js");
@ -30,7 +30,6 @@ describe("MiMC Sponge Circuit test", function () {
assert.equal(xRout.toString(), out2.xR.toString());
assert(circuit.checkWitness(w));
});
it("Should check hash", async () => {
@ -53,6 +52,5 @@ describe("MiMC Sponge Circuit test", function () {
assert.equal(o3.toString(), out2[2].toString());
assert(circuit.checkWitness(w));
});
});

@ -1,6 +1,6 @@
const chai = require("chai");
const path = require("path");
const snarkjs = require("snarkjs");
const snarkjs = require("@tornado/snarkjs");
const compiler = require("circom");
const babyJub = require("../src/babyjub.js");
@ -16,7 +16,8 @@ describe("Montgomery test", function () {
let g = [
snarkjs.bigInt("5299619240641551281634865583518297030282874472190772894086521144482721001553"),
snarkjs.bigInt("16950150798460657717958625567821834550301663161624707787222815936182638968203")];
snarkjs.bigInt("16950150798460657717958625567821834550301663161624707787222815936182638968203"),
];
let mg, mg2, g2, g3, mg3;

@ -1,16 +1,14 @@
const chai = require("chai");
const path = require("path");
const snarkjs = require("snarkjs");
const snarkjs = require("@tornado/snarkjs");
const compiler = require("circom");
const assert = chai.assert;
const bigInt = snarkjs.bigInt;
describe("Mux4 test", () => {
it("Should create a constant multiplexer 4", async () => {
const cirDef = await compiler(path.join(__dirname, "circuits", "mux4_1.circom"));
// console.log(JSON.stringify(cirDef, null, 1));
@ -37,11 +35,11 @@ describe("Mux4 test", () => {
bigInt("1223"),
bigInt("4546"),
bigInt("4256"),
bigInt("4456")
bigInt("4456"),
];
for (let i = 0; i < 16; i++) {
const w = circuit.calculateWitness({ "selector": i });
const w = circuit.calculateWitness({ selector: i });
assert(circuit.checkWitness(w));
@ -53,26 +51,16 @@ describe("Mux4 test", () => {
});
it("Should create a constant multiplexer 3", async () => {
const cirDef = await compiler(path.join(__dirname, "circuits", "mux3_1.circom"));
const circuit = new snarkjs.Circuit(cirDef);
console.log("NConstrains Mux3: " + circuit.nConstraints);
const ct8 = [
bigInt("37"),
bigInt("47"),
bigInt("53"),
bigInt("71"),
bigInt("89"),
bigInt("107"),
bigInt("163"),
bigInt("191")
];
const ct8 = [bigInt("37"), bigInt("47"), bigInt("53"), bigInt("71"), bigInt("89"), bigInt("107"), bigInt("163"), bigInt("191")];
for (let i = 0; i < 8; i++) {
const w = circuit.calculateWitness({ "selector": i });
const w = circuit.calculateWitness({ selector: i });
assert(w[0].equals(bigInt(1)));
@ -81,22 +69,16 @@ describe("Mux4 test", () => {
}
});
it("Should create a constant multiplexer 2", async () => {
const cirDef = await compiler(path.join(__dirname, "circuits", "mux2_1.circom"));
const circuit = new snarkjs.Circuit(cirDef);
console.log("NConstrains Mux2: " + circuit.nConstraints);
const ct8 = [
bigInt("37"),
bigInt("47"),
bigInt("53"),
bigInt("71"),
];
const ct8 = [bigInt("37"), bigInt("47"), bigInt("53"), bigInt("71")];
for (let i = 0; i < 4; i++) {
const w = circuit.calculateWitness({ "selector": i });
const w = circuit.calculateWitness({ selector: i });
assert(circuit.checkWitness(w));
@ -107,20 +89,16 @@ describe("Mux4 test", () => {
}
});
it("Should create a constant multiplexer 1", async () => {
const cirDef = await compiler(path.join(__dirname, "circuits", "mux1_1.circom"));
const circuit = new snarkjs.Circuit(cirDef);
console.log("NConstrains Mux1: " + circuit.nConstraints);
const ct8 = [
bigInt("37"),
bigInt("47"),
];
const ct8 = [bigInt("37"), bigInt("47")];
for (let i = 0; i < 2; i++) {
const w = circuit.calculateWitness({ "selector": i });
const w = circuit.calculateWitness({ selector: i });
assert(circuit.checkWitness(w));

@ -1,6 +1,6 @@
const chai = require("chai");
const path = require("path");
const snarkjs = require("snarkjs");
const snarkjs = require("@tornado/snarkjs");
const compiler = require("circom");
const assert = chai.assert;
@ -9,13 +9,27 @@ const bigInt = snarkjs.bigInt;
const babyJub = require("../src/babyjub.js");
const PBASE =
const PBASE = [
[
[bigInt("10457101036533406547632367118273992217979173478358440826365724437999023779287"),bigInt("19824078218392094440610104313265183977899662750282163392862422243483260492317")],
[bigInt("2671756056509184035029146175565761955751135805354291559563293617232983272177"),bigInt("2663205510731142763556352975002641716101654201788071096152948830924149045094")],
[bigInt("5802099305472655231388284418920769829666717045250560929368476121199858275951"),bigInt("5980429700218124965372158798884772646841287887664001482443826541541529227896")],
[bigInt("7107336197374528537877327281242680114152313102022415488494307685842428166594"),bigInt("2857869773864086953506483169737724679646433914307247183624878062391496185654")],
[bigInt("20265828622013100949498132415626198973119240347465898028410217039057588424236"),bigInt("1160461593266035632937973507065134938065359936056410650153315956301179689506")]
bigInt("10457101036533406547632367118273992217979173478358440826365724437999023779287"),
bigInt("19824078218392094440610104313265183977899662750282163392862422243483260492317"),
],
[
bigInt("2671756056509184035029146175565761955751135805354291559563293617232983272177"),
bigInt("2663205510731142763556352975002641716101654201788071096152948830924149045094"),
],
[
bigInt("5802099305472655231388284418920769829666717045250560929368476121199858275951"),
bigInt("5980429700218124965372158798884772646841287887664001482443826541541529227896"),
],
[
bigInt("7107336197374528537877327281242680114152313102022415488494307685842428166594"),
bigInt("2857869773864086953506483169737724679646433914307247183624878062391496185654"),
],
[
bigInt("20265828622013100949498132415626198973119240347465898028410217039057588424236"),
bigInt("1160461593266035632937973507065134938065359936056410650153315956301179689506"),
],
];
describe("Double Pedersen test", function () {
@ -29,7 +43,6 @@ describe("Double Pedersen test", function() {
console.log("NConstrains: " + circuit.nConstraints);
});
it("Should pedersen at zero", async () => {
let w, xout, yout;
w = circuit.calculateWitness({ in: ["0", "0"] });
@ -61,7 +74,6 @@ describe("Double Pedersen test", function() {
assert(xout.equals(PBASE[1][0]));
assert(yout.equals(PBASE[1][1]));
});
it("Should pedersen at mixed generators", async () => {
let w, xout, yout;
@ -70,15 +82,10 @@ describe("Double Pedersen test", function() {
xout = w[circuit.getSignalIdx("main.out[0]")];
yout = w[circuit.getSignalIdx("main.out[1]")];
const r = babyJub.addPoint(
babyJub.mulPointEscalar(PBASE[0], 3),
babyJub.mulPointEscalar(PBASE[1], 7)
);
const r = babyJub.addPoint(babyJub.mulPointEscalar(PBASE[0], 3), babyJub.mulPointEscalar(PBASE[1], 7));
assert(xout.equals(r[0]));
assert(yout.equals(r[1]));
});
it("Should pedersen all ones", async () => {
let w, xout, yout;
@ -89,10 +96,7 @@ describe("Double Pedersen test", function() {
xout = w[circuit.getSignalIdx("main.out[0]")];
yout = w[circuit.getSignalIdx("main.out[1]")];
const r2 = babyJub.addPoint(
babyJub.mulPointEscalar(PBASE[0], allOnes),
babyJub.mulPointEscalar(PBASE[1], allOnes)
);
const r2 = babyJub.addPoint(babyJub.mulPointEscalar(PBASE[0], allOnes), babyJub.mulPointEscalar(PBASE[1], allOnes));
assert(xout.equals(r2[0]));
assert(yout.equals(r2[1]));

@ -1,6 +1,6 @@
const chai = require("chai");
const path = require("path");
const snarkjs = require("snarkjs");
const snarkjs = require("@tornado/snarkjs");
const compiler = require("circom");
const assert = chai.assert;
@ -10,7 +10,6 @@ const bigInt = snarkjs.bigInt;
const babyJub = require("../src/babyjub.js");
const pedersen = require("../src/pedersenHash.js");
describe("Pedersen test", function () {
let circuit;
this.timeout(100000);
@ -22,7 +21,6 @@ describe("Pedersen test", function() {
console.log("NConstrains Pedersen2: " + circuit.nConstraints);
});
it("Should pedersen at zero", async () => {
let w, xout, yout;
w = circuit.calculateWitness({ in: 0 });
@ -44,7 +42,6 @@ describe("Pedersen test", function() {
assert(yout.equals(hP[1]));
});
it("Should pedersen with 253 ones", async () => {
let w, xout, yout;
const n = bigInt.one.shl(253).sub(bigInt.one);
@ -56,9 +53,8 @@ describe("Pedersen test", function() {
yout = w[circuit.getSignalIdx("main.out[1]")];
const b = Buffer.alloc(32);
for (let i=0; i<31; i++) b[i] = 0xFF;
b[31] = 0x1F;
for (let i = 0; i < 31; i++) b[i] = 0xff;
b[31] = 0x1f;
const h = pedersen.hash(b);
const hP = babyJub.unpackPoint(h);

@ -1,6 +1,6 @@
const chai = require("chai");
const path = require("path");
const snarkjs = require("snarkjs");
const snarkjs = require("@tornado/snarkjs");
const compiler = require("circom");
const assert = chai.assert;
@ -9,7 +9,6 @@ const bigInt = snarkjs.bigInt;
const babyJub = require("../src/babyjub.js");
describe("Point 2 bits test", function () {
let circuit;
this.timeout(100000);

@ -1,6 +1,6 @@
const chai = require("chai");
const path = require("path");
const snarkjs = require("snarkjs");
const snarkjs = require("@tornado/snarkjs");
const compiler = require("circom");
const poseidon = require("../src/poseidon.js");
@ -39,7 +39,6 @@ describe("Poseidon Circuit test", function () {
await circuit2.checkWitness(w);
});
it("Should check constrain of hash([1, 2, 3, 4])", async () => {
const hash = poseidon([1, 2, 3, 4]);
assert.equal("0x2e4fb80ce74868b0d33f4acb22071d8d8f8da7d30ebf972e6e4f72a64bb0633f", "0x" + hash.toString(16));

@ -1,6 +1,6 @@
const chai = require("chai");
const path = require("path");
const snarkjs = require("snarkjs");
const snarkjs = require("@tornado/snarkjs");
const crypto = require("crypto");
const compiler = require("circom");
@ -11,12 +11,11 @@ const sha256 = require("./helpers/sha256");
// const printSignal = require("./helpers/printsignal");
function buffer2bitArray(b) {
const res = [];
for (let i = 0; i < b.length; i++) {
for (let j = 0; j < 8; j++) {
res.push((b[i] >> (7-j) &1));
res.push((b[i] >> (7 - j)) & 1);
}
}
return res;
@ -33,10 +32,7 @@ function bitArray2buffer(a) {
return b;
}
describe("SHA256 test", () => {
it("Should work bits to array and array to bits", async () => {
const b = new Buffer.alloc(64);
for (let i = 0; i < 64; i++) {
@ -55,15 +51,13 @@ describe("SHA256 test", () => {
console.log("Vars: " + circuit.nVars);
console.log("Constraints: " + circuit.nConstraints);
const witness = circuit.calculateWitness({ "a": "1", "b": "2" });
const witness = circuit.calculateWitness({ a: "1", b: "2" });
const b = new Buffer.alloc(54);
b[26] = 1;
b[53] = 2;
const hash = crypto.createHash("sha256")
.update(b)
.digest("hex");
const hash = crypto.createHash("sha256").update(b).digest("hex");
const r = "0x" + hash.slice(10);
const hash2 = sha256.hash(b.toString("hex"), { msgFormat: "hex-bytes" });
@ -90,21 +84,17 @@ describe("SHA256 test", () => {
b[i] = i + 1;
}
const hash = crypto.createHash("sha256")
.update(b)
.digest("hex");
const hash = crypto.createHash("sha256").update(b).digest("hex");
const arrIn = buffer2bitArray(b);
const witness = circuit.calculateWitness({ "in": arrIn } /*, {logOutput: true} */);
const witness = circuit.calculateWitness({ in: arrIn } /*, {logOutput: true} */);
const arrOut = witness.slice(1, 257);
const hash2 = bitArray2buffer(arrOut).toString("hex");
assert.equal(hash, hash2);
}).timeout(1000000);
it("Should calculate a hash of 2 compressor", async () => {
const cirDef = await compiler(path.join(__dirname, "circuits", "sha256_test448.circom"), { reduceConstraints: false });
const circuit = new snarkjs.Circuit(cirDef);
@ -112,25 +102,21 @@ describe("SHA256 test", () => {
console.log("Vars: " + circuit.nVars);
console.log("Constraints: " + circuit.nConstraints);
const testStr = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
const b = Buffer.from(testStr, 'utf8');
const b = Buffer.from(testStr, "utf8");
for (let i = 0; i < 64; i++) {
b[i] = i + 1;
}
const hash = crypto.createHash("sha256")
.update(b)
.digest("hex");
const hash = crypto.createHash("sha256").update(b).digest("hex");
const arrIn = buffer2bitArray(b);
const witness = circuit.calculateWitness({ "in": arrIn } /*, {logOutput: true} */);
const witness = circuit.calculateWitness({ in: arrIn } /*, {logOutput: true} */);
const arrOut = witness.slice(1, 257);
const hash2 = bitArray2buffer(arrOut).toString("hex");
assert.equal(hash, hash2);
}).timeout(1000000);
});

@ -1,6 +1,6 @@
const chai = require("chai");
const path = require("path");
const snarkjs = require("snarkjs");
const snarkjs = require("@tornado/snarkjs");
const compiler = require("circom");
const assert = chai.assert;
@ -83,6 +83,4 @@ describe("Sign test", () => {
assert(w[circuit.getSignalIdx("main.sign")].equals(bigInt(1)));
});
});

@ -1,5 +1,5 @@
const chai = require("chai");
const snarkjs = require("snarkjs");
const snarkjs = require("@tornado/snarkjs");
const smt = require("../src/smt.js");
@ -7,9 +7,8 @@ const assert = chai.assert;
const bigInt = snarkjs.bigInt;
function stringifyBigInts(o) {
if ((typeof(o) == "bigint") || (o instanceof bigInt)) {
if (typeof o == "bigint" || o instanceof bigInt) {
return o.toString(10);
} else if (Array.isArray(o)) {
return o.map(stringifyBigInts);
@ -26,8 +25,7 @@ function stringifyBigInts(o) {
describe("SMT Javascript test", function () {
this.timeout(100000);
before( async () => {
});
before(async () => {});
it("Should insert 2 elements and empty them", async () => {
const tree = await smt.newMemEmptyTrie();
@ -165,7 +163,7 @@ describe("SMT Javascript test", function () {
const tree2 = await smt.newMemEmptyTrie();
await tree1.insert(8, 88);
await tree1.insert(9,99,);
await tree1.insert(9, 99);
await tree1.insert(32, 3232);
await tree2.insert(8, 888);
@ -178,5 +176,4 @@ describe("SMT Javascript test", function () {
assert(tree1.root.equals(tree2.root));
});
});

@ -1,6 +1,6 @@
const chai = require("chai");
const path = require("path");
const snarkjs = require("snarkjs");
const snarkjs = require("@tornado/snarkjs");
const compiler = require("circom");
const smt = require("../src/smt.js");
@ -14,12 +14,12 @@ function print(circuit, w, s) {
}
async function testInsert(tree, key, value, circuit, log) {
const res = await tree.insert(key, value);
let siblings = res.siblings;
while (siblings.length < 10) siblings.push(bigInt(0));
const w = circuit.calculateWitness({
const w = circuit.calculateWitness(
{
fnc: [1, 0],
oldRoot: res.oldRoot,
siblings: siblings,
@ -27,8 +27,10 @@ async function testInsert(tree, key, value, circuit, log ) {
oldValue: res.isOld0 ? 0 : res.oldValue,
isOld0: res.isOld0 ? 1 : 0,
newKey: key,
newValue: value
}, log);
newValue: value,
},
log
);
const root1 = w[circuit.getSignalIdx("main.newRoot")];
assert(circuit.checkWitness(w));
@ -48,7 +50,7 @@ async function testDelete(tree, key, circuit) {
oldValue: res.isOld0 ? 0 : res.oldValue,
isOld0: res.isOld0 ? 1 : 0,
newKey: res.delKey,
newValue: res.delValue
newValue: res.delValue,
});
const root1 = w[circuit.getSignalIdx("main.newRoot")];
@ -70,7 +72,7 @@ async function testUpdate(tree, key, newValue, circuit) {
oldValue: res.oldValue,
isOld0: 0,
newKey: res.newKey,
newValue: res.newValue
newValue: res.newValue,
});
const root1 = w[circuit.getSignalIdx("main.newRoot")];
@ -79,7 +81,6 @@ async function testUpdate(tree, key, newValue, circuit) {
assert(root1.equals(res.newRoot));
}
describe("SMT test", function () {
let circuit;
let tree;
@ -110,8 +111,6 @@ describe("SMT test", function () {
await testInsert(tree, key, value, circuit);
});
it("Should remove an element", async () => {
await testDelete(tree, 111, circuit);
await testDelete(tree, 333, circuit);
@ -151,7 +150,6 @@ describe("SMT test", function () {
await testInsert(tree6, keys[1], values[1], circuit);
await testInsert(tree6, keys[0], values[0], circuit);
await testDelete(tree1, keys[0], circuit);
await testDelete(tree1, keys[1], circuit);
await testDelete(tree2, keys[1], circuit);
@ -162,7 +160,6 @@ describe("SMT test", function () {
await testDelete(tree4, keys[2], circuit);
await testDelete(tree4, keys[0], circuit);
await testDelete(tree5, keys[1], circuit);
await testDelete(tree5, keys[2], circuit);
await testDelete(tree6, keys[2], circuit);
@ -187,7 +184,7 @@ describe("SMT test", function () {
oldValue: 44,
isOld0: 55,
newKey: 66,
newValue: 77
newValue: 77,
});
const root1 = w[circuit.getSignalIdx("main.oldRoot")];
@ -195,7 +192,6 @@ describe("SMT test", function () {
assert(circuit.checkWitness(w));
assert(root1.equals(root2));
});
it("Should update an element", async () => {
const tree1 = await smt.newMemEmptyTrie();
@ -213,5 +209,4 @@ describe("SMT test", function () {
await testUpdate(tree1, 9, 999, circuit);
await testUpdate(tree1, 32, 323232, circuit);
});
});

@ -1,6 +1,6 @@
const chai = require("chai");
const path = require("path");
const snarkjs = require("snarkjs");
const snarkjs = require("@tornado/snarkjs");
const compiler = require("circom");
const smt = require("../src/smt.js");
@ -14,7 +14,6 @@ function print(circuit, w, s) {
}
async function testInclusion(tree, key, circuit) {
const res = await tree.find(key);
assert(res.found);
@ -30,7 +29,7 @@ async function testInclusion(tree, key, circuit) {
oldValue: 0,
isOld0: 0,
key: key,
value: res.foundValue
value: res.foundValue,
});
assert(circuit.checkWitness(w));
@ -52,7 +51,7 @@ async function testExclusion(tree, key, circuit) {
oldValue: res.isOld0 ? 0 : res.notFoundValue,
isOld0: res.isOld0 ? 1 : 0,
key: key,
value: 0
value: 0,
});
assert(circuit.checkWitness(w));
@ -106,7 +105,7 @@ describe("SMT test", function () {
oldValue: 33,
isOld0: 0,
key: 44,
value: 0
value: 0,
});
assert(circuit.checkWitness(w));
});
@ -133,6 +132,4 @@ describe("SMT test", function () {
await testInclusion(tree2, e2fail_hi, circuit);
});
});

@ -1,7 +1,7 @@
const path = require("path");
const snarkjs = require("snarkjs");
const snarkjs = require("@tornado/snarkjs");
const compiler = require("circom");
const fs = require("fs")
const fs = require("fs");
const bigInt = snarkjs.bigInt;
const smt = require("../src/smt.js");
@ -61,10 +61,7 @@ describe("smt3test", function () {
hv: e1.hv,
};
const compiledCircuit = await compiler(
circuitFileName,
{ reduceConstraints: false }
);
const compiledCircuit = await compiler(circuitFileName, { reduceConstraints: false });
const circuit = new snarkjs.Circuit(compiledCircuit);
const witness = circuit.calculateWitness(input);
@ -72,7 +69,6 @@ describe("smt3test", function () {
}
it("TestSmts", async () => {
const e1 = {
hi: bigInt("17124152697573569611556136390143205198134245887034837071647643529178599000839"),
hv: bigInt("19650379996168153643111744440707177573540245771926102415571667548153444658179"),
@ -95,4 +91,3 @@ describe("smt3test", function () {
await testsmt3(e1, e2fail);
});
});