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.

183 lines
5.7 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
  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("./compiler");
  7. const util = require("util");
  8. const exec = util.promisify(require("child_process").exec);
  9. const stringifyBigInts = require("./utils").stringifyBigInts;
  10. const unstringifyBigInts = require("./utils").unstringifyBigInts;
  11. const bigInt = require("big-integer");
  12. const utils = require("./utils");
  13. const loadR1cs = require("./r1csfile").loadR1cs;
  14. const ZqField = require("fflib").ZqField;
  15. module.exports = c_tester;
  16. async function c_tester(circomFile, _options) {
  17. tmp.setGracefulCleanup();
  18. const dir = await tmp.dir({prefix: "circom_", unsafeCleanup: true });
  19. const baseName = path.basename(circomFile, ".circom");
  20. const options = Object.assign({}, _options);
  21. options.cSourceWriteStream = fs.createWriteStream(path.join(dir.path, baseName + ".cpp"));
  22. options.symWriteStream = fs.createWriteStream(path.join(dir.path, baseName + ".sym"));
  23. options.r1csFileName = path.join(dir.path, baseName + ".r1cs");
  24. await compiler(circomFile, options);
  25. const cdir = path.join(__dirname, "..", "c");
  26. await exec("cp" +
  27. ` ${path.join(dir.path, baseName + ".cpp")}` +
  28. " /tmp/circuit.cpp"
  29. );
  30. await exec("g++" +
  31. ` ${path.join(cdir, "main.cpp")}` +
  32. ` ${path.join(cdir, "calcwit.cpp")}` +
  33. ` ${path.join(cdir, "utils.cpp")}` +
  34. ` ${path.join(cdir, "zqfield.cpp")}` +
  35. ` ${path.join(dir.path, baseName + ".cpp")} ` +
  36. ` -o ${path.join(dir.path, baseName)}` +
  37. ` -I ${cdir}` +
  38. " -lgmp -std=c++11 -DSANITY_CHECK"
  39. );
  40. // console.log(dir.path);
  41. return new CTester(dir, baseName);
  42. }
  43. class CTester {
  44. constructor(dir, baseName) {
  45. this.dir=dir;
  46. this.baseName = baseName;
  47. }
  48. async release() {
  49. await this.dir.cleanup();
  50. }
  51. async calculateWitness(input) {
  52. await fs.promises.writeFile(
  53. path.join(this.dir.path, "in.json"),
  54. JSON.stringify(stringifyBigInts(input), null, 1)
  55. );
  56. await exec(`${path.join(this.dir.path, this.baseName)}` +
  57. ` ${path.join(this.dir.path, "in.json")}` +
  58. ` ${path.join(this.dir.path, "out.json")}`
  59. );
  60. const resStr = await fs.promises.readFile(
  61. path.join(this.dir.path, "out.json")
  62. );
  63. const res = unstringifyBigInts(JSON.parse(resStr));
  64. return res;
  65. }
  66. async loadSymbols() {
  67. if (this.symbols) return;
  68. this.symbols = {};
  69. const symsStr = await fs.promises.readFile(
  70. path.join(this.dir.path, this.baseName + ".sym"),
  71. "utf8"
  72. );
  73. const lines = symsStr.split("\n");
  74. for (let i=0; i<lines.length; i++) {
  75. const arr = lines[i].split(",");
  76. if (arr.length!=3) continue;
  77. this.symbols[arr[2]] = {
  78. idx: Number(arr[0]),
  79. idxWit: Number(arr[1])
  80. };
  81. }
  82. }
  83. async loadConstraints() {
  84. const self = this;
  85. if (this.constraints) return;
  86. const r1cs = await loadR1cs(path.join(this.dir.path, this.baseName + ".r1cs"),true, false);
  87. self.field = new ZqField(r1cs.prime);
  88. self.nWires = r1cs.nWires;
  89. self.constraints = r1cs.constraints;
  90. }
  91. async assertOut(actualOut, expectedOut) {
  92. const self = this;
  93. if (!self.symbols) await self.loadSymbols();
  94. checkObject("main", expectedOut);
  95. function checkObject(prefix, eOut) {
  96. if (Array.isArray(eOut)) {
  97. for (let i=0; i<eOut.length; i++) {
  98. checkObject(prefix + "["+i+"]", eOut[i]);
  99. }
  100. } else if ((typeof eOut == "object")&&(eOut.constructor.name == "Object")) {
  101. for (let k in eOut) {
  102. checkObject(prefix + "."+k, eOut[k]);
  103. }
  104. } else {
  105. if (typeof self.symbols[prefix] == "undefined") {
  106. assert(false, "Output variable not defined: "+ prefix);
  107. }
  108. const ba = bigInt(actualOut[self.symbols[prefix].idxWit]).toString();
  109. const be = bigInt(eOut).toString();
  110. assert.strictEqual(ba, be, prefix);
  111. }
  112. }
  113. }
  114. async getDecoratedOutput(witness) {
  115. const self = this;
  116. const lines = [];
  117. if (!self.symbols) await self.loadSymbols();
  118. for (let n in self.symbols) {
  119. let v;
  120. if (utils.isDefined(witness[self.symbols[n].idxWit])) {
  121. v = witness[self.symbols[n].idxWit].toString();
  122. } else {
  123. v = "undefined";
  124. }
  125. lines.push(`${n} --> ${v}`);
  126. }
  127. return lines.join("\n");
  128. }
  129. async checkConstraints(witness) {
  130. const self = this;
  131. if (!self.constraints) await self.loadConstraints();
  132. for (let i=0; i<self.constraints.length; i++) {
  133. checkConstraint(self.constraints[i]);
  134. }
  135. function checkConstraint(constraint) {
  136. const F = self.field;
  137. const a = evalLC(constraint.a);
  138. const b = evalLC(constraint.b);
  139. const c = evalLC(constraint.c);
  140. assert (F.sub(F.mul(a,b), c).isZero(), "Constraint doesn't match");
  141. }
  142. function evalLC(lc) {
  143. const F = self.field;
  144. let v = F.zero;
  145. for (let w in lc) {
  146. v = F.add(
  147. v,
  148. F.mul( lc[w], witness[w] )
  149. );
  150. }
  151. return v;
  152. }
  153. }
  154. }