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]")
34 .alias("o", "output")
35 .help("h")
36 .alias("h", "help")
37 .option("verbose", {
38 alias: "v",
39 type: "boolean",
40 description: "Run with verbose logging"
41 })
42 .option("fast", {
43 alias: "f",
44 type: "boolean",
45 description: "Do not optimize constraints"
46 })
47 .epilogue(`Copyright (C) 2018 0kims association
48 This program comes with ABSOLUTELY NO WARRANTY;
49 This is free software, and you are welcome to redistribute it
50 under certain conditions; see the COPYING file in the official
51 repo directory at https://github.com/iden3/circom `)
52 .argv;
53
54
55let inputFile;
56if (argv._.length == 0) {
57 inputFile = "circuit.circom";
58} else if (argv._.length == 1) {
59 inputFile = argv._[0];
60} else {
61 console.log("Only one circuit at a time is permited");
62 process.exit(1);
63}
64
65const fullFileName = path.resolve(process.cwd(), inputFile);
66const outName = argv.output ? argv.output : "circuit.json";
67
68compiler(fullFileName, {reduceConstraints: !argv.fast, verbose: argv.verbose}).then( (cir) => {
69 fs.writeFileSync(outName, JSON.stringify(cir, null, 1), "utf8");
70 process.exit(0);
71}, (err) => {
72// console.log(err);
73 console.log(err.stack);
74 if (err.pos) {
75 console.error(`ERROR at ${err.errFile}:${err.pos.first_line},${err.pos.first_column}-${err.pos.last_line},${err.pos.last_column} ${err.errStr}`);
76 } else {
77 console.log(err.message);
78 if (argv.verbose) console.log(err.stack);
79 }
80 if (err.ast) {
81 console.error(JSON.stringify(err.ast, null, 1));
82 }
83 process.exit(1);
84});
85
86
87
88