snarkjs/test/pols.js
test/pols.jsRaw
1const chai = require("chai");
2
3const bigInt = require("../src/bigint.js");
4const PolField = require("../src/polfield.js");
5const ZqField = require("../src/zqfield");
6
7const assert = chai.assert;
8
9const r = bigInt("21888242871839275222246405745257275088548364400416034343698204186575808495617");
10
11describe("Polinomial field", () => {
12 it("Should compute a multiplication", () => {
13 const PF = new PolField(new ZqField(r));
14
15 const a = [bigInt(1), bigInt(2), bigInt(3)];
16 const b = [bigInt(1), bigInt(2), bigInt(3)];
17 const res = PF.mul(a,b);
18
19 assert(PF.equals(res, [bigInt(1), bigInt(4), bigInt(10), bigInt(12), bigInt(9)]));
20 });
21 it("Should compute a multiplication 2", () => {
22 const PF = new PolField(new ZqField(r));
23
24 const a = [bigInt(5), bigInt(1)];
25 const b = [bigInt(-5), bigInt(1)];
26 const res = PF.mul(a,b);
27
28 assert(PF.equals(res, [bigInt(-25), bigInt(0), bigInt(1)]));
29 });
30 it("Should compute an addition", () => {
31 const PF = new PolField(new ZqField(r));
32
33 const a = [bigInt(5), bigInt(1)];
34 const b = [bigInt(-5), bigInt(1)];
35 const res = PF.add(a,b);
36
37 assert(PF.equals(res, [bigInt(0), bigInt(2)]));
38 });
39 it("Should compute a substraction", () => {
40 const PF = new PolField(new ZqField(r));
41
42 const a = [bigInt(5), bigInt(3), bigInt(4)];
43 const b = [bigInt(5), bigInt(1)];
44 const res = PF.sub(a,b);
45
46 assert(PF.equals(res, [bigInt(0), bigInt(2), bigInt(4)]));
47 });
48 it("Should compute reciprocal", () => {
49 const PF = new PolField(new ZqField(r));
50
51 const a = [bigInt(4), bigInt(1), bigInt(-3), bigInt(-1), bigInt(2),bigInt(1), bigInt(-1), bigInt(1)];
52 const res = PF._reciprocal(a, 3, 0);
53
54 assert(PF.equals(res, [bigInt(12), bigInt(15), bigInt(3), bigInt(-4), bigInt(-3), bigInt(0), bigInt(1), bigInt(1)]));
55 });
56 it("Should div2", () => {
57 const PF = new PolField(new ZqField(r));
58
59 // x^6
60 const a = [bigInt(0), bigInt(0), bigInt(0), bigInt(0), bigInt(0),bigInt(0), bigInt(1)];
61 // x^5
62 const b = [bigInt(0), bigInt(0), bigInt(0), bigInt(0), bigInt(0), bigInt(1)];
63
64 const res = PF._div2(6, b);
65 assert(PF.equals(res, [bigInt(0), bigInt(1)]));
66
67 const res2 = PF.div(a,b);
68 assert(PF.equals(res2, [bigInt(0), bigInt(1)]));
69 });
70 it("Should div", () => {
71 const PF = new PolField(new ZqField(r));
72
73 const a = [bigInt(1), bigInt(2), bigInt(3), bigInt(4), bigInt(5),bigInt(6), bigInt(7)];
74 const b = [bigInt(8), bigInt(9), bigInt(10), bigInt(11), bigInt(12), bigInt(13)];
75
76 const c = PF.mul(a,b);
77 const d = PF.div(c,b);
78
79 assert(PF.equals(a, d));
80 });
81
82 it("Should div big/small", () => {
83 const PF = new PolField(new ZqField(r));
84
85 const a = [bigInt(1), bigInt(2), bigInt(3), bigInt(4), bigInt(5),bigInt(6), bigInt(7)];
86 const b = [bigInt(8), bigInt(9)];
87
88 const c = PF.mul(a,b);
89 const d = PF.div(c,b);
90
91 assert(PF.equals(a, d));
92 });
93 it("Should div random big", () => {
94 const PF = new PolField(new ZqField(r));
95
96 const a = [];
97 const b = [];
98 for (let i=0; i<1000; i++) a.push(bigInt(Math.floor(Math.random()*100000) -500000));
99 for (let i=0; i<900; i++) b.push(bigInt(Math.floor(Math.random()*100000) -500000));
100
101 const c = PF.mul(a,b);
102
103 const d = PF.div(c,b);
104
105 assert(PF.equals(a, d));
106 }).timeout(10000);
107 it("Should evaluate and zero", () => {
108 const PF = new PolField(new ZqField(r));
109 const p = [PF.F.neg(bigInt(2)), bigInt(1)];
110 const v = PF.eval(p, bigInt(2));
111 assert(PF.F.equals(v, bigInt(0)));
112 });
113 it("Should create lagrange polynomial minmal", () => {
114 const PF = new PolField(new ZqField(r));
115
116 const points=[];
117 points.push([bigInt(1), bigInt(1)]);
118 points.push([bigInt(2), bigInt(2)]);
119 points.push([bigInt(3), bigInt(5)]);
120
121 const p=PF.lagrange(points);
122
123 for (let i=0; i<points.length; i++) {
124 const v = PF.eval(p, points[i][0]);
125 assert(PF.F.equals(v, points[i][1]));
126 }
127 });
128 it("Should create lagrange polynomial", () => {
129 const PF = new PolField(new ZqField(r));
130
131 const points=[];
132 points.push([bigInt(1), bigInt(2)]);
133 points.push([bigInt(2), bigInt(-2)]);
134 points.push([bigInt(3), bigInt(0)]);
135 points.push([bigInt(4), bigInt(453345)]);
136
137 const p=PF.lagrange(points);
138
139 for (let i=0; i<points.length; i++) {
140 const v = PF.eval(p, points[i][0]);
141 assert(PF.F.equals(v, points[i][1]));
142 }
143 });
144 it("Should test ruffini", () => {
145 const PF = new PolField(new ZqField(r));
146 const a = [bigInt(1), bigInt(2), bigInt(3), bigInt(4), bigInt(5),bigInt(6), bigInt(7)];
147
148 const b = PF.mul(a, [bigInt(-7), bigInt(1)]);
149 const c = PF.ruffini(b, bigInt(7));
150
151 assert(PF.equals(a, c));
152 });
153
154});