Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

section size 8 bytes #52

Open
wants to merge 1 commit into
base: c_build
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions doc/r1cs_bin_format.md
Original file line number Diff line number Diff line change
Expand Up @@ -507,9 +507,9 @@ The format will be:
┣━━━━━━━━━━━━━━┫
┃ 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
┣━━━━━━━━━━━━━━┻━━━━━━━━━━━━━┻━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
Expand All @@ -531,9 +531,9 @@ The format will be:
┣━━━━━━━━━━━━━━┫
┃ 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
┃ 02 00 00 00 ┃
┣━━━━━━━━━━━━━━╋━━━━━━━━┓
Expand Down Expand Up @@ -598,9 +598,9 @@ The format will be:
┃ 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 ┃
┣━━━━━━━━━━━━━━┫
Expand All @@ -624,7 +624,7 @@ And the binary representation in Hex:
72 31 63 77
01 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
20 010000f0 93f5e143 9170b979 48e83328 5d588181 b64550b8 29a031e1 724e6430
00 00 00 00
Expand All @@ -635,7 +635,7 @@ And the binary representation in Hex:
03 00 00 00
e8 03 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
05 00 00 00 01 03
06 00 00 00 01 08
Expand All @@ -662,7 +662,7 @@ e8 03 00 00
03 00 00 00 01 05
01 00 00 00
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
03 00 00 00
0a 00 00 00
Expand Down
38 changes: 28 additions & 10 deletions src/r1csfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ async function loadR1cs(fileName, loadConstraints, loadMap) {
let mapSize;
for (let i=0; i<nSections; i++) {
let ht = await readU32();
let hl = await readU32();
let hl = await readDouble64();
if (ht == 1) {
if (typeof pHeader != "undefined") assert(false, "File has two headder sections");
pHeader = p;
Expand Down Expand Up @@ -114,6 +114,15 @@ async function loadR1cs(fileName, loadConstraints, loadMap) {
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() {
const bl = Buffer.allocUnsafe(1);
await fd.read(bl, 0, 1, p);
Expand Down Expand Up @@ -171,7 +180,7 @@ async function buildR1cs(ctx, fileName) {
///////////
await writeU32(1); // Header type
const pHeaderSize = p;
await writeU32(0); // Temporally set to 0 length
await writeDouble64(0); // Temporally set to 0 length


// Field Def
Expand All @@ -198,13 +207,13 @@ async function buildR1cs(ctx, fileName) {
await writeU32(ctx.signals.length);
await writeU32(ctx.constraints.length);

const headerSize = p - pHeaderSize - 4;
const headerSize = p - pHeaderSize - 8;

// Write constraints
///////////
await writeU32(2); // Constraints type
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++) {
if ((ctx.verbose)&&(i%10000 == 0)) {
Expand All @@ -214,13 +223,13 @@ async function buildR1cs(ctx, fileName) {
await writeConstraint(ctx.constraints[i]);
}

const constraintsSize = p - pConstraintsSize - 4;
const constraintsSize = p - pConstraintsSize - 8;

// Write map
///////////
await writeU32(3); // wires2label type
const pMapSize = p;
await writeU32(0); // Temporally set to 0 length
await writeDouble64(0); // Temporally set to 0 length


const arr = new Array(NWires);
Expand All @@ -238,13 +247,13 @@ async function buildR1cs(ctx, fileName) {
if ((ctx.verbose)&&(i%100000)) console.log("writing label2wire map: ", i);
}

const mapSize = p - pMapSize -4;
const mapSize = p - pMapSize - 8;

// Write sizes
await writeU32(headerSize, pHeaderSize);
await writeDouble64(headerSize, pHeaderSize);
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.close();
Expand All @@ -258,6 +267,15 @@ async function buildR1cs(ctx, fileName) {
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) {
await writeLC(c.a);
await writeLC(c.b);
Expand Down