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.

199 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. b = await exec("circom " + flags + fileName);
  37. assert(b.stderr == "",
  38. "circom compiler error \n" + b.stderr);
  39. }
  40. class WasmTester {
  41. constructor(dir, baseName, witnessCalculator) {
  42. this.dir=dir;
  43. this.baseName = baseName;
  44. this.witnessCalculator = witnessCalculator;
  45. }
  46. async release() {
  47. await this.dir.cleanup();
  48. }
  49. async calculateWitness(input, sanityCheck) {
  50. return await this.witnessCalculator.calculateWitness(input, sanityCheck);
  51. }
  52. async loadSymbols() {
  53. if (this.symbols) return;
  54. this.symbols = {};
  55. const symsStr = await fs.promises.readFile(
  56. path.join(this.dir.path, this.baseName + ".sym"),
  57. "utf8"
  58. );
  59. const lines = symsStr.split("\n");
  60. for (let i=0; i<lines.length; i++) {
  61. const arr = lines[i].split(",");
  62. if (arr.length!=4) continue;
  63. this.symbols[arr[3]] = {
  64. labelIdx: Number(arr[0]),
  65. varIdx: Number(arr[1]),
  66. componentIdx: Number(arr[2]),
  67. };
  68. }
  69. }
  70. async loadConstraints() {
  71. const self = this;
  72. if (this.constraints) return;
  73. const r1cs = await loadR1cs(path.join(this.dir.path, this.baseName + ".r1cs"),true, false);
  74. self.F = new ZqField(r1cs.prime);
  75. self.nVars = r1cs.nVars;
  76. self.constraints = r1cs.constraints;
  77. }
  78. async assertOut(actualOut, expectedOut) {
  79. const self = this;
  80. if (!self.symbols) await self.loadSymbols();
  81. checkObject("main", expectedOut);
  82. function checkObject(prefix, eOut) {
  83. if (Array.isArray(eOut)) {
  84. for (let i=0; i<eOut.length; i++) {
  85. checkObject(prefix + "["+i+"]", eOut[i]);
  86. }
  87. } else if ((typeof eOut == "object")&&(eOut.constructor.name == "Object")) {
  88. for (let k in eOut) {
  89. checkObject(prefix + "."+k, eOut[k]);
  90. }
  91. } else {
  92. if (typeof self.symbols[prefix] == "undefined") {
  93. assert(false, "Output variable not defined: "+ prefix);
  94. }
  95. const ba = actualOut[self.symbols[prefix].varIdx].toString();
  96. const be = eOut.toString();
  97. assert.strictEqual(ba, be, prefix);
  98. }
  99. }
  100. }
  101. async getDecoratedOutput(witness) {
  102. const self = this;
  103. const lines = [];
  104. if (!self.symbols) await self.loadSymbols();
  105. for (let n in self.symbols) {
  106. let v;
  107. if (utils.isDefined(witness[self.symbols[n].varIdx])) {
  108. v = witness[self.symbols[n].varIdx].toString();
  109. } else {
  110. v = "undefined";
  111. }
  112. lines.push(`${n} --> ${v}`);
  113. }
  114. return lines.join("\n");
  115. }
  116. async checkConstraints(witness) {
  117. const self = this;
  118. if (!self.constraints) await self.loadConstraints();
  119. for (let i=0; i<self.constraints.length; i++) {
  120. checkConstraint(self.constraints[i]);
  121. }
  122. function checkConstraint(constraint) {
  123. const F = self.F;
  124. const a = evalLC(constraint[0]);
  125. const b = evalLC(constraint[1]);
  126. const c = evalLC(constraint[2]);
  127. assert (F.isZero(F.sub(F.mul(a,b), c)), "Constraint doesn't match");
  128. }
  129. function evalLC(lc) {
  130. const F = self.F;
  131. let v = F.zero;
  132. for (let w in lc) {
  133. v = F.add(
  134. v,
  135. F.mul( lc[w], witness[w] )
  136. );
  137. }
  138. return v;
  139. }
  140. }
  141. }
  142. function version_to_list ( v ) {
  143. return v.split(".").map(function(x) {
  144. return parseInt(x, 10);
  145. });
  146. }
  147. function check_versions ( v1, v2 ) {
  148. //check if v1 is newer than or equal to v2
  149. for (let i = 0; i < v2.length; i++) {
  150. if (v1[i] > v2[i]) return true;
  151. if (v1[i] < v2[i]) return false;
  152. }
  153. return true;
  154. }
  155. async function compiler_above_version(v) {
  156. let output = await exec('circom --version').toString();
  157. let compiler_version = version_to_list(output.slice(output.search(/\d/),-1));
  158. vlist = version_to_list(v);
  159. return check_versions ( compiler_version, vlist );
  160. }