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.

241 lines
7.2 KiB

2 years ago
2 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 util = require("util");
  7. const exec = util.promisify(require("child_process").exec);
  8. const loadR1cs = require("r1csfile").load;
  9. const ZqField = require("ffjavascript").ZqField;
  10. module.exports = c_tester;
  11. async function c_tester(circomInput, _options) {
  12. assert(await compiler_above_version("2.0.0"),"Wrong compiler version. Must be at least 2.0.0");
  13. tmp.setGracefulCleanup();
  14. const dir = await tmp.dir({prefix: "circom_", unsafeCleanup: true });
  15. // console.log(dir.path);
  16. const baseName = path.basename(circomInput, ".circom");
  17. const options = Object.assign({}, _options);
  18. options.wasm = true;
  19. options.sym = true;
  20. options.json = options.json || false; // costraints in json format
  21. //options.r1cs = options.r1cs || false; // costraints in r1cs format
  22. //if (!options.json) options.r1cs = true; // r1cs if not json
  23. options.r1cs = true;
  24. options.output = dir.path;
  25. await compile(baseName, circomInput, options);
  26. return new WasmTester(dir, baseName, run);
  27. }
  28. async function compile (baseName, fileName, options) {
  29. var flags = "--c ";
  30. if (options.sym) flags += "--sym ";
  31. if (options.r1cs) flags += "--r1cs ";
  32. if (options.json) flags += "--json ";
  33. if (options.output) flags += "--output " + options.output + " ";
  34. if (options.O === 0) flags += "--O0 "
  35. if (options.O === 1) flags += "--O1 "
  36. b = await exec("circom " + flags + fileName);
  37. assert(b.stderr == "",
  38. "circom compiler error \n" + b.stderr);
  39. const c_folder = path.join(options.output, baseName+"_cpp/")
  40. b = await exec("make -C "+c_folder);
  41. assert(b.stderr == "",
  42. "error building the executable C program\n" + b.stderr);
  43. }
  44. class WasmTester {
  45. constructor(dir, baseName, witnessCalculator) {
  46. this.dir=dir;
  47. this.baseName = baseName;
  48. this.witnessCalculator = witnessCalculator;
  49. }
  50. async release() {
  51. await this.dir.cleanup();
  52. }
  53. async calculateWitness(input) {
  54. const inputjson = JSON.stringify(input);
  55. const inputFile = path.join(this.dir.path, this.baseName+"_cpp/" + this.baseName + ".json");
  56. const wtnsFile = path.join(this.dir.path, this.baseName+"_cpp/" + this.baseName + ".wtns");
  57. const runc = path.join(this.dir.path, this.baseName+"_cpp/" + this.baseName);
  58. fs.writeFile(inputFile, inputjson, function(err) {
  59. if (err) throw err;
  60. });
  61. await exec("ls " + path.join(this.dir.path, this.baseName+"_cpp/"));
  62. await exec(runc + " " + inputFile + " " + wtnsFile);
  63. return readBinaryFile(wtnsFile);
  64. }
  65. async loadSymbols() {
  66. if (this.symbols) return;
  67. this.symbols = {};
  68. const symsStr = await fs.promises.readFile(
  69. path.join(this.dir.path, this.baseName + ".sym"),
  70. "utf8"
  71. );
  72. const lines = symsStr.split("\n");
  73. for (let i=0; i<lines.length; i++) {
  74. const arr = lines[i].split(",");
  75. if (arr.length!=4) continue;
  76. this.symbols[arr[3]] = {
  77. labelIdx: Number(arr[0]),
  78. varIdx: Number(arr[1]),
  79. componentIdx: Number(arr[2]),
  80. };
  81. }
  82. }
  83. async loadConstraints() {
  84. const self = this;
  85. if (this.constraints) return;
  86. const r1cs = await loadR1cs(path.join(this.dir.path, this.baseName + ".r1cs"),true, false);
  87. self.F = new ZqField(r1cs.prime);
  88. self.nVars = r1cs.nVars;
  89. self.constraints = r1cs.constraints;
  90. }
  91. async assertOut(actualOut, expectedOut) {
  92. const self = this;
  93. if (!self.symbols) await self.loadSymbols();
  94. checkObject("main", expectedOut);
  95. function checkObject(prefix, eOut) {
  96. if (Array.isArray(eOut)) {
  97. for (let i=0; i<eOut.length; i++) {
  98. checkObject(prefix + "["+i+"]", eOut[i]);
  99. }
  100. } else if ((typeof eOut == "object")&&(eOut.constructor.name == "Object")) {
  101. for (let k in eOut) {
  102. checkObject(prefix + "."+k, eOut[k]);
  103. }
  104. } else {
  105. if (typeof self.symbols[prefix] == "undefined") {
  106. assert(false, "Output variable not defined: "+ prefix);
  107. }
  108. const ba = actualOut[self.symbols[prefix].varIdx].toString();
  109. const be = eOut.toString();
  110. assert.strictEqual(ba, be, prefix);
  111. }
  112. }
  113. }
  114. async getDecoratedOutput(witness) {
  115. const self = this;
  116. const lines = [];
  117. if (!self.symbols) await self.loadSymbols();
  118. for (let n in self.symbols) {
  119. let v;
  120. if (utils.isDefined(witness[self.symbols[n].varIdx])) {
  121. v = witness[self.symbols[n].varIdx].toString();
  122. } else {
  123. v = "undefined";
  124. }
  125. lines.push(`${n} --> ${v}`);
  126. }
  127. return lines.join("\n");
  128. }
  129. async checkConstraints(witness) {
  130. const self = this;
  131. if (!self.constraints) await self.loadConstraints();
  132. for (let i=0; i<self.constraints.length; i++) {
  133. checkConstraint(self.constraints[i]);
  134. }
  135. function checkConstraint(constraint) {
  136. const F = self.F;
  137. const a = evalLC(constraint[0]);
  138. const b = evalLC(constraint[1]);
  139. const c = evalLC(constraint[2]);
  140. assert (F.isZero(F.sub(F.mul(a,b), c)), "Constraint doesn't match");
  141. }
  142. function evalLC(lc) {
  143. const F = self.F;
  144. let v = F.zero;
  145. for (let w in lc) {
  146. v = F.add(
  147. v,
  148. F.mul( lc[w], witness[w] )
  149. );
  150. }
  151. return v;
  152. }
  153. }
  154. }
  155. function version_to_list ( v ) {
  156. return v.split(".").map(function(x) {
  157. return parseInt(x, 10);
  158. });
  159. }
  160. function check_versions ( v1, v2 ) {
  161. //check if v1 is newer than or equal to v2
  162. for (let i = 0; i < v2.length; i++) {
  163. if (v1[i] > v2[i]) return true;
  164. if (v1[i] < v2[i]) return false;
  165. }
  166. return true;
  167. }
  168. async function compiler_above_version(v) {
  169. let output = await exec('circom --version').toString();
  170. let compiler_version = version_to_list(output.slice(output.search(/\d/),-1));
  171. vlist = version_to_list(v);
  172. return check_versions ( compiler_version, vlist );
  173. }
  174. function readBinaryFile(fileName) {
  175. const buff = fs.readFileSync(fileName);
  176. const n32 = fromArray8ToUint(buff.slice(24,27));
  177. var pos = 28+n32;
  178. const ws = fromArray8ToUint(buff.slice(pos,pos+3));
  179. pos += 16;
  180. const w = [];
  181. for (let i=0; i<ws; i++) {
  182. w.push(fromArray8(buff.slice(pos,pos+n32-1)));
  183. pos += n32;
  184. }
  185. return w;
  186. }
  187. function fromArray8(arr) { //returns a BigInt
  188. var res = BigInt(0);
  189. const radix = BigInt(0x100);
  190. for (let i = arr.length-1 ; i>=0; i--) {
  191. res = res*radix + BigInt(arr[i]);
  192. }
  193. return res;
  194. }
  195. function fromArray8ToUint(arr) { //returns a BigInt
  196. var res = 0;
  197. const radix = 8;
  198. for (let i = arr.length-1 ; i>=0; i--) {
  199. res = res*radix + arr[i];
  200. }
  201. return res;
  202. }