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