mirror of
https://github.com/arnaucube/grapevine-sonobe.git
synced 2026-01-12 08:51:30 +01:00
out of bounds on constraint gen maybe 2d array?
This commit is contained in:
77
circom/node_modules/circomlib/test/aliascheck.js
generated
vendored
Normal file
77
circom/node_modules/circomlib/test/aliascheck.js
generated
vendored
Normal file
@@ -0,0 +1,77 @@
|
||||
const chai = require("chai");
|
||||
const path = require("path");
|
||||
|
||||
const assert = chai.assert;
|
||||
|
||||
const Scalar = require("ffjavascript").Scalar;
|
||||
const F1Field = require("ffjavascript").F1Field;
|
||||
const utils = require("ffjavascript").utils;
|
||||
const q = Scalar.fromString("21888242871839275222246405745257275088548364400416034343698204186575808495617");
|
||||
const F = new F1Field(q);
|
||||
|
||||
const wasm_tester = require("circom_tester").wasm;
|
||||
|
||||
function print(circuit, w, s) {
|
||||
console.log(s + ": " + w[circuit.getSignalIdx(s)]);
|
||||
}
|
||||
|
||||
function getBits(v, n) {
|
||||
const res = [];
|
||||
for (let i=0; i<n; i++) {
|
||||
if (Scalar.isOdd(Scalar.shr(v,i))) {
|
||||
res.push(F.one);
|
||||
} else {
|
||||
res.push(F.zero);
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
describe("Aliascheck test", function () {
|
||||
this.timeout(100000);
|
||||
|
||||
let cir;
|
||||
before( async() => {
|
||||
|
||||
cir = await wasm_tester(path.join(__dirname, "circuits", "aliascheck_test.circom"));
|
||||
});
|
||||
|
||||
it("Satisfy the aliastest 0", async () => {
|
||||
const inp = getBits(0, 254);
|
||||
await cir.calculateWitness({in: inp}, true);
|
||||
});
|
||||
|
||||
it("Satisfy the aliastest 3", async () => {
|
||||
const inp = getBits(3, 254);
|
||||
await cir.calculateWitness({in: inp}, true);
|
||||
});
|
||||
|
||||
it("Satisfy the aliastest q-1", async () => {
|
||||
const inp = getBits(F.e(-1), 254);
|
||||
// console.log(JSON.stringify(utils.stringifyBigInts(inp)));
|
||||
await cir.calculateWitness({in: inp}, true);
|
||||
});
|
||||
|
||||
it("Should not satisfy an input of q", async () => {
|
||||
const inp = getBits(q, 254);
|
||||
try {
|
||||
await cir.calculateWitness({in: inp}, true);
|
||||
assert(false);
|
||||
} catch(err) {
|
||||
assert(err.message.includes("Assert Failed"));
|
||||
}
|
||||
});
|
||||
|
||||
it("Should not satisfy all ones", async () => {
|
||||
|
||||
const inp = getBits(Scalar.sub(Scalar.shl(1, 254) , 1) , 254);
|
||||
try {
|
||||
await cir.calculateWitness({in: inp}, true);
|
||||
assert(false);
|
||||
} catch(err) {
|
||||
assert(err.message.includes("Assert Failed"));
|
||||
}
|
||||
});
|
||||
|
||||
});
|
||||
118
circom/node_modules/circomlib/test/babyjub.js
generated
vendored
Normal file
118
circom/node_modules/circomlib/test/babyjub.js
generated
vendored
Normal file
@@ -0,0 +1,118 @@
|
||||
const chai = require("chai");
|
||||
const path = require("path");
|
||||
|
||||
const createBlakeHash = require("blake-hash");
|
||||
const buildEddsa = require("circomlibjs").buildEddsa;
|
||||
|
||||
const assert = chai.assert;
|
||||
|
||||
const wasm_tester = require("circom_tester").wasm;
|
||||
const utils = require("ffjavascript").utils;
|
||||
const Scalar = require("ffjavascript").Scalar;
|
||||
|
||||
describe("Baby Jub test", function () {
|
||||
let eddsa;
|
||||
let F;
|
||||
let circuitAdd;
|
||||
let circuitTest;
|
||||
let circuitPbk;
|
||||
|
||||
this.timeout(100000);
|
||||
|
||||
before( async() => {
|
||||
|
||||
eddsa = await buildEddsa();
|
||||
F = eddsa.F;
|
||||
|
||||
circuitAdd = await wasm_tester(path.join(__dirname, "circuits", "babyadd_tester.circom"));
|
||||
|
||||
circuitTest = await wasm_tester(path.join(__dirname, "circuits", "babycheck_test.circom"));
|
||||
|
||||
circuitPbk = await wasm_tester(path.join(__dirname, "circuits", "babypbk_test.circom"));
|
||||
});
|
||||
|
||||
it("Should add point (0,1) and (0,1)", async () => {
|
||||
|
||||
const input={
|
||||
x1: 0,
|
||||
y1: 1,
|
||||
x2: 0,
|
||||
y2: 1
|
||||
};
|
||||
|
||||
const w = await circuitAdd.calculateWitness(input, true);
|
||||
|
||||
await circuitAdd.assertOut(w, {xout: 0, yout: 1});
|
||||
});
|
||||
|
||||
it("Should add 2 same numbers", async () => {
|
||||
|
||||
const input={
|
||||
x1: 17777552123799933955779906779655732241715742912184938656739573121738514868268n,
|
||||
y1: 2626589144620713026669568689430873010625803728049924121243784502389097019475n,
|
||||
x2: 17777552123799933955779906779655732241715742912184938656739573121738514868268n,
|
||||
y2: 2626589144620713026669568689430873010625803728049924121243784502389097019475n
|
||||
};
|
||||
|
||||
const w = await circuitAdd.calculateWitness(input, true);
|
||||
|
||||
await circuitAdd.assertOut(w, {
|
||||
xout: 6890855772600357754907169075114257697580319025794532037257385534741338397365n,
|
||||
yout: 4338620300185947561074059802482547481416142213883829469920100239455078257889n
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
it("Should add 2 different numbers", async () => {
|
||||
|
||||
const input={
|
||||
x1: 17777552123799933955779906779655732241715742912184938656739573121738514868268n,
|
||||
y1: 2626589144620713026669568689430873010625803728049924121243784502389097019475n,
|
||||
x2: 16540640123574156134436876038791482806971768689494387082833631921987005038935n,
|
||||
y2: 20819045374670962167435360035096875258406992893633759881276124905556507972311n
|
||||
};
|
||||
|
||||
const w = await circuitAdd.calculateWitness(input, true);
|
||||
|
||||
await circuitAdd.assertOut(w, {
|
||||
xout: 7916061937171219682591368294088513039687205273691143098332585753343424131937n,
|
||||
yout: 14035240266687799601661095864649209771790948434046947201833777492504781204499n
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
it("Should check (0,1) is a valid point", async() => {
|
||||
const w = await circuitTest.calculateWitness({x: 0, y:1}, true);
|
||||
|
||||
await circuitTest.checkConstraints(w);
|
||||
});
|
||||
|
||||
it("Should check (1,0) is an invalid point", async() => {
|
||||
try {
|
||||
await circuitTest.calculateWitness({x: 1, y: 0}, true);
|
||||
assert(false, "Should be a valid point");
|
||||
} catch(err) {
|
||||
assert(err.message.includes("Assert Failed"));
|
||||
}
|
||||
});
|
||||
|
||||
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 = Scalar.shr(utils.leBuff2int(pvk), 3);
|
||||
|
||||
const A = eddsa.prv2pub(rawpvk);
|
||||
|
||||
const input = {
|
||||
in : S
|
||||
};
|
||||
|
||||
const w = await circuitPbk.calculateWitness(input, true);
|
||||
|
||||
await circuitPbk.assertOut(w, {Ax : F.toObject(A[0]), Ay: F.toObject(A[1])});
|
||||
|
||||
await circuitPbk.checkConstraints(w);
|
||||
});
|
||||
|
||||
});
|
||||
52
circom/node_modules/circomlib/test/binsub.js
generated
vendored
Normal file
52
circom/node_modules/circomlib/test/binsub.js
generated
vendored
Normal file
@@ -0,0 +1,52 @@
|
||||
const path = require("path");
|
||||
|
||||
const Scalar = require("ffjavascript").Scalar;
|
||||
const wasm_tester = require("circom_tester").wasm;
|
||||
|
||||
function print(circuit, w, s) {
|
||||
console.log(s + ": " + w[circuit.getSignalIdx(s)]);
|
||||
}
|
||||
|
||||
async function checkSub(_a,_b, circuit) {
|
||||
let a=Scalar.e(_a);
|
||||
let b=Scalar.e(_b);
|
||||
if (Scalar.lt(a, 0)) a = Scalar.add(a, Scalar.shl(1, 16));
|
||||
if (Scalar.lt(b, 0)) b = Scalar.add(b, Scalar.shl(1, 16));
|
||||
const w = await circuit.calculateWitness({a: a, b: b}, true);
|
||||
|
||||
let res = Scalar.sub(a, b);
|
||||
if (Scalar.lt(res, 0)) res = Scalar.add(res, Scalar.shl(1, 16));
|
||||
|
||||
await circuit.assertOut(w, {out: res});
|
||||
}
|
||||
|
||||
describe("BinSub test", function () {
|
||||
|
||||
this.timeout(100000);
|
||||
|
||||
let circuit;
|
||||
before( async() => {
|
||||
circuit = await wasm_tester(path.join(__dirname, "circuits", "binsub_test.circom"));
|
||||
});
|
||||
|
||||
it("Should check variuos ege cases", async () => {
|
||||
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);
|
||||
});
|
||||
|
||||
|
||||
});
|
||||
38
circom/node_modules/circomlib/test/binsum.js
generated
vendored
Normal file
38
circom/node_modules/circomlib/test/binsum.js
generated
vendored
Normal file
@@ -0,0 +1,38 @@
|
||||
const chai = require("chai");
|
||||
const path = require("path");
|
||||
|
||||
const wasm_tester = require("circom_tester").wasm;
|
||||
|
||||
const F1Field = require("ffjavascript").F1Field;
|
||||
const Scalar = require("ffjavascript").Scalar;
|
||||
exports.p = Scalar.fromString("21888242871839275222246405745257275088548364400416034343698204186575808495617");
|
||||
const Fr = new F1Field(exports.p);
|
||||
|
||||
const assert = chai.assert;
|
||||
|
||||
describe("Binary sum test", function () {
|
||||
this.timeout(100000000);
|
||||
|
||||
it("Should create a constant circuit", async () => {
|
||||
const circuit = await wasm_tester(path.join(__dirname, "circuits", "constants_test.circom"));
|
||||
await circuit.loadConstraints();
|
||||
assert.equal(circuit.nVars, 2);
|
||||
assert.equal(circuit.constraints.length, 1);
|
||||
|
||||
const witness = await circuit.calculateWitness({ "in": Fr.toString(Fr.e("0xd807aa98"))}, true);
|
||||
|
||||
assert(Fr.eq(Fr.e(witness[0]),Fr.e(1)));
|
||||
assert(Fr.eq(Fr.e(witness[1]),Fr.e("0xd807aa98")));
|
||||
});
|
||||
it("Should create a sum circuit", async () => {
|
||||
const circuit = await wasm_tester(path.join(__dirname, "circuits", "sum_test.circom"));
|
||||
await circuit.loadConstraints();
|
||||
|
||||
assert.equal(circuit.constraints.length, 97); // 32 (in1) + 32(in2) + 32(out) + 1 (carry)
|
||||
|
||||
const witness = await circuit.calculateWitness({ "a": "111", "b": "222" }, true);
|
||||
|
||||
assert(Fr.eq(Fr.e(witness[0]),Fr.e(1)));
|
||||
assert(Fr.eq(Fr.e(witness[1]),Fr.e("333")));
|
||||
});
|
||||
});
|
||||
4
circom/node_modules/circomlib/test/circuits/aliascheck_test.circom
generated
vendored
Normal file
4
circom/node_modules/circomlib/test/circuits/aliascheck_test.circom
generated
vendored
Normal file
@@ -0,0 +1,4 @@
|
||||
pragma circom 2.0.0;
|
||||
include "../../circuits/aliascheck.circom";
|
||||
|
||||
component main = AliasCheck();
|
||||
4
circom/node_modules/circomlib/test/circuits/babyadd_tester.circom
generated
vendored
Normal file
4
circom/node_modules/circomlib/test/circuits/babyadd_tester.circom
generated
vendored
Normal file
@@ -0,0 +1,4 @@
|
||||
pragma circom 2.0.0;
|
||||
include "../../circuits/babyjub.circom";
|
||||
|
||||
component main = BabyAdd();
|
||||
4
circom/node_modules/circomlib/test/circuits/babycheck_test.circom
generated
vendored
Normal file
4
circom/node_modules/circomlib/test/circuits/babycheck_test.circom
generated
vendored
Normal file
@@ -0,0 +1,4 @@
|
||||
pragma circom 2.0.0;
|
||||
include "../../circuits/babyjub.circom";
|
||||
|
||||
component main = BabyCheck();
|
||||
4
circom/node_modules/circomlib/test/circuits/babypbk_test.circom
generated
vendored
Normal file
4
circom/node_modules/circomlib/test/circuits/babypbk_test.circom
generated
vendored
Normal file
@@ -0,0 +1,4 @@
|
||||
pragma circom 2.0.0;
|
||||
include "../../circuits/babyjub.circom";
|
||||
|
||||
component main = BabyPbk();
|
||||
33
circom/node_modules/circomlib/test/circuits/binsub_test.circom
generated
vendored
Normal file
33
circom/node_modules/circomlib/test/circuits/binsub_test.circom
generated
vendored
Normal file
@@ -0,0 +1,33 @@
|
||||
pragma circom 2.0.0;
|
||||
|
||||
include "../../circuits/bitify.circom";
|
||||
include "../../circuits/binsub.circom";
|
||||
|
||||
template A() {
|
||||
signal input a; //private
|
||||
signal input b;
|
||||
signal output out;
|
||||
|
||||
var i;
|
||||
|
||||
component n2ba = Num2Bits(16);
|
||||
component n2bb = Num2Bits(16);
|
||||
component sub = BinSub(16);
|
||||
component b2n = Bits2Num(16);
|
||||
|
||||
n2ba.in <== a;
|
||||
n2bb.in <== b;
|
||||
|
||||
for (i=0; i<16; i++) {
|
||||
sub.in[0][i] <== n2ba.out[i];
|
||||
sub.in[1][i] <== n2bb.out[i];
|
||||
}
|
||||
|
||||
for (i=0; i<16; i++) {
|
||||
b2n.in[i] <== sub.out[i];
|
||||
}
|
||||
|
||||
out <== b2n.out;
|
||||
}
|
||||
|
||||
component main = A();
|
||||
20
circom/node_modules/circomlib/test/circuits/constants_test.circom
generated
vendored
Normal file
20
circom/node_modules/circomlib/test/circuits/constants_test.circom
generated
vendored
Normal file
@@ -0,0 +1,20 @@
|
||||
pragma circom 2.0.0;
|
||||
|
||||
include "../../circuits/sha256/constants.circom";
|
||||
|
||||
template A() {
|
||||
signal input in;
|
||||
component h0;
|
||||
h0 = K(8);
|
||||
|
||||
var lc = 0;
|
||||
var e = 1;
|
||||
for (var i=0; i<32; i++) {
|
||||
lc = lc + e*h0.out[i];
|
||||
e *= 2;
|
||||
}
|
||||
|
||||
lc === in;
|
||||
}
|
||||
|
||||
component main {public [in]} = A();
|
||||
5
circom/node_modules/circomlib/test/circuits/eddsa_test.circom
generated
vendored
Normal file
5
circom/node_modules/circomlib/test/circuits/eddsa_test.circom
generated
vendored
Normal file
@@ -0,0 +1,5 @@
|
||||
pragma circom 2.0.0;
|
||||
|
||||
include "../../circuits/eddsa.circom";
|
||||
|
||||
component main = EdDSAVerifier(80);
|
||||
5
circom/node_modules/circomlib/test/circuits/eddsamimc_test.circom
generated
vendored
Normal file
5
circom/node_modules/circomlib/test/circuits/eddsamimc_test.circom
generated
vendored
Normal file
@@ -0,0 +1,5 @@
|
||||
pragma circom 2.0.0;
|
||||
|
||||
include "../../circuits/eddsamimc.circom";
|
||||
|
||||
component main = EdDSAMiMCVerifier();
|
||||
5
circom/node_modules/circomlib/test/circuits/eddsaposeidon_test.circom
generated
vendored
Normal file
5
circom/node_modules/circomlib/test/circuits/eddsaposeidon_test.circom
generated
vendored
Normal file
@@ -0,0 +1,5 @@
|
||||
pragma circom 2.0.0;
|
||||
|
||||
include "../../circuits/eddsaposeidon.circom";
|
||||
|
||||
component main = EdDSAPoseidonVerifier();
|
||||
5
circom/node_modules/circomlib/test/circuits/edwards2montgomery.circom
generated
vendored
Normal file
5
circom/node_modules/circomlib/test/circuits/edwards2montgomery.circom
generated
vendored
Normal file
@@ -0,0 +1,5 @@
|
||||
pragma circom 2.0.0;
|
||||
|
||||
include "../../circuits/montgomery.circom";
|
||||
|
||||
component main = Edwards2Montgomery();
|
||||
27
circom/node_modules/circomlib/test/circuits/escalarmul_min_test.circom
generated
vendored
Normal file
27
circom/node_modules/circomlib/test/circuits/escalarmul_min_test.circom
generated
vendored
Normal file
@@ -0,0 +1,27 @@
|
||||
pragma circom 2.0.0;
|
||||
|
||||
include "../../circuits/escalarmul.circom";
|
||||
|
||||
|
||||
template Main() {
|
||||
signal input in[256];
|
||||
signal output out[2];
|
||||
|
||||
var i;
|
||||
|
||||
var base[2] = [5299619240641551281634865583518297030282874472190772894086521144482721001553, 16950150798460657717958625567821834550301663161624707787222815936182638968203];
|
||||
|
||||
component escalarMul = EscalarMul(256, base);
|
||||
|
||||
escalarMul.inp[0] <== 0;
|
||||
escalarMul.inp[1] <== 1;
|
||||
|
||||
for (i=0; i<256; i++) {
|
||||
in[i] ==> escalarMul.in[i];
|
||||
}
|
||||
|
||||
escalarMul.out[0] ==> out[0];
|
||||
escalarMul.out[1] ==> out[1];
|
||||
}
|
||||
|
||||
component main = Main();
|
||||
33
circom/node_modules/circomlib/test/circuits/escalarmul_test.circom
generated
vendored
Normal file
33
circom/node_modules/circomlib/test/circuits/escalarmul_test.circom
generated
vendored
Normal file
@@ -0,0 +1,33 @@
|
||||
pragma circom 2.0.0;
|
||||
|
||||
include "../../circuits/escalarmul.circom";
|
||||
include "../../circuits/bitify.circom";
|
||||
|
||||
|
||||
template Main() {
|
||||
signal input in;
|
||||
signal output out[2];
|
||||
|
||||
var base[2] = [5299619240641551281634865583518297030282874472190772894086521144482721001553,
|
||||
16950150798460657717958625567821834550301663161624707787222815936182638968203];
|
||||
|
||||
|
||||
component n2b = Num2Bits(253);
|
||||
component escalarMul = EscalarMul(253, base);
|
||||
|
||||
escalarMul.inp[0] <== 0;
|
||||
escalarMul.inp[1] <== 1;
|
||||
|
||||
var i;
|
||||
|
||||
in ==> n2b.in;
|
||||
|
||||
for (i=0; i<253; i++) {
|
||||
n2b.out[i] ==> escalarMul.in[i];
|
||||
}
|
||||
|
||||
escalarMul.out[0] ==> out[0];
|
||||
escalarMul.out[1] ==> out[1];
|
||||
}
|
||||
|
||||
component main = Main();
|
||||
28
circom/node_modules/circomlib/test/circuits/escalarmul_test_min.circom
generated
vendored
Normal file
28
circom/node_modules/circomlib/test/circuits/escalarmul_test_min.circom
generated
vendored
Normal file
@@ -0,0 +1,28 @@
|
||||
pragma circom 2.0.0;
|
||||
|
||||
include "../../circuits/escalarmul.circom";
|
||||
|
||||
|
||||
template Main() {
|
||||
signal input in[256];
|
||||
signal output out[2];
|
||||
|
||||
var i;
|
||||
|
||||
var base[2] = [5299619240641551281634865583518297030282874472190772894086521144482721001553,
|
||||
16950150798460657717958625567821834550301663161624707787222815936182638968203];
|
||||
|
||||
component escalarMul = EscalarMul(256, base);
|
||||
|
||||
escalarMul.inp[0] <== 0;
|
||||
escalarMul.inp[1] <== 1;
|
||||
|
||||
for (i=0; i<256; i++) {
|
||||
in[i] ==> escalarMul.in[i];
|
||||
}
|
||||
|
||||
escalarMul.out[0] ==> out[0];
|
||||
escalarMul.out[1] ==> out[1];
|
||||
}
|
||||
|
||||
component main = Main();
|
||||
30
circom/node_modules/circomlib/test/circuits/escalarmulany_test.circom
generated
vendored
Normal file
30
circom/node_modules/circomlib/test/circuits/escalarmulany_test.circom
generated
vendored
Normal file
@@ -0,0 +1,30 @@
|
||||
pragma circom 2.0.0;
|
||||
|
||||
include "../../circuits/escalarmulany.circom";
|
||||
include "../../circuits/bitify.circom";
|
||||
|
||||
template Main() {
|
||||
signal input e;
|
||||
signal input p[2];
|
||||
signal output out[2];
|
||||
|
||||
component n2b = Num2Bits(253);
|
||||
component escalarMulAny = EscalarMulAny(253);
|
||||
|
||||
escalarMulAny.p[0] <== p[0];
|
||||
escalarMulAny.p[1] <== p[1];
|
||||
|
||||
var i;
|
||||
|
||||
e ==> n2b.in;
|
||||
|
||||
for (i=0; i<253; i++) {
|
||||
n2b.out[i] ==> escalarMulAny.e[i];
|
||||
}
|
||||
|
||||
escalarMulAny.out[0] ==> out[0];
|
||||
escalarMulAny.out[1] ==> out[1];
|
||||
}
|
||||
|
||||
component main = Main();
|
||||
|
||||
31
circom/node_modules/circomlib/test/circuits/escalarmulfix_test.circom
generated
vendored
Normal file
31
circom/node_modules/circomlib/test/circuits/escalarmulfix_test.circom
generated
vendored
Normal file
@@ -0,0 +1,31 @@
|
||||
pragma circom 2.0.0;
|
||||
|
||||
include "../../circuits/escalarmulfix.circom";
|
||||
include "../../circuits/bitify.circom";
|
||||
|
||||
|
||||
template Main() {
|
||||
signal input e;
|
||||
signal output out[2];
|
||||
|
||||
var base[2] = [5299619240641551281634865583518297030282874472190772894086521144482721001553,
|
||||
16950150798460657717958625567821834550301663161624707787222815936182638968203];
|
||||
|
||||
|
||||
component n2b = Num2Bits(253);
|
||||
component escalarMul = EscalarMulFix(253, base);
|
||||
|
||||
var i;
|
||||
|
||||
e ==> n2b.in;
|
||||
|
||||
for (i=0; i<253; i++) {
|
||||
n2b.out[i] ==> escalarMul.e[i];
|
||||
}
|
||||
|
||||
escalarMul.out[0] ==> out[0];
|
||||
escalarMul.out[1] ==> out[1];
|
||||
}
|
||||
|
||||
component main = Main();
|
||||
|
||||
20
circom/node_modules/circomlib/test/circuits/escalarmulw4table.circom
generated
vendored
Normal file
20
circom/node_modules/circomlib/test/circuits/escalarmulw4table.circom
generated
vendored
Normal file
@@ -0,0 +1,20 @@
|
||||
pragma circom 2.0.0;
|
||||
|
||||
include "../../circuits/escalarmulw4table.circom";
|
||||
|
||||
|
||||
|
||||
|
||||
template Main() {
|
||||
signal output out[16][2];
|
||||
var base[2] = [5299619240641551281634865583518297030282874472190772894086521144482721001553,
|
||||
16950150798460657717958625567821834550301663161624707787222815936182638968203];
|
||||
|
||||
var escalarMul[16][2] = EscalarMulW4Table(base, 0);
|
||||
for (var i=0; i<16; i++) {
|
||||
out[i][0] <== escalarMul[i][0];
|
||||
out[i][1] <== escalarMul[i][1];
|
||||
}
|
||||
}
|
||||
|
||||
component main = Main();
|
||||
19
circom/node_modules/circomlib/test/circuits/escalarmulw4table_test.circom
generated
vendored
Normal file
19
circom/node_modules/circomlib/test/circuits/escalarmulw4table_test.circom
generated
vendored
Normal file
@@ -0,0 +1,19 @@
|
||||
pragma circom 2.0.0;
|
||||
|
||||
include "../../circuits/escalarmulw4table.circom";
|
||||
|
||||
|
||||
template Main() {
|
||||
signal input in;
|
||||
signal output out[16][2];
|
||||
var base[2] = [5299619240641551281634865583518297030282874472190772894086521144482721001553,
|
||||
16950150798460657717958625567821834550301663161624707787222815936182638968203];
|
||||
|
||||
var escalarMul[16][2] = EscalarMulW4Table(base, 0);
|
||||
for (var i=0; i<16; i++) {
|
||||
out[i][0] <== escalarMul[i][0]*in;
|
||||
out[i][1] <== escalarMul[i][1]*in;
|
||||
}
|
||||
}
|
||||
|
||||
component main = Main();
|
||||
19
circom/node_modules/circomlib/test/circuits/escalarmulw4table_test3.circom
generated
vendored
Normal file
19
circom/node_modules/circomlib/test/circuits/escalarmulw4table_test3.circom
generated
vendored
Normal file
@@ -0,0 +1,19 @@
|
||||
pragma circom 2.0.0;
|
||||
|
||||
include "../../circuits/escalarmulw4table.circom";
|
||||
|
||||
|
||||
template Main() {
|
||||
signal input in;
|
||||
signal output out[16][2];
|
||||
var base[2] = [5299619240641551281634865583518297030282874472190772894086521144482721001553,
|
||||
16950150798460657717958625567821834550301663161624707787222815936182638968203];
|
||||
|
||||
var escalarMul[16][2] = EscalarMulW4Table(base, 3);
|
||||
for (var i=0; i<16; i++) {
|
||||
out[i][0] <== escalarMul[i][0]*in;
|
||||
out[i][1] <== escalarMul[i][1]*in;
|
||||
}
|
||||
}
|
||||
|
||||
component main = Main();
|
||||
5
circom/node_modules/circomlib/test/circuits/greatereqthan.circom
generated
vendored
Normal file
5
circom/node_modules/circomlib/test/circuits/greatereqthan.circom
generated
vendored
Normal file
@@ -0,0 +1,5 @@
|
||||
pragma circom 2.0.0;
|
||||
|
||||
include "../../circuits/comparators.circom";
|
||||
|
||||
component main = GreaterEqThan(32);
|
||||
5
circom/node_modules/circomlib/test/circuits/greaterthan.circom
generated
vendored
Normal file
5
circom/node_modules/circomlib/test/circuits/greaterthan.circom
generated
vendored
Normal file
@@ -0,0 +1,5 @@
|
||||
pragma circom 2.0.0;
|
||||
|
||||
include "../../circuits/comparators.circom";
|
||||
|
||||
component main = GreaterThan(32);
|
||||
5
circom/node_modules/circomlib/test/circuits/isequal.circom
generated
vendored
Normal file
5
circom/node_modules/circomlib/test/circuits/isequal.circom
generated
vendored
Normal file
@@ -0,0 +1,5 @@
|
||||
pragma circom 2.0.0;
|
||||
|
||||
include "../../circuits/comparators.circom";
|
||||
|
||||
component main = IsEqual();
|
||||
5
circom/node_modules/circomlib/test/circuits/iszero.circom
generated
vendored
Normal file
5
circom/node_modules/circomlib/test/circuits/iszero.circom
generated
vendored
Normal file
@@ -0,0 +1,5 @@
|
||||
pragma circom 2.0.0;
|
||||
|
||||
include "../../circuits/comparators.circom";
|
||||
|
||||
component main = IsZero();
|
||||
5
circom/node_modules/circomlib/test/circuits/lesseqthan.circom
generated
vendored
Normal file
5
circom/node_modules/circomlib/test/circuits/lesseqthan.circom
generated
vendored
Normal file
@@ -0,0 +1,5 @@
|
||||
pragma circom 2.0.0;
|
||||
|
||||
include "../../circuits/comparators.circom";
|
||||
|
||||
component main = LessEqThan(32);
|
||||
5
circom/node_modules/circomlib/test/circuits/lessthan.circom
generated
vendored
Normal file
5
circom/node_modules/circomlib/test/circuits/lessthan.circom
generated
vendored
Normal file
@@ -0,0 +1,5 @@
|
||||
pragma circom 2.0.0;
|
||||
|
||||
include "../../circuits/comparators.circom";
|
||||
|
||||
component main = LessThan(32);
|
||||
5
circom/node_modules/circomlib/test/circuits/mimc_sponge_hash_test.circom
generated
vendored
Normal file
5
circom/node_modules/circomlib/test/circuits/mimc_sponge_hash_test.circom
generated
vendored
Normal file
@@ -0,0 +1,5 @@
|
||||
pragma circom 2.0.0;
|
||||
|
||||
include "../../circuits/mimcsponge.circom";
|
||||
|
||||
component main = MiMCSponge(2, 220, 3);
|
||||
5
circom/node_modules/circomlib/test/circuits/mimc_sponge_test.circom
generated
vendored
Normal file
5
circom/node_modules/circomlib/test/circuits/mimc_sponge_test.circom
generated
vendored
Normal file
@@ -0,0 +1,5 @@
|
||||
pragma circom 2.0.0;
|
||||
|
||||
include "../../circuits/mimcsponge.circom";
|
||||
|
||||
component main = MiMCFeistel(220);
|
||||
5
circom/node_modules/circomlib/test/circuits/mimc_test.circom
generated
vendored
Normal file
5
circom/node_modules/circomlib/test/circuits/mimc_test.circom
generated
vendored
Normal file
@@ -0,0 +1,5 @@
|
||||
pragma circom 2.0.0;
|
||||
|
||||
include "../../circuits/mimc.circom";
|
||||
|
||||
component main = MiMC7(91);
|
||||
5
circom/node_modules/circomlib/test/circuits/montgomery2edwards.circom
generated
vendored
Normal file
5
circom/node_modules/circomlib/test/circuits/montgomery2edwards.circom
generated
vendored
Normal file
@@ -0,0 +1,5 @@
|
||||
pragma circom 2.0.0;
|
||||
|
||||
include "../../circuits/montgomery.circom";
|
||||
|
||||
component main = Montgomery2Edwards();
|
||||
5
circom/node_modules/circomlib/test/circuits/montgomeryadd.circom
generated
vendored
Normal file
5
circom/node_modules/circomlib/test/circuits/montgomeryadd.circom
generated
vendored
Normal file
@@ -0,0 +1,5 @@
|
||||
pragma circom 2.0.0;
|
||||
|
||||
include "../../circuits/montgomery.circom";
|
||||
|
||||
component main = MontgomeryAdd();
|
||||
5
circom/node_modules/circomlib/test/circuits/montgomerydouble.circom
generated
vendored
Normal file
5
circom/node_modules/circomlib/test/circuits/montgomerydouble.circom
generated
vendored
Normal file
@@ -0,0 +1,5 @@
|
||||
pragma circom 2.0.0;
|
||||
|
||||
include "../../circuits/montgomery.circom";
|
||||
|
||||
component main = MontgomeryDouble();
|
||||
33
circom/node_modules/circomlib/test/circuits/mux1_1.circom
generated
vendored
Normal file
33
circom/node_modules/circomlib/test/circuits/mux1_1.circom
generated
vendored
Normal file
@@ -0,0 +1,33 @@
|
||||
pragma circom 2.0.0;
|
||||
|
||||
include "../../circuits/mux1.circom";
|
||||
include "../../circuits/bitify.circom";
|
||||
|
||||
|
||||
template Constants() {
|
||||
var i;
|
||||
signal output out[2];
|
||||
|
||||
out[0] <== 37;
|
||||
out[1] <== 47;
|
||||
}
|
||||
|
||||
template Main() {
|
||||
var i;
|
||||
signal input selector;//private
|
||||
signal output out;
|
||||
|
||||
component mux = Mux1();
|
||||
component n2b = Num2Bits(1);
|
||||
component cst = Constants();
|
||||
|
||||
selector ==> n2b.in;
|
||||
n2b.out[0] ==> mux.s;
|
||||
for (i=0; i<2; i++) {
|
||||
cst.out[i] ==> mux.c[i];
|
||||
}
|
||||
|
||||
mux.out ==> out;
|
||||
}
|
||||
|
||||
component main = Main();
|
||||
37
circom/node_modules/circomlib/test/circuits/mux2_1.circom
generated
vendored
Normal file
37
circom/node_modules/circomlib/test/circuits/mux2_1.circom
generated
vendored
Normal file
@@ -0,0 +1,37 @@
|
||||
pragma circom 2.0.0;
|
||||
|
||||
include "../../circuits/mux2.circom";
|
||||
include "../../circuits/bitify.circom";
|
||||
|
||||
|
||||
template Constants() {
|
||||
var i;
|
||||
signal output out[4];
|
||||
|
||||
out[0] <== 37;
|
||||
out[1] <== 47;
|
||||
out[2] <== 53;
|
||||
out[3] <== 71;
|
||||
}
|
||||
|
||||
template Main() {
|
||||
var i;
|
||||
signal input selector;//private
|
||||
signal output out;
|
||||
|
||||
component mux = Mux2();
|
||||
component n2b = Num2Bits(2);
|
||||
component cst = Constants();
|
||||
|
||||
selector ==> n2b.in;
|
||||
for (i=0; i<2; i++) {
|
||||
n2b.out[i] ==> mux.s[i];
|
||||
}
|
||||
for (i=0; i<4; i++) {
|
||||
cst.out[i] ==> mux.c[i];
|
||||
}
|
||||
|
||||
mux.out ==> out;
|
||||
}
|
||||
|
||||
component main = Main();
|
||||
41
circom/node_modules/circomlib/test/circuits/mux3_1.circom
generated
vendored
Normal file
41
circom/node_modules/circomlib/test/circuits/mux3_1.circom
generated
vendored
Normal file
@@ -0,0 +1,41 @@
|
||||
pragma circom 2.0.0;
|
||||
|
||||
include "../../circuits/mux3.circom";
|
||||
include "../../circuits/bitify.circom";
|
||||
|
||||
|
||||
template Constants() {
|
||||
var i;
|
||||
signal output out[8];
|
||||
|
||||
out[0] <== 37;
|
||||
out[1] <== 47;
|
||||
out[2] <== 53;
|
||||
out[3] <== 71;
|
||||
out[4] <== 89;
|
||||
out[5] <== 107;
|
||||
out[6] <== 163;
|
||||
out[7] <== 191;
|
||||
}
|
||||
|
||||
template Main() {
|
||||
var i;
|
||||
signal input selector;//private
|
||||
signal output out;
|
||||
|
||||
component mux = Mux3();
|
||||
component n2b = Num2Bits(3);
|
||||
component cst = Constants();
|
||||
|
||||
selector ==> n2b.in;
|
||||
for (i=0; i<3; i++) {
|
||||
n2b.out[i] ==> mux.s[i];
|
||||
}
|
||||
for (i=0; i<8; i++) {
|
||||
cst.out[i] ==> mux.c[i];
|
||||
}
|
||||
|
||||
mux.out ==> out;
|
||||
}
|
||||
|
||||
component main = Main();
|
||||
56
circom/node_modules/circomlib/test/circuits/mux4_1.circom
generated
vendored
Normal file
56
circom/node_modules/circomlib/test/circuits/mux4_1.circom
generated
vendored
Normal file
@@ -0,0 +1,56 @@
|
||||
pragma circom 2.0.0;
|
||||
|
||||
include "../../circuits/mux4.circom";
|
||||
include "../../circuits/bitify.circom";
|
||||
|
||||
|
||||
template Constants() {
|
||||
var i;
|
||||
signal output out[16];
|
||||
|
||||
out[0] <== 123;
|
||||
out[1] <== 456;
|
||||
out[2] <== 789;
|
||||
out[3] <== 012;
|
||||
out[4] <== 111;
|
||||
out[5] <== 222;
|
||||
out[6] <== 333;
|
||||
out[7] <== 4546;
|
||||
out[8] <== 134523;
|
||||
out[9] <== 44356;
|
||||
out[10] <== 15623;
|
||||
out[11] <== 4566;
|
||||
out[12] <== 1223;
|
||||
out[13] <== 4546;
|
||||
out[14] <== 4256;
|
||||
out[15] <== 4456;
|
||||
|
||||
/*
|
||||
for (i=0;i<16; i++) {
|
||||
out[i] <== i*2+100;
|
||||
}
|
||||
*/
|
||||
|
||||
}
|
||||
|
||||
template Main() {
|
||||
var i;
|
||||
signal input selector;//private
|
||||
signal output out;
|
||||
|
||||
component mux = Mux4();
|
||||
component n2b = Num2Bits(4);
|
||||
component cst = Constants();
|
||||
|
||||
selector ==> n2b.in;
|
||||
for (i=0; i<4; i++) {
|
||||
n2b.out[i] ==> mux.s[i];
|
||||
}
|
||||
for (i=0; i<16; i++) {
|
||||
cst.out[i] ==> mux.c[i];
|
||||
}
|
||||
|
||||
mux.out ==> out;
|
||||
}
|
||||
|
||||
component main = Main();
|
||||
34
circom/node_modules/circomlib/test/circuits/pedersen2_test.circom
generated
vendored
Normal file
34
circom/node_modules/circomlib/test/circuits/pedersen2_test.circom
generated
vendored
Normal file
@@ -0,0 +1,34 @@
|
||||
pragma circom 2.0.0;
|
||||
|
||||
include "../../circuits/pedersen.circom";
|
||||
include "../../circuits/bitify.circom";
|
||||
|
||||
|
||||
template Main() {
|
||||
signal input in;
|
||||
signal output out[2];
|
||||
|
||||
component pedersen = Pedersen(256);
|
||||
|
||||
component n2b;
|
||||
n2b = Num2Bits(253);
|
||||
|
||||
var i;
|
||||
|
||||
in ==> n2b.in;
|
||||
|
||||
for (i=0; i<253; i++) {
|
||||
pedersen.in[i] <== n2b.out[i];
|
||||
}
|
||||
|
||||
for (i=253; i<256; i++) {
|
||||
pedersen.in[i] <== 0;
|
||||
}
|
||||
|
||||
pedersen.out[0] ==> out[0];
|
||||
pedersen.out[1] ==> out[1];
|
||||
}
|
||||
|
||||
component main = Main();
|
||||
|
||||
|
||||
31
circom/node_modules/circomlib/test/circuits/pedersen_test.circom
generated
vendored
Normal file
31
circom/node_modules/circomlib/test/circuits/pedersen_test.circom
generated
vendored
Normal file
@@ -0,0 +1,31 @@
|
||||
pragma circom 2.0.0;
|
||||
|
||||
include "../../circuits/pedersen_old.circom";
|
||||
include "../../circuits/bitify.circom";
|
||||
|
||||
|
||||
template Main() {
|
||||
signal input in[2];
|
||||
signal output out[2];
|
||||
|
||||
component pedersen = Pedersen(250*2);
|
||||
|
||||
component n2b[2];
|
||||
n2b[0] = Num2Bits(250);
|
||||
n2b[1] = Num2Bits(250);
|
||||
|
||||
var i;
|
||||
|
||||
in[0] ==> n2b[0].in;
|
||||
in[1] ==> n2b[1].in;
|
||||
|
||||
for (i=0; i<250; i++) {
|
||||
n2b[0].out[i] ==> pedersen.in[i];
|
||||
n2b[1].out[i] ==> pedersen.in[250+i];
|
||||
}
|
||||
|
||||
pedersen.out[0] ==> out[0];
|
||||
pedersen.out[1] ==> out[1];
|
||||
}
|
||||
|
||||
component main = Main();
|
||||
25
circom/node_modules/circomlib/test/circuits/pointbits_loopback.circom
generated
vendored
Normal file
25
circom/node_modules/circomlib/test/circuits/pointbits_loopback.circom
generated
vendored
Normal file
@@ -0,0 +1,25 @@
|
||||
pragma circom 2.0.0;
|
||||
|
||||
include "../../circuits/pointbits.circom";
|
||||
|
||||
|
||||
template Main() {
|
||||
signal input in[2];
|
||||
|
||||
var i;
|
||||
|
||||
component p2b = Point2Bits_Strict();
|
||||
component b2p = Bits2Point_Strict();
|
||||
|
||||
p2b.in[0] <== in[0];
|
||||
p2b.in[1] <== in[1];
|
||||
|
||||
for (i=0; i<256; i++) {
|
||||
b2p.in[i] <== p2b.out[i];
|
||||
}
|
||||
|
||||
b2p.out[0] === in[0];
|
||||
b2p.out[1] === in[1];
|
||||
}
|
||||
|
||||
component main = Main();
|
||||
5
circom/node_modules/circomlib/test/circuits/poseidon3_test.circom
generated
vendored
Normal file
5
circom/node_modules/circomlib/test/circuits/poseidon3_test.circom
generated
vendored
Normal file
@@ -0,0 +1,5 @@
|
||||
pragma circom 2.0.0;
|
||||
|
||||
include "../../circuits/poseidon.circom";
|
||||
|
||||
component main = Poseidon(2);
|
||||
5
circom/node_modules/circomlib/test/circuits/poseidon6_test.circom
generated
vendored
Normal file
5
circom/node_modules/circomlib/test/circuits/poseidon6_test.circom
generated
vendored
Normal file
@@ -0,0 +1,5 @@
|
||||
pragma circom 2.0.0;
|
||||
|
||||
include "../../circuits/poseidon.circom";
|
||||
|
||||
component main = Poseidon(5);
|
||||
5
circom/node_modules/circomlib/test/circuits/poseidonex_test.circom
generated
vendored
Normal file
5
circom/node_modules/circomlib/test/circuits/poseidonex_test.circom
generated
vendored
Normal file
@@ -0,0 +1,5 @@
|
||||
pragma circom 2.0.0;
|
||||
|
||||
include "../../circuits/poseidon.circom";
|
||||
|
||||
component main = PoseidonEx(16, 17);
|
||||
17
circom/node_modules/circomlib/test/circuits/sha256_2_test.circom
generated
vendored
Normal file
17
circom/node_modules/circomlib/test/circuits/sha256_2_test.circom
generated
vendored
Normal file
@@ -0,0 +1,17 @@
|
||||
pragma circom 2.0.0;
|
||||
|
||||
include "../../circuits/sha256/sha256_2.circom";
|
||||
|
||||
template Main() {
|
||||
signal input a; //private
|
||||
signal input b; //private
|
||||
signal output out;
|
||||
|
||||
component sha256_2 = Sha256_2();
|
||||
|
||||
sha256_2.a <== a;
|
||||
sha256_2.b <== b;
|
||||
out <== sha256_2.out;
|
||||
}
|
||||
|
||||
component main = Main();
|
||||
5
circom/node_modules/circomlib/test/circuits/sha256_test448.circom
generated
vendored
Normal file
5
circom/node_modules/circomlib/test/circuits/sha256_test448.circom
generated
vendored
Normal file
@@ -0,0 +1,5 @@
|
||||
pragma circom 2.0.0;
|
||||
|
||||
include "../../circuits/sha256/sha256.circom";
|
||||
|
||||
component main = Sha256(448);
|
||||
5
circom/node_modules/circomlib/test/circuits/sha256_test512.circom
generated
vendored
Normal file
5
circom/node_modules/circomlib/test/circuits/sha256_test512.circom
generated
vendored
Normal file
@@ -0,0 +1,5 @@
|
||||
pragma circom 2.0.0;
|
||||
|
||||
include "../../circuits/sha256/sha256.circom";
|
||||
|
||||
component main = Sha256(512);
|
||||
5
circom/node_modules/circomlib/test/circuits/sign_test.circom
generated
vendored
Normal file
5
circom/node_modules/circomlib/test/circuits/sign_test.circom
generated
vendored
Normal file
@@ -0,0 +1,5 @@
|
||||
pragma circom 2.0.0;
|
||||
|
||||
include "../../circuits/sign.circom";
|
||||
|
||||
component main = Sign();
|
||||
5
circom/node_modules/circomlib/test/circuits/smtprocessor10_test.circom
generated
vendored
Normal file
5
circom/node_modules/circomlib/test/circuits/smtprocessor10_test.circom
generated
vendored
Normal file
@@ -0,0 +1,5 @@
|
||||
pragma circom 2.0.0;
|
||||
|
||||
include "../../circuits/smt/smtprocessor.circom";
|
||||
|
||||
component main = SMTProcessor(10);
|
||||
5
circom/node_modules/circomlib/test/circuits/smtverifier10_test.circom
generated
vendored
Normal file
5
circom/node_modules/circomlib/test/circuits/smtverifier10_test.circom
generated
vendored
Normal file
@@ -0,0 +1,5 @@
|
||||
pragma circom 2.0.0;
|
||||
|
||||
include "../../circuits/smt/smtverifier.circom";
|
||||
|
||||
component main = SMTVerifier(10);
|
||||
33
circom/node_modules/circomlib/test/circuits/sum_test.circom
generated
vendored
Normal file
33
circom/node_modules/circomlib/test/circuits/sum_test.circom
generated
vendored
Normal file
@@ -0,0 +1,33 @@
|
||||
pragma circom 2.0.0;
|
||||
|
||||
include "../../circuits/bitify.circom";
|
||||
include "../../circuits/binsum.circom";
|
||||
|
||||
template A() {
|
||||
signal input a; //private
|
||||
signal input b;
|
||||
signal output out;
|
||||
|
||||
var i;
|
||||
|
||||
component n2ba = Num2Bits(32);
|
||||
component n2bb = Num2Bits(32);
|
||||
component sum = BinSum(32,2);
|
||||
component b2n = Bits2Num(32);
|
||||
|
||||
n2ba.in <== a;
|
||||
n2bb.in <== b;
|
||||
|
||||
for (i=0; i<32; i++) {
|
||||
sum.in[0][i] <== n2ba.out[i];
|
||||
sum.in[1][i] <== n2bb.out[i];
|
||||
}
|
||||
|
||||
for (i=0; i<32; i++) {
|
||||
b2n.in[i] <== sum.out[i];
|
||||
}
|
||||
|
||||
out <== b2n.out;
|
||||
}
|
||||
|
||||
component main = A();
|
||||
187
circom/node_modules/circomlib/test/comparators.js
generated
vendored
Normal file
187
circom/node_modules/circomlib/test/comparators.js
generated
vendored
Normal file
@@ -0,0 +1,187 @@
|
||||
const chai = require("chai");
|
||||
const path = require("path");
|
||||
const F1Field = require("ffjavascript").F1Field;
|
||||
const Scalar = require("ffjavascript").Scalar;
|
||||
exports.p = Scalar.fromString("21888242871839275222246405745257275088548364400416034343698204186575808495617");
|
||||
const Fr = new F1Field(exports.p);
|
||||
|
||||
const wasm_tester = require("circom_tester").wasm;
|
||||
|
||||
const assert = chai.assert;
|
||||
|
||||
describe("Comparators test", function () {
|
||||
|
||||
this.timeout(100000);
|
||||
|
||||
it("Should create a iszero circuit", async() => {
|
||||
const circuit = await wasm_tester(path.join(__dirname, "circuits", "iszero.circom"));
|
||||
|
||||
let witness;
|
||||
witness = await circuit.calculateWitness({ "in": 111}, true);
|
||||
assert(Fr.eq(Fr.e(witness[0]), Fr.e(1)));
|
||||
assert(Fr.eq(Fr.e(witness[1]), Fr.e(0)));
|
||||
|
||||
witness = await circuit.calculateWitness({ "in": 0 }, true);
|
||||
assert(Fr.eq(Fr.e(witness[0]), Fr.e(1)));
|
||||
assert(Fr.eq(Fr.e(witness[1]), Fr.e(1)));
|
||||
});
|
||||
it("Should create a isequal circuit", async() => {
|
||||
const circuit = await wasm_tester(path.join(__dirname, "circuits", "isequal.circom"));
|
||||
|
||||
let witness;
|
||||
witness = await circuit.calculateWitness({ "in": [111,222] }, true);
|
||||
assert(Fr.eq(Fr.e(witness[0]), Fr.e(1)));
|
||||
assert(Fr.eq(Fr.e(witness[1]), Fr.e(0)));
|
||||
|
||||
|
||||
witness = await circuit.calculateWitness({ "in": [444,444] }, true);
|
||||
assert(Fr.eq(Fr.e(witness[0]), Fr.e(1)));
|
||||
assert(Fr.eq(Fr.e(witness[1]), Fr.e(1)));
|
||||
});
|
||||
it("Should create a comparison lessthan", async() => {
|
||||
const circuit = await wasm_tester(path.join(__dirname, "circuits", "lessthan.circom"));
|
||||
|
||||
let witness;
|
||||
witness = await circuit.calculateWitness({ "in": [333,444] }), true;
|
||||
assert(Fr.eq(Fr.e(witness[0]), Fr.e(1)));
|
||||
assert(Fr.eq(Fr.e(witness[1]), Fr.e(1)));
|
||||
|
||||
witness = await circuit.calculateWitness({ "in":[1,1] }, true);
|
||||
assert(Fr.eq(Fr.e(witness[0]), Fr.e(1)));
|
||||
assert(Fr.eq(Fr.e(witness[1]), Fr.e(0)));
|
||||
|
||||
witness = await circuit.calculateWitness({ "in": [661, 660] }, true);
|
||||
assert(Fr.eq(Fr.e(witness[0]), Fr.e(1)));
|
||||
assert(Fr.eq(Fr.e(witness[1]), Fr.e(0)));
|
||||
|
||||
witness = await circuit.calculateWitness({ "in": [0, 1] }, true);
|
||||
assert(Fr.eq(Fr.e(witness[0]), Fr.e(1)));
|
||||
assert(Fr.eq(Fr.e(witness[1]), Fr.e(1)));
|
||||
|
||||
witness = await circuit.calculateWitness({ "in": [0, 444] }, true);
|
||||
assert(Fr.eq(Fr.e(witness[0]), Fr.e(1)));
|
||||
assert(Fr.eq(Fr.e(witness[1]), Fr.e(1)));
|
||||
|
||||
witness = await circuit.calculateWitness({ "in": [1, 0] }, true);
|
||||
assert(Fr.eq(Fr.e(witness[0]), Fr.e(1)));
|
||||
assert(Fr.eq(Fr.e(witness[1]), Fr.e(0)));
|
||||
|
||||
witness = await circuit.calculateWitness({ "in": [555, 0] }, true);
|
||||
assert(Fr.eq(Fr.e(witness[0]), Fr.e(1)));
|
||||
assert(Fr.eq(Fr.e(witness[1]), Fr.e(0)));
|
||||
|
||||
witness = await circuit.calculateWitness({ "in": [0, 0] }, true);
|
||||
assert(Fr.eq(Fr.e(witness[0]), Fr.e(1)));
|
||||
assert(Fr.eq(Fr.e(witness[1]), Fr.e(0)));
|
||||
});
|
||||
it("Should create a comparison lesseqthan", async() => {
|
||||
|
||||
const circuit = await wasm_tester(path.join(__dirname, "circuits", "lesseqthan.circom"));
|
||||
|
||||
let witness;
|
||||
witness = await circuit.calculateWitness({ "in": [333,444] }, true);
|
||||
assert(Fr.eq(Fr.e(witness[0]), Fr.e(1)));
|
||||
assert(Fr.eq(Fr.e(witness[1]), Fr.e(1)));
|
||||
|
||||
witness = await circuit.calculateWitness({ "in":[1,1] }, true);
|
||||
assert(Fr.eq(Fr.e(witness[0]), Fr.e(1)));
|
||||
assert(Fr.eq(Fr.e(witness[1]), Fr.e(1)));
|
||||
|
||||
witness = await circuit.calculateWitness({ "in": [661, 660] }, true);
|
||||
assert(Fr.eq(Fr.e(witness[0]), Fr.e(1)));
|
||||
assert(Fr.eq(Fr.e(witness[1]), Fr.e(0)));
|
||||
|
||||
witness = await circuit.calculateWitness({ "in": [0, 1] }, true);
|
||||
assert(Fr.eq(Fr.e(witness[0]), Fr.e(1)));
|
||||
assert(Fr.eq(Fr.e(witness[1]), Fr.e(1)));
|
||||
|
||||
witness = await circuit.calculateWitness({ "in": [0, 444] }, true);
|
||||
assert(Fr.eq(Fr.e(witness[0]), Fr.e(1)));
|
||||
assert(Fr.eq(Fr.e(witness[1]), Fr.e(1)));
|
||||
|
||||
witness = await circuit.calculateWitness({ "in": [1, 0] }, true);
|
||||
assert(Fr.eq(Fr.e(witness[0]), Fr.e(1)));
|
||||
assert(Fr.eq(Fr.e(witness[1]), Fr.e(0)));
|
||||
|
||||
witness = await circuit.calculateWitness({ "in": [555, 0] }, true);
|
||||
assert(Fr.eq(Fr.e(witness[0]), Fr.e(1)));
|
||||
assert(Fr.eq(Fr.e(witness[1]), Fr.e(0)));
|
||||
|
||||
witness = await circuit.calculateWitness({ "in": [0, 0] }, true);
|
||||
assert(Fr.eq(Fr.e(witness[0]), Fr.e(1)));
|
||||
assert(Fr.eq(Fr.e(witness[1]), Fr.e(1)));
|
||||
});
|
||||
it("Should create a comparison greaterthan", async() => {
|
||||
|
||||
const circuit = await wasm_tester(path.join(__dirname, "circuits", "greaterthan.circom"));
|
||||
|
||||
let witness;
|
||||
witness = await circuit.calculateWitness({ "in": [333,444] }, true);
|
||||
assert(Fr.eq(Fr.e(witness[0]), Fr.e(1)));
|
||||
assert(Fr.eq(Fr.e(witness[1]), Fr.e(0)));
|
||||
|
||||
witness = await circuit.calculateWitness({ "in":[1,1] }, true);
|
||||
assert(Fr.eq(Fr.e(witness[0]), Fr.e(1)));
|
||||
assert(Fr.eq(Fr.e(witness[1]), Fr.e(0)));
|
||||
|
||||
witness = await circuit.calculateWitness({ "in": [661, 660] }, true);
|
||||
assert(Fr.eq(Fr.e(witness[0]), Fr.e(1)));
|
||||
assert(Fr.eq(Fr.e(witness[1]), Fr.e(1)));
|
||||
|
||||
witness = await circuit.calculateWitness({ "in": [0, 1] }, true);
|
||||
assert(Fr.eq(Fr.e(witness[0]), Fr.e(1)));
|
||||
assert(Fr.eq(Fr.e(witness[1]), Fr.e(0)));
|
||||
|
||||
witness = await circuit.calculateWitness({ "in": [0, 444] }, true);
|
||||
assert(Fr.eq(Fr.e(witness[0]), Fr.e(1)));
|
||||
assert(Fr.eq(Fr.e(witness[1]), Fr.e(0)));
|
||||
|
||||
witness = await circuit.calculateWitness({ "in": [1, 0] }, true);
|
||||
assert(Fr.eq(Fr.e(witness[0]), Fr.e(1)));
|
||||
assert(Fr.eq(Fr.e(witness[1]), Fr.e(1)));
|
||||
|
||||
witness = await circuit.calculateWitness({ "in": [555, 0] }, true);
|
||||
assert(Fr.eq(Fr.e(witness[0]), Fr.e(1)));
|
||||
assert(Fr.eq(Fr.e(witness[1]), Fr.e(1)));
|
||||
|
||||
witness = await circuit.calculateWitness({ "in": [0, 0] }, true);
|
||||
assert(Fr.eq(Fr.e(witness[0]), Fr.e(1)));
|
||||
assert(Fr.eq(Fr.e(witness[1]), Fr.e(0)));
|
||||
});
|
||||
it("Should create a comparison greatereqthan", async() => {
|
||||
const circuit = await wasm_tester(path.join(__dirname, "circuits", "greatereqthan.circom"));
|
||||
|
||||
let witness;
|
||||
witness = await circuit.calculateWitness({ "in": [333,444] }, true);
|
||||
assert(Fr.eq(Fr.e(witness[0]), Fr.e(1)));
|
||||
assert(Fr.eq(Fr.e(witness[1]), Fr.e(0)));
|
||||
|
||||
witness = await circuit.calculateWitness({ "in":[1,1] }, true);
|
||||
assert(Fr.eq(Fr.e(witness[0]), Fr.e(1)));
|
||||
assert(Fr.eq(Fr.e(witness[1]), Fr.e(1)));
|
||||
|
||||
witness = await circuit.calculateWitness({ "in": [661, 660] }, true);
|
||||
assert(Fr.eq(Fr.e(witness[0]), Fr.e(1)));
|
||||
assert(Fr.eq(Fr.e(witness[1]), Fr.e(1)));
|
||||
|
||||
witness = await circuit.calculateWitness({ "in": [0, 1] }, true);
|
||||
assert(Fr.eq(Fr.e(witness[0]), Fr.e(1)));
|
||||
assert(Fr.eq(Fr.e(witness[1]), Fr.e(0)));
|
||||
|
||||
witness = await circuit.calculateWitness({ "in": [0, 444] }, true);
|
||||
assert(Fr.eq(Fr.e(witness[0]), Fr.e(1)));
|
||||
assert(Fr.eq(Fr.e(witness[1]), Fr.e(0)));
|
||||
|
||||
witness = await circuit.calculateWitness({ "in": [1, 0] }, true);
|
||||
assert(Fr.eq(Fr.e(witness[0]), Fr.e(1)));
|
||||
assert(Fr.eq(Fr.e(witness[1]), Fr.e(1)));
|
||||
|
||||
witness = await circuit.calculateWitness({ "in": [555, 0] }, true);
|
||||
assert(Fr.eq(Fr.e(witness[0]), Fr.e(1)));
|
||||
assert(Fr.eq(Fr.e(witness[1]), Fr.e(1)));
|
||||
|
||||
witness = await circuit.calculateWitness({ "in": [0, 0] }, true);
|
||||
assert(Fr.eq(Fr.e(witness[0]), Fr.e(1)));
|
||||
assert(Fr.eq(Fr.e(witness[1]), Fr.e(1)));
|
||||
});
|
||||
});
|
||||
75
circom/node_modules/circomlib/test/eddsa.js
generated
vendored
Normal file
75
circom/node_modules/circomlib/test/eddsa.js
generated
vendored
Normal file
@@ -0,0 +1,75 @@
|
||||
const chai = require("chai");
|
||||
const path = require("path");
|
||||
|
||||
const wasm_tester = require("circom_tester").wasm;
|
||||
|
||||
const buildEddsa = require("circomlibjs").buildEddsa;
|
||||
const buildBabyjub = require("circomlibjs").buildBabyjub;
|
||||
|
||||
const Scalar = require("ffjavascript").Scalar;
|
||||
|
||||
const assert = chai.assert;
|
||||
|
||||
function print(circuit, w, s) {
|
||||
console.log(s + ": " + w[circuit.getSignalIdx(s)]);
|
||||
}
|
||||
|
||||
function buffer2bits(buff) {
|
||||
const res = [];
|
||||
for (let i=0; i<buff.length; i++) {
|
||||
for (let j=0; j<8; j++) {
|
||||
if ((buff[i]>>j)&1) {
|
||||
res.push(1n);
|
||||
} else {
|
||||
res.push(0n);
|
||||
}
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
describe("EdDSA test", function () {
|
||||
let circuit;
|
||||
let eddsa;
|
||||
let babyJub;
|
||||
let F;
|
||||
|
||||
this.timeout(100000);
|
||||
|
||||
before( async () => {
|
||||
eddsa = await buildEddsa();
|
||||
babyJub = await buildBabyjub();
|
||||
F = babyJub.F;
|
||||
circuit = await wasm_tester(path.join(__dirname, "circuits", "eddsa_test.circom"));
|
||||
});
|
||||
|
||||
|
||||
it("Sign a single 10 bytes from 0 to 9", async () => {
|
||||
const msg = Buffer.from("00010203040506070809", "hex");
|
||||
|
||||
// const prvKey = crypto.randomBytes(32);
|
||||
|
||||
const prvKey = Buffer.from("0001020304050607080900010203040506070809000102030405060708090001", "hex");
|
||||
|
||||
const pubKey = eddsa.prv2pub(prvKey);
|
||||
|
||||
const pPubKey = babyJub.packPoint(pubKey);
|
||||
|
||||
const signature = eddsa.signPedersen(prvKey, msg);
|
||||
|
||||
const pSignature = eddsa.packSignature(signature);
|
||||
const uSignature = eddsa.unpackSignature(pSignature);
|
||||
|
||||
assert(eddsa.verifyPedersen(msg, uSignature, pubKey));
|
||||
|
||||
const msgBits = buffer2bits( msg);
|
||||
const r8Bits = buffer2bits( pSignature.slice(0, 32));
|
||||
const sBits = buffer2bits( pSignature.slice(32, 64));
|
||||
const aBits = buffer2bits( pPubKey);
|
||||
|
||||
const w = await circuit.calculateWitness({A: aBits, R8: r8Bits, S: sBits, msg: msgBits}, true);
|
||||
|
||||
await circuit.checkConstraints(w);
|
||||
});
|
||||
});
|
||||
102
circom/node_modules/circomlib/test/eddsamimc.js
generated
vendored
Normal file
102
circom/node_modules/circomlib/test/eddsamimc.js
generated
vendored
Normal file
@@ -0,0 +1,102 @@
|
||||
const chai = require("chai");
|
||||
const path = require("path");
|
||||
const wasm_tester = require("circom_tester").wasm;
|
||||
|
||||
const buildEddsa = require("circomlibjs").buildEddsa;
|
||||
const buildBabyjub = require("circomlibjs").buildBabyjub;
|
||||
|
||||
const assert = chai.assert;
|
||||
|
||||
describe("EdDSA MiMC test", function () {
|
||||
let circuit;
|
||||
let eddsa;
|
||||
let babyJub;
|
||||
let F;
|
||||
|
||||
this.timeout(100000);
|
||||
|
||||
before( async () => {
|
||||
eddsa = await buildEddsa();
|
||||
babyJub = await buildBabyjub();
|
||||
F = babyJub.F;
|
||||
|
||||
circuit = await wasm_tester(path.join(__dirname, "circuits", "eddsamimc_test.circom"));
|
||||
});
|
||||
|
||||
it("Sign a single number", async () => {
|
||||
const msg = F.e(1234);
|
||||
|
||||
const prvKey = Buffer.from("0001020304050607080900010203040506070809000102030405060708090001", "hex");
|
||||
|
||||
const pubKey = eddsa.prv2pub(prvKey);
|
||||
|
||||
const signature = eddsa.signMiMC(prvKey, msg);
|
||||
|
||||
assert(eddsa.verifyMiMC(msg, signature, pubKey));
|
||||
|
||||
const w = await circuit.calculateWitness({
|
||||
enabled: 1,
|
||||
Ax: F.toObject(pubKey[0]),
|
||||
Ay: F.toObject(pubKey[1]),
|
||||
R8x: F.toObject(signature.R8[0]),
|
||||
R8y: F.toObject(signature.R8[1]),
|
||||
S: signature.S,
|
||||
M: F.toObject(msg)}, true);
|
||||
|
||||
|
||||
await circuit.checkConstraints(w);
|
||||
|
||||
});
|
||||
|
||||
it("Detect Invalid signature", async () => {
|
||||
const msg = F.e(1234);
|
||||
|
||||
const prvKey = Buffer.from("0001020304050607080900010203040506070809000102030405060708090001", "hex");
|
||||
|
||||
const pubKey = eddsa.prv2pub(prvKey);
|
||||
|
||||
|
||||
const signature = eddsa.signMiMC(prvKey, msg);
|
||||
|
||||
assert(eddsa.verifyMiMC(msg, signature, pubKey));
|
||||
try {
|
||||
const w = await circuit.calculateWitness({
|
||||
enabled: 1,
|
||||
Ax: F.toObject(pubKey[0]),
|
||||
Ay: F.toObject(pubKey[1]),
|
||||
R8x: F.toObject(F.add(signature.R8[0], F.e(1))),
|
||||
R8y: F.toObject(signature.R8[1]),
|
||||
S: signature.S,
|
||||
M: F.toObject(msg)}, true);
|
||||
assert(false);
|
||||
} catch(err) {
|
||||
assert(err.message.includes("Assert Failed"));
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
it("Test a dissabled circuit with a bad signature", async () => {
|
||||
const msg = F.e(1234);
|
||||
|
||||
const prvKey = Buffer.from("0001020304050607080900010203040506070809000102030405060708090001", "hex");
|
||||
|
||||
const pubKey = eddsa.prv2pub(prvKey);
|
||||
|
||||
|
||||
const signature = eddsa.signMiMC(prvKey, msg);
|
||||
|
||||
assert(eddsa.verifyMiMC(msg, signature, pubKey));
|
||||
|
||||
const w = await circuit.calculateWitness({
|
||||
enabled: 0,
|
||||
Ax: F.toObject(pubKey[0]),
|
||||
Ay: F.toObject(pubKey[1]),
|
||||
R8x: F.toObject(F.add(signature.R8[0], F.e(1))),
|
||||
R8y: F.toObject(signature.R8[1]),
|
||||
S: signature.S,
|
||||
M: F.toObject(msg)}, true);
|
||||
|
||||
await circuit.checkConstraints(w);
|
||||
|
||||
});
|
||||
});
|
||||
103
circom/node_modules/circomlib/test/eddsaposeidon.js
generated
vendored
Normal file
103
circom/node_modules/circomlib/test/eddsaposeidon.js
generated
vendored
Normal file
@@ -0,0 +1,103 @@
|
||||
const chai = require("chai");
|
||||
const path = require("path");
|
||||
const wasm_tester = require("circom_tester").wasm;
|
||||
|
||||
const buildEddsa = require("circomlibjs").buildEddsa;
|
||||
const buildBabyjub = require("circomlibjs").buildBabyjub;
|
||||
|
||||
const assert = chai.assert;
|
||||
|
||||
describe("EdDSA Poseidon test", function () {
|
||||
let circuit;
|
||||
let eddsa;
|
||||
let babyJub;
|
||||
let F;
|
||||
|
||||
this.timeout(100000);
|
||||
|
||||
before( async () => {
|
||||
eddsa = await buildEddsa();
|
||||
babyJub = await buildBabyjub();
|
||||
F = babyJub.F;
|
||||
circuit = await wasm_tester(path.join(__dirname, "circuits", "eddsaposeidon_test.circom"));
|
||||
});
|
||||
|
||||
it("Sign a single number", async () => {
|
||||
const msg = F.e(1234);
|
||||
|
||||
const prvKey = Buffer.from("0001020304050607080900010203040506070809000102030405060708090001", "hex");
|
||||
|
||||
const pubKey = eddsa.prv2pub(prvKey);
|
||||
|
||||
const signature = eddsa.signPoseidon(prvKey, msg);
|
||||
|
||||
assert(eddsa.verifyPoseidon(msg, signature, pubKey));
|
||||
|
||||
const input = {
|
||||
enabled: 1,
|
||||
Ax: F.toObject(pubKey[0]),
|
||||
Ay: F.toObject(pubKey[1]),
|
||||
R8x: F.toObject(signature.R8[0]),
|
||||
R8y: F.toObject(signature.R8[1]),
|
||||
S: signature.S,
|
||||
M: F.toObject(msg)
|
||||
};
|
||||
|
||||
// console.log(JSON.stringify(utils.stringifyBigInts(input)));
|
||||
|
||||
const w = await circuit.calculateWitness(input, true);
|
||||
|
||||
await circuit.checkConstraints(w);
|
||||
});
|
||||
|
||||
it("Detect Invalid signature", async () => {
|
||||
const msg = F.e(1234);
|
||||
|
||||
const prvKey = Buffer.from("0001020304050607080900010203040506070809000102030405060708090001", "hex");
|
||||
|
||||
const pubKey = eddsa.prv2pub(prvKey);
|
||||
|
||||
|
||||
const signature = eddsa.signPoseidon(prvKey, msg);
|
||||
|
||||
assert(eddsa.verifyPoseidon(msg, signature, pubKey));
|
||||
try {
|
||||
await circuit.calculateWitness({
|
||||
enabled: 1,
|
||||
Ax: F.toObject(pubKey[0]),
|
||||
Ay: F.toObject(pubKey[1]),
|
||||
R8x: F.toObject(F.add(signature.R8[0], F.e(1))),
|
||||
R8y: F.toObject(signature.R8[1]),
|
||||
S: signature.S,
|
||||
M: F.toObject(msg)}, true);
|
||||
assert(false);
|
||||
} catch(err) {
|
||||
assert(err.message.includes("Assert Failed"));
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
it("Test a dissabled circuit with a bad signature", async () => {
|
||||
const msg = F.e(1234);
|
||||
|
||||
const prvKey = Buffer.from("0001020304050607080900010203040506070809000102030405060708090001", "hex");
|
||||
|
||||
const pubKey = eddsa.prv2pub(prvKey);
|
||||
|
||||
|
||||
const signature = eddsa.signPoseidon(prvKey, msg);
|
||||
|
||||
assert(eddsa.verifyPoseidon(msg, signature, pubKey));
|
||||
|
||||
const w = await circuit.calculateWitness({
|
||||
enabled: 0,
|
||||
Ax: F.toObject(pubKey[0]),
|
||||
Ay: F.toObject(pubKey[1]),
|
||||
R8x: F.toObject(F.add(signature.R8[0], F.e(1))),
|
||||
R8y: F.toObject(signature.R8[1]),
|
||||
S: signature.S,
|
||||
M: F.toObject(msg)}, true);
|
||||
|
||||
await circuit.checkConstraints(w);
|
||||
});
|
||||
});
|
||||
121
circom/node_modules/circomlib/test/escalarmul.js
generated
vendored
Normal file
121
circom/node_modules/circomlib/test/escalarmul.js
generated
vendored
Normal file
@@ -0,0 +1,121 @@
|
||||
const chai = require("chai");
|
||||
const path = require("path");
|
||||
const wasm_tester = require("circom_tester").wasm;
|
||||
const buildBabyjub = require("circomlibjs").buildBabyjub;
|
||||
|
||||
const Scalar = require("ffjavascript").Scalar;
|
||||
|
||||
const assert = chai.assert;
|
||||
|
||||
function print(circuit, w, s) {
|
||||
console.log(s + ": " + w[circuit.getSignalIdx(s)]);
|
||||
}
|
||||
|
||||
describe("Exponentioation test", function () {
|
||||
let babyJub;
|
||||
let Fr;
|
||||
this.timeout(100000);
|
||||
|
||||
before( async () => {
|
||||
babyJub = await buildBabyjub();
|
||||
Fr = babyJub.F;
|
||||
});
|
||||
|
||||
it("Should generate the Exponentiation table in k=0", async () => {
|
||||
|
||||
const circuit = await wasm_tester(path.join(__dirname, "circuits", "escalarmulw4table_test.circom"));
|
||||
|
||||
const w = await circuit.calculateWitness({in: 1});
|
||||
|
||||
await circuit.checkConstraints(w);
|
||||
|
||||
let g = [
|
||||
Fr.e("5299619240641551281634865583518297030282874472190772894086521144482721001553"),
|
||||
Fr.e("16950150798460657717958625567821834550301663161624707787222815936182638968203")
|
||||
];
|
||||
|
||||
let dbl= [Fr.e("0"), Fr.e("1")];
|
||||
|
||||
const expectedOut = [];
|
||||
|
||||
for (let i=0; i<16; i++) {
|
||||
|
||||
expectedOut.push([Fr.toObject(dbl[0]), Fr.toObject(dbl[1])]);
|
||||
dbl = babyJub.addPoint(dbl,g);
|
||||
}
|
||||
|
||||
await circuit.assertOut(w, {out: expectedOut});
|
||||
|
||||
});
|
||||
|
||||
it("Should generate the Exponentiation table in k=3", async () => {
|
||||
|
||||
const circuit = await wasm_tester(path.join(__dirname, "circuits", "escalarmulw4table_test3.circom"));
|
||||
|
||||
const w = await circuit.calculateWitness({in: 1});
|
||||
|
||||
await circuit.checkConstraints(w);
|
||||
|
||||
let g = [
|
||||
Fr.e("5299619240641551281634865583518297030282874472190772894086521144482721001553"),
|
||||
Fr.e("16950150798460657717958625567821834550301663161624707787222815936182638968203")
|
||||
];
|
||||
|
||||
for (let i=0; i<12;i++) {
|
||||
g = babyJub.addPoint(g,g);
|
||||
}
|
||||
|
||||
let dbl= [Fr.e("0"), Fr.e("1")];
|
||||
|
||||
const expectedOut = [];
|
||||
|
||||
for (let i=0; i<16; i++) {
|
||||
expectedOut.push([Fr.toObject(dbl[0]), Fr.toObject(dbl[1])]);
|
||||
|
||||
dbl = babyJub.addPoint(dbl,g);
|
||||
}
|
||||
|
||||
await circuit.assertOut(w, {out: expectedOut});
|
||||
|
||||
});
|
||||
|
||||
it("Should exponentiate g^31", async () => {
|
||||
|
||||
const circuit = await wasm_tester(path.join(__dirname, "circuits", "escalarmul_test.circom"));
|
||||
|
||||
const w = await circuit.calculateWitness({"in": 31});
|
||||
|
||||
await circuit.checkConstraints(w);
|
||||
|
||||
let g = [
|
||||
Fr.e("5299619240641551281634865583518297030282874472190772894086521144482721001553"),
|
||||
Fr.e("16950150798460657717958625567821834550301663161624707787222815936182638968203")
|
||||
];
|
||||
|
||||
let c = [Fr.e(0), Fr.e(1)];
|
||||
|
||||
for (let i=0; i<31;i++) {
|
||||
c = babyJub.addPoint(c,g);
|
||||
}
|
||||
|
||||
await circuit.assertOut(w, {out: [Fr.toObject(c[0]), Fr.toObject(c[1])] });
|
||||
|
||||
const w2 = await circuit.calculateWitness({"in": Scalar.add(Scalar.shl(Scalar.e(1), 252),Scalar.e(1))});
|
||||
|
||||
c = [g[0], g[1]];
|
||||
for (let i=0; i<252;i++) {
|
||||
c = babyJub.addPoint(c,c);
|
||||
}
|
||||
c = babyJub.addPoint(c,g);
|
||||
|
||||
await circuit.assertOut(w2, {out: [Fr.toObject(c[0]), Fr.toObject(c[1])] });
|
||||
|
||||
}).timeout(10000000);
|
||||
|
||||
it("Number of constrains for 256 bits", async () => {
|
||||
|
||||
const circuit = await wasm_tester(path.join(__dirname, "circuits", "escalarmul_test_min.circom"));
|
||||
|
||||
}).timeout(10000000);
|
||||
|
||||
});
|
||||
51
circom/node_modules/circomlib/test/escalarmulany.js
generated
vendored
Normal file
51
circom/node_modules/circomlib/test/escalarmulany.js
generated
vendored
Normal file
@@ -0,0 +1,51 @@
|
||||
const chai = require("chai");
|
||||
const path = require("path");
|
||||
const wasm_tester = require("circom_tester").wasm;
|
||||
|
||||
const F1Field = require("ffjavascript").F1Field;
|
||||
const Scalar = require("ffjavascript").Scalar;
|
||||
exports.p = Scalar.fromString("21888242871839275222246405745257275088548364400416034343698204186575808495617");
|
||||
const Fr = new F1Field(exports.p);
|
||||
|
||||
function print(circuit, w, s) {
|
||||
console.log(s + ": " + w[circuit.getSignalIdx(s)]);
|
||||
}
|
||||
|
||||
describe("Escalarmul test", function () {
|
||||
let circuitEMulAny;
|
||||
|
||||
this.timeout(100000);
|
||||
|
||||
let g;
|
||||
|
||||
before( async() => {
|
||||
circuitEMulAny = await wasm_tester(path.join(__dirname, "circuits", "escalarmulany_test.circom"));
|
||||
g = [
|
||||
Fr.e("5299619240641551281634865583518297030282874472190772894086521144482721001553"),
|
||||
Fr.e("16950150798460657717958625567821834550301663161624707787222815936182638968203")
|
||||
]
|
||||
});
|
||||
|
||||
it("Should generate Same escalar mul", async () => {
|
||||
|
||||
const w = await circuitEMulAny.calculateWitness({"e": 1, "p": g});
|
||||
|
||||
await circuitEMulAny.checkConstraints(w);
|
||||
|
||||
await circuitEMulAny.assertOut(w, {out: g}, true);
|
||||
|
||||
});
|
||||
|
||||
it("If multiply by order should return 0", async () => {
|
||||
|
||||
const r = Fr.e("2736030358979909402780800718157159386076813972158567259200215660948447373041");
|
||||
const w = await circuitEMulAny.calculateWitness({"e": r, "p": g});
|
||||
|
||||
await circuitEMulAny.checkConstraints(w);
|
||||
|
||||
await circuitEMulAny.assertOut(w, {out: [0,1]}, true);
|
||||
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
95
circom/node_modules/circomlib/test/escalarmulfix.js
generated
vendored
Normal file
95
circom/node_modules/circomlib/test/escalarmulfix.js
generated
vendored
Normal file
@@ -0,0 +1,95 @@
|
||||
const chai = require("chai");
|
||||
const path = require("path");
|
||||
const wasm_tester = require("circom_tester").wasm;
|
||||
const buildBabyjub = require("circomlibjs").buildBabyjub;
|
||||
const Scalar = require("ffjavascript").Scalar;
|
||||
|
||||
const assert = chai.assert;
|
||||
|
||||
function print(circuit, w, s) {
|
||||
console.log(s + ": " + w[circuit.getSignalIdx(s)]);
|
||||
}
|
||||
|
||||
describe("Escalarmul test", function () {
|
||||
let babyJub;
|
||||
let Fr;
|
||||
let circuit;
|
||||
|
||||
this.timeout(100000);
|
||||
|
||||
|
||||
before( async() => {
|
||||
babyJub = await buildBabyjub();
|
||||
Fr = babyJub.F;
|
||||
circuit = await wasm_tester(path.join(__dirname, "circuits", "escalarmulfix_test.circom"));
|
||||
});
|
||||
|
||||
it("Should generate Same escalar mul", async () => {
|
||||
|
||||
const w = await circuit.calculateWitness({"e": 0});
|
||||
|
||||
await circuit.checkConstraints(w);
|
||||
|
||||
await circuit.assertOut(w, {out: [0,1]}, true);
|
||||
|
||||
});
|
||||
|
||||
it("Should generate Same escalar mul", async () => {
|
||||
|
||||
const w = await circuit.calculateWitness({"e": 1}, true);
|
||||
|
||||
await circuit.checkConstraints(w);
|
||||
|
||||
await circuit.assertOut(w, {out: [Fr.toObject(babyJub.Base8[0]), Fr.toObject(babyJub.Base8[1])]});
|
||||
|
||||
});
|
||||
|
||||
it("Should generate scalar mul of a specific constant", async () => {
|
||||
|
||||
const s = Scalar.e("2351960337287830298912035165133676222414898052661454064215017316447594616519");
|
||||
const base8 = [
|
||||
Fr.e("5299619240641551281634865583518297030282874472190772894086521144482721001553"),
|
||||
Fr.e("16950150798460657717958625567821834550301663161624707787222815936182638968203")
|
||||
];
|
||||
|
||||
const w = await circuit.calculateWitness({"e": s}, true);
|
||||
|
||||
await circuit.checkConstraints(w);
|
||||
|
||||
const expectedRes = babyJub.mulPointEscalar(base8, s);
|
||||
|
||||
await circuit.assertOut(w, {out: [Fr.toObject(expectedRes[0]), Fr.toObject(expectedRes[1])]});
|
||||
|
||||
});
|
||||
|
||||
it("Should generate scalar mul of the firsts 50 elements", async () => {
|
||||
|
||||
const base8 = [
|
||||
Fr.e("5299619240641551281634865583518297030282874472190772894086521144482721001553"),
|
||||
Fr.e("16950150798460657717958625567821834550301663161624707787222815936182638968203")
|
||||
];
|
||||
|
||||
for (let i=0; i<50; i++) {
|
||||
const s = Scalar.e(i);
|
||||
|
||||
const w = await circuit.calculateWitness({"e": s}, true);
|
||||
|
||||
await circuit.checkConstraints(w);
|
||||
|
||||
const expectedRes = babyJub.mulPointEscalar(base8, s);
|
||||
|
||||
await circuit.assertOut(w, {out: [Fr.toObject(expectedRes[0]), Fr.toObject(expectedRes[1])]});
|
||||
}
|
||||
});
|
||||
|
||||
it("If multiply by order should return 0", async () => {
|
||||
|
||||
const w = await circuit.calculateWitness({"e": babyJub.subOrder }, true);
|
||||
|
||||
await circuit.checkConstraints(w);
|
||||
|
||||
await circuit.assertOut(w, {out: [0,1]});
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
22
circom/node_modules/circomlib/test/helpers/printsignal.js
generated
vendored
Normal file
22
circom/node_modules/circomlib/test/helpers/printsignal.js
generated
vendored
Normal file
@@ -0,0 +1,22 @@
|
||||
|
||||
const snarkjs = require("snarkjs");
|
||||
|
||||
const bigInt = snarkjs.bigInt;
|
||||
|
||||
module.exports = function hexBits(cir, witness, sig, nBits) {
|
||||
let v = bigInt(0);
|
||||
for (let i=nBits-1; i>=0; i--) {
|
||||
v = v.shiftLeft(1);
|
||||
const name = sig+"["+i+"]";
|
||||
const idx = cir.getSignalIdx(name);
|
||||
const vbit = bigInt(witness[idx].toString());
|
||||
if (vbit.equals(bigInt(1))) {
|
||||
v = v.add(bigInt(1));
|
||||
} else if (vbit.equals(bigInt(0))) {
|
||||
v;
|
||||
} else {
|
||||
console.log("Not Binary: "+name);
|
||||
}
|
||||
}
|
||||
return v.toString(16);
|
||||
};
|
||||
178
circom/node_modules/circomlib/test/helpers/sha256.js
generated
vendored
Normal file
178
circom/node_modules/circomlib/test/helpers/sha256.js
generated
vendored
Normal file
@@ -0,0 +1,178 @@
|
||||
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
|
||||
/* SHA-256 (FIPS 180-4) implementation in JavaScript (c) Chris Veness 2002-2017 */
|
||||
/* MIT Licence */
|
||||
/* www.movable-type.co.uk/scripts/sha256.html */
|
||||
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
|
||||
|
||||
'use strict';
|
||||
|
||||
|
||||
/**
|
||||
* SHA-256 hash function reference implementation.
|
||||
*
|
||||
* This is an annotated direct implementation of FIPS 180-4, without any optimisations. It is
|
||||
* intended to aid understanding of the algorithm rather than for production use.
|
||||
*
|
||||
* While it could be used where performance is not critical, I would recommend using the ‘Web
|
||||
* Cryptography API’ (developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/digest) for the browser,
|
||||
* or the ‘crypto’ library (nodejs.org/api/crypto.html#crypto_class_hash) in Node.js.
|
||||
*
|
||||
* See csrc.nist.gov/groups/ST/toolkit/secure_hashing.html
|
||||
* csrc.nist.gov/groups/ST/toolkit/examples.html
|
||||
*/
|
||||
class Sha256 {
|
||||
|
||||
/**
|
||||
* Generates SHA-256 hash of string.
|
||||
*
|
||||
* @param {string} msg - (Unicode) string to be hashed.
|
||||
* @param {Object} [options]
|
||||
* @param {string} [options.msgFormat=string] - Message format: 'string' for JavaScript string
|
||||
* (gets converted to UTF-8 for hashing); 'hex-bytes' for string of hex bytes ('616263' ≡ 'abc') .
|
||||
* @param {string} [options.outFormat=hex] - Output format: 'hex' for string of contiguous
|
||||
* hex bytes; 'hex-w' for grouping hex bytes into groups of (4 byte / 8 character) words.
|
||||
* @returns {string} Hash of msg as hex character string.
|
||||
*/
|
||||
static hash(msg, options) {
|
||||
const defaults = { msgFormat: 'string', outFormat: 'hex' };
|
||||
const opt = Object.assign(defaults, options);
|
||||
|
||||
// note use throughout this routine of 'n >>> 0' to coerce Number 'n' to unsigned 32-bit integer
|
||||
|
||||
switch (opt.msgFormat) {
|
||||
default: // default is to convert string to UTF-8, as SHA only deals with byte-streams
|
||||
case 'string': msg = utf8Encode(msg); break;
|
||||
case 'hex-bytes':msg = hexBytesToString(msg); break; // mostly for running tests
|
||||
}
|
||||
|
||||
// constants [§4.2.2]
|
||||
const K = [
|
||||
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
|
||||
0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
|
||||
0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
|
||||
0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
|
||||
0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
|
||||
0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
|
||||
0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
|
||||
0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2 ];
|
||||
|
||||
// initial hash value [§5.3.3]
|
||||
const H = [
|
||||
0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19 ];
|
||||
|
||||
// PREPROCESSING [§6.2.1]
|
||||
|
||||
msg += String.fromCharCode(0x80); // add trailing '1' bit (+ 0's padding) to string [§5.1.1]
|
||||
|
||||
// convert string msg into 512-bit blocks (array of 16 32-bit integers) [§5.2.1]
|
||||
const l = msg.length/4 + 2; // length (in 32-bit integers) of msg + ‘1’ + appended length
|
||||
const N = Math.ceil(l/16); // number of 16-integer (512-bit) blocks required to hold 'l' ints
|
||||
const M = new Array(N); // message M is N×16 array of 32-bit integers
|
||||
|
||||
for (let i=0; i<N; i++) {
|
||||
M[i] = new Array(16);
|
||||
for (let j=0; j<16; j++) { // encode 4 chars per integer (64 per block), big-endian encoding
|
||||
M[i][j] = (msg.charCodeAt(i*64+j*4+0)<<24) | (msg.charCodeAt(i*64+j*4+1)<<16)
|
||||
| (msg.charCodeAt(i*64+j*4+2)<< 8) | (msg.charCodeAt(i*64+j*4+3)<< 0);
|
||||
} // note running off the end of msg is ok 'cos bitwise ops on NaN return 0
|
||||
}
|
||||
// add length (in bits) into final pair of 32-bit integers (big-endian) [§5.1.1]
|
||||
// note: most significant word would be (len-1)*8 >>> 32, but since JS converts
|
||||
// bitwise-op args to 32 bits, we need to simulate this by arithmetic operators
|
||||
const lenHi = ((msg.length-1)*8) / Math.pow(2, 32);
|
||||
const lenLo = ((msg.length-1)*8) >>> 0;
|
||||
M[N-1][14] = Math.floor(lenHi);
|
||||
M[N-1][15] = lenLo;
|
||||
|
||||
// HASH COMPUTATION [§6.2.2]
|
||||
|
||||
for (let i=0; i<N; i++) {
|
||||
const W = new Array(64);
|
||||
|
||||
// 1 - prepare message schedule 'W'
|
||||
for (let t=0; t<16; t++) W[t] = M[i][t];
|
||||
for (let t=16; t<64; t++) {
|
||||
W[t] = (Sha256.σ1(W[t-2]) + W[t-7] + Sha256.σ0(W[t-15]) + W[t-16]) >>> 0;
|
||||
}
|
||||
|
||||
// 2 - initialise working variables a, b, c, d, e, f, g, h with previous hash value
|
||||
let a = H[0], b = H[1], c = H[2], d = H[3], e = H[4], f = H[5], g = H[6], h = H[7];
|
||||
|
||||
// 3 - main loop (note '>>> 0' for 'addition modulo 2^32')
|
||||
for (let t=0; t<64; t++) {
|
||||
const T1 = h + Sha256.Σ1(e) + Sha256.Ch(e, f, g) + K[t] + W[t];
|
||||
const T2 = Sha256.Σ0(a) + Sha256.Maj(a, b, c);
|
||||
h = g;
|
||||
g = f;
|
||||
f = e;
|
||||
e = (d + T1) >>> 0;
|
||||
d = c;
|
||||
c = b;
|
||||
b = a;
|
||||
a = (T1 + T2) >>> 0;
|
||||
}
|
||||
|
||||
// 4 - compute the new intermediate hash value (note '>>> 0' for 'addition modulo 2^32')
|
||||
H[0] = (H[0]+a) >>> 0;
|
||||
H[1] = (H[1]+b) >>> 0;
|
||||
H[2] = (H[2]+c) >>> 0;
|
||||
H[3] = (H[3]+d) >>> 0;
|
||||
H[4] = (H[4]+e) >>> 0;
|
||||
H[5] = (H[5]+f) >>> 0;
|
||||
H[6] = (H[6]+g) >>> 0;
|
||||
H[7] = (H[7]+h) >>> 0;
|
||||
}
|
||||
|
||||
// convert H0..H7 to hex strings (with leading zeros)
|
||||
for (let h=0; h<H.length; h++) H[h] = ('00000000'+H[h].toString(16)).slice(-8);
|
||||
|
||||
// concatenate H0..H7, with separator if required
|
||||
const separator = opt.outFormat=='hex-w' ? ' ' : '';
|
||||
|
||||
return H.join(separator);
|
||||
|
||||
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
|
||||
|
||||
function utf8Encode(str) {
|
||||
try {
|
||||
return new TextEncoder().encode(str, 'utf-8').reduce((prev, curr) => prev + String.fromCharCode(curr), '');
|
||||
} catch (e) { // no TextEncoder available?
|
||||
return unescape(encodeURIComponent(str)); // monsur.hossa.in/2012/07/20/utf-8-in-javascript.html
|
||||
}
|
||||
}
|
||||
|
||||
function hexBytesToString(hexStr) { // convert string of hex numbers to a string of chars (eg '616263' -> 'abc').
|
||||
const str = hexStr.replace(' ', ''); // allow space-separated groups
|
||||
return str=='' ? '' : str.match(/.{2}/g).map(byte => String.fromCharCode(parseInt(byte, 16))).join('');
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Rotates right (circular right shift) value x by n positions [§3.2.4].
|
||||
* @private
|
||||
*/
|
||||
static ROTR(n, x) {
|
||||
return (x >>> n) | (x << (32-n));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Logical functions [§4.1.2].
|
||||
* @private
|
||||
*/
|
||||
static Σ0(x) { return Sha256.ROTR(2, x) ^ Sha256.ROTR(13, x) ^ Sha256.ROTR(22, x); }
|
||||
static Σ1(x) { return Sha256.ROTR(6, x) ^ Sha256.ROTR(11, x) ^ Sha256.ROTR(25, x); }
|
||||
static σ0(x) { return Sha256.ROTR(7, x) ^ Sha256.ROTR(18, x) ^ (x>>>3); }
|
||||
static σ1(x) { return Sha256.ROTR(17, x) ^ Sha256.ROTR(19, x) ^ (x>>>10); }
|
||||
static Ch(x, y, z) { return (x & y) ^ (~x & z); } // 'choice'
|
||||
static Maj(x, y, z) { return (x & y) ^ (x & z) ^ (y & z); } // 'majority'
|
||||
|
||||
}
|
||||
|
||||
|
||||
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
|
||||
|
||||
if (typeof module != 'undefined' && module.exports) module.exports = Sha256; // ≡ export default Sha256
|
||||
|
||||
27
circom/node_modules/circomlib/test/mimccircuit.js
generated
vendored
Normal file
27
circom/node_modules/circomlib/test/mimccircuit.js
generated
vendored
Normal file
@@ -0,0 +1,27 @@
|
||||
const chai = require("chai");
|
||||
const path = require("path");
|
||||
const wasm_tester = require("circom_tester").wasm;
|
||||
|
||||
const buildMimc7 = require("circomlibjs").buildMimc7;
|
||||
|
||||
describe("MiMC Circuit test", function () {
|
||||
let circuit;
|
||||
let mimc7;
|
||||
|
||||
this.timeout(100000);
|
||||
|
||||
before( async () => {
|
||||
mimc7 = await buildMimc7();
|
||||
circuit = await wasm_tester(path.join(__dirname, "circuits", "mimc_test.circom"));
|
||||
});
|
||||
|
||||
it("Should check constrain", async () => {
|
||||
const w = await circuit.calculateWitness({x_in: 1, k: 2}, true);
|
||||
|
||||
const res2 = mimc7.hash(1,2,91);
|
||||
|
||||
await circuit.assertOut(w, {out: mimc7.F.toObject(res2)});
|
||||
|
||||
await circuit.checkConstraints(w);
|
||||
});
|
||||
});
|
||||
47
circom/node_modules/circomlib/test/mimcspongecircuit.js
generated
vendored
Normal file
47
circom/node_modules/circomlib/test/mimcspongecircuit.js
generated
vendored
Normal file
@@ -0,0 +1,47 @@
|
||||
const path = require("path");
|
||||
const wasm_tester = require("circom_tester").wasm;
|
||||
|
||||
const buildMimcSponge = require("circomlibjs").buildMimcSponge;
|
||||
|
||||
|
||||
describe("MiMC Sponge Circuit test", function () {
|
||||
let circuit;
|
||||
let mimcSponge;
|
||||
let F;
|
||||
|
||||
this.timeout(100000);
|
||||
|
||||
before( async () => {
|
||||
mimcSponge = await buildMimcSponge();
|
||||
F = mimcSponge.F;
|
||||
});
|
||||
|
||||
|
||||
it("Should check permutation", async () => {
|
||||
|
||||
circuit = await wasm_tester(path.join(__dirname, "circuits", "mimc_sponge_test.circom"));
|
||||
|
||||
const w = await circuit.calculateWitness({xL_in: 1, xR_in: 2, k: 3});
|
||||
|
||||
const out2 = mimcSponge.hash(1,2,3);
|
||||
|
||||
await circuit.assertOut(w, {xL_out: F.toObject(out2.xL), xR_out: F.toObject(out2.xR)});
|
||||
|
||||
await circuit.checkConstraints(w);
|
||||
|
||||
});
|
||||
|
||||
it("Should check hash", async () => {
|
||||
circuit = await wasm_tester(path.join(__dirname, "circuits", "mimc_sponge_hash_test.circom"));
|
||||
|
||||
const w = await circuit.calculateWitness({ins: [1, 2], k: 0});
|
||||
|
||||
const out2 = mimcSponge.multiHash([1,2], 0, 3);
|
||||
|
||||
for (let i=0; i<out2.length; i++) out2[i] = F.toObject(out2[i]);
|
||||
|
||||
await circuit.assertOut(w, {outs: out2});
|
||||
|
||||
await circuit.checkConstraints(w);
|
||||
});
|
||||
});
|
||||
101
circom/node_modules/circomlib/test/montgomery.js
generated
vendored
Normal file
101
circom/node_modules/circomlib/test/montgomery.js
generated
vendored
Normal file
@@ -0,0 +1,101 @@
|
||||
const chai = require("chai");
|
||||
const path = require("path");
|
||||
const wasm_tester = require("circom_tester").wasm;
|
||||
const Scalar = require("ffjavascript").Scalar;
|
||||
const buildBabyjub = require("circomlibjs").buildBabyjub;
|
||||
|
||||
const assert = chai.assert;
|
||||
|
||||
describe("Montgomery test", function () {
|
||||
let babyJub;
|
||||
let Fr;
|
||||
let circuitE2M;
|
||||
let circuitM2E;
|
||||
let circuitMAdd;
|
||||
let circuitMDouble;
|
||||
|
||||
let g;
|
||||
|
||||
let mg, mg2, g2, g3, mg3;
|
||||
|
||||
this.timeout(100000);
|
||||
|
||||
|
||||
before( async() => {
|
||||
babyJub = await buildBabyjub();
|
||||
Fr = babyJub.F;
|
||||
g = [
|
||||
Fr.e("5299619240641551281634865583518297030282874472190772894086521144482721001553"),
|
||||
Fr.e("16950150798460657717958625567821834550301663161624707787222815936182638968203")
|
||||
];
|
||||
|
||||
circuitE2M = await wasm_tester(path.join(__dirname, "circuits", "edwards2montgomery.circom"));
|
||||
await circuitE2M.loadSymbols();
|
||||
circuitM2E = await wasm_tester(path.join(__dirname, "circuits", "montgomery2edwards.circom"));
|
||||
await circuitM2E.loadSymbols();
|
||||
circuitMAdd = await wasm_tester(path.join(__dirname, "circuits", "montgomeryadd.circom"));
|
||||
await circuitMAdd.loadSymbols();
|
||||
circuitMDouble = await wasm_tester(path.join(__dirname, "circuits", "montgomerydouble.circom"));
|
||||
await circuitMDouble.loadSymbols();
|
||||
});
|
||||
|
||||
it("Convert Edwards to Montgomery and back again", async () => {
|
||||
let w, xout, yout;
|
||||
|
||||
w = await circuitE2M.calculateWitness({ in: [Fr.toObject(g[0]), Fr.toObject(g[1])]}, true);
|
||||
|
||||
xout = w[circuitE2M.symbols["main.out[0]"].varIdx];
|
||||
yout = w[circuitE2M.symbols["main.out[1]"].varIdx];
|
||||
|
||||
mg = [xout, yout];
|
||||
|
||||
w = await circuitM2E.calculateWitness({ in: [xout, yout]}, true);
|
||||
|
||||
xout = w[circuitM2E.symbols["main.out[0]"].varIdx];
|
||||
yout = w[circuitM2E.symbols["main.out[1]"].varIdx];
|
||||
|
||||
assert(Fr.eq(Fr.e(xout), g[0]));
|
||||
assert(Fr.eq(Fr.e(yout), g[1]));
|
||||
});
|
||||
it("Should double a point", async () => {
|
||||
let w, xout, yout;
|
||||
|
||||
g2 = babyJub.addPoint(g,g);
|
||||
|
||||
w = await circuitMDouble.calculateWitness({ in: mg}, true);
|
||||
|
||||
xout = w[circuitE2M.symbols["main.out[0]"].varIdx];
|
||||
yout = w[circuitE2M.symbols["main.out[1]"].varIdx];
|
||||
|
||||
mg2 = [xout, yout];
|
||||
|
||||
w = await circuitM2E.calculateWitness({ in: mg2}, true);
|
||||
|
||||
xout = w[circuitM2E.symbols["main.out[0]"].varIdx];
|
||||
yout = w[circuitM2E.symbols["main.out[1]"].varIdx];
|
||||
|
||||
|
||||
assert(Fr.eq(Fr.e(xout), g2[0]));
|
||||
assert(Fr.eq(Fr.e(yout), g2[1]));
|
||||
});
|
||||
it("Should add a point", async () => {
|
||||
let w, xout, yout;
|
||||
|
||||
g3 = babyJub.addPoint(g,g2);
|
||||
|
||||
w = await circuitMAdd.calculateWitness({ in1: mg, in2: mg2}, true);
|
||||
|
||||
xout = w[circuitMAdd.symbols["main.out[0]"].varIdx];
|
||||
yout = w[circuitMAdd.symbols["main.out[1]"].varIdx];
|
||||
|
||||
mg3 = [xout, yout];
|
||||
|
||||
w = await circuitM2E.calculateWitness({ in: mg3}, true);
|
||||
|
||||
xout = w[circuitM2E.symbols["main.out[0]"].varIdx];
|
||||
yout = w[circuitM2E.symbols["main.out[1]"].varIdx];
|
||||
|
||||
assert(Fr.eq(Fr.e(xout), g3[0]));
|
||||
assert(Fr.eq(Fr.e(yout), g3[1]));
|
||||
});
|
||||
});
|
||||
101
circom/node_modules/circomlib/test/multiplexer.js
generated
vendored
Normal file
101
circom/node_modules/circomlib/test/multiplexer.js
generated
vendored
Normal file
@@ -0,0 +1,101 @@
|
||||
const path = require("path");
|
||||
const wasm_tester = require("circom_tester").wasm;
|
||||
const F1Field = require("ffjavascript").F1Field;
|
||||
const Scalar = require("ffjavascript").Scalar;
|
||||
exports.p = Scalar.fromString("21888242871839275222246405745257275088548364400416034343698204186575808495617");
|
||||
const Fr = new F1Field(exports.p);
|
||||
|
||||
describe("Mux4 test", function() {
|
||||
this.timeout(100000);
|
||||
it("Should create a constant multiplexer 4", async () => {
|
||||
|
||||
const circuit = await wasm_tester(path.join(__dirname, "circuits", "mux4_1.circom"));
|
||||
|
||||
const ct16 = [
|
||||
Fr.e("123"),
|
||||
Fr.e("456"),
|
||||
Fr.e("789"),
|
||||
Fr.e("012"),
|
||||
Fr.e("111"),
|
||||
Fr.e("222"),
|
||||
Fr.e("333"),
|
||||
Fr.e("4546"),
|
||||
Fr.e("134523"),
|
||||
Fr.e("44356"),
|
||||
Fr.e("15623"),
|
||||
Fr.e("4566"),
|
||||
Fr.e("1223"),
|
||||
Fr.e("4546"),
|
||||
Fr.e("4256"),
|
||||
Fr.e("4456")
|
||||
];
|
||||
|
||||
for (let i=0; i<16; i++) {
|
||||
const w = await circuit.calculateWitness({ "selector": i }, true);
|
||||
|
||||
await circuit.checkConstraints(w);
|
||||
|
||||
await circuit.assertOut(w, {out: ct16[i]});
|
||||
}
|
||||
});
|
||||
|
||||
it("Should create a constant multiplexer 3", async () => {
|
||||
|
||||
const circuit = await wasm_tester(path.join(__dirname, "circuits", "mux3_1.circom"));
|
||||
|
||||
const ct8 = [
|
||||
Fr.e("37"),
|
||||
Fr.e("47"),
|
||||
Fr.e("53"),
|
||||
Fr.e("71"),
|
||||
Fr.e("89"),
|
||||
Fr.e("107"),
|
||||
Fr.e("163"),
|
||||
Fr.e("191")
|
||||
];
|
||||
|
||||
for (let i=0; i<8; i++) {
|
||||
const w = await circuit.calculateWitness({ "selector": i }, true);
|
||||
|
||||
await circuit.checkConstraints(w);
|
||||
|
||||
await circuit.assertOut(w, {out: ct8[i]});
|
||||
}
|
||||
});
|
||||
it("Should create a constant multiplexer 2", async () => {
|
||||
|
||||
const circuit = await wasm_tester(path.join(__dirname, "circuits", "mux2_1.circom"));
|
||||
|
||||
const ct4 = [
|
||||
Fr.e("37"),
|
||||
Fr.e("47"),
|
||||
Fr.e("53"),
|
||||
Fr.e("71"),
|
||||
];
|
||||
|
||||
for (let i=0; i<4; i++) {
|
||||
const w = await circuit.calculateWitness({ "selector": i }, true);
|
||||
|
||||
await circuit.checkConstraints(w);
|
||||
|
||||
await circuit.assertOut(w, {out: ct4[i]});
|
||||
}
|
||||
});
|
||||
it("Should create a constant multiplexer 1", async () => {
|
||||
|
||||
const circuit = await wasm_tester(path.join(__dirname, "circuits", "mux1_1.circom"));
|
||||
|
||||
const ct2 = [
|
||||
Fr.e("37"),
|
||||
Fr.e("47"),
|
||||
];
|
||||
|
||||
for (let i=0; i<2; i++) {
|
||||
const w = await circuit.calculateWitness({ "selector": i }, true);
|
||||
|
||||
await circuit.checkConstraints(w);
|
||||
|
||||
await circuit.assertOut(w, {out: ct2[i]});
|
||||
}
|
||||
});
|
||||
});
|
||||
83
circom/node_modules/circomlib/test/pedersen.js
generated
vendored
Normal file
83
circom/node_modules/circomlib/test/pedersen.js
generated
vendored
Normal file
@@ -0,0 +1,83 @@
|
||||
const chai = require("chai");
|
||||
const path = require("path");
|
||||
|
||||
const Scalar = require("ffjavascript").Scalar;
|
||||
|
||||
const wasm_tester = require("circom_tester").wasm;
|
||||
|
||||
const buildBabyjub = require("circomlibjs").buildBabyjub;
|
||||
|
||||
|
||||
describe("Double Pedersen test", function() {
|
||||
let babyJub;
|
||||
let Fr;
|
||||
let PBASE;
|
||||
let circuit;
|
||||
this.timeout(100000);
|
||||
before( async() => {
|
||||
babyJub = await buildBabyjub();
|
||||
Fr = babyJub.F;
|
||||
PBASE =
|
||||
[
|
||||
[Fr.e("10457101036533406547632367118273992217979173478358440826365724437999023779287"),Fr.e("19824078218392094440610104313265183977899662750282163392862422243483260492317")],
|
||||
[Fr.e("2671756056509184035029146175565761955751135805354291559563293617232983272177"),Fr.e("2663205510731142763556352975002641716101654201788071096152948830924149045094")],
|
||||
[Fr.e("5802099305472655231388284418920769829666717045250560929368476121199858275951"),Fr.e("5980429700218124965372158798884772646841287887664001482443826541541529227896")],
|
||||
[Fr.e("7107336197374528537877327281242680114152313102022415488494307685842428166594"),Fr.e("2857869773864086953506483169737724679646433914307247183624878062391496185654")],
|
||||
[Fr.e("20265828622013100949498132415626198973119240347465898028410217039057588424236"),Fr.e("1160461593266035632937973507065134938065359936056410650153315956301179689506")]
|
||||
];
|
||||
circuit = await wasm_tester(path.join(__dirname, "circuits", "pedersen_test.circom"));
|
||||
|
||||
});
|
||||
|
||||
it("Should pedersen at zero", async () => {
|
||||
|
||||
let w;
|
||||
|
||||
w = await circuit.calculateWitness({ in: ["0", "0"]}, true);
|
||||
|
||||
await circuit.assertOut(w, {out: [0,1]});
|
||||
|
||||
});
|
||||
it("Should pedersen at one first generator", async () => {
|
||||
let w;
|
||||
|
||||
w = await circuit.calculateWitness({ in: ["1", "0"]}, true);
|
||||
|
||||
await circuit.assertOut(w, {out: [Fr.toObject(PBASE[0][0]), Fr.toObject(PBASE[0][1])]});
|
||||
|
||||
});
|
||||
it("Should pedersen at one second generator", async () => {
|
||||
let w;
|
||||
|
||||
w = await circuit.calculateWitness({ in: ["0", "1"]}, true);
|
||||
|
||||
await circuit.assertOut(w, {out: [Fr.toObject(PBASE[1][0]), Fr.toObject(PBASE[1][1])]});
|
||||
|
||||
});
|
||||
it("Should pedersen at mixed generators", async () => {
|
||||
let w;
|
||||
w = await circuit.calculateWitness({ in: ["3", "7"]}, true);
|
||||
|
||||
const r = babyJub.addPoint(
|
||||
babyJub.mulPointEscalar(PBASE[0], 3),
|
||||
babyJub.mulPointEscalar(PBASE[1], 7)
|
||||
);
|
||||
|
||||
await circuit.assertOut(w, {out: [Fr.toObject(r[0]), Fr.toObject(r[1])]});
|
||||
|
||||
});
|
||||
it("Should pedersen all ones", async () => {
|
||||
let w;
|
||||
|
||||
const allOnes = Scalar.sub(Scalar.shl(Scalar.e(1), 250), Scalar.e(1));
|
||||
w = await circuit.calculateWitness({ in: [allOnes, allOnes]}, true);
|
||||
|
||||
|
||||
const r2 = babyJub.addPoint(
|
||||
babyJub.mulPointEscalar(PBASE[0], allOnes),
|
||||
babyJub.mulPointEscalar(PBASE[1], allOnes)
|
||||
);
|
||||
|
||||
await circuit.assertOut(w, {out: [Fr.toObject(r2[0]), Fr.toObject(r2[1])]});
|
||||
});
|
||||
});
|
||||
56
circom/node_modules/circomlib/test/pedersen2.js
generated
vendored
Normal file
56
circom/node_modules/circomlib/test/pedersen2.js
generated
vendored
Normal file
@@ -0,0 +1,56 @@
|
||||
const path = require("path");
|
||||
|
||||
const Scalar = require("ffjavascript").Scalar;
|
||||
|
||||
const buildPedersenHash = require("circomlibjs").buildPedersenHash;
|
||||
const buildBabyJub = require("circomlibjs").buildBabyjub;
|
||||
|
||||
const wasm_tester = require("circom_tester").wasm;
|
||||
|
||||
|
||||
describe("Pedersen test", function() {
|
||||
let babyJub
|
||||
let pedersen;
|
||||
let F;
|
||||
let circuit;
|
||||
this.timeout(100000);
|
||||
before( async() => {
|
||||
|
||||
babyJub = await buildBabyJub();
|
||||
F = babyJub.F;
|
||||
pedersen = await buildPedersenHash();
|
||||
circuit = await wasm_tester(path.join(__dirname, "circuits", "pedersen2_test.circom"));
|
||||
});
|
||||
it("Should pedersen at zero", async () => {
|
||||
|
||||
let w;
|
||||
|
||||
w = await circuit.calculateWitness({ in: 0}, true);
|
||||
|
||||
const b = Buffer.alloc(32);
|
||||
|
||||
const h = pedersen.hash(b);
|
||||
const hP = babyJub.unpackPoint(h);
|
||||
|
||||
await circuit.assertOut(w, {out: [F.toObject(hP[0]), F.toObject(hP[1])] });
|
||||
|
||||
});
|
||||
it("Should pedersen with 253 ones", async () => {
|
||||
|
||||
let w;
|
||||
|
||||
const n = F.e(Scalar.sub(Scalar.shl(Scalar.e(1), 253), Scalar.e(1)));
|
||||
|
||||
w = await circuit.calculateWitness({ in: F.toObject(n)}, true);
|
||||
|
||||
const b = Buffer.alloc(32);
|
||||
for (let i=0; i<31; i++) b[i] = 0xFF;
|
||||
b[31] = 0x1F;
|
||||
|
||||
const h = pedersen.hash(b);
|
||||
const hP = babyJub.unpackPoint(h);
|
||||
|
||||
await circuit.assertOut(w, {out: [F.toObject(hP[0]), F.toObject(hP[1])] });
|
||||
|
||||
});
|
||||
});
|
||||
30
circom/node_modules/circomlib/test/point2bits.js
generated
vendored
Normal file
30
circom/node_modules/circomlib/test/point2bits.js
generated
vendored
Normal file
@@ -0,0 +1,30 @@
|
||||
const path = require("path");
|
||||
const wasm_tester = require("circom_tester").wasm;
|
||||
const buildBabyJub = require("circomlibjs").buildBabyjub;
|
||||
|
||||
const babyJub = require("circomlibjs").babyjub;
|
||||
|
||||
|
||||
describe("Point 2 bits test", function() {
|
||||
let babyJub;
|
||||
let F;
|
||||
let circuit;
|
||||
this.timeout(100000);
|
||||
before( async() => {
|
||||
babyJub = await buildBabyJub();
|
||||
F = babyJub.F;
|
||||
|
||||
circuit = await wasm_tester(path.join(__dirname, "circuits", "pointbits_loopback.circom"));
|
||||
});
|
||||
|
||||
it("Should do the both convertions for 8Base", async () => {
|
||||
const w = await circuit.calculateWitness({ in: [F.toObject(babyJub.Base8[0]), F.toObject(babyJub.Base8[1])]}, true);
|
||||
|
||||
await circuit.checkConstraints(w);
|
||||
});
|
||||
it("Should do the both convertions for Zero point", async () => {
|
||||
const w = await circuit.calculateWitness({ in: [0, 1]}, true);
|
||||
|
||||
await circuit.checkConstraints(w);
|
||||
});
|
||||
});
|
||||
80
circom/node_modules/circomlib/test/poseidoncircuit.js
generated
vendored
Normal file
80
circom/node_modules/circomlib/test/poseidoncircuit.js
generated
vendored
Normal file
@@ -0,0 +1,80 @@
|
||||
const chai = require("chai");
|
||||
const path = require("path");
|
||||
const wasm_tester = require("circom_tester").wasm;
|
||||
|
||||
const buildPoseidon = require("circomlibjs").buildPoseidon;
|
||||
|
||||
const assert = chai.assert;
|
||||
|
||||
describe("Poseidon Circuit test", function () {
|
||||
let poseidon;
|
||||
let F;
|
||||
let circuit6;
|
||||
let circuit3;
|
||||
let circuitEx;
|
||||
|
||||
this.timeout(1000000);
|
||||
|
||||
before( async () => {
|
||||
poseidon = await buildPoseidon();
|
||||
F = poseidon.F;
|
||||
circuit6 = await wasm_tester(path.join(__dirname, "circuits", "poseidon6_test.circom"));
|
||||
circuit3 = await wasm_tester(path.join(__dirname, "circuits", "poseidon3_test.circom"));
|
||||
circuitEx = await wasm_tester(path.join(__dirname, "circuits", "poseidonex_test.circom"));
|
||||
});
|
||||
|
||||
it("Should check constrain of hash([1, 2]) t=6", async () => {
|
||||
const w = await circuit6.calculateWitness({inputs: [1, 2, 0,0,0]}, true);
|
||||
|
||||
const res2 = poseidon([1,2,0,0,0]);
|
||||
|
||||
assert(F.eq(F.e("1018317224307729531995786483840663576608797660851238720571059489595066344487"), F.e(res2)));
|
||||
await circuit6.assertOut(w, {out : F.toObject(res2)});
|
||||
await circuit6.checkConstraints(w);
|
||||
});
|
||||
|
||||
it("Should check constrain of hash([3, 4]) t=6", async () => {
|
||||
const w = await circuit6.calculateWitness({inputs: [3, 4,5,10,23]});
|
||||
|
||||
const res2 = poseidon([3, 4,5,10,23]);
|
||||
|
||||
assert(F.eq(F.e("13034429309846638789535561449942021891039729847501137143363028890275222221409"), F.e(res2)));
|
||||
await circuit6.assertOut(w, {out : F.toObject(res2)});
|
||||
await circuit6.checkConstraints(w);
|
||||
});
|
||||
|
||||
|
||||
it("Should check constrain of hash([1, 2]) t=3", async () => {
|
||||
const w = await circuit3.calculateWitness({inputs: [1, 2]});
|
||||
|
||||
const res2 = poseidon([1,2]);
|
||||
|
||||
assert(F.eq(F.e("7853200120776062878684798364095072458815029376092732009249414926327459813530"), F.e(res2)));
|
||||
await circuit3.assertOut(w, {out : F.toObject(res2)});
|
||||
await circuit3.checkConstraints(w);
|
||||
});
|
||||
|
||||
it("Should check constrain of hash([3, 4]) t=3", async () => {
|
||||
const w = await circuit3.calculateWitness({inputs: [3, 4]});
|
||||
|
||||
const res2 = poseidon([3, 4]);
|
||||
|
||||
assert(F.eq(F.e("14763215145315200506921711489642608356394854266165572616578112107564877678998"), F.e(res2)));
|
||||
await circuit3.assertOut(w, {out : F.toObject(res2)});
|
||||
await circuit3.checkConstraints(w);
|
||||
});
|
||||
|
||||
it("Should check constrain of hash with state and 16 ins and outs", async () => {
|
||||
const ins = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]
|
||||
const w = await circuitEx.calculateWitness({inputs: ins, initialState: 17});
|
||||
|
||||
const res2 = poseidon(ins, 17, 17);
|
||||
const res2f = [];
|
||||
for (let i=0; i<res2.length; i++) {
|
||||
res2f[i] = F.toObject(res2[i]);
|
||||
}
|
||||
await circuitEx.assertOut(w, {out : res2f});
|
||||
await circuitEx.checkConstraints(w);
|
||||
});
|
||||
|
||||
});
|
||||
118
circom/node_modules/circomlib/test/sha256.js
generated
vendored
Normal file
118
circom/node_modules/circomlib/test/sha256.js
generated
vendored
Normal file
@@ -0,0 +1,118 @@
|
||||
const chai = require("chai");
|
||||
const path = require("path");
|
||||
const crypto = require("crypto");
|
||||
const F1Field = require("ffjavascript").F1Field;
|
||||
const Scalar = require("ffjavascript").Scalar;
|
||||
exports.p = Scalar.fromString("21888242871839275222246405745257275088548364400416034343698204186575808495617");
|
||||
const Fr = new F1Field(exports.p);
|
||||
|
||||
const assert = chai.assert;
|
||||
|
||||
const sha256 = require("./helpers/sha256");
|
||||
|
||||
const wasm_tester = require("circom_tester").wasm;
|
||||
|
||||
// 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));
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
function bitArray2buffer(a) {
|
||||
const len = Math.floor((a.length -1 )/8)+1;
|
||||
const b = new Buffer.alloc(len);
|
||||
|
||||
for (let i=0; i<a.length; i++) {
|
||||
const p = Math.floor(i/8);
|
||||
b[p] = b[p] | (Number(a[i]) << ( 7 - (i%8) ));
|
||||
}
|
||||
return b;
|
||||
}
|
||||
|
||||
|
||||
describe("SHA256 test", function () {
|
||||
this.timeout(100000);
|
||||
|
||||
|
||||
it("Should work bits to array and array to bits", async () => {
|
||||
const b = new Buffer.alloc(64);
|
||||
for (let i=0; i<64; i++) {
|
||||
b[i] = i+1;
|
||||
}
|
||||
const a = buffer2bitArray(b);
|
||||
const b2 = bitArray2buffer(a);
|
||||
|
||||
assert.equal(b.toString("hex"), b2.toString("hex"), true);
|
||||
});
|
||||
|
||||
it("Should calculate a hash of 1 compressor", async () => {
|
||||
const cir = await wasm_tester(path.join(__dirname, "circuits", "sha256_2_test.circom"));
|
||||
|
||||
const witness = await cir.calculateWitness({ "a": "1", "b": "2" }, true);
|
||||
|
||||
const b = new Buffer.alloc(54);
|
||||
b[26] = 1;
|
||||
b[53] = 2;
|
||||
|
||||
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"});
|
||||
|
||||
assert.equal(hash, hash2);
|
||||
|
||||
assert(Fr.eq(witness[1], Fr.e(r)));
|
||||
}).timeout(1000000);
|
||||
|
||||
it("Should calculate a hash of 2 compressor", async () => {
|
||||
const cir = await wasm_tester(path.join(__dirname, "circuits", "sha256_test512.circom"));
|
||||
|
||||
const b = new Buffer.alloc(64);
|
||||
for (let i=0; i<64; i++) {
|
||||
b[i] = i+1;
|
||||
}
|
||||
|
||||
const hash = crypto.createHash("sha256")
|
||||
.update(b)
|
||||
.digest("hex");
|
||||
|
||||
const arrIn = buffer2bitArray(b);
|
||||
const witness = await cir.calculateWitness({ "in": arrIn }, 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 cir = await wasm_tester(path.join(__dirname, "circuits", "sha256_test448.circom"));
|
||||
|
||||
const testStr = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
|
||||
|
||||
const b = Buffer.from(testStr, "utf8");
|
||||
|
||||
const hash = crypto.createHash("sha256")
|
||||
.update(b)
|
||||
.digest("hex");
|
||||
|
||||
const arrIn = buffer2bitArray(b);
|
||||
|
||||
const witness = await cir.calculateWitness({ "in": arrIn }, true);
|
||||
|
||||
const arrOut = witness.slice(1, 257);
|
||||
const hash2 = bitArray2buffer(arrOut).toString("hex");
|
||||
|
||||
assert.equal(hash, hash2);
|
||||
});
|
||||
|
||||
});
|
||||
82
circom/node_modules/circomlib/test/sign.js
generated
vendored
Normal file
82
circom/node_modules/circomlib/test/sign.js
generated
vendored
Normal file
@@ -0,0 +1,82 @@
|
||||
const path = require("path");
|
||||
const F1Field = require("ffjavascript").F1Field;
|
||||
const Scalar = require("ffjavascript").Scalar;
|
||||
exports.p = Scalar.fromString("21888242871839275222246405745257275088548364400416034343698204186575808495617");
|
||||
const Fr = new F1Field(exports.p);
|
||||
const wasm_tester = require("circom_tester").wasm;
|
||||
|
||||
function print(circuit, w, s) {
|
||||
console.log(s + ": " + w[circuit.getSignalIdx(s)]);
|
||||
}
|
||||
|
||||
function getBits(v, n) {
|
||||
const res = [];
|
||||
for (let i=0; i<n; i++) {
|
||||
if (Scalar.isOdd(Scalar.shr(v, i))) {
|
||||
res.push(Fr.one);
|
||||
} else {
|
||||
res.push(Fr.zero);
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
const q = Scalar.fromString("21888242871839275222246405745257275088548364400416034343698204186575808495617");
|
||||
|
||||
describe("Sign test", function() {
|
||||
let circuit;
|
||||
this.timeout(100000);
|
||||
|
||||
before( async() => {
|
||||
circuit = await wasm_tester(path.join(__dirname, "circuits", "sign_test.circom"));
|
||||
});
|
||||
|
||||
it("Sign of 0", async () => {
|
||||
const inp = getBits(Scalar.e(0), 254);
|
||||
const w = await circuit.calculateWitness({in: inp}, true);
|
||||
|
||||
await circuit.assertOut(w, {sign: 0});
|
||||
});
|
||||
|
||||
it("Sign of 3", async () => {
|
||||
const inp = getBits(Scalar.e(3), 254);
|
||||
const w = await circuit.calculateWitness({in: inp}, true);
|
||||
|
||||
await circuit.assertOut(w, {sign: 0});
|
||||
});
|
||||
|
||||
it("Sign of q/2", async () => {
|
||||
const inp = getBits(Scalar.shr(q, 1), 254);
|
||||
const w = await circuit.calculateWitness({in: inp}, true);
|
||||
|
||||
await circuit.assertOut(w, {sign: 0});
|
||||
});
|
||||
|
||||
it("Sign of q/2+1", async () => {
|
||||
const inp = getBits(Scalar.add(Scalar.shr(q, 1), 1) , 254);
|
||||
const w = await circuit.calculateWitness({in: inp}, true);
|
||||
|
||||
await circuit.assertOut(w, {sign: 1});
|
||||
});
|
||||
|
||||
it("Sign of q-1", async () => {
|
||||
const inp = getBits(Scalar.sub(q, 1), 254);
|
||||
const w = await circuit.calculateWitness({in: inp}, true);
|
||||
|
||||
await circuit.assertOut(w, {sign: 1});
|
||||
});
|
||||
|
||||
it("Sign of q", async () => {
|
||||
const inp = getBits(q, 254);
|
||||
const w = await circuit.calculateWitness({in: inp}, true);
|
||||
|
||||
await circuit.assertOut(w, {sign: 1});
|
||||
});
|
||||
|
||||
it("Sign of all ones", async () => {
|
||||
const inp = getBits(Scalar.sub(Scalar.shl(1,254),1), 254);
|
||||
const w = await circuit.calculateWitness({in: inp}, true);
|
||||
|
||||
await circuit.assertOut(w, {sign: 1});
|
||||
});
|
||||
});
|
||||
219
circom/node_modules/circomlib/test/smtprocessor.js
generated
vendored
Normal file
219
circom/node_modules/circomlib/test/smtprocessor.js
generated
vendored
Normal file
@@ -0,0 +1,219 @@
|
||||
const chai = require("chai");
|
||||
const path = require("path");
|
||||
const wasm_tester = require("circom_tester").wasm;
|
||||
const F1Field = require("ffjavascript").F1Field;
|
||||
const Scalar = require("ffjavascript").Scalar;
|
||||
|
||||
const newMemEmptyTrie = require("circomlibjs").newMemEmptyTrie;
|
||||
|
||||
const assert = chai.assert;
|
||||
|
||||
function print(circuit, w, s) {
|
||||
console.log(s + ": " + w[circuit.getSignalIdx(s)]);
|
||||
}
|
||||
|
||||
async function testInsert(tree, _key, _value, circuit ) {
|
||||
const key = tree.F.e(_key);
|
||||
const value = tree.F.e(_value)
|
||||
|
||||
const res = await tree.insert(key,value);
|
||||
let siblings = res.siblings;
|
||||
for (let i=0; i<siblings.length; i++) siblings[i] = tree.F.toObject(siblings[i]);
|
||||
while (siblings.length<10) siblings.push(0);
|
||||
|
||||
const w = await circuit.calculateWitness({
|
||||
fnc: [1,0],
|
||||
oldRoot: tree.F.toObject(res.oldRoot),
|
||||
siblings: siblings,
|
||||
oldKey: res.isOld0 ? 0 : tree.F.toObject(res.oldKey),
|
||||
oldValue: res.isOld0 ? 0 : tree.F.toObject(res.oldValue),
|
||||
isOld0: res.isOld0 ? 1 : 0,
|
||||
newKey: tree.F.toObject(key),
|
||||
newValue: tree.F.toObject(value)
|
||||
}, true);
|
||||
|
||||
await circuit.checkConstraints(w);
|
||||
|
||||
await circuit.assertOut(w, {newRoot: tree.F.toObject(res.newRoot)});
|
||||
|
||||
}
|
||||
|
||||
async function testDelete(tree, _key, circuit) {
|
||||
const key = tree.F.e(_key);
|
||||
const res = await tree.delete(key);
|
||||
let siblings = res.siblings;
|
||||
for (let i=0; i<siblings.length; i++) siblings[i] = tree.F.toObject(siblings[i]);
|
||||
while (siblings.length<10) siblings.push(0);
|
||||
|
||||
const w = await circuit.calculateWitness({
|
||||
fnc: [1,1],
|
||||
oldRoot: tree.F.toObject(res.oldRoot),
|
||||
siblings: siblings,
|
||||
oldKey: res.isOld0 ? 0 : tree.F.toObject(res.oldKey),
|
||||
oldValue: res.isOld0 ? 0 : tree.F.toObject(res.oldValue),
|
||||
isOld0: res.isOld0 ? 1 : 0,
|
||||
newKey: tree.F.toObject(res.delKey),
|
||||
newValue: tree.F.toObject(res.delValue)
|
||||
}, true);
|
||||
|
||||
await circuit.checkConstraints(w);
|
||||
|
||||
await circuit.assertOut(w, {newRoot: tree.F.toObject(res.newRoot)});
|
||||
}
|
||||
|
||||
async function testUpdate(tree, _key, _newValue, circuit) {
|
||||
const key = tree.F.e(_key);
|
||||
const newValue = tree.F.e(_newValue);
|
||||
const res = await tree.update(key, newValue);
|
||||
let siblings = res.siblings;
|
||||
for (let i=0; i<siblings.length; i++) siblings[i] = tree.F.toObject(siblings[i]);
|
||||
while (siblings.length<10) siblings.push(0);
|
||||
|
||||
const w = await circuit.calculateWitness({
|
||||
fnc: [0,1],
|
||||
oldRoot: tree.F.toObject(res.oldRoot),
|
||||
siblings: siblings,
|
||||
oldKey: tree.F.toObject(res.oldKey),
|
||||
oldValue: tree.F.toObject(res.oldValue),
|
||||
isOld0: 0,
|
||||
newKey: tree.F.toObject(res.newKey),
|
||||
newValue: tree.F.toObject(res.newValue)
|
||||
});
|
||||
|
||||
await circuit.checkConstraints(w);
|
||||
|
||||
await circuit.assertOut(w, {newRoot: tree.F.toObject(res.newRoot)});
|
||||
}
|
||||
|
||||
|
||||
describe("SMT Processor test", function () {
|
||||
let circuit;
|
||||
let tree;
|
||||
let Fr;
|
||||
|
||||
this.timeout(1000000000);
|
||||
|
||||
before( async () => {
|
||||
circuit = await wasm_tester(path.join(__dirname, "circuits", "smtprocessor10_test.circom"));
|
||||
await circuit.loadSymbols();
|
||||
|
||||
tree = await newMemEmptyTrie();
|
||||
Fr = tree.F;
|
||||
});
|
||||
|
||||
it("Should verify an insert to an empty tree", async () => {
|
||||
const key = Fr.e(111);
|
||||
const value = Fr.e(222);
|
||||
|
||||
await testInsert(tree, key, value, circuit);
|
||||
});
|
||||
|
||||
it("It should add another element", async () => {
|
||||
const key = Fr.e(333);
|
||||
const value = Fr.e(444);
|
||||
|
||||
await testInsert(tree, key, value, circuit);
|
||||
});
|
||||
|
||||
it("Should remove an element", async () => {
|
||||
await testDelete(tree, 111, circuit);
|
||||
await testDelete(tree, 333, circuit);
|
||||
});
|
||||
|
||||
it("Should test convination of adding and removing 3 elements", async () => {
|
||||
const keys = [Fr.e(8), Fr.e(9), Fr.e(32)];
|
||||
const values = [Fr.e(88), Fr.e(99), Fr.e(3232)];
|
||||
const tree1 = await newMemEmptyTrie();
|
||||
const tree2 = await newMemEmptyTrie();
|
||||
const tree3 = await newMemEmptyTrie();
|
||||
const tree4 = await newMemEmptyTrie();
|
||||
const tree5 = await newMemEmptyTrie();
|
||||
const tree6 = await newMemEmptyTrie();
|
||||
|
||||
await testInsert(tree1,keys[0],values[0], circuit);
|
||||
await testInsert(tree1,keys[1],values[1], circuit);
|
||||
await testInsert(tree1,keys[2],values[2], circuit);
|
||||
|
||||
await testInsert(tree2,keys[0],values[0], circuit);
|
||||
await testInsert(tree2,keys[2],values[2], circuit);
|
||||
await testInsert(tree2,keys[1],values[1], circuit);
|
||||
|
||||
await testInsert(tree3,keys[1],values[1], circuit);
|
||||
await testInsert(tree3,keys[0],values[0], circuit);
|
||||
await testInsert(tree3,keys[2],values[2], circuit);
|
||||
|
||||
await testInsert(tree4,keys[1],values[1], circuit);
|
||||
await testInsert(tree4,keys[2],values[2], circuit);
|
||||
await testInsert(tree4,keys[0],values[0], circuit);
|
||||
|
||||
await testInsert(tree5,keys[2],values[2], circuit);
|
||||
await testInsert(tree5,keys[0],values[0], circuit);
|
||||
await testInsert(tree5,keys[1],values[1], circuit);
|
||||
|
||||
await testInsert(tree6,keys[2],values[2], circuit);
|
||||
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);
|
||||
await testDelete(tree2, keys[0], circuit);
|
||||
|
||||
await testDelete(tree3, keys[0], circuit);
|
||||
await testDelete(tree3, keys[2], circuit);
|
||||
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);
|
||||
await testDelete(tree6, keys[1], circuit);
|
||||
|
||||
await testDelete(tree1, keys[2], circuit);
|
||||
await testDelete(tree2, keys[2], circuit);
|
||||
await testDelete(tree3, keys[1], circuit);
|
||||
await testDelete(tree4, keys[1], circuit);
|
||||
await testDelete(tree5, keys[0], circuit);
|
||||
await testDelete(tree6, keys[0], circuit);
|
||||
});
|
||||
|
||||
it("Should match a NOp with random vals", async () => {
|
||||
let siblings = [];
|
||||
while (siblings.length<10) siblings.push(88);
|
||||
const w = await circuit.calculateWitness({
|
||||
fnc: [0,0],
|
||||
oldRoot: 11,
|
||||
siblings: siblings,
|
||||
oldKey: 33,
|
||||
oldValue: 44,
|
||||
isOld0: 55,
|
||||
newKey: 66,
|
||||
newValue: 77
|
||||
});
|
||||
|
||||
const root1 = Fr.e(w[circuit.symbols["main.oldRoot"].varIdx]);
|
||||
const root2 = Fr.e(w[circuit.symbols["main.newRoot"].varIdx]);
|
||||
|
||||
await circuit.checkConstraints(w);
|
||||
|
||||
assert(Fr.eq(root1, root2));
|
||||
});
|
||||
it("Should update an element", async () => {
|
||||
const tree1 = await newMemEmptyTrie();
|
||||
const tree2 = await newMemEmptyTrie();
|
||||
|
||||
await testInsert(tree1,8,88, circuit);
|
||||
await testInsert(tree1,9,99, circuit);
|
||||
await testInsert(tree1,32,3232, circuit);
|
||||
|
||||
await testInsert(tree2,8,888, circuit);
|
||||
await testInsert(tree2,9,999, circuit);
|
||||
await testInsert(tree2,32,323232, circuit);
|
||||
|
||||
await testUpdate(tree1, 8, 888, circuit);
|
||||
await testUpdate(tree1, 9, 999, circuit);
|
||||
await testUpdate(tree1, 32, 323232, circuit);
|
||||
});
|
||||
});
|
||||
141
circom/node_modules/circomlib/test/smtverifier.js
generated
vendored
Normal file
141
circom/node_modules/circomlib/test/smtverifier.js
generated
vendored
Normal file
@@ -0,0 +1,141 @@
|
||||
const chai = require("chai");
|
||||
const path = require("path");
|
||||
const Scalar = require("ffjavascript").Scalar;
|
||||
const wasm_tester = require("circom_tester").wasm;
|
||||
|
||||
const newMemEmptyTrie = require("circomlibjs").newMemEmptyTrie;
|
||||
|
||||
const assert = chai.assert;
|
||||
|
||||
function print(circuit, w, s) {
|
||||
console.log(s + ": " + w[circuit.getSignalIdx(s)]);
|
||||
}
|
||||
|
||||
async function testInclusion(tree, _key, circuit) {
|
||||
const key = tree.F.e(_key);
|
||||
const res = await tree.find(key);
|
||||
|
||||
assert(res.found);
|
||||
let siblings = res.siblings;
|
||||
for (let i=0; i<siblings.length; i++) siblings[i] = tree.F.toObject(siblings[i]);
|
||||
while (siblings.length<10) siblings.push(0);
|
||||
|
||||
const w = await circuit.calculateWitness({
|
||||
enabled: 1,
|
||||
fnc: 0,
|
||||
root: tree.F.toObject(tree.root),
|
||||
siblings: siblings,
|
||||
oldKey: 0,
|
||||
oldValue: 0,
|
||||
isOld0: 0,
|
||||
key: tree.F.toObject(key),
|
||||
value: tree.F.toObject(res.foundValue)
|
||||
}, true);
|
||||
|
||||
await circuit.checkConstraints(w);
|
||||
|
||||
}
|
||||
|
||||
async function testExclusion(tree, _key, circuit) {
|
||||
const key = tree.F.e(_key);
|
||||
const res = await tree.find(key);
|
||||
|
||||
assert(!res.found);
|
||||
let siblings = res.siblings;
|
||||
for (let i=0; i<siblings.length; i++) siblings[i] = tree.F.toObject(siblings[i]);
|
||||
while (siblings.length<10) siblings.push(0);
|
||||
|
||||
const w = await circuit.calculateWitness({
|
||||
enabled: 1,
|
||||
fnc: 1,
|
||||
root: tree.F.toObject(tree.root),
|
||||
siblings: siblings,
|
||||
oldKey: res.isOld0 ? 0 : tree.F.toObject(res.notFoundKey),
|
||||
oldValue: res.isOld0 ? 0 : tree.F.toObject(res.notFoundValue),
|
||||
isOld0: res.isOld0 ? 1 : 0,
|
||||
key: tree.F.toObject(key),
|
||||
value: 0
|
||||
});
|
||||
|
||||
await circuit.checkConstraints(w);
|
||||
|
||||
}
|
||||
|
||||
describe("SMT Verifier test", function () {
|
||||
let Fr;
|
||||
let circuit;
|
||||
let tree;
|
||||
|
||||
this.timeout(100000);
|
||||
|
||||
before( async () => {
|
||||
circuit = await wasm_tester(path.join(__dirname, "circuits", "smtverifier10_test.circom"));
|
||||
|
||||
tree = await newMemEmptyTrie();
|
||||
Fr = tree.F;
|
||||
await tree.insert(7,77);
|
||||
await tree.insert(8,88);
|
||||
await tree.insert(32,3232);
|
||||
});
|
||||
|
||||
it("Check inclussion in a tree of 3", async () => {
|
||||
await testInclusion(tree, 7, circuit);
|
||||
await testInclusion(tree, 8, circuit);
|
||||
await testInclusion(tree, 32, circuit);
|
||||
});
|
||||
|
||||
it("Check exclussion in a tree of 3", async () => {
|
||||
await testExclusion(tree, 0, circuit);
|
||||
await testExclusion(tree, 6, circuit);
|
||||
await testExclusion(tree, 9, circuit);
|
||||
await testExclusion(tree, 33, circuit);
|
||||
await testExclusion(tree, 31, circuit);
|
||||
await testExclusion(tree, 16, circuit);
|
||||
await testExclusion(tree, 64, circuit);
|
||||
});
|
||||
|
||||
it("Check not enabled accepts any thing", async () => {
|
||||
let siblings = [];
|
||||
for (let i=0; i<10; i++) siblings.push(i);
|
||||
|
||||
const w = await circuit.calculateWitness({
|
||||
enabled: 0,
|
||||
fnc: 0,
|
||||
root: 1,
|
||||
siblings: siblings,
|
||||
oldKey: 22,
|
||||
oldValue: 33,
|
||||
isOld0: 0,
|
||||
key: 44,
|
||||
value: 0
|
||||
});
|
||||
|
||||
|
||||
await circuit.checkConstraints(w);
|
||||
});
|
||||
|
||||
it("Check inclussion Adria case", async () => {
|
||||
const e1_hi= Fr.e("17124152697573569611556136390143205198134245887034837071647643529178599000839");
|
||||
const e1_hv= Fr.e("19650379996168153643111744440707177573540245771926102415571667548153444658179");
|
||||
|
||||
const e2ok_hi= Fr.e("16498254692537945203721083102154618658340563351558973077349594629411025251262");
|
||||
const e2ok_hv= Fr.e("19650379996168153643111744440707177573540245771926102415571667548153444658179");
|
||||
|
||||
const e2fail_hi= Fr.e("17195092312975762537892237130737365903429674363577646686847513978084990105579");
|
||||
const e2fail_hv= Fr.e("19650379996168153643111744440707177573540245771926102415571667548153444658179");
|
||||
|
||||
const tree1 = await newMemEmptyTrie();
|
||||
await tree1.insert(e1_hi,e1_hv);
|
||||
await tree1.insert(e2ok_hi,e2ok_hv);
|
||||
|
||||
await testInclusion(tree1, e2ok_hi, circuit);
|
||||
|
||||
const tree2 = await newMemEmptyTrie();
|
||||
await tree2.insert(e1_hi,e1_hv);
|
||||
await tree2.insert(e2fail_hi,e2fail_hv);
|
||||
|
||||
await testInclusion(tree2, e2fail_hi, circuit);
|
||||
});
|
||||
|
||||
|
||||
});
|
||||
Reference in New Issue
Block a user