arnaucube
arnaucube/snarkjsPublic Self-hosted repository
snarkjs/test/algebra.js
test/algebra.jsRaw
  1/*
  2    Copyright 2018 0kims association
  3
  4    This file is part of zksnark javascript library.
  5
  6    zksnark javascript library is free software: you can redistribute it and/or modify
  7    it under the terms of the GNU General Public License as published by
  8    the Free Software Foundation, either version 3 of the License, or
  9    (at your option) any later version.
 10
 11    zksnark javascript library is distributed in the hope that it will be useful,
 12    but WITHOUT ANY WARRANTY; without even the implied warranty of
 13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 14    GNU General Public License for more details.
 15
 16    You should have received a copy of the GNU General Public License
 17    along with zksnark javascript library.  If not, see <https://www.gnu.org/licenses/>.
 18*/
 19const chai = require("chai");
 20
 21const bigInt = require("../src/bigint.js");
 22const BN128 = require("../src/bn128.js");
 23const F1Field = require("../src/zqfield.js");
 24
 25const assert = chai.assert;
 26
 27
 28describe("F1 testing", () => {
 29    it("Should compute euclidean", () => {
 30        const F = new F1Field(bigInt(7));
 31        const res = F.inverse(bigInt(4));
 32
 33        assert(F.equals(res, bigInt(2)));
 34    });
 35
 36    it("Should multiply and divide in F1", () => {
 37        const bn128 = new BN128();
 38        const a = bigInt("1");
 39        const b = bn128.F1.affine(bigInt("-3"));
 40        const c = bn128.F1.mul(a,b);
 41        const d = bn128.F1.div(c,b);
 42
 43        assert(bn128.F1.equals(a, d));
 44    });
 45});
 46
 47
 48describe("Curve G1 Test", () => {
 49    it("r*one == 0", () => {
 50        const bn128 = new BN128();
 51
 52        const res = bn128.G1.mulScalar(bn128.G1.g, bn128.r);
 53
 54        assert(bn128.G1.equals(res, bn128.G1.zero), "G1 does not have range r");
 55    });
 56
 57    it("Should add match in various in G1", () => {
 58
 59        const bn128 = new BN128();
 60
 61        const r1 = bigInt(33);
 62        const r2 = bigInt(44);
 63
 64        const gr1 = bn128.G1.mulScalar(bn128.G1.g, r1);
 65        const gr2 = bn128.G1.mulScalar(bn128.G1.g, r2);
 66
 67        const grsum1 = bn128.G1.add(gr1, gr2);
 68
 69        const grsum2 = bn128.G1.mulScalar(bn128.G1.g, r1.add(r2));
 70
 71        assert(bn128.G1.equals(grsum1, grsum2));
 72    });
 73});
 74
 75describe("Curve G2 Test", () => {
 76    it ("r*one == 0", () => {
 77        const bn128 = new BN128();
 78
 79        const res = bn128.G2.mulScalar(bn128.G2.g, bn128.r);
 80
 81        assert(bn128.G2.equals(res, bn128.G2.zero), "G2 does not have range r");
 82    });
 83
 84    it("Should add match in various in G2", () => {
 85        const bn128 = new BN128();
 86
 87        const r1 = bigInt(33);
 88        const r2 = bigInt(44);
 89
 90        const gr1 = bn128.G2.mulScalar(bn128.G2.g, r1);
 91        const gr2 = bn128.G2.mulScalar(bn128.G2.g, r2);
 92
 93        const grsum1 = bn128.G2.add(gr1, gr2);
 94
 95        const grsum2 = bn128.G2.mulScalar(bn128.G2.g, r1.add(r2));
 96
 97        /*
 98        console.log(G2.toString(grsum1));
 99        console.log(G2.toString(grsum2));
100        */
101
102        assert(bn128.G2.equals(grsum1, grsum2));
103    });
104});
105
106describe("F6 testing", () => {
107    it("Should multiply and divide in F6", () => {
108        const bn128 = new BN128();
109        const a =
110            [
111                [bigInt("1"), bigInt("2")],
112                [bigInt("3"), bigInt("4")],
113                [bigInt("5"), bigInt("6")]
114            ];
115        const b =
116            [
117                [bigInt("12"), bigInt("11")],
118                [bigInt("10"), bigInt("9")],
119                [bigInt("8"), bigInt("7")]
120            ];
121        const c = bn128.F6.mul(a,b);
122        const d = bn128.F6.div(c,b);
123
124        assert(bn128.F6.equals(a, d));
125    });
126});
127
128describe("F12 testing", () => {
129    it("Should multiply and divide in F12", () => {
130        const bn128 = new BN128();
131        const a =
132        [
133            [
134                [bigInt("1"), bigInt("2")],
135                [bigInt("3"), bigInt("4")],
136                [bigInt("5"), bigInt("6")]
137            ],
138            [
139                [bigInt("7"), bigInt("8")],
140                [bigInt("9"), bigInt("10")],
141                [bigInt("11"), bigInt("12")]
142            ]
143        ];
144        const b =
145        [
146            [
147                [bigInt("12"), bigInt("11")],
148                [bigInt("10"), bigInt("9")],
149                [bigInt("8"), bigInt("7")]
150            ],
151            [
152                [bigInt("6"), bigInt("5")],
153                [bigInt("4"), bigInt("3")],
154                [bigInt("2"), bigInt("1")]
155            ]
156        ];
157        const c = bn128.F12.mul(a,b);
158        const d = bn128.F12.div(c,b);
159
160        assert(bn128.F12.equals(a, d));
161    });
162});
163
164describe("Pairing", () => {
165    it("Should match pairing", () => {
166        for (let i=0; i<1; i++) {
167            const bn128 = new BN128();
168
169            const g1a = bn128.G1.mulScalar(bn128.G1.g, 25);
170            const g2a = bn128.G2.mulScalar(bn128.G2.g, 30);
171
172            const g1b = bn128.G1.mulScalar(bn128.G1.g, 30);
173            const g2b = bn128.G2.mulScalar(bn128.G2.g, 25);
174
175            const pre1a = bn128.precomputeG1(g1a);
176            const pre2a = bn128.precomputeG2(g2a);
177            const pre1b = bn128.precomputeG1(g1b);
178            const pre2b = bn128.precomputeG2(g2b);
179
180            const r1 = bn128.millerLoop(pre1a, pre2a);
181            const r2 = bn128.millerLoop(pre1b, pre2b);
182
183            const rbe = bn128.F12.mul(r1, bn128.F12.inverse(r2));
184
185            const res = bn128.finalExponentiation(rbe);
186
187            assert(bn128.F12.equals(res, bn128.F12.one));
188        }
189    }).timeout(10000);
190
191});