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        await circuit.checkConstraints(w);
 24
 25        let g = [
 26            bigInt("5299619240641551281634865583518297030282874472190772894086521144482721001553"),
 27            bigInt("16950150798460657717958625567821834550301663161624707787222815936182638968203")
 28        ];
 29
 30        let dbl= [bigInt("0"), bigInt("1")];
 31
 32        const expectedOut = [];
 33
 34        for (let i=0; i<16; i++) {
 35
 36            expectedOut.push(dbl);
 37            dbl = babyJub.addPoint(dbl,g);
 38        }
 39
 40        await circuit.assertOut(w, {out: expectedOut});
 41
 42    });
 43
 44    it("Should generate the Exponentiation table in k=3", async () => {
 45
 46        const circuit = await tester(path.join(__dirname, "circuits", "escalarmulw4table_test3.circom"));
 47
 48        const w = await circuit.calculateWitness({in: 1});
 49
 50        await circuit.checkConstraints(w);
 51
 52        let g = [
 53            bigInt("5299619240641551281634865583518297030282874472190772894086521144482721001553"),
 54            bigInt("16950150798460657717958625567821834550301663161624707787222815936182638968203")
 55        ];
 56
 57        for (let i=0; i<12;i++) {
 58            g = babyJub.addPoint(g,g);
 59        }
 60
 61        let dbl= [bigInt("0"), bigInt("1")];
 62
 63        const expectedOut = [];
 64
 65        for (let i=0; i<16; i++) {
 66            expectedOut.push(dbl);
 67
 68            dbl = babyJub.addPoint(dbl,g);
 69        }
 70
 71        await circuit.assertOut(w, {out: expectedOut});
 72
 73    });
 74
 75    it("Should exponentiate g^31", async () => {
 76
 77        const circuit = await tester(path.join(__dirname, "circuits", "escalarmul_test.circom"));
 78
 79        const w = await circuit.calculateWitness({"in": 31});
 80
 81        await circuit.checkConstraints(w);
 82
 83        let g = [
 84            bigInt("5299619240641551281634865583518297030282874472190772894086521144482721001553"),
 85            bigInt("16950150798460657717958625567821834550301663161624707787222815936182638968203")
 86        ];
 87
 88        let c = [bigInt(0), bigInt(1)];
 89
 90        for (let i=0; i<31;i++) {
 91            c = babyJub.addPoint(c,g);
 92        }
 93
 94        await circuit.assertOut(w, {out: c});
 95
 96        const w2 = await circuit.calculateWitness({"in": bigInt(1).shiftLeft(252).add(bigInt.one)});
 97
 98        c = [g[0], g[1]];
 99        for (let i=0; i<252;i++) {
100            c = babyJub.addPoint(c,c);
101        }
102        c = babyJub.addPoint(c,g);
103
104        await circuit.assertOut(w2, {out: c});
105
106    }).timeout(10000000);
107
108    it("Number of constrains for 256 bits", async () => {
109
110        const circuit = await tester(path.join(__dirname, "circuits", "escalarmul_test_min.circom"));
111
112    }).timeout(10000000);
113
114});