arnaucube
arnaucube/snarkjsPublic Self-hosted repository
snarkjs/test/algebra.js
test/algebra.jsRaw
 1const bigInt = require("big-integer");
 2const F1Field = require("../src/f1field.js");
 3const F2Field = require("../src/f2field.js");
 4const GCurve = require("../src/gcurve.js");
 5const constants = require("../src/constants.js");
 6const chai = require("chai");
 7
 8const assert = chai.assert;
 9
10describe("Curve G1 Test", () => {
11    it ("r*one == 0", () => {
12        const F1 = new F1Field(constants.q);
13        const G1 = new GCurve(F1, constants.g1);
14
15        const res = G1.mulEscalar(G1.g, constants.r);
16
17        assert(G1.equals(res, G1.zero), "G1 does not have range r");
18    });
19
20    it("Should add match in various in G1", () => {
21        const F1 = new F1Field(constants.q);
22        const G1 = new GCurve(F1, constants.g1);
23
24        const r1 = bigInt(33);
25        const r2 = bigInt(44);
26
27        const gr1 = G1.mulEscalar(G1.g, r1);
28        const gr2 = G1.mulEscalar(G1.g, r2);
29
30        const grsum1 = G1.add(gr1, gr2);
31
32        const grsum2 = G1.mulEscalar(G1.g, r1.add(r2));
33
34        assert(G1.equals(grsum1, grsum2));
35    });
36});
37
38describe("Curve G2 Test", () => {
39    it ("r*one == 0", () => {
40        const F1 = new F1Field(constants.q);
41        const F2 = new F2Field(F1, constants.f2nonResidue);
42        const G2 = new GCurve(F2, constants.g2);
43
44        const res = G2.mulEscalar(G2.g, constants.r);
45
46        assert(G2.equals(res, G2.zero), "G2 does not have range r");
47    });
48
49    it("Should add match in various in G2", () => {
50        const F1 = new F1Field(constants.q);
51        const F2 = new F2Field(F1, constants.f2nonResidue);
52        const G2 = new GCurve(F2, constants.g2);
53
54        const r1 = bigInt(33);
55        const r2 = bigInt(44);
56
57        const gr1 = G2.mulEscalar(G2.g, r1);
58        const gr2 = G2.mulEscalar(G2.g, r2);
59
60        const grsum1 = G2.add(gr1, gr2);
61
62        const grsum2 = G2.mulEscalar(G2.g, r1.add(r2));
63
64        /*
65        console.log(G2.toString(grsum1));
66        console.log(G2.toString(grsum2));
67        */
68
69        assert(G2.equals(grsum1, grsum2));
70    });
71
72});