Browse Source

fastfile

master
Jordi Baylina 4 years ago
parent
commit
7fc457ac90
No known key found for this signature in database GPG Key ID: 7480C80C1BE43112
3 changed files with 26 additions and 19 deletions
  1. +9
    -3
      package-lock.json
  2. +2
    -1
      package.json
  3. +15
    -15
      src/r1csfile.js

+ 9
- 3
package-lock.json

@ -581,6 +581,11 @@
"integrity": "sha1-PYpcZog6FqMMqGQ+hR8Zuqd5eRc=",
"dev": true
},
"fastfile": {
"version": "0.0.1",
"resolved": "https://registry.npmjs.org/fastfile/-/fastfile-0.0.1.tgz",
"integrity": "sha512-Fk8PWafGWGEUw7oPq/dJen92ASxknCEy4ZC8n4VEvSwCp/jcReyEmVoWsRIWTf+IvAp2MzvFi54vOPeK2LQZtQ=="
},
"ffiasm": {
"version": "0.0.2",
"resolved": "https://registry.npmjs.org/ffiasm/-/ffiasm-0.0.2.tgz",
@ -1123,10 +1128,11 @@
"dev": true
},
"r1csfile": {
"version": "0.0.4",
"resolved": "https://registry.npmjs.org/r1csfile/-/r1csfile-0.0.4.tgz",
"integrity": "sha512-1Y/zzzEjQVTR/gPlduRaKi2K+yU+UxqtsS+obDLEEb4WAzwCkKGybRfp037CUW5OApeleS1WdGmtKv9K9FPhsA==",
"version": "0.0.5",
"resolved": "https://registry.npmjs.org/r1csfile/-/r1csfile-0.0.5.tgz",
"integrity": "sha512-B+BdKPb/WUTp4N/3X4d1Spgx9Ojx5tFVejGZRJxpTtzq34mC8Vi/czWfiPj85V8kud31lCfYcZ16z7+czvM0Sw==",
"requires": {
"fastfile": "0.0.1",
"ffjavascript": "0.1.0"
}
},

+ 2
- 1
package.json

@ -31,11 +31,12 @@
"dependencies": {
"chai": "^4.2.0",
"circom_runtime": "0.0.5",
"fastfile": "0.0.1",
"ffiasm": "0.0.2",
"ffjavascript": "0.1.0",
"ffwasm": "0.0.7",
"fnv-plus": "^1.3.1",
"r1csfile": "0.0.4",
"r1csfile": "0.0.5",
"tmp-promise": "^2.0.2",
"wasmbuilder": "0.0.10"
},

+ 15
- 15
src/r1csfile.js

@ -1,16 +1,15 @@
const fs = require("fs");
const fastFile = require("fastfile");
const assert = require("assert");
const Scalar = require("ffjavascript").Scalar;
module.exports.buildR1cs = buildR1cs;
async function buildR1cs(ctx, fileName) {
const fd = await fs.promises.open(fileName, "w");
const fd = await fastFile.createOverride(fileName);
await fd.write("r1cs"); // Magic "r1cs"
await fd.write(Buffer.from("r1cs"), 0); // Magic "r1cs"
let p = 4;
await writeU32(1); // Version
@ -53,7 +52,6 @@ async function buildR1cs(ctx, fileName) {
for (let i=0; i<ctx.constraints.length; i++) {
if ((ctx.verbose)&&(i%10000 == 0)) {
if (ctx.verbose) console.log("writing constraint: ", i);
await fd.datasync();
}
await writeConstraint(ctx.constraints[i]);
}
@ -89,19 +87,22 @@ async function buildR1cs(ctx, fileName) {
await writeU32(constraintsSize, pConstraintsSize);
await writeU32(mapSize, pMapSize);
await fd.sync();
await fd.close();
async function writeU32(v, pos) {
let o = (typeof pos == "undefined") ? p : pos;
const b = Buffer.allocUnsafe(4);
b.writeInt32LE(v);
await fd.write(b, 0, 4, pos);
await fd.write(b, o);
if (typeof(pos) == "undefined") p += 4;
}
async function writeU64(v, pos) {
let o = (typeof pos == "undefined") ? p : pos;
const b = Buffer.allocUnsafe(8);
const LSB = v & 0xFFFFFFFF;
@ -109,7 +110,7 @@ async function buildR1cs(ctx, fileName) {
b.writeInt32LE(LSB, 0);
b.writeInt32LE(MSB, 4);
await fd.write(b, 0, 8, pos);
await fd.write(b, o);
if (typeof(pos) == "undefined") p += 8;
}
@ -134,16 +135,15 @@ async function buildR1cs(ctx, fileName) {
}
async function writeBigInt(n, pos) {
const b = Buffer.allocUnsafe(n8);
const dwords = Scalar.toArray(n, 0x100000000);
let o = (typeof pos == "undefined") ? p : pos;
for (let i=0; i<dwords.length; i++) {
b.writeUInt32LE(dwords[dwords.length-1-i], i*4, 4 );
}
b.fill(0, dwords.length*4);
const s = n.toString(16);
const b = Buffer.from(s.padStart(n8*2, "0"), "hex");
const buff = Buffer.allocUnsafe(b.length);
for (let i=0; i<b.length; i++) buff[i] = b[b.length-1-i];
await fd.write(b, 0, fs, pos);
await fd.write(buff, o);
if (typeof(pos) == "undefined") p += n8;
}

Loading…
Cancel
Save