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.

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