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.

124 lines
4.3 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  1. /*
  2. Copyright 2018 0kims association
  3. This file is part of zksnark javascript library.
  4. zksnark javascript library is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. zksnark javascript library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with zksnark javascript library. If not, see <https://www.gnu.org/licenses/>.
  14. */
  15. const bigInt = require("./bigint.js");
  16. const __P__ = bigInt("21888242871839275222246405745257275088548364400416034343698204186575808495617");
  17. const __MASK__ = bigInt("28948022309329048855892746252171976963317496166410141009864396001978282409983"); // 0x3FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
  18. const calculateWitness = require("./calculateWitness.js");
  19. module.exports = class Circuit {
  20. constructor(circuitDef) {
  21. this.nPubInputs = circuitDef.nPubInputs;
  22. this.nPrvInputs = circuitDef.nPrvInputs;
  23. this.nInputs = circuitDef.nInputs;
  24. this.nOutputs = circuitDef.nOutputs;
  25. this.nVars = circuitDef.nVars;
  26. this.nSignals = circuitDef.nSignals;
  27. this.nConstants = circuitDef.nConstants;
  28. this.nConstraints = circuitDef.constraints.length;
  29. this.signalName2Idx = circuitDef.signalName2Idx;
  30. this.components = circuitDef.components;
  31. this.componentName2Idx = circuitDef.componentName2Idx;
  32. this.signals = circuitDef.signals;
  33. this.constraints = circuitDef.constraints;
  34. this.templates = {};
  35. for (let t in circuitDef.templates) {
  36. this.templates[t] = eval(" const __f= " +circuitDef.templates[t] + "\n__f");
  37. }
  38. this.functions = {};
  39. for (let f in circuitDef.functions) {
  40. this.functions[f] = {
  41. params: circuitDef.functions[f].params,
  42. func: eval(" const __f= " +circuitDef.functions[f].func + "\n__f;")
  43. };
  44. }
  45. }
  46. calculateWitness(input) {
  47. return calculateWitness(this, input);
  48. }
  49. getSignalIdx(name) {
  50. if (typeof(this.signalName2Idx[name]) != "undefined") return this.signalName2Idx[name];
  51. if (!isNaN(name)) return Number(name);
  52. throw new Error("Invalid signal identifier: ", name);
  53. }
  54. // returns the index of the i'th output
  55. outputIdx(i) {
  56. if (i>=this.nOutputs) throw new Error("Accessing an invalid output: "+i);
  57. return i+1;
  58. }
  59. // returns the index of the i'th input
  60. inputIdx(i) {
  61. if (i>=this.nInputs) throw new Error("Accessing an invalid input: "+i);
  62. return this.nOutputs + 1 + i;
  63. }
  64. // returns the index of the i'th public input
  65. pubInputIdx(i) {
  66. if (i>=this.nPubInputs) throw new Error("Accessing an invalid pubInput: "+i);
  67. return this.inputIdx(i);
  68. }
  69. // returns the index of the i'th private input
  70. prvInputIdx(i) {
  71. if (i>=this.nPrvInputs) throw new Error("Accessing an invalid prvInput: "+i);
  72. return this.inputIdx(this.nPubInputs + i);
  73. }
  74. // returns the index of the i'th variable
  75. varIdx(i) {
  76. if (i>=this.nVars) throw new Error("Accessing an invalid variable: "+i);
  77. return i;
  78. }
  79. // returns the index of the i'th constant
  80. constantIdx(i) {
  81. if (i>=this.nConstants) throw new Error("Accessing an invalid constant: "+i);
  82. return this.nVars + i;
  83. }
  84. // returns the index of the i'th signal
  85. signalIdx(i) {
  86. if (i>=this.nSignls) throw new Error("Accessing an invalid signal: "+i);
  87. return i;
  88. }
  89. signalNames(i) {
  90. return this.signals[ this.getSignalIdx(i) ].names.join(", ");
  91. }
  92. a(constraint, signalIdx) {
  93. return bigInt(this.constraints[constraint][0][signalIdx] || 0 );
  94. }
  95. b(constraint, signalIdx) {
  96. return bigInt(this.constraints[constraint][1][signalIdx] || 0);
  97. }
  98. c(constraint, signalIdx) {
  99. return bigInt(this.constraints[constraint][2][signalIdx] || 0);
  100. }
  101. };