Compare commits

..

1 Commits

Author SHA1 Message Date
krlosMata
babe908d35 add header to witness bin 2020-02-27 20:58:37 +01:00
3 changed files with 34 additions and 40 deletions

View File

@@ -118,6 +118,18 @@ void writeOutBin(Circom_CalcWit *ctx, std::string filename) {
write_ptr = fopen(filename.c_str(),"wb"); write_ptr = fopen(filename.c_str(),"wb");
// Add header
// 4 bytes --> total number of constraints
uint32_t witnessLength = _circuit.NVars;
// 4 bytes --> witness element size (measured in number of 32bit words)
uint32_t witnessSize = 8; // witness size = 8*32 = 256 bits
// 8bytes --> empty
uint64_t otherField = 0;
fwrite(&witnessLength, 4, 1, write_ptr);
fwrite(&witnessSize, 4, 1, write_ptr);
fwrite(&otherField, 8, 1, write_ptr);
FrElement v; FrElement v;
u8 buffOut[256]; u8 buffOut[256];

View File

@@ -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 00 00 00 00 ┃ SectionType: Header ┃ 01 00 00 00 ┃ 49 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 00 00 00 00 ┃ SectionType: Constraints ┃ 02 00 00 00 ┃ 8b 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 00 00 00 00 ┃ Wire to Label Map ┃ 03 00 00 00 ┃ 1c 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 00 00 00 00 01 00 00 00 49 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 00 00 00 00 02 00 00 00 8b 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 00 00 00 00 03 00 00 00 1c 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

View File

@@ -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 readDouble64(); let hl = await readU32();
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,15 +114,6 @@ 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);
@@ -180,7 +171,7 @@ async function buildR1cs(ctx, fileName) {
/////////// ///////////
await writeU32(1); // Header type await writeU32(1); // Header type
const pHeaderSize = p; const pHeaderSize = p;
await writeDouble64(0); // Temporally set to 0 length await writeU32(0); // Temporally set to 0 length
// Field Def // Field Def
@@ -207,13 +198,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 - 8; const headerSize = p - pHeaderSize - 4;
// Write constraints // Write constraints
/////////// ///////////
await writeU32(2); // Constraints type await writeU32(2); // Constraints type
const pConstraintsSize = p; const pConstraintsSize = p;
await writeDouble64(0); // Temporally set to 0 length await writeU32(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)) {
@@ -223,13 +214,13 @@ async function buildR1cs(ctx, fileName) {
await writeConstraint(ctx.constraints[i]); await writeConstraint(ctx.constraints[i]);
} }
const constraintsSize = p - pConstraintsSize - 8; const constraintsSize = p - pConstraintsSize - 4;
// Write map // Write map
/////////// ///////////
await writeU32(3); // wires2label type await writeU32(3); // wires2label type
const pMapSize = p; const pMapSize = p;
await writeDouble64(0); // Temporally set to 0 length await writeU32(0); // Temporally set to 0 length
const arr = new Array(NWires); const arr = new Array(NWires);
@@ -247,13 +238,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 - 8; const mapSize = p - pMapSize -4;
// Write sizes // Write sizes
await writeDouble64(headerSize, pHeaderSize); await writeU32(headerSize, pHeaderSize);
await writeU32(fieldDefSize, pFieldDefSize); await writeU32(fieldDefSize, pFieldDefSize);
await writeDouble64(constraintsSize, pConstraintsSize); await writeU32(constraintsSize, pConstraintsSize);
await writeDouble64(mapSize, pMapSize); await writeU32(mapSize, pMapSize);
await fd.sync(); await fd.sync();
await fd.close(); await fd.close();
@@ -267,15 +258,6 @@ 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);