You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

162 lines
5.5 KiB

4 years ago
5 years ago
5 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
5 years ago
4 years ago
  1. #!/usr/bin/env node
  2. /*
  3. Copyright 2018 0KIMS association.
  4. This file is part of circom (Zero Knowledge Circuit Compiler).
  5. circom is a free software: you can redistribute it and/or modify it
  6. under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9. circom is distributed in the hope that it will be useful, but WITHOUT
  10. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  11. or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
  12. License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with circom. If not, see <https://www.gnu.org/licenses/>.
  15. */
  16. /* eslint-disable no-console */
  17. const fs = require("fs");
  18. const path = require("path");
  19. const Scalar = require("ffjavascript").Scalar;
  20. const stringifyBigInts = require("ffjavascript").utils.stringifyBigInts;
  21. const fastFile = require("fastfile");
  22. const compiler = require("./src/compiler");
  23. const version = require("./package").version;
  24. const argv = require("yargs")
  25. .version(version)
  26. .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]")
  27. .alias("o", "output")
  28. .alias("c", "csource")
  29. .alias("w", "wasm")
  30. .alias("t", "wat")
  31. .alias("s", "sym")
  32. .alias("r", "r1cs")
  33. .alias("p", "prime")
  34. .alias("n", "newThreadTemplates")
  35. .help("h")
  36. .alias("h", "help")
  37. .option("verbose", {
  38. alias: "v",
  39. type: "boolean",
  40. description: "Run with verbose logging"
  41. })
  42. .option("fast", {
  43. alias: "f",
  44. type: "boolean",
  45. description: "Do not optimize constraints"
  46. })
  47. .epilogue(`Copyright (C) 2018 0kims association
  48. This program comes with ABSOLUTELY NO WARRANTY;
  49. This is free software, and you are welcome to redistribute it
  50. under certain conditions; see the COPYING file in the official
  51. repo directory at https://github.com/iden3/circom `)
  52. .argv;
  53. async function run() {
  54. let inputFile;
  55. if (argv._.length == 0) {
  56. inputFile = "circuit.circom";
  57. } else if (argv._.length == 1) {
  58. inputFile = argv._[0];
  59. } else {
  60. console.log("Only one circuit at a time is permited");
  61. process.exit(1);
  62. }
  63. const fullFileName = path.resolve(process.cwd(), inputFile);
  64. const fileName = path.basename(fullFileName, ".circom");
  65. const cSourceName = typeof(argv.csource) === "string" ? argv.csource : fileName + ".cpp";
  66. const wasmName = typeof(argv.wasm) === "string" ? argv.wasm : fileName + ".wasm";
  67. const watName = typeof(argv.wat) === "string" ? argv.wat : fileName + ".wat";
  68. const r1csName = typeof(argv.r1cs) === "string" ? argv.r1cs : fileName + ".r1cs";
  69. const symName = typeof(argv.sym) === "string" ? argv.sym : fileName + ".sym";
  70. const options = {};
  71. options.reduceConstraints = !argv.fast;
  72. options.verbose = argv.verbose || false;
  73. options.sanityCheck = argv.sanitycheck;
  74. if (argv.csource) {
  75. options.cSourceFile = await fastFile.createOverride(cSourceName);
  76. const noExt = cSourceName.substr(0, cSourceName.lastIndexOf(".")) || cSourceName;
  77. options.dataFile = await fastFile.createOverride(noExt+".dat");
  78. }
  79. if (argv.wasm) {
  80. options.wasmFile = await fastFile.createOverride(wasmName);
  81. }
  82. if (argv.wat) {
  83. options.watFile = await fastFile.createOverride(watName);
  84. }
  85. if (argv.r1cs) {
  86. options.r1csFileName = r1csName;
  87. }
  88. if (argv.sym) {
  89. options.symWriteStream = fs.createWriteStream(symName);
  90. }
  91. if (argv.newThreadTemplates) {
  92. options.newThreadTemplates = new RegExp(argv.newThreadTemplates);
  93. }
  94. if (!argv.prime) {
  95. options.prime = Scalar.fromString("21888242871839275222246405745257275088548364400416034343698204186575808495617");
  96. } else if (["BLS12-381", "BLS12381"]. indexOf(argv.prime.toUpperCase()) >=0) {
  97. options.prime = Scalar.fromString("73eda753299d7d483339d80809a1d80553bda402fffe5bfeffffffff00000001",16);
  98. } else if (["BN-128", "BN128", "BN254", "BN-254"]. indexOf(argv.prime.toUpperCase()) >=0) {
  99. options.prime = Scalar.fromString("21888242871839275222246405745257275088548364400416034343698204186575808495617");
  100. } else {
  101. options.prime = Scalar.fromString(argv.prime);
  102. }
  103. await compiler(fullFileName, options);
  104. if (options.cSourceFile) await options.cSourceFile.close();
  105. if (options.dataFile) await options.dataFile.close();
  106. if (options.wasmFile) await options.wasmFile.close();
  107. if (options.watFile) await options.watFile.close();
  108. let symDone = false;
  109. if (options.symWriteStream) {
  110. options.symWriteStream.on("finish", () => {
  111. symDone = true;
  112. finishIfDone();
  113. });
  114. } else {
  115. symDone = true;
  116. }
  117. function finishIfDone() {
  118. if (symDone) {
  119. setTimeout(() => {
  120. process.exit(0);
  121. }, 300);
  122. }
  123. }
  124. }
  125. run().then(()=> {
  126. process.exit(0);
  127. }, (err) => {
  128. // console.log(err);
  129. console.log(err.stack);
  130. if (err.pos) {
  131. console.error(`ERROR at ${err.errFile}:${err.pos.first_line},${err.pos.first_column}-${err.pos.last_line},${err.pos.last_column} ${err.errStr}`);
  132. } else {
  133. console.log(err.message);
  134. if (argv.verbose) console.log(err.stack);
  135. }
  136. if (err.ast) {
  137. console.error(JSON.stringify(stringifyBigInts(err.ast), null, 1));
  138. }
  139. process.exit(1);
  140. });