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.

228 lines
7.5 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. /*
  2. Copyright 2018 0kims association.
  3. This file is part of zksnark JavaScript library.
  4. zksnark JavaScript library is a free software: you can redistribute it and/or
  5. modify it under the terms of the GNU General Public License as published by the
  6. Free Software Foundation, either version 3 of the License, or (at your option)
  7. 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 MERCHANTABILITY
  10. or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. more details.
  12. You should have received a copy of the GNU General Public License along with
  13. zksnark JavaScript library. If not, see <https://www.gnu.org/licenses/>.
  14. */
  15. const bigInt = require("./bigint");
  16. module.exports = calculateWitness;
  17. function calculateWitness(circuit, inputSignals, log) {
  18. log = log || (() => {});
  19. const ctx = new RTCtx(circuit, log);
  20. function iterateSelector(values, sels, cb) {
  21. if (!Array.isArray(values)) {
  22. return cb(sels, values);
  23. }
  24. for (let i=0; i<values.length; i++) {
  25. sels.push(i);
  26. iterateSelector(values[i], sels, cb);
  27. sels.pop(i);
  28. }
  29. }
  30. ctx.setSignal("one", [], bigInt(1));
  31. for (let c in ctx.notInitSignals) {
  32. if (ctx.notInitSignals[c] == 0) ctx.triggerComponent(c);
  33. }
  34. for (let s in inputSignals) {
  35. ctx.currentComponent = "main";
  36. iterateSelector(inputSignals[s], [], function(selector, value) {
  37. ctx.setSignal(s, selector, bigInt(value));
  38. });
  39. }
  40. for (let i=0; i<circuit.nInputs; i++) {
  41. const idx = circuit.inputIdx(i);
  42. if (typeof(ctx.witness[idx]) == "undefined") {
  43. throw new Error("Input Signal not assigned: " + circuit.signalNames(i));
  44. }
  45. }
  46. for (let i=0; i<ctx.witness.length; i++) {
  47. if (typeof(ctx.witness[i]) == "undefined") {
  48. throw new Error("Signal not assigned: " + circuit.signalNames(i));
  49. }
  50. log(circuit.signalNames(i) + " --> " + ctx.witness[i].toString());
  51. }
  52. return ctx.witness.slice(0, circuit.nVars);
  53. }
  54. class RTCtx {
  55. constructor(circuit, log) {
  56. this.log = log || function() {};
  57. this.scopes = [];
  58. this.circuit = circuit;
  59. this.witness = new Array(circuit.nSignals);
  60. this.notInitSignals = {};
  61. for (let c in this.circuit.components) {
  62. this.notInitSignals[c] = this.circuit.components[c].inputSignals;
  63. }
  64. }
  65. _sels2str(sels) {
  66. let res = "";
  67. for (let i=0; i<sels.length; i++) {
  68. res += `[${sels[i]}]`;
  69. }
  70. return res;
  71. }
  72. setPin(componentName, componentSels, signalName, signalSels, value) {
  73. let fullName = componentName=="one" ? "one" : this.currentComponent + "." + componentName;
  74. fullName += this._sels2str(componentSels) +
  75. "."+
  76. signalName+
  77. this._sels2str(signalSels);
  78. this.setSignalFullName(fullName, value);
  79. }
  80. setSignal(name, sels, value) {
  81. let fullName = this.currentComponent ? this.currentComponent + "." + name : name;
  82. fullName += this._sels2str(sels);
  83. this.setSignalFullName(fullName, value);
  84. }
  85. triggerComponent(c) {
  86. this.log("Component Treiggered: " + c);
  87. // Set notInitSignals to -1 to not initialize again
  88. this.notInitSignals[c] --;
  89. const oldComponent = this.currentComponent;
  90. this.currentComponent = this.circuit.components[c].name;
  91. const template = this.circuit.components[c].template;
  92. const newScope = {};
  93. for (let p in this.circuit.components[c].params) {
  94. newScope[p] = this.circuit.components[c].params[p];
  95. }
  96. const oldScope = this.scopes;
  97. this.scopes = [ this.scopes[0], newScope ];
  98. // TODO set params.
  99. this.circuit.templates[template](this);
  100. this.scopes = oldScope;
  101. this.currentComponent = oldComponent;
  102. }
  103. callFunction(functionName, params) {
  104. const newScope = {};
  105. for (let p=0; p<this.circuit.functions[functionName].params.length; p++) {
  106. const paramName = this.circuit.functions[functionName].params[p];
  107. newScope[paramName] = params[p];
  108. }
  109. const oldScope = this.scopes;
  110. this.scopes = [ this.scopes[0], newScope ];
  111. // TODO set params.
  112. const res = this.circuit.functions[functionName].func(this);
  113. this.scopes = oldScope;
  114. return res;
  115. }
  116. setSignalFullName(fullName, value) {
  117. this.log("set " + fullName + " <-- " + value.toString());
  118. const sId = this.circuit.getSignalIdx(fullName);
  119. let firstInit =false;
  120. if (typeof(this.witness[sId]) == "undefined") {
  121. firstInit = true;
  122. }
  123. this.witness[sId] = value;
  124. const callComponents = [];
  125. for (let i=0; i<this.circuit.signals[sId].triggerComponents.length; i++) {
  126. var idCmp = this.circuit.signals[sId].triggerComponents[i];
  127. if (firstInit) this.notInitSignals[idCmp] --;
  128. callComponents.push(idCmp);
  129. }
  130. callComponents.map( (c) => {
  131. if (this.notInitSignals[c] == 0) this.triggerComponent(c);
  132. });
  133. return value;
  134. }
  135. setVar(name, sels, value) {
  136. function setVarArray(a, sels2, value) {
  137. if (sels2.length == 1) {
  138. a[sels2[0]] = value;
  139. } else {
  140. if (typeof(a[sels2[0]]) == "undefined") a[sels2[0]] = [];
  141. setVarArray(a[sels2[0]], sels2.slice(1), value);
  142. }
  143. }
  144. const scope = this.scopes[this.scopes.length-1];
  145. if (sels.length == 0) {
  146. scope[name] = value;
  147. } else {
  148. if (typeof(scope[name]) == "undefined") scope[name] = [];
  149. setVarArray(scope[name], sels, value);
  150. }
  151. return value;
  152. }
  153. getVar(name, sels) {
  154. function select(a, sels2) {
  155. return (sels2.length == 0) ? a : select(a[sels2[0]], sels2.slice(1));
  156. }
  157. for (let i=this.scopes.length-1; i>=0; i--) {
  158. if (typeof(this.scopes[i][name]) != "undefined") return select(this.scopes[i][name], sels);
  159. }
  160. throw new Error("Variable not defined: " + name);
  161. }
  162. getSignal(name, sels) {
  163. let fullName = name=="one" ? "one" : this.currentComponent + "." + name;
  164. fullName += this._sels2str(sels);
  165. return this.getSignalFullName(fullName);
  166. }
  167. getPin(componentName, componentSels, signalName, signalSels) {
  168. let fullName = componentName=="one" ? "one" : this.currentComponent + "." + componentName;
  169. fullName += this._sels2str(componentSels) +
  170. "."+
  171. signalName+
  172. this._sels2str(signalSels);
  173. return this.getSignalFullName(fullName);
  174. }
  175. getSignalFullName(fullName) {
  176. const sId = this.circuit.getSignalIdx(fullName);
  177. if (typeof(this.witness[sId]) == "undefined") {
  178. throw new Error("Signal not initialized: "+fullName);
  179. }
  180. this.log("get --->" + fullName + " = " + this.witness[sId].toString() );
  181. return this.witness[sId];
  182. }
  183. assert(a,b) {
  184. const ba = bigInt(a);
  185. const bb = bigInt(b);
  186. if (!ba.equals(bb)) {
  187. throw new Error("Constraint doesn't match: " + ba.toString() + " != " + bb.toString());
  188. }
  189. }
  190. }