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