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