mirror of
https://github.com/arnaucube/circom.git
synced 2026-02-06 18:56:40 +01:00
Remove streams c and wasm gen
This commit is contained in:
138
cli.js
138
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;
|
async function run() {
|
||||||
if (argv._.length == 0) {
|
let inputFile;
|
||||||
inputFile = "circuit.circom";
|
if (argv._.length == 0) {
|
||||||
} else if (argv._.length == 1) {
|
inputFile = "circuit.circom";
|
||||||
inputFile = argv._[0];
|
} else if (argv._.length == 1) {
|
||||||
} else {
|
inputFile = argv._[0];
|
||||||
console.log("Only one circuit at a time is permited");
|
} else {
|
||||||
process.exit(1);
|
console.log("Only one circuit at a time is permited");
|
||||||
}
|
process.exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
const fullFileName = path.resolve(process.cwd(), inputFile);
|
const fullFileName = path.resolve(process.cwd(), inputFile);
|
||||||
const fileName = path.basename(fullFileName, ".circom");
|
const fileName = path.basename(fullFileName, ".circom");
|
||||||
const cSourceName = typeof(argv.csource) === "string" ? argv.csource : fileName + ".cpp";
|
const cSourceName = typeof(argv.csource) === "string" ? argv.csource : fileName + ".cpp";
|
||||||
const wasmName = typeof(argv.wasm) === "string" ? argv.wasm : fileName + ".wasm";
|
const wasmName = typeof(argv.wasm) === "string" ? argv.wasm : fileName + ".wasm";
|
||||||
const watName = typeof(argv.wat) === "string" ? argv.wat : fileName + ".wat";
|
const watName = typeof(argv.wat) === "string" ? argv.wat : fileName + ".wat";
|
||||||
const r1csName = typeof(argv.r1cs) === "string" ? argv.r1cs : fileName + ".r1cs";
|
const r1csName = typeof(argv.r1cs) === "string" ? argv.r1cs : fileName + ".r1cs";
|
||||||
const symName = typeof(argv.sym) === "string" ? argv.sym : fileName + ".sym";
|
const symName = typeof(argv.sym) === "string" ? argv.sym : fileName + ".sym";
|
||||||
|
|
||||||
const options = {};
|
const options = {};
|
||||||
options.reduceConstraints = !argv.fast;
|
options.reduceConstraints = !argv.fast;
|
||||||
options.verbose = argv.verbose || false;
|
options.verbose = argv.verbose || false;
|
||||||
options.sanityCheck = argv.sanitycheck;
|
options.sanityCheck = argv.sanitycheck;
|
||||||
|
|
||||||
if (argv.csource) {
|
if (argv.csource) {
|
||||||
options.cSourceWriteStream = fs.createWriteStream(cSourceName);
|
options.cSourceFile = await fastFile.createOverride(cSourceName);
|
||||||
}
|
}
|
||||||
if (argv.wasm) {
|
if (argv.wasm) {
|
||||||
options.wasmWriteStream = fs.createWriteStream(wasmName);
|
options.wasmFile = await fastFile.createOverride(wasmName);
|
||||||
}
|
}
|
||||||
if (argv.wat) {
|
if (argv.wat) {
|
||||||
options.watWriteStream = fs.createWriteStream(watName);
|
options.watFile = await fastFile.createOverride(watName);
|
||||||
}
|
}
|
||||||
if (argv.r1cs) {
|
if (argv.r1cs) {
|
||||||
options.r1csFileName = r1csName;
|
options.r1csFileName = r1csName;
|
||||||
}
|
}
|
||||||
if (argv.sym) {
|
if (argv.sym) {
|
||||||
options.symWriteStream = fs.createWriteStream(symName);
|
options.symWriteStream = fs.createWriteStream(symName);
|
||||||
}
|
}
|
||||||
if (argv.newThreadTemplates) {
|
if (argv.newThreadTemplates) {
|
||||||
options.newThreadTemplates = new RegExp(argv.newThreadTemplates);
|
options.newThreadTemplates = new RegExp(argv.newThreadTemplates);
|
||||||
}
|
}
|
||||||
if (!argv.prime) {
|
if (!argv.prime) {
|
||||||
options.prime = Scalar.fromString("21888242871839275222246405745257275088548364400416034343698204186575808495617");
|
options.prime = Scalar.fromString("21888242871839275222246405745257275088548364400416034343698204186575808495617");
|
||||||
} else if (["BLS12-381", "BLS12381"]. indexOf(argv.prime.toUpperCase()) >=0) {
|
} else if (["BLS12-381", "BLS12381"]. indexOf(argv.prime.toUpperCase()) >=0) {
|
||||||
options.prime = Scalar.fromString("73eda753299d7d483339d80809a1d80553bda402fffe5bfeffffffff00000001",16);
|
options.prime = Scalar.fromString("73eda753299d7d483339d80809a1d80553bda402fffe5bfeffffffff00000001",16);
|
||||||
} else if (["BN-128", "BN128", "BN254", "BN-254"]. indexOf(argv.prime.toUpperCase()) >=0) {
|
} else if (["BN-128", "BN128", "BN254", "BN-254"]. indexOf(argv.prime.toUpperCase()) >=0) {
|
||||||
options.prime = Scalar.fromString("21888242871839275222246405745257275088548364400416034343698204186575808495617");
|
options.prime = Scalar.fromString("21888242871839275222246405745257275088548364400416034343698204186575808495617");
|
||||||
} else {
|
} else {
|
||||||
options.prime = Scalar.fromString(argv.prime);
|
options.prime = Scalar.fromString(argv.prime);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
compiler(fullFileName, options).then( () => {
|
await compiler(fullFileName, options);
|
||||||
let cSourceDone = false;
|
|
||||||
let wasmDone = false;
|
if (options.cSourceFile) await options.cSourceFile.close();
|
||||||
|
if (options.wasmFile) await options.wasmFile.close();
|
||||||
|
if (options.watFile) await options.watFile.close();
|
||||||
let symDone = false;
|
let symDone = false;
|
||||||
let watDone = false;
|
|
||||||
if (options.cSourceWriteStream) {
|
|
||||||
options.cSourceWriteStream.on("finish", () => {
|
|
||||||
cSourceDone = true;
|
|
||||||
finishIfDone();
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
cSourceDone = true;
|
|
||||||
}
|
|
||||||
if (options.wasmWriteStream) {
|
|
||||||
options.wasmWriteStream.on("finish", () => {
|
|
||||||
wasmDone = true;
|
|
||||||
finishIfDone();
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
wasmDone = true;
|
|
||||||
}
|
|
||||||
if (options.watWriteStream) {
|
|
||||||
options.watWriteStream.on("finish", () => {
|
|
||||||
watDone = true;
|
|
||||||
finishIfDone();
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
watDone = true;
|
|
||||||
}
|
|
||||||
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( () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -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"));
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -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"));
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -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();
|
await ctx.builder.build(options.cSourceFile);
|
||||||
rdStream.pipe(options.cSourceWriteStream);
|
|
||||||
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) {
|
if (options.wasmFile) {
|
||||||
const rdStream = ctx.builder.build("wasm");
|
await ctx.builder.build(options.wasmFile, "wasm");
|
||||||
rdStream.pipe(options.wasmWriteStream);
|
|
||||||
}
|
}
|
||||||
if (options.watWriteStream) {
|
if (options.watFile) {
|
||||||
const rdStream = ctx.builder.build("wat");
|
await ctx.builder.build(options.watFile, "wat");
|
||||||
rdStream.pipe(options.watWriteStream);
|
|
||||||
}
|
}
|
||||||
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);
|
||||||
|
|||||||
Reference in New Issue
Block a user