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.

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