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.

723 lines
22 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. const utils = require("../../src/utils");
  2. const assert = require("assert");
  3. const Scalar = require("ffjavascript").Scalar;
  4. const F1Field = require("ffjavascript").F1Field;
  5. const BigArray = require("../../src/bigarray");
  6. function ref2src(c) {
  7. if ((c[0] == "R")||(c[0] == "RI")) {
  8. return c[1];
  9. } else if (c[0] == "V") {
  10. return c[1].toString();
  11. } else if (c[0] == "C") {
  12. return `(ctx->circuit->constants + ${c[1]})`;
  13. } else if (c[0] == "CC") {
  14. return "__cIdx";
  15. } else {
  16. assert(false);
  17. }
  18. }
  19. class CodeBuilderC {
  20. constructor() {
  21. this.ops = [];
  22. }
  23. addComment(comment) {
  24. this.ops.push({op: "COMMENT", comment});
  25. }
  26. addBlock(block) {
  27. this.ops.push({op: "BLOCK", block});
  28. }
  29. calcOffset(dLabel, offsets) {
  30. this.ops.push({op: "CALCOFFSETS", dLabel, offsets});
  31. }
  32. assign(dLabel, src, sOffset) {
  33. this.ops.push({op: "ASSIGN", dLabel, src, sOffset});
  34. }
  35. getSubComponentOffset(dLabel, component, hash, hashLabel) {
  36. this.ops.push({op: "GETSUBCOMPONENTOFFSET", dLabel, component, hash, hashLabel});
  37. }
  38. getSubComponentSizes(dLabel, component, hash, hashLabel) {
  39. this.ops.push({op: "GETSUBCOMPONENTSIZES", dLabel, component, hash, hashLabel});
  40. }
  41. getSignalOffset(dLabel, component, hash, hashLabel) {
  42. this.ops.push({op: "GETSIGNALOFFSET", dLabel, component, hash, hashLabel});
  43. }
  44. getSignalSizes(dLabel, component, hash, hashLabel) {
  45. this.ops.push({op: "GETSIGNALSIZES", dLabel, component, hash, hashLabel});
  46. }
  47. setSignal(component, signal, value) {
  48. this.ops.push({op: "SETSIGNAL", component, signal, value});
  49. }
  50. getSignal(dLabel, component, signal) {
  51. this.ops.push({op: "GETSIGNAL", dLabel, component, signal});
  52. }
  53. copyN(dLabel, offset, src, n) {
  54. this.ops.push({op: "COPYN", dLabel, offset, src, n});
  55. }
  56. copyNRet(src, n) {
  57. this.ops.push({op: "COPYNRET", src, n});
  58. }
  59. fieldOp(dLabel, fOp, params) {
  60. this.ops.push({op: "FOP", dLabel, fOp, params});
  61. }
  62. ret() {
  63. this.ops.push({op: "RET"});
  64. }
  65. addLoop(condLabel, body) {
  66. this.ops.push({op: "LOOP", condLabel, body});
  67. }
  68. addIf(condLabel, thenCode, elseCode) {
  69. this.ops.push({op: "IF", condLabel, thenCode, elseCode});
  70. }
  71. fnCall(fnName, retLabel, params) {
  72. this.ops.push({op: "FNCALL", fnName, retLabel, params});
  73. }
  74. checkConstraint(a, b, strErr) {
  75. this.ops.push({op: "CHECKCONSTRAINT", a, b, strErr});
  76. }
  77. checkAssert(a, strErr) {
  78. this.ops.push({op: "CHECKASSERT", a, strErr});
  79. }
  80. log(val) {
  81. this.ops.push({op: "LOG", val});
  82. }
  83. concat(cb) {
  84. this.ops.push(...cb.ops);
  85. }
  86. hasCode() {
  87. for (let i=0; i<this.ops.length; i++) {
  88. if (this.ops[i].op != "COMMENT") return true;
  89. }
  90. return false;
  91. }
  92. _buildOffset(offsets) {
  93. let rN=0;
  94. let S = "";
  95. offsets.forEach((o) => {
  96. if ((o[0][0] == "V") && (o[1][0]== "V")) {
  97. rN += o[0][1]*o[1][1];
  98. return;
  99. }
  100. let f="";
  101. if (o[0][0] == "V") {
  102. if (o[0][1]==0) return;
  103. f += o[0][1];
  104. } else if (o[0][0] == "RI") {
  105. if (o[0][1]==0) return;
  106. f += o[0][1];
  107. } else if (o[0][0] == "R") {
  108. f += `Fr_toInt(${o[0][1]})`;
  109. } else {
  110. assert(false);
  111. }
  112. if (o[1][0] == "V") {
  113. if (o[1][1]==0) return;
  114. if (o[1][1]>1) {
  115. f += "*" + o[1][1];
  116. }
  117. } else if (o[1][0] == "RS") {
  118. f += `*${o[1][1]}[${o[1][2]}]`;
  119. } else {
  120. assert(false);
  121. }
  122. if (S!="") S+= " + ";
  123. S += f;
  124. });
  125. if (rN>0) {
  126. S = `${rN} + ${S}`;
  127. }
  128. return S;
  129. }
  130. build(code) {
  131. this.ops.forEach( (o) => {
  132. if (o.op == "COMMENT") {
  133. code.push(`/* ${o.comment} */`);
  134. } else if (o.op == "BLOCK") {
  135. const codeBlock=[];
  136. o.block.build(codeBlock);
  137. code.push(utils.ident(codeBlock));
  138. } else if (o.op == "CALCOFFSETS") {
  139. code.push(`${o.dLabel} = ${this._buildOffset(o.offsets)};`);
  140. } else if (o.op == "ASSIGN") {
  141. const oS = ref2src(o.sOffset);
  142. if (oS != "0") {
  143. code.push(`${o.dLabel} = ${ref2src(o.src)} + ${oS};`);
  144. } else {
  145. code.push(`${o.dLabel} = ${ref2src(o.src)};`);
  146. }
  147. } else if (o.op == "GETSUBCOMPONENTOFFSET") {
  148. code.push(`${o.dLabel} = ctx->getSubComponentOffset(${ref2src(o.component)}, 0x${o.hash}LL /* ${o.hashLabel} */);`);
  149. } else if (o.op == "GETSUBCOMPONENTSIZES") {
  150. code.push(`${o.dLabel} = ctx->getSubComponentSizes(${ref2src(o.component)}, 0x${o.hash}LL /* ${o.hashLabel} */);`);
  151. } else if (o.op == "GETSIGNALOFFSET") {
  152. code.push(`${o.dLabel} = ctx->getSignalOffset(${ref2src(o.component)}, 0x${o.hash}LL /* ${o.hashLabel} */);`);
  153. } else if (o.op == "GETSIGNALSIZES") {
  154. code.push(`${o.dLabel} = ctx->getSignalSizes(${ref2src(o.component)}, 0x${o.hash}LL /* ${o.hashLabel} */);`);
  155. } else if (o.op == "SETSIGNAL") {
  156. code.push(`ctx->setSignal(__cIdx, ${ref2src(o.component)}, ${ref2src(o.signal)}, ${ref2src(o.value)});`);
  157. } else if (o.op == "GETSIGNAL") {
  158. code.push(`ctx->getSignal(__cIdx, ${ref2src(o.component)}, ${ref2src(o.signal)}, ${o.dLabel});`);
  159. } else if (o.op == "COPYN") {
  160. const oS = ref2src(o.offset);
  161. const dLabel = (oS != "0") ? (o.dLabel + "+" + oS) : o.dLabel;
  162. code.push(`Fr_copyn(${dLabel}, ${ref2src(o.src)}, ${o.n});`);
  163. } else if (o.op == "COPYNRET") {
  164. code.push(`Fr_copyn(__retValue, ${ref2src(o.src)}, ${o.n});`);
  165. } else if (o.op == "RET") {
  166. code.push("goto returnFunc;");
  167. } else if (o.op == "FOP") {
  168. let paramsS = "";
  169. for (let i=0; i<o.params.length; i++) {
  170. if (i>0) paramsS += ", ";
  171. paramsS += ref2src(o.params[i]);
  172. }
  173. code.push(`Fr_${o.fOp}(${o.dLabel}, ${paramsS});`);
  174. } else if (o.op == "LOOP") {
  175. code.push(`while (Fr_isTrue(${o.condLabel})) {`);
  176. const body = [];
  177. o.body.build(body);
  178. code.push(utils.ident(body));
  179. code.push("}");
  180. } else if (o.op == "IF") {
  181. code.push(`if (Fr_isTrue(${o.condLabel})) {`);
  182. const thenCode = [];
  183. o.thenCode.build(thenCode);
  184. code.push(utils.ident(thenCode));
  185. if (o.elseCode) {
  186. code.push("} else {");
  187. const elseCode = [];
  188. o.elseCode.build(elseCode);
  189. code.push(utils.ident(elseCode));
  190. }
  191. code.push("}");
  192. } else if (o.op == "FNCALL") {
  193. code.push(`${o.fnName}(ctx, ${o.retLabel}, ${o.params.join(",")});`);
  194. } else if (o.op == "CHECKCONSTRAINT") {
  195. code.push(`ctx->checkConstraint(__cIdx, ${ref2src(o.a)}, ${ref2src(o.b)}, "${o.strErr}");`);
  196. } else if (o.op == "CHECKASSERT") {
  197. code.push(`ctx->checkAssert(__cIdx, ${ref2src(o.a)}, "${o.strErr}");`);
  198. } else if (o.op == "LOG") {
  199. code.push(`ctx->log(${ref2src(o.val)});`);
  200. }
  201. });
  202. }
  203. }
  204. class FunctionBuilderC {
  205. constructor(name, instanceDef, type) {
  206. this.name = name;
  207. this.instanceDef = instanceDef;
  208. this.type = type; // "COMPONENT" or "FUNCTIOM"
  209. this.definedFrElements = [];
  210. this.definedIntElements = [];
  211. this.definedSizeElements = [];
  212. this.definedPFrElements = [];
  213. this.initializedElements = [];
  214. this.initializedSignalOffset = [];
  215. this.initializedSignalSizes = [];
  216. }
  217. defineFrElements(dLabel, size) {
  218. this.definedFrElements.push({dLabel, size});
  219. }
  220. defineIntElement(dLabel) {
  221. this.definedIntElements.push({dLabel});
  222. }
  223. defineSizesElement(dLabel) {
  224. this.definedSizeElements.push({dLabel});
  225. }
  226. definePFrElement(dLabel) {
  227. this.definedPFrElements.push({dLabel});
  228. }
  229. initializeFrElement(dLabel, offset, idConstant) {
  230. this.initializedElements.push({dLabel, offset, idConstant});
  231. }
  232. initializeSignalOffset(dLabel, component, hash, hashLabel) {
  233. this.initializedSignalOffset.push({dLabel, component, hash, hashLabel});
  234. }
  235. initializeSignalSizes(dLabel, component, hash, hashLabel) {
  236. this.initializedSignalSizes.push({dLabel, component, hash, hashLabel});
  237. }
  238. setParams(params) {
  239. this.params = params;
  240. }
  241. _buildHeader(code) {
  242. this.definedFrElements.forEach( (o) => {
  243. code.push(`FrElement ${o.dLabel}[${o.size}];`);
  244. });
  245. this.definedIntElements.forEach( (o) => {
  246. code.push(`int ${o.dLabel};`);
  247. });
  248. this.definedSizeElements.forEach( (o) => {
  249. code.push(`Circom_Sizes ${o.dLabel};`);
  250. });
  251. this.definedPFrElements.forEach( (o) => {
  252. code.push(`PFrElement ${o.dLabel};`);
  253. });
  254. this.initializedElements.forEach( (o) => {
  255. code.push(`Fr_copy(&(${o.dLabel}[${o.offset}]), ctx->circuit->constants +${o.idConstant});`);
  256. });
  257. this.initializedSignalOffset.forEach( (o) => {
  258. code.push(`${o.dLabel} = ctx->getSignalOffset(${ref2src(o.component)}, 0x${o.hash}LL /* ${o.hashLabel} */);`);
  259. });
  260. this.initializedSignalSizes.forEach( (o) => {
  261. code.push(`${o.dLabel} = ctx->getSignalSizes(${ref2src(o.component)}, 0x${o.hash}LL /* ${o.hashLabel} */);`);
  262. });
  263. }
  264. _buildFooter(code) {
  265. }
  266. newCodeBuilder() {
  267. return new CodeBuilderC();
  268. }
  269. setBody(body) {
  270. this.body = body;
  271. }
  272. build(code) {
  273. code.push(
  274. "/*",
  275. this.instanceDef,
  276. "*/"
  277. );
  278. if (this.type=="COMPONENT") {
  279. code.push(`void ${this.name}(Circom_CalcWit *ctx, int __cIdx) {`);
  280. } else if (this.type=="FUNCTION") {
  281. let sParams = "";
  282. for (let i=0;i<this.params.length;i++ ) sParams += `, PFrElement ${this.params[i]}`;
  283. code.push(`void ${this.name}(Circom_CalcWit *ctx, PFrElement __retValue ${sParams}) {`);
  284. } else {
  285. assert(false);
  286. }
  287. const fnCode = [];
  288. this._buildHeader(fnCode);
  289. this.body.build(fnCode);
  290. if (this.type=="COMPONENT") {
  291. fnCode.push("ctx->finished(__cIdx);");
  292. } else if (this.type=="FUNCTION") {
  293. fnCode.push("returnFunc: ;");
  294. } else {
  295. assert(false);
  296. }
  297. this._buildFooter(fnCode);
  298. code.push(utils.ident(fnCode));
  299. code.push("}");
  300. }
  301. }
  302. class BuilderC {
  303. constructor(p, verbose) {
  304. this.F = new F1Field(p);
  305. this.hashMaps={};
  306. this.componentEntriesTables=new BigArray();
  307. this.sizes ={};
  308. this.constants = [];
  309. this.functions = [];
  310. this.components = new BigArray();
  311. this.usedConstants = {};
  312. this.verbose = verbose;
  313. this.sizePointers = {};
  314. this.hashMapPointers = {};
  315. this.functionIdx = {};
  316. this.nCets = 0;
  317. }
  318. setHeader(header) {
  319. this.header=header;
  320. }
  321. // ht is an array of 256 element that can be undefined or [Hash, Idx, KeyName] elements.
  322. addHashMap(name, hm) {
  323. this.hashMaps[name] = hm;
  324. }
  325. addComponentEntriesTable(name, cet, idComponent) {
  326. this.componentEntriesTables[idComponent] = {
  327. name: name,
  328. cet: cet
  329. };
  330. }
  331. addSizes(name, accSizes) {
  332. this.sizes[name] = accSizes;
  333. }
  334. addConstant(c) {
  335. c = this.F.e(c);
  336. const cS = c.toString();
  337. if (typeof this.usedConstants[cS] != "undefined") return this.usedConstants[cS];
  338. this.constants.push(c);
  339. this.usedConstants[cS] = this.constants.length - 1;
  340. return this.constants.length - 1;
  341. }
  342. addFunction(fnBuilder) {
  343. this.functions.push(fnBuilder);
  344. }
  345. addComponent(component) {
  346. this.components.push(component);
  347. }
  348. setMapIsInput(map) {
  349. this.mapIsInput = map;
  350. }
  351. setWit2Sig(wit2sig) {
  352. this.wit2sig = wit2sig;
  353. }
  354. newComponentFunctionBuilder(name, instanceDef) {
  355. return new FunctionBuilderC(name, instanceDef, "COMPONENT");
  356. }
  357. newFunctionBuilder(name, instanceDef) {
  358. return new FunctionBuilderC(name, instanceDef, "FUNCTION");
  359. }
  360. // Body functions
  361. _buildHeader(code) {
  362. code.push(
  363. "#include \"circom.hpp\"",
  364. "#include \"calcwit.hpp\"",
  365. `#define NSignals ${this.header.NSignals}`,
  366. `#define NComponents ${this.header.NComponents}`,
  367. `#define NOutputs ${this.header.NOutputs}`,
  368. `#define NInputs ${this.header.NInputs}`,
  369. `#define NVars ${this.header.NVars}`,
  370. `#define __P__ "${this.header.P.toString()}"`,
  371. ""
  372. );
  373. }
  374. async _buildHashMaps(fdData) {
  375. while (fdData.pos % 8) fdData.pos++;
  376. this.pHashMaps = fdData.pos;
  377. const buff = new Uint8Array(256*12);
  378. const buffV = new DataView(buff.buffer);
  379. for (let hmName in this.hashMaps ) {
  380. while (fdData.pos % 8) fdData.pos++;
  381. this.hashMapPointers[hmName] = fdData.pos;
  382. const hm = this.hashMaps[hmName];
  383. for (let i=0; i<256; i++) {
  384. buffV.setUint32(i*12, hm[i] ? parseInt( hm[i][0].slice(8), 16 ) : 0, true);
  385. buffV.setUint32(i*12+4, hm[i] ? parseInt( hm[i][0].slice(0,8), 16 ) : 0, true);
  386. buffV.setUint32(i*12+8, hm[i] ? hm[i][1] : 0, true);
  387. }
  388. await fdData.write(buff);
  389. }
  390. }
  391. async _buildComponentEntriesTables(fdData) {
  392. while (fdData.pos % 8) fdData.pos++;
  393. this.pCets = fdData.pos;
  394. for (let i=0; i< this.componentEntriesTables.length; i++) {
  395. if ((this.verbose)&&(i%100000 ==0)) console.log(`_buildComponentEntriesTables ${i}/${this.componentEntriesTables.length}`);
  396. const cet = this.componentEntriesTables[i].cet;
  397. this.components[i].entryTablePointer = fdData.pos;
  398. const buff = new Uint8Array(16*cet.length);
  399. const buffV = new DataView(buff.buffer);
  400. for (let j=0; j<cet.length; j++) {
  401. utils.setUint64(buffV, 16*j+0, this.sizePointers[ cet[j].sizeName]);
  402. buffV.setUint32(16*j+8, cet[j].offset, true);
  403. buffV.setUint32(16*j+12, cet[j].type == "S" ? 0 : 1, true); // Size type 0-> Signal, 1->Component
  404. this.nCets ++;
  405. }
  406. await fdData.write(buff);
  407. }
  408. }
  409. async _buildSizes(fdData) {
  410. for (let sName in this.sizes) {
  411. const accSizes = this.sizes[sName];
  412. while (fdData.pos % 8) fdData.pos++;
  413. this.sizePointers[sName] = fdData.pos;
  414. const buff = new Uint8Array(4*accSizes.length);
  415. const buffV = new DataView(buff.buffer);
  416. for (let i=0; i<accSizes.length; i++) {
  417. buffV.setUint32(i*4, accSizes[i], true);
  418. }
  419. await fdData.write(buff);
  420. }
  421. }
  422. async _buildConstants(fdData) {
  423. const self = this;
  424. const frSize = (8 + self.F.n64*8);
  425. const buff = new Uint8Array(self.constants.length* frSize);
  426. const buffV = new DataView(buff.buffer);
  427. while (fdData.pos % 8) fdData.pos++;
  428. this.pConstants = fdData.pos;
  429. let o = 0;
  430. for (let i=0; i<self.constants.length; i++) {
  431. Fr2Bytes(buffV, o, self.constants[i]);
  432. o += frSize;
  433. }
  434. await fdData.write(buff);
  435. function Fr2Bytes(buffV, offset, n) {
  436. const minShort = self.F.neg(self.F.e("80000000"));
  437. const maxShort = self.F.e("7FFFFFFF", 16);
  438. if ( (self.F.geq(n, minShort))
  439. &&(self.F.leq(n, maxShort)))
  440. {
  441. if (self.F.geq(n, self.F.zero)) {
  442. return shortMontgomeryPositive(n);
  443. } else {
  444. return shortMontgomeryNegative(n);
  445. }
  446. }
  447. return longMontgomery(n);
  448. function shortMontgomeryPositive(a) {
  449. buffV.setUint32(offset, Scalar.toNumber(a) , true );
  450. buffV.setUint32(offset + 4, 0x40000000 , true );
  451. long(buffV, offset + 8, toMontgomery(a));
  452. }
  453. function shortMontgomeryNegative(a) {
  454. const b = -Scalar.toNumber(self.F.neg(a));
  455. buffV.setUint32(offset, b , true );
  456. buffV.setUint32(offset + 4, 0x40000000 , true );
  457. long(buffV, offset + 8, toMontgomery(a));
  458. }
  459. function longMontgomery(a) {
  460. buffV.setUint32(offset, 0 , true );
  461. buffV.setUint32(offset + 4, 0xC0000000 , true );
  462. long(buffV, offset + 8, toMontgomery(a));
  463. }
  464. function long(buffV, offset, a) {
  465. let p = offset;
  466. const arr = Scalar.toArray(a, 0x100000000);
  467. for (let i=0; i<self.F.n64*2; i++) {
  468. const idx = arr.length-1-i;
  469. if ( idx >=0) {
  470. buffV.setUint32(p, arr[idx], true);
  471. } else {
  472. buffV.setUint32(p, 0, true);
  473. }
  474. p+= 4;
  475. }
  476. }
  477. function toMontgomery(a) {
  478. return self.F.mul(a, self.F.R);
  479. }
  480. }
  481. }
  482. _buildFunctions(code) {
  483. const listedFunctions = [];
  484. for (let i=0; i<this.functions.length; i++) {
  485. const cfb = this.functions[i];
  486. cfb.build(code);
  487. if (this.functions[i].type == "COMPONENT") {
  488. this.functionIdx[this.functions[i].name] = listedFunctions.length;
  489. listedFunctions.push(i);
  490. }
  491. }
  492. code.push("// Function Table");
  493. code.push(`Circom_ComponentFunction _functionTable[${listedFunctions.length}] = {`);
  494. for (let i=0; i<listedFunctions.length; i++) {
  495. const sep = i>0 ? " ," : " ";
  496. code.push(`${sep}${this.functions[listedFunctions[i]].name}`);
  497. }
  498. code.push("};");
  499. }
  500. async _buildComponents(fdData) {
  501. const buff = new Uint8Array(32);
  502. const buffV = new DataView(buff.buffer);
  503. while (fdData.pos % 8) fdData.pos++;
  504. this.pComponents = fdData.pos;
  505. for (let i=0; i<this.components.length; i++) {
  506. if ((this.verbose)&&(i%1000000 ==0)) console.log(`_buildComponents ${i}/${this.components.length}`);
  507. const c = this.components[i];
  508. utils.setUint64(buffV, 0, this.hashMapPointers[c.hashMapName], true);
  509. utils.setUint64(buffV, 8, c.entryTablePointer, true);
  510. utils.setUint64(buffV, 16, this.functionIdx[c.functionName], true);
  511. buffV.setUint32(24, c.nInSignals, true);
  512. buffV.setUint32(28, c.newThread ? 1 : 0, true);
  513. await fdData.write(buff);
  514. }
  515. }
  516. async _buildMapIsInput(fdData) {
  517. const buff = new Uint8Array(this.mapIsInput.length * 4);
  518. const buffV = new DataView(buff.buffer);
  519. while (fdData.pos % 8) fdData.pos++;
  520. this.pMapIsInput = fdData.pos;
  521. for (let i=0; i<this.mapIsInput.length; i++) {
  522. if ((this.verbose)&&(i%1000000 ==0)) console.log(`_buildMapIsInput ${i}/${this.mapIsInput.length}`);
  523. buffV.setUint32(4*i, this.mapIsInput[i], true);
  524. }
  525. await fdData.write(buff);
  526. }
  527. async _buildWit2Sig(fdData) {
  528. const buff = new Uint8Array(this.wit2sig.length * 4);
  529. const buffV = new DataView(buff.buffer);
  530. while (fdData.pos % 8) fdData.pos++;
  531. this.pWit2Sig = fdData.pos;
  532. for (let i=0; i<this.wit2sig.length; i++) {
  533. if ((this.verbose)&&(i%1000000 ==0)) console.log(`_buildWit2Sig ${i}/${this.wit2sig.length}`);
  534. buffV.setUint32(4*i, this.wit2sig[i], true);
  535. }
  536. await fdData.write(buff);
  537. }
  538. async _buildCircuitVar(fdData) {
  539. const pP = fdData.pos;
  540. const strBuff = new TextEncoder("utf-8").encode(this.header.P.toString());
  541. await fdData.write(strBuff);
  542. const buff = new Uint8Array(72);
  543. const buffV = new DataView(buff.buffer);
  544. utils.setUint64(buffV, 0, this.pWit2Sig, true);
  545. utils.setUint64(buffV, 8, this.pComponents, true);
  546. utils.setUint64(buffV, 16, this.pMapIsInput, true);
  547. utils.setUint64(buffV, 24, this.pConstants, true);
  548. utils.setUint64(buffV, 32, pP, true);
  549. utils.setUint64(buffV, 40, this.pCets, true);
  550. buffV.setUint32(48, this.header.NSignals, true);
  551. buffV.setUint32(52, this.header.NComponents, true);
  552. buffV.setUint32(56, this.header.NOutputs, true);
  553. buffV.setUint32(60, this.header.NInputs, true);
  554. buffV.setUint32(64, this.header.NVars, true);
  555. buffV.setUint32(68, this.nCets, true);
  556. fdData.pos = 0;
  557. await fdData.write(buff);
  558. }
  559. async build(fdCode, fdData) {
  560. const encoder = new TextEncoder("utf-8");
  561. fdData.pos = 72;
  562. while (fdData.pos % 8) fdData.pos++;
  563. const code=new BigArray();
  564. this._buildHeader(code);
  565. await this._buildSizes(fdData);
  566. await this._buildConstants(fdData);
  567. await this._buildHashMaps(fdData);
  568. await this._buildComponentEntriesTables(fdData);
  569. this._buildFunctions(code);
  570. await this._buildComponents(fdData);
  571. await this._buildMapIsInput(fdData);
  572. await this._buildWit2Sig(fdData);
  573. await this._buildCircuitVar(fdData);
  574. await writeCode(code);
  575. async function writeCode(c) {
  576. if (c.push) {
  577. for (let i=0; i<c.length; i++) {
  578. await writeCode(c[i]);
  579. }
  580. } else if (typeof c === "string") {
  581. await fdCode.write(encoder.encode(c + "\n"));
  582. }
  583. }
  584. }
  585. }
  586. module.exports = BuilderC;