arnaucube
arnaucube/snarkjsPublic Self-hosted repository
snarkjs/test/ratzqfield.js
test/ratzqfield.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 ZqField = require("../src/zqfield.js");
23const RatField = require("../src/ratfield.js");
24
25const q  = bigInt("21888242871839275222246405745257275088548364400416034343698204186575808495617");
26const Z = new ZqField(q);
27const R = new RatField(Z);
28
29const assert = chai.assert;
30
31function r(a,b) {
32    return [bigInt(a), bigInt(b)];
33}
34
35
36describe("Rational zq Field", () => {
37    it("Should compare correctly", () => {
38        assert( R.equals(r(3,5), r(6,10)));
39        assert(!R.equals(r(3,5), r(6,11)));
40    });
41    it("Should add correctly", () => {
42        const a = r(7,4);
43        const b = r(5,12);
44
45        assert(R.equals( R.add(a,b), r(13, 6)));
46    });
47    it("Should substract", () => {
48        const a = r(7,4);
49        const b = r(5,12);
50
51        assert(R.equals( R.sub(a,b), r(4, 3)));
52    });
53    it("Should multiply", () => {
54        const a = r(7,4);
55        const b = r(5,12);
56
57        assert(R.equals( R.mul(a,b), r(35, 48)));
58    });
59    it("Should div", () => {
60        const a = r(7,4);
61        const b = r(5,12);
62
63        assert(R.equals( R.div(a,b), r(7*12, 5*4)));
64    });
65    it("Should square", () => {
66        const a = r(7,4);
67
68        assert(R.equals( R.square(a), r(49, 16)));
69    });
70    it("Should affine", () => {
71        const a = r(12,4);
72        const aa = R.affine(a);
73        assert(Z.equals( aa[0], bigInt(3)));
74        assert(Z.equals( aa[1], Z.one));
75    });
76    it("Should convert from Z to R", () => {
77        const vz = bigInt(34);
78        const vr = R.fromF(vz);
79
80        assert(R.equals( vr, r(34,1)));
81    });
82    it("Should convert from R to Z", () => {
83        const vr = r(32, 2);
84        const vz = R.toF(vr);
85
86        assert(Z.equals( vz, bigInt(16)));
87    });
88});