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.

163 lines
4.9 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  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 compiler = require("../../src/compiler");
  7. const utils = require("../../src/utils");
  8. const loadR1cs = require("r1csfile").load;
  9. const ZqField = require("ffjavascript").ZqField;
  10. const WitnessCalculatorBuilder = require("circom_runtime").WitnessCalculatorBuilder;
  11. module.exports = wasm_tester;
  12. async function wasm_tester(circomFile, _options) {
  13. tmp.setGracefulCleanup();
  14. const dir = await tmp.dir({prefix: "circom_", unsafeCleanup: true });
  15. // console.log(dir.path);
  16. const baseName = path.basename(circomFile, ".circom");
  17. const options = Object.assign({}, _options);
  18. options.wasmWriteStream = fs.createWriteStream(path.join(dir.path, baseName + ".wasm"));
  19. options.symWriteStream = fs.createWriteStream(path.join(dir.path, baseName + ".sym"));
  20. options.r1csFileName = path.join(dir.path, baseName + ".r1cs");
  21. const promisesArr = [];
  22. promisesArr.push(new Promise(fulfill => options.wasmWriteStream.on("finish", fulfill)));
  23. await compiler(circomFile, options);
  24. await Promise.all(promisesArr);
  25. const wasm = await fs.promises.readFile(path.join(dir.path, baseName + ".wasm"));
  26. const wc = await WitnessCalculatorBuilder(wasm);
  27. return new WasmTester(dir, baseName, wc);
  28. }
  29. class WasmTester {
  30. constructor(dir, baseName, witnessCalculator) {
  31. this.dir=dir;
  32. this.baseName = baseName;
  33. this.witnessCalculator = witnessCalculator;
  34. }
  35. async release() {
  36. await this.dir.cleanup();
  37. }
  38. async calculateWitness(input, sanityCheck) {
  39. return await this.witnessCalculator.calculateWitness(input, sanityCheck);
  40. }
  41. async loadSymbols() {
  42. if (this.symbols) return;
  43. this.symbols = {};
  44. const symsStr = await fs.promises.readFile(
  45. path.join(this.dir.path, this.baseName + ".sym"),
  46. "utf8"
  47. );
  48. const lines = symsStr.split("\n");
  49. for (let i=0; i<lines.length; i++) {
  50. const arr = lines[i].split(",");
  51. if (arr.length!=4) continue;
  52. this.symbols[arr[3]] = {
  53. labelIdx: Number(arr[0]),
  54. varIdx: Number(arr[1]),
  55. componentIdx: Number(arr[2]),
  56. };
  57. }
  58. }
  59. async loadConstraints() {
  60. const self = this;
  61. if (this.constraints) return;
  62. const r1cs = await loadR1cs(path.join(this.dir.path, this.baseName + ".r1cs"),true, false);
  63. self.F = new ZqField(r1cs.prime);
  64. self.nVars = r1cs.nVars;
  65. self.constraints = r1cs.constraints;
  66. }
  67. async assertOut(actualOut, expectedOut) {
  68. const self = this;
  69. if (!self.symbols) await self.loadSymbols();
  70. checkObject("main", expectedOut);
  71. function checkObject(prefix, eOut) {
  72. if (Array.isArray(eOut)) {
  73. for (let i=0; i<eOut.length; i++) {
  74. checkObject(prefix + "["+i+"]", eOut[i]);
  75. }
  76. } else if ((typeof eOut == "object")&&(eOut.constructor.name == "Object")) {
  77. for (let k in eOut) {
  78. checkObject(prefix + "."+k, eOut[k]);
  79. }
  80. } else {
  81. if (typeof self.symbols[prefix] == "undefined") {
  82. assert(false, "Output variable not defined: "+ prefix);
  83. }
  84. const ba = actualOut[self.symbols[prefix].varIdx].toString();
  85. const be = eOut.toString();
  86. assert.strictEqual(ba, be, prefix);
  87. }
  88. }
  89. }
  90. async getDecoratedOutput(witness) {
  91. const self = this;
  92. const lines = [];
  93. if (!self.symbols) await self.loadSymbols();
  94. for (let n in self.symbols) {
  95. let v;
  96. if (utils.isDefined(witness[self.symbols[n].varIdx])) {
  97. v = witness[self.symbols[n].varIdx].toString();
  98. } else {
  99. v = "undefined";
  100. }
  101. lines.push(`${n} --> ${v}`);
  102. }
  103. return lines.join("\n");
  104. }
  105. async checkConstraints(witness) {
  106. const self = this;
  107. if (!self.constraints) await self.loadConstraints();
  108. for (let i=0; i<self.constraints.length; i++) {
  109. checkConstraint(self.constraints[i]);
  110. }
  111. function checkConstraint(constraint) {
  112. const F = self.F;
  113. const a = evalLC(constraint[0]);
  114. const b = evalLC(constraint[1]);
  115. const c = evalLC(constraint[2]);
  116. assert (F.isZero(F.sub(F.mul(a,b), c)), "Constraint doesn't match");
  117. }
  118. function evalLC(lc) {
  119. const F = self.F;
  120. let v = F.zero;
  121. for (let w in lc) {
  122. v = F.add(
  123. v,
  124. F.mul( lc[w], witness[w] )
  125. );
  126. }
  127. return v;
  128. }
  129. }
  130. }