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.

198 lines
5.8 KiB

  1. const chai = require("chai");
  2. const assert = chai.assert;
  3. const fs = require("fs");
  4. var tmp = require("tmp-promise");
  5. const path = require("path");
  6. const util = require("util");
  7. const exec = util.promisify(require("child_process").exec);
  8. const loadR1cs = require("r1csfile").load;
  9. const ZqField = require("ffjavascript").ZqField;
  10. module.exports = wasm_tester;
  11. async function wasm_tester(circomInput, _options) {
  12. assert(await compiler_above_version("2.0.0"),"Wrong compiler version. Must be at least 2.0.0");
  13. tmp.setGracefulCleanup();
  14. const dir = await tmp.dir({prefix: "circom_", unsafeCleanup: true });
  15. //console.log(dir.path);
  16. const baseName = path.basename(circomInput, ".circom");
  17. const options = Object.assign({}, _options);
  18. options.wasm = true;
  19. options.sym = true;
  20. options.json = options.json || false; // costraints in json format
  21. options.r1cs = true;
  22. options.output = dir.path;
  23. await compile(circomInput, options);
  24. const utils = require("./utils");
  25. const WitnessCalculator = require("./witness_calculator");
  26. const wasm = await fs.promises.readFile(path.join(dir.path, baseName+"_js/"+ baseName + ".wasm"));
  27. const wc = await WitnessCalculator(wasm);
  28. return new WasmTester(dir, baseName, wc);
  29. }
  30. async function compile (fileName, options) {
  31. var flags = "--wasm ";
  32. if (options.sym) flags += "--sym ";
  33. if (options.r1cs) flags += "--r1cs ";
  34. if (options.json) flags += "--json ";
  35. if (options.output) flags += "--output " + options.output + " ";
  36. console.log(circom + flags + fileName);
  37. await exec("circom " + flags + fileName);
  38. }
  39. class WasmTester {
  40. constructor(dir, baseName, witnessCalculator) {
  41. this.dir=dir;
  42. this.baseName = baseName;
  43. this.witnessCalculator = witnessCalculator;
  44. }
  45. async release() {
  46. await this.dir.cleanup();
  47. }
  48. async calculateWitness(input, sanityCheck) {
  49. return await this.witnessCalculator.calculateWitness(input, sanityCheck);
  50. }
  51. async loadSymbols() {
  52. if (this.symbols) return;
  53. this.symbols = {};
  54. const symsStr = await fs.promises.readFile(
  55. path.join(this.dir.path, this.baseName + ".sym"),
  56. "utf8"
  57. );
  58. const lines = symsStr.split("\n");
  59. for (let i=0; i<lines.length; i++) {
  60. const arr = lines[i].split(",");
  61. if (arr.length!=4) continue;
  62. this.symbols[arr[3]] = {
  63. labelIdx: Number(arr[0]),
  64. varIdx: Number(arr[1]),
  65. componentIdx: Number(arr[2]),
  66. };
  67. }
  68. }
  69. async loadConstraints() {
  70. const self = this;
  71. if (this.constraints) return;
  72. const r1cs = await loadR1cs(path.join(this.dir.path, this.baseName + ".r1cs"),true, false);
  73. self.F = new ZqField(r1cs.prime);
  74. self.nVars = r1cs.nVars;
  75. self.constraints = r1cs.constraints;
  76. }
  77. async assertOut(actualOut, expectedOut) {
  78. const self = this;
  79. if (!self.symbols) await self.loadSymbols();
  80. checkObject("main", expectedOut);
  81. function checkObject(prefix, eOut) {
  82. if (Array.isArray(eOut)) {
  83. for (let i=0; i<eOut.length; i++) {
  84. checkObject(prefix + "["+i+"]", eOut[i]);
  85. }
  86. } else if ((typeof eOut == "object")&&(eOut.constructor.name == "Object")) {
  87. for (let k in eOut) {
  88. checkObject(prefix + "."+k, eOut[k]);
  89. }
  90. } else {
  91. if (typeof self.symbols[prefix] == "undefined") {
  92. assert(false, "Output variable not defined: "+ prefix);
  93. }
  94. const ba = actualOut[self.symbols[prefix].varIdx].toString();
  95. const be = eOut.toString();
  96. assert.strictEqual(ba, be, prefix);
  97. }
  98. }
  99. }
  100. async getDecoratedOutput(witness) {
  101. const self = this;
  102. const lines = [];
  103. if (!self.symbols) await self.loadSymbols();
  104. for (let n in self.symbols) {
  105. let v;
  106. if (utils.isDefined(witness[self.symbols[n].varIdx])) {
  107. v = witness[self.symbols[n].varIdx].toString();
  108. } else {
  109. v = "undefined";
  110. }
  111. lines.push(`${n} --> ${v}`);
  112. }
  113. return lines.join("\n");
  114. }
  115. async checkConstraints(witness) {
  116. const self = this;
  117. if (!self.constraints) await self.loadConstraints();
  118. for (let i=0; i<self.constraints.length; i++) {
  119. checkConstraint(self.constraints[i]);
  120. }
  121. function checkConstraint(constraint) {
  122. const F = self.F;
  123. const a = evalLC(constraint[0]);
  124. const b = evalLC(constraint[1]);
  125. const c = evalLC(constraint[2]);
  126. assert (F.isZero(F.sub(F.mul(a,b), c)), "Constraint doesn't match");
  127. }
  128. function evalLC(lc) {
  129. const F = self.F;
  130. let v = F.zero;
  131. for (let w in lc) {
  132. v = F.add(
  133. v,
  134. F.mul( lc[w], witness[w] )
  135. );
  136. }
  137. return v;
  138. }
  139. }
  140. }
  141. function version_to_list ( v ) {
  142. return v.split(".").map(function(x) {
  143. return parseInt(x, 10);
  144. });
  145. }
  146. function check_versions ( v1, v2 ) {
  147. //check if v1 is newer than or equal to v2
  148. for (let i = 0; i < v2.length; i++) {
  149. if (v1[i] > v2[i]) return true;
  150. if (v1[i] < v2[i]) return false;
  151. }
  152. return true;
  153. }
  154. async function compiler_above_version(v) {
  155. let output = await exec('circom --version').toString();
  156. let compiler_version = version_to_list(output.slice(output.search(/\d/),-1));
  157. vlist = version_to_list(v);
  158. return check_versions ( compiler_version, vlist );
  159. }