Skip to content

Commit

Permalink
Lint
Browse files Browse the repository at this point in the history
  • Loading branch information
hieu-w committed Jan 14, 2025
1 parent d278702 commit cadbf84
Show file tree
Hide file tree
Showing 9 changed files with 313 additions and 304 deletions.
6 changes: 3 additions & 3 deletions build/lib.cjs/src/f1field.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
'use strict';

var scalar = require('./scalar.js');
var futils = require('./futils.js');
var fft = require('./fft.js');
var fsqrt = require('./fsqrt.js');
var futils = require('./futils.js');
var random = require('./random.js');
var fft = require('./fft.js');
var scalar = require('./scalar.js');

/* global BigInt */

Expand Down
6 changes: 3 additions & 3 deletions build/lib.esm/src/f1field.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { bitLength, toRprLE, toRprBE, fromRprLE, fromRprBE } from './scalar.js';
import { exp } from './futils.js';
import FFT from './fft.js';
import buildSqrt from './fsqrt.js';
import { exp } from './futils.js';
import { getRandomBytes } from './random.js';
import FFT from './fft.js';
import { bitLength, toRprLE, toRprBE, fromRprLE, fromRprBE } from './scalar.js';

/* global BigInt */

Expand Down
292 changes: 146 additions & 146 deletions build/main.cjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -315,43 +315,130 @@ var _Scalar = /*#__PURE__*/Object.freeze({
snarkjs. If not, see <https://www.gnu.org/licenses/>.
*/


/*
exports.mulScalar = (F, base, e) =>{
let res = F.zero;
let rem = bigInt(e);
let exp = base;
This library does operations on polynomials with coefficients in a field F.
while (! rem.eq(bigInt.zero)) {
if (rem.and(bigInt.one).eq(bigInt.one)) {
res = F.add(res, exp);
A polynomial P(x) = p0 + p1 * x + p2 * x^2 + ... + pn * x^n is represented
by the array [ p0, p1, p2, ... , pn ].
*/

class FFT {
constructor(G, F, opMulGF) {
this.F = F;
this.G = G;
this.opMulGF = opMulGF;

let rem = F.sqrt_t || F.t;
let s = F.sqrt_s || F.s;

let nqr = F.one;
while (F.eq(F.pow(nqr, F.half), F.one)) nqr = F.add(nqr, F.one);

this.w = new Array(s + 1);
this.wi = new Array(s + 1);
this.w[s] = this.F.pow(nqr, rem);
this.wi[s] = this.F.inv(this.w[s]);

let n = s - 1;
while (n >= 0) {
this.w[n] = this.F.square(this.w[n + 1]);
this.wi[n] = this.F.square(this.wi[n + 1]);
n--;
}

this.roots = [];
/*
for (let i=0; i<16; i++) {
let r = this.F.one;
n = 1 << i;
const rootsi = new Array(n);
for (let j=0; j<n; j++) {
rootsi[j] = r;
r = this.F.mul(r, this.w[i]);
}
this.roots.push(rootsi);
}
exp = F.double(exp);
rem = rem.shiftRight(1);
*/
this._setRoots(Math.min(s, 15));
}

_setRoots(n) {
for (let i = n; i >= 0 && !this.roots[i]; i--) {
let r = this.F.one;
const nroots = 1 << i;
const rootsi = new Array(nroots);
for (let j = 0; j < nroots; j++) {
rootsi[j] = r;
r = this.F.mul(r, this.w[i]);
}

this.roots[i] = rootsi;
}
}

fft(p) {
if (p.length <= 1) return p;
const bits = log2(p.length - 1) + 1;
this._setRoots(bits);

const m = 1 << bits;
if (p.length != m) {
throw new Error("Size must be multiple of 2");
}
const res = __fft(this, p, bits, 0, 1);
return res;
};
*/
}

function exp(F, base, e) {
if (isZero(e)) return F.one;
ifft(p) {
if (p.length <= 1) return p;
const bits = log2(p.length - 1) + 1;
this._setRoots(bits);
const m = 1 << bits;
if (p.length != m) {
throw new Error("Size must be multiple of 2");
}
const res = __fft(this, p, bits, 0, 1);
const twoinvm = this.F.inv(this.F.mulScalar(this.F.one, m));
const resn = new Array(m);
for (let i = 0; i < m; i++) {
resn[i] = this.opMulGF(res[(m - i) % m], twoinvm);
}

const n = bits(e);
return resn;
}
}

if (n.length == 0) return F.one;
function log2(V) {
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)
);
}

let res = base;
function __fft(PF, pall, bits, offset, step) {
const n = 1 << bits;
if (n == 1) {
return [pall[offset]];
} else if (n == 2) {
return [PF.G.add(pall[offset], pall[offset + step]), PF.G.sub(pall[offset], pall[offset + step])];
}

for (let i = n.length - 2; i >= 0; i--) {
res = F.square(res);
const ndiv2 = n >> 1;
const p1 = __fft(PF, pall, bits - 1, offset, step * 2);
const p2 = __fft(PF, pall, bits - 1, offset + step, step * 2);

if (n[i]) {
res = F.mul(res, base);
}
const out = new Array(n);

for (let i = 0; i < ndiv2; i++) {
out[i] = PF.G.add(p1[i], PF.opMulGF(p2[i], PF.roots[bits][i]));
out[i + ndiv2] = PF.G.sub(p1[i], PF.opMulGF(p2[i], PF.roots[bits][i]));
}

return res;
return out;
}

// Check here: https://eprint.iacr.org/2012/685.pdf
Expand Down Expand Up @@ -516,21 +603,6 @@ function alg8_complex(F) {
};
}

