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.

913 lines
28 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. const errs = require("./errs");
  2. const buildWasmFf = require("ffwasm").buildWasmFf;
  3. module.exports = function buildRuntime(module, builder) {
  4. const pSanityCheck = module.alloc(4);
  5. function buildInit() {
  6. const f = module.addFunction("init");
  7. f.addParam("sanityCheck", "i32");
  8. f.addLocal("i", "i32");
  9. const c = f.getCodeBuilder();
  10. // Set the stack to current memory
  11. f.addCode(
  12. c.i32_store(
  13. c.i32_const(4),
  14. c.i32_shl(
  15. c.i32_and(
  16. c.current_memory(),
  17. c.i32_const(0xFFFFFFF8)
  18. ),
  19. c.i32_const(16)
  20. )
  21. )
  22. );
  23. // Save Sanity check flag
  24. f.addCode(
  25. c.i32_store(
  26. c.i32_const(pSanityCheck),
  27. c.getLocal("sanityCheck")
  28. )
  29. );
  30. f.addCode(
  31. // i=0
  32. c.setLocal("i", c.i32_const(0)),
  33. c.block(c.loop(
  34. // if (i==NComponents) break
  35. c.br_if(1, c.i32_eq(c.getLocal("i"), c.i32_const(builder.header.NComponents))),
  36. // inputSignalsToTrigger[i] = components[i].nInputSignals
  37. c.i32_store(
  38. c.i32_add(
  39. c.i32_const(builder.pInputSignalsToTrigger),
  40. c.i32_mul(
  41. c.getLocal("i"),
  42. c.i32_const(4)
  43. )
  44. ),
  45. c.i32_load(
  46. c.i32_add(
  47. c.i32_load(c.i32_const(builder.ppComponents)),
  48. c.i32_mul(
  49. c.getLocal("i"),
  50. c.i32_const(builder.sizeofComponent) // Sizeof component
  51. )
  52. ),
  53. builder.offsetComponentNInputSignals
  54. )
  55. ),
  56. // i=i+1
  57. c.setLocal(
  58. "i",
  59. c.i32_add(
  60. c.getLocal("i"),
  61. c.i32_const(1)
  62. )
  63. ),
  64. c.br(0)
  65. ))
  66. );
  67. f.addCode(ifSanityCheck(c,
  68. // i=0
  69. c.setLocal("i", c.i32_const(0)),
  70. c.block(c.loop(
  71. // if (i==NSignals) break
  72. c.br_if(1, c.i32_eq(c.getLocal("i"), c.i32_const(builder.header.NSignals))),
  73. // signalsAssigned[i] = false
  74. c.i32_store(
  75. c.i32_add(
  76. c.i32_const(builder.pSignalsAssigned),
  77. c.i32_mul(
  78. c.getLocal("i"),
  79. c.i32_const(4)
  80. )
  81. ),
  82. c.i32_const(0)
  83. ),
  84. // i=i+1
  85. c.setLocal(
  86. "i",
  87. c.i32_add(
  88. c.getLocal("i"),
  89. c.i32_const(1)
  90. )
  91. ),
  92. c.br(0)
  93. ))
  94. ));
  95. f.addCode(
  96. c.call(
  97. "Fr_copy",
  98. c.i32_const(builder.pSignals),
  99. c.i32_add(
  100. c.i32_load(c.i32_const(builder.ppConstants)),
  101. c.i32_const(builder.addConstant(1) * builder.sizeFr)
  102. )
  103. )
  104. );
  105. f.addCode(ifSanityCheck(c,
  106. c.i32_store(
  107. c.i32_const(builder.pSignalsAssigned),
  108. c.i32_const(1)
  109. )
  110. ));
  111. f.addCode(
  112. // i=0
  113. c.setLocal("i", c.i32_const(0)),
  114. c.block(c.loop(
  115. // if (i==NComponents) break
  116. c.br_if(1, c.i32_eq(c.getLocal("i"), c.i32_const(builder.header.NComponents))),
  117. // if (inputSignalsToTrigger[i] == 0) triggerComponent(i)
  118. c.if(
  119. c.i32_eqz(
  120. c.i32_load(
  121. c.i32_add(
  122. c.i32_const(builder.pInputSignalsToTrigger),
  123. c.i32_mul(
  124. c.getLocal("i"),
  125. c.i32_const(4)
  126. )
  127. )
  128. )
  129. ),
  130. c.call(
  131. "triggerComponent",
  132. c.getLocal("i")
  133. )
  134. ),
  135. // i=i+1
  136. c.setLocal(
  137. "i",
  138. c.i32_add(
  139. c.getLocal("i"),
  140. c.i32_const(1)
  141. )
  142. ),
  143. c.br(0)
  144. ))
  145. );
  146. }
  147. function ifSanityCheck(c, ...args) {
  148. return c.if(
  149. c.i32_load(c.i32_const(pSanityCheck)),
  150. [].concat(...[...args])
  151. );
  152. }
  153. function buildTriggerComponent() {
  154. const f = module.addFunction("triggerComponent");
  155. f.addParam("component", "i32");
  156. const c = f.getCodeBuilder();
  157. f.addCode(
  158. c.call_indirect(
  159. c.getLocal("component"), // Idx in table
  160. c.getLocal("component") // Parameter
  161. )
  162. );
  163. }
  164. function buildHash2ComponentEntry() {
  165. const f = module.addFunction("hash2ComponentEntry");
  166. f.addParam("component", "i32");
  167. f.addParam("hash", "i64");
  168. f.setReturnType("i32");
  169. f.addLocal("pComponent", "i32");
  170. f.addLocal("pHashTable", "i32");
  171. f.addLocal("hIdx", "i32");
  172. f.addLocal("h", "i64");
  173. const c = f.getCodeBuilder();
  174. f.addCode(
  175. c.setLocal(
  176. "pComponent",
  177. c.i32_add(
  178. c.i32_load(c.i32_const(builder.ppComponents)), // pComponents
  179. c.i32_mul(
  180. c.getLocal("component"),
  181. c.i32_const(20) // sizeof(Component)
  182. )
  183. )
  184. ),
  185. c.setLocal(
  186. "pHashTable",
  187. c.i32_load(c.getLocal("pComponent"))
  188. ),
  189. c.setLocal(
  190. "hIdx",
  191. c.i32_and(
  192. c.i32_wrap_i64(c.getLocal("hash")),
  193. c.i32_const(0xFF)
  194. )
  195. ),
  196. c.block(c.loop(
  197. c.setLocal(
  198. "h",
  199. c.i64_load(
  200. c.i32_add(
  201. c.getLocal("pHashTable"),
  202. c.i32_mul(
  203. c.getLocal("hIdx"),
  204. c.i32_const(12)
  205. )
  206. )
  207. )
  208. ),
  209. c.br_if(1, c.i64_eq(c.getLocal("h"), c.getLocal("hash"))),
  210. c.if(
  211. c.i64_eqz(c.getLocal("h")),
  212. c.call(
  213. "error",
  214. c.i32_const(errs.HASH_NOT_FOUND.code),
  215. c.i32_const(errs.HASH_NOT_FOUND.pointer),
  216. c.i32_const(0),
  217. c.i32_const(0),
  218. c.i32_const(0),
  219. c.i32_const(0)
  220. )
  221. ),
  222. c.setLocal(
  223. "hIdx",
  224. c.i32_and(
  225. c.i32_add(
  226. c.getLocal("hIdx"),
  227. c.i32_const(1)
  228. ),
  229. c.i32_const(0xFF)
  230. )
  231. ),
  232. c.br(0)
  233. )),
  234. c.i32_add( // pComponentEntry
  235. c.i32_load( // pComponentEntryTable
  236. c.i32_add(
  237. c.getLocal("pComponent"),
  238. c.i32_const(4)
  239. )
  240. ),
  241. c.i32_mul(
  242. c.i32_load( // idx to the componentEntry
  243. c.i32_add(
  244. c.getLocal("pHashTable"),
  245. c.i32_mul(
  246. c.getLocal("hIdx"),
  247. c.i32_const(12)
  248. )
  249. ),
  250. 8
  251. ),
  252. c.i32_const(12)
  253. )
  254. )
  255. );
  256. }
  257. function buildGetFromComponentEntry(fnName, offset, type) {
  258. const f = module.addFunction(fnName);
  259. f.addParam("pR", "i32");
  260. f.addParam("component", "i32");
  261. f.addParam("hash", "i64");
  262. f.addLocal("pComponentEntry", "i32");
  263. const c = f.getCodeBuilder();
  264. f.addCode(
  265. c.setLocal(
  266. "pComponentEntry",
  267. c.call(
  268. "hash2ComponentEntry",
  269. c.getLocal("component"),
  270. c.getLocal("hash")
  271. )
  272. ),
  273. c.if( // If type is not signal
  274. c.i32_ne(
  275. c.i32_load(
  276. c.getLocal("pComponentEntry"),
  277. 8 // type offset
  278. ),
  279. c.i32_const(type)
  280. ),
  281. c.call(
  282. "error",
  283. c.i32_const(errs.INVALID_TYPE.code),
  284. c.i32_const(errs.INVALID_TYPE.pointer),
  285. c.i32_const(0),
  286. c.i32_const(0),
  287. c.i32_const(0),
  288. c.i32_const(0)
  289. )
  290. ),
  291. c.i32_store(
  292. c.getLocal("pR"),
  293. c.i32_load(
  294. c.getLocal("pComponentEntry"),
  295. offset
  296. )
  297. )
  298. );
  299. const f2 = module.addFunction(fnName + "32");
  300. f2.addParam("pR", "i32");
  301. f2.addParam("component", "i32");
  302. f2.addParam("hashMSB", "i32");
  303. f2.addParam("hashLSB", "i32");
  304. const c2 = f2.getCodeBuilder();
  305. f2.addCode(
  306. c2.call(
  307. fnName,
  308. c2.getLocal("pR"),
  309. c2.getLocal("component"),
  310. c2.i64_or(
  311. c2.i64_shl(
  312. c2.i64_extend_i32_u(c2.getLocal("hashMSB")),
  313. c2.i64_const(32)
  314. ),
  315. c2.i64_extend_i32_u(c2.getLocal("hashLSB"))
  316. )
  317. )
  318. );
  319. }
  320. function buildGetSignal() {
  321. const f = module.addFunction("getSignal");
  322. f.addParam("cIdx", "i32");
  323. f.addParam("pR", "i32");
  324. f.addParam("component", "i32");
  325. f.addParam("signal", "i32");
  326. const c = f.getCodeBuilder();
  327. f.addCode(ifSanityCheck(c,
  328. c.if(
  329. c.i32_eqz(
  330. c.i32_load(
  331. c.i32_add(
  332. c.i32_const(builder.pSignalsAssigned),
  333. c.i32_mul(
  334. c.getLocal("signal"),
  335. c.i32_const(4)
  336. )
  337. ),
  338. )
  339. ),
  340. c.call(
  341. "error",
  342. c.i32_const(errs.ACCESSING_NOT_ASSIGNED_SIGNAL.code),
  343. c.i32_const(errs.ACCESSING_NOT_ASSIGNED_SIGNAL.pointer),
  344. c.getLocal("cIdx"),
  345. c.getLocal("component"),
  346. c.getLocal("signal"),
  347. c.i32_const(0)
  348. )
  349. )
  350. ));
  351. f.addCode(
  352. c.call(
  353. "Fr_copy",
  354. c.getLocal("pR"),
  355. c.i32_add(
  356. c.i32_const(builder.pSignals),
  357. c.i32_mul(
  358. c.getLocal("signal"),
  359. c.i32_const(builder.sizeFr)
  360. )
  361. )
  362. )
  363. );
  364. f.addCode(ifSanityCheck(c,
  365. c.call("logGetSignal", c.getLocal("signal"), c.getLocal("pR") )
  366. ));
  367. }
  368. function buildMultiGetSignal() {
  369. const f = module.addFunction("multiGetSignal");
  370. f.addParam("cIdx", "i32");
  371. f.addParam("pR", "i32");
  372. f.addParam("component", "i32");
  373. f.addParam("signal", "i32");
  374. f.addParam("n", "i32");
  375. f.addLocal("i", "i32");
  376. const c = f.getCodeBuilder();
  377. f.addCode(
  378. c.setLocal("i", c.i32_const(0)),
  379. c.block(c.loop(
  380. c.br_if(1, c.i32_eq(c.getLocal("i"), c.getLocal("n"))),
  381. c.call(
  382. "getSignal",
  383. c.getLocal("cIdx"),
  384. c.i32_add(
  385. c.getLocal("pR"),
  386. c.i32_mul(
  387. c.getLocal("i"),
  388. c.i32_const(builder.sizeFr)
  389. )
  390. ),
  391. c.getLocal("component"),
  392. c.i32_add(
  393. c.getLocal("signal"),
  394. c.getLocal("i")
  395. )
  396. ),
  397. c.setLocal("i", c.i32_add(c.getLocal("i"), c.i32_const(1))),
  398. c.br(0)
  399. ))
  400. );
  401. }
  402. function buildSetSignal() {
  403. const f = module.addFunction("setSignal");
  404. f.addParam("cIdx", "i32");
  405. f.addParam("component", "i32");
  406. f.addParam("signal", "i32");
  407. f.addParam("pVal", "i32");
  408. f.addLocal("signalsToTrigger", "i32");
  409. const c = f.getCodeBuilder();
  410. f.addCode(ifSanityCheck(c,
  411. c.call("logSetSignal", c.getLocal("signal"), c.getLocal("pVal") ),
  412. c.if(
  413. c.i32_load(
  414. c.i32_add(
  415. c.i32_const(builder.pSignalsAssigned),
  416. c.i32_mul(
  417. c.getLocal("signal"),
  418. c.i32_const(4)
  419. )
  420. ),
  421. ),
  422. c.call(
  423. "error",
  424. c.i32_const(errs.SIGNAL_ASSIGNED_TWICE.code),
  425. c.i32_const(errs.SIGNAL_ASSIGNED_TWICE.pointer),
  426. c.i32_const(0),
  427. c.i32_const(0),
  428. c.i32_const(0),
  429. c.i32_const(0)
  430. )
  431. ),
  432. c.i32_store(
  433. c.i32_add(
  434. c.i32_const(builder.pSignalsAssigned),
  435. c.i32_mul(
  436. c.getLocal("signal"),
  437. c.i32_const(4)
  438. )
  439. ),
  440. c.i32_const(1)
  441. ),
  442. ));
  443. f.addCode(
  444. c.call(
  445. "Fr_copy",
  446. c.i32_add(
  447. c.i32_const(builder.pSignals),
  448. c.i32_mul(
  449. c.getLocal("signal"),
  450. c.i32_const(builder.sizeFr)
  451. )
  452. ),
  453. c.getLocal("pVal"),
  454. )
  455. );
  456. f.addCode(
  457. c.if( // If ( mapIsInput[s >> 5] & (1 << (s & 0x1f)) )
  458. c.i32_and(
  459. c.i32_load(
  460. c.i32_add(
  461. c.i32_load(c.i32_const(builder.ppMapIsInput)),
  462. c.i32_shl(
  463. c.i32_shr_u(
  464. c.getLocal("signal"),
  465. c.i32_const(5)
  466. ),
  467. c.i32_const(2)
  468. )
  469. )
  470. ),
  471. c.i32_shl(
  472. c.i32_const(1),
  473. c.i32_and(
  474. c.getLocal("signal"),
  475. c.i32_const(0x1F)
  476. )
  477. )
  478. ),
  479. [
  480. ...c.setLocal(
  481. "signalsToTrigger",
  482. c.i32_load(
  483. c.i32_add(
  484. c.i32_const(builder.pInputSignalsToTrigger),
  485. c.i32_mul(
  486. c.getLocal("component"),
  487. c.i32_const(4)
  488. )
  489. )
  490. )
  491. ),
  492. ...c.if( // if (signalsToTrigger > 0)
  493. c.i32_gt_u(
  494. c.getLocal("signalsToTrigger"),
  495. c.i32_const(0)
  496. ),
  497. [
  498. ...c.setLocal( // signalsToTrigger--
  499. "signalsToTrigger",
  500. c.i32_sub(
  501. c.getLocal("signalsToTrigger"),
  502. c.i32_const(1)
  503. )
  504. ),
  505. ...c.i32_store(
  506. c.i32_add(
  507. c.i32_const(builder.pInputSignalsToTrigger),
  508. c.i32_mul(
  509. c.getLocal("component"),
  510. c.i32_const(4)
  511. )
  512. ),
  513. c.getLocal("signalsToTrigger"),
  514. ),
  515. ...c.if( // if (signalsToTrigger==0) triggerCompomnent(component)
  516. c.i32_eqz(c.getLocal("signalsToTrigger")),
  517. c.call(
  518. "triggerComponent",
  519. c.getLocal("component")
  520. )
  521. )
  522. ],
  523. c.call(
  524. "error",
  525. c.i32_const(errs.MAPISINPUT_DONT_MATCH.code),
  526. c.i32_const(errs.MAPISINPUT_DONT_MATCH.pointer),
  527. c.getLocal("component"),
  528. c.getLocal("signal"),
  529. c.i32_const(0),
  530. c.i32_const(0)
  531. )
  532. )
  533. ]
  534. )
  535. );
  536. }
  537. function buildComponentFinished() {
  538. const f = module.addFunction("componentFinished");
  539. f.addParam("cIdx", "i32");
  540. const c = f.getCodeBuilder();
  541. f.addCode(ifSanityCheck(c,
  542. c.call("logFinishComponent", c.getLocal("cIdx"))
  543. ));
  544. f.addCode(c.ret([]));
  545. }
  546. function buildComponentStarted() {
  547. const f = module.addFunction("componentStarted");
  548. f.addParam("cIdx", "i32");
  549. const c = f.getCodeBuilder();
  550. f.addCode(ifSanityCheck(c,
  551. c.call("logStartComponent", c.getLocal("cIdx"))
  552. ));
  553. f.addCode(c.ret([]));
  554. }
  555. function buildCheckConstraint() {
  556. const pTmp = module.alloc(builder.sizeFr);
  557. const f = module.addFunction("checkConstraint");
  558. f.addParam("cIdx", "i32");
  559. f.addParam("pA", "i32");
  560. f.addParam("pB", "i32");
  561. f.addParam("pStr", "i32");
  562. const c = f.getCodeBuilder();
  563. f.addCode(ifSanityCheck(c,
  564. c.call(
  565. "Fr_eq",
  566. c.i32_const(pTmp),
  567. c.getLocal("pA"),
  568. c.getLocal("pB")
  569. ),
  570. c.if (
  571. c.i32_eqz(
  572. c.call(
  573. "Fr_isTrue",
  574. c.i32_const(pTmp),
  575. )
  576. ),
  577. c.call(
  578. "error",
  579. c.i32_const(errs.CONSTRAIN_DOES_NOT_MATCH.code),
  580. c.i32_const(errs.CONSTRAIN_DOES_NOT_MATCH.pointer),
  581. c.getLocal("cIdx"),
  582. c.getLocal("pA"),
  583. c.getLocal("pB"),
  584. c.getLocal("pStr"),
  585. )
  586. )
  587. ));
  588. }
  589. function buildCheckAssert() {
  590. const f = module.addFunction("checkAssert");
  591. f.addParam("cIdx", "i32");
  592. f.addParam("pA", "i32");
  593. f.addParam("pStr", "i32");
  594. const c = f.getCodeBuilder();
  595. f.addCode(ifSanityCheck(c,
  596. c.if (
  597. c.i32_eqz(
  598. c.call(
  599. "Fr_isTrue",
  600. c.getLocal("pA"),
  601. )
  602. ),
  603. c.call(
  604. "error",
  605. c.i32_const(errs.ASSERT_DOES_NOT_MATCH.code),
  606. c.i32_const(errs.ASSERT_DOES_NOT_MATCH.pointer),
  607. c.getLocal("cIdx"),
  608. c.getLocal("pA"),
  609. c.getLocal("pStr"),
  610. c.i32_const(0)
  611. )
  612. )
  613. ));
  614. }
  615. function buildGetNVars() {
  616. const f = module.addFunction("getNVars");
  617. f.setReturnType("i32");
  618. const c = f.getCodeBuilder();
  619. f.addCode(c.i32_const(builder.header.NVars));
  620. }
  621. function buildGetFrLen() {
  622. const f = module.addFunction("getFrLen");
  623. f.setReturnType("i32");
  624. const c = f.getCodeBuilder();
  625. f.addCode(
  626. c.i32_const(builder.sizeFr));
  627. }
  628. function buildGetPRawPrime() {
  629. const f = module.addFunction("getPRawPrime");
  630. f.setReturnType("i32");
  631. const c = f.getCodeBuilder();
  632. f.addCode(
  633. c.i32_const(module.modules["Fr_F1m"].pq));
  634. }
  635. function buildGetPWitness() {
  636. const f = module.addFunction("getPWitness");
  637. f.addParam("w", "i32");
  638. f.addLocal("signal", "i32");
  639. f.setReturnType("i32");
  640. const c = f.getCodeBuilder();
  641. f.addCode(
  642. c.setLocal(
  643. "signal",
  644. c.i32_load( // wit2sig[w]
  645. c.i32_add(
  646. c.i32_load( c.i32_const(builder.ppWit2sig)),
  647. c.i32_mul(
  648. c.getLocal("w"),
  649. c.i32_const(4)
  650. )
  651. )
  652. )
  653. )
  654. );
  655. if (builder.sanityCheck) {
  656. f.addCode(
  657. c.if(
  658. c.i32_eqz(
  659. c.i32_load(
  660. c.i32_add(
  661. c.i32_const(builder.pSignalsAssigned),
  662. c.i32_mul(
  663. c.getLocal("signal"),
  664. c.i32_const(4)
  665. )
  666. ),
  667. )
  668. ),
  669. c.call(
  670. "error",
  671. c.i32_const(errs.ACCESSING_NOT_ASSIGNED_SIGNAL.code),
  672. c.i32_const(errs.ACCESSING_NOT_ASSIGNED_SIGNAL.pointer),
  673. c.i32_const(0),
  674. c.i32_const(0),
  675. c.i32_const(0),
  676. c.i32_const(0)
  677. )
  678. )
  679. );
  680. }
  681. f.addCode(
  682. c.i32_add(
  683. c.i32_const(builder.pSignals),
  684. c.i32_mul(
  685. c.getLocal("signal"),
  686. c.i32_const(builder.sizeFr)
  687. )
  688. )
  689. );
  690. }
  691. function buildGetWitnessBuffer() {
  692. const f = module.addFunction("getWitnessBuffer");
  693. f.setReturnType("i32");
  694. f.addLocal("i", "i32");
  695. f.addLocal("pSrc", "i32");
  696. f.addLocal("pDst", "i32");
  697. const c = f.getCodeBuilder();
  698. f.addCode(
  699. c.setLocal("i", c.i32_const(0)),
  700. c.block(c.loop(
  701. // if (i==NComponents) break
  702. c.br_if(1, c.i32_eq(c.getLocal("i"), c.i32_const(builder.header.NVars))),
  703. c.setLocal(
  704. "pSrc",
  705. c.call(
  706. "getPWitness",
  707. c.getLocal("i"),
  708. )
  709. ),
  710. c.call(
  711. "Fr_toLongNormal",
  712. c.getLocal("pSrc")
  713. ),
  714. c.setLocal(
  715. "pDst",
  716. c.i32_add(
  717. c.i32_const(builder.pOutput),
  718. c.i32_mul(
  719. c.getLocal("i"),
  720. c.i32_const(builder.sizeFr-8)
  721. )
  722. )
  723. ),
  724. c.call(
  725. "Fr_F1m_copy",
  726. c.i32_add(c.getLocal("pSrc"), c.i32_const(8)),
  727. c.getLocal("pDst")
  728. ),
  729. // i=i+1
  730. c.setLocal(
  731. "i",
  732. c.i32_add(
  733. c.getLocal("i"),
  734. c.i32_const(1)
  735. )
  736. ),
  737. c.br(0)
  738. )),
  739. c.i32_const(builder.pOutput)
  740. );
  741. }
  742. const fError = module.addIimportFunction("error", "runtime");
  743. fError.addParam("code", "i32");
  744. fError.addParam("pStr", "i32");
  745. fError.addParam("param1", "i32");
  746. fError.addParam("param2", "i32");
  747. fError.addParam("param3", "i32");
  748. fError.addParam("param4", "i32");
  749. const fLogSetSignal = module.addIimportFunction("logSetSignal", "runtime");
  750. fLogSetSignal.addParam("signal", "i32");
  751. fLogSetSignal.addParam("val", "i32");
  752. const fLogGetSignal = module.addIimportFunction("logGetSignal", "runtime");
  753. fLogGetSignal.addParam("signal", "i32");
  754. fLogGetSignal.addParam("val", "i32");
  755. const fLogFinishComponent = module.addIimportFunction("logFinishComponent", "runtime");
  756. fLogFinishComponent.addParam("cIdx", "i32");
  757. const fLogStartComponent = module.addIimportFunction("logStartComponent", "runtime");
  758. fLogStartComponent.addParam("cIdx", "i32");
  759. const fLog = module.addIimportFunction("log", "runtime");
  760. fLog.addParam("code", "i32");
  761. buildWasmFf(module, "Fr", builder.header.P);
  762. builder.pSignals=module.alloc(builder.header.NSignals*builder.sizeFr);
  763. builder.pOutput=module.alloc(builder.header.NVars*(builder.sizeFr-8));
  764. builder.pInputSignalsToTrigger=module.alloc(builder.header.NComponents*4);
  765. builder.pSignalsAssigned=module.alloc(builder.header.NSignals*4);
  766. buildHash2ComponentEntry();
  767. buildTriggerComponent();
  768. buildInit();
  769. buildGetFromComponentEntry("getSubComponentOffset", 0 /* offset */, builder.TYPE_COMPONENT);
  770. buildGetFromComponentEntry("getSubComponentSizes", 4 /* offset */, builder.TYPE_COMPONENT);
  771. buildGetFromComponentEntry("getSignalOffset", 0 /* offset */, builder.TYPE_SIGNAL);
  772. buildGetFromComponentEntry("getSignalSizes", 4 /* offset */, builder.TYPE_SIGNAL);
  773. buildGetSignal();
  774. buildSetSignal();
  775. buildMultiGetSignal();
  776. buildComponentStarted();
  777. buildComponentFinished();
  778. buildCheckConstraint();
  779. buildCheckAssert();
  780. buildGetNVars();
  781. buildGetFrLen();
  782. buildGetPWitness();
  783. buildGetPRawPrime();
  784. buildGetWitnessBuffer();
  785. // buildFrToInt();
  786. module.exportFunction("init");
  787. module.exportFunction("getNVars");
  788. module.exportFunction("getFrLen");
  789. module.exportFunction("getSignalOffset32");
  790. module.exportFunction("setSignal");
  791. module.exportFunction("multiGetSignal");
  792. module.exportFunction("getPWitness");
  793. module.exportFunction("Fr_toInt");
  794. module.exportFunction("getPRawPrime");
  795. module.exportFunction("getWitnessBuffer");
  796. };