Skip to content

Commit

Permalink
Minor adjustment prover_helpers and generateWtnsCols
Browse files Browse the repository at this point in the history
  • Loading branch information
RogerTaule committed Jun 13, 2024
1 parent 9f4e1b1 commit eb6137e
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 59 deletions.
65 changes: 23 additions & 42 deletions src/prover/prover_helpers.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
const workerpool = require("workerpool");
const {BigBuffer} = require("ffjavascript");
const {BigBuffer} = require("pilcom");
const { starkgen_execute } = require("./stark_prover_worker");
const { fflonkgen_execute } = require("./fflonk_prover_worker");
const F3g = require("../helpers/f3g");

const maxNperThread = 1<<18;
const minNperThread = 1<<12;

module.exports.calculateExpression = function calculateExpression(ctx, expId, dom = "n") {
module.exports.calculateExpression = function calculateExpression(ctx, expId) {
const expressionCode = ctx.expressionsInfo.expressionsCode.find(e => e.expId === expId);
return module.exports.calculateExps(ctx, expressionCode.code, dom, false, true);
return module.exports.calculateExps(ctx, expressionCode.code, "n", false, true);
}

module.exports.calculateExpressionAtRow = function calculateExpressionAtRow(ctx, expId, row) {
Expand All @@ -30,7 +31,7 @@ module.exports.calculateExps = function calculateExps(ctx, code, dom, debug, ret
const retValue = debug || ret || false;
cEveryRow = new Function("ctx", "i", module.exports.compileCode(ctx, code.code, dom, retValue, global));

const N = dom=="n" ? ctx.N : ctx.extN;
const N = dom=="n" ? 1 << ctx.nBits : 1 << ctx.nBitsExt;

const pCtx = ctxProxy(ctx, global);

Expand All @@ -41,6 +42,7 @@ module.exports.calculateExps = function calculateExps(ctx, code, dom, debug, ret
}
} else {
let first, last;
pCtx.errors = [];
if(code.boundary === "everyRow") {
first = 0;
last = N;
Expand Down Expand Up @@ -540,56 +542,35 @@ module.exports.printPol = function printPol(buffer, Fr) {
console.log("---------------------------");
}

function ctxProxy(ctx, global) {
function ctxProxy(ctx, global, stark = true) {
const pCtx = {};

const stark = ctx.prover === "stark" ? true : false;

if(!global) {
createProxy("const_n", stark);
createProxy("const_ext", stark);
createProxy("const_coefs", stark);
for(let i = 0; i < ctx.pilInfo.nStages; i++) {
createProxy(`cm${i + 1}_n`, stark);
createProxy(`cm${i + 1}_ext`, stark);
if(!stark) createProxy(`cm${i + 1}_coefs`, stark);
}

createProxy("tmpExp_n", stark);
createProxy("x_n", stark);
createProxy("x_ext", stark);
createProxy("q_ext", stark);

if(stark) {
createProxy(`cm${ctx.pilInfo.nStages + 1}_ext`, stark);

createProxy("Zi_ext", stark);

createProxy("xDivXSubXi_ext", stark);

createProxy("f_ext", stark);
for(let i = 0; i < Object.keys(ctx).length; ++i) {
const key = Object.keys(ctx)[i];
if(ctx[key] instanceof BigBuffer) {
createProxy(key, stark);
}
}
}

pCtx.N = ctx.N;
pCtx.nBits = ctx.nBits;
pCtx.N = 1 << ctx.nBits;

pCtx.extN = ctx.extN;
pCtx.nBitsExt = ctx.nBitsExt;
if(ctx.nBitsExt) {
pCtx.nBitsExt = ctx.nBitsExt;
pCtx.extN = ctx.extN;
}

pCtx.tmp = ctx.tmp;

pCtx.pilInfo = ctx.pilInfo;

pCtx.F = ctx.F;

pCtx.publics = ctx.publics;
pCtx.challenges = ctx.challenges;
pCtx.challengesFRISteps = ctx.challengesFRISteps;
pCtx.subAirValues = ctx.subAirValues;
pCtx.evals = ctx.evals;
pCtx.F = new F3g();

pCtx.errors = ctx.errors;
if(ctx.publics) pCtx.publics = ctx.publics;
if(ctx.challenges) pCtx.challenges = ctx.challenges;
if(ctx.challengesFRISteps) pCtx.challengesFRISteps = ctx.challengesFRISteps;
if(ctx.subAirValues) pCtx.subAirValues = ctx.subAirValues;
if(ctx.evals) pCtx.evals = ctx.evals;

return pCtx;

Expand Down
34 changes: 18 additions & 16 deletions src/witness/witnessCalculator.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ module.exports.generateFixedCols = function generateFixedCols(symbols, degree) {
return fixedCols;
}

module.exports.generateWtnsCols = function generateWtnsCols(stage, symbols, degree) {
module.exports.generateWtnsCols = function generateWtnsCols(stage, symbols, degree, nCols, buffer) {
const witnessSymbols = [];
for (let i = 0; i < symbols.length; ++i) {
if(symbols[i].type !== 3 || symbols[i].stage !== stage) continue;
Expand All @@ -51,19 +51,26 @@ module.exports.generateWtnsCols = function generateWtnsCols(stage, symbols, degr
}
}

const wtnsCols = new ColsPil2(witnessSymbols, degree);
const wtnsCols = new ColsPil2(witnessSymbols, degree, nCols, buffer);
return wtnsCols;
}

class ColsPil2 {
constructor(symbols, degree) {
constructor(symbols, degree, nCols, buffer) {
this.$$def = {};
this.$$defArray = [];

if(!nCols) nCols = symbols.length;

this.F = new F3g();
this.$$n = degree;
this.$$nCols = symbols.length;
this.polBuffer = new BigBuffer(symbols.length*this.$$n);
this.$$nCols = nCols;

if(!buffer) {
this.polBuffer = new BigBuffer(this.$$nCols*this.$$n);
} else {
this.polBuffer = buffer;
}

this.symbols = symbols;
for(let i = 0; i < symbols.length; ++i) {
Expand Down Expand Up @@ -102,13 +109,14 @@ class ColsPil2 {
set(target, prop, value) {
const pos = parseInt(prop, 10);
const buffIndex = nCols * pos + symbolId;
polBuffer[buffIndex] = value;
polBuffer.setElement(buffIndex,value);
return true;
},

get(target, prop) {
const pos = parseInt(prop, 10);
const buffIndex = nCols * pos + symbolId;
return polBuffer[buffIndex];
return polBuffer.getElement(buffIndex);
}
});
}
Expand Down Expand Up @@ -175,23 +183,17 @@ class ColsPil2 {
for (let i=0; i<this.$$n; i++) {
for(let j = 0; j < this.$$nCols; ++j) {
let c = i*this.$$nCols + j;
const value = (this.polBuffer[c] < 0n) ? (this.polBuffer[c] + this.F.p) : this.polBuffer[c];
const value = (this.polBuffer.getElement(c) < 0n) ? (this.polBuffer.getElement(c) + this.F.p) : this.polBuffer.getElement(c);
buff.setElement(p++, value);
}
for(let j = this.$$nCols; j < nCols; ++j) buff.setElement(p++, 0n);

}
return buff;
}
};

writeToBuff(buff) {
if (typeof buff == "undefined") {
const constBuffBuff = new ArrayBuffer(this.$$n*this.$$nCols*8);
buff = new BigUint64Array(constBuffBuff);
}
for (let i=0; i<this.$$n * this.$$nCols; i++) {
buff[i] = (this.polBuffer[i] < 0n) ? (this.polBuffer[i] + 0xffffffff00000001n) : this.polBuffer[i];
}
buff = this.polBuffer;
return buff;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ async function run() {
const pil = pilout.subproofs[0].airs[0];
pil.symbols = pilout.symbols;

const cmPols = generateWtnsCols(1, pil.symbols, pil.numRows, F);
const cmPols = generateWtnsCols(1, pil.symbols, pil.numRows);

const input = JSON.parse(await fs.promises.readFile(inputFile, "utf8"));

Expand Down

0 comments on commit eb6137e

Please sign in to comment.