arnaucube
arnaucube/circomlibPublic Self-hosted repository
circomlib/test/escalarmul.js
test/escalarmul.jsRaw
  1const chai = require("chai");
  2const path = require("path");
  3const bigInt = require("big-integer");
  4const tester = require("circom").tester;
  5const babyJub = require("../src/babyjub.js");
  6
  7const assert = chai.assert;
  8
  9function print(circuit, w, s) {
 10    console.log(s + ": " + w[circuit.getSignalIdx(s)]);
 11}
 12
 13describe("Exponentioation test", function () {
 14
 15    this.timeout(100000);
 16
 17    it("Should generate the Exponentiation table in k=0", async () => {
 18
 19        const circuit = await tester(path.join(__dirname, "circuits", "escalarmulw4table_test.circom"));
 20
 21        const w = await circuit.calculateWitness({in: 1});
 22
 23        // TODO
 24        // assert(circuit.checkWitness(w));
 25
 26        let g = [
 27            bigInt("5299619240641551281634865583518297030282874472190772894086521144482721001553"),
 28            bigInt("16950150798460657717958625567821834550301663161624707787222815936182638968203")
 29        ];
 30
 31        let dbl= [bigInt("0"), bigInt("1")];
 32
 33        const expectedOut = [];
 34
 35        for (let i=0; i<16; i++) {
 36
 37            expectedOut.push(dbl);
 38            dbl = babyJub.addPoint(dbl,g);
 39        }
 40
 41        await circuit.assertOut(w, {out: expectedOut});
 42
 43    });
 44
 45    it("Should generate the Exponentiation table in k=3", async () => {
 46
 47        const circuit = await tester(path.join(__dirname, "circuits", "escalarmulw4table_test3.circom"));
 48
 49        const w = await circuit.calculateWitness({in: 1});
 50
 51        // TODO
 52        // assert(circuit.checkWitness(w));
 53
 54        let g = [
 55            bigInt("5299619240641551281634865583518297030282874472190772894086521144482721001553"),
 56            bigInt("16950150798460657717958625567821834550301663161624707787222815936182638968203")
 57        ];
 58
 59        for (let i=0; i<12;i++) {
 60            g = babyJub.addPoint(g,g);
 61        }
 62
 63        let dbl= [bigInt("0"), bigInt("1")];
 64
 65        const expectedOut = [];
 66
 67        for (let i=0; i<16; i++) {
 68            expectedOut.push(dbl);
 69
 70            dbl = babyJub.addPoint(dbl,g);
 71        }
 72
 73        await circuit.assertOut(w, {out: expectedOut});
 74
 75    });
 76
 77    it("Should exponentiate g^31", async () => {
 78
 79        const circuit = await tester(path.join(__dirname, "circuits", "escalarmul_test.circom"));
 80
 81        const w = await circuit.calculateWitness({"in": 31});
 82
 83        // TODO
 84        // assert(circuit.checkWitness(w));
 85
 86        let g = [
 87            bigInt("5299619240641551281634865583518297030282874472190772894086521144482721001553"),
 88            bigInt("16950150798460657717958625567821834550301663161624707787222815936182638968203")
 89        ];
 90
 91        let c = [bigInt(0), bigInt(1)];
 92
 93        for (let i=0; i<31;i++) {
 94            c = babyJub.addPoint(c,g);
 95        }
 96
 97        await circuit.assertOut(w, {out: c});
 98
 99        const w2 = await circuit.calculateWitness({"in": bigInt(1).shiftLeft(252).add(bigInt.one)});
100
101        c = [g[0], g[1]];
102        for (let i=0; i<252;i++) {
103            c = babyJub.addPoint(c,c);
104        }
105        c = babyJub.addPoint(c,g);
106
107        await circuit.assertOut(w2, {out: c});
108
109    }).timeout(10000000);
110
111    it("Number of constrains for 256 bits", async () => {
112
113        const circuit = await tester(path.join(__dirname, "circuits", "escalarmul_test_min.circom"));
114
115    }).timeout(10000000);
116
117});