snarkjs/test/pols.js
test/pols.jsRaw
1const chai = require("chai");
2
3const bigInt = require("../src/bigint.js");
4const PolField = require("../src/polfield.js");
5
6const assert = chai.assert;
7
8const r = bigInt("21888242871839275222246405745257275088548364400416034343698204186575808495617");
9
10describe("Polinomial field", () => {
11 it("Should compute a multiplication", () => {
12 const PF = new PolField(r);
13
14 const a = [bigInt(1), bigInt(2), bigInt(3)];
15 const b = [bigInt(1), bigInt(2), bigInt(3)];
16 const res = PF.mul(a,b);
17
18 assert(PF.equals(res, [bigInt(1), bigInt(4), bigInt(10), bigInt(12), bigInt(9)]));
19 });
20 it("Should compute a multiplication 2", () => {
21 const PF = new PolField(r);
22
23 const a = [bigInt(5), bigInt(1)];
24 const b = [bigInt(-5), bigInt(1)];
25 const res = PF.mul(a,b);
26
27 assert(PF.equals(res, [bigInt(-25), bigInt(0), bigInt(1)]));
28 });
29 it("Should compute an addition", () => {
30 const PF = new PolField(r);
31
32 const a = [bigInt(5), bigInt(1)];
33 const b = [bigInt(-5), bigInt(1)];
34 const res = PF.add(a,b);
35
36 assert(PF.equals(res, [bigInt(0), bigInt(2)]));
37 });
38 it("Should compute a substraction", () => {
39 const PF = new PolField(r);
40
41 const a = [bigInt(5), bigInt(3), bigInt(4)];
42 const b = [bigInt(5), bigInt(1)];
43 const res = PF.sub(a,b);
44
45 assert(PF.equals(res, [bigInt(0), bigInt(2), bigInt(4)]));
46 });
47 it("Should compute reciprocal", () => {
48 const PF = new PolField(r);
49
50 const a = [bigInt(4), bigInt(1), bigInt(-3), bigInt(-1), bigInt(2),bigInt(1), bigInt(-1), bigInt(1)];
51 const res = PF._reciprocal(a, 3, 0);
52
53 assert(PF.equals(res, [bigInt(12), bigInt(15), bigInt(3), bigInt(-4), bigInt(-3), bigInt(0), bigInt(1), bigInt(1)]));
54 });
55 it("Should div2", () => {
56 const PF = new PolField(r);
57
58 // x^6
59 const a = [bigInt(0), bigInt(0), bigInt(0), bigInt(0), bigInt(0),bigInt(0), bigInt(1)];
60 // x^5
61 const b = [bigInt(0), bigInt(0), bigInt(0), bigInt(0), bigInt(0), bigInt(1)];
62
63 const res = PF._div2(6, b);
64 assert(PF.equals(res, [bigInt(0), bigInt(1)]));
65
66 const res2 = PF.div(a,b);
67 assert(PF.equals(res2, [bigInt(0), bigInt(1)]));
68 });
69 it("Should div", () => {
70 const PF = new PolField(r);
71
72 const a = [bigInt(1), bigInt(2), bigInt(3), bigInt(4), bigInt(5),bigInt(6), bigInt(7)];
73 const b = [bigInt(8), bigInt(9), bigInt(10), bigInt(11), bigInt(12), bigInt(13)];
74
75 const c = PF.mul(a,b);
76 const d = PF.div(c,b);
77
78 assert(PF.equals(a, d));
79 });
80
81 it("Should div big/small", () => {
82 const PF = new PolField(r);
83
84 const a = [bigInt(1), bigInt(2), bigInt(3), bigInt(4), bigInt(5),bigInt(6), bigInt(7)];
85 const b = [bigInt(8), bigInt(9)];
86
87 const c = PF.mul(a,b);
88 const d = PF.div(c,b);
89
90 assert(PF.equals(a, d));
91 });
92 it("Should div random big", () => {
93 const PF = new PolField(r);
94
95 const a = [];
96 const b = [];
97 for (let i=0; i<1000; i++) a.push(bigInt(Math.floor(Math.random()*100000) -500000));
98 for (let i=0; i<300; i++) b.push(bigInt(Math.floor(Math.random()*100000) -500000));
99
100 const c = PF.mul(a,b);
101
102 const d = PF.div(c,b);
103
104 assert(PF.equals(a, d));
105 }).timeout(10000000);
106
107});