const bigInt = require("big-integer"); module.exports = calculateWitness; function calculateWitness(circuit, inputSignals) { const ctx = new RTCtx(circuit); function iterateSelector(values, sels, cb) { if (!Array.isArray(values)) { return cb(sels, values); } for (let i=0; i " + ctx.witness[i].toString()); } return ctx.witness; } class RTCtx { constructor(circuit) { this.scopes = []; this.circuit = circuit; this.witness = []; this.notInitSignals = {}; for (let c in this.circuit.components) { this.notInitSignals[c] = this.circuit.components[c].inputSignals; if (this.notInitSignals == 0) { this.currentComponent = c; this.components.calc(this); this.currentComponent = null; } } } _sels2str(sels) { let res = ""; for (let i=0; i=0; i--) { if (typeof(this.scopes[i][name]) != "undefined") return select(this.scopes[i][name], sels); } throw new Error("Variable not defined: " + name); } getSignal(name, sels) { let fullName = name=="one" ? "one" : this.currentComponent + "." + name; fullName += this._sels2str(sels); return this.getSignalFullName(fullName); } getPin(componentName, componentSels, signalName, signalSels) { let fullName = componentName=="one" ? "one" : this.currentComponent + "." + componentName; fullName += this._sels2str(componentSels) + "."+ signalName+ this._sels2str(signalSels); return this.getSignalFullName(fullName); } getSignalFullName(fullName) { const sId = this.circuit.signals[fullName].id; if (typeof(this.witness[sId]) == "undefined") { throw new Error("Signal not initialized: "+fullName); } console.log("get --->" + fullName + " = " + this.witness[sId].toString() ); return this.witness[sId]; } assert(a,b) { const ba = bigInt(a); const bb = bigInt(b); if (!ba.equals(bb)) { throw new Error("Constrain doesn't match: " + ba.toString() + " != " + bb.toString()); } } }