miden-protocol 0.14.5

Core components of the Miden protocol
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
use $kernel::constants::ACCOUNT_PROCEDURE_DATA_LENGTH
use $kernel::constants::MAX_ASSETS_PER_NOTE
use $kernel::constants::NOTE_MEM_SIZE
# use $kernel::types::AccountId
use miden::core::mem

pub type AccountId = struct { prefix: felt, suffix: felt }

# ERRORS
# =================================================================================================

const ERR_NOTE_NUM_OF_ASSETS_EXCEED_LIMIT="number of assets in a note exceed 255"

const ERR_ACCOUNT_IS_NOT_NATIVE="the active account is not native"

const ERR_ACCOUNT_STACK_OVERFLOW="depth of the nested FPI calls exceeded 64"

const ERR_ACCOUNT_STACK_UNDERFLOW="failed to end foreign context because the active account is the native account"

const ERR_FOREIGN_ACCOUNT_CONTEXT_AGAINST_NATIVE_ACCOUNT="creation of a foreign context against the native account is forbidden"

const ERR_LINK_MAP_MAX_ENTRIES_EXCEEDED="number of link map entries exceeds maximum"

# MEMORY ADDRESS CONSTANTS
# =================================================================================================

# BOOK KEEPING
# -------------------------------------------------------------------------------------------------

# The memory address at which a pointer to the currently active input note is stored.
const ACTIVE_INPUT_NOTE_PTR = 0

# The memory address at which the number of output notes is stored.
const NUM_OUTPUT_NOTES_PTR = 1

# The memory address at which the absolute expiration block number is stored.
const TX_EXPIRATION_BLOCK_NUM_PTR = 2

# The memory address at which the dirty flag of the storage commitment of the native account is
# stored.
#
# This binary flag specifies whether the commitment is outdated: it holds 1 if some changes were
# made to the account storage since the last re-computation, and 0 otherwise.
const NATIVE_ACCT_STORAGE_COMMITMENT_DIRTY_FLAG_PTR = 3

# The memory address at which the input vault root is stored.
const INPUT_VAULT_ROOT_PTR = 4

# The memory address at which the output vault root is stored.
const OUTPUT_VAULT_ROOT_PTR = 8

# Pointer to the prefix and suffix of the ID of the foreign account which will be loaded during the
# upcoming FPI call. This ID is updated during the `prepare_fpi_call` kernel procedure.
const UPCOMING_FOREIGN_ACCOUNT_SUFFIX_PTR = 12
const UPCOMING_FOREIGN_ACCOUNT_PREFIX_PTR = UPCOMING_FOREIGN_ACCOUNT_SUFFIX_PTR + 1

# Pointer to the 16th input value (with index 15) of the foreign procedure which will be loaded
# during the upcoming FPI call. This "buffer" value helps to work around the 15 value limitation of
# the `exec_kernel_proc` kernel procedure, so that any account procedure, even if it has 16 input
# values, could be executed as foreign.
const UPCOMING_FOREIGN_PROC_INPUT_VALUE_15_PTR = 14

# Pointer to the root of the foreign procedure which will be executed during the upcoming FPI call.
# This root is updated during the `prepare_fpi_call` kernel procedure.
const UPCOMING_FOREIGN_PROCEDURE_PTR = 16

# The memory address at which the pointer to the account stack element containing the pointer to the
# currently accessing account (active account) data is stored.
const ACCOUNT_STACK_TOP_PTR=20

# Pointer to the first element on the account stack.
const MIN_ACCOUNT_STACK_PTR=21

# Pointer to the last element on the account stack.
const MAX_ACCOUNT_STACK_PTR=84

# GLOBAL INPUTS
# -------------------------------------------------------------------------------------------------

# The memory address at which the global inputs section begins.
const GLOBAL_INPUTS_SECTION_OFFSET=400

# The memory address at which the transaction reference block's commitment is stored.
const BLOCK_COMMITMENT_PTR=400

# The memory address at which the native account ID provided as a global transaction input is
# stored.
const GLOBAL_ACCOUNT_ID_SUFFIX_PTR = 404
const GLOBAL_ACCOUNT_ID_PREFIX_PTR = GLOBAL_ACCOUNT_ID_SUFFIX_PTR + 1

# The memory address at which the initial account commitment is stored.
const INIT_ACCOUNT_COMMITMENT_PTR=408

# The memory address at which the initial nonce is stored.
const INIT_NONCE_PTR=412

# The memory address at which the initial vault root of the native account is stored.
const INIT_NATIVE_ACCOUNT_VAULT_ROOT_PTR=416

# The memory address at which the initial storage commitment of the native account is stored.
const INIT_NATIVE_ACCOUNT_STORAGE_COMMITMENT_PTR=420

# The memory address at which the input notes commitment is stored.
const INPUT_NOTES_COMMITMENT_PTR=424

# The memory address at which the transaction script mast root is stored.
const TX_SCRIPT_ROOT_PTR=428

# The memory address at which the transaction script arguments are stored.
const TX_SCRIPT_ARGS_PTR=432

# The memory address at which the auth procedure arguments are stored.
const AUTH_ARGS_PTR=436

# GLOBAL BLOCK DATA
# -------------------------------------------------------------------------------------------------

# The memory address at which the block data section begins
const BLOCK_DATA_SECTION_OFFSET=800

# The memory address at which the previous block commitment is stored
const PREV_BLOCK_COMMITMENT_PTR=800

# The memory address at which the chain commitment is stored
const CHAIN_COMMITMENT_PTR=804

# The memory address at which the account root is stored
const ACCT_DB_ROOT_PTR=808

# The memory address at which the nullifier root is stored
const NULLIFIER_ROOT_PTR=812

# The memory address at which the tx commitment is stored
const TX_COMMITMENT_PTR=816

# The memory address at which the kernel commitment is stored
const TX_KERNEL_COMMITMENT_PTR=820

# The memory address at which the proof commitment is stored
const VALIDATOR_KEY_COMMITMENT_PTR=824

# The memory address at which the block metadata is stored [block_number, version, timestamp, 0]
const BLOCK_METADATA_PTR=828

# The memory address at which the fee parameters are stored. These occupy a double word.
# [0, verification_base_fee, native_asset_id_suffix, native_asset_id_prefix]
# [0, 0, 0, 0]
const FEE_PARAMETERS_PTR=832

# The memory address at which the verification base fee is stored.
const VERIFICATION_BASE_FEE_PTR = FEE_PARAMETERS_PTR + 1

# The memory address at which the native asset ID is stored.
const NATIVE_ASSET_ID_SUFFIX_PTR = FEE_PARAMETERS_PTR + 2
const NATIVE_ASSET_ID_PREFIX_PTR = FEE_PARAMETERS_PTR + 3

# The memory address at which the note root is stored
const NOTE_ROOT_PTR=840

# PARTIAL BLOCKCHAIN
# -------------------------------------------------------------------------------------------------

# The memory address at which the chain data section begins
const PARTIAL_BLOCKCHAIN_PTR=1200

# The memory address at which the total number of leaves in the partial blockchain is stored
const PARTIAL_BLOCKCHAIN_NUM_LEAVES_PTR=1200

# The memory address at which the partial blockchain peaks are stored
const PARTIAL_BLOCKCHAIN_PEAKS_PTR=1204

# KERNEL DATA
# -------------------------------------------------------------------------------------------------

# The memory address at which the number of the kernel procedures is stored.
const NUM_KERNEL_PROCEDURES_PTR=1600

# The memory address at which the hashes of kernel procedures begin.
const KERNEL_PROCEDURES_PTR=1604

# ACCOUNT DATA
# -------------------------------------------------------------------------------------------------

# The largest memory address which can be used to load the foreign account data.
# It is computed as `2048 * 64 * 4` -- this is the memory address where the data block of the 64th
# account starts.
const MAX_FOREIGN_ACCOUNT_PTR=524288

# The memory address at which the native account data is stored.
const NATIVE_ACCOUNT_DATA_PTR=8192
const NATIVE_ACCOUNT_ID_SUFFIX_PTR = NATIVE_ACCOUNT_DATA_PTR + ACCT_ID_SUFFIX_OFFSET
const NATIVE_ACCOUNT_ID_PREFIX_PTR = NATIVE_ACCOUNT_DATA_PTR + ACCT_ID_PREFIX_OFFSET

# The length of the memory interval that the account data occupies.
const ACCOUNT_DATA_LENGTH=8192

# The offsets at which the account data is stored relative to the start of the account data segment.
const ACCT_NONCE_OFFSET=0
const ACCT_ID_AND_NONCE_OFFSET=0
const ACCT_ID_SUFFIX_OFFSET=2
const ACCT_ID_PREFIX_OFFSET=3
const ACCT_VAULT_ROOT_OFFSET=4
const ACCT_STORAGE_COMMITMENT_OFFSET=8
const ACCT_CODE_COMMITMENT_OFFSET=12
const ACCT_CORE_DATA_SECTION_END_OFFSET=16
const ACCT_NUM_PROCEDURES_OFFSET=28
const ACCT_PROCEDURES_SECTION_OFFSET=32
const ACCT_PROCEDURES_CALL_TRACKING_OFFSET=1060
const ACCT_NUM_STORAGE_SLOTS_OFFSET=1316
# Offset at which a copy of the initial account's storage slot section is kept. This section is
# only present for the native account.
const ACCT_INITIAL_STORAGE_SLOTS_SECTION_OFFSET=1320

# Offset at which the account's active storage slot section is kept. This section contains the
# current values of the account storage slots.
const ACCT_ACTIVE_STORAGE_SLOTS_SECTION_OFFSET=3360

