arnaucube
arnaucube/snarkjsPublic Self-hosted repository
snarkjs/test/ratzqfield.js
test/ratzqfield.jsRaw
 1const chai = require("chai");
 2
 3const bigInt = require("../src/bigint.js");
 4const ZqField = require("../src/zqfield.js");
 5const RatField = require("../src/ratfield.js");
 6
 7const q  = bigInt("21888242871839275222246405745257275088548364400416034343698204186575808495617");
 8const Z = new ZqField(q);
 9const R = new RatField(Z);
10
11const assert = chai.assert;
12
13function r(a,b) {
14    return [bigInt(a), bigInt(b)];
15}
16
17
18describe("Rational zq Field", () => {
19    it("Should compare correctly", () => {
20        assert( R.equals(r(3,5), r(6,10)));
21        assert(!R.equals(r(3,5), r(6,11)));
22    });
23    it("Should add correctly", () => {
24        const a = r(7,4);
25        const b = r(5,12);
26
27        assert(R.equals( R.add(a,b), r(13, 6)));
28    });
29    it("Should substract", () => {
30        const a = r(7,4);
31        const b = r(5,12);
32
33        assert(R.equals( R.sub(a,b), r(4, 3)));
34    });
35    it("Should multiply", () => {
36        const a = r(7,4);
37        const b = r(5,12);
38
39        assert(R.equals( R.mul(a,b), r(35, 48)));
40    });
41    it("Should div", () => {
42        const a = r(7,4);
43        const b = r(5,12);
44
45        assert(R.equals( R.div(a,b), r(7*12, 5*4)));
46    });
47    it("Should square", () => {
48        const a = r(7,4);
49
50        assert(R.equals( R.square(a), r(49, 16)));
51    });
52    it("Should affine", () => {
53        const a = r(12,4);
54        const aa = R.affine(a);
55        assert(Z.equals( aa[0], bigInt(3)));
56        assert(Z.equals( aa[1], Z.one));
57    });
58    it("Should convert from Z to R", () => {
59        const vz = bigInt(34);
60        const vr = R.fromF(vz);
61
62        assert(R.equals( vr, r(34,1)));
63    });
64    it("Should convert from R to Z", () => {
65        const vr = r(32, 2);
66        const vz = R.toF(vr);
67
68        assert(Z.equals( vz, bigInt(16)));
69    });
70});