@ -0,0 +1,87 @@ |
|||||
|
const bigInt = require("big-integer"); |
||||
|
|
||||
|
module.exports.leBuff2int = leBuff2int; |
||||
|
module.exports.leInt2Buff = leInt2Buff; |
||||
|
module.exports.beBuff2int = beBuff2int; |
||||
|
module.exports.beInt2Buff = beInt2Buff; |
||||
|
module.exports.stringifyBigInts = stringifyBigInts; |
||||
|
module.exports.unstringifyBigInts = unstringifyBigInts; |
||||
|
|
||||
|
function leBuff2int (buff) { |
||||
|
let res = bigInt.zero; |
||||
|
for (let i=0; i<buff.length; i++) { |
||||
|
const n = bigInt(buff[i]); |
||||
|
res = res.add(n.shiftLeft(i*8)); |
||||
|
} |
||||
|
return res; |
||||
|
} |
||||
|
|
||||
|
function leInt2Buff(n, len) { |
||||
|
let r = n; |
||||
|
let o =0; |
||||
|
const buff = Buffer.alloc(len); |
||||
|
while ((r.gt(bigInt.zero))&&(o<buff.length)) { |
||||
|
let c = Number(r.and(bigInt(255))); |
||||
|
buff[o] = c; |
||||
|
o++; |
||||
|
r = r.shiftRight(8); |
||||
|
} |
||||
|
if (r.gt(bigInt.zero)) throw new Error("Number does not feed in buffer"); |
||||
|
return buff; |
||||
|
} |
||||
|
|
||||
|
function beBuff2int (buff) { |
||||
|
let res = bigInt.zero; |
||||
|
for (let i=0; i<buff.length; i++) { |
||||
|
const n = bigInt(buff[buff.length - i - 1]); |
||||
|
res = res.add(n.shiftLeft(i*8)); |
||||
|
} |
||||
|
return res; |
||||
|
} |
||||
|
|
||||
|
function beInt2Buff(n, len) { |
||||
|
let r = n; |
||||
|
let o =len-1; |
||||
|
const buff = Buffer.alloc(len); |
||||
|
while ((r.greater(bigInt.zero))&&(o>=0)) { |
||||
|
let c = Number(r.and(bigInt(255))); |
||||
|
buff[o] = c; |
||||
|
o--; |
||||
|
r = r.shiftRight(8); |
||||
|
} |
||||
|
if (r.gt(bigInt.zero)) throw new Error("Number does not feed in buffer"); |
||||
|
return buff; |
||||
|
} |
||||
|
|
||||
|
|
||||
|
function stringifyBigInts(o) { |
||||
|
if ((typeof(o) == "bigint") || o.isZero !== undefined) { |
||||
|
return o.toString(10); |
||||
|
} else if (Array.isArray(o)) { |
||||
|
return o.map(stringifyBigInts); |
||||
|
} else if (typeof o == "object") { |
||||
|
const res = {}; |
||||
|
for (let k in o) { |
||||
|
res[k] = stringifyBigInts(o[k]); |
||||
|
} |
||||
|
return res; |
||||
|
} else { |
||||
|
return o; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
function unstringifyBigInts(o) { |
||||
|
if ((typeof(o) == "string") && (/^[0-9]+$/.test(o) )) { |
||||
|
return bigInt(o); |
||||
|
} else if (Array.isArray(o)) { |
||||
|
return o.map(unstringifyBigInts); |
||||
|
} else if (typeof o == "object") { |
||||
|
const res = {}; |
||||
|
for (let k in o) { |
||||
|
res[k] = unstringifyBigInts(o[k]); |
||||
|
} |
||||
|
return res; |
||||
|
} else { |
||||
|
return o; |
||||
|
} |
||||
|
} |
@ -1,55 +1,50 @@ |
|||||
const chai = require("chai"); |
|
||||
const path = require("path"); |
const path = require("path"); |
||||
const snarkjs = require("snarkjs"); |
|
||||
const compiler = require("circom"); |
|
||||
|
|
||||
const assert = chai.assert; |
|
||||
|
|
||||
const bigInt = snarkjs.bigInt; |
|
||||
|
const bigInt = require("big-integer"); |
||||
|
const tester = require("circom").tester; |
||||
|
|
||||
function print(circuit, w, s) { |
function print(circuit, w, s) { |
||||
console.log(s + ": " + w[circuit.getSignalIdx(s)]); |
console.log(s + ": " + w[circuit.getSignalIdx(s)]); |
||||
} |
} |
||||
|
|
||||
function checkSub(_a,_b, circuit) { |
|
||||
|
async function checkSub(_a,_b, circuit) { |
||||
let a=bigInt(_a); |
let a=bigInt(_a); |
||||
let b=bigInt(_b); |
let b=bigInt(_b); |
||||
if (a.lesser(bigInt.zero)) a = a.add(bigInt.one.shl(16)); |
|
||||
if (b.lesser(bigInt.zero)) b = b.add(bigInt.one.shl(16)); |
|
||||
const w = circuit.calculateWitness({a: a, b: b}); |
|
||||
|
if (a.lesser(bigInt.zero)) a = a.add(bigInt.one.shiftLeft(16)); |
||||
|
if (b.lesser(bigInt.zero)) b = b.add(bigInt.one.shiftLeft(16)); |
||||
|
const w = await circuit.calculateWitness({a: a, b: b}, true); |
||||
|
|
||||
let res = a.sub(b); |
|
||||
if (res.lesser(bigInt.zero)) res = res.add(bigInt.one.shl(16)); |
|
||||
assert( w[circuit.getSignalIdx("main.out")].equals(bigInt(res)) ); |
|
||||
|
let res = a.minus(b); |
||||
|
if (res.lesser(bigInt.zero)) res = res.add(bigInt.one.shiftLeft(16)); |
||||
|
await circuit.assertOut(w, {out: bigInt(res)}); |
||||
} |
} |
||||
|
|
||||
describe("BinSub test", () => { |
|
||||
let circuit; |
|
||||
before( async() => { |
|
||||
const cirDef = await compiler(path.join(__dirname, "circuits", "binsub_test.circom")); |
|
||||
|
describe("BinSub test", function () { |
||||
|
|
||||
circuit = new snarkjs.Circuit(cirDef); |
|
||||
|
this.timeout(100000); |
||||
|
|
||||
console.log("NConstrains BinSub: " + circuit.nConstraints); |
|
||||
|
let circuit; |
||||
|
before( async() => { |
||||
|
circuit = await tester(path.join(__dirname, "circuits", "binsub_test.circom")); |
||||
}); |
}); |
||||
|
|
||||
it("Should check variuos ege cases", async () => { |
it("Should check variuos ege cases", async () => { |
||||
checkSub(0,0, circuit); |
|
||||
checkSub(1,0, circuit); |
|
||||
checkSub(-1,0, circuit); |
|
||||
checkSub(2,1, circuit); |
|
||||
checkSub(2,2, circuit); |
|
||||
checkSub(2,3, circuit); |
|
||||
checkSub(2,-1, circuit); |
|
||||
checkSub(2,-2, circuit); |
|
||||
checkSub(2,-3, circuit); |
|
||||
checkSub(-2,-3, circuit); |
|
||||
checkSub(-2,-2, circuit); |
|
||||
checkSub(-2,-1, circuit); |
|
||||
checkSub(-2,0, circuit); |
|
||||
checkSub(-2,1, circuit); |
|
||||
checkSub(-2,2, circuit); |
|
||||
checkSub(-2,3, circuit); |
|
||||
|
await checkSub(0,0, circuit); |
||||
|
await checkSub(1,0, circuit); |
||||
|
await checkSub(-1,0, circuit); |
||||
|
await checkSub(2,1, circuit); |
||||
|
await checkSub(2,2, circuit); |
||||
|
await checkSub(2,3, circuit); |
||||
|
await checkSub(2,-1, circuit); |
||||
|
await checkSub(2,-2, circuit); |
||||
|
await checkSub(2,-3, circuit); |
||||
|
await checkSub(-2,-3, circuit); |
||||
|
await checkSub(-2,-2, circuit); |
||||
|
await checkSub(-2,-1, circuit); |
||||
|
await checkSub(-2,0, circuit); |
||||
|
await checkSub(-2,1, circuit); |
||||
|
await checkSub(-2,2, circuit); |
||||
|
await checkSub(-2,3, circuit); |
||||
}); |
}); |
||||
|
|
||||
|
|
||||
|
@ -1,35 +1,37 @@ |
|||||
const chai = require("chai"); |
const chai = require("chai"); |
||||
const path = require("path"); |
const path = require("path"); |
||||
const snarkjs = require("snarkjs"); |
|
||||
const crypto = require("crypto"); |
|
||||
|
|
||||
const compiler = require("circom"); |
|
||||
|
const tester = require("circom").tester; |
||||
|
|
||||
|
const bigInt = require("big-integer"); |
||||
|
|
||||
const assert = chai.assert; |
const assert = chai.assert; |
||||
|
|
||||
describe("Sum test", () => { |
|
||||
it("Should create a constant circuit", async () => { |
|
||||
|
describe("Binary sum test", function () { |
||||
|
|
||||
const cirDef = await compiler(path.join(__dirname, "circuits", "constants_test.circom")); |
|
||||
assert.equal(cirDef.nVars, 2); |
|
||||
|
this.timeout(100000000); |
||||
|
|
||||
const circuit = new snarkjs.Circuit(cirDef); |
|
||||
|
it("Should create a constant circuit", async () => { |
||||
|
const circuit = await tester(path.join(__dirname, "circuits", "constants_test.circom")); |
||||
|
await circuit.loadConstraints(); |
||||
|
|
||||
const witness = circuit.calculateWitness({ "in": "0xd807aa98" }); |
|
||||
|
assert.equal(circuit.nVars, 2); |
||||
|
assert.equal(circuit.constraints.length, 1); |
||||
|
|
||||
assert(witness[0].equals(snarkjs.bigInt(1))); |
|
||||
assert(witness[1].equals(snarkjs.bigInt("0xd807aa98"))); |
|
||||
|
const witness = await circuit.calculateWitness({ "in": bigInt("d807aa98", 16)}, true); |
||||
|
|
||||
|
assert(witness[0].equals(bigInt(1))); |
||||
|
assert(witness[1].equals(bigInt("d807aa98", 16))); |
||||
}); |
}); |
||||
it("Should create a sum circuit", async () => { |
it("Should create a sum circuit", async () => { |
||||
|
const circuit = await tester(path.join(__dirname, "circuits", "sum_test.circom")); |
||||
|
await circuit.loadConstraints(); |
||||
|
|
||||
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); |
|
||||
|
assert.equal(circuit.constraints.length, 97); // 32 (in1) + 32(in2) + 32(out) + 1 (carry)
|
||||
|
|
||||
const witness = circuit.calculateWitness({ "a": "111", "b": "222" }); |
|
||||
|
const witness = await circuit.calculateWitness({ "a": "111", "b": "222" }, true); |
||||
|
|
||||
assert(witness[0].equals(snarkjs.bigInt(1))); |
|
||||
assert(witness[1].equals(snarkjs.bigInt("333"))); |
|
||||
|
assert(witness[0].equals(bigInt(1))); |
||||
|
assert(witness[1].equals(bigInt("333"))); |
||||
}); |
}); |
||||
}); |
}); |
@ -1,6 +1,6 @@ |
|||||
include "../../circuits/escalarmulw4table.circom"; |
include "../../circuits/escalarmulw4table.circom"; |
||||
|
|
||||
var base = [5299619240641551281634865583518297030282874472190772894086521144482721001553, |
|
||||
|
var base[2] = [5299619240641551281634865583518297030282874472190772894086521144482721001553, |
||||
16950150798460657717958625567821834550301663161624707787222815936182638968203] |
16950150798460657717958625567821834550301663161624707787222815936182638968203] |
||||
|
|
||||
component main = EscalarMulW4Table(base, 0); |
component main = EscalarMulW4Table(base, 0); |
@ -0,0 +1,258 @@ |
|||||
|
{ |
||||
|
"in": [ |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1", |
||||
|
"1" |
||||
|
] |
||||
|
} |
@ -1,193 +1,184 @@ |
|||||
const chai = require("chai"); |
const chai = require("chai"); |
||||
const path = require("path"); |
const path = require("path"); |
||||
const snarkjs = require("snarkjs"); |
|
||||
const crypto = require("crypto"); |
|
||||
|
|
||||
const compiler = require("circom"); |
|
||||
|
const tester = require("circom").tester; |
||||
|
|
||||
|
const bigInt = require("big-integer"); |
||||
|
|
||||
const assert = chai.assert; |
const assert = chai.assert; |
||||
|
|
||||
describe("Sum test", () => { |
|
||||
it("Should create a iszero circuit", async() => { |
|
||||
const cirDef = await compiler(path.join(__dirname, "circuits", "iszero.circom")); |
|
||||
|
describe("Comparators test", function () { |
||||
|
|
||||
const circuit = new snarkjs.Circuit(cirDef); |
|
||||
|
this.timeout(100000); |
||||
|
|
||||
|
it("Should create a iszero circuit", async() => { |
||||
|
const circuit = await tester(path.join(__dirname, "circuits", "iszero.circom")); |
||||
|
|
||||
let witness; |
let witness; |
||||
witness = circuit.calculateWitness({ "in": 111}); |
|
||||
assert(witness[0].equals(snarkjs.bigInt(1))); |
|
||||
assert(witness[1].equals(snarkjs.bigInt(0))); |
|
||||
|
witness = await circuit.calculateWitness({ "in": 111}, true); |
||||
|
assert(witness[0].equals(bigInt(1))); |
||||
|
assert(witness[1].equals(bigInt(0))); |
||||
|
|
||||
witness = circuit.calculateWitness({ "in": 0 }); |
|
||||
assert(witness[0].equals(snarkjs.bigInt(1))); |
|
||||
assert(witness[1].equals(snarkjs.bigInt(1))); |
|
||||
|
witness = await circuit.calculateWitness({ "in": 0 }, true); |
||||
|
assert(witness[0].equals(bigInt(1))); |
||||
|
assert(witness[1].equals(bigInt(1))); |
||||
}); |
}); |
||||
it("Should create a isequal circuit", async() => { |
it("Should create a isequal circuit", async() => { |
||||
const cirDef = await compiler(path.join(__dirname, "circuits", "isequal.circom")); |
|
||||
|
|
||||
const circuit = new snarkjs.Circuit(cirDef); |
|
||||
|
const circuit = await tester(path.join(__dirname, "circuits", "isequal.circom")); |
||||
|
|
||||
let witness; |
let witness; |
||||
witness = circuit.calculateWitness({ "in[0]": "111", "in[1]": "222" }); |
|
||||
assert(witness[0].equals(snarkjs.bigInt(1))); |
|
||||
assert(witness[1].equals(snarkjs.bigInt(0))); |
|
||||
|
witness = await circuit.calculateWitness({ "in": [111,222] }, true); |
||||
|
assert(witness[0].equals(bigInt(1))); |
||||
|
assert(witness[1].equals(bigInt(0))); |
||||
|
|
||||
witness = circuit.calculateWitness({ "in[0]": "444", "in[1]": "444" }); |
|
||||
assert(witness[0].equals(snarkjs.bigInt(1))); |
|
||||
assert(witness[1].equals(snarkjs.bigInt(1))); |
|
||||
|
witness = await circuit.calculateWitness({ "in": [444,444] }, true); |
||||
|
assert(witness[0].equals(bigInt(1))); |
||||
|
assert(witness[1].equals(bigInt(1))); |
||||
}); |
}); |
||||
it("Should create a comparison lessthan", async() => { |
it("Should create a comparison lessthan", async() => { |
||||
const cirDef = await compiler(path.join(__dirname, "circuits", "lessthan.circom")); |
|
||||
|
|
||||
const circuit = new snarkjs.Circuit(cirDef); |
|
||||
|
const circuit = await tester(path.join(__dirname, "circuits", "lessthan.circom")); |
||||
|
|
||||
let witness; |
let witness; |
||||
witness = circuit.calculateWitness({ "in[0]": "333", "in[1]": "444" }); |
|
||||
assert(witness[0].equals(snarkjs.bigInt(1))); |
|
||||
assert(witness[1].equals(snarkjs.bigInt(1))); |
|
||||
|
witness = await circuit.calculateWitness({ "in": [333,444] }), true; |
||||
|
assert(witness[0].equals(bigInt(1))); |
||||
|
assert(witness[1].equals(bigInt(1))); |
||||
|
|
||||
witness = circuit.calculateWitness({ "in[0]": "1", "in[1]": "1" }); |
|
||||
assert(witness[0].equals(snarkjs.bigInt(1))); |
|
||||
assert(witness[1].equals(snarkjs.bigInt(0))); |
|
||||
|
witness = await circuit.calculateWitness({ "in":[1,1] }, true); |
||||
|
assert(witness[0].equals(bigInt(1))); |
||||
|
assert(witness[1].equals(bigInt(0))); |
||||
|
|
||||
witness = circuit.calculateWitness({ "in[0]": "661", "in[1]": "660" }); |
|
||||
assert(witness[0].equals(snarkjs.bigInt(1))); |
|
||||
assert(witness[1].equals(snarkjs.bigInt(0))); |
|
||||
|
witness = await circuit.calculateWitness({ "in": [661, 660] }, true); |
||||
|
assert(witness[0].equals(bigInt(1))); |
||||
|
assert(witness[1].equals(bigInt(0))); |
||||
|
|
||||
witness = circuit.calculateWitness({ "in[0]": "0", "in[1]": "1" }); |
|
||||
assert(witness[0].equals(snarkjs.bigInt(1))); |
|
||||
assert(witness[1].equals(snarkjs.bigInt(1))); |
|
||||
|
witness = await circuit.calculateWitness({ "in": [0, 1] }, true); |
||||
|
assert(witness[0].equals(bigInt(1))); |
||||
|
assert(witness[1].equals(bigInt(1))); |
||||
|
|
||||
witness = circuit.calculateWitness({ "in[0]": "0", "in[1]": "444" }); |
|
||||
assert(witness[0].equals(snarkjs.bigInt(1))); |
|
||||
assert(witness[1].equals(snarkjs.bigInt(1))); |
|
||||
|
witness = await circuit.calculateWitness({ "in": [0, 444] }, true); |
||||
|
assert(witness[0].equals(bigInt(1))); |
||||
|
assert(witness[1].equals(bigInt(1))); |
||||
|
|
||||
witness = circuit.calculateWitness({ "in[0]": "1", "in[1]": "0" }); |
|
||||
assert(witness[0].equals(snarkjs.bigInt(1))); |
|
||||
assert(witness[1].equals(snarkjs.bigInt(0))); |
|
||||
|
witness = await circuit.calculateWitness({ "in": [1, 0] }, true); |
||||
|
assert(witness[0].equals(bigInt(1))); |
||||
|
assert(witness[1].equals(bigInt(0))); |
||||
|
|
||||
witness = circuit.calculateWitness({ "in[0]": "555", "in[1]": "0" }); |
|
||||
assert(witness[0].equals(snarkjs.bigInt(1))); |
|
||||
assert(witness[1].equals(snarkjs.bigInt(0))); |
|
||||
|
witness = await circuit.calculateWitness({ "in": [555, 0] }, true); |
||||
|
assert(witness[0].equals(bigInt(1))); |
||||
|
assert(witness[1].equals(bigInt(0))); |
||||
|
|
||||
witness = circuit.calculateWitness({ "in[0]": "0", "in[1]": "0" }); |
|
||||
assert(witness[0].equals(snarkjs.bigInt(1))); |
|
||||
assert(witness[1].equals(snarkjs.bigInt(0))); |
|
||||
|
witness = await circuit.calculateWitness({ "in": [0, 0] }, true); |
||||
|
assert(witness[0].equals(bigInt(1))); |
||||
|
assert(witness[1].equals(bigInt(0))); |
||||
}); |
}); |
||||
it("Should create a comparison lesseqthan", async() => { |
it("Should create a comparison lesseqthan", async() => { |
||||
const cirDef = await compiler(path.join(__dirname, "circuits", "lesseqthan.circom")); |
|
||||
|
|
||||
const circuit = new snarkjs.Circuit(cirDef); |
|
||||
|
const circuit = await tester(path.join(__dirname, "circuits", "lesseqthan.circom")); |
||||
|
|
||||
let witness; |
let witness; |
||||
witness = circuit.calculateWitness({ "in[0]": "333", "in[1]": "444" }); |
|
||||
assert(witness[0].equals(snarkjs.bigInt(1))); |
|
||||
assert(witness[1].equals(snarkjs.bigInt(1))); |
|
||||
|
witness = await circuit.calculateWitness({ "in": [333,444] }, true); |
||||
|
assert(witness[0].equals(bigInt(1))); |
||||
|
assert(witness[1].equals(bigInt(1))); |
||||
|
|
||||
witness = circuit.calculateWitness({ "in[0]": "1", "in[1]": "1" }); |
|
||||
assert(witness[0].equals(snarkjs.bigInt(1))); |
|
||||
assert(witness[1].equals(snarkjs.bigInt(1))); |
|
||||
|
witness = await circuit.calculateWitness({ "in":[1,1] }, true); |
||||
|
assert(witness[0].equals(bigInt(1))); |
||||
|
assert(witness[1].equals(bigInt(1))); |
||||
|
|
||||
witness = circuit.calculateWitness({ "in[0]": "661", "in[1]": "660" }); |
|
||||
assert(witness[0].equals(snarkjs.bigInt(1))); |
|
||||
assert(witness[1].equals(snarkjs.bigInt(0))); |
|
||||
|
witness = await circuit.calculateWitness({ "in": [661, 660] }, true); |
||||
|
assert(witness[0].equals(bigInt(1))); |
||||
|
assert(witness[1].equals(bigInt(0))); |
||||
|
|
||||
witness = circuit.calculateWitness({ "in[0]": "0", "in[1]": "1" }); |
|
||||
assert(witness[0].equals(snarkjs.bigInt(1))); |
|
||||
assert(witness[1].equals(snarkjs.bigInt(1))); |
|
||||
|
witness = await circuit.calculateWitness({ "in": [0, 1] }, true); |
||||
|
assert(witness[0].equals(bigInt(1))); |
||||
|
assert(witness[1].equals(bigInt(1))); |
||||
|
|
||||
witness = circuit.calculateWitness({ "in[0]": "0", "in[1]": "444" }); |
|
||||
assert(witness[0].equals(snarkjs.bigInt(1))); |
|
||||
assert(witness[1].equals(snarkjs.bigInt(1))); |
|
||||
|
witness = await circuit.calculateWitness({ "in": [0, 444] }, true); |
||||
|
assert(witness[0].equals(bigInt(1))); |
||||
|
assert(witness[1].equals(bigInt(1))); |
||||
|
|
||||
witness = circuit.calculateWitness({ "in[0]": "1", "in[1]": "0" }); |
|
||||
assert(witness[0].equals(snarkjs.bigInt(1))); |
|
||||
assert(witness[1].equals(snarkjs.bigInt(0))); |
|
||||
|
witness = await circuit.calculateWitness({ "in": [1, 0] }, true); |
||||
|
assert(witness[0].equals(bigInt(1))); |
||||
|
assert(witness[1].equals(bigInt(0))); |
||||
|
|
||||
witness = circuit.calculateWitness({ "in[0]": "555", "in[1]": "0" }); |
|
||||
assert(witness[0].equals(snarkjs.bigInt(1))); |
|
||||
assert(witness[1].equals(snarkjs.bigInt(0))); |
|
||||
|
witness = await circuit.calculateWitness({ "in": [555, 0] }, true); |
||||
|
assert(witness[0].equals(bigInt(1))); |
||||
|
assert(witness[1].equals(bigInt(0))); |
||||
|
|
||||
witness = circuit.calculateWitness({ "in[0]": "0", "in[1]": "0" }); |
|
||||
assert(witness[0].equals(snarkjs.bigInt(1))); |
|
||||
assert(witness[1].equals(snarkjs.bigInt(1))); |
|
||||
|
witness = await circuit.calculateWitness({ "in": [0, 0] }, true); |
||||
|
assert(witness[0].equals(bigInt(1))); |
||||
|
assert(witness[1].equals(bigInt(1))); |
||||
}); |
}); |
||||
it("Should create a comparison greaterthan", async() => { |
it("Should create a comparison greaterthan", async() => { |
||||
const cirDef = await compiler(path.join(__dirname, "circuits", "greaterthan.circom")); |
|
||||
|
|
||||
const circuit = new snarkjs.Circuit(cirDef); |
|
||||
|
const circuit = await tester(path.join(__dirname, "circuits", "greaterthan.circom")); |
||||
|
|
||||
let witness; |
let witness; |
||||
witness = circuit.calculateWitness({ "in[0]": "333", "in[1]": "444" }); |
|
||||
assert(witness[0].equals(snarkjs.bigInt(1))); |
|
||||
assert(witness[1].equals(snarkjs.bigInt(0))); |
|
||||
|
witness = await circuit.calculateWitness({ "in": [333,444] }, true); |
||||
|
assert(witness[0].equals(bigInt(1))); |
||||
|
assert(witness[1].equals(bigInt(0))); |
||||
|
|
||||
witness = circuit.calculateWitness({ "in[0]": "1", "in[1]": "1" }); |
|
||||
assert(witness[0].equals(snarkjs.bigInt(1))); |
|
||||
assert(witness[1].equals(snarkjs.bigInt(0))); |
|
||||
|
witness = await circuit.calculateWitness({ "in":[1,1] }, true); |
||||
|
assert(witness[0].equals(bigInt(1))); |
||||
|
assert(witness[1].equals(bigInt(0))); |
||||
|
|
||||
witness = circuit.calculateWitness({ "in[0]": "661", "in[1]": "660" }); |
|
||||
assert(witness[0].equals(snarkjs.bigInt(1))); |
|
||||
assert(witness[1].equals(snarkjs.bigInt(1))); |
|
||||
|
witness = await circuit.calculateWitness({ "in": [661, 660] }, true); |
||||
|
assert(witness[0].equals(bigInt(1))); |
||||
|
assert(witness[1].equals(bigInt(1))); |
||||
|
|
||||
witness = circuit.calculateWitness({ "in[0]": "0", "in[1]": "1" }); |
|
||||
assert(witness[0].equals(snarkjs.bigInt(1))); |
|
||||
assert(witness[1].equals(snarkjs.bigInt(0))); |
|
||||
|
witness = await circuit.calculateWitness({ "in": [0, 1] }, true); |
||||
|
assert(witness[0].equals(bigInt(1))); |
||||
|
assert(witness[1].equals(bigInt(0))); |
||||
|
|
||||
witness = circuit.calculateWitness({ "in[0]": "0", "in[1]": "444" }); |
|
||||
assert(witness[0].equals(snarkjs.bigInt(1))); |
|
||||
assert(witness[1].equals(snarkjs.bigInt(0))); |
|
||||
|
witness = await circuit.calculateWitness({ "in": [0, 444] }, true); |
||||
|
assert(witness[0].equals(bigInt(1))); |
||||
|
assert(witness[1].equals(bigInt(0))); |
||||
|
|
||||
witness = circuit.calculateWitness({ "in[0]": "1", "in[1]": "0" }); |
|
||||
assert(witness[0].equals(snarkjs.bigInt(1))); |
|
||||
assert(witness[1].equals(snarkjs.bigInt(1))); |
|
||||
|
witness = await circuit.calculateWitness({ "in": [1, 0] }, true); |
||||
|
assert(witness[0].equals(bigInt(1))); |
||||
|
assert(witness[1].equals(bigInt(1))); |
||||
|
|
||||
witness = circuit.calculateWitness({ "in[0]": "555", "in[1]": "0" }); |
|
||||
assert(witness[0].equals(snarkjs.bigInt(1))); |
|
||||
assert(witness[1].equals(snarkjs.bigInt(1))); |
|
||||
|
witness = await circuit.calculateWitness({ "in": [555, 0] }, true); |
||||
|
assert(witness[0].equals(bigInt(1))); |
||||
|
assert(witness[1].equals(bigInt(1))); |
||||
|
|
||||
witness = circuit.calculateWitness({ "in[0]": "0", "in[1]": "0" }); |
|
||||
assert(witness[0].equals(snarkjs.bigInt(1))); |
|
||||
assert(witness[1].equals(snarkjs.bigInt(0))); |
|
||||
|
witness = await circuit.calculateWitness({ "in": [0, 0] }, true); |
||||
|
assert(witness[0].equals(bigInt(1))); |
||||
|
assert(witness[1].equals(bigInt(0))); |
||||
}); |
}); |
||||
it("Should create a comparison greatereqthan", async() => { |
it("Should create a comparison greatereqthan", async() => { |
||||
const cirDef = await compiler(path.join(__dirname, "circuits", "greatereqthan.circom")); |
|
||||
|
|
||||
const circuit = new snarkjs.Circuit(cirDef); |
|
||||
|
|
||||
console.log("NConstraints BalancesUpdater: " + circuit.nConstraints); |
|
||||
|
const circuit = await tester(path.join(__dirname, "circuits", "greatereqthan.circom")); |
||||
|
|
||||
let witness; |
let witness; |
||||
witness = circuit.calculateWitness({ "in[0]": "333", "in[1]": "444" }); |
|
||||
assert(witness[0].equals(snarkjs.bigInt(1))); |
|
||||
assert(witness[1].equals(snarkjs.bigInt(0))); |
|
||||
|
witness = await circuit.calculateWitness({ "in": [333,444] }, true); |
||||
|
assert(witness[0].equals(bigInt(1))); |
||||
|
assert(witness[1].equals(bigInt(0))); |
||||
|
|
||||
witness = circuit.calculateWitness({ "in[0]": "1", "in[1]": "1" }); |
|
||||
assert(witness[0].equals(snarkjs.bigInt(1))); |
|
||||
assert(witness[1].equals(snarkjs.bigInt(1))); |
|
||||
|
witness = await circuit.calculateWitness({ "in":[1,1] }, true); |
||||
|
assert(witness[0].equals(bigInt(1))); |
||||
|
assert(witness[1].equals(bigInt(1))); |
||||
|
|
||||
witness = circuit.calculateWitness({ "in[0]": "661", "in[1]": "660" }); |
|
||||
assert(witness[0].equals(snarkjs.bigInt(1))); |
|
||||
assert(witness[1].equals(snarkjs.bigInt(1))); |
|
||||
|
witness = await circuit.calculateWitness({ "in": [661, 660] }, true); |
||||
|
assert(witness[0].equals(bigInt(1))); |
||||
|
assert(witness[1].equals(bigInt(1))); |
||||
|
|
||||
witness = circuit.calculateWitness({ "in[0]": "0", "in[1]": "1" }); |
|
||||
assert(witness[0].equals(snarkjs.bigInt(1))); |
|
||||
assert(witness[1].equals(snarkjs.bigInt(0))); |
|
||||
|
witness = await circuit.calculateWitness({ "in": [0, 1] }, true); |
||||
|
assert(witness[0].equals(bigInt(1))); |
||||
|
assert(witness[1].equals(bigInt(0))); |
||||
|
|
||||
witness = circuit.calculateWitness({ "in[0]": "0", "in[1]": "444" }); |
|
||||
assert(witness[0].equals(snarkjs.bigInt(1))); |
|
||||
assert(witness[1].equals(snarkjs.bigInt(0))); |
|
||||
|
witness = await circuit.calculateWitness({ "in": [0, 444] }, true); |
||||
|
assert(witness[0].equals(bigInt(1))); |
||||
|
assert(witness[1].equals(bigInt(0))); |
||||
|
|
||||
witness = circuit.calculateWitness({ "in[0]": "1", "in[1]": "0" }); |
|
||||
assert(witness[0].equals(snarkjs.bigInt(1))); |
|
||||
assert(witness[1].equals(snarkjs.bigInt(1))); |
|
||||
|
witness = await circuit.calculateWitness({ "in": [1, 0] }, true); |
||||
|
assert(witness[0].equals(bigInt(1))); |
||||
|
assert(witness[1].equals(bigInt(1))); |
||||
|
|
||||
witness = circuit.calculateWitness({ "in[0]": "555", "in[1]": "0" }); |
|
||||
assert(witness[0].equals(snarkjs.bigInt(1))); |
|
||||
assert(witness[1].equals(snarkjs.bigInt(1))); |
|
||||
|
witness = await circuit.calculateWitness({ "in": [555, 0] }, true); |
||||
|
assert(witness[0].equals(bigInt(1))); |
||||
|
assert(witness[1].equals(bigInt(1))); |
||||
|
|
||||
witness = circuit.calculateWitness({ "in[0]": "0", "in[1]": "0" }); |
|
||||
assert(witness[0].equals(snarkjs.bigInt(1))); |
|
||||
assert(witness[1].equals(snarkjs.bigInt(1))); |
|
||||
|
witness = await circuit.calculateWitness({ "in": [0, 0] }, true); |
||||
|
assert(witness[0].equals(bigInt(1))); |
||||
|
assert(witness[1].equals(bigInt(1))); |
||||
}); |
}); |
||||
}); |
}); |
@ -1,172 +1,114 @@ |
|||||
const chai = require("chai"); |
const chai = require("chai"); |
||||
const path = require("path"); |
const path = require("path"); |
||||
const snarkjs = require("snarkjs"); |
|
||||
const compiler = require("circom"); |
|
||||
|
const bigInt = require("big-integer"); |
||||
|
const tester = require("circom").tester; |
||||
|
const babyJub = require("../src/babyjub.js"); |
||||
|
|
||||
const assert = chai.assert; |
const assert = chai.assert; |
||||
|
|
||||
const bigInt = snarkjs.bigInt; |
|
||||
|
|
||||
|
|
||||
const q=bigInt("21888242871839275222246405745257275088548364400416034343698204186575808495617"); |
|
||||
function addPoint(a,b) { |
|
||||
const cta = bigInt("168700"); |
|
||||
const d = bigInt("168696"); |
|
||||
|
|
||||
const res = []; |
|
||||
res[0] = bigInt((a[0]*b[1] + b[0]*a[1]) * bigInt(bigInt.one + 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.one - d*a[0]*b[0]*a[1]*b[1]).inverse(q)).affine(q); |
|
||||
return res; |
|
||||
} |
|
||||
|
|
||||
function print(circuit, w, s) { |
function print(circuit, w, s) { |
||||
console.log(s + ": " + w[circuit.getSignalIdx(s)]); |
console.log(s + ": " + w[circuit.getSignalIdx(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")); |
|
||||
|
describe("Exponentioation test", function () { |
||||
|
|
||||
// console.log(JSON.stringify(cirDef, null, 1));
|
|
||||
|
this.timeout(100000); |
||||
|
|
||||
// assert.equal(cirDef.nVars, 2);
|
|
||||
|
it("Should generate the Exponentiation table in k=0", async () => { |
||||
|
|
||||
const circuit = new snarkjs.Circuit(cirDef); |
|
||||
|
const circuit = await tester(path.join(__dirname, "circuits", "escalarmulw4table_test.circom")); |
||||
|
|
||||
console.log("NConstrains: " + circuit.nConstraints); |
|
||||
|
const w = await circuit.calculateWitness({in: 1}); |
||||
|
|
||||
const w = circuit.calculateWitness({in: 1}); |
|
||||
|
await circuit.checkConstraints(w); |
||||
|
|
||||
assert(circuit.checkWitness(w)); |
|
||||
|
let g = [ |
||||
|
bigInt("5299619240641551281634865583518297030282874472190772894086521144482721001553"), |
||||
|
bigInt("16950150798460657717958625567821834550301663161624707787222815936182638968203") |
||||
|
]; |
||||
|
|
||||
let g = [bigInt("5299619240641551281634865583518297030282874472190772894086521144482721001553"), |
|
||||
bigInt("16950150798460657717958625567821834550301663161624707787222815936182638968203")] |
|
||||
|
let dbl= [bigInt("0"), bigInt("1")]; |
||||
|
|
||||
dbl= [bigInt("0"), snarkjs.bigInt("1")]; |
|
||||
|
const expectedOut = []; |
||||
|
|
||||
for (let i=0; i<16; i++) { |
for (let i=0; i<16; i++) { |
||||
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]);
|
|
||||
// console.log(dbl[1]);
|
|
||||
|
|
||||
assert(xout1.equals(dbl[0])); |
|
||||
assert(yout1.equals(dbl[1])); |
|
||||
|
|
||||
dbl = addPoint([xout1, yout1],g); |
|
||||
|
expectedOut.push(dbl); |
||||
|
dbl = babyJub.addPoint(dbl,g); |
||||
} |
} |
||||
|
|
||||
|
await circuit.assertOut(w, {out: expectedOut}); |
||||
|
|
||||
}); |
}); |
||||
|
|
||||
it("Should generate the Exponentiation table in k=3", async () => { |
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));
|
|
||||
|
|
||||
// assert.equal(cirDef.nVars, 2);
|
|
||||
|
const circuit = await tester(path.join(__dirname, "circuits", "escalarmulw4table_test3.circom")); |
||||
|
|
||||
const circuit = new snarkjs.Circuit(cirDef); |
|
||||
|
const w = await circuit.calculateWitness({in: 1}); |
||||
|
|
||||
console.log("NConstrains: " + circuit.nConstraints); |
|
||||
|
await circuit.checkConstraints(w); |
||||
|
|
||||
const w = circuit.calculateWitness({in: 1}); |
|
||||
|
|
||||
assert(circuit.checkWitness(w)); |
|
||||
|
|
||||
let g = [snarkjs.bigInt("5299619240641551281634865583518297030282874472190772894086521144482721001553"), |
|
||||
snarkjs.bigInt("16950150798460657717958625567821834550301663161624707787222815936182638968203")] |
|
||||
|
let g = [ |
||||
|
bigInt("5299619240641551281634865583518297030282874472190772894086521144482721001553"), |
||||
|
bigInt("16950150798460657717958625567821834550301663161624707787222815936182638968203") |
||||
|
]; |
||||
|
|
||||
for (let i=0; i<12;i++) { |
for (let i=0; i<12;i++) { |
||||
g = addPoint(g,g); |
|
||||
|
g = babyJub.addPoint(g,g); |
||||
} |
} |
||||
|
|
||||
dbl= [snarkjs.bigInt("0"), snarkjs.bigInt("1")]; |
|
||||
|
|
||||
for (let i=0; i<16; i++) { |
|
||||
const xout1 = w[circuit.getSignalIdx(`main.out[${i}][0]`)]; |
|
||||
const yout1 = w[circuit.getSignalIdx(`main.out[${i}][1]`)]; |
|
||||
|
let dbl= [bigInt("0"), bigInt("1")]; |
||||
|
|
||||
|
const expectedOut = []; |
||||
|
|
||||
// console.log(xout1.toString());
|
|
||||
// console.log(yout1.toString());
|
|
||||
// console.log(dbl[0]);
|
|
||||
// console.log(dbl[1]);
|
|
||||
|
|
||||
assert(xout1.equals(dbl[0])); |
|
||||
assert(yout1.equals(dbl[1])); |
|
||||
|
for (let i=0; i<16; i++) { |
||||
|
expectedOut.push(dbl); |
||||
|
|
||||
dbl = addPoint([xout1, yout1],g); |
|
||||
|
dbl = babyJub.addPoint(dbl,g); |
||||
} |
} |
||||
|
|
||||
|
await circuit.assertOut(w, {out: expectedOut}); |
||||
|
|
||||
}); |
}); |
||||
|
|
||||
it("Should exponentiate g^31", async () => { |
it("Should exponentiate g^31", async () => { |
||||
const cirDef = await compiler(path.join(__dirname, "circuits", "escalarmul_test.circom"), {reduceConstraints: true}); |
|
||||
|
|
||||
// console.log(JSON.stringify(cirDef, null, 1));
|
|
||||
|
const circuit = await tester(path.join(__dirname, "circuits", "escalarmul_test.circom")); |
||||
|
|
||||
// assert.equal(cirDef.nVars, 2);
|
|
||||
|
const w = await circuit.calculateWitness({"in": 31}); |
||||
|
|
||||
const circuit = new snarkjs.Circuit(cirDef); |
|
||||
|
await circuit.checkConstraints(w); |
||||
|
|
||||
console.log("NConstrains: " + circuit.nConstraints); |
|
||||
|
let g = [ |
||||
|
bigInt("5299619240641551281634865583518297030282874472190772894086521144482721001553"), |
||||
|
bigInt("16950150798460657717958625567821834550301663161624707787222815936182638968203") |
||||
|
]; |
||||
|
|
||||
const w = circuit.calculateWitness({"in": 31}); |
|
||||
|
|
||||
assert(circuit.checkWitness(w)); |
|
||||
|
|
||||
let g = [snarkjs.bigInt("5299619240641551281634865583518297030282874472190772894086521144482721001553"), |
|
||||
snarkjs.bigInt("16950150798460657717958625567821834550301663161624707787222815936182638968203")] |
|
||||
|
|
||||
let c = [0n, 1n]; |
|
||||
|
let c = [bigInt(0), bigInt(1)]; |
||||
|
|
||||
for (let i=0; i<31;i++) { |
for (let i=0; i<31;i++) { |
||||
c = addPoint(c,g); |
|
||||
|
c = babyJub.addPoint(c,g); |
||||
} |
} |
||||
|
|
||||
const xout = w[circuit.getSignalIdx(`main.out[0]`)]; |
|
||||
const yout = w[circuit.getSignalIdx(`main.out[1]`)]; |
|
||||
|
await circuit.assertOut(w, {out: c}); |
||||
|
|
||||
/* |
|
||||
console.log(xout.toString()); |
|
||||
console.log(yout.toString()); |
|
||||
*/ |
|
||||
assert(xout.equals(c[0])); |
|
||||
assert(yout.equals(c[1])); |
|
||||
|
|
||||
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]`)]; |
|
||||
|
const w2 = await circuit.calculateWitness({"in": bigInt(1).shiftLeft(252).add(bigInt.one)}); |
||||
|
|
||||
c = [g[0], g[1]]; |
c = [g[0], g[1]]; |
||||
for (let i=0; i<252;i++) { |
for (let i=0; i<252;i++) { |
||||
c = addPoint(c,c); |
|
||||
|
c = babyJub.addPoint(c,c); |
||||
} |
} |
||||
c = addPoint(c,g); |
|
||||
|
|
||||
// console.log(xout2.toString());
|
|
||||
// console.log(yout2.toString());
|
|
||||
// console.log(c[0].toString());
|
|
||||
// console.log(c[1].toString());
|
|
||||
|
c = babyJub.addPoint(c,g); |
||||
|
|
||||
assert(xout2.equals(c[0])); |
|
||||
assert(yout2.equals(c[1])); |
|
||||
|
await circuit.assertOut(w2, {out: c}); |
||||
|
|
||||
}).timeout(10000000); |
}).timeout(10000000); |
||||
|
|
||||
it("Number of constrains for 256 bits", async () => { |
it("Number of constrains for 256 bits", async () => { |
||||
const cirDef = await compiler(path.join(__dirname, "circuits", "escalarmul_test_min.circom")); |
|
||||
|
|
||||
const circuit = new snarkjs.Circuit(cirDef); |
|
||||
|
const circuit = await tester(path.join(__dirname, "circuits", "escalarmul_test_min.circom")); |
||||
|
|
||||
console.log("NConstrains: " + circuit.nConstraints); |
|
||||
}).timeout(10000000); |
}).timeout(10000000); |
||||
|
|
||||
}); |
}); |
@ -1,35 +1,25 @@ |
|||||
const chai = require("chai"); |
const chai = require("chai"); |
||||
const path = require("path"); |
const path = require("path"); |
||||
const snarkjs = require("snarkjs"); |
|
||||
const compiler = require("circom"); |
|
||||
|
const tester = require("circom").tester; |
||||
|
|
||||
const mimcjs = require("../src/mimc7.js"); |
const mimcjs = require("../src/mimc7.js"); |
||||
|
|
||||
const assert = chai.assert; |
|
||||
|
|
||||
describe("MiMC Circuit test", function () { |
describe("MiMC Circuit test", function () { |
||||
let circuit; |
let circuit; |
||||
|
|
||||
this.timeout(100000); |
this.timeout(100000); |
||||
|
|
||||
before( async () => { |
before( async () => { |
||||
const cirDef = await compiler(path.join(__dirname, "circuits", "mimc_test.circom")); |
|
||||
|
|
||||
circuit = new snarkjs.Circuit(cirDef); |
|
||||
|
|
||||
console.log("MiMC constraints: " + circuit.nConstraints); |
|
||||
|
circuit = await tester(path.join(__dirname, "circuits", "mimc_test.circom")); |
||||
}); |
}); |
||||
|
|
||||
it("Should check constrain", async () => { |
it("Should check constrain", async () => { |
||||
const w = circuit.calculateWitness({x_in: 1, k: 2}); |
|
||||
|
|
||||
const res = w[circuit.getSignalIdx("main.out")]; |
|
||||
|
const w = await circuit.calculateWitness({x_in: 1, k: 2}, true); |
||||
|
|
||||
const res2 = mimcjs.hash(1,2,91); |
const res2 = mimcjs.hash(1,2,91); |
||||
|
|
||||
assert.equal(res.toString(), res2.toString()); |
|
||||
|
|
||||
assert(circuit.checkWitness(w)); |
|
||||
|
await circuit.assertOut(w, {out: res2}); |
||||
|
|
||||
|
await circuit.checkConstraints(w); |
||||
}); |
}); |
||||
}); |
}); |
@ -1,23 +0,0 @@ |
|||||
|
|
||||
include "../circuits/smt/smtverifier.circom"; |
|
||||
template SMT(nLevels) { |
|
||||
signal input root; |
|
||||
signal input mtp[nLevels]; |
|
||||
signal input hi; |
|
||||
signal input hv; |
|
||||
|
|
||||
component smtClaimExists = SMTVerifier(nLevels); |
|
||||
smtClaimExists.enabled <== 1; |
|
||||
smtClaimExists.fnc <== 0; |
|
||||
smtClaimExists.root <== root; |
|
||||
for (var i=0; i<nLevels; i++) { |
|
||||
smtClaimExists.siblings[i] <== mtp[i]; |
|
||||
} |
|
||||
smtClaimExists.oldKey <== 0; |
|
||||
smtClaimExists.oldValue <== 0; |
|
||||
smtClaimExists.isOld0 <== 0; |
|
||||
|
|
||||
smtClaimExists.key <== hi; |
|
||||
smtClaimExists.value <== hv; |
|
||||
} |
|
||||
component main = SMT(4); |
|
@ -1,98 +0,0 @@ |
|||||
const path = require("path"); |
|
||||
const snarkjs = require("snarkjs"); |
|
||||
const compiler = require("circom"); |
|
||||
const fs = require("fs") |
|
||||
|
|
||||
const bigInt = snarkjs.bigInt; |
|
||||
const smt = require("../src/smt.js"); |
|
||||
|
|
||||
const circuitSource = `
|
|
||||
include "../circuits/smt/smtverifier.circom"; |
|
||||
template SMT(nLevels) { |
|
||||
signal input root; |
|
||||
signal input mtp[nLevels]; |
|
||||
signal input hi; |
|
||||
signal input hv; |
|
||||
|
|
||||
component smtClaimExists = SMTVerifier(nLevels); |
|
||||
smtClaimExists.enabled <== 1; |
|
||||
smtClaimExists.fnc <== 0; |
|
||||
smtClaimExists.root <== root; |
|
||||
for (var i=0; i<nLevels; i++) { |
|
||||
smtClaimExists.siblings[i] <== mtp[i]; |
|
||||
} |
|
||||
smtClaimExists.oldKey <== 0; |
|
||||
smtClaimExists.oldValue <== 0; |
|
||||
smtClaimExists.isOld0 <== 0; |
|
||||
|
|
||||
smtClaimExists.key <== hi; |
|
||||
smtClaimExists.value <== hv; |
|
||||
} |
|
||||
component main = SMT(4); |
|
||||
`;
|
|
||||
|
|
||||
describe("smt3test", function () { |
|
||||
this.timeout(200000); |
|
||||
|
|
||||
let circuitFileName; |
|
||||
|
|
||||
before( async () => { |
|
||||
circuitFileName = path.join(__dirname, ".", "rawsmt3.circom"); |
|
||||
fs.writeFileSync(circuitFileName,circuitSource); |
|
||||
}); |
|
||||
|
|
||||
const levels = 4; |
|
||||
async function testsmt3(e1, e2) { |
|
||||
let tree = await smt.newMemEmptyTrie(); |
|
||||
|
|
||||
// insert e1, e2
|
|
||||
await tree.insert(e1.hi, e1.hv); |
|
||||
await tree.insert(e2.hi, e2.hv); |
|
||||
|
|
||||
// generate proof for e1
|
|
||||
const findInfo = await tree.find(e1.hi); |
|
||||
const siblings = findInfo.siblings; |
|
||||
while (siblings.length < levels) siblings.push(bigInt(0)); |
|
||||
|
|
||||
const input = { |
|
||||
root: tree.root, |
|
||||
mtp: siblings, |
|
||||
hi: e1.hi, |
|
||||
hv: e1.hv, |
|
||||
}; |
|
||||
|
|
||||
const compiledCircuit = await compiler( |
|
||||
circuitFileName, |
|
||||
{ reduceConstraints: false } |
|
||||
); |
|
||||
|
|
||||
const circuit = new snarkjs.Circuit(compiledCircuit); |
|
||||
const witness = circuit.calculateWitness(input); |
|
||||
circuit.checkWitness(witness); |
|
||||
} |
|
||||
|
|
||||
it("TestSmts", async () => { |
|
||||
|
|
||||
const e1 = { |
|
||||
hi: bigInt("17124152697573569611556136390143205198134245887034837071647643529178599000839"), |
|
||||
hv: bigInt("19650379996168153643111744440707177573540245771926102415571667548153444658179"), |
|
||||
}; |
|
||||
|
|
||||
const e2ok = { |
|
||||
hi: bigInt("16498254692537945203721083102154618658340563351558973077349594629411025251262"), |
|
||||
hv: bigInt("19650379996168153643111744440707177573540245771926102415571667548153444658179"), |
|
||||
}; |
|
||||
|
|
||||
const e2fail = { |
|
||||
hi: bigInt("17195092312975762537892237130737365903429674363577646686847513978084990105579"), |
|
||||
hv: bigInt("19650379996168153643111744440707177573540245771926102415571667548153444658179"), |
|
||||
}; |
|
||||
|
|
||||
console.log("test e1, e2ok"); |
|
||||
await testsmt3(e1, e2ok); |
|
||||
|
|
||||
console.log("test e1, e2fail"); |
|
||||
await testsmt3(e1, e2fail); |
|
||||
}); |
|
||||
}); |
|
||||
|
|