# NATIVE ACCOUNT DELTA
# -------------------------------------------------------------------------------------------------

# The link map pointer at which the delta of the fungible asset vault is stored.
pub const ACCOUNT_DELTA_FUNGIBLE_ASSET_PTR=532480

# The link map pointer at which the delta of the non-fungible asset vault is stored.
pub const ACCOUNT_DELTA_NON_FUNGIBLE_ASSET_PTR=ACCOUNT_DELTA_FUNGIBLE_ASSET_PTR+4

# The section of link map pointers where storage map deltas are stored.
# This section is offset by `slot index` to get the link map ptr for the storage map
# delta at that slot index. Slot indices that are not map slots are simply unused.
const ACCOUNT_DELTA_STORAGE_MAP_SECTION=ACCOUNT_DELTA_FUNGIBLE_ASSET_PTR+8

# INPUT NOTES DATA
# -------------------------------------------------------------------------------------------------

# The memory address at which the input note section begins.
const INPUT_NOTE_SECTION_OFFSET=4194304

# The memory address at which the nullifier section of the input notes begins.
const INPUT_NOTE_NULLIFIER_SECTION_PTR=4194308

# The memory address at which the input note data section begins.
const INPUT_NOTE_DATA_SECTION_OFFSET=4259840

# The memory address at which the number of input notes is stored.
const NUM_INPUT_NOTES_PTR=INPUT_NOTE_SECTION_OFFSET

# The offsets at which data of an input note is stored relative to the start of its data segment
const INPUT_NOTE_ID_OFFSET=0
const INPUT_NOTE_CORE_DATA_OFFSET=4
const INPUT_NOTE_SERIAL_NUM_OFFSET=4
const INPUT_NOTE_SCRIPT_ROOT_OFFSET=8
const INPUT_NOTE_STORAGE_COMMITMENT_OFFSET=12
const INPUT_NOTE_ASSETS_COMMITMENT_OFFSET=16
const INPUT_NOTE_RECIPIENT_OFFSET=20
const INPUT_NOTE_METADATA_HEADER_OFFSET=24
const INPUT_NOTE_ATTACHMENT_OFFSET=28
const INPUT_NOTE_ARGS_OFFSET=32
const INPUT_NOTE_NUM_STORAGE_ITEMS_OFFSET=36
const INPUT_NOTE_NUM_ASSETS_OFFSET=40
const INPUT_NOTE_ASSETS_OFFSET=44

# OUTPUT NOTES
# -------------------------------------------------------------------------------------------------

# The memory address at which the output notes section begins.
const OUTPUT_NOTE_SECTION_OFFSET=16777216

# The offsets at which data of an output note is stored relative to the start of its data segment.
const OUTPUT_NOTE_ID_OFFSET=0
const OUTPUT_NOTE_METADATA_HEADER_OFFSET=4
const OUTPUT_NOTE_METADATA_ATTACHMENT_KIND_SCHEME_OFFSET=OUTPUT_NOTE_METADATA_HEADER_OFFSET + 3
const OUTPUT_NOTE_ATTACHMENT_OFFSET=8
const OUTPUT_NOTE_RECIPIENT_OFFSET=12
const OUTPUT_NOTE_ASSETS_COMMITMENT_OFFSET=16
const OUTPUT_NOTE_NUM_ASSETS_OFFSET=20
const OUTPUT_NOTE_DIRTY_FLAG_OFFSET=21
const OUTPUT_NOTE_ASSETS_OFFSET=24

# LINK MAP MEMORY
# -------------------------------------------------------------------------------------------------

# The inclusive start of the link map dynamic memory region.
# Chosen as a number greater than 2^25 such that all entry pointers are multiples of
# LINK_MAP_ENTRY_SIZE. That enables a simpler check in assert_entry_ptr_is_valid.
const LINK_MAP_REGION_START_PTR=33554448

# The non-inclusive end of the link map dynamic memory region.
# This happens to be 2^26, but if it is changed, it should be chosen as a number such that
# LINK_MAP_REGION_END_PTR - LINK_MAP_REGION_START_PTR is a multiple of LINK_MAP_ENTRY_SIZE,
# because that enables checking whether a newly allocated entry pointer is at the end of the range
# using equality rather than lt/gt in link_map_malloc.
const LINK_MAP_REGION_END_PTR=67108864

# LINK_MAP_REGION_START_PTR + the currently used size stored at this pointer defines the next
# entry pointer that will be allocated.
const LINK_MAP_USED_MEMORY_SIZE=33554432

# The size of each map entry, i.e. four words.
const LINK_MAP_ENTRY_SIZE=16

# MEMORY PROCEDURES
# =================================================================================================

# BOOK KEEPING
# -------------------------------------------------------------------------------------------------

#! Returns the current number of output notes created in this transaction.
#!
#! Inputs:  []
#! Outputs: [num_output_notes]
#!
#! Where:
#! - num_output_notes is the number of output notes created in this transaction so far.
pub proc get_num_output_notes
    mem_load.NUM_OUTPUT_NOTES_PTR
end

#! Sets the number of output notes.
#!
#! Inputs:  [num_output_notes]
#! Outputs: []
#!
#! Where:
#! - num_output_notes is the number of output notes.
pub proc set_num_output_notes
    mem_store.NUM_OUTPUT_NOTES_PTR
end

#! Returns the pointer to the active input note.
#!
#! Inputs:  []
#! Outputs: [note_ptr]
#!
#! Where:
#! - note_ptr is the memory address of the data segment for the active note.
pub proc get_active_input_note_ptr
    mem_load.ACTIVE_INPUT_NOTE_PTR
end

#! Sets the active note pointer to the specified value.
#!
#! Inputs:  [note_ptr]
#! Outputs: []
#!
#! Where:
#! - note_ptr is the new memory address of the data segment for the input note.
pub proc set_active_input_note_ptr
    mem_store.ACTIVE_INPUT_NOTE_PTR
end

#! Returns the pointer to the memory address at which the input vault root is stored.
#!
#! Inputs:  []
#! Outputs: [input_vault_root_ptr]
#!
#! Where:
#! - input_vault_root_ptr is a pointer to the memory address at which the input vault root is
#!   stored.
pub proc get_input_vault_root_ptr
    push.INPUT_VAULT_ROOT_PTR
end

#! Returns the input vault root.
#!
#! Inputs:  []
#! Outputs: [INPUT_VAULT_ROOT]
#!
#! Where:
#! - INPUT_VAULT_ROOT is the input vault root.
pub proc get_input_vault_root
    padw mem_loadw_le.INPUT_VAULT_ROOT_PTR
end

#! Sets the input vault root.
#!
#! Inputs:  [INPUT_VAULT_ROOT]
#! Outputs: [INPUT_VAULT_ROOT]
#!
#! Where:
#! - INPUT_VAULT_ROOT is the input vault root.
pub proc set_input_vault_root
    mem_storew_le.INPUT_VAULT_ROOT_PTR
end

#! Returns the pointer to the memory address at which the output vault root is stored.
#!
#! Inputs:  []
#! Outputs: [output_vault_root_ptr]
#!
#! Where:
#! - output_vault_root_ptr is the pointer to the memory address at which the output vault root is
#!   stored.
pub proc get_output_vault_root_ptr
    push.OUTPUT_VAULT_ROOT_PTR
end

#! Returns the output vault root.
#!
#! Inputs:  []
#! Outputs: [OUTPUT_VAULT_ROOT]
#!
#! Where:
#! - OUTPUT_VAULT_ROOT is the output vault root.
pub proc get_output_vault_root
    padw mem_loadw_le.OUTPUT_VAULT_ROOT_PTR
end

#! Sets the output vault root.
#!
#! Inputs:  [OUTPUT_VAULT_ROOT]
#! Outputs: [OUTPUT_VAULT_ROOT]
#!
#! Where:
#! - OUTPUT_VAULT_ROOT is the output vault root.
pub proc set_output_vault_root
    mem_storew_le.OUTPUT_VAULT_ROOT_PTR
end

#! Sets the ID of the foreign account which is going to be loaded during the upcoming FPI call.
#!
#! Inputs:  [foreign_account_id_suffix, foreign_account_id_prefix]
#! Outputs: []
#!
#! Where:
#! - foreign_account_id_{prefix,suffix} are the prefix and suffix felts of the ID of the foreign
#!   account whose procedure is going to be executed.
pub proc set_fpi_account_id(foreign_account_id: AccountId)
    mem_store.UPCOMING_FOREIGN_ACCOUNT_SUFFIX_PTR
    # => [foreign_account_id_prefix]

    mem_store.UPCOMING_FOREIGN_ACCOUNT_PREFIX_PTR
    # => []
end

#! Returns the ID of the foreign account which is going to be loaded during the upcoming FPI call.
#!
#! WARNING: The ID felts may be zero.
#!
#! Inputs:  []
#! Outputs: [foreign_account_id_suffix, foreign_account_id_prefix]
#!
#! Where:
#! - foreign_account_id_{prefix,suffix} are the prefix and suffix felts of the ID of the foreign
#!   account whose procedure is going to be executed.
pub proc get_fpi_account_id() -> AccountId
    mem_load.UPCOMING_FOREIGN_ACCOUNT_PREFIX_PTR
    # => [foreign_account_id_prefix]

    mem_load.UPCOMING_FOREIGN_ACCOUNT_SUFFIX_PTR
    # => [foreign_account_id_suffix, foreign_account_id_prefix]
end

