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