From 263dc73906f0ef8bc17b7769e6971f1b00ca422b Mon Sep 17 00:00:00 2001 From: Jordi Baylina Date: Thu, 17 Sep 2020 01:09:24 +0200 Subject: [PATCH] FIX Bad optimization --- package-lock.json | 6 +- package.json | 2 +- src/compiler.js | 370 ++++++---------------------------------------- src/r1csfile.js | 8 +- 4 files changed, 51 insertions(+), 335 deletions(-) diff --git a/package-lock.json b/package-lock.json index ff0de2e..0962111 100644 --- a/package-lock.json +++ b/package-lock.json @@ -566,9 +566,9 @@ "dev": true }, "fastfile": { - "version": "0.0.12", - "resolved": "https://registry.npmjs.org/fastfile/-/fastfile-0.0.12.tgz", - "integrity": "sha512-0EZo2y5eW8X0oiDDRvcnufjVxlM96CQL5hvmRQtbRABWlCkH73IHwkzl0qOSdxtchaMr+0TSB7GVqaVEixRr1Q==" + "version": "0.0.16", + "resolved": "https://registry.npmjs.org/fastfile/-/fastfile-0.0.16.tgz", + "integrity": "sha512-2sX5M4JQWAVmS2GojuRPA759aTOs5PyUEpZHjaecNtWBU9CfRFlTf2aBqFG2PJJnrcrawBBAQ1s0QSO+qQ4Zmg==" }, "ffiasm": { "version": "0.1.1", diff --git a/package.json b/package.json index 8535141..38146ab 100644 --- a/package.json +++ b/package.json @@ -31,7 +31,7 @@ "dependencies": { "chai": "^4.2.0", "circom_runtime": "0.1.5", - "fastfile": "0.0.12", + "fastfile": "0.0.16", "ffiasm": "0.1.1", "ffjavascript": "0.2.10", "ffwasm": "0.0.7", diff --git a/src/compiler.js b/src/compiler.js index b1a8a00..9f8ede9 100644 --- a/src/compiler.js +++ b/src/compiler.js @@ -318,26 +318,26 @@ async function reduceConstrains(ctx) { const c = ctx.constraints[i]; for (let s in c.a.coefs) { if (!insertedSig[s]) { - if (!sig2constraint[s]) sig2constraint[s] = []; - sig2constraint[s].push(i); + if (!sig2constraint[s]) sig2constraint[s] = {}; + sig2constraint[s][i] = true; insertedSig[s] = true; } } for (let s in c.b.coefs) { if (!insertedSig[s]) { - if (!sig2constraint[s]) sig2constraint[s] = []; - sig2constraint[s].push(i); + if (!sig2constraint[s]) sig2constraint[s] = {}; + sig2constraint[s][i] = true; insertedSig[s] = true; } } for (let s in c.c.coefs) { if (!insertedSig[s]) { - if (!sig2constraint[s]) sig2constraint[s] = []; - sig2constraint[s].push(i); + if (!sig2constraint[s]) sig2constraint[s] = {}; + sig2constraint[s][i] = true; insertedSig[s] = true; } } - possibleConstraints[i] = i; + possibleConstraints[i] = ctx.constraints.length - i -1; } while (possibleConstraints.length >0) { @@ -378,22 +378,28 @@ async function reduceConstrains(ctx) { const freeC = substituteRemoved(c.c); const isolatedSignal = getFirstInternalSignal(ctx, freeC); if (isolatedSignal) { - // console.log(isolatedSignal); - // console.log(freeC); removedSignals[isolatedSignal] = isolateSignal(freeC, isolatedSignal); if (lIdx[isolatedSignal]) { - lIdx[isolatedSignal].forEach( (s) => { + const sigs = Object.keys(lIdx[isolatedSignal]); + + for (let k=0; k { - nextPossibleConstraints[s] = true; - }); + delete lIdx[isolatedSignal]; + + const cts = Object.keys(sig2constraint[isolatedSignal]); + for (let k=0; k0) { - let nextPossibleConstraints = new BigArray(); - for (let i=0; i Object.keys(c.a).length) { - const aux = c.a; - c.a=c.b; - c.b=aux; - } - - // Mov to C if possible. - if (isConstant(c.a)) { - const ct = {t: "N", v: c.a.coefs[sONE]}; - c.c = ctx.lc.add(ctx.lc.mul(c.b, ct), c.c); - c.a = { t: "LC", coefs: {} }; - c.b = { t: "LC", coefs: {} }; - } - if (isConstant(c.b)) { - const ct = {t: "N", v: c.b.coefs[sONE]}; - c.c = ctx.lc.add(ctx.lc.mul(c.a, ct), c.c); - c.a = { t: "LC", coefs: {} }; - c.b = { t: "LC", coefs: {} }; - } - - if (ctx.lc.isZero(c.a) || ctx.lc.isZero(c.b)) { - const isolatedSignal = getFirstInternalSignal(ctx, c.c); - if (isolatedSignal) { - - let lSignal = ctx.signals[isolatedSignal]; - while (lSignal.e>=0) { - lSignal = ctx.signals[lSignal.e]; - } - - - const isolatedSignalEquivalence = { - t: "LC", - coefs: {} - }; - const invCoef = ctx.F.inv(c.c.coefs[isolatedSignal]); - for (const s in c.c.coefs) { - if (s != isolatedSignal) { - const v = ctx.F.mul( ctx.F.neg(c.c.coefs[s]), invCoef); - if (!ctx.F.isZero(v)) { - isolatedSignalEquivalence.coefs[s] = v; - } - } - } - - for (let j in lSignal.inConstraints) { - if ((j!=i)&&(ctx.constraints[j])) { - ctx.constraints[j] = ctx.lc.substitute(ctx.constraints[j], isolatedSignal, isolatedSignalEquivalence); - linkSignalsConstraint(j); - if (j=0) { - lSignal = ctx.signals[lSignal.e]; - } - if (lSignal.inConstraints) delete lSignal.inConstraints; - } - } - -/* - function unlinkSignal(signalName, cidx) { - let lSignal = ctx.signals[signalName]; - while (lSignal.e>=0) { - lSignal = ctx.signals[lSignal.e]; - } - if ((lSignal.inConstraints)&&(lSignal.inConstraints[cidx])) { - delete lSignal.inConstraints[cidx]; - } - } -*/ - - function linkSignal(signalName, cidx) { - let lSignal = ctx.signals[signalName]; - while (lSignal.e>=0) { - lSignal = ctx.signals[lSignal.e]; - } - if (!lSignal.inConstraints) lSignal.inConstraints = {}; - lSignal.inConstraints[cidx] = true; - } - - function getFirstInternalSignal(ctx, l) { - for (let k in l.coefs) { - const signal = ctx.signals[k]; - if (signal.c == ctx.stINTERNAL) return k; - } - return null; - } - - function isConstant(l) { - for (let k in l.coefs) { - if ((k != sONE) && (!ctx.F.isZero(l.coefs[k]))) return false; - } - if (!l.coefs[sONE] || ctx.F.isZero(l.coefs[sONE])) return false; - return true; - } - -} - -/* - -function buildCircuitDef(ctx, mainCode) { - const res = { - mainCode: mainCode - }; - res.signalName2Idx = ctx.signalName2Idx; - - res.components = []; - res.componentName2Idx = {}; - for (let c in ctx.components) { - const idCoponent = res.components.length; - res.components.push({ - name: c, - params: ctx.components[c].params, - template: ctx.components[c].template, - inputSignals: 0 - }); - res.componentName2Idx[c] = idCoponent; - } - - res.signals = new Array(ctx.signalNames.length); - for (let i=0; i { - const idComponet = res.componentName2Idx[ctx.signals[fullName].component]; - if (ctx.signals[fullName].direction == "IN") { - res.signals[i].triggerComponents.push(idComponet); - res.components[idComponet].inputSignals++; + const sigs = Object.keys(lc.coefs); + for (let k=0; k= 0) s = ctx.signals[s].e; - let wId = ctx.signals[s].id; - if (typeof(wId) == "undefined") wId=-1; - strm.write(`${offset},${wId},${prefix}\n`); - nSyms ++; - if ((ctx.verbose)&&(nSyms%10000 == 0)) console.log("Symbols saved: "+nSyms); - } else { - addSymbolsComponent(prefix+".", offset); - } - return 1; - } else { - let acc = 0; - for (let i=0; i=0 ) lSignal = ctx.signals[lSignal.e]; + if (lSignal.id >= NWires) throw new Error("Signal Coef A: " + coef + " Constraint: " + ctIdx + " id: " + lSignal.id); buffV.setUint32(o, lSignal.id, true); o+=4; ctx.F.toRprLE(buff, o, c.a.coefs[coef]); o+=n8; } @@ -118,8 +119,8 @@ async function buildR1cs(ctx, fileName) { const coef = idxB[i]; let lSignal = ctx.signals[coef]; while (lSignal.e >=0 ) lSignal = ctx.signals[lSignal.e]; + if (lSignal.id >= NWires) throw new Error("Signal Coef B: " + coef + " Constraint: " + ctIdx + " id: " + lSignal.id); buffV.setUint32(o, lSignal.id, true); o+=4; - ctx.F.toRprLE(buff, o, c.b.coefs[coef]); o+=n8; } @@ -128,6 +129,7 @@ async function buildR1cs(ctx, fileName) { const coef = idxC[i]; let lSignal = ctx.signals[coef]; while (lSignal.e >=0 ) lSignal = ctx.signals[lSignal.e]; + if (lSignal.id >= NWires) throw new Error("Signal Coef C: " + coef + " Constraint: " + ctIdx + " id: " + lSignal.id); buffV.setUint32(o, lSignal.id, true); o+=4; ctx.F.toRprLE(buff, o, ctx.F.neg(c.c.coefs[coef])); o+=n8; }