Browse Source

section size 8 bytes

feature/sectionSize-8bytes
krlosMata 4 years ago
parent
commit
b7ca8aceb4
2 changed files with 40 additions and 22 deletions
  1. +12
    -12
      doc/r1cs_bin_format.md
  2. +28
    -10
      src/r1csfile.js

+ 12
- 12
doc/r1cs_bin_format.md

@ -507,9 +507,9 @@ The format will be:
┣━━━━━━━━━━━━━━┫ ┣━━━━━━━━━━━━━━┫
┃ 03 00 00 00 ┃ nSections ┃ 03 00 00 00 ┃ nSections
┗━━━━━━━━━━━━━━┛ ┗━━━━━━━━━━━━━━┛
┏━━━━━━━━━━━━━━┳━━━━━━━━━━━━━┓
┃ 01 00 00 00 ┃ 49 00 00 00 ┃ SectionType: Header
┗━━━━━━━━━━━━━━┻━━━━━━━━━━━━━┛
┏━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━
┃ 01 00 00 00 ┃ 49 00 00 00 00 00 00 00 ┃ SectionType: Header
┗━━━━━━━━━━━━━━┻━━━━━━━━━━━━━━━━━━━━━━━━━━
┏━━━━━━━━━━━━━━┳━━━━━━━━━━━━━┓ ┏━━━━━━━━━━━━━━┳━━━━━━━━━━━━━┓
┃ 25 00 00 00 ┃ 10 00 00 00 ┃ FieldDefSize FieldDef ┃ 25 00 00 00 ┃ 10 00 00 00 ┃ FieldDefSize FieldDef
┣━━━━━━━━━━━━━━┻━━━━━━━━━━━━━┻━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓ ┣━━━━━━━━━━━━━━┻━━━━━━━━━━━━━┻━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
@ -531,9 +531,9 @@ The format will be:
┣━━━━━━━━━━━━━━┫ ┣━━━━━━━━━━━━━━┫
┃ 03 00 00 00 ┃ # Constraints ┃ 03 00 00 00 ┃ # Constraints
┗━━━━━━━━━━━━━━┛ ┗━━━━━━━━━━━━━━┛
┏━━━━━━━━━━━━━━┳━━━━━━━━━━━━━┓
┃ 02 00 00 00 ┃ 8b 00 00 00 ┃ SectionType: Constraints
┗━━━━━━━━━━━━━━┻━━━━━━━━━━━━━┛
┏━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━
┃ 02 00 00 00 ┃ 8b 00 00 00 00 00 00 00 ┃ SectionType: Constraints
┗━━━━━━━━━━━━━━┻━━━━━━━━━━━━━━━━━━━━━━━━━
┏━━━━━━━━━━━━━━┓ Constraint 0: (3w_5 + 8w_6) * (2w_0 + 20w_2 + 12w_3) - (5w_0 + 7w_2) = 0 ┏━━━━━━━━━━━━━━┓ Constraint 0: (3w_5 + 8w_6) * (2w_0 + 20w_2 + 12w_3) - (5w_0 + 7w_2) = 0
┃ 02 00 00 00 ┃ ┃ 02 00 00 00 ┃
┣━━━━━━━━━━━━━━╋━━━━━━━━┓ ┣━━━━━━━━━━━━━━╋━━━━━━━━┓
@ -598,9 +598,9 @@ The format will be:
┃ 06 00 00 00 ┃ 02 58 02 ┃ ┃ 06 00 00 00 ┃ 02 58 02 ┃
┗━━━━━━━━━━━━━━┻━━━━━━━━━━━━━┛ ┗━━━━━━━━━━━━━━┻━━━━━━━━━━━━━┛
┏━━━━━━━━━━━━━━┳━━━━━━━━━━━━━┓
┃ 03 00 00 00 ┃ 1c 00 00 00 ┃ Wire to Label Map
┗━━━━━━━━━━━━━━┻━━━━━━━━━━━━━┛
┏━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━
┃ 03 00 00 00 ┃ 1c 00 00 00 00 00 00 00 ┃ Wire to Label Map
┗━━━━━━━━━━━━━━┻━━━━━━━━━━━━━━━━━━━━━━━━━
┏━━━━━━━━━━━━━━┓ ┏━━━━━━━━━━━━━━┓
┃ 00 00 00 00 ┃ ┃ 00 00 00 00 ┃
┣━━━━━━━━━━━━━━┫ ┣━━━━━━━━━━━━━━┫
@ -624,7 +624,7 @@ And the binary representation in Hex:
72 31 63 77 72 31 63 77
01 00 00 00 01 00 00 00
03 00 00 00 03 00 00 00
01 00 00 00 49 00 00 00
01 00 00 00 49 00 00 00 00 00 00 00
25 00 00 00 10 00 00 00 25 00 00 00 10 00 00 00
20 010000f0 93f5e143 9170b979 48e83328 5d588181 b64550b8 29a031e1 724e6430 20 010000f0 93f5e143 9170b979 48e83328 5d588181 b64550b8 29a031e1 724e6430
00 00 00 00 00 00 00 00
@ -635,7 +635,7 @@ And the binary representation in Hex:
03 00 00 00 03 00 00 00
e8 03 00 00 e8 03 00 00
03 00 00 00 03 00 00 00
02 00 00 00 8b 00 00 00
02 00 00 00 8b 00 00 00 00 00 00 00
02 00 00 00 02 00 00 00
05 00 00 00 01 03 05 00 00 00 01 03
06 00 00 00 01 08 06 00 00 00 01 08
@ -662,7 +662,7 @@ e8 03 00 00
03 00 00 00 01 05 03 00 00 00 01 05
01 00 00 00 01 00 00 00
06 00 00 00 02 58 02 06 00 00 00 02 58 02
03 00 00 00 1c 00 00 00
03 00 00 00 1c 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00
03 00 00 00 03 00 00 00
0a 00 00 00 0a 00 00 00

