arnaucube
arnaucube/circomPublic Self-hosted repository
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;
 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;
 85
 86if (argv.csource) {
 87    options.cSourceWriteStream = fs.createWriteStream(cSourceName);
 88}
 89if (argv.wasm) {
 90    options.wasmWriteStream = fs.createWriteStream(wasmName);
 91}
 92if (argv.wat) {
 93    options.watWriteStream = fs.createWriteStream(watName);
 94}
 95if (argv.r1cs) {
 96    options.r1csFileName = r1csName;
 97}
 98if (argv.sym) {
 99    options.symWriteStream = fs.createWriteStream(symName);
100}
101if (argv.newThreadTemplates) {
102    options.newThreadTemplates = new RegExp(argv.newThreadTemplates);
103}
104if (!argv.prime) {
105    options.prime = Scalar.fromString("21888242871839275222246405745257275088548364400416034343698204186575808495617");
106} else if (["BLS12-381", "BLS12381"]. indexOf(argv.prime.toUpperCase()) >=0) {
107    options.prime = Scalar.fromString("73eda753299d7d483339d80809a1d80553bda402fffe5bfeffffffff00000001",16);
108} else if (["BN-128", "BN128", "BN254", "BN-254"]. indexOf(argv.prime.toUpperCase()) >=0) {
109    options.prime = Scalar.fromString("21888242871839275222246405745257275088548364400416034343698204186575808495617");
110} else {
111    options.prime = Scalar.fromString(argv.prime);
112}
113
114
115compiler(fullFileName, options).then( () => {
116    let cSourceDone = false;
117    let wasmDone = false;
118    let symDone = false;
119    let watDone = false;
120    if (options.cSourceWriteStream) {
121        options.cSourceWriteStream.on("finish", () => {
122            cSourceDone = true;
123            finishIfDone();
124        });
125    } else {
126        cSourceDone = true;
127    }
128    if (options.wasmWriteStream) {
129        options.wasmWriteStream.on("finish", () => {
130            wasmDone = true;
131            finishIfDone();
132        });
133    } else {
134        wasmDone = true;
135    }
136    if (options.watWriteStream) {
137        options.watWriteStream.on("finish", () => {
138            watDone = true;
139            finishIfDone();
140        });
141    } else {
142        watDone = true;
143    }
144    if (options.symWriteStream) {
145        options.symWriteStream.on("finish", () => {
146            symDone = true;
147            finishIfDone();
148        });
149    } else {
150        symDone = true;
151    }
152    function finishIfDone() {
153        if ((cSourceDone)&&(symDone)&&(wasmDone)&&(watDone)) {
154            setTimeout(() => {
155                process.exit(0);
156            }, 300);
157        }
158    }
159}, (err) => {
160//    console.log(err);
161    console.log(err.stack);
162    if (err.pos) {
163        console.error(`ERROR at ${err.errFile}:${err.pos.first_line},${err.pos.first_column}-${err.pos.last_line},${err.pos.last_column}   ${err.errStr}`);
164    } else {
165        console.log(err.message);
166        if (argv.verbose) console.log(err.stack);
167    }
168    if (err.ast) {
169        console.error(JSON.stringify(err.ast, null, 1));
170    }
171    process.exit(1);
172});
173
174
175
176