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.

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