Browse Source

Remove streams c and wasm gen

master
Jordi Baylina 4 years ago
parent
commit
90cc7d5072
No known key found for this signature in database GPG Key ID: 7480C80C1BE43112
4 changed files with 98 additions and 98 deletions
  1. +61
    -79
      cli.js
  2. +13
    -2
      ports/c/builder.js
  3. +17
    -3
      ports/wasm/builder.js
  4. +7
    -14
      src/compiler.js

+ 61
- 79
cli.js

@ -24,6 +24,7 @@
const fs = require("fs"); const fs = require("fs");
const path = require("path"); const path = require("path");
const Scalar = require("ffjavascript").Scalar; const Scalar = require("ffjavascript").Scalar;
const fastFile = require("fastfile");
const compiler = require("./src/compiler"); const compiler = require("./src/compiler");
@ -60,87 +61,65 @@ const argv = require("yargs")
.argv; .argv;
let inputFile;
if (argv._.length == 0) {
inputFile = "circuit.circom";
} else if (argv._.length == 1) {
inputFile = argv._[0];
} else {
console.log("Only one circuit at a time is permited");
process.exit(1);
}
const fullFileName = path.resolve(process.cwd(), inputFile);
const fileName = path.basename(fullFileName, ".circom");
const cSourceName = typeof(argv.csource) === "string" ? argv.csource : fileName + ".cpp";
const wasmName = typeof(argv.wasm) === "string" ? argv.wasm : fileName + ".wasm";
const watName = typeof(argv.wat) === "string" ? argv.wat : fileName + ".wat";
const r1csName = typeof(argv.r1cs) === "string" ? argv.r1cs : fileName + ".r1cs";
const symName = typeof(argv.sym) === "string" ? argv.sym : fileName + ".sym";
const options = {};
options.reduceConstraints = !argv.fast;
options.verbose = argv.verbose || false;
options.sanityCheck = argv.sanitycheck;
if (argv.csource) {
options.cSourceWriteStream = fs.createWriteStream(cSourceName);
}
if (argv.wasm) {
options.wasmWriteStream = fs.createWriteStream(wasmName);
}
if (argv.wat) {
options.watWriteStream = fs.createWriteStream(watName);
}
if (argv.r1cs) {
options.r1csFileName = r1csName;
}
if (argv.sym) {
options.symWriteStream = fs.createWriteStream(symName);
}
if (argv.newThreadTemplates) {
options.newThreadTemplates = new RegExp(argv.newThreadTemplates);
}
if (!argv.prime) {
options.prime = Scalar.fromString("21888242871839275222246405745257275088548364400416034343698204186575808495617");
} else if (["BLS12-381", "BLS12381"]. indexOf(argv.prime.toUpperCase()) >=0) {
options.prime = Scalar.fromString("73eda753299d7d483339d80809a1d80553bda402fffe5bfeffffffff00000001",16);
} else if (["BN-128", "BN128", "BN254", "BN-254"]. indexOf(argv.prime.toUpperCase()) >=0) {
options.prime = Scalar.fromString("21888242871839275222246405745257275088548364400416034343698204186575808495617");
} else {
options.prime = Scalar.fromString(argv.prime);
}
async function run() {
let inputFile;
if (argv._.length == 0) {
inputFile = "circuit.circom";
} else if (argv._.length == 1) {
inputFile = argv._[0];
} else {
console.log("Only one circuit at a time is permited");
process.exit(1);
}
compiler(fullFileName, options).then( () => {
let cSourceDone = false;
let wasmDone = false;
let symDone = false;
let watDone = false;
if (options.cSourceWriteStream) {
options.cSourceWriteStream.on("finish", () => {
cSourceDone = true;
finishIfDone();
});
} else {
cSourceDone = true;
const fullFileName = path.resolve(process.cwd(), inputFile);
const fileName = path.basename(fullFileName, ".circom");
const cSourceName = typeof(argv.csource) === "string" ? argv.csource : fileName + ".cpp";
const wasmName = typeof(argv.wasm) === "string" ? argv.wasm : fileName + ".wasm";
const watName = typeof(argv.wat) === "string" ? argv.wat : fileName + ".wat";
const r1csName = typeof(argv.r1cs) === "string" ? argv.r1cs : fileName + ".r1cs";
const symName = typeof(argv.sym) === "string" ? argv.sym : fileName + ".sym";
const options = {};
options.reduceConstraints = !argv.fast;
options.verbose = argv.verbose || false;
options.sanityCheck = argv.sanitycheck;
if (argv.csource) {
options.cSourceFile = await fastFile.createOverride(cSourceName);
} }
if (options.wasmWriteStream) {
options.wasmWriteStream.on("finish", () => {
wasmDone = true;
finishIfDone();
});
} else {
wasmDone = true;
if (argv.wasm) {
options.wasmFile = await fastFile.createOverride(wasmName);
} }
if (options.watWriteStream) {
options.watWriteStream.on("finish", () => {
watDone = true;
finishIfDone();
});
if (argv.wat) {
options.watFile = await fastFile.createOverride(watName);
}
if (argv.r1cs) {
options.r1csFileName = r1csName;
}
if (argv.sym) {
options.symWriteStream = fs.createWriteStream(symName);
}
if (argv.newThreadTemplates) {
options.newThreadTemplates = new RegExp(argv.newThreadTemplates);
}
if (!argv.prime) {
options.prime = Scalar.fromString("21888242871839275222246405745257275088548364400416034343698204186575808495617");
} else if (["BLS12-381", "BLS12381"]. indexOf(argv.prime.toUpperCase()) >=0) {
options.prime = Scalar.fromString("73eda753299d7d483339d80809a1d80553bda402fffe5bfeffffffff00000001",16);
} else if (["BN-128", "BN128", "BN254", "BN-254"]. indexOf(argv.prime.toUpperCase()) >=0) {
options.prime = Scalar.fromString("21888242871839275222246405745257275088548364400416034343698204186575808495617");
} else { } else {
watDone = true;
options.prime = Scalar.fromString(argv.prime);
} }
await compiler(fullFileName, options);
if (options.cSourceFile) await options.cSourceFile.close();
if (options.wasmFile) await options.wasmFile.close();
if (options.watFile) await options.watFile.close();
let symDone = false;
if (options.symWriteStream) { if (options.symWriteStream) {
options.symWriteStream.on("finish", () => { options.symWriteStream.on("finish", () => {
symDone = true; symDone = true;
@ -150,12 +129,17 @@ compiler(fullFileName, options).then( () => {
symDone = true; symDone = true;
} }
function finishIfDone() { function finishIfDone() {
if ((cSourceDone)&&(symDone)&&(wasmDone)&&(watDone)) {
if (symDone) {
setTimeout(() => { setTimeout(() => {
process.exit(0); process.exit(0);
}, 300); }, 300);
} }
} }
}
run().then(()=> {
process.exit(0);
}, (err) => { }, (err) => {
// console.log(err); // console.log(err);
console.log(err.stack); console.log(err.stack);
@ -172,5 +156,3 @@ compiler(fullFileName, options).then( () => {
}); });

+ 13
- 2
ports/c/builder.js

@ -631,7 +631,8 @@ class BuilderC {
} }
build() {
async build(fd) {
const encoder = new TextEncoder("utf-8");
const code=new BigArray(); const code=new BigArray();
this._buildHeader(code); this._buildHeader(code);
this._buildSizes(code); this._buildSizes(code);
@ -643,7 +644,17 @@ class BuilderC {
this._buildMapIsInput(code); this._buildMapIsInput(code);
this._buildWit2Sig(code); this._buildWit2Sig(code);
this._buildCircuitVar(code); this._buildCircuitVar(code);
return streamFromMultiArray(code);
await writeCode(code);
async function writeCode(c) {
if (c.push) {
for (let i=0; i<c.length; i++) {
await writeCode(c[i]);
}
} else if (typeof c === "string") {
await fd.write(encoder.encode(c + "\n"));
}
}
} }
} }

+ 17
- 3
ports/wasm/builder.js

@ -990,7 +990,8 @@ class BuilderWasm {
} }
build(outType) {
async build(fd, outType) {
const encoder = new TextEncoder("utf-8");
let module; let module;
if (outType == "wasm") { if (outType == "wasm") {
module=new ModuleBuilder(); module=new ModuleBuilder();
@ -1017,12 +1018,25 @@ class BuilderWasm {
module.setMemory(2000); module.setMemory(2000);
if (outType == "wasm") { if (outType == "wasm") {
return streamFromArrayBin(module.build());
const bytes = module.build();
const bytesArr = new Uint8Array(bytes);
await fd.write(bytesArr);
} else if (outType == "wat") { } else if (outType == "wat") {
return streamFromArrayTxt(module.build());
const code = module.build();
await writeCode(code);
} else { } else {
assert(false); assert(false);
} }
async function writeCode(c) {
if (c.push) {
for (let i=0; i<c.length; i++) {
await writeCode(c[i]);
}
} else if (typeof c === "string") {
await fd.write(encoder.encode(c + "\n"));
}
}
} }
} }

+ 7
- 14
src/compiler.js

@ -129,36 +129,29 @@ async function compile(srcFile, options) {
delete ctx.constraints; // Liberate memory. delete ctx.constraints; // Liberate memory.
if (options.cSourceWriteStream) {
if (options.cSourceFile) {
if (ctx.verbose) console.log("Generating c..."); if (ctx.verbose) console.log("Generating c...");
measures.generateC = -performance.now(); measures.generateC = -performance.now();
ctx.builder = new BuilderC(options.prime, ctx.verbose); ctx.builder = new BuilderC(options.prime, ctx.verbose);
build(ctx); build(ctx);
const rdStream = ctx.builder.build();
rdStream.pipe(options.cSourceWriteStream);
await ctx.builder.build(options.cSourceFile);
measures.generateC += performance.now(); measures.generateC += performance.now();
// await new Promise(fulfill => options.cSourceWriteStream.on("finish", fulfill));
} }
if (ctx.error) throw(ctx.error); if (ctx.error) throw(ctx.error);
if ((options.wasmWriteStream)||(options.watWriteStream)) {
if ((options.wasmFile)||(options.watFile)) {
if (ctx.verbose) console.log("Generating wasm..."); if (ctx.verbose) console.log("Generating wasm...");
measures.generateWasm = -performance.now(); measures.generateWasm = -performance.now();
ctx.builder = new BuilderWasm(options.prime); ctx.builder = new BuilderWasm(options.prime);
build(ctx); build(ctx);
if (options.wasmWriteStream) {
const rdStream = ctx.builder.build("wasm");
rdStream.pipe(options.wasmWriteStream);
if (options.wasmFile) {
await ctx.builder.build(options.wasmFile, "wasm");
} }
if (options.watWriteStream) {
const rdStream = ctx.builder.build("wat");
rdStream.pipe(options.watWriteStream);
if (options.watFile) {
await ctx.builder.build(options.watFile, "wat");
} }
measures.generateWasm += performance.now(); measures.generateWasm += performance.now();
// await new Promise(fulfill => options.wasmWriteStream.on("finish", fulfill));
} }
// const mainCode = gen(ctx,ast); // const mainCode = gen(ctx,ast);

Loading…
Cancel
Save