From 90cc7d5072a525a78bbaddb474e548b721668fd2 Mon Sep 17 00:00:00 2001 From: Jordi Baylina Date: Fri, 7 Aug 2020 17:10:30 +0200 Subject: [PATCH] Remove streams c and wasm gen --- cli.js | 140 ++++++++++++++++++------------------------ ports/c/builder.js | 15 ++++- ports/wasm/builder.js | 20 +++++- src/compiler.js | 21 +++---- 4 files changed, 98 insertions(+), 98 deletions(-) diff --git a/cli.js b/cli.js index 5ed2d58..a78e33f 100755 --- a/cli.js +++ b/cli.js @@ -24,6 +24,7 @@ const fs = require("fs"); const path = require("path"); const Scalar = require("ffjavascript").Scalar; +const fastFile = require("fastfile"); const compiler = require("./src/compiler"); @@ -60,87 +61,65 @@ const argv = require("yargs") .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 { - 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) { options.symWriteStream.on("finish", () => { symDone = true; @@ -150,12 +129,17 @@ compiler(fullFileName, options).then( () => { symDone = true; } function finishIfDone() { - if ((cSourceDone)&&(symDone)&&(wasmDone)&&(watDone)) { + if (symDone) { setTimeout(() => { process.exit(0); }, 300); } } + +} + +run().then(()=> { + process.exit(0); }, (err) => { // console.log(err); console.log(err.stack); @@ -172,5 +156,3 @@ compiler(fullFileName, options).then( () => { }); - - diff --git a/ports/c/builder.js b/ports/c/builder.js index cf60c1e..46fe097 100644 --- a/ports/c/builder.js +++ b/ports/c/builder.js @@ -631,7 +631,8 @@ class BuilderC { } - build() { + async build(fd) { + const encoder = new TextEncoder("utf-8"); const code=new BigArray(); this._buildHeader(code); this._buildSizes(code); @@ -643,7 +644,17 @@ class BuilderC { this._buildMapIsInput(code); this._buildWit2Sig(code); this._buildCircuitVar(code); - return streamFromMultiArray(code); + await writeCode(code); + + async function writeCode(c) { + if (c.push) { + for (let i=0; i options.cSourceWriteStream.on("finish", fulfill)); } if (ctx.error) throw(ctx.error); - if ((options.wasmWriteStream)||(options.watWriteStream)) { + if ((options.wasmFile)||(options.watFile)) { if (ctx.verbose) console.log("Generating wasm..."); measures.generateWasm = -performance.now(); ctx.builder = new BuilderWasm(options.prime); 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(); - - // await new Promise(fulfill => options.wasmWriteStream.on("finish", fulfill)); } // const mainCode = gen(ctx,ast);