Skip to content

Commit 9a52e6a

Browse files
committed
fix: correct variable assignment in bn128.js and improve FFT handling in engine_fft.js
- Fixed the assignment of `pr` in `bn128.js` to use the correct property from `bn128wasmPrebuilt`. - Enhanced the FFT implementation in `engine_fft.js` to utilize a fast WASM path when input format matches the native stride, falling back to a JS implementation otherwise. - Updated `wasm_curve.js` to return a sliced copy of the input array in `toJacobian` when the byte length matches the expected size, ensuring immutability.
1 parent bfb0a2c commit 9a52e6a

6 files changed

Lines changed: 44 additions & 43 deletions

File tree

build/browser/browser.esm.js

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2835,7 +2835,7 @@ var WasmCurve = class {
28352835
else throw new Error("invalid point size");
28362836
}
28372837
toJacobian(a) {
2838-
if (a.byteLength == this.F.n8 * 3) return a;
2838+
if (a.byteLength == this.F.n8 * 3) return a.slice();
28392839
else if (a.byteLength == this.F.n8 * 2) return this.op1("_toJacobian", a);
28402840
else throw new Error("invalid point size");
28412841
}
@@ -5507,10 +5507,8 @@ function buildFFT(curve, groupName) {
55075507
buff = array2buffer(buff, sIn);
55085508
returnArray = true;
55095509
} else buff = buff.slice(0, buff.byteLength);
5510-
console.log("FFT input size:", buff.byteLength, " bytes");
55115510
const nPoints = buff.byteLength / sIn;
55125511
const bits = log2(nPoints);
5513-
console.log("FFT points:", nPoints, " bits:", bits);
55145512
if (1 << bits != nPoints) throw new Error("fft must be multiple of 2");
55155513
if (bits == Fr.s + 1) {
55165514
let buffOut;
@@ -5522,25 +5520,26 @@ function buildFFT(curve, groupName) {
55225520
let inv;
55235521
if (inverse) inv = Fr.inv(Fr.e(nPoints));
55245522
let buffOut;
5525-
console.log("fnReversePermutation:", fnReversePermutation);
5526-
const task = [];
5527-
task.push({
5528-
cmd: "ALLOCSET",
5529-
var: 0,
5530-
buff
5531-
});
5532-
task.push({
5533-
cmd: "CALL",
5534-
fnName: fnReversePermutation,
5535-
params: [{ var: 0 }, { val: bits }]
5536-
});
5537-
task.push({
5538-
cmd: "GET",
5539-
out: 0,
5540-
var: 0,
5541-
len: nPoints * sIn
5542-
});
5543-
buff = (await tm.queueAction(task, [buff.buffer]))[0];
5523+
if (sIn === sMid) {
5524+
const task = [];
5525+
task.push({
5526+
cmd: "ALLOCSET",
5527+
var: 0,
5528+
buff
5529+
});
5530+
task.push({
5531+
cmd: "CALL",
5532+
fnName: fnReversePermutation,
5533+
params: [{ var: 0 }, { val: bits }]
5534+
});
5535+
task.push({
5536+
cmd: "GET",
5537+
out: 0,
5538+
var: 0,
5539+
len: nPoints * sIn
5540+
});
5541+
buff = (await tm.queueAction(task, [buff.buffer]))[0];
5542+
} else buffReverseBits(buff, sIn);
55445543
let chunks;
55455544
let pointsInChunk = Math.min(1 << MAX_BITS_THREAD, nPoints);
55465545
let nChunks = nPoints / pointsInChunk;
@@ -6350,7 +6349,7 @@ async function buildBn128(singleThread, plugins) {
63506349
console.log("Using prebuilt bn128 wasm");
63516350
const bn128wasmPrebuilt = await import("wasmcurves/build/bn128_wasm_gzip.js");
63526351
bn128wasm.pq = bn128wasmPrebuilt.pq;
6353-
bn128wasm.pr = bn128wasmPrebuilt.pq;
6352+
bn128wasm.pr = bn128wasmPrebuilt.pr;
63546353
bn128wasm.pG1gen = bn128wasmPrebuilt.pG1gen;
63556354
bn128wasm.pG1zero = bn128wasmPrebuilt.pG1zero;
63566355
bn128wasm.pG1b = bn128wasmPrebuilt.pG1b;

build/browser/browser.iife.js

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

build/browser/browser.iife.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/bn128.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export default async function buildBn128(singleThread, plugins) {
1919

2020
//console.log(bn128wasmPrebuilt);
2121
bn128wasm.pq = bn128wasmPrebuilt.pq;
22-
bn128wasm.pr = bn128wasmPrebuilt.pq;
22+
bn128wasm.pr = bn128wasmPrebuilt.pr;
2323
bn128wasm.pG1gen = bn128wasmPrebuilt.pG1gen;
2424
bn128wasm.pG1zero = bn128wasmPrebuilt.pG1zero;
2525
bn128wasm.pG1b = bn128wasmPrebuilt.pG1b;

src/engine_fft.js

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { log2, array2buffer, buffer2array } from "./utils.js";
1+
import { log2, buffReverseBits, array2buffer, buffer2array } from "./utils.js";
22
import BigBuffer from "./bigbuffer.js";
33

44

@@ -76,13 +76,9 @@ export default function buildFFT(curve, groupName) {
7676
buff = buff.slice(0, buff.byteLength);
7777
}
7878

79-
console.log("FFT input size:", buff.byteLength, " bytes");
80-
8179
const nPoints = buff.byteLength / sIn;
8280
const bits = log2(nPoints);
8381

84-
console.log("FFT points:", nPoints, " bits:", bits);
85-
8682
if ((1 << bits) != nPoints) {
8783
throw new Error("fft must be multiple of 2" );
8884
}
@@ -110,15 +106,21 @@ export default function buildFFT(curve, groupName) {
110106

111107
let buffOut;
112108

113-
console.log("fnReversePermutation:", fnReversePermutation);
114-
115-
const task = [];
116-
task.push({cmd: "ALLOCSET", var: 0, buff: buff});
117-
task.push({cmd: "CALL", fnName: fnReversePermutation, params: [{var:0}, {val: bits}]});
118-
task.push({ cmd: "GET", out: 0, var: 0, len: nPoints * sIn });
119-
const reversedBuff = await tm.queueAction(task, [buff.buffer]);
120-
121-
buff = reversedBuff[0];
109+
// The WASM _reversePermutation functions use a fixed element stride
110+
// (the native group representation size, e.g. Jacobian for G1/G2).
111+
// When the input format (sIn) matches the native stride (sMid), we can
112+
// use the fast WASM path. Otherwise fall back to the JS implementation
113+
// which accepts an arbitrary element size.
114+
if (sIn === sMid) {
115+
const task = [];
116+
task.push({cmd: "ALLOCSET", var: 0, buff: buff});
117+
task.push({cmd: "CALL", fnName: fnReversePermutation, params: [{var:0}, {val: bits}]});
118+
task.push({ cmd: "GET", out: 0, var: 0, len: nPoints * sIn });
119+
const reversedBuff = await tm.queueAction(task, [buff.buffer]);
120+
buff = reversedBuff[0];
121+
} else {
122+
buffReverseBits(buff, sIn);
123+
}
122124

123125
let chunks;
124126
let pointsInChunk = Math.min(1 << MAX_BITS_THREAD, nPoints);

src/wasm_curve.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ export default class WasmCurve {
209209

210210
toJacobian(a) {
211211
if (a.byteLength == this.F.n8*3) {
212-
return a;
212+
return a.slice();
213213
} else if (a.byteLength == this.F.n8*2) {
214214
return this.op1("_toJacobian", a);
215215
} else {

0 commit comments

Comments
 (0)