#! Sets the root of the foreign procedure which is going to be loaded during the upcoming FPI call.
#!
#! Inputs:  [FOREIGN_PROC_ROOT]
#! Outputs: [FOREIGN_PROC_ROOT]
#!
#! Where:
#! - FOREIGN_PROC_ROOT is the root of the foreign procedure which will be executed during the FPI
#!   call.
pub proc set_fpi_procedure_root(foreign_proc_root: word) -> word
    mem_storew_le.UPCOMING_FOREIGN_PROCEDURE_PTR
end

# GLOBAL INPUTS
# -------------------------------------------------------------------------------------------------

#! Sets the commitment of the transaction reference block.
#!
#! Inputs:  [BLOCK_COMMITMENT]
#! Outputs: [BLOCK_COMMITMENT]
#!
#! Where:
#! - BLOCK_COMMITMENT is the commitment of the transaction reference block.
pub proc set_block_commitment
    mem_storew_le.BLOCK_COMMITMENT_PTR
end

#! Returns the block commitment of the reference block.
#!
#! Inputs:  []
#! Outputs: [BLOCK_COMMITMENT]
#!
#! Where:
#! - BLOCK_COMMITMENT is the commitment of the transaction reference block.
pub proc get_block_commitment
    padw mem_loadw_le.BLOCK_COMMITMENT_PTR
end

#! Sets the global ID of the native account.
#!
#! Inputs:  [account_id_suffix, account_id_prefix]
#! Outputs: []
#!
#! Where:
#! - account_id_{prefix,suffix} are the prefix and suffix felts of the account ID.
pub proc set_global_account_id
    mem_store.GLOBAL_ACCOUNT_ID_SUFFIX_PTR
    mem_store.GLOBAL_ACCOUNT_ID_PREFIX_PTR
    # => []
end

#! Returns the global ID of the native account.
#!
#! Inputs:  []
#! Outputs: [account_id_suffix, account_id_prefix]
#!
#! Where:
#! - account_id_{prefix,suffix} are the prefix and suffix felts of the account ID.
pub proc get_global_account_id
    mem_load.GLOBAL_ACCOUNT_ID_PREFIX_PTR
    mem_load.GLOBAL_ACCOUNT_ID_SUFFIX_PTR
    # => [account_id_suffix, account_id_prefix]
end

#! Sets the native account commitment at the beginning of the transaction.
#!
#! Inputs:  [INIT_ACCOUNT_COMMITMENT]
#! Outputs: [INIT_ACCOUNT_COMMITMENT]
#!
#! Where:
#! - INIT_ACCOUNT_COMMITMENT is the initial account commitment.
pub proc set_init_account_commitment
    mem_storew_le.INIT_ACCOUNT_COMMITMENT_PTR
end

#! Returns the native account commitment at the beginning of the transaction.
#!
#! Inputs:  []
#! Outputs: [INIT_ACCOUNT_COMMITMENT]
#!
#! Where:
#! - INIT_ACCOUNT_COMMITMENT is the initial account commitment.
pub proc get_init_account_commitment
    padw mem_loadw_le.INIT_ACCOUNT_COMMITMENT_PTR
end

#! Sets the initial account nonce.
#!
#! Inputs:  [init_nonce]
#! Outputs: []
#!
#! Where:
#! - init_nonce is the initial account nonce.
pub proc set_init_nonce
    mem_store.INIT_NONCE_PTR
end

#! Returns the initial account nonce.
#!
#! Inputs:  []
#! Outputs: [init_nonce]
#!
#! Where:
#! - init_nonce is the initial account nonce.
pub proc get_init_nonce
    mem_load.INIT_NONCE_PTR
end

#! Sets the vault root of the native account at the beginning of the transaction.
#!
#! Inputs:  [INIT_NATIVE_ACCOUNT_VAULT_ROOT]
#! Outputs: [INIT_NATIVE_ACCOUNT_VAULT_ROOT]
#!
#! Where:
#! - INIT_NATIVE_ACCOUNT_VAULT_ROOT is the initial vault root of the native account.
pub proc set_init_native_account_vault_root
    mem_storew_le.INIT_NATIVE_ACCOUNT_VAULT_ROOT_PTR
end

#! Returns the vault root of the native account at the beginning of the transaction.
#!
#! Inputs:  []
#! Outputs: [INIT_NATIVE_ACCOUNT_VAULT_ROOT]
#!
#! Where:
#! - INIT_NATIVE_ACCOUNT_VAULT_ROOT is the initial vault root of the native account.
pub proc get_init_native_account_vault_root
    padw mem_loadw_le.INIT_NATIVE_ACCOUNT_VAULT_ROOT_PTR
end

#! Returns the memory address of the vault root of the native account at the beginning of the
#! transaction.
#!
#! Inputs:  []
#! Outputs: [native_account_initial_vault_root_ptr]
#!
#! Where:
#! - native_account_initial_vault_root_ptr is the memory pointer to the initial vault root of the
#!   native account.
pub proc get_init_native_account_vault_root_ptr
    push.INIT_NATIVE_ACCOUNT_VAULT_ROOT_PTR
end

#! Sets the storage commitment of the native account at the beginning of the transaction.
#!
#! Inputs:  [INIT_NATIVE_ACCOUNT_STORAGE_COMMITMENT]
#! Outputs: [INIT_NATIVE_ACCOUNT_STORAGE_COMMITMENT]
#!
#! Where:
#! - INIT_NATIVE_ACCOUNT_STORAGE_COMMITMENT is the initial storage commitment of the native account.
pub proc set_init_account_storage_commitment
    mem_storew_le.INIT_NATIVE_ACCOUNT_STORAGE_COMMITMENT_PTR
end

#! Returns the storage commitment of the native account at the beginning of the transaction.
#!
#! Inputs:  []
#! Outputs: [INIT_NATIVE_ACCOUNT_STORAGE_COMMITMENT]
#!
#! Where:
#! - INIT_NATIVE_ACCOUNT_STORAGE_COMMITMENT is the initial storage commitment of the native account.
pub proc get_init_account_storage_commitment
    padw mem_loadw_le.INIT_NATIVE_ACCOUNT_STORAGE_COMMITMENT_PTR
end

#! Returns the input notes commitment.
#!
#! See `transaction::api::get_input_notes_commitment` for details.
#!
#! Inputs:  []
#! Outputs: [INPUT_NOTES_COMMITMENT]
#!
#! Where:
#! - INPUT_NOTES_COMMITMENT is the input notes commitment.
pub proc get_input_notes_commitment
    padw mem_loadw_le.INPUT_NOTES_COMMITMENT_PTR
end

#! Sets the input notes' commitment.
#!
#! Inputs:  [INPUT_NOTES_COMMITMENT]
#! Outputs: [INPUT_NOTES_COMMITMENT]
#!
#! Where:
#! - INPUT_NOTES_COMMITMENT is the notes' commitment.
pub proc set_nullifier_commitment
    mem_storew_le.INPUT_NOTES_COMMITMENT_PTR
end

#! Returns the memory address of the transaction script root.
#!
#! Inputs:  []
#! Outputs: [tx_script_root_ptr]
#!
#! Where:
#! - tx_script_root_ptr is the pointer to the memory where transaction script root is stored.
pub proc get_tx_script_root_ptr
    push.TX_SCRIPT_ROOT_PTR
end

#! Sets the transaction script root.
#!
#! Inputs:  [TX_SCRIPT_ROOT]
#! Outputs: [TX_SCRIPT_ROOT]
#!
#! Where:
#! - TX_SCRIPT_ROOT is the transaction script root.
pub proc set_tx_script_root
    mem_storew_le.TX_SCRIPT_ROOT_PTR
end

#! Returns the transaction script arguments.
#!
#! Inputs:  []
#! Outputs: [TX_SCRIPT_ARGS]
#!
#! Where:
#! - TX_SCRIPT_ARGS is the word of values which could be used directly or could be used to obtain
#!   some values associated with it from the advice map.
pub proc get_tx_script_args
    padw mem_loadw_le.TX_SCRIPT_ARGS_PTR
end

#! Sets the transaction script arguments.
#!
#! Inputs:  [TX_SCRIPT_ARGS]
#! Outputs: [TX_SCRIPT_ARGS]
#!
#! Where:
#! - TX_SCRIPT_ARGS is the word of values which could be used directly or could be used to obtain
#!   some values associated with it from the advice map.
pub proc set_tx_script_args
    mem_storew_le.TX_SCRIPT_ARGS_PTR
end

#! Returns the auth procedure arguments.
#!
#! Inputs:  []
#! Outputs: [AUTH_ARGS]
#!
#! Where:
#! - AUTH_ARGS is the argument passed to the auth procedure.
pub proc get_auth_args
    padw mem_loadw_le.AUTH_ARGS_PTR
end

#! Sets the auth procedure arguments.
#!
#! Inputs:  [AUTH_ARGS]
#! Outputs: [AUTH_ARGS]
#!
#! Where:
#! - AUTH_ARGS is the argument passed to the auth procedure.
pub proc set_auth_args
    mem_storew_le.AUTH_ARGS_PTR
end

# BLOCK DATA
# -------------------------------------------------------------------------------------------------

#! Returns a pointer to the block data section.
#!
#! Inputs:  []
#! Outputs: [ptr]
#!
#! Where:
#! - ptr is a pointer to the block data section.
pub proc get_block_data_ptr
    push.BLOCK_DATA_SECTION_OFFSET
end

#! Returns the previous block commitment of the transaction reference block.
#!
#! Inputs:  []
#! Outputs: [PREV_BLOCK_COMMITMENT]
#!
#! Where:
#! - PREV_BLOCK_COMMITMENT_PTR is the block commitment of the transaction reference block.
pub proc get_prev_block_commitment
    padw mem_loadw_le.PREV_BLOCK_COMMITMENT_PTR
end

