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.

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