arnaucube
arnaucube/circomPublic Self-hosted repository
circom/cli.js
cli.jsRaw
  1#!/usr/bin/env node
  2
  3/*
  4    Copyright 2018 0KIMS association.
  5
  6    This file is part of circom (Zero Knowledge Circuit Compiler).
  7
  8    circom is a free software: you can redistribute it and/or modify it
  9    under the terms of the GNU General Public License as published by
 10    the Free Software Foundation, either version 3 of the License, or
 11    (at your option) any later version.
 12
 13    circom is distributed in the hope that it will be useful, but WITHOUT
 14    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 15    or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
 16    License for more details.
 17
 18    You should have received a copy of the GNU General Public License
 19    along with circom. If not, see <https://www.gnu.org/licenses/>.
 20*/
 21
 22/* eslint-disable no-console */
 23
 24const fs = require("fs");
 25const path = require("path");
 26
 27const compiler = require("./src/compiler");
 28
 29const version = require("./package").version;
 30
 31const argv = require("yargs")
 32    .version(version)
 33    .usage("circom [input source circuit file] -o [output definition circuit file] -c [output c file]")
 34    .alias("o", "output")
 35    .alias("c", "csource")
 36    .alias("s", "sym")
 37    .alias("r", "r1cs")
 38    .alias("n", "newThreadTemplates")
 39    .help("h")
 40    .alias("h", "help")
 41    .option("verbose", {
 42        alias: "v",
 43        type: "boolean",
 44        description: "Run with verbose logging"
 45    })
 46    .option("fast", {
 47        alias: "f",
 48        type: "boolean",
 49        description: "Do not optimize constraints"
 50    })
 51    .epilogue(`Copyright (C) 2018  0kims association
 52    This program comes with ABSOLUTELY NO WARRANTY;
 53    This is free software, and you are welcome to redistribute it
 54    under certain conditions; see the COPYING file in the official
 55    repo directory at  https://github.com/iden3/circom `)
 56    .argv;
 57
 58
 59let inputFile;
 60if (argv._.length == 0) {
 61    inputFile = "circuit.circom";
 62} else if (argv._.length == 1) {
 63    inputFile = argv._[0];
 64} else  {
 65    console.log("Only one circuit at a time is permited");
 66    process.exit(1);
 67}
 68
 69const fullFileName = path.resolve(process.cwd(), inputFile);
 70const fileName = path.basename(fullFileName, ".circom");
 71const cSourceName = typeof(argv.csource) === "string" ?  argv.csource : fileName + ".cpp";
 72const r1csName = typeof(argv.r1cs) === "string" ?  argv.r1cs : fileName + ".r1cs";
 73const symName = typeof(argv.sym) === "string" ?  argv.sym : fileName + ".sym";
 74
 75const options = {};
 76options.reduceConstraints = !argv.fast;
 77options.verbose = argv.verbose || false;
 78if (argv.csource) {
 79    options.cSourceWriteStream = fs.createWriteStream(cSourceName);
 80}
 81if (argv.r1cs) {
 82    options.r1csFileName = r1csName;
 83}
 84if (argv.sym) {
 85    options.symWriteStream = fs.createWriteStream(symName);
 86}
 87if (argv.newThreadTemplates) {
 88    options.newThreadTemplates = new RegExp(argv.newThreadTemplates);
 89}
 90
 91compiler(fullFileName, options).then( () => {
 92    let cSourceDone = false;
 93    let symDone = false;
 94    if (options.cSourceWriteStream) {
 95        options.cSourceWriteStream.end(() => {
 96            cSourceDone = true;
 97            finishIfDone();
 98        });
 99    } else {
100        cSourceDone = true;
101    }
102    if (options.symWriteStream) {
103        options.symWriteStream.end(() => {
104            symDone = true;
105            finishIfDone();
106        });
107    } else {
108        symDone = true;
109    }
110    function finishIfDone() {
111        if ((cSourceDone)&&(symDone)) {
112            setTimeout(() => {
113                process.exit(0);
114            }, 300);
115        }
116    }
117}, (err) => {
118//    console.log(err);
119    console.log(err.stack);
120    if (err.pos) {
121        console.error(`ERROR at ${err.errFile}:${err.pos.first_line},${err.pos.first_column}-${err.pos.last_line},${err.pos.last_column}   ${err.errStr}`);
122    } else {
123        console.log(err.message);
124        if (argv.verbose) console.log(err.stack);
125    }
126    if (err.ast) {
127        console.error(JSON.stringify(err.ast, null, 1));
128    }
129    process.exit(1);
130});
131
132
133
134