#! Returns the block number of the transaction reference block.
#!
#! Inputs:  []
#! Outputs: [blk_num]
#!
#! Where:
#! - blk_num is the block number of the transaction reference block.
pub proc get_blk_num
    mem_load.BLOCK_METADATA_PTR
end

#! Returns the protocol version of the transaction reference block.
#!
#! Inputs:  []
#! Outputs: [version]
#!
#! Where:
#! - version is the protocol version of the transaction reference block.
const BLOCK_METADATA_VERSION_PTR=BLOCK_METADATA_PTR+1
pub proc get_blk_version
    mem_load.BLOCK_METADATA_VERSION_PTR
end

#! Returns the block timestamp of the reference block for this transaction.
#!
#! Inputs:  []
#! Outputs: [timestamp]
#!
#! Where:
#! - timestamp is the timestamp of the reference block for this transaction.
const BLOCK_METADATA_TIMESTAMP_PTR=BLOCK_METADATA_PTR+2
pub proc get_blk_timestamp
    mem_load.BLOCK_METADATA_TIMESTAMP_PTR
end

#! Returns the faucet ID of the native asset as defined in the transaction's reference block.
#!
#! Inputs:  []
#! Outputs: [native_asset_id_suffix, native_asset_id_prefix]
#!
#! Where:
#! - native_asset_id_{prefix,suffix} are the prefix and suffix felts of the faucet ID that defines
#!   the native asset.
pub proc get_native_asset_id
    mem_load.NATIVE_ASSET_ID_PREFIX_PTR
    mem_load.NATIVE_ASSET_ID_SUFFIX_PTR
    # => [native_asset_id_suffix, native_asset_id_prefix]
end

#! Returns the verification base fee from the transaction's reference block.
#!
#! Inputs:  []
#! Outputs: [verification_base_fee]
#!
#! Where:
#! - verification_base_fee is the base fee capturing the cost for the verification of a
#!   transaction.
pub proc get_verification_base_fee
    mem_load.VERIFICATION_BASE_FEE_PTR
end

#! Returns the chain commitment of the transaction reference block.
#!
#! Inputs:  []
#! Outputs: [CHAIN_COMMITMENT]
#!
#! Where:
#! - CHAIN_COMMITMENT is the chain commitment of the transaction reference block.
pub proc get_chain_commitment
    padw mem_loadw_le.CHAIN_COMMITMENT_PTR
end

#! Returns the account db root of the transaction reference block.
#!
#! Inputs:  []
#! Outputs: [ACCT_DB_ROOT]
#!
#! Where:
#! - ACCT_DB_ROOT is the account database root of the transaction reference block.
pub proc get_account_db_root
    padw mem_loadw_le.ACCT_DB_ROOT_PTR
end

#! Returns the nullifier db root of the transaction reference block.
#!
#! Inputs:  []
#! Outputs: [NULLIFIER_ROOT]
#!
#! Where:
#! - NULLIFIER_ROOT is the nullifier root of the transaction reference block.
pub proc get_nullifier_db_root
    padw mem_loadw_le.NULLIFIER_ROOT_PTR
end

#! Returns the tx commitment of the transaction reference block.
#!
#! Inputs:  []
#! Outputs: [TX_COMMITMENT]
#!
#! Where:
#! - TX_COMMITMENT is the tx commitment of the transaction reference block.
pub proc get_tx_commitment
    padw mem_loadw_le.TX_COMMITMENT_PTR
end

#! Returns the transaction kernel commitment of the transaction reference block.
#!
#! Inputs:  []
#! Outputs: [TX_KERNEL_COMMITMENT]
#!
#! Where:
#! - TX_KERNEL_COMMITMENT is the sequential hash of the kernel procedures.
pub proc get_tx_kernel_commitment
    padw mem_loadw_le.TX_KERNEL_COMMITMENT_PTR
end

#! Returns the validator key commitment of the transaction reference block.
#!
#! Inputs:  []
#! Outputs: [VALIDATOR_KEY_COMMITMENT]
#!
#! Where:
#! - VALIDATOR_KEY_COMMITMENT is the public key commitment of the transaction reference block.
pub proc get_validator_key_commitment
    padw mem_loadw_le.VALIDATOR_KEY_COMMITMENT_PTR
end

#! Returns the note root of the transaction reference block.
#!
#! Inputs:  []
#! Outputs: [NOTE_ROOT]
#!
#! Where:
#! - NOTE_ROOT is the note root of the transaction reference block.
pub proc get_note_root
    padw mem_loadw_le.NOTE_ROOT_PTR
end

#! Sets the note root of the transaction reference block.
#!
#! Inputs:  [NOTE_ROOT]
#! Outputs: [NOTE_ROOT]
#!
#! Where:
#! - NOTE_ROOT is the note root of the transaction reference block.
pub proc set_note_root
    mem_storew_le.NOTE_ROOT_PTR
end

# CHAIN DATA
# -------------------------------------------------------------------------------------------------

#! Returns a pointer to the partial blockchain section.
#!
#! Inputs:  []
#! Outputs: [ptr]
#!
#! Where:
#! - ptr is the pointer to the partial blockchain section.
pub proc get_partial_blockchain_ptr
    push.PARTIAL_BLOCKCHAIN_PTR
end

#! Sets the number of leaves in the partial blockchain.
#!
#! Inputs:  [num_leaves]
#! Outputs: []
#!
#! Where:
#! - num_leaves is the number of leaves in the partial blockchain.
pub proc set_partial_blockchain_num_leaves
    mem_store.PARTIAL_BLOCKCHAIN_NUM_LEAVES_PTR
end

#! Returns a pointer to start of the partial blockchain peaks section.
#!
#! Inputs:  []
#! Outputs: [ptr]
#!
#! Where:
#! - ptr is the pointer to the start of the partial blockchain peaks section.
pub proc get_partial_blockchain_peaks_ptr
    push.PARTIAL_BLOCKCHAIN_PEAKS_PTR
end

# ACCOUNT DATA
# -------------------------------------------------------------------------------------------------

#! Returns the memory pointer at which the native account data is stored.
#!
#! Inputs:  []
#! Outputs: [ptr]
#!
#! Where:
#! - ptr is the memory address at which the native account data is stored.
pub proc get_native_account_data_ptr
    push.NATIVE_ACCOUNT_DATA_PTR
end

#! Returns the length of the memory interval that the account data occupies.
#!
#! Inputs:  []
#! Outputs: [account_data_length]
#!
#! Where:
#! - account_data_length is the length of the memory interval that the account data occupies.
pub proc get_account_data_length
    push.ACCOUNT_DATA_LENGTH
end

#! Returns the largest memory address which can be used to load the foreign account data.
#!
#! Inputs:  []
#! Outputs: [max_foreign_account_ptr]
#!
#! Where:
#! - max_foreign_account_ptr is the largest memory address which can be used to load the foreign
#!   account data.
pub proc get_max_foreign_account_ptr
    push.MAX_FOREIGN_ACCOUNT_PTR
end

#! Sets the memory pointer of the active account data to the native account (8192).
#!
#! Inputs:  []
#! Outputs: []
pub proc set_active_account_data_ptr_to_native_account
    # store the native account data pointer into the first account stack element.
    push.NATIVE_ACCOUNT_DATA_PTR mem_store.MIN_ACCOUNT_STACK_PTR
    # => [native_acct_stack_ptr, account_stack_top_ptr]

    # store the pointer to the first account stack element into the account stack top pointer.
    push.MIN_ACCOUNT_STACK_PTR mem_store.ACCOUNT_STACK_TOP_PTR
    # => []
end

#! Returns the memory pointer of the active account data.
#!
#! Inputs:  []
#! Outputs: [active_account_data_ptr]
#!
#! Where:
#! - active_account_data_ptr is the memory address at which the data of the active account begins.
pub proc get_active_account_data_ptr
    mem_load.ACCOUNT_STACK_TOP_PTR
    # => [account_stack_top_ptr]

    mem_load
    # => [active_account_data_ptr]
end

#! Adds the pointer to the account stack.
#!
#! It is done by writing the pointer to the element next to the stack top and moving the top pointer
#! to it.
#!
#! Inputs:  [curr_account_data_ptr]
#! Outputs: []
#!
#! Where:
#! - curr_account_data_ptr is the memory address
#!
#! Panics if:
#! - the account stack is full, containing 64 accounts total.
#! - the provided account data pointer is equal to the native account data pointer.
pub proc push_ptr_to_account_stack
    # check that the account stack is not full
    mem_load.ACCOUNT_STACK_TOP_PTR dup
    # => [account_stack_top_ptr, account_stack_top_ptr, curr_account_data_ptr]

    u32lt.MAX_ACCOUNT_STACK_PTR assert.err=ERR_ACCOUNT_STACK_OVERFLOW
    # => [account_stack_top_ptr, curr_account_data_ptr]

    # check that the active account data pointer is not equal to the native account data pointer
    dup.1 eq.NATIVE_ACCOUNT_DATA_PTR
    assertz.err=ERR_FOREIGN_ACCOUNT_CONTEXT_AGAINST_NATIVE_ACCOUNT
    # => [account_stack_top_ptr, curr_account_data_ptr]

    add.1 dup movdn.2 mem_store
    # => [account_stack_top_ptr+1]

    mem_store.ACCOUNT_STACK_TOP_PTR
    # => []
end

