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.

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