const fs = require("fs"); const assert = require("assert"); const bigInt = require("big-integer"); module.exports.buildR1cs = buildR1cs; async function buildR1cs(ctx, fileName) { const fd = await fs.promises.open(fileName, "w"); await fd.write("r1cs"); // Magic "r1cs" let p = 4; await writeU32(1); // Version await writeU32(3); // Number of Sections // Write the header /////////// await writeU32(1); // Header type const pHeaderSize = p; await writeU64(0); // Temporally set to 0 length const n8 = (Math.floor( (ctx.field.p.bitLength() - 1) / 64) +1)*8; // Field Def await writeU32(n8); // Temporally set to 0 length await writeBigInt(ctx.field.p); const NWires = ctx.totals[ctx.stONE] + ctx.totals[ctx.stOUTPUT] + ctx.totals[ctx.stPUBINPUT] + ctx.totals[ctx.stPRVINPUT] + ctx.totals[ctx.stINTERNAL]; await writeU32(NWires); await writeU32(ctx.totals[ctx.stOUTPUT]); await writeU32(ctx.totals[ctx.stPUBINPUT]); await writeU32(ctx.totals[ctx.stPRVINPUT]); await writeU64(ctx.signals.length); await writeU32(ctx.constraints.length); const headerSize = p - pHeaderSize - 8; // Write constraints /////////// await writeU32(2); // Constraints type const pConstraintsSize = p; await writeU64(0); // Temporally set to 0 length for (let i=0; i=0) continue; // If has an alias, continue.. assert(typeof outIdx != "undefined", `Signal ${i} does not have index`); if (outIdx>=NWires) continue; // Is a constant or a discarded variable if (typeof arr[ctx.signals[i].id] == "undefined") { arr[outIdx] = i; } } for (let i=0; i=0 ) lSignal = ctx.signals[lSignal.e]; await writeU32(lSignal.id); await writeBigInt(lc.coefs[s]); } } async function writeBigInt(n, pos) { const b = Buffer.allocUnsafe(n8); const dwords = bigInt(n).toArray(0x100000000).value; for (let i=0; i