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.

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