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.

158 lines
5.2 KiB

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