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.

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