+ 28
- 10
src/r1csfile.js

@ -31,7 +31,7 @@ async function loadR1cs(fileName, loadConstraints, loadMap) {
let mapSize; let mapSize;
for (let i=0; i<nSections; i++) { for (let i=0; i<nSections; i++) {
let ht = await readU32(); let ht = await readU32();
let hl = await readU32();
let hl = await readDouble64();
if (ht == 1) { if (ht == 1) {
if (typeof pHeader != "undefined") assert(false, "File has two headder sections"); if (typeof pHeader != "undefined") assert(false, "File has two headder sections");
pHeader = p; pHeader = p;
@ -114,6 +114,15 @@ async function loadR1cs(fileName, loadConstraints, loadMap) {
return b.readInt32LE(0); return b.readInt32LE(0);
} }
async function readDouble64() {
const b = Buffer.allocUnsafe(8);
await fd.read(b, 0, 8, p);
p+=8;
return b.readDoubleLE(0);
}
async function readBigInt() { async function readBigInt() {
const bl = Buffer.allocUnsafe(1); const bl = Buffer.allocUnsafe(1);
await fd.read(bl, 0, 1, p); await fd.read(bl, 0, 1, p);
@ -171,7 +180,7 @@ async function buildR1cs(ctx, fileName) {
/////////// ///////////
await writeU32(1); // Header type await writeU32(1); // Header type
const pHeaderSize = p; const pHeaderSize = p;
await writeU32(0); // Temporally set to 0 length
await writeDouble64(0); // Temporally set to 0 length
// Field Def // Field Def
@ -198,13 +207,13 @@ async function buildR1cs(ctx, fileName) {
await writeU32(ctx.signals.length); await writeU32(ctx.signals.length);
await writeU32(ctx.constraints.length); await writeU32(ctx.constraints.length);
const headerSize = p - pHeaderSize - 4;
const headerSize = p - pHeaderSize - 8;
// Write constraints // Write constraints
/////////// ///////////
await writeU32(2); // Constraints type await writeU32(2); // Constraints type
const pConstraintsSize = p; const pConstraintsSize = p;
await writeU32(0); // Temporally set to 0 length
await writeDouble64(0); // Temporally set to 0 length
for (let i=0; i<ctx.constraints.length; i++) { for (let i=0; i<ctx.constraints.length; i++) {
if ((ctx.verbose)&&(i%10000 == 0)) { if ((ctx.verbose)&&(i%10000 == 0)) {
@ -214,13 +223,13 @@ async function buildR1cs(ctx, fileName) {
await writeConstraint(ctx.constraints[i]); await writeConstraint(ctx.constraints[i]);
} }
const constraintsSize = p - pConstraintsSize - 4;
const constraintsSize = p - pConstraintsSize - 8;
// Write map // Write map
/////////// ///////////
await writeU32(3); // wires2label type await writeU32(3); // wires2label type
const pMapSize = p; const pMapSize = p;
await writeU32(0); // Temporally set to 0 length
await writeDouble64(0); // Temporally set to 0 length
const arr = new Array(NWires); const arr = new Array(NWires);
@ -238,13 +247,13 @@ async function buildR1cs(ctx, fileName) {
if ((ctx.verbose)&&(i%100000)) console.log("writing label2wire map: ", i); if ((ctx.verbose)&&(i%100000)) console.log("writing label2wire map: ", i);
} }
const mapSize = p - pMapSize -4;
const mapSize = p - pMapSize - 8;
// Write sizes // Write sizes
await writeU32(headerSize, pHeaderSize);
await writeDouble64(headerSize, pHeaderSize);
await writeU32(fieldDefSize, pFieldDefSize); await writeU32(fieldDefSize, pFieldDefSize);
await writeU32(constraintsSize, pConstraintsSize);
await writeU32(mapSize, pMapSize);
await writeDouble64(constraintsSize, pConstraintsSize);
await writeDouble64(mapSize, pMapSize);
await fd.sync(); await fd.sync();
await fd.close(); await fd.close();
@ -258,6 +267,15 @@ async function buildR1cs(ctx, fileName) {
if (typeof(pos) == "undefined") p += 4; if (typeof(pos) == "undefined") p += 4;
} }
async function writeDouble64(v, pos) {
const b = Buffer.allocUnsafe(8);
b.writeDoubleLE(v);
await fd.write(b, 0, 8, pos);
if (typeof(pos) == "undefined") p += 8;
}
async function writeConstraint(c) { async function writeConstraint(c) {
await writeLC(c.a); await writeLC(c.a);
await writeLC(c.b); await writeLC(c.b);

Loading…
Cancel
Save