function getRandomBytes(n) {
let array = new Uint8Array(n);
// Browser & Node
if (typeof globalThis.crypto !== "undefined") {
// Supported
globalThis.crypto.getRandomValues(array);
} else {
// fallback
for (let i = 0; i < n; i++) {
array[i] = (Math.random() * 4294967296) >>> 0;
}
}
return array;
}

/*
Copyright 2018 0kims association.
Expand All @@ -550,130 +622,58 @@ function getRandomBytes(n) {
snarkjs. If not, see <https://www.gnu.org/licenses/>.
*/

/*
This library does operations on polynomials with coefficients in a field F.
A polynomial P(x) = p0 + p1 * x + p2 * x^2 + ... + pn * x^n is represented
by the array [ p0, p1, p2, ... , pn ].
*/

class FFT {
constructor(G, F, opMulGF) {
this.F = F;
this.G = G;
this.opMulGF = opMulGF;

let rem = F.sqrt_t || F.t;
let s = F.sqrt_s || F.s;

let nqr = F.one;
while (F.eq(F.pow(nqr, F.half), F.one)) nqr = F.add(nqr, F.one);

this.w = new Array(s + 1);
this.wi = new Array(s + 1);
this.w[s] = this.F.pow(nqr, rem);
this.wi[s] = this.F.inv(this.w[s]);
/*
exports.mulScalar = (F, base, e) =>{
let res = F.zero;
let rem = bigInt(e);
let exp = base;
let n = s - 1;
while (n >= 0) {
this.w[n] = this.F.square(this.w[n + 1]);
this.wi[n] = this.F.square(this.wi[n + 1]);
n--;
while (! rem.eq(bigInt.zero)) {
if (rem.and(bigInt.one).eq(bigInt.one)) {
res = F.add(res, exp);
}
exp = F.double(exp);
rem = rem.shiftRight(1);
}
this.roots = [];
/*
for (let i=0; i<16; i++) {
let r = this.F.one;
n = 1 << i;
const rootsi = new Array(n);
for (let j=0; j<n; j++) {
rootsi[j] = r;
r = this.F.mul(r, this.w[i]);
}
return res;
};
*/

this.roots.push(rootsi);
}
*/
this._setRoots(Math.min(s, 15));
}
function exp(F, base, e) {
if (isZero(e)) return F.one;

_setRoots(n) {
for (let i = n; i >= 0 && !this.roots[i]; i--) {
let r = this.F.one;
const nroots = 1 << i;
const rootsi = new Array(nroots);
for (let j = 0; j < nroots; j++) {
rootsi[j] = r;
r = this.F.mul(r, this.w[i]);
}
const n = bits(e);

this.roots[i] = rootsi;
}
}
if (n.length == 0) return F.one;

fft(p) {
if (p.length <= 1) return p;
const bits = log2(p.length - 1) + 1;
this._setRoots(bits);
let res = base;

const m = 1 << bits;
if (p.length != m) {
throw new Error("Size must be multiple of 2");
}
const res = __fft(this, p, bits, 0, 1);
return res;
}
for (let i = n.length - 2; i >= 0; i--) {
res = F.square(res);

ifft(p) {
if (p.length <= 1) return p;
const bits = log2(p.length - 1) + 1;
this._setRoots(bits);
const m = 1 << bits;
if (p.length != m) {
throw new Error("Size must be multiple of 2");
}
const res = __fft(this, p, bits, 0, 1);
const twoinvm = this.F.inv(this.F.mulScalar(this.F.one, m));
const resn = new Array(m);
for (let i = 0; i < m; i++) {
resn[i] = this.opMulGF(res[(m - i) % m], twoinvm);
if (n[i]) {
res = F.mul(res, base);
}

return resn;
}
}

function log2(V) {
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)
);
return res;
}

function __fft(PF, pall, bits, offset, step) {
const n = 1 << bits;
if (n == 1) {
return [pall[offset]];
} else if (n == 2) {
return [PF.G.add(pall[offset], pall[offset + step]), PF.G.sub(pall[offset], pall[offset + step])];
}

const ndiv2 = n >> 1;
const p1 = __fft(PF, pall, bits - 1, offset, step * 2);
const p2 = __fft(PF, pall, bits - 1, offset + step, step * 2);

const out = new Array(n);

for (let i = 0; i < ndiv2; i++) {
out[i] = PF.G.add(p1[i], PF.opMulGF(p2[i], PF.roots[bits][i]));
out[i + ndiv2] = PF.G.sub(p1[i], PF.opMulGF(p2[i], PF.roots[bits][i]));
function getRandomBytes(n) {
let array = new Uint8Array(n);
// Browser & Node
if (typeof globalThis.crypto !== "undefined") {
// Supported
globalThis.crypto.getRandomValues(array);
} else {
// fallback
for (let i = 0; i < n; i++) {
array[i] = (Math.random() * 4294967296) >>> 0;
}
}

return out;
return array;
}

/* global BigInt */
Expand Down
Loading

0 comments on commit cadbf84

Please sign in to comment.