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 4", 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 3", 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    it("Should create a constant multiplexer 2", async () => {
 82
 83        const cirDef = await compiler(path.join(__dirname, "circuits", "mux2_1.circom"));
 84
 85        const circuit = new snarkjs.Circuit(cirDef);
 86
 87        console.log("NConstrains Mux2: " + circuit.nConstraints);
 88
 89        const ct8 = [
 90            bigInt("37"),
 91            bigInt("47"),
 92            bigInt("53"),
 93            bigInt("71"),
 94        ];
 95
 96        for (let i=0; i<4; i++) {
 97            const w = circuit.calculateWitness({ "selector": i });
 98
 99            assert(w[0].equals(bigInt(1)));
100
101            // console.log(i + " -> " + w[circuit.getSignalIdx("main.out")].toString());
102            assert(w[circuit.getSignalIdx("main.out")].equals(ct8[i]));
103        }
104    });
105    it("Should create a constant multiplexer 1", async () => {
106
107        const cirDef = await compiler(path.join(__dirname, "circuits", "mux1_1.circom"));
108
109        const circuit = new snarkjs.Circuit(cirDef);
110
111        console.log("NConstrains Mux1: " + circuit.nConstraints);
112
113        const ct8 = [
114            bigInt("37"),
115            bigInt("47"),
116        ];
117
118        for (let i=0; i<2; i++) {
119            const w = circuit.calculateWitness({ "selector": i });
120
121            assert(w[0].equals(bigInt(1)));
122
123            // console.log(i + " -> " + w[circuit.getSignalIdx("main.out")].toString());
124            assert(w[circuit.getSignalIdx("main.out")].equals(ct8[i]));
125        }
126    });
127});