Skip to content

Commit 2cf2803

Browse files
committed
Use ruffini for calculatig lagrange polinomials
1 parent 2910b7c commit 2cf2803

File tree

9 files changed

+63
-290
lines changed

9 files changed

+63
-290
lines changed

file%3a/Users/jbaylina/git/personal/zksnark/src/polfield.js

Lines changed: 0 additions & 260 deletions
This file was deleted.

src/bigint.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ if (typeof(BigInt) != "undefined") {
1717
if (aux <= nq) {
1818
aux = aux % q;
1919
}
20-
if (aux.isNegative()) {
21-
aux = aux.add(q);
20+
if (aux < wBigInt.zero) {
21+
aux = aux + q;
2222
}
2323
} else {
2424
if (aux >= q) {

src/polfield.js

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@
55
is represented by the array [ p0, p1, p2, p3, ... ]
66
*/
77

8-
const bigInt = require("./bigInt");
8+
const bigInt = require("./bigint.js");
99

10-
class PolFieldZq {
10+
class PolField {
1111
constructor (F) {
1212
this.F = F;
1313

1414
const q = this.F.q;
15-
let rem = q.sub(this.F.one);
15+
let rem = q.sub(bigInt(1));
1616
let s = 0;
1717
while (!rem.isOdd()) {
1818
s ++;
@@ -79,7 +79,7 @@ class PolFieldZq {
7979
[b, a] = [a, b];
8080
}
8181

82-
if (b.length < log2(a.length)) {
82+
if ((b.length <= 2) || (b.length < log2(a.length))) {
8383
return this.mulNormal(a,b);
8484
} else {
8585
return this.mulFFT(a,b);
@@ -151,14 +151,14 @@ class PolFieldZq {
151151
}
152152

153153
lagrange(points) {
154+
let roots = [this.F.one];
155+
for (let i=0; i<points.length; i++) {
156+
roots = this.mul(roots, [this.F.neg(points[i][0]), this.F.one]);
157+
}
158+
154159
let sum = [];
155160
for (let i=0; i<points.length; i++) {
156-
let mpol = [this.F.one];
157-
for (let j=0;j<points.length;j++) {
158-
if (i!=j) {
159-
mpol = this.mul(mpol, [this.F.neg(points[j][0]), this.F.one]);
160-
}
161-
}
161+
let mpol = this.ruffini(roots, points[i][0]);
162162
const factor =
163163
this.F.mul(
164164
this.F.inverse(this.eval(mpol, points[i][0])),
@@ -226,6 +226,15 @@ class PolFieldZq {
226226
return true;
227227
}
228228

229+
ruffini(p, r) {
230+
const res = new Array(p.length-1);
231+
res[res.length-1] = p[p.length-1];
232+
for (let i = res.length-2; i>=0; i--) {
233+
res[i] = this.F.add(this.F.mul(res[i+1], r), p[i+1]);
234+
}
235+
return res;
236+
}
237+
229238
_next2Power(v) {
230239
v--;
231240
v |= v >> 1;
@@ -333,4 +342,4 @@ function log2( V )
333342
return( ( ( V & 0xFFFF0000 ) !== 0 ? ( V &= 0xFFFF0000, 16 ) : 0 ) | ( ( V & 0xFF00FF00 ) !== 0 ? ( V &= 0xFF00FF00, 8 ) : 0 ) | ( ( V & 0xF0F0F0F0 ) !== 0 ? ( V &= 0xF0F0F0F0, 4 ) : 0 ) | ( ( V & 0xCCCCCCCC ) !== 0 ? ( V &= 0xCCCCCCCC, 2 ) : 0 ) | ( ( V & 0xAAAAAAAA ) !== 0 ) );
334343
}
335344

336-
module.exports = PolFieldZq;
345+
module.exports = PolField;

src/ratzqfield.js renamed to src/ratfield.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const fUtils = require("./futils.js");
22

3-
class RatZqField {
3+
class RatField {
44
constructor(F) {
55
this.F = F;
66
this.zero = [F.zero, F.one];
@@ -105,4 +105,4 @@ class RatZqField {
105105
}
106106

107107

108-
module.exports = RatZqField;
108+
module.exports = RatField;

0 commit comments

Comments
 (0)