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.

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