Browse Source

bigarray

feature/witness_bin
Jordi Baylina 4 years ago
parent
commit
111c91c70d
No known key found for this signature in database GPG Key ID: 7480C80C1BE43112
3 changed files with 74 additions and 13 deletions
  1. +59
    -0
      src/bigarray.js
  2. +11
    -10
      src/compiler.js
  3. +4
    -3
      src/ctx.js

+ 59
- 0
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;

+ 11
- 10
src/compiler.js

@ -26,6 +26,7 @@ const Ctx = require("./ctx");
const ZqField = require("fflib").ZqField; const ZqField = require("fflib").ZqField;
const utils = require("./utils"); const utils = require("./utils");
const buildR1cs = require("./r1csfile").buildR1cs; const buildR1cs = require("./r1csfile").buildR1cs;
const BigArray = require("./bigarray");
module.exports = compile; module.exports = compile;
@ -124,7 +125,7 @@ function classifySignals(ctx) {
} }
// First classify the signals // First classify the signals
for (let s in ctx.signals) {
for (let s=0; s<ctx.signals.length; s++) {
const signal = ctx.signals[s]; const signal = ctx.signals[s];
let tAll = ctx.stINTERNAL; let tAll = ctx.stINTERNAL;
let lSignal = signal; let lSignal = signal;
@ -216,7 +217,7 @@ function generateWitnessNames(ctx) {
} }
function reduceConstants(ctx) { function reduceConstants(ctx) {
const newConstraints = [];
const newConstraints = new BigArray();
for (let i=0; i<ctx.constraints.length; i++) { for (let i=0; i<ctx.constraints.length; i++) {
if ((ctx.verbose)&&(i%10000 == 0)) console.log("reducing constants: ", i); if ((ctx.verbose)&&(i%10000 == 0)) console.log("reducing constants: ", i);
const c = ctx.lc.canonize(ctx, ctx.constraints[i]); const c = ctx.lc.canonize(ctx, ctx.constraints[i]);
@ -230,11 +231,11 @@ function reduceConstants(ctx) {
function reduceConstrains(ctx) { function reduceConstrains(ctx) {
indexVariables(); indexVariables();
let possibleConstraints = Object.keys(ctx.constraints);
let possibleConstraints = ctx.constraints;
let ii=0; let ii=0;
while (possibleConstraints.length>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++; ii++;
if ((ctx.verbose)&&(ii%10000 == 0)) console.log("reducing constraints: ", i); if ((ctx.verbose)&&(ii%10000 == 0)) console.log("reducing constraints: ", i);
if (!ctx.constraints[i]) continue; if (!ctx.constraints[i]) continue;
@ -290,7 +291,7 @@ function reduceConstrains(ctx) {
ctx.constraints[j] = ctx.lc.substitute(ctx.constraints[j], isolatedSignal, isolatedSignalEquivalence); ctx.constraints[j] = ctx.lc.substitute(ctx.constraints[j], isolatedSignal, isolatedSignalEquivalence);
linkSignalsConstraint(j); linkSignalsConstraint(j);
if (j<i) { if (j<i) {
nextPossibleConstraints[j] = true;
nextPossibleConstraints.push(j);
} }
} }
} }
@ -303,7 +304,7 @@ function reduceConstrains(ctx) {
} }
} }
} }
possibleConstraints = Object.keys(nextPossibleConstraints);
possibleConstraints = nextPossibleConstraints;
} }
unindexVariables(); unindexVariables();
@ -331,7 +332,7 @@ function reduceConstrains(ctx) {
} }
function unindexVariables() { function unindexVariables() {
for (let s in ctx.signals) {
for (let s=0; s<ctx.signals.length; s++) {
let lSignal = ctx.signals[s]; let lSignal = ctx.signals[s];
while (lSignal.e>=0) { while (lSignal.e>=0) {
lSignal = ctx.signals[lSignal.e]; lSignal = ctx.signals[lSignal.e];
@ -462,7 +463,7 @@ is converted to
A B C A B C
*/ */
/*
function buildConstraints(ctx) { function buildConstraints(ctx) {
const res = []; const res = [];
@ -489,7 +490,7 @@ function buildConstraints(ctx) {
return res; return res;
} }
*/
function buildSyms(ctx, strm) { function buildSyms(ctx, strm) {
let nSyms; let nSyms;

+ 4
- 3
src/ctx.js

@ -1,4 +1,5 @@
const bigInt = require("big-integer"); const bigInt = require("big-integer");
const BigArray = require("./bigarray.js");
class TableName { class TableName {
@ -103,11 +104,11 @@ module.exports = class Ctx {
this.COUNTED = 0x20; this.COUNTED = 0x20;
this.scopes = [{}]; this.scopes = [{}];
this.signals = [];
this.signals = new BigArray();
this.currentComponent= -1; this.currentComponent= -1;
this.constraints= [];
this.components= [];
this.constraints= new BigArray();
this.components= new BigArray();
this.templates= {}; this.templates= {};
this.functions= {}; this.functions= {};
this.functionParams= {}; this.functionParams= {};

Loading…
Cancel
Save