const utils = require("../../src/utils"); const assert = require("assert"); const Scalar = require("ffjavascript").Scalar; const F1Field = require("ffjavascript").F1Field; const BigArray = require("../../src/bigarray"); function ref2src(c) { if ((c[0] == "R")||(c[0] == "RI")) { return c[1]; } else if (c[0] == "V") { return c[1].toString(); } else if (c[0] == "C") { return `(ctx->circuit->constants + ${c[1]})`; } else if (c[0] == "CC") { return "__cIdx"; } else { assert(false); } } class CodeBuilderC { constructor() { this.ops = []; } addComment(comment) { this.ops.push({op: "COMMENT", comment}); } addBlock(block) { this.ops.push({op: "BLOCK", block}); } calcOffset(dLabel, offsets) { this.ops.push({op: "CALCOFFSETS", dLabel, offsets}); } assign(dLabel, src, sOffset) { this.ops.push({op: "ASSIGN", dLabel, src, sOffset}); } getSubComponentOffset(dLabel, component, hash, hashLabel) { this.ops.push({op: "GETSUBCOMPONENTOFFSET", dLabel, component, hash, hashLabel}); } getSubComponentSizes(dLabel, component, hash, hashLabel) { this.ops.push({op: "GETSUBCOMPONENTSIZES", dLabel, component, hash, hashLabel}); } getSignalOffset(dLabel, component, hash, hashLabel) { this.ops.push({op: "GETSIGNALOFFSET", dLabel, component, hash, hashLabel}); } getSignalSizes(dLabel, component, hash, hashLabel) { this.ops.push({op: "GETSIGNALSIZES", dLabel, component, hash, hashLabel}); } setSignal(component, signal, value) { this.ops.push({op: "SETSIGNAL", component, signal, value}); } getSignal(dLabel, component, signal) { this.ops.push({op: "GETSIGNAL", dLabel, component, signal}); } copyN(dLabel, offset, src, n) { this.ops.push({op: "COPYN", dLabel, offset, src, n}); } copyNRet(src, n) { this.ops.push({op: "COPYNRET", src, n}); } fieldOp(dLabel, fOp, params) { this.ops.push({op: "FOP", dLabel, fOp, params}); } ret() { this.ops.push({op: "RET"}); } addLoop(condLabel, body) { this.ops.push({op: "LOOP", condLabel, body}); } addIf(condLabel, thenCode, elseCode) { this.ops.push({op: "IF", condLabel, thenCode, elseCode}); } fnCall(fnName, retLabel, params) { this.ops.push({op: "FNCALL", fnName, retLabel, params}); } checkConstraint(a, b, strErr) { this.ops.push({op: "CHECKCONSTRAINT", a, b, strErr}); } log(val) { this.ops.push({op: "LOG", val}); } concat(cb) { this.ops.push(...cb.ops); } hasCode() { for (let i=0; i { if ((o[0][0] == "V") && (o[1][0]== "V")) { rN += o[0][1]*o[1][1]; return; } let f=""; if (o[0][0] == "V") { if (o[0][1]==0) return; f += o[0][1]; } else if (o[0][0] == "RI") { if (o[0][1]==0) return; f += o[0][1]; } else if (o[0][0] == "R") { f += `Fr_toInt(${o[0][1]})`; } else { assert(false); } if (o[1][0] == "V") { if (o[1][1]==0) return; if (o[1][1]>1) { f += "*" + o[1][1]; } } else if (o[1][0] == "RS") { f += `*${o[1][1]}[${o[1][2]}]`; } else { assert(false); } if (S!="") S+= " + "; S += f; }); if (rN>0) { S = `${rN} + ${S}`; } return S; } build(code) { this.ops.forEach( (o) => { if (o.op == "COMMENT") { code.push(`/* ${o.comment} */`); } else if (o.op == "BLOCK") { const codeBlock=[]; o.block.build(codeBlock); code.push(utils.ident(codeBlock)); } else if (o.op == "CALCOFFSETS") { code.push(`${o.dLabel} = ${this._buildOffset(o.offsets)};`); } else if (o.op == "ASSIGN") { const oS = ref2src(o.sOffset); if (oS != "0") { code.push(`${o.dLabel} = ${ref2src(o.src)} + ${oS};`); } else { code.push(`${o.dLabel} = ${ref2src(o.src)};`); } } else if (o.op == "GETSUBCOMPONENTOFFSET") { code.push(`${o.dLabel} = ctx->getSubComponentOffset(${ref2src(o.component)}, 0x${o.hash}LL /* ${o.hashLabel} */);`); } else if (o.op == "GETSUBCOMPONENTSIZES") { code.push(`${o.dLabel} = ctx->getSubComponentSizes(${ref2src(o.component)}, 0x${o.hash}LL /* ${o.hashLabel} */);`); } else if (o.op == "GETSIGNALOFFSET") { code.push(`${o.dLabel} = ctx->getSignalOffset(${ref2src(o.component)}, 0x${o.hash}LL /* ${o.hashLabel} */);`); } else if (o.op == "GETSIGNALSIZES") { code.push(`${o.dLabel} = ctx->getSignalSizes(${ref2src(o.component)}, 0x${o.hash}LL /* ${o.hashLabel} */);`); } else if (o.op == "SETSIGNAL") { code.push(`ctx->setSignal(__cIdx, ${ref2src(o.component)}, ${ref2src(o.signal)}, ${ref2src(o.value)});`); } else if (o.op == "GETSIGNAL") { code.push(`ctx->getSignal(__cIdx, ${ref2src(o.component)}, ${ref2src(o.signal)}, ${o.dLabel});`); } else if (o.op == "COPYN") { const oS = ref2src(o.offset); const dLabel = (oS != "0") ? (o.dLabel + "+" + oS) : o.dLabel; code.push(`Fr_copyn(${dLabel}, ${ref2src(o.src)}, ${o.n});`); } else if (o.op == "COPYNRET") { code.push(`Fr_copyn(__retValue, ${ref2src(o.src)}, ${o.n});`); } else if (o.op == "RET") { code.push("goto returnFunc;"); } else if (o.op == "FOP") { let paramsS = ""; for (let i=0; i0) paramsS += ", "; paramsS += ref2src(o.params[i]); } code.push(`Fr_${o.fOp}(${o.dLabel}, ${paramsS});`); } else if (o.op == "LOOP") { code.push(`while (Fr_isTrue(${o.condLabel})) {`); const body = []; o.body.build(body); code.push(utils.ident(body)); code.push("}"); } else if (o.op == "IF") { code.push(`if (Fr_isTrue(${o.condLabel})) {`); const thenCode = []; o.thenCode.build(thenCode); code.push(utils.ident(thenCode)); if (o.elseCode) { code.push("} else {"); const elseCode = []; o.elseCode.build(elseCode); code.push(utils.ident(elseCode)); } code.push("}"); } else if (o.op == "FNCALL") { code.push(`${o.fnName}(ctx, ${o.retLabel}, ${o.params.join(",")});`); } else if (o.op == "CHECKCONSTRAINT") { code.push(`ctx->checkConstraint(__cIdx, ${ref2src(o.a)}, ${ref2src(o.b)}, "${o.strErr}");`); } else if (o.op == "LOG") { code.push(`ctx->log(${ref2src(o.val)});`); } }); } } class FunctionBuilderC { constructor(name, instanceDef, type) { this.name = name; this.instanceDef = instanceDef; this.type = type; // "COMPONENT" or "FUNCTIOM" this.definedFrElements = []; this.definedIntElements = []; this.definedSizeElements = []; this.definedPFrElements = []; this.initializedElements = []; this.initializedSignalOffset = []; this.initializedSignalSizes = []; } defineFrElements(dLabel, size) { this.definedFrElements.push({dLabel, size}); } defineIntElement(dLabel) { this.definedIntElements.push({dLabel}); } defineSizesElement(dLabel) { this.definedSizeElements.push({dLabel}); } definePFrElement(dLabel) { this.definedPFrElements.push({dLabel}); } initializeFrElement(dLabel, offset, idConstant) { this.initializedElements.push({dLabel, offset, idConstant}); } initializeSignalOffset(dLabel, component, hash, hashLabel) { this.initializedSignalOffset.push({dLabel, component, hash, hashLabel}); } initializeSignalSizes(dLabel, component, hash, hashLabel) { this.initializedSignalSizes.push({dLabel, component, hash, hashLabel}); } setParams(params) { this.params = params; } _buildHeader(code) { this.definedFrElements.forEach( (o) => { code.push(`FrElement ${o.dLabel}[${o.size}];`); }); this.definedIntElements.forEach( (o) => { code.push(`int ${o.dLabel};`); }); this.definedSizeElements.forEach( (o) => { code.push(`Circom_Sizes ${o.dLabel};`); }); this.definedPFrElements.forEach( (o) => { code.push(`PFrElement ${o.dLabel};`); }); this.initializedElements.forEach( (o) => { code.push(`Fr_copy(&(${o.dLabel}[${o.offset}]), ctx->circuit->constants +${o.idConstant});`); }); this.initializedSignalOffset.forEach( (o) => { code.push(`${o.dLabel} = ctx->getSignalOffset(${ref2src(o.component)}, 0x${o.hash}LL /* ${o.hashLabel} */);`); }); this.initializedSignalSizes.forEach( (o) => { code.push(`${o.dLabel} = ctx->getSignalSizes(${ref2src(o.component)}, 0x${o.hash}LL /* ${o.hashLabel} */);`); }); } _buildFooter(code) { } newCodeBuilder() { return new CodeBuilderC(); } setBody(body) { this.body = body; } build(code) { code.push( "/*", this.instanceDef, "*/" ); if (this.type=="COMPONENT") { code.push(`void ${this.name}(Circom_CalcWit *ctx, int __cIdx) {`); } else if (this.type=="FUNCTION") { let sParams = ""; for (let i=0;ifinished(__cIdx);"); } else if (this.type=="FUNCTION") { fnCode.push("returnFunc: ;"); } else { assert(false); } this._buildFooter(fnCode); code.push(utils.ident(fnCode)); code.push("}"); } } class BuilderC { constructor(p, verbose) { this.F = new F1Field(p); this.hashMaps={}; this.componentEntriesTables=new BigArray(); this.sizes ={}; this.constants = []; this.functions = []; this.components = new BigArray(); this.usedConstants = {}; this.verbose = verbose; this.sizePointers = {}; this.hashMapPointers = {}; this.cetPointers = {}; this.functionIdx = {}; this.nCets = 0; } setHeader(header) { this.header=header; } // ht is an array of 256 element that can be undefined or [Hash, Idx, KeyName] elements. addHashMap(name, hm) { this.hashMaps[name] = hm; } addComponentEntriesTable(name, cet) { this.componentEntriesTables.push({ name: name, cet: cet }); } addSizes(name, accSizes) { this.sizes[name] = accSizes; } addConstant(c) { c = this.F.e(c); const cS = c.toString(); if (typeof this.usedConstants[cS] != "undefined") return this.usedConstants[cS]; this.constants.push(c); this.usedConstants[cS] = this.constants.length - 1; return this.constants.length - 1; } addFunction(fnBuilder) { this.functions.push(fnBuilder); } addComponent(component) { this.components.push(component); } setMapIsInput(map) { this.mapIsInput = map; } setWit2Sig(wit2sig) { this.wit2sig = wit2sig; } newComponentFunctionBuilder(name, instanceDef) { return new FunctionBuilderC(name, instanceDef, "COMPONENT"); } newFunctionBuilder(name, instanceDef) { return new FunctionBuilderC(name, instanceDef, "FUNCTION"); } // Body functions _buildHeader(code) { code.push( "#include \"circom.hpp\"", "#include \"calcwit.hpp\"", `#define NSignals ${this.header.NSignals}`, `#define NComponents ${this.header.NComponents}`, `#define NOutputs ${this.header.NOutputs}`, `#define NInputs ${this.header.NInputs}`, `#define NVars ${this.header.NVars}`, `#define __P__ "${this.header.P.toString()}"`, "" ); } async _buildHashMaps(fdData) { while (fdData.pos % 8) fdData.pos++; this.pHashMaps = fdData.pos; const buff = new Uint8Array(256*12); const buffV = new DataView(buff.buffer); for (let hmName in this.hashMaps ) { while (fdData.pos % 8) fdData.pos++; this.hashMapPointers[hmName] = fdData.pos; const hm = this.hashMaps[hmName]; for (let i=0; i<256; i++) { buffV.setUint32(i*12, hm[i] ? parseInt( hm[i][0].slice(8), 16 ) : 0, true); buffV.setUint32(i*12+4, hm[i] ? parseInt( hm[i][0].slice(0,8), 16 ) : 0, true); buffV.setUint32(i*12+8, hm[i] ? hm[i][1] : 0, true); } await fdData.write(buff); } } async _buildComponentEntriesTables(fdData) { while (fdData.pos % 8) fdData.pos++; this.pCets = fdData.pos; for (let i=0; i< this.componentEntriesTables.length; i++) { if ((this.verbose)&&(i%100000 ==0)) console.log(`_buildComponentEntriesTables ${i}/${this.componentEntriesTables.length}`); const cetName = this.componentEntriesTables[i].name; const cet = this.componentEntriesTables[i].cet; this.cetPointers[cetName] = fdData.pos; const buff = new Uint8Array(16*cet.length); const buffV = new DataView(buff.buffer); for (let j=0; j Signal, 1->Component this.nCets ++; } await fdData.write(buff); } } async _buildSizes(fdData) { for (let sName in this.sizes) { const accSizes = this.sizes[sName]; while (fdData.pos % 8) fdData.pos++; this.sizePointers[sName] = fdData.pos; const buff = new Uint8Array(4*accSizes.length); const buffV = new DataView(buff.buffer); for (let i=0; i=0) { buffV.setUint32(p, arr[idx], true); } else { buffV.setUint32(p, 0, true); } p+= 4; } } function toMontgomery(a) { return self.F.mul(a, self.F.R); } } } _buildFunctions(code) { const listedFunctions = []; for (let i=0; i0 ? " ," : " "; code.push(`${sep}${this.functions[listedFunctions[i]].name}`); } code.push("};"); } async _buildComponents(fdData) { const buff = new Uint8Array(32); const buffV = new DataView(buff.buffer); while (fdData.pos % 8) fdData.pos++; this.pComponents = fdData.pos; for (let i=0; i