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