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.

216 lines
6.9 KiB

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