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 a free software: you can redistribute it and/or
7 modify it under the terms of the GNU General Public License as published by the
8 Free Software Foundation, either version 3 of the License, or (at your option)
9 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 MERCHANTABILITY
13 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 more details.
15
16 You should have received a copy of the GNU General Public License along with
17 zksnark JavaScript library. If not, see <https://www.gnu.org/licenses/>.
18*/
19
20const chai = require("chai");
21
22const bigInt = require("../src/bigint.js");
23const ZqField = require("../src/zqfield.js");
24const RatField = require("../src/ratfield.js");
25
26const q = bigInt("21888242871839275222246405745257275088548364400416034343698204186575808495617");
27const Z = new ZqField(q);
28const R = new RatField(Z);
29
30const assert = chai.assert;
31
32function r(a,b) {
33 return [bigInt(a), bigInt(b)];
34}
35
36
37describe("Rational zq Field", () => {
38 it("Should compare correctly", () => {
39 assert( R.equals(r(3,5), r(6,10)));
40 assert(!R.equals(r(3,5), r(6,11)));
41 });
42 it("Should add correctly", () => {
43 const a = r(7,4);
44 const b = r(5,12);
45
46 assert(R.equals( R.add(a,b), r(13, 6)));
47 });
48 it("Should substract", () => {
49 const a = r(7,4);
50 const b = r(5,12);
51
52 assert(R.equals( R.sub(a,b), r(4, 3)));
53 });
54 it("Should multiply", () => {
55 const a = r(7,4);
56 const b = r(5,12);
57
58 assert(R.equals( R.mul(a,b), r(35, 48)));
59 });
60 it("Should div", () => {
61 const a = r(7,4);
62 const b = r(5,12);
63
64 assert(R.equals( R.div(a,b), r(7*12, 5*4)));
65 });
66 it("Should square", () => {
67 const a = r(7,4);
68
69 assert(R.equals( R.square(a), r(49, 16)));
70 });
71 it("Should affine", () => {
72 const a = r(12,4);
73 const aa = R.affine(a);
74 assert(Z.equals( aa[0], bigInt(3)));
75 assert(Z.equals( aa[1], Z.one));
76 });
77 it("Should convert from Z to R", () => {
78 const vz = bigInt(34);
79 const vr = R.fromF(vz);
80
81 assert(R.equals( vr, r(34,1)));
82 });
83 it("Should convert from R to Z", () => {
84 const vr = r(32, 2);
85 const vz = R.toF(vr);
86
87 assert(Z.equals( vz, bigInt(16)));
88 });
89});