#! Removes the pointer from the account stack.
#!
#! It is done simply by moving the stack top pointer one element back.
#!
#! Inputs:  []
#! Outputs: []
#!
#! Panics if:
#! - the account stack contains only native account.
pub proc pop_ptr_from_account_stack
    # check that the account stack always is at least of size 1, that is, it contains at least the
    # native account
    mem_load.ACCOUNT_STACK_TOP_PTR dup
    # => [account_stack_top_ptr, account_stack_top_ptr]

    u32gt.MIN_ACCOUNT_STACK_PTR assert.err=ERR_ACCOUNT_STACK_UNDERFLOW
    # => [account_stack_top_ptr]

    # move the account stack pointer one element back and store it as the top pointer
    sub.1 mem_store.ACCOUNT_STACK_TOP_PTR
    # => []
end

#! Asserts that active account data pointer matches the data pointer of the native account (8192).
#! It is used to prevent usage of the account procedures which can mutate the account state with the
#! foreign accounts.
#!
#! Inputs:  []
#! Outputs: []
#!
#! Panics if:
#! - the active account data pointer is not equal to native account data pointer (8192).
pub proc assert_native_account
    exec.is_native_account assert.err=ERR_ACCOUNT_IS_NOT_NATIVE
end

#! Returns 1 if the active account data pointer matches the data pointer of the native account,
#! 0 otherwise.
#!
#! Inputs:  []
#! Outputs: [is_native_account]
pub proc is_native_account
    exec.get_active_account_data_ptr
    # => [active_account_data_ptr]

    eq.NATIVE_ACCOUNT_DATA_PTR
    # => [is_native_account]
end

#! Returns 1 if the native account is new, i.e. its nonce is zero, 0 otherwise.
#!
#! Inputs:  []
#! Outputs: [is_new_account]
pub proc is_new_account
    # the account is new if its nonce is zero
    # use get_init_nonce so this procedure works correctly even after the account's nonce was
    # incremented
    exec.get_init_nonce eq.0
end

#! Returns a pointer to the end of the core account data section.
#!
#! Inputs:  []
#! Outputs: [ptr]
#!
#! Where:
#! - ptr is the memory address at which the core account data ends.
pub proc get_core_account_data_end_ptr
    exec.get_active_account_data_ptr add.ACCT_CORE_DATA_SECTION_END_OFFSET
end

### ACCOUNT ID AND NONCE #################################################

#! Returns the ID of the active account.
#!
#! Inputs:  []
#! Outputs: [account_id_suffix, account_id_prefix]
#!
#! Where:
#! - account_id_{prefix,suffix} are the prefix and suffix felts of the ID of the active account.
pub proc get_account_id
    exec.get_active_account_data_ptr
    # => [active_account_data_ptr]

    dup add.ACCT_ID_PREFIX_OFFSET mem_load
    # => [account_id_prefix, active_account_data_ptr]

    swap add.ACCT_ID_SUFFIX_OFFSET mem_load
    # => [account_id_suffix, account_id_prefix]
end

#! Returns the ID of the native account of the transaction.
#!
#! Inputs:  []
#! Outputs: [account_id_suffix, account_id_prefix]
#!
#! Where:
#! - account_id_{prefix,suffix} are the prefix and suffix felts of the ID of the native account
#!   of the transaction.
pub proc get_native_account_id
    mem_load.NATIVE_ACCOUNT_ID_PREFIX_PTR
    mem_load.NATIVE_ACCOUNT_ID_SUFFIX_PTR
    # => [account_id_suffix, account_id_prefix]
end

#! Sets the account ID and nonce.
#!
#! Inputs:  [nonce, 0, account_id_suffix, account_id_prefix]
#! Outputs: [nonce, 0, account_id_suffix, account_id_prefix]
#!
#! Where:
#! - account_id_{prefix,suffix} are the prefix and suffix felts of the ID of the active account.
#! - nonce is the nonce of the active account.
pub proc set_account_id_and_nonce
    exec.get_active_account_data_ptr add.ACCT_ID_AND_NONCE_OFFSET
    mem_storew_le
end

#! Returns the nonce of the active account.
#!
#! Inputs:  []
#! Outputs: [nonce]
#!
#! Where:
#! - nonce is the nonce of the active account.
pub proc get_account_nonce
    exec.get_active_account_data_ptr add.ACCT_NONCE_OFFSET
    mem_load
end

#! Returns the nonce of the native account of the transaction.
#!
#! Inputs:  []
#! Outputs: [nonce]
#!
#! Where:
#! - nonce is the nonce of the native account of the transaction.
pub proc get_native_account_nonce
    push.NATIVE_ACCOUNT_DATA_PTR add.ACCT_NONCE_OFFSET
    mem_load
end

#! Sets the nonce of the native account.
#!
#! Inputs:  [nonce]
#! Outputs: []
#!
#! Where:
#! - nonce is the new nonce of the native account.
pub proc set_native_account_nonce
    push.NATIVE_ACCOUNT_DATA_PTR add.ACCT_NONCE_OFFSET
    mem_store
end

### ACCOUNT VAULT #################################################

#! Returns the memory pointer to the account vault root.
#!
#! Inputs:  []
#! Outputs: [account_vault_root_ptr]
#!
#! Where:
#! - account_vault_root_ptr is the memory pointer to the account asset vault root.
pub proc get_account_vault_root_ptr
    exec.get_active_account_data_ptr add.ACCT_VAULT_ROOT_OFFSET
end

#! Returns the account vault root.
#!
#! Inputs:  []
#! Outputs: [ACCT_VAULT_ROOT]
#!
#! Where:
#! - ACCT_VAULT_ROOT is the account asset vault root.
pub proc get_account_vault_root
    padw
    exec.get_active_account_data_ptr add.ACCT_VAULT_ROOT_OFFSET
    mem_loadw_le
end

#! Sets the account vault root.
#!
#! Inputs:  [ACCT_VAULT_ROOT]
#! Outputs: [ACCT_VAULT_ROOT]
#!
#! Where:
#! - ACCT_VAULT_ROOT is the account vault root to be set.
pub proc set_account_vault_root
    exec.get_active_account_data_ptr add.ACCT_VAULT_ROOT_OFFSET
    mem_storew_le
end

#! Returns the memory pointer to the initial vault root of the active account.
#!
#! For the native account, this returns the pointer to the initial vault root.
#! For foreign accounts, this returns the regular vault root pointer since foreign accounts
#! are read-only and their initial and current vault state always matches.
#!
#! Inputs:  []
#! Outputs: [account_initial_vault_root_ptr]
#!
#! Where:
#! - account_initial_vault_root_ptr is the memory pointer to the initial vault root.
pub proc get_account_initial_vault_root_ptr
    # for foreign account, use the regular vault root pointer since foreign accounts are read-only
    # and initial == current
    exec.get_account_vault_root_ptr
    # => [account_vault_root_ptr]

    # for native account, use the initial vault root pointer
    exec.get_init_native_account_vault_root_ptr
    # => [native_account_initial_vault_root_ptr, account_vault_root_ptr]

    # get the flag indicating whether the active account is native
    exec.is_native_account
    # => [is_native_account, native_account_initial_vault_root_ptr, account_vault_root_ptr]

    # according to the is_native_account flag, return the corresponding pointer
    cdrop
    # => [account_initial_vault_root_ptr]
end

### ACCOUNT CODE #################################################

#! Returns the code commitment of the account.
#!
#! Inputs:  []
#! Outputs: [CODE_COMMITMENT]
#!
#! Where:
#! - CODE_COMMITMENT is the code commitment of the account.
pub proc get_account_code_commitment
    padw
    exec.get_active_account_data_ptr add.ACCT_CODE_COMMITMENT_OFFSET
    mem_loadw_le
end

#! Sets the code commitment of the account.
#!
#! Inputs:  [CODE_COMMITMENT]
#! Outputs: [CODE_COMMITMENT]
#!
#! Where:
#! - CODE_COMMITMENT is the code commitment to be set.
pub proc set_account_code_commitment
    exec.get_active_account_data_ptr add.ACCT_CODE_COMMITMENT_OFFSET
    mem_storew_le
end

#! Sets the transaction expiration block number.
#!
#! Inputs:  [tx_expiration_block_num]
#! Outputs: []
#!
#! Where:
#! - tx_expiration_block_num is the number of the transaction expiration block.
pub proc set_expiration_block_num
    mem_store.TX_EXPIRATION_BLOCK_NUM_PTR
end

#! Gets the transaction expiration block number.
#!
#! Inputs:  []
#! Outputs: [tx_expiration_block_num]
#!
#! Where:
#! - tx_expiration_block_num is the number of the transaction expiration block.
pub proc get_expiration_block_num
    mem_load.TX_EXPIRATION_BLOCK_NUM_PTR
end

#! Returns the number of procedures contained in the account code.
#!
#! Inputs:  []
#! Outputs: [num_procedures]
#!
#! Where:
#! - num_procedures is the number of procedures contained in the account code.
pub proc get_num_account_procedures
    exec.get_active_account_data_ptr add.ACCT_NUM_PROCEDURES_OFFSET
    mem_load
end

#! Sets the number of procedures contained in the account code.
#!
#! Inputs:  [num_procedures]
#! Outputs: []
#!
#! Where:
#! - num_procedures is the number of procedures contained in the account code.
pub proc set_num_account_procedures
    exec.get_active_account_data_ptr add.ACCT_NUM_PROCEDURES_OFFSET
    mem_store
end

#! Returns the memory pointer to the account procedures section.
#!
#! Inputs:  []
#! Outputs: [account_procedures_section_ptr]
#!
#! Where:
#! - account_procedures_section_ptr is the memory pointer to the account procedures section.
pub proc get_account_procedures_section_ptr
    exec.get_active_account_data_ptr add.ACCT_PROCEDURES_SECTION_OFFSET
end

