diff --git a/src/r1csfile.js b/src/r1csfile.js index ba722a9..7930145 100644 --- a/src/r1csfile.js +++ b/src/r1csfile.js @@ -9,22 +9,24 @@ async function buildR1cs(ctx, fileName) { const fd = await fastFile.createOverride(fileName); - await fd.write(Buffer.from("r1cs"), 0); // Magic "r1cs" + const type = "r1cs"; + const buff = new Uint8Array(4); + for (let i=0; i<4; i++) buff[i] = type.charCodeAt(i); + await fd.write(buff, 0); // Magic "r1cs" - let p = 4; - await writeU32(1); // Version - await writeU32(3); // Number of Sections + await fd.writeULE32(1); // Version + await fd.writeULE32(3); // Number of Sections // Write the header /////////// - await writeU32(1); // Header type - const pHeaderSize = p; - await writeU64(0); // Temporally set to 0 length + await fd.writeULE32(1); // Header type + const pHeaderSize = fd.pos; + await fd.writeULE64(0); // Temporally set to 0 length const n8 = (Math.floor( (ctx.F.bitLength - 1) / 64) +1)*8; // Field Def - await writeU32(n8); // Temporally set to 0 length + await fd.writeULE32(n8); // Temporally set to 0 length await writeBigInt(ctx.F.p); const NWires = @@ -34,20 +36,20 @@ async function buildR1cs(ctx, fileName) { 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); + await fd.writeULE32(NWires); + await fd.writeULE32(ctx.totals[ctx.stOUTPUT]); + await fd.writeULE32(ctx.totals[ctx.stPUBINPUT]); + await fd.writeULE32(ctx.totals[ctx.stPRVINPUT]); + await fd.writeULE64(ctx.signals.length); + await fd.writeULE32(ctx.constraints.length); - const headerSize = p - pHeaderSize - 8; + const headerSize = fd.pos - pHeaderSize - 8; // Write constraints /////////// - await writeU32(2); // Constraints type - const pConstraintsSize = p; - await writeU64(0); // Temporally set to 0 length + await fd.writeULE32(2); // Constraints type + const pConstraintsSize = fd.pos; + await fd.writeULE64(0); // Temporally set to 0 length for (let i=0; i=0 ) lSignal = ctx.signals[lSignal.e]; - await writeU32(lSignal.id); + await fd.writeULE32(lSignal.id); await writeBigInt(lc.coefs[s]); } } async function writeBigInt(n, pos) { - let o = (typeof pos == "undefined") ? p : pos; - const s = n.toString(16); const b = Buffer.from(s.padStart(n8*2, "0"), "hex"); - const buff = Buffer.allocUnsafe(b.length); + const buff = new Uint8Array(b.length); for (let i=0; i