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.

924 lines
28 KiB

4 years ago
3 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
3 years ago
3 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 buildGetNPublic() {
  622. const f = module.addFunction("getNPublic");
  623. f.setReturnType("i32");
  624. const c = f.getCodeBuilder();
  625. f.addCode(c.i32_const(builder.header.NPublic));
  626. }
  627. function buildGetFrLen() {
  628. const f = module.addFunction("getFrLen");
  629. f.setReturnType("i32");
  630. const c = f.getCodeBuilder();
  631. f.addCode(
  632. c.i32_const(builder.sizeFr));
  633. }
  634. function buildGetPRawPrime() {
  635. const f = module.addFunction("getPRawPrime");
  636. f.setReturnType("i32");
  637. const c = f.getCodeBuilder();
  638. f.addCode(
  639. c.i32_const(module.modules["Fr_F1m"].pq));
  640. }
  641. function buildGetPWitness() {
  642. const f = module.addFunction("getPWitness");
  643. f.addParam("w", "i32");
  644. f.addLocal("signal", "i32");
  645. f.setReturnType("i32");
  646. const c = f.getCodeBuilder();
  647. f.addCode(
  648. c.setLocal(
  649. "signal",
  650. c.i32_load( // wit2sig[w]
  651. c.i32_add(
  652. c.i32_load( c.i32_const(builder.ppWit2sig)),
  653. c.i32_mul(
  654. c.getLocal("w"),
  655. c.i32_const(4)
  656. )
  657. )
  658. )
  659. )
  660. );
  661. if (builder.sanityCheck) {
  662. f.addCode(
  663. c.if(
  664. c.i32_eqz(
  665. c.i32_load(
  666. c.i32_add(
  667. c.i32_const(builder.pSignalsAssigned),
  668. c.i32_mul(
  669. c.getLocal("signal"),
  670. c.i32_const(4)
  671. )
  672. ),
  673. )
  674. ),
  675. c.call(
  676. "error",
  677. c.i32_const(errs.ACCESSING_NOT_ASSIGNED_SIGNAL.code),
  678. c.i32_const(errs.ACCESSING_NOT_ASSIGNED_SIGNAL.pointer),
  679. c.i32_const(0),
  680. c.i32_const(0),
  681. c.i32_const(0),
  682. c.i32_const(0)
  683. )
  684. )
  685. );
  686. }
  687. f.addCode(
  688. c.i32_add(
  689. c.i32_const(builder.pSignals),
  690. c.i32_mul(
  691. c.getLocal("signal"),
  692. c.i32_const(builder.sizeFr)
  693. )
  694. )
  695. );
  696. }
  697. function buildGetWitnessBuffer() {
  698. const f = module.addFunction("getWitnessBuffer");
  699. f.setReturnType("i32");
  700. f.addLocal("i", "i32");
  701. f.addLocal("pSrc", "i32");
  702. f.addLocal("pDst", "i32");
  703. const c = f.getCodeBuilder();
  704. f.addCode(
  705. c.setLocal("i", c.i32_const(0)),
  706. c.block(c.loop(
  707. // if (i==NComponents) break
  708. c.br_if(1, c.i32_eq(c.getLocal("i"), c.i32_const(builder.header.NVars))),
  709. c.setLocal(
  710. "pSrc",
  711. c.call(
  712. "getPWitness",
  713. c.getLocal("i"),
  714. )
  715. ),
  716. c.call(
  717. "Fr_toLongNormal",
  718. c.getLocal("pSrc")
  719. ),
  720. c.setLocal(
  721. "pDst",
  722. c.i32_add(
  723. c.i32_const(builder.pOutput),
  724. c.i32_mul(
  725. c.getLocal("i"),
  726. c.i32_const(builder.sizeFr-8)
  727. )
  728. )
  729. ),
  730. c.call(
  731. "Fr_F1m_copy",
  732. c.i32_add(c.getLocal("pSrc"), c.i32_const(8)),
  733. c.getLocal("pDst")
  734. ),
  735. // i=i+1
  736. c.setLocal(
  737. "i",
  738. c.i32_add(
  739. c.getLocal("i"),
  740. c.i32_const(1)
  741. )
  742. ),
  743. c.br(0)
  744. )),
  745. c.i32_const(builder.pOutput)
  746. );
  747. }
  748. const fError = module.addIimportFunction("error", "runtime");
  749. fError.addParam("code", "i32");
  750. fError.addParam("pStr", "i32");
  751. fError.addParam("param1", "i32");
  752. fError.addParam("param2", "i32");
  753. fError.addParam("param3", "i32");
  754. fError.addParam("param4", "i32");
  755. const fLogSetSignal = module.addIimportFunction("logSetSignal", "runtime");
  756. fLogSetSignal.addParam("signal", "i32");
  757. fLogSetSignal.addParam("val", "i32");
  758. const fLogGetSignal = module.addIimportFunction("logGetSignal", "runtime");
  759. fLogGetSignal.addParam("signal", "i32");
  760. fLogGetSignal.addParam("val", "i32");
  761. const fLogFinishComponent = module.addIimportFunction("logFinishComponent", "runtime");
  762. fLogFinishComponent.addParam("cIdx", "i32");
  763. const fLogStartComponent = module.addIimportFunction("logStartComponent", "runtime");
  764. fLogStartComponent.addParam("cIdx", "i32");
  765. const fLog = module.addIimportFunction("log", "runtime");
  766. fLog.addParam("code", "i32");
  767. buildWasmFf(module, "Fr", builder.header.P);
  768. builder.pSignals=module.alloc(builder.header.NSignals*builder.sizeFr);
  769. builder.pOutput=module.alloc(builder.header.NVars*(builder.sizeFr-8));
  770. builder.pInputSignalsToTrigger=module.alloc(builder.header.NComponents*4);
  771. builder.pSignalsAssigned=module.alloc(builder.header.NSignals*4);
  772. buildHash2ComponentEntry();
  773. buildTriggerComponent();
  774. buildInit();
  775. buildGetFromComponentEntry("getSubComponentOffset", 0 /* offset */, builder.TYPE_COMPONENT);
  776. buildGetFromComponentEntry("getSubComponentSizes", 4 /* offset */, builder.TYPE_COMPONENT);
  777. buildGetFromComponentEntry("getSignalOffset", 0 /* offset */, builder.TYPE_SIGNAL);
  778. buildGetFromComponentEntry("getSignalSizes", 4 /* offset */, builder.TYPE_SIGNAL);
  779. buildGetSignal();
  780. buildSetSignal();
  781. buildMultiGetSignal();
  782. buildComponentStarted();
  783. buildComponentFinished();
  784. buildCheckConstraint();
  785. buildCheckAssert();
  786. buildGetNVars();
  787. buildGetNPublic();
  788. buildGetFrLen();
  789. buildGetPWitness();
  790. buildGetPRawPrime();
  791. buildGetWitnessBuffer();
  792. // buildFrToInt();
  793. module.exportFunction("init");
  794. module.exportFunction("getNVars");
  795. module.exportFunction("getNPublic");
  796. module.exportFunction("getFrLen");
  797. module.exportFunction("getSignalOffset32");
  798. module.exportFunction("setSignal");
  799. module.exportFunction("multiGetSignal");
  800. module.exportFunction("getPWitness");
  801. module.exportFunction("Fr_toInt");
  802. module.exportFunction("getPRawPrime");
  803. module.exportFunction("getWitnessBuffer");
  804. };