2020-05-15 21:30:37 +02:00
|
|
|
|
2020-07-11 10:31:52 +02:00
|
|
|
import * as binFileUtils from "./binfileutils.js";
|
2020-05-15 21:30:37 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
This function creates a new section in the fdTo file with id idSection.
|
|
|
|
It multiplies the pooints in fdFrom by first, first*inc, first*inc^2, ....
|
|
|
|
nPoint Times.
|
|
|
|
It also updates the newChallangeHasher with the new points
|
|
|
|
*/
|
|
|
|
|
2020-07-11 10:31:52 +02:00
|
|
|
export async function applyKeyToSection(fdOld, sections, fdNew, idSection, curve, groupName, first, inc, sectionName, logger) {
|
2020-06-16 16:45:32 +02:00
|
|
|
const MAX_CHUNK_SIZE = 1 << 16;
|
|
|
|
const G = curve[groupName];
|
2020-05-15 21:30:37 +02:00
|
|
|
const sG = G.F.n8*2;
|
2020-06-16 16:45:32 +02:00
|
|
|
const nPoints = sections[idSection][0].size / sG;
|
2020-05-15 21:30:37 +02:00
|
|
|
|
2020-06-16 16:45:32 +02:00
|
|
|
await binFileUtils.startReadUniqueSection(fdOld, sections,idSection );
|
|
|
|
await binFileUtils.startWriteSection(fdNew, idSection);
|
2020-05-15 21:30:37 +02:00
|
|
|
|
|
|
|
let t = first;
|
2020-06-16 16:45:32 +02:00
|
|
|
for (let i=0; i<nPoints; i += MAX_CHUNK_SIZE) {
|
2020-07-11 10:31:52 +02:00
|
|
|
if (logger) logger.debug(`Applying key: ${sectionName}: ${i}/${nPoints}`);
|
2020-06-16 16:45:32 +02:00
|
|
|
const n= Math.min(nPoints - i, MAX_CHUNK_SIZE);
|
|
|
|
let buff;
|
|
|
|
buff = await fdOld.read(n*sG);
|
|
|
|
buff = await G.batchApplyKey(buff, t, inc);
|
|
|
|
await fdNew.write(buff);
|
2020-06-30 21:39:25 +02:00
|
|
|
t = curve.Fr.mul(t, curve.Fr.exp(inc, n));
|
2020-05-15 21:30:37 +02:00
|
|
|
}
|
|
|
|
|
2020-06-16 16:45:32 +02:00
|
|
|
await binFileUtils.endWriteSection(fdNew);
|
|
|
|
await binFileUtils.endReadSection(fdOld);
|
2020-06-18 19:14:06 +02:00
|
|
|
}
|
2020-05-15 21:30:37 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
2020-07-11 10:31:52 +02:00
|
|
|
export async function applyKeyToChallangeSection(fdOld, fdNew, responseHasher, curve, groupName, nPoints, first, inc, formatOut, sectionName, logger) {
|
2020-06-18 19:14:06 +02:00
|
|
|
const G = curve[groupName];
|
|
|
|
const sG = G.F.n8*2;
|
|
|
|
const chunkSize = Math.floor((1<<20) / sG); // 128Mb chunks
|
|
|
|
let t = first;
|
|
|
|
for (let i=0 ; i<nPoints ; i+= chunkSize) {
|
2020-07-11 10:31:52 +02:00
|
|
|
if (logger) logger.debug(`Applying key ${sectionName}: ${i}/${nPoints}`);
|
2020-06-18 19:14:06 +02:00
|
|
|
const n= Math.min(nPoints-i, chunkSize );
|
|
|
|
const buffInU = await fdOld.read(n * sG);
|
|
|
|
const buffInLEM = await G.batchUtoLEM(buffInU);
|
|
|
|
const buffOutLEM = await G.batchApplyKey(buffInLEM, t, inc);
|
|
|
|
let buffOut;
|
|
|
|
if (formatOut == "COMPRESSED") {
|
|
|
|
buffOut = await G.batchLEMtoC(buffOutLEM);
|
|
|
|
} else {
|
|
|
|
buffOut = await G.batchLEMtoU(buffOutLEM);
|
|
|
|
}
|
2020-05-15 21:30:37 +02:00
|
|
|
|
2020-06-30 15:45:21 +02:00
|
|
|
if (responseHasher) responseHasher.update(buffOut);
|
2020-06-18 19:14:06 +02:00
|
|
|
await fdNew.write(buffOut);
|
2020-06-30 15:45:21 +02:00
|
|
|
t = curve.Fr.mul(t, curve.Fr.exp(inc, n));
|
2020-06-18 19:14:06 +02:00
|
|
|
}
|
2020-05-15 21:30:37 +02:00
|
|
|
}
|
|
|
|
|