2018-10-21 20:51:38 +03:00
|
|
|
const chai = require("chai");
|
|
|
|
const path = require("path");
|
2018-11-11 21:52:07 +03:00
|
|
|
const snarkjs = require("snarkjs");
|
2018-10-21 20:51:38 +03:00
|
|
|
const compiler = require("circom");
|
|
|
|
|
|
|
|
const assert = chai.assert;
|
|
|
|
|
2018-11-27 18:03:57 +03:00
|
|
|
const bigInt = snarkjs.bigInt;
|
2018-10-21 20:51:38 +03:00
|
|
|
|
|
|
|
|
|
|
|
describe("Mux4 test", () => {
|
2019-07-08 15:08:04 +03:00
|
|
|
it("Should create a constant multiplexer 4", async () => {
|
2018-10-21 20:51:38 +03:00
|
|
|
|
|
|
|
const cirDef = await compiler(path.join(__dirname, "circuits", "mux4_1.circom"));
|
|
|
|
|
|
|
|
// console.log(JSON.stringify(cirDef, null, 1));
|
|
|
|
|
|
|
|
// assert.equal(cirDef.nVars, 2);
|
|
|
|
|
2018-11-11 21:52:07 +03:00
|
|
|
const circuit = new snarkjs.Circuit(cirDef);
|
2018-10-21 20:51:38 +03:00
|
|
|
|
2018-11-27 18:03:57 +03:00
|
|
|
console.log("NConstrains Mux4: " + circuit.nConstraints);
|
2018-10-21 20:51:38 +03:00
|
|
|
|
2018-11-27 18:03:57 +03:00
|
|
|
const ct16 = [
|
|
|
|
bigInt("123"),
|
|
|
|
bigInt("456"),
|
|
|
|
bigInt("789"),
|
|
|
|
bigInt("012"),
|
|
|
|
bigInt("111"),
|
|
|
|
bigInt("222"),
|
|
|
|
bigInt("333"),
|
|
|
|
bigInt("4546"),
|
|
|
|
bigInt("134523"),
|
|
|
|
bigInt("44356"),
|
|
|
|
bigInt("15623"),
|
|
|
|
bigInt("4566"),
|
|
|
|
bigInt("1223"),
|
|
|
|
bigInt("4546"),
|
|
|
|
bigInt("4256"),
|
|
|
|
bigInt("4456")
|
|
|
|
];
|
2018-10-21 20:51:38 +03:00
|
|
|
|
2018-11-27 18:03:57 +03:00
|
|
|
for (let i=0; i<16; i++) {
|
|
|
|
const w = circuit.calculateWitness({ "selector": i });
|
2018-10-21 20:51:38 +03:00
|
|
|
|
2019-12-04 23:57:02 +03:00
|
|
|
assert(circuit.checkWitness(w));
|
|
|
|
|
2018-11-27 18:03:57 +03:00
|
|
|
assert(w[0].equals(bigInt(1)));
|
|
|
|
|
|
|
|
// console.log(i + " -> " + w[circuit.getSignalIdx("main.out")].toString());
|
|
|
|
assert(w[circuit.getSignalIdx("main.out")].equals(ct16[i]));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-07-08 15:08:04 +03:00
|
|
|
it("Should create a constant multiplexer 3", async () => {
|
2018-11-27 18:03:57 +03:00
|
|
|
|
|
|
|
const cirDef = await compiler(path.join(__dirname, "circuits", "mux3_1.circom"));
|
|
|
|
|
|
|
|
const circuit = new snarkjs.Circuit(cirDef);
|
|
|
|
|
|
|
|
console.log("NConstrains Mux3: " + circuit.nConstraints);
|
|
|
|
|
|
|
|
const ct8 = [
|
|
|
|
bigInt("37"),
|
|
|
|
bigInt("47"),
|
|
|
|
bigInt("53"),
|
|
|
|
bigInt("71"),
|
|
|
|
bigInt("89"),
|
|
|
|
bigInt("107"),
|
|
|
|
bigInt("163"),
|
|
|
|
bigInt("191")
|
|
|
|
];
|
|
|
|
|
|
|
|
for (let i=0; i<8; i++) {
|
|
|
|
const w = circuit.calculateWitness({ "selector": i });
|
|
|
|
|
|
|
|
assert(w[0].equals(bigInt(1)));
|
|
|
|
|
2019-07-08 15:08:04 +03:00
|
|
|
// console.log(i + " -> " + w[circuit.getSignalIdx("main.out")].toString());
|
|
|
|
assert(w[circuit.getSignalIdx("main.out")].equals(ct8[i]));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
it("Should create a constant multiplexer 2", async () => {
|
|
|
|
|
|
|
|
const cirDef = await compiler(path.join(__dirname, "circuits", "mux2_1.circom"));
|
|
|
|
|
|
|
|
const circuit = new snarkjs.Circuit(cirDef);
|
|
|
|
|
|
|
|
console.log("NConstrains Mux2: " + circuit.nConstraints);
|
|
|
|
|
|
|
|
const ct8 = [
|
|
|
|
bigInt("37"),
|
|
|
|
bigInt("47"),
|
|
|
|
bigInt("53"),
|
|
|
|
bigInt("71"),
|
|
|
|
];
|
|
|
|
|
|
|
|
for (let i=0; i<4; i++) {
|
|
|
|
const w = circuit.calculateWitness({ "selector": i });
|
|
|
|
|
2019-12-04 23:57:02 +03:00
|
|
|
assert(circuit.checkWitness(w));
|
|
|
|
|
2019-07-08 15:08:04 +03:00
|
|
|
assert(w[0].equals(bigInt(1)));
|
|
|
|
|
|
|
|
// console.log(i + " -> " + w[circuit.getSignalIdx("main.out")].toString());
|
|
|
|
assert(w[circuit.getSignalIdx("main.out")].equals(ct8[i]));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
it("Should create a constant multiplexer 1", async () => {
|
|
|
|
|
|
|
|
const cirDef = await compiler(path.join(__dirname, "circuits", "mux1_1.circom"));
|
|
|
|
|
|
|
|
const circuit = new snarkjs.Circuit(cirDef);
|
|
|
|
|
|
|
|
console.log("NConstrains Mux1: " + circuit.nConstraints);
|
|
|
|
|
|
|
|
const ct8 = [
|
|
|
|
bigInt("37"),
|
|
|
|
bigInt("47"),
|
|
|
|
];
|
|
|
|
|
|
|
|
for (let i=0; i<2; i++) {
|
|
|
|
const w = circuit.calculateWitness({ "selector": i });
|
|
|
|
|
2019-12-04 23:57:02 +03:00
|
|
|
assert(circuit.checkWitness(w));
|
|
|
|
|
2019-07-08 15:08:04 +03:00
|
|
|
assert(w[0].equals(bigInt(1)));
|
|
|
|
|
2018-11-27 18:03:57 +03:00
|
|
|
// console.log(i + " -> " + w[circuit.getSignalIdx("main.out")].toString());
|
|
|
|
assert(w[circuit.getSignalIdx("main.out")].equals(ct8[i]));
|
2018-10-21 20:51:38 +03:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|