websnark/test/bn128.js
test/bn128.jsRaw
1const assert = require("assert");
2const refBn128 = require("snarkjs").bn128;
3const refBigInt = require("snarkjs").bigInt;
4
5const buildBn128 = require("../index.js").buildBn128;
6
7describe("Basic tests for g1 in bn128", () => {
8 it("It should do a basic point doubling G1", async () => {
9 const bn128 = await buildBn128();
10 const refD = refBn128.G1.double(refBn128.g1);
11
12 const p1 = bn128.g1_allocPoint(refBn128.g1);
13 bn128.g1_toMontgomery(p1, p1);
14 bn128.g1_double(p1, p1);
15 bn128.g1_fromMontgomery(p1, p1);
16 const d = bn128.g1_getPoint(p1);
17
18 for (let i=0; i<3; i++) {
19 d[i] = refBigInt(d[i].toString());
20 }
21
22 assert(refBn128.G1.equals(d, refD));
23 });
24 it("It should add two points G1", async () => {
25 const bn128 = await buildBn128();
26 const refD = refBn128.G1.affine(refBn128.G1.mulScalar(refBn128.g1, 3));
27
28 const p1 = bn128.g1_allocPoint(refBn128.g1);
29 bn128.g1_toMontgomery(p1, p1);
30 bn128.g1_double(p1, p1);
31 const p2 = bn128.g1_allocPoint(refBn128.g1);
32 bn128.g1_toMontgomery(p2, p2);
33 bn128.g1_add(p1, p2, p2);
34 bn128.g1_affine(p2, p2);
35 bn128.g1_fromMontgomery(p2, p2);
36 const d = bn128.g1_getPoint(p2);
37
38 d[0] = refBigInt(d[0].toString());
39 d[1] = refBigInt(d[1].toString());
40 d[2] = refBigInt(d[2].toString());
41
42
43 assert(d[0].equals(refD[0]));
44 assert(d[1].equals(refD[1]));
45 assert(d[2].equals(1));
46
47 assert(refBn128.G1.equals(d, refD));
48 });
49 it("It should timesScalar G1", async () => {
50 const bn128 = await buildBn128();
51 const refD = refBn128.G1.mulScalar(refBn128.g1, 55);
52
53 const p1 = bn128.g1_allocPoint(refBn128.g1);
54 bn128.g1_toMontgomery(p1, p1);
55
56 const s = bn128.allocInt(55);
57 bn128.g1_timesScalar(p1, s, 32, p1);
58
59 bn128.g1_fromMontgomery(p1, p1);
60 const d = bn128.g1_getPoint(p1);
61
62 for (let i=0; i<3; i++) {
63 d[i] = refBigInt(d[i].toString());
64 }
65
66 assert(refBn128.G1.equals(d, refD));
67 });
68 it("G1n == 0", async () => {
69 const bn128 = await buildBn128();
70
71 const p1 = bn128.g1_allocPoint(refBn128.g1);
72 bn128.g1_toMontgomery(p1, p1);
73
74 const s = bn128.allocInt(bn128.r);
75 bn128.g1_timesScalar(p1, s, 32, p1);
76
77 bn128.g1_fromMontgomery(p1, p1);
78
79 assert(bn128.g1_isZero(p1));
80 });
81 it("It should do a test", async () => {
82 const bn128 = await buildBn128();
83
84 const t = bn128.test_AddG1(100000);
85
86 console.log(t);
87 }).timeout(10000000);
88 it("It should validate the test", async () => {
89 const bn128 = await buildBn128();
90 const refD = refBn128.G1.mulScalar(refBn128.g1, 100000);
91
92 const p1 = bn128.g1_allocPoint(refBn128.g1);
93 bn128.g1_toMontgomery(p1, p1);
94 const p2 = bn128.g1_allocPoint();
95 bn128.testAddG1(100000, p1, p2);
96 bn128.g1_fromMontgomery(p2, p2);
97 const d = bn128.g1_getPoint(p2);
98
99 for (let i=0; i<3; i++) {
100 d[i] = refBigInt(d[i].toString());
101 }
102
103 assert(refBn128.G1.equals(d, refD));
104
105 }).timeout(10000000);
106 it("It should do a basic point doubling in G2", async () => {
107 const bn128 = await buildBn128();
108 const refD = refBn128.G2.double(refBn128.g2);
109
110 const p1 = bn128.g2_allocPoint(refBn128.g2);
111 bn128.g2_toMontgomery(p1, p1);
112 bn128.g2_double(p1, p1);
113 bn128.g2_fromMontgomery(p1, p1);
114 const d = bn128.g2_getPoint(p1);
115
116 for (let i=0; i<3; i++) {
117 for (let j=0; j<2; j++) {
118 d[i][j] = refBigInt(d[i][j].toString());
119 }
120 }
121
122 assert(refBn128.G2.equals(d, refD));
123 });
124 it("It should add two points in G2", async () => {
125 const bn128 = await buildBn128();
126 const refD = refBn128.G2.affine(refBn128.G2.mulScalar(refBn128.g2, 3));
127
128 const p1 = bn128.g2_allocPoint(refBn128.g2);
129 bn128.g2_toMontgomery(p1, p1);
130 bn128.g2_double(p1, p1);
131 const p2 = bn128.g2_allocPoint(refBn128.g2);
132 bn128.g2_toMontgomery(p2, p2);
133 bn128.g2_add(p1, p2, p2);
134 bn128.g2_affine(p2, p2);
135 bn128.g2_fromMontgomery(p2, p2);
136 const d = bn128.g2_getPoint(p2);
137
138 for (let i=0; i<3; i++) {
139 for (let j=0; j<2; j++) {
140 d[i][j] = refBigInt(d[i][j].toString());
141 assert(d[i][j].equals(refD[i][j]));
142 }
143 }
144
145 assert(refBn128.G2.equals(d, refD));
146 });
147});