ffjavascript
Advanced tools
Comparing version 0.2.10 to 0.2.13
@@ -21,1 +21,4 @@ | ||
export {default as BigBuffer} from "./src/bigbuffer.js"; | ||
{ | ||
"name": "ffjavascript", | ||
"type": "module", | ||
"version": "0.2.10", | ||
"version": "0.2.13", | ||
"description": "Finite Field Library in Javascript", | ||
@@ -6,0 +6,0 @@ "main": "./build/main.cjs", |
@@ -0,1 +1,2 @@ | ||
import BigBuffer from "./bigbuffer.js"; | ||
@@ -37,3 +38,9 @@ export default function buildBatchConvert(tm, fnName, sIn, sOut) { | ||
const fullBuffOut = new Uint8Array(nPoints*sOut); | ||
let fullBuffOut; | ||
if (buffIn instanceof BigBuffer) { | ||
fullBuffOut = new BigBuffer(nPoints*sOut); | ||
} else { | ||
fullBuffOut = new Uint8Array(nPoints*sOut); | ||
} | ||
let p =0; | ||
@@ -40,0 +47,0 @@ for (let i=0; i<result.length; i++) { |
import {log2, buffReverseBits} from "./utils.js"; | ||
import BigBuffer from "./bigbuffer.js"; | ||
@@ -99,3 +100,3 @@ export default function buildFFT(curve, groupName) { | ||
task.push({cmd: "ALLOC", var: 0, len: sMid*nPoints}); | ||
task.push({cmd: "SET", var: 0, buff: buff}); | ||
task.push({cmd: "SET", var: 0, buff: buff.slice()}); | ||
if (fnIn2Mid) { | ||
@@ -206,3 +207,7 @@ task.push({cmd: "CALL", fnName:fnIn2Mid, params: [{var:0}, {val: nPoints}, {var: 0}]}); | ||
buffOut = new Uint8Array(nPoints * sOut); | ||
if (buff instanceof BigBuffer) { | ||
buffOut = new BigBuffer(nPoints*sOut); | ||
} else { | ||
buffOut = new Uint8Array(nPoints*sOut); | ||
} | ||
if (inverse) { | ||
@@ -333,4 +338,8 @@ buffOut.set(chunks[0].slice((pointsInChunk-1)*sOut)); | ||
const fullBuffOut = new Uint8Array(nPoints*sG); | ||
let fullBuffOut; | ||
if (buff instanceof BigBuffer) { | ||
fullBuffOut = new BigBuffer(nPoints*sG); | ||
} else { | ||
fullBuffOut = new Uint8Array(nPoints*sG); | ||
} | ||
let p =0; | ||
@@ -399,4 +408,12 @@ for (let i=0; i<nChunks; i++) { | ||
const fullBuffOut1 = new Uint8Array(nPoints*sG); | ||
const fullBuffOut2 = new Uint8Array(nPoints*sG); | ||
let fullBuffOut1; | ||
let fullBuffOut2; | ||
if (buff1 instanceof BigBuffer) { | ||
fullBuffOut1 = new BigBuffer(nPoints*sG); | ||
fullBuffOut2 = new BigBuffer(nPoints*sG); | ||
} else { | ||
fullBuffOut1 = new Uint8Array(nPoints*sG); | ||
fullBuffOut2 = new Uint8Array(nPoints*sG); | ||
} | ||
let p =0; | ||
@@ -465,3 +482,9 @@ for (let i=0; i<result.length; i++) { | ||
const fullBuffOut = new Uint8Array(nPoints*sGout); | ||
let fullBuffOut; | ||
if (buff instanceof BigBuffer) { | ||
fullBuffOut = new BigBuffer(nPoints*sGout); | ||
} else { | ||
fullBuffOut = new Uint8Array(nPoints*sGout); | ||
} | ||
let p =0; | ||
@@ -468,0 +491,0 @@ for (let i=result.length-1; i>=0; i--) { |
@@ -12,3 +12,4 @@ import { log2 } from "./utils.js"; | ||
const G = curve[groupName]; | ||
async function _multiExp(buffBases, buffScalars, inType) { | ||
const tm = G.tm; | ||
async function _multiExpChunk(buffBases, buffScalars, inType) { | ||
inType = inType || "affine"; | ||
@@ -83,8 +84,62 @@ | ||
G.multiExp = async function multiExpAffine(buffBases, buffScalars) { | ||
return await _multiExp(buffBases, buffScalars, "jacobian"); | ||
async function _multiExp(buffBases, buffScalars, inType, logger, logText) { | ||
const MAX_CHUNK_SIZE = 1 << 22; | ||
const MIN_CHUNK_SIZE = 1 << 10; | ||
let sGIn; | ||
if (groupName == "G1") { | ||
if (inType == "affine") { | ||
sGIn = G.F.n8*2; | ||
} else { | ||
sGIn = G.F.n8*3; | ||
} | ||
} else if (groupName == "G2") { | ||
if (inType == "affine") { | ||
sGIn = G.F.n8*2; | ||
} else { | ||
sGIn = G.F.n8*3; | ||
} | ||
} else { | ||
throw new Error("Invalid group"); | ||
} | ||
const nPoints = Math.floor(buffBases.byteLength / sGIn); | ||
const sScalar = Math.floor(buffScalars.byteLength / nPoints); | ||
if( sScalar * nPoints != buffScalars.byteLength) { | ||
throw new Error("Scalar size does not match"); | ||
} | ||
const bitChunkSize = pTSizes[log2(nPoints)]; | ||
const nChunks = Math.floor((sScalar*8 - 1) / bitChunkSize) +1; | ||
let chunkSize; | ||
chunkSize = Math.floor(nPoints / (tm.concurrency /nChunks)); | ||
if (chunkSize>MAX_CHUNK_SIZE) chunkSize = MAX_CHUNK_SIZE; | ||
if (chunkSize<MIN_CHUNK_SIZE) chunkSize = MIN_CHUNK_SIZE; | ||
const opPromises = []; | ||
for (let i=0; i<nPoints; i += chunkSize) { | ||
if (logger) logger.debug(`Multiexp: ${logText}: ${i}/${nPoints}`); | ||
const n= Math.min(nPoints - i, chunkSize); | ||
const buffBasesChunk = await buffBases.slice(i*sGIn, (i+n)*sGIn); | ||
const buffScalarsChunk = await buffScalars.slice(i*sScalar, (i+n)*sScalar); | ||
opPromises.push(_multiExpChunk(buffBasesChunk, buffScalarsChunk, inType)); | ||
} | ||
const result = await Promise.all(opPromises); | ||
let res = G.zero; | ||
for (let i=result.length-1; i>=0; i--) { | ||
res = G.add(res, result[i]); | ||
} | ||
return res; | ||
} | ||
G.multiExp = async function multiExpAffine(buffBases, buffScalars, logger, logText) { | ||
return await _multiExp(buffBases, buffScalars, "jacobian", logger, logText); | ||
}; | ||
G.multiExpAffine = async function multiExpAffine(buffBases, buffScalars) { | ||
return await _multiExp(buffBases, buffScalars, "affine"); | ||
G.multiExpAffine = async function multiExpAffine(buffBases, buffScalars, logger, logText) { | ||
return await _multiExp(buffBases, buffScalars, "affine", logger, logText); | ||
}; | ||
} |
@@ -59,3 +59,3 @@ import WasmField1 from "./wasm_field1.js"; | ||
curve.buffer2array = function(buff , sG) { | ||
const n= buff.length / sG; | ||
const n= buff.byteLength / sG; | ||
const arr = new Array(n); | ||
@@ -62,0 +62,0 @@ for (let i=0; i<n; i++) { |
@@ -123,7 +123,7 @@ /* global BigInt */ | ||
} else if (o+2 <= len) { | ||
buff.setUint16(Number(o, r & 0xFFFFn), true ); | ||
buffV.setUint16(Number(o, r & 0xFFFFn), true ); | ||
o += 2; | ||
r = r >> 16n; | ||
} else { | ||
buff.setUint8(Number(o, r & 0xFFn), true ); | ||
buffV.setUint8(Number(o, r & 0xFFn), true ); | ||
o += 1; | ||
@@ -130,0 +130,0 @@ r = r >> 8n; |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
697988
50
13286