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.

148 lines
4.3 KiB

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