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