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.

161 lines
5.4 KiB

5 years ago
5 years ago
5 years ago
4 years ago
5 years ago
4 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. const noExt = cSourceName.substr(0, cSourceName.lastIndexOf(".")) || cSourceName;
  76. options.dataFile = await fastFile.createOverride(noExt+".dat");
  77. }
  78. if (argv.wasm) {
  79. options.wasmFile = await fastFile.createOverride(wasmName);
  80. }
  81. if (argv.wat) {
  82. options.watFile = await fastFile.createOverride(watName);
  83. }
  84. if (argv.r1cs) {
  85. options.r1csFileName = r1csName;
  86. }
  87. if (argv.sym) {
  88. options.symWriteStream = fs.createWriteStream(symName);
  89. }
  90. if (argv.newThreadTemplates) {
  91. options.newThreadTemplates = new RegExp(argv.newThreadTemplates);
  92. }
  93. if (!argv.prime) {
  94. options.prime = Scalar.fromString("21888242871839275222246405745257275088548364400416034343698204186575808495617");
  95. } else if (["BLS12-381", "BLS12381"]. indexOf(argv.prime.toUpperCase()) >=0) {
  96. options.prime = Scalar.fromString("73eda753299d7d483339d80809a1d80553bda402fffe5bfeffffffff00000001",16);
  97. } else if (["BN-128", "BN128", "BN254", "BN-254"]. indexOf(argv.prime.toUpperCase()) >=0) {
  98. options.prime = Scalar.fromString("21888242871839275222246405745257275088548364400416034343698204186575808495617");
  99. } else {
  100. options.prime = Scalar.fromString(argv.prime);
  101. }
  102. await compiler(fullFileName, options);
  103. if (options.cSourceFile) await options.cSourceFile.close();
  104. if (options.dataFile) await options.dataFile.close();
  105. if (options.wasmFile) await options.wasmFile.close();
  106. if (options.watFile) await options.watFile.close();
  107. let symDone = false;
  108. if (options.symWriteStream) {
  109. options.symWriteStream.on("finish", () => {
  110. symDone = true;
  111. finishIfDone();
  112. });
  113. } else {
  114. symDone = true;
  115. }
  116. function finishIfDone() {
  117. if (symDone) {
  118. setTimeout(() => {
  119. process.exit(0);
  120. }, 300);
  121. }
  122. }
  123. }
  124. run().then(()=> {
  125. process.exit(0);
  126. }, (err) => {
  127. // console.log(err);
  128. console.log(err.stack);
  129. if (err.pos) {
  130. console.error(`ERROR at ${err.errFile}:${err.pos.first_line},${err.pos.first_column}-${err.pos.last_line},${err.pos.last_column} ${err.errStr}`);
  131. } else {
  132. console.log(err.message);
  133. if (argv.verbose) console.log(err.stack);
  134. }
  135. if (err.ast) {
  136. console.error(JSON.stringify(err.ast, null, 1));
  137. }
  138. process.exit(1);
  139. });