arnaucube
arnaucube/circomlibPublic Self-hosted repository
circomlib/test/escalarmulfix.js
test/escalarmulfix.jsRaw
  1const chai = require("chai");
  2const path = require("path");
  3const snarkjs = require("snarkjs");
  4const compiler = require("circom");
  5const babyjub = require("../src/babyjub");
  6
  7const assert = chai.assert;
  8
  9const bigInt = snarkjs.bigInt;
 10
 11
 12function print(circuit, w, s) {
 13    console.log(s + ": " + w[circuit.getSignalIdx(s)]);
 14}
 15
 16describe("Escalarmul test", function () {
 17    let circuit;
 18
 19    this.timeout(100000);
 20
 21    before( async() => {
 22        const cirDef = await compiler(path.join(__dirname, "circuits", "escalarmulfix_test.circom"));
 23        circuit = new snarkjs.Circuit(cirDef);
 24        console.log("NConstrains Escalarmul fix: " + circuit.nConstraints);
 25    });
 26
 27    it("Should generate Same escalar mul", async () => {
 28
 29        const w = circuit.calculateWitness({"e": 0});
 30
 31        assert(circuit.checkWitness(w));
 32
 33        const xout = w[circuit.getSignalIdx("main.out[0]")];
 34        const yout = w[circuit.getSignalIdx("main.out[1]")];
 35
 36        assert(xout.equals(0));
 37        assert(yout.equals(1));
 38    });
 39
 40    it("Should generate Same escalar mul", async () => {
 41
 42        const w = circuit.calculateWitness({"e": 1});
 43
 44        assert(circuit.checkWitness(w));
 45
 46        const xout = w[circuit.getSignalIdx("main.out[0]")];
 47        const yout = w[circuit.getSignalIdx("main.out[1]")];
 48
 49        assert(xout.equals(babyjub.Base8[0]));
 50        assert(yout.equals(babyjub.Base8[1]));
 51    });
 52
 53    it("Should generate scalar mul of a specific constant", async () => {
 54
 55        const s = bigInt("2351960337287830298912035165133676222414898052661454064215017316447594616519");
 56        const base8 = [
 57            bigInt("17777552123799933955779906779655732241715742912184938656739573121738514868268"),
 58            bigInt("2626589144620713026669568689430873010625803728049924121243784502389097019475")
 59        ];
 60
 61        const w = circuit.calculateWitness({"e": s});
 62
 63        assert(circuit.checkWitness(w));
 64
 65        const xout = w[circuit.getSignalIdx("main.out[0]")];
 66        const yout = w[circuit.getSignalIdx("main.out[1]")];
 67
 68        const expectedRes = babyjub.mulPointEscalar(base8, s);
 69
 70        assert(xout.equals(expectedRes[0]));
 71        assert(yout.equals(expectedRes[1]));
 72    });
 73
 74    it("Should generate scalar mul of the firsts 50 elements", async () => {
 75
 76        const base8 = [
 77            bigInt("17777552123799933955779906779655732241715742912184938656739573121738514868268"),
 78            bigInt("2626589144620713026669568689430873010625803728049924121243784502389097019475")
 79        ];
 80
 81        for (let i=0; i<50; i++) {
 82            const s = bigInt(i);
 83
 84            const w = circuit.calculateWitness({"e": s});
 85
 86            assert(circuit.checkWitness(w));
 87
 88            const xout = w[circuit.getSignalIdx("main.out[0]")];
 89            const yout = w[circuit.getSignalIdx("main.out[1]")];
 90
 91            const expectedRes = babyjub.mulPointEscalar(base8, s);
 92
 93            assert(xout.equals(expectedRes[0]));
 94            assert(yout.equals(expectedRes[1]));
 95        }
 96    });
 97
 98    it("If multiply by order should return 0", async () => {
 99
100        const w = circuit.calculateWitness({"e": babyjub.subOrder });
101
102        assert(circuit.checkWitness(w));
103
104        const xout = w[circuit.getSignalIdx("main.out[0]")];
105        const yout = w[circuit.getSignalIdx("main.out[1]")];
106
107        assert(xout.equals(bigInt.zero));
108        assert(yout.equals(bigInt.one));
109    });
110
111});
112