arnaucube
arnaucube/snarkjsPublic Self-hosted repository
snarkjs/test/algebra.js
test/algebra.jsRaw
  1const chai = require("chai");
  2
  3const bigInt = require("big-integer");
  4const BN128 = require("../src/BN128.js");
  5
  6const assert = chai.assert;
  7
  8describe("Curve G1 Test", () => {
  9    it("r*one == 0", () => {
 10        const bn128 = new BN128();
 11
 12        const res = bn128.G1.mulEscalar(bn128.G1.g, bn128.r);
 13
 14        assert(bn128.G1.equals(res, bn128.G1.zero), "G1 does not have range r");
 15    });
 16
 17    it("Should add match in various in G1", () => {
 18
 19        const bn128 = new BN128();
 20
 21        const r1 = bigInt(33);
 22        const r2 = bigInt(44);
 23
 24        const gr1 = bn128.G1.mulEscalar(bn128.G1.g, r1);
 25        const gr2 = bn128.G1.mulEscalar(bn128.G1.g, r2);
 26
 27        const grsum1 = bn128.G1.add(gr1, gr2);
 28
 29        const grsum2 = bn128.G1.mulEscalar(bn128.G1.g, r1.add(r2));
 30
 31        assert(bn128.G1.equals(grsum1, grsum2));
 32    });
 33});
 34
 35describe("Curve G2 Test", () => {
 36    it ("r*one == 0", () => {
 37        const bn128 = new BN128();
 38
 39        const res = bn128.G2.mulEscalar(bn128.G2.g, bn128.r);
 40
 41        assert(bn128.G2.equals(res, bn128.G2.zero), "G2 does not have range r");
 42    });
 43
 44    it("Should add match in various in G2", () => {
 45        const bn128 = new BN128();
 46
 47        const r1 = bigInt(33);
 48        const r2 = bigInt(44);
 49
 50        const gr1 = bn128.G2.mulEscalar(bn128.G2.g, r1);
 51        const gr2 = bn128.G2.mulEscalar(bn128.G2.g, r2);
 52
 53        const grsum1 = bn128.G2.add(gr1, gr2);
 54
 55        const grsum2 = bn128.G2.mulEscalar(bn128.G2.g, r1.add(r2));
 56
 57        /*
 58        console.log(G2.toString(grsum1));
 59        console.log(G2.toString(grsum2));
 60        */
 61
 62        assert(bn128.G2.equals(grsum1, grsum2));
 63    });
 64});
 65
 66describe("F6 testing", () => {
 67    it("Should multiply and divide in F6", () => {
 68        const bn128 = new BN128();
 69        const a =
 70            [
 71                [bigInt("1"), bigInt("2")],
 72                [bigInt("3"), bigInt("4")],
 73                [bigInt("5"), bigInt("6")]
 74            ];
 75        const b =
 76            [
 77                [bigInt("12"), bigInt("11")],
 78                [bigInt("10"), bigInt("9")],
 79                [bigInt("8"), bigInt("7")]
 80            ];
 81        const c = bn128.F6.mul(a,b);
 82        const d = bn128.F6.div(c,b);
 83
 84        assert(bn128.F6.equals(a, d));
 85    });
 86});
 87
 88describe("F12 testing", () => {
 89    it("Should multiply and divide in F12", () => {
 90        const bn128 = new BN128();
 91        const a =
 92        [
 93            [
 94                [bigInt("1"), bigInt("2")],
 95                [bigInt("3"), bigInt("4")],
 96                [bigInt("5"), bigInt("6")]
 97            ],
 98            [
 99                [bigInt("7"), bigInt("8")],
100                [bigInt("9"), bigInt("10")],
101                [bigInt("11"), bigInt("12")]
102            ]
103        ];
104        const b =
105        [
106            [
107                [bigInt("12"), bigInt("11")],
108                [bigInt("10"), bigInt("9")],
109                [bigInt("8"), bigInt("7")]
110            ],
111            [
112                [bigInt("6"), bigInt("5")],
113                [bigInt("4"), bigInt("3")],
114                [bigInt("2"), bigInt("1")]
115            ]
116        ];
117        const c = bn128.F12.mul(a,b);
118        const d = bn128.F12.div(c,b);
119
120        assert(bn128.F12.equals(a, d));
121    });
122});
123
124describe("Pairing", () => {
125    it("Should match pairing", () => {
126        const bn128 = new BN128();
127
128
129        const g1a = bn128.G1.mulEscalar(bn128.G1.g, 25);
130        const g2a = bn128.G2.mulEscalar(bn128.G2.g, 30);
131
132        const g1b = bn128.G1.mulEscalar(bn128.G1.g, 30);
133        const g2b = bn128.G2.mulEscalar(bn128.G2.g, 25);
134
135
136        const pre1a = bn128.precomputeG1(g1a);
137        const pre2a = bn128.precomputeG2(g2a);
138        const pre1b = bn128.precomputeG1(g1b);
139        const pre2b = bn128.precomputeG2(g2b);
140
141        const r1 = bn128.millerLoop(pre1a, pre2a);
142        const r2 = bn128.millerLoop(pre1b, pre2b);
143
144        const rbe = bn128.F12.mul(r1, bn128.F12.inverse(r2));
145
146        const res = bn128.finalExponentiation(rbe);
147
148        assert(bn128.F12.equals(res, bn128.F12.one));
149    }).timeout(10000);
150
151});