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.

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