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;
 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        const noExt = cSourceName.substr(0, cSourceName.lastIndexOf(".")) || cSourceName;
 91        options.dataFile = await fastFile.createOverride(noExt+".dat");
 92    }
 93    if (argv.wasm) {
 94        options.wasmFile = await fastFile.createOverride(wasmName);
 95    }
 96    if (argv.wat) {
 97        options.watFile = await fastFile.createOverride(watName);
 98    }
 99    if (argv.r1cs) {
100        options.r1csFileName = r1csName;
101    }
102    if (argv.sym) {
103        options.symWriteStream = fs.createWriteStream(symName);
104    }
105    if (argv.newThreadTemplates) {
106        options.newThreadTemplates = new RegExp(argv.newThreadTemplates);
107    }
108    if (!argv.prime) {
109        options.prime = Scalar.fromString("21888242871839275222246405745257275088548364400416034343698204186575808495617");
110    } else if (["BLS12-381", "BLS12381"]. indexOf(argv.prime.toUpperCase()) >=0) {
111        options.prime = Scalar.fromString("73eda753299d7d483339d80809a1d80553bda402fffe5bfeffffffff00000001",16);
112    } else if (["BN-128", "BN128", "BN254", "BN-254"]. indexOf(argv.prime.toUpperCase()) >=0) {
113        options.prime = Scalar.fromString("21888242871839275222246405745257275088548364400416034343698204186575808495617");
114    } else {
115        options.prime = Scalar.fromString(argv.prime);
116    }
117
118
119    await compiler(fullFileName, options);
120
121    if (options.cSourceFile) await options.cSourceFile.close();
122    if (options.dataFile) await options.dataFile.close();
123    if (options.wasmFile) await options.wasmFile.close();
124    if (options.watFile) await options.watFile.close();
125    let symDone = false;
126    if (options.symWriteStream) {
127        options.symWriteStream.on("finish", () => {
128            symDone = true;
129            finishIfDone();
130        });
131    } else {
132        symDone = true;
133    }
134    function finishIfDone() {
135        if (symDone) {
136            setTimeout(() => {
137                process.exit(0);
138            }, 300);
139        }
140    }
141
142}
143
144run().then(()=> {
145    process.exit(0);
146}, (err) => {
147//    console.log(err);
148    console.log(err.stack);
149    if (err.pos) {
150        console.error(`ERROR at ${err.errFile}:${err.pos.first_line},${err.pos.first_column}-${err.pos.last_line},${err.pos.last_column}   ${err.errStr}`);
151    } else {
152        console.log(err.message);
153        if (argv.verbose) console.log(err.stack);
154    }
155    if (err.ast) {
156        console.error(JSON.stringify(err.ast, null, 1));
157    }
158    process.exit(1);
159});
160
161