#! Returns the memory pointer to the account procedures call tracking section.
#!
#! Inputs:  []
#! Outputs: [procedures_call_tracking_ptr]
#!
#! Where:
#! - procedures_call_tracking_ptr is the memory pointer to the procedure call tracking section.
pub proc get_account_procedures_call_tracking_ptr
    exec.get_active_account_data_ptr add.ACCT_PROCEDURES_CALL_TRACKING_OFFSET
end

#! Returns the memory pointer to a specific account procedure.
#!
#! Inputs:  [proc_idx]
#! Outputs: [proc_ptr]
#!
#! Where:
#! - proc_idx is the index of the account procedure.
#! - proc_ptr is the memory pointer to the account procedure at the specified index.
pub proc get_account_procedure_ptr
    mul.ACCOUNT_PROCEDURE_DATA_LENGTH exec.get_account_procedures_section_ptr add
end

### ACCOUNT STORAGE #################################################

#! Returns the account storage commitment.
#!
#! Inputs:  []
#! Outputs: [STORAGE_COMMITMENT]
#!
#! Where:
#! - STORAGE_COMMITMENT is the account storage commitment.
pub proc get_account_storage_commitment
    padw
    exec.get_active_account_data_ptr add.ACCT_STORAGE_COMMITMENT_OFFSET
    mem_loadw_le
end

#! Sets the account storage commitment.
#!
#! Inputs:  [STORAGE_COMMITMENT]
#! Outputs: [STORAGE_COMMITMENT]
#!
#! Where:
#! - STORAGE_COMMITMENT is the account storage commitment.
pub proc set_account_storage_commitment
    exec.get_active_account_data_ptr add.ACCT_STORAGE_COMMITMENT_OFFSET
    mem_storew_le
end

#! Sets the dirty flag for the native account storage commitment.
#!
#! This binary flag specifies whether the storage commitment stored in the native account data is
#! outdated.
#!
#! Inputs:  [dirty_flag]
#! Outputs: []
#!
#! Where:
#! - dirty_flag is the flag indicating whether the storage commitment is outdated.
pub proc set_native_account_storage_commitment_dirty_flag
    push.NATIVE_ACCT_STORAGE_COMMITMENT_DIRTY_FLAG_PTR mem_store
    # => []
end

#! Returns the flag indicating whether the account storage commitment should be recomputed.
#!
#! This flag equals 1 if the active account is native and the storage commitment is outdated.
#!
#! Inputs:  []
#! Outputs: [should_recompute_storage_commitment]
#!
#! Where:
#! - should_recompute_storage_commitment is the flag indicating whether the storage commitment
#!   should be recomputed.
pub proc get_recompute_storage_commitment_flag
    # get the is_native_account flag
    exec.is_native_account
    # => [is_native_account]

    # get the storage commitment dirty flag
    mem_load.NATIVE_ACCT_STORAGE_COMMITMENT_DIRTY_FLAG_PTR
    # => [dirty_flag, is_native_account]

    # commitment should be recomputed only if the account is native and the commitment is outdated
    and
    # => [should_recompute_storage_commitment]
end

#! Returns the number of storage slots contained in the account storage.
#!
#! Inputs:  []
#! Outputs: [num_storage_slots]
#!
#! Where:
#! - num_storage_slots is the number of storage slots contained in the account storage.
pub proc get_num_storage_slots
    exec.get_active_account_data_ptr add.ACCT_NUM_STORAGE_SLOTS_OFFSET
    mem_load
end

#! Sets the number of storage slots contained in the account storage.
#!
#! Inputs:  [num_storage_slots]
#! Outputs: []
#!
#! Where:
#! - num_storage_slots is the number of storage slots contained in the account storage.
pub proc set_num_storage_slots
    exec.get_active_account_data_ptr add.ACCT_NUM_STORAGE_SLOTS_OFFSET
    mem_store
end

#! Returns the memory pointer to the account storage slots section.
#!
#! Inputs:  []
#! Outputs: [storage_slots_section_ptr]
#!
#! Where:
#! - storage_slots_section_ptr is the memory pointer to the account storage slots section.
pub proc get_account_active_storage_slots_section_ptr
    exec.get_active_account_data_ptr add.ACCT_ACTIVE_STORAGE_SLOTS_SECTION_OFFSET
end

#! Returns the memory pointer to the native account's storage slots section.
#!
#! Inputs:  []
#! Outputs: [storage_slots_section_ptr]
#!
#! Where:
#! - storage_slots_section_ptr is the memory pointer to the native account's storage slots section.
pub proc get_native_account_active_storage_slots_ptr
    exec.get_native_account_data_ptr add.ACCT_ACTIVE_STORAGE_SLOTS_SECTION_OFFSET
end

#! Returns the memory pointer to the initial storage slots of the native account.
#!
#! Inputs:  []
#! Outputs: [account_initial_storage_slots_ptr]
#!
#! Where:
#! - account_initial_storage_slots_ptr is the memory pointer to the initial storage slot values.
pub proc get_native_account_initial_storage_slots_ptr
  exec.get_native_account_data_ptr add.ACCT_INITIAL_STORAGE_SLOTS_SECTION_OFFSET
end

#! Returns the memory pointer to the initial storage slots of the active account.
#!
#! For the native account, this returns the pointer to the initial storage slots section.
#! For foreign accounts, this returns the regular storage slots pointer since foreign accounts
#! are read-only and their initial and current storage state always matches.
#!
#! Inputs:  []
#! Outputs: [account_initial_storage_slots_ptr]
#!
#! Where:
#! - account_initial_storage_slots_ptr is the memory pointer to the initial storage slot values.
pub proc get_account_initial_storage_slots_ptr
    # for foreign account, use the regular storage slots pointer since foreign accounts are
    # read-only and initial == current
    exec.get_account_active_storage_slots_section_ptr
    # => [account_storage_slots_ptr]

    # for native account, use the initial storage slots pointer
    exec.get_native_account_initial_storage_slots_ptr
    # => [native_account_initial_storage_slots_ptr, account_storage_slots_ptr]

    # get the flag indicating whether the active account is native
    exec.is_native_account
    # => [is_native_account, native_account_initial_storage_slots_ptr, account_storage_slots_ptr]

    # according to the is_native_account flag, return the corresponding pointer
    cdrop
    # => [account_initial_storage_slots_ptr]
end

### ACCOUNT DELTA #################################################

#! Returns the link map pointer to the storage map delta of the storage map in the given slot index.
#!
#! Inputs:  [slot_idx]
#! Outputs: [account_delta_storage_map_ptr]
#!
#! Where:
#! - account_delta_storage_map_ptr is the link map pointer to the storage map delta for the
#!   requested slot index.
pub proc get_account_delta_storage_map_ptr
    add.ACCOUNT_DELTA_STORAGE_MAP_SECTION
end

#! Initializes the storage slots holding the initial values of each slot.
#!
#! Inputs:  []
#! Outputs: []
pub proc mem_copy_native_account_initial_storage_slots
  exec.get_native_account_initial_storage_slots_ptr
  exec.get_native_account_active_storage_slots_ptr
  # => [active_storage_slots_ptr, initial_storage_slots_ptr]

  # each slot takes up two words
  exec.get_num_storage_slots mul.2
  # => [num_storage_slot_words, active_storage_slots_ptr, initial_storage_slots_ptr]

  exec.mem::memcopy_words
  # => []
end

# INPUT NOTES
# -------------------------------------------------------------------------------------------------

#! Returns the total number of input notes consumed by this transaction.
#!
#! Inputs:  []
#! Outputs: [num_input_notes]
#!
#! Where:
#! - num_input_notes is the total number of input notes consumed by this transaction.
pub proc get_num_input_notes
    mem_load.NUM_INPUT_NOTES_PTR
end

#! Sets the total number of input notes in the transaction.
#!
#! Inputs:  [num_input_notes]
#! Outputs: []
#!
#! Where:
#! - num_input_notes is the total number of input notes consumed by this transaction.
pub proc set_num_input_notes
    mem_store.NUM_INPUT_NOTES_PTR
end

#! Computes a pointer to the memory address at which the data associated with an input note with
#! index `idx` is stored.
#!
#! Inputs:  [idx]
#! Outputs: [note_ptr]
#!
#! Where:
#! - idx is the index of the input note.
#! - note_ptr is the memory address of the data segment for the input note with `idx`.
pub proc get_input_note_ptr
    push.NOTE_MEM_SIZE mul add.INPUT_NOTE_DATA_SECTION_OFFSET
end

#! Set the note id of the input note.
#!
#! Inputs:  [note_ptr, NOTE_ID]
#! Outputs: [NOTE_ID]
#!
#! Where:
#! - note_ptr is the input note's the memory address.
#! - NOTE_ID is the note's id.
pub proc set_input_note_id
    mem_storew_le
end

#! Computes a pointer to the memory address at which the nullifier associated a note with `idx` is
#! stored.
#!
#! Inputs:  [idx]
#! Outputs: [nullifier_ptr]
#!
#! Where:
#! - idx is the index of the input note.
#! - nullifier_ptr is the memory address of the nullifier for note idx.
pub proc get_input_note_nullifier_ptr
    mul.4 add.INPUT_NOTE_NULLIFIER_SECTION_PTR
end

#! Returns the nullifier of an input note with `idx`.
#!
#! Inputs:  [idx]
#! Outputs: [nullifier]
#!
#! Where:
#! - idx is the index of the input note.
#! - nullifier is the nullifier of the input note.
pub proc get_input_note_nullifier
    mul.4 padw movup.4 add.INPUT_NOTE_NULLIFIER_SECTION_PTR mem_loadw_le
end

