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