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 .help("h")
39 .alias("h", "help")
40 .option("verbose", {
41 alias: "v",
42 type: "boolean",
43 description: "Run with verbose logging"
44 })
45 .option("fast", {
46 alias: "f",
47 type: "boolean",
48 description: "Do not optimize constraints"
49 })
50 .epilogue(`Copyright (C) 2018 0kims association
51 This program comes with ABSOLUTELY NO WARRANTY;
52 This is free software, and you are welcome to redistribute it
53 under certain conditions; see the COPYING file in the official
54 repo directory at https://github.com/iden3/circom `)
55 .argv;
56
57
58let inputFile;
59if (argv._.length == 0) {
60 inputFile = "circuit.circom";
61} else if (argv._.length == 1) {
62 inputFile = argv._[0];
63} else {
64 console.log("Only one circuit at a time is permited");
65 process.exit(1);
66}
67
68const fullFileName = path.resolve(process.cwd(), inputFile);
69const fileName = path.basename(fullFileName, ".circom");
70const cSourceName = typeof(argv.csource) === "string" ? argv.csource : fileName + ".cpp";
71const r1csName = typeof(argv.r1cs) === "string" ? argv.r1cs : fileName + ".r1cs";
72const symName = typeof(argv.sym) === "string" ? argv.sym : fileName + ".sym";
73
74const options = {};
75options.reduceConstraints = !argv.fast;
76options.verbose = argv.verbose || false;
77if (argv.csource) {
78 options.cSourceWriteStream = fs.createWriteStream(cSourceName);
79}
80if (argv.r1cs) {
81 options.r1csFileName = r1csName;
82}
83if (argv.sym) {
84 options.symWriteStream = fs.createWriteStream(symName);
85}
86
87compiler(fullFileName, options).then( () => {
88 let cSourceDone = false;
89 let symDone = false;
90 if (options.cSourceWriteStream) {
91 options.cSourceWriteStream.end(() => {
92 cSourceDone = true;
93 finishIfDone();
94 });
95 } else {
96 cSourceDone = true;
97 }
98 if (options.symWriteStream) {
99 options.symWriteStream.end(() => {
100 symDone = true;
101 finishIfDone();
102 });
103 } else {
104 symDone = true;
105 }
106 function finishIfDone() {
107 if ((cSourceDone)&&(symDone)) {
108 setTimeout(() => {
109 process.exit(0);
110 }, 300);
111 }
112 }
113}, (err) => {
114// console.log(err);
115 console.log(err.stack);
116 if (err.pos) {
117 console.error(`ERROR at ${err.errFile}:${err.pos.first_line},${err.pos.first_column}-${err.pos.last_line},${err.pos.last_column} ${err.errStr}`);
118 } else {
119 console.log(err.message);
120 if (argv.verbose) console.log(err.stack);
121 }
122 if (err.ast) {
123 console.error(JSON.stringify(err.ast, null, 1));
124 }
125 process.exit(1);
126});
127
128
129
130