#! Returns a pointer to the start of the input note core data segment for the note located at the
#! specified memory address.
#!
#! Inputs:  [note_ptr]
#! Outputs: [note_data_ptr]
#!
#! Where:
#! - note_ptr is the memory address at which the input note data begins.
#! - note_data_ptr is the memory address at which the input note core data begins.
pub proc get_input_note_core_ptr
    add.INPUT_NOTE_CORE_DATA_OFFSET
end

#! Returns the script root of an input note located at the specified memory address.
#!
#! Inputs:  [note_ptr]
#! Outputs: [SCRIPT_ROOT]
#!
#! Where:
#! - note_ptr is the memory address at which the input note data begins.
#! - SCRIPT_ROOT is the script root of the input note.
pub proc get_input_note_script_root
    padw
    movup.4 add.INPUT_NOTE_SCRIPT_ROOT_OFFSET
    mem_loadw_le
end

#! Returns the memory address of the script root of an input note.
#!
#! Inputs:  [note_ptr]
#! Outputs: [script_root_ptr]
#!
#! Where:
#! - note_ptr is the memory address at which the input note data begins.
#! - script_root_ptr is the memory address where script root of the input note is stored.
pub proc get_input_note_script_root_ptr
    add.INPUT_NOTE_SCRIPT_ROOT_OFFSET
end

#! Returns the inputs commitment of an input note located at the specified memory address.
#!
#! Inputs:  [note_ptr]
#! Outputs: [STORAGE_COMMITMENT]
#!
#! Where:
#! - note_ptr is the memory address at which the input note data begins.
#! - STORAGE_COMMITMENT is the inputs commitment of the input note.
pub proc get_input_note_storage_commitment
    padw
    movup.4 add.INPUT_NOTE_STORAGE_COMMITMENT_OFFSET
    mem_loadw_le
end

#! Returns the metadata of an input note located at the specified memory address.
#!
#! Inputs:  [note_ptr]
#! Outputs: [NOTE_METADATA_HEADER]
#!
#! Where:
#! - note_ptr is the memory address at which the input note data begins.
#! - NOTE_METADATA_HEADER is the metadata header of the input note.
pub proc get_input_note_metadata_header
    padw
    movup.4 add.INPUT_NOTE_METADATA_HEADER_OFFSET
    mem_loadw_le
end

#! Sets the metadata for an input note located at the specified memory address.
#!
#! Inputs:  [note_ptr, NOTE_METADATA_HEADER]
#! Outputs: [NOTE_METADATA_HEADER]
#!
#! Where:
#! - note_ptr is the memory address at which the input note data begins.
#! - NOTE_METADATA_HEADER is the metadata header of the input note.
pub proc set_input_note_metadata_header
    add.INPUT_NOTE_METADATA_HEADER_OFFSET
    mem_storew_le
end

#! Returns the attachment of an input note located at the specified memory address.
#!
#! Inputs:  [note_ptr]
#! Outputs: [NOTE_ATTACHMENT]
#!
#! Where:
#! - note_ptr is the memory address at which the input note data begins.
#! - NOTE_ATTACHMENT is the attachment of the input note.
pub proc get_input_note_attachment
    padw
    movup.4 add.INPUT_NOTE_ATTACHMENT_OFFSET
    mem_loadw_le
end

#! Sets the attachment for an input note located at the specified memory address.
#!
#! Inputs:  [note_ptr, NOTE_ATTACHMENT]
#! Outputs: [NOTE_ATTACHMENT]
#!
#! Where:
#! - note_ptr is the memory address at which the input note data begins.
#! - NOTE_ATTACHMENT is the attachment of the input note.
pub proc set_input_note_attachment
    add.INPUT_NOTE_ATTACHMENT_OFFSET
    mem_storew_le
end

#! Returns the note's args.
#!
#! Inputs:  [note_ptr]
#! Outputs: [NOTE_ARGS]
#!
#! Where:
#! - note_ptr is the start memory address of the note.
#! - NOTE_ARGS are the note's args.
pub proc get_input_note_args
    padw
    movup.4 add.INPUT_NOTE_ARGS_OFFSET
    mem_loadw_le
end

#! Sets the note args for an input note located at the specified memory address.
#!
#! Inputs:  [note_ptr, NOTE_ARGS]
#! Outputs: [NOTE_ARGS]
#!
#! Where:
#! - note_ptr is the memory address at which the input note data begins.
#! - NOTE_ARGS are optional note args of the input note.
pub proc set_input_note_args
    add.INPUT_NOTE_ARGS_OFFSET
    mem_storew_le
end

#! Returns the number of inputs of the note located at the specified memory address.
#!
#! Inputs:  [note_ptr]
#! Outputs: [num_storage_items]
#!
#! Where:
#! - note_ptr is the memory address at which the input note data begins.
#! - num_storage_items is the number of storage items of the input note.
pub proc get_input_note_num_storage_items
    add.INPUT_NOTE_NUM_STORAGE_ITEMS_OFFSET
    mem_load
end

#! Sets the number of inputs for an input note located at the specified memory address.
#!
#! Inputs:  [note_ptr, num_storage_items]
#! Outputs: []
#!
#! Where:
#! - note_ptr is the memory address at which the input note data begins.
#! - num_storage_items is the number of storage items of the input note.
pub proc set_input_note_num_storage_items
    add.INPUT_NOTE_NUM_STORAGE_ITEMS_OFFSET
    mem_store
end

#! Returns the number of assets in the input note located at the specified memory address.
#!
#! Inputs:  [note_ptr]
#! Outputs: [num_assets]
#!
#! Where:
#! - note_ptr is the memory address at which the input note data begins.
#! - num_assets is the number of assets in the input note.
pub proc get_input_note_num_assets
    add.INPUT_NOTE_NUM_ASSETS_OFFSET
    mem_load
end

#! Sets the number of assets for an input note located at the specified memory address.
#!
#! Inputs:  [note_ptr, num_assets]
#! Outputs: []
#!
#! Where:
#! - note_ptr is the memory address at which the input note data begins.
#! - num_assets is the number of assets in the input note.
pub proc set_input_note_num_assets
    add.INPUT_NOTE_NUM_ASSETS_OFFSET
    mem_store
end

#! Returns a pointer to the start of the assets segment for the input note located at the specified
#! memory address.
#!
#! Inputs:  [note_ptr]
#! Outputs: [assets_ptr]
#!
#! Where:
#! - note_ptr is the memory address at which the input note data begins.
#! - assets_ptr is the memory address at which the assets segment for the input note begins.
pub proc get_input_note_assets_ptr
    add.INPUT_NOTE_ASSETS_OFFSET
end

#! Returns the input note recipient.
#!
#! Inputs:  [note_ptr]
#! Outputs: [RECIPIENT]
#!
#! Where:
#! - note_ptr is the memory address at which the input note data begins.
#! - RECIPIENT is the commitment to the note's script, storage and the serial number.
pub proc get_input_note_recipient
    padw
    movup.4 add.INPUT_NOTE_RECIPIENT_OFFSET
    mem_loadw_le
end

#! Sets the input note's recipient.
#!
#! Inputs:  [note_ptr, RECIPIENT]
#! Outputs: [RECIPIENT]
#!
#! Where:
#! - note_ptr is the memory address at which the output note data begins.
#! - RECIPIENT is the commitment to the note's script, storage and the serial number.
pub proc set_input_note_recipient
    add.INPUT_NOTE_RECIPIENT_OFFSET
    mem_storew_le
end

#! Returns the assets commitment for the input note located at the specified memory address.
#!
#! Inputs:  [note_ptr]
#! Outputs: [ASSET_COMMITMENT]
#!
#! Where:
#! - note_ptr is the memory address at which the input note data begins.
#! - ASSET_COMMITMENT is the sequential hash of the padded assets of an input note.
pub proc get_input_note_assets_commitment
    padw
    movup.4 add.INPUT_NOTE_ASSETS_COMMITMENT_OFFSET
    mem_loadw_le
end

#! Returns the serial number for the input note located at the specified memory address.
#!
#! Inputs:  [note_ptr]
#! Outputs: [SERIAL_NUMBER]
#!
#! Where:
#! - note_ptr is the memory address at which the input note data begins.
#! - SERIAL_NUMBER is the input note's serial number.
pub proc get_input_note_serial_num
    padw
    movup.4 add.INPUT_NOTE_SERIAL_NUM_OFFSET
    mem_loadw_le
end

# OUTPUT NOTES
# -------------------------------------------------------------------------------------------------

#! Returns the offset of the output note data segment.
#!
#! Inputs:  []
#! Outputs: [offset]
#!
#! Where:
#! - offset is the offset of the output note data segment.
pub proc get_output_note_data_offset
    push.OUTPUT_NOTE_SECTION_OFFSET
end

#! Computes a pointer to the memory address at which the data associated with an output note with
#! index `i` is stored.
#!
#! Inputs:  [i]
#! Outputs: [ptr]
#!
#! Where:
#! - i is the index of the output note.
#! - ptr is the memory address of the data segment for output note i.
pub proc get_output_note_ptr
    push.NOTE_MEM_SIZE mul add.OUTPUT_NOTE_SECTION_OFFSET
end

#! Returns the output note recipient.
#!
#! Inputs:  [note_ptr]
#! Outputs: [RECIPIENT]
#!
#! Where:
#! - note_ptr is the memory address at which the output note data begins.
#! - RECIPIENT is the commitment to the note's script, storage and the serial number.
pub proc get_output_note_recipient
    padw
    movup.4 add.OUTPUT_NOTE_RECIPIENT_OFFSET
    mem_loadw_le
end

#! Sets the output note's recipient.
#!
#! Inputs:  [note_ptr, RECIPIENT]
#! Outputs: [RECIPIENT]
#!
#! Where:
#! - note_ptr is the memory address at which the output note data begins.
#! - RECIPIENT is the commitment to the note's script, storage and the serial number.
pub proc set_output_note_recipient
    add.OUTPUT_NOTE_RECIPIENT_OFFSET
    mem_storew_le
end

#! Returns the output note's metadata.
#!
#! Inputs:  [note_ptr]
#! Outputs: [METADATA_HEADER]
#!
#! Where:
#! - METADATA_HEADER is the note metadata header.
#! - note_ptr is the memory address at which the output note data begins.
pub proc get_output_note_metadata_header
    padw
    # => [0, 0, 0, 0, note_ptr]
    movup.4 add.OUTPUT_NOTE_METADATA_HEADER_OFFSET
    # => [(note_ptr + offset), 0, 0, 0, 0]
    mem_loadw_le
    # => [METADATA_HEADER]
end

#! Sets the output note's metadata header.
#!
#! Inputs:  [note_ptr, METADATA_HEADER]
#! Outputs: [METADATA_HEADER]
#!
#! Where:
#! - METADATA_HEADER is the note metadata header.
#! - note_ptr is the memory address at which the output note data begins.
pub proc set_output_note_metadata_header
    add.OUTPUT_NOTE_METADATA_HEADER_OFFSET
    mem_storew_le
end

#! Sets the output note's attachment kind and scheme in the metadata header.
#!
#! Inputs:  [note_ptr, attachment_kind_scheme]
#! Outputs: []
#!
#! Where:
#! - attachment_kind_scheme is the type information of the attachment that will be overwritten.
#! - note_ptr is the memory address at which the output note data begins.
pub proc set_output_note_attachment_kind_scheme
    add.OUTPUT_NOTE_METADATA_ATTACHMENT_KIND_SCHEME_OFFSET
    mem_store
end

#! Returns the output note's attachment.
#!
#! Inputs:  [note_ptr]
#! Outputs: [ATTACHMENT]
#!
#! Where:
#! - ATTACHMENT is the note attachment.
#! - note_ptr is the memory address at which the output note data begins.
pub proc get_output_note_attachment
    padw
    movup.4 add.OUTPUT_NOTE_ATTACHMENT_OFFSET
    mem_loadw_le
    # => [ATTACHMENT]
end

#! Sets the output note's attachment.
#!
#! Inputs:  [note_ptr, ATTACHMENT]
#! Outputs: []
#!
#! Where:
#! - ATTACHMENT is the note attachment.
#! - note_ptr is the memory address at which the output note data begins.
pub proc set_output_note_attachment
    add.OUTPUT_NOTE_ATTACHMENT_OFFSET
    mem_storew_le
    dropw
end

#! Returns the number of assets in the output note.
#!
#! Inputs:  [note_ptr]
#! Outputs: [num_assets]
#!
#! Where:
#! - note_ptr is a pointer to the memory address at which the output note is stored.
#! - num_assets is the number of assets in the output note.
pub proc get_output_note_num_assets
    add.OUTPUT_NOTE_NUM_ASSETS_OFFSET mem_load
end

#! Sets the number of assets in the output note.
#!
#! Inputs:  [note_ptr, num_assets]
#! Outputs: []
#!
#! Where:
#! - note_ptr is the memory address at which the output note data begins.
#! - num_assets is the number of assets in the output note.
#!
#! Panics if:
#! - the number of assets exceeds the maximum allowed number of assets per note.
pub proc set_output_note_num_assets
    add.OUTPUT_NOTE_NUM_ASSETS_OFFSET
    # => [note_ptr + offset, num_assets]

    # check note number of assets limit
    dup.1 push.MAX_ASSETS_PER_NOTE lt assert.err=ERR_NOTE_NUM_OF_ASSETS_EXCEED_LIMIT

    mem_store
end

#! Increments the number of assets in the output note by 1.
#!
#! Inputs:  [note_ptr]
#! Outputs: []
#!
#! Where:
#! - note_ptr is the memory address at which the output note data begins.
#!
#! Panics if:
#! - the number of assets exceeds the maximum allowed number of assets per note.
pub proc increment_output_note_num_assets
    dup exec.get_output_note_num_assets add.1
    # => [num_assets + 1, note_ptr]

    swap exec.set_output_note_num_assets
    # => []
end

#! Returns the dirty flag for the assets commitment.
#!
#! This binary flag specifies whether the assets commitment stored in the specified note is
#! outdated.
#!
#! Inputs:  [output_note_data_ptr]
#! Outputs: [dirty_flag]
#!
#! Where:
#! - output_note_data_ptr is the memory address at which the output note data begins.
#! - dirty_flag is the flag indicating whether the assets commitment is outdated.
pub proc get_output_note_dirty_flag
    add.OUTPUT_NOTE_DIRTY_FLAG_OFFSET mem_load
end

#! Sets the dirty flag for the assets commitment.
#!
#! This binary flag specifies whether the assets commitment stored in the specified note is
#! outdated.
#!
#! Inputs:  [output_note_data_ptr, dirty_flag]
#! Outputs: []
#!
#! Where:
#! - output_note_data_ptr is the memory address at which the output note data begins.
#! - dirty_flag is the flag indicating whether the assets commitment is outdated.
pub proc set_output_note_dirty_flag
    add.OUTPUT_NOTE_DIRTY_FLAG_OFFSET mem_store
end

#! Returns a pointer to the output note asset data.
#!
#! Inputs:  [output_note_data_ptr]
#! Outputs: [asset_data_ptr]
#!
#! Where:
#! - output_note_data_ptr is the memory address at which the output note data begins.
#! - asset_data_ptr is the memory address at which the output note asset data begins.
pub proc get_output_note_asset_data_ptr
    add.OUTPUT_NOTE_ASSETS_OFFSET
end

#! Returns the assets commitment for the output note located at the specified memory address.
#!
#! Inputs:  [output_note_data_ptr]
#! Outputs: [ASSETS_COMMITMENT]
#!
#! Where:
#! - output_note_data_ptr is the memory address at which the output note data begins.
#! - ASSETS_COMMITMENT is the sequential hash of the padded assets of an output note.
pub proc get_output_note_assets_commitment
    padw
    movup.4 add.OUTPUT_NOTE_ASSETS_COMMITMENT_OFFSET
    mem_loadw_le
end

#! Sets the output note assets commitment for the output note located at the specified memory
#! address.
#!
#! Inputs:  [output_note_data_ptr, ASSETS_COMMITMENT]
#! Outputs: [ASSETS_COMMITMENT]
#!
#! Where:
#! - output_note_data_ptr is the memory address at which the output note data begins.
#! - ASSETS_COMMITMENT is the sequential hash of the padded assets of an output note.
pub proc set_output_note_assets_commitment
    add.OUTPUT_NOTE_ASSETS_COMMITMENT_OFFSET
    mem_storew_le
end

# KERNEL DATA
# -------------------------------------------------------------------------------------------------

#! Sets the number of the procedures of the selected kernel.
#!
#! Inputs:  [num_kernel_procedures]
#! Outputs: []
#!
#! Where:
#! - num_kernel_procedures is the number of the procedures of the selected kernel.
pub proc set_num_kernel_procedures
    mem_store.NUM_KERNEL_PROCEDURES_PTR
end

#! Returns the number of the procedures of the selected kernel.
#!
#! Inputs:  []
#! Outputs: [num_kernel_procedures]
#!
#! Where:
#! - num_kernel_procedures is the number of the procedures of the selected kernel.
pub proc get_num_kernel_procedures
    mem_load.NUM_KERNEL_PROCEDURES_PTR
end

#! Returns a pointer to the memory where hashes of the kernel procedures are stored.
#!
#! Inputs:  []
#! Outputs: [kernel_procedures_ptr]
#!
#! Where:
#! - kernel_procedures_ptr is the memory address where the hashes of the kernel procedures are
#!   stored.
pub proc get_kernel_procedures_ptr
    push.KERNEL_PROCEDURES_PTR
end

# LINK MAP
# -------------------------------------------------------------------------------------------------

#! Returns the link map memory start ptr constant.
#!
#! Inputs:  []
#! Outputs: [start_ptr]
pub proc get_link_map_region_start_ptr
    push.LINK_MAP_REGION_START_PTR
end

#! Returns the link map memory end ptr constant.
#!
#! Inputs:  []
#! Outputs: [end_ptr]
pub proc get_link_map_region_end_ptr
    push.LINK_MAP_REGION_END_PTR
end

#! Returns the link map entry size constant.
#!
#! Inputs:  []
#! Outputs: [entry_size]
pub proc get_link_map_entry_size
    push.LINK_MAP_ENTRY_SIZE
end

#! Returns the next pointer to an empty link map entry.
#!
#! Inputs:  []
#! Outputs: [entry_ptr]
#!
#! Panics if:
#! - the allocation exceeds the maximum possible number of link map entries.
pub proc link_map_malloc
    # retrieve the current memory size
    mem_load.LINK_MAP_USED_MEMORY_SIZE dup
    # => [current_mem_size, current_mem_size]

    # store next offset
    add.LINK_MAP_ENTRY_SIZE
    # => [next_mem_size, current_mem_size]

    mem_store.LINK_MAP_USED_MEMORY_SIZE
    # => [current_mem_size]

    add.LINK_MAP_REGION_START_PTR
    # => [entry_ptr]

    # if entry_ptr is the end_ptr the entry would be allocated in the next memory region so
    # we must abort.
    # we can use neq because of how the end ptr is chosen. See its docs for details.
    dup neq.LINK_MAP_REGION_END_PTR assert.err=ERR_LINK_MAP_MAX_ENTRIES_EXCEEDED
    # => [entry_ptr]
end