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.

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