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