arnaucube
arnaucube/circomlibPublic Self-hosted repository
circomlib/test/multiplexer.js
test/multiplexer.jsRaw
 1const chai = require("chai");
 2const path = require("path");
 3const snarkjs = require("snarkjs");
 4const compiler = require("circom");
 5
 6const assert = chai.assert;
 7
 8const bigInt = snarkjs.bigInt;
 9
10
11describe("Mux4 test", () => {
12    it("Should create a constant multiplexer", async () => {
13
14        const cirDef = await compiler(path.join(__dirname, "circuits", "mux4_1.circom"));
15
16//        console.log(JSON.stringify(cirDef, null, 1));
17
18//        assert.equal(cirDef.nVars, 2);
19
20        const circuit = new snarkjs.Circuit(cirDef);
21
22        console.log("NConstrains Mux4: " + circuit.nConstraints);
23
24        const ct16 = [
25            bigInt("123"),
26            bigInt("456"),
27            bigInt("789"),
28            bigInt("012"),
29            bigInt("111"),
30            bigInt("222"),
31            bigInt("333"),
32            bigInt("4546"),
33            bigInt("134523"),
34            bigInt("44356"),
35            bigInt("15623"),
36            bigInt("4566"),
37            bigInt("1223"),
38            bigInt("4546"),
39            bigInt("4256"),
40            bigInt("4456")
41        ];
42
43        for (let i=0; i<16; i++) {
44            const w = circuit.calculateWitness({ "selector": i });
45
46            assert(w[0].equals(bigInt(1)));
47
48            // console.log(i + " -> " + w[circuit.getSignalIdx("main.out")].toString());
49            assert(w[circuit.getSignalIdx("main.out")].equals(ct16[i]));
50        }
51    });
52
53    it("Should create a constant multiplexer", async () => {
54
55        const cirDef = await compiler(path.join(__dirname, "circuits", "mux3_1.circom"));
56
57        const circuit = new snarkjs.Circuit(cirDef);
58
59        console.log("NConstrains Mux3: " + circuit.nConstraints);
60
61        const ct8 = [
62            bigInt("37"),
63            bigInt("47"),
64            bigInt("53"),
65            bigInt("71"),
66            bigInt("89"),
67            bigInt("107"),
68            bigInt("163"),
69            bigInt("191")
70        ];
71
72        for (let i=0; i<8; i++) {
73            const w = circuit.calculateWitness({ "selector": i });
74
75            assert(w[0].equals(bigInt(1)));
76
77            // console.log(i + " -> " + w[circuit.getSignalIdx("main.out")].toString());
78            assert(w[circuit.getSignalIdx("main.out")].equals(ct8[i]));
79        }
80    });
81});