bigarray
Jordi Baylina jordi@baylina.cat
Thu, 30 Jan 2020 07:25:48 +0100
3 files changed,
74 insertions(+),
13 deletions(-)
A
src/bigarray.js
@@ -0,0 +1,59 @@
+const SUBARRAY_SIZE = 0x10000; + +const BigArrayHandler = { + get: function(obj, prop) { + if (!isNaN(prop)) { + return obj.getElement(prop); + } else return obj[prop]; + }, + set: function(obj, prop, value) { + if (!isNaN(prop)) { + return obj.setElement(prop, value); + } else { + obj[prop] = value; + return true; + } + } +}; + +class _BigArray { + constructor (initSize) { + this.length = initSize || 0; + this.arr = []; + + for (let i=0; i<initSize; i+=SUBARRAY_SIZE) { + this.arr[i/SUBARRAY_SIZE] = new Array(Math.min(SUBARRAY_SIZE, initSize - i)); + } + return this; + } + push (element) { + this.setElement (this.length, element); + } + getElement(idx) { + idx = parseInt(idx); + const idx1 = Math.floor(idx / SUBARRAY_SIZE); + const idx2 = idx % SUBARRAY_SIZE; + return this.arr[idx1] ? this.arr[idx1][idx2] : undefined; + } + setElement(idx, value) { + idx = parseInt(idx); + const idx1 = Math.floor(idx / SUBARRAY_SIZE); + if (!this.arr[idx1]) { + this.arr[idx1] = []; + } + const idx2 = idx % SUBARRAY_SIZE; + this.arr[idx1][idx2] = value; + if (idx >= this.length) this.length = idx+1; + return true; + } +} + +class BigArray { + constructor( initSize ) { + const obj = new _BigArray(initSize); + const extObj = new Proxy(obj, BigArrayHandler); + return extObj; + } +} + +module.exports = BigArray;
M
src/compiler.js
→
src/compiler.js
@@ -26,6 +26,7 @@ const Ctx = require("./ctx");
const ZqField = require("fflib").ZqField; const utils = require("./utils"); const buildR1cs = require("./r1csfile").buildR1cs; +const BigArray = require("./bigarray"); module.exports = compile;@@ -124,7 +125,7 @@ return t1;
} // First classify the signals - for (let s in ctx.signals) { + for (let s=0; s<ctx.signals.length; s++) { const signal = ctx.signals[s]; let tAll = ctx.stINTERNAL; let lSignal = signal;@@ -216,7 +217,7 @@ ctx.totals = totals;
} function reduceConstants(ctx) { - const newConstraints = []; + const newConstraints = new BigArray(); for (let i=0; i<ctx.constraints.length; i++) { if ((ctx.verbose)&&(i%10000 == 0)) console.log("reducing constants: ", i); const c = ctx.lc.canonize(ctx, ctx.constraints[i]);@@ -230,11 +231,11 @@ }
function reduceConstrains(ctx) { indexVariables(); - let possibleConstraints = Object.keys(ctx.constraints); + let possibleConstraints = ctx.constraints; let ii=0; while (possibleConstraints.length>0) { - let nextPossibleConstraints = {}; - for (let i in possibleConstraints) { + let nextPossibleConstraints = new BigArray(); + for (let i=0; i<possibleConstraints.length; i++) { ii++; if ((ctx.verbose)&&(ii%10000 == 0)) console.log("reducing constraints: ", i); if (!ctx.constraints[i]) continue;@@ -290,7 +291,7 @@ if ((j!=i)&&(ctx.constraints[j])) {
ctx.constraints[j] = ctx.lc.substitute(ctx.constraints[j], isolatedSignal, isolatedSignalEquivalence); linkSignalsConstraint(j); if (j<i) { - nextPossibleConstraints[j] = true; + nextPossibleConstraints.push(j); } } }@@ -303,7 +304,7 @@ if (ctx.lc.isZero(c.c)) ctx.constraints[i] = null;
} } } - possibleConstraints = Object.keys(nextPossibleConstraints); + possibleConstraints = nextPossibleConstraints; } unindexVariables();@@ -331,7 +332,7 @@ for (let k in ct.c.coefs) linkSignal(k, cidx);
} function unindexVariables() { - for (let s in ctx.signals) { + for (let s=0; s<ctx.signals.length; s++) { let lSignal = ctx.signals[s]; while (lSignal.e>=0) { lSignal = ctx.signals[lSignal.e];@@ -462,7 +463,7 @@ | | |
A B C */ - +/* function buildConstraints(ctx) { const res = [];@@ -489,7 +490,7 @@ }
return res; } - +*/ function buildSyms(ctx, strm) { let nSyms;
M
src/ctx.js
→
src/ctx.js
@@ -1,4 +1,5 @@
const bigInt = require("big-integer"); +const BigArray = require("./bigarray.js"); class TableName {@@ -103,11 +104,11 @@ this.MAIN = 0x10;
this.COUNTED = 0x20; this.scopes = [{}]; - this.signals = []; + this.signals = new BigArray(); this.currentComponent= -1; - this.constraints= []; - this.components= []; + this.constraints= new BigArray(); + this.components= new BigArray(); this.templates= {}; this.functions= {}; this.functionParams= {};