snarkvm-synthesizer-program 4.6.1

Program for a decentralized virtual machine
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
// Copyright (c) 2019-2026 Provable Inc.
// This file is part of the snarkVM library.

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at:

// http://www.apache.org/licenses/LICENSE-2.0

// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#![forbid(unsafe_code)]
#![allow(clippy::too_many_arguments)]
#![warn(clippy::cast_possible_truncation)]

extern crate snarkvm_circuit as circuit;
extern crate snarkvm_console as console;

pub type Program<N> = crate::ProgramCore<N>;
pub type Function<N> = crate::FunctionCore<N>;
pub type Finalize<N> = crate::FinalizeCore<N>;
pub type Closure<N> = crate::ClosureCore<N>;
pub type Constructor<N> = crate::ConstructorCore<N>;

mod closure;
pub use closure::*;

mod constructor;
pub use constructor::*;

pub mod finalize;
pub use finalize::*;

mod function;
pub use function::*;

mod import;
pub use import::*;

pub mod logic;
pub use logic::*;

mod mapping;
pub use mapping::*;

mod traits;
pub use traits::*;

mod bytes;
mod parse;
mod serialize;
mod to_checksum;

use console::{
    network::{
        ConsensusVersion,
        consensus_config_value,
        prelude::{
            Debug,
            Deserialize,
            Deserializer,
            Display,
            Err,
            Error,
            ErrorKind,
            Formatter,
            FromBytes,
            FromBytesDeserializer,
            FromStr,
            IoResult,
            Itertools,
            Network,
            Parser,
            ParserResult,
            Read,
            Result,
            Sanitizer,
            Serialize,
            Serializer,
            ToBytes,
            ToBytesSerializer,
            TypeName,
            Write,
            anyhow,
            bail,
            de,
            ensure,
            error,
            fmt,
            make_error,
            many0,
            many1,
            map,
            map_res,
            tag,
            take,
        },
    },
    program::{
        EntryType,
        FinalizeType,
        Identifier,
        Locator,
        PlaintextType,
        ProgramID,
        RecordType,
        RegisterType,
        StructType,
        ValueType,
    },
    types::U8,
};
use snarkvm_utilities::{cfg_find_map, cfg_iter};

use indexmap::{IndexMap, IndexSet};
use std::collections::BTreeSet;
use tiny_keccak::{Hasher, Sha3 as TinySha3};

#[derive(Copy, Clone, PartialEq, Eq, Hash)]
enum ProgramLabel<N: Network> {
    /// A program constructor.
    Constructor,
    /// A named component.
    Identifier(Identifier<N>),
}

#[cfg(not(feature = "serial"))]
use rayon::prelude::*;

#[derive(Copy, Clone, PartialEq, Eq, Hash)]
enum ProgramDefinition {
    /// A program constructor.
    Constructor,
    /// A program mapping.
    Mapping,
    /// A program struct.
    Struct,
    /// A program record.
    Record,
    /// A program closure.
    Closure,
    /// A program function.
    Function,
}

#[derive(Clone)]
pub struct ProgramCore<N: Network> {
    /// The ID of the program.
    id: ProgramID<N>,
    /// A map of the declared imports for the program.
    imports: IndexMap<ProgramID<N>, Import<N>>,
    /// A map of program labels to their program definitions.
    components: IndexMap<ProgramLabel<N>, ProgramDefinition>,
    /// An optional constructor for the program.
    constructor: Option<ConstructorCore<N>>,
    /// A map of the declared mappings for the program.
    mappings: IndexMap<Identifier<N>, Mapping<N>>,
    /// A map of the declared structs for the program.
    structs: IndexMap<Identifier<N>, StructType<N>>,
    /// A map of the declared record types for the program.
    records: IndexMap<Identifier<N>, RecordType<N>>,
    /// A map of the declared closures for the program.
    closures: IndexMap<Identifier<N>, ClosureCore<N>>,
    /// A map of the declared functions for the program.
    functions: IndexMap<Identifier<N>, FunctionCore<N>>,
}

impl<N: Network> PartialEq for ProgramCore<N> {
    /// Compares two programs for equality, verifying that the components are in the same order.
    /// The order of the components must match to ensure that deployment tree is well-formed.
    fn eq(&self, other: &Self) -> bool {
        // Check that the number of components is the same.
        if self.components.len() != other.components.len() {
            return false;
        }
        // Check that the components match in order.
        for (left, right) in self.components.iter().zip_eq(other.components.iter()) {
            if left != right {
                return false;
            }
        }
        // Check that the remaining fields match.
        self.id == other.id
            && self.imports == other.imports
            && self.mappings == other.mappings
            && self.structs == other.structs
            && self.records == other.records
            && self.closures == other.closures
            && self.functions == other.functions
    }
}

impl<N: Network> Eq for ProgramCore<N> {}

impl<N: Network> ProgramCore<N> {
    /// A list of reserved keywords for Aleo programs, enforced at the parser level.
    // New keywords should be enforced through `RESTRICTED_KEYWORDS` instead, if possible.
    // Adding keywords to this list will require a backwards-compatible versioning for programs.
    #[rustfmt::skip]
    pub const KEYWORDS: &'static [&'static str] = &[
        // Mode
        "const",
        "constant",
        "public",
        "private",
        // Literals
        "address",
        "boolean",
        "field",
        "group",
        "i8",
        "i16",
        "i32",
        "i64",
        "i128",
        "u8",
        "u16",
        "u32",
        "u64",
        "u128",
        "scalar",
        "signature",
        "string",
        // Boolean
        "true",
        "false",
        // Statements
        "input",
        "output",
        "as",
        "into",
        // Record
        "record",
        "owner",
        // Program
        "transition",
        "import",
        "function",
        "struct",
        "closure",
        "program",
        "aleo",
        "self",
        "storage",
        "mapping",
        "key",
        "value",
        "async",
        "finalize",
        // Reserved (catch all)
        "global",
        "block",
        "return",
        "break",
        "assert",
        "continue",
        "let",
        "if",
        "else",
        "while",
        "for",
        "switch",
        "case",
        "default",
        "match",
        "enum",
        "struct",
        "union",
        "trait",
        "impl",
        "type",
        "future",
    ];
    /// A list of restricted keywords for Aleo programs, enforced at the VM-level for program hygiene.
    /// Each entry is a tuple of the consensus version and a list of keywords.
    /// If the current consensus version is greater than or equal to the specified version,
    /// the keywords in the list should be restricted.
    #[rustfmt::skip]
    pub const RESTRICTED_KEYWORDS: &'static [(ConsensusVersion, &'static [&'static str])] = &[
        (ConsensusVersion::V6, &["constructor"]),
        (ConsensusVersion::V14, &["dynamic", "identifier"]),
    ];

    /// Initializes an empty program.
    #[inline]
    pub fn new(id: ProgramID<N>) -> Result<Self> {
        // Ensure the program name is valid.
        ensure!(!Self::is_reserved_keyword(id.name()), "Program name is invalid: {}", id.name());

        Ok(Self {
            id,
            imports: IndexMap::new(),
            constructor: None,
            components: IndexMap::new(),
            mappings: IndexMap::new(),
            structs: IndexMap::new(),
            records: IndexMap::new(),
            closures: IndexMap::new(),
            functions: IndexMap::new(),
        })
    }

    /// Initializes the credits program.
    #[inline]
    pub fn credits() -> Result<Self> {
        Self::from_str(include_str!("./resources/credits.aleo"))
    }

    /// Returns the ID of the program.
    pub const fn id(&self) -> &ProgramID<N> {
        &self.id
    }

    /// Returns the imports in the program.
    pub const fn imports(&self) -> &IndexMap<ProgramID<N>, Import<N>> {
        &self.imports
    }

    /// Returns the constructor for the program.
    pub const fn constructor(&self) -> Option<&ConstructorCore<N>> {
        self.constructor.as_ref()
    }

    /// Returns the mappings in the program.
    pub const fn mappings(&self) -> &IndexMap<Identifier<N>, Mapping<N>> {
        &self.mappings
    }

    /// Returns the structs in the program.
    pub const fn structs(&self) -> &IndexMap<Identifier<N>, StructType<N>> {
        &self.structs
    }

    /// Returns the records in the program.
    pub const fn records(&self) -> &IndexMap<Identifier<N>, RecordType<N>> {
        &self.records
    }

    /// Returns the closures in the program.
    pub const fn closures(&self) -> &IndexMap<Identifier<N>, ClosureCore<N>> {
        &self.closures
    }

    /// Returns the functions in the program.
    pub const fn functions(&self) -> &IndexMap<Identifier<N>, FunctionCore<N>> {
        &self.functions
    }

    /// Returns `true` if the program contains an import with the given program ID.
    pub fn contains_import(&self, id: &ProgramID<N>) -> bool {
        self.imports.contains_key(id)
    }

    /// Returns `true` if the program contains a constructor.
    pub const fn contains_constructor(&self) -> bool {
        self.constructor.is_some()
    }

    /// Returns `true` if the program contains a mapping with the given name.
    pub fn contains_mapping(&self, name: &Identifier<N>) -> bool {
        self.mappings.contains_key(name)
    }

    /// Returns `true` if the program contains a struct with the given name.
    pub fn contains_struct(&self, name: &Identifier<N>) -> bool {
        self.structs.contains_key(name)
    }

    /// Returns `true` if the program contains a record with the given name.
    pub fn contains_record(&self, name: &Identifier<N>) -> bool {
        self.records.contains_key(name)
    }

    /// Returns `true` if the program contains a closure with the given name.
    pub fn contains_closure(&self, name: &Identifier<N>) -> bool {
        self.closures.contains_key(name)
    }

    /// Returns `true` if the program contains a function with the given name.
    pub fn contains_function(&self, name: &Identifier<N>) -> bool {
        self.functions.contains_key(name)
    }

    /// Returns the mapping with the given name.
    pub fn get_mapping(&self, name: &Identifier<N>) -> Result<Mapping<N>> {
        // Attempt to retrieve the mapping.
        let mapping = self.mappings.get(name).cloned().ok_or_else(|| anyhow!("Mapping '{name}' is not defined."))?;
        // Ensure the mapping name matches.
        ensure!(mapping.name() == name, "Expected mapping '{name}', but found mapping '{}'", mapping.name());
        // Return the mapping.
        Ok(mapping)
    }

    /// Returns the struct with the given name.
    pub fn get_struct(&self, name: &Identifier<N>) -> Result<&StructType<N>> {
        // Attempt to retrieve the struct.
        let struct_ = self.structs.get(name).ok_or_else(|| anyhow!("Struct '{name}' is not defined."))?;
        // Ensure the struct name matches.
        ensure!(struct_.name() == name, "Expected struct '{name}', but found struct '{}'", struct_.name());
        // Ensure the struct contains members.
        ensure!(!struct_.members().is_empty(), "Struct '{name}' is missing members.");
        // Return the struct.
        Ok(struct_)
    }

    /// Returns the record with the given name.
    pub fn get_record(&self, name: &Identifier<N>) -> Result<&RecordType<N>> {
        // Attempt to retrieve the record.
        let record = self.records.get(name).ok_or_else(|| anyhow!("Record '{name}' is not defined."))?;
        // Ensure the record name matches.
        ensure!(record.name() == name, "Expected record '{name}', but found record '{}'", record.name());
        // Return the record.
        Ok(record)
    }

    /// Returns the closure with the given name.
    pub fn get_closure(&self, name: &Identifier<N>) -> Result<ClosureCore<N>> {
        // Attempt to retrieve the closure.
        let closure = self.closures.get(name).cloned().ok_or_else(|| anyhow!("Closure '{name}' is not defined."))?;
        // Ensure the closure name matches.
        ensure!(closure.name() == name, "Expected closure '{name}', but found closure '{}'", closure.name());
        // Ensure there are input statements in the closure.
        ensure!(!closure.inputs().is_empty(), "Cannot evaluate a closure without input statements");
        // Ensure the number of inputs is within the allowed range.
        ensure!(closure.inputs().len() <= N::MAX_INPUTS, "Closure exceeds maximum number of inputs");
        // Ensure there are instructions in the closure.
        ensure!(!closure.instructions().is_empty(), "Cannot evaluate a closure without instructions");
        // Ensure the number of outputs is within the allowed range.
        ensure!(closure.outputs().len() <= N::MAX_OUTPUTS, "Closure exceeds maximum number of outputs");
        // Return the closure.
        Ok(closure)
    }

    /// Returns the function with the given name.
    pub fn get_function(&self, name: &Identifier<N>) -> Result<FunctionCore<N>> {
        self.get_function_ref(name).cloned()
    }

    /// Returns a reference to the function with the given name.
    pub fn get_function_ref(&self, name: &Identifier<N>) -> Result<&FunctionCore<N>> {
        // Attempt to retrieve the function.
        let function = self.functions.get(name).ok_or(anyhow!("Function '{}/{name}' is not defined.", self.id))?;
        // Ensure the function name matches.
        ensure!(function.name() == name, "Expected function '{name}', but found function '{}'", function.name());
        // Ensure the number of inputs is within the allowed range.
        ensure!(function.inputs().len() <= N::MAX_INPUTS, "Function exceeds maximum number of inputs");
        // Ensure the number of instructions is within the allowed range.
        ensure!(function.instructions().len() <= N::MAX_INSTRUCTIONS, "Function exceeds maximum instructions");
        // Ensure the number of outputs is within the allowed range.
        ensure!(function.outputs().len() <= N::MAX_OUTPUTS, "Function exceeds maximum number of outputs");
        // Return the function.
        Ok(function)
    }

    /// Adds a new import statement to the program.
    ///
    /// # Errors
    /// This method will halt if the imported program was previously added.
    #[inline]
    fn add_import(&mut self, import: Import<N>) -> Result<()> {
        // Retrieve the imported program name.
        let import_name = *import.name();

        // Ensure that the number of imports is within the allowed range.
        ensure!(self.imports.len() < N::MAX_IMPORTS, "Program exceeds the maximum number of imports");

        // Ensure the import name is new.
        ensure!(self.is_unique_name(&import_name), "'{import_name}' is already in use.");
        // Ensure the import name is not a reserved opcode.
        ensure!(!Self::is_reserved_opcode(&import_name.to_string()), "'{import_name}' is a reserved opcode.");
        // Ensure the import name is not a reserved keyword.
        ensure!(!Self::is_reserved_keyword(&import_name), "'{import_name}' is a reserved keyword.");

        // Ensure the import is new.
        ensure!(
            !self.imports.contains_key(import.program_id()),
            "Import '{}' is already defined.",
            import.program_id()
        );

        // Add the import statement to the program.
        if self.imports.insert(*import.program_id(), import.clone()).is_some() {
            bail!("'{}' already exists in the program.", import.program_id())
        }
        Ok(())
    }

    /// Adds a constructor to the program.
    ///
    /// # Errors
    /// This method will halt if a constructor was previously added.
    /// This method will halt if a constructor exceeds the maximum number of commands.
    fn add_constructor(&mut self, constructor: ConstructorCore<N>) -> Result<()> {
        // Ensure the program does not already have a constructor.
        ensure!(self.constructor.is_none(), "Program already has a constructor.");
        // Ensure the number of commands is within the allowed range.
        ensure!(!constructor.commands().is_empty(), "Constructor must have at least one command");
        ensure!(constructor.commands().len() <= N::MAX_COMMANDS, "Constructor exceeds maximum number of commands");
        // Add the constructor to the components.
        if self.components.insert(ProgramLabel::Constructor, ProgramDefinition::Constructor).is_some() {
            bail!("Constructor already exists in the program.")
        }
        // Add the constructor to the program.
        self.constructor = Some(constructor);
        Ok(())
    }

    /// Adds a new mapping to the program.
    ///
    /// # Errors
    /// This method will halt if the mapping name is already in use.
    /// This method will halt if the mapping name is a reserved opcode or keyword.
    #[inline]
    fn add_mapping(&mut self, mapping: Mapping<N>) -> Result<()> {
        // Retrieve the mapping name.
        let mapping_name = *mapping.name();

        // Ensure the program has not exceeded the maximum number of mappings.
        ensure!(self.mappings.len() < N::MAX_MAPPINGS, "Program exceeds the maximum number of mappings");

        // Ensure the mapping name is new.
        ensure!(self.is_unique_name(&mapping_name), "'{mapping_name}' is already in use.");
        // Ensure the mapping name is not a reserved keyword.
        ensure!(!Self::is_reserved_keyword(&mapping_name), "'{mapping_name}' is a reserved keyword.");
        // Ensure the mapping name is not a reserved opcode.
        ensure!(!Self::is_reserved_opcode(&mapping_name.to_string()), "'{mapping_name}' is a reserved opcode.");

        // Add the mapping name to the identifiers.
        if self.components.insert(ProgramLabel::Identifier(mapping_name), ProgramDefinition::Mapping).is_some() {
            bail!("'{mapping_name}' already exists in the program.")
        }
        // Add the mapping to the program.
        if self.mappings.insert(mapping_name, mapping).is_some() {
            bail!("'{mapping_name}' already exists in the program.")
        }
        Ok(())
    }

    /// Adds a new struct to the program.
    ///
    /// # Errors
    /// This method will halt if the struct was previously added.
    /// This method will halt if the struct name is already in use in the program.
    /// This method will halt if the struct name is a reserved opcode or keyword.
    /// This method will halt if any structs in the struct's members are not already defined.
    #[inline]
    fn add_struct(&mut self, struct_: StructType<N>) -> Result<()> {
        // Retrieve the struct name.
        let struct_name = *struct_.name();

        // Ensure the program has not exceeded the maximum number of structs.
        ensure!(self.structs.len() < N::MAX_STRUCTS, "Program exceeds the maximum number of structs.");

        // Ensure the struct name is new.
        ensure!(self.is_unique_name(&struct_name), "'{struct_name}' is already in use.");
        // Ensure the struct name is not a reserved opcode.
        ensure!(!Self::is_reserved_opcode(&struct_name.to_string()), "'{struct_name}' is a reserved opcode.");
        // Ensure the struct name is not a reserved keyword.
        ensure!(!Self::is_reserved_keyword(&struct_name), "'{struct_name}' is a reserved keyword.");

        // Ensure the struct contains members.
        ensure!(!struct_.members().is_empty(), "Struct '{struct_name}' is missing members.");

        // Ensure all struct members are well-formed.
        // Note: This design ensures cyclic references are not possible.
        for (identifier, plaintext_type) in struct_.members() {
            // Ensure the member name is not a reserved keyword.
            ensure!(!Self::is_reserved_keyword(identifier), "'{identifier}' is a reserved keyword.");
            // Ensure the member type is already defined in the program.
            match plaintext_type {
                PlaintextType::Literal(_) => continue,
                PlaintextType::Struct(member_identifier) => {
                    // Ensure the member struct name exists in the program.
                    if !self.structs.contains_key(member_identifier) {
                        bail!("'{member_identifier}' in struct '{struct_name}' is not defined.")
                    }
                }
                PlaintextType::ExternalStruct(locator) => {
                    if !self.imports.contains_key(locator.program_id()) {
                        bail!(
                            "External program {} referenced in struct '{struct_name}' does not exist",
                            locator.program_id()
                        );
                    }
                }
                PlaintextType::Array(array_type) => {
                    match array_type.base_element_type() {
                        PlaintextType::Struct(struct_name) =>
                        // Ensure the member struct name exists in the program.
                        {
                            if !self.structs.contains_key(struct_name) {
                                bail!("'{struct_name}' in array '{array_type}' is not defined.")
                            }
                        }
                        PlaintextType::ExternalStruct(locator) => {
                            if !self.imports.contains_key(locator.program_id()) {
                                bail!(
                                    "External program {} in array '{array_type}' does not exist",
                                    locator.program_id()
                                );
                            }
                        }
                        PlaintextType::Array(..) | PlaintextType::Literal(..) => {}
                    }
                }
            }
        }

        // Add the struct name to the identifiers.
        if self.components.insert(ProgramLabel::Identifier(struct_name), ProgramDefinition::Struct).is_some() {
            bail!("'{struct_name}' already exists in the program.")
        }
        // Add the struct to the program.
        if self.structs.insert(struct_name, struct_).is_some() {
            bail!("'{struct_name}' already exists in the program.")
        }
        Ok(())
    }

    /// Adds a new record to the program.
    ///
    /// # Errors
    /// This method will halt if the record was previously added.
    /// This method will halt if the record name is already in use in the program.
    /// This method will halt if the record name is a reserved opcode or keyword.
    /// This method will halt if any records in the record's members are not already defined.
    #[inline]
    fn add_record(&mut self, record: RecordType<N>) -> Result<()> {
        // Retrieve the record name.
        let record_name = *record.name();

        // Ensure the program has not exceeded the maximum number of records.
        ensure!(self.records.len() < N::MAX_RECORDS, "Program exceeds the maximum number of records.");

        // Ensure the record name is new.
        ensure!(self.is_unique_name(&record_name), "'{record_name}' is already in use.");
        // Ensure the record name is not a reserved opcode.
        ensure!(!Self::is_reserved_opcode(&record_name.to_string()), "'{record_name}' is a reserved opcode.");
        // Ensure the record name is not a reserved keyword.
        ensure!(!Self::is_reserved_keyword(&record_name), "'{record_name}' is a reserved keyword.");

        // Ensure all record entries are well-formed.
        // Note: This design ensures cyclic references are not possible.
        for (identifier, entry_type) in record.entries() {
            // Ensure the member name is not a reserved keyword.
            ensure!(!Self::is_reserved_keyword(identifier), "'{identifier}' is a reserved keyword.");
            // Ensure the member type is already defined in the program.
            match entry_type.plaintext_type() {
                PlaintextType::Literal(_) => continue,
                PlaintextType::Struct(identifier) => {
                    if !self.structs.contains_key(identifier) {
                        bail!("Struct '{identifier}' in record '{record_name}' is not defined.")
                    }
                }
                PlaintextType::ExternalStruct(locator) => {
                    if !self.imports.contains_key(locator.program_id()) {
                        bail!(
                            "External program {} referenced in record '{record_name}' does not exist",
                            locator.program_id()
                        );
                    }
                }
                PlaintextType::Array(array_type) => {
                    match array_type.base_element_type() {
                        PlaintextType::Struct(struct_name) =>
                        // Ensure the member struct name exists in the program.
                        {
                            if !self.structs.contains_key(struct_name) {
                                bail!("'{struct_name}' in array '{array_type}' is not defined.")
                            }
                        }
                        PlaintextType::ExternalStruct(locator) => {
                            if !self.imports.contains_key(locator.program_id()) {
                                bail!(
                                    "External program {} in array '{array_type}' does not exist",
                                    locator.program_id()
                                );
                            }
                        }
                        PlaintextType::Array(..) | PlaintextType::Literal(..) => {}
                    }
                }
            }
        }

        // Add the record name to the identifiers.
        if self.components.insert(ProgramLabel::Identifier(record_name), ProgramDefinition::Record).is_some() {
            bail!("'{record_name}' already exists in the program.")
        }
        // Add the record to the program.
        if self.records.insert(record_name, record).is_some() {
            bail!("'{record_name}' already exists in the program.")
        }
        Ok(())
    }

    /// Adds a new closure to the program.
    ///
    /// # Errors
    /// This method will halt if the closure was previously added.
    /// This method will halt if the closure name is already in use in the program.
    /// This method will halt if the closure name is a reserved opcode or keyword.
    /// This method will halt if any registers are assigned more than once.
    /// This method will halt if the registers are not incrementing monotonically.
    /// This method will halt if an input type references a non-existent definition.
    /// This method will halt if an operand register does not already exist in memory.
    /// This method will halt if a destination register already exists in memory.
    /// This method will halt if an output register does not already exist.
    /// This method will halt if an output type references a non-existent definition.
    #[inline]
    fn add_closure(&mut self, closure: ClosureCore<N>) -> Result<()> {
        // Retrieve the closure name.
        let closure_name = *closure.name();

        // Ensure the program has not exceeded the maximum number of closures.
        ensure!(self.closures.len() < N::MAX_CLOSURES, "Program exceeds the maximum number of closures.");

        // Ensure the closure name is new.
        ensure!(self.is_unique_name(&closure_name), "'{closure_name}' is already in use.");
        // Ensure the closure name is not a reserved opcode.
        ensure!(!Self::is_reserved_opcode(&closure_name.to_string()), "'{closure_name}' is a reserved opcode.");
        // Ensure the closure name is not a reserved keyword.
        ensure!(!Self::is_reserved_keyword(&closure_name), "'{closure_name}' is a reserved keyword.");

        // Ensure there are input statements in the closure.
        ensure!(!closure.inputs().is_empty(), "Cannot evaluate a closure without input statements");
        // Ensure the number of inputs is within the allowed range.
        ensure!(closure.inputs().len() <= N::MAX_INPUTS, "Closure exceeds maximum number of inputs");
        // Ensure there are instructions in the closure.
        ensure!(!closure.instructions().is_empty(), "Cannot evaluate a closure without instructions");
        // Ensure the number of outputs is within the allowed range.
        ensure!(closure.outputs().len() <= N::MAX_OUTPUTS, "Closure exceeds maximum number of outputs");

        // Add the function name to the identifiers.
        if self.components.insert(ProgramLabel::Identifier(closure_name), ProgramDefinition::Closure).is_some() {
            bail!("'{closure_name}' already exists in the program.")
        }
        // Add the closure to the program.
        if self.closures.insert(closure_name, closure).is_some() {
            bail!("'{closure_name}' already exists in the program.")
        }
        Ok(())
    }

    /// Adds a new function to the program.
    ///
    /// # Errors
    /// This method will halt if the function was previously added.
    /// This method will halt if the function name is already in use in the program.
    /// This method will halt if the function name is a reserved opcode or keyword.
    /// This method will halt if any registers are assigned more than once.
    /// This method will halt if the registers are not incrementing monotonically.
    /// This method will halt if an input type references a non-existent definition.
    /// This method will halt if an operand register does not already exist in memory.
    /// This method will halt if a destination register already exists in memory.
    /// This method will halt if an output register does not already exist.
    /// This method will halt if an output type references a non-existent definition.
    #[inline]
    fn add_function(&mut self, function: FunctionCore<N>) -> Result<()> {
        // Retrieve the function name.
        let function_name = *function.name();

        // Ensure the program has not exceeded the maximum number of functions.
        ensure!(self.functions.len() < N::MAX_FUNCTIONS, "Program exceeds the maximum number of functions");

        // Ensure the function name is new.
        ensure!(self.is_unique_name(&function_name), "'{function_name}' is already in use.");
        // Ensure the function name is not a reserved opcode.
        ensure!(!Self::is_reserved_opcode(&function_name.to_string()), "'{function_name}' is a reserved opcode.");
        // Ensure the function name is not a reserved keyword.
        ensure!(!Self::is_reserved_keyword(&function_name), "'{function_name}' is a reserved keyword.");

        // Ensure the number of inputs is within the allowed range.
        ensure!(function.inputs().len() <= N::MAX_INPUTS, "Function exceeds maximum number of inputs");
        // Ensure the number of instructions is within the allowed range.
        ensure!(function.instructions().len() <= N::MAX_INSTRUCTIONS, "Function exceeds maximum instructions");
        // Ensure the number of outputs is within the allowed range.
        ensure!(function.outputs().len() <= N::MAX_OUTPUTS, "Function exceeds maximum number of outputs");

        // Add the function name to the identifiers.
        if self.components.insert(ProgramLabel::Identifier(function_name), ProgramDefinition::Function).is_some() {
            bail!("'{function_name}' already exists in the program.")
        }
        // Add the function to the program.
        if self.functions.insert(function_name, function).is_some() {
            bail!("'{function_name}' already exists in the program.")
        }
        Ok(())
    }

    /// Returns `true` if the given name does not already exist in the program.
    fn is_unique_name(&self, name: &Identifier<N>) -> bool {
        !self.components.contains_key(&ProgramLabel::Identifier(*name))
    }

    /// Returns `true` if the given name is a reserved opcode.
    pub fn is_reserved_opcode(name: &str) -> bool {
        Instruction::<N>::is_reserved_opcode(name)
    }

    /// Returns `true` if the given name uses a reserved keyword.
    pub fn is_reserved_keyword(name: &Identifier<N>) -> bool {
        // Convert the given name to a string.
        let name = name.to_string();
        // Check if the name is a keyword.
        Self::KEYWORDS.iter().any(|keyword| *keyword == name)
    }

    /// Returns an iterator over the restricted keywords for the given consensus version.
    pub fn restricted_keywords_for_consensus_version(
        consensus_version: ConsensusVersion,
    ) -> impl Iterator<Item = &'static str> {
        Self::RESTRICTED_KEYWORDS
            .iter()
            .filter(move |(version, _)| *version <= consensus_version)
            .flat_map(|(_, keywords)| *keywords)
            .copied()
    }

    /// Checks a program for restricted keywords for the given consensus version.
    /// Returns an error if any restricted keywords are found.
    /// Note: Restrictions are not enforced on the import names in case they were deployed before the restrictions were added.
    pub fn check_restricted_keywords_for_consensus_version(&self, consensus_version: ConsensusVersion) -> Result<()> {
        // Get all keywords that are restricted for the consensus version.
        let keywords =
            Program::<N>::restricted_keywords_for_consensus_version(consensus_version).collect::<IndexSet<_>>();
        // Check if the program name is a restricted keywords.
        let program_name = self.id().name().to_string();
        if keywords.contains(&program_name.as_str()) {
            bail!("Program name '{program_name}' is a restricted keyword for the current consensus version")
        }
        // Check that all top-level program components are not restricted keywords.
        for component in self.components.keys() {
            match component {
                ProgramLabel::Identifier(identifier) => {
                    if keywords.contains(identifier.to_string().as_str()) {
                        bail!(
                            "Program component '{identifier}' is a restricted keyword for the current consensus version"
                        )
                    }
                }
                ProgramLabel::Constructor => continue,
            }
        }
        // Check that all record entry names are not restricted keywords.
        for record_type in self.records().values() {
            for entry_name in record_type.entries().keys() {
                if keywords.contains(entry_name.to_string().as_str()) {
                    bail!("Record entry '{entry_name}' is a restricted keyword for the current consensus version")
                }
            }
        }
        // Check that all struct member names are not restricted keywords.
        for struct_type in self.structs().values() {
            for member_name in struct_type.members().keys() {
                if keywords.contains(member_name.to_string().as_str()) {
                    bail!("Struct member '{member_name}' is a restricted keyword for the current consensus version")
                }
            }
        }
        // Check that all `finalize` positions.
        // Note: It is sufficient to only check the positions in `FinalizeCore` since `FinalizeTypes::initialize` checks that every
        // `Branch` instruction targets a valid position.
        for function in self.functions().values() {
            if let Some(finalize_logic) = function.finalize_logic() {
                for position in finalize_logic.positions().keys() {
                    if keywords.contains(position.to_string().as_str()) {
                        bail!(
                            "Finalize position '{position}' is a restricted keyword for the current consensus version"
                        )
                    }
                }
            }
        }
        Ok(())
    }

    /// Checks that the program structure is well-formed under the following rules:
    ///  1. The program ID must not contain the keyword "aleo" in the program name.
    ///  2. The record name must not contain the keyword "aleo".
    ///  3. Record names must not be prefixes of other record names.
    ///  4. Record entry names must not contain the keyword "aleo".
    pub fn check_program_naming_structure(&self) -> Result<()> {
        // 1. Check if the program ID contains the "aleo" substring
        let program_id = self.id().name().to_string();
        if program_id.contains("aleo") {
            bail!("Program ID '{program_id}' can't contain the reserved keyword 'aleo'.");
        }

        // Fetch the record names in a sorted BTreeSet.
        let record_names: BTreeSet<String> = self.records.keys().map(|name| name.to_string()).collect();

        // 2. Check if any record name contains the "aleo" substring.
        for record_name in &record_names {
            if record_name.contains("aleo") {
                bail!("Record name '{record_name}' can't contain the reserved keyword 'aleo'.");
            }
        }

        // 3. Check if any of the record names are a prefix of another.
        let mut record_names_iter = record_names.iter();
        let mut previous_record_name = record_names_iter.next();
        for record_name in record_names_iter {
            if let Some(previous) = previous_record_name {
                if record_name.starts_with(previous) {
                    bail!("Record name '{previous}' can't be a prefix of record name '{record_name}'.");
                }
            }
            previous_record_name = Some(record_name);
        }

        // 4. Check if any record entry names contain the "aleo" substring.
        for record_entry_name in self.records.values().flat_map(|record_type| record_type.entries().keys()) {
            if record_entry_name.to_string().contains("aleo") {
                bail!("Record entry name '{record_entry_name}' can't contain the reserved keyword 'aleo'.");
            }
        }

        Ok(())
    }

    /// Checks that the program does not make external calls to `credits.aleo/upgrade`.
    pub fn check_external_calls_to_credits_upgrade(&self) -> Result<()> {
        // Check if the program makes external calls to `credits.aleo/upgrade`.
        cfg_iter!(self.functions()).flat_map(|(_, function)| function.instructions()).try_for_each(|instruction| {
            if let Some(CallOperator::Locator(locator)) = instruction.call_operator() {
                // Check if the locator is restricted.
                if locator.to_string() == "credits.aleo/upgrade" {
                    bail!("External call to restricted locator '{locator}'")
                }
            }
            Ok(())
        })?;
        Ok(())
    }

    /// Returns `true` if a program contains any V9 syntax.
    /// This includes `constructor`, `Operand::Edition`, `Operand::Checksum`, and `Operand::ProgramOwner`.
    /// This is enforced to be `false` for programs before `ConsensusVersion::V9`.
    #[inline]
    pub fn contains_v9_syntax(&self) -> bool {
        // Check if the program contains a constructor.
        if self.contains_constructor() {
            return true;
        }
        // Check each instruction and output in each function's finalize scope for the use of
        // `Operand::Checksum`, `Operand::Edition` or `Operand::ProgramOwner`.
        for function in self.functions().values() {
            // Check the finalize scope if it exists.
            if let Some(finalize_logic) = function.finalize_logic() {
                // Check the command operands.
                for command in finalize_logic.commands() {
                    for operand in command.operands() {
                        if matches!(operand, Operand::Checksum(_) | Operand::Edition(_) | Operand::ProgramOwner(_)) {
                            return true;
                        }
                    }
                }
            }
        }
        // Return `false` since no V9 syntax was found.
        false
    }

    /// Returns whether this program explicitly refers to an external struct, like `other_program.aleo/StructType`?
    ///
    /// This function exists to check if programs to be deployed use external structs so they can be gated
    /// by consensus version.
    pub fn contains_external_struct(&self) -> bool {
        self.mappings.values().any(|mapping| mapping.contains_external_struct())
            || self
                .structs
                .values()
                .flat_map(|struct_| struct_.members().values())
                .any(|plaintext_type| plaintext_type.contains_external_struct())
            || self
                .records
                .values()
                .flat_map(|record| record.entries().values())
                .any(|entry| entry.plaintext_type().contains_external_struct())
            || self.closures.values().any(|closure| closure.contains_external_struct())
            || self.functions.values().any(|function| function.contains_external_struct())
            || self.constructor.iter().any(|constructor| constructor.contains_external_struct())
    }

    /// Returns `true` if this program violates pre-V13 rules for external records
    /// or futures by containing registers with non-local struct types across program boundaries.
    ///
    /// Notes:
    /// 1. We only need to check functions because closures and constructors cannot reference
    ///    external records or futures.
    /// 2. We need to check function inputs.
    /// 3. No need to check instructions other than `Call`. The only other instruction that can
    ///    refer to a record is a `cast` but we cannot cast to external records anyways.
    ///4.  No need to check function outputs, because they have already been checked in either the
    ///    inputs or call instruction.
    pub fn violates_pre_v13_external_record_and_future_rules<F0, F1, F2, F3>(
        &self,
        get_external_record: &F0,
        get_external_function: &F1,
        get_external_future: &F2,
        is_local_struct: &F3,
    ) -> bool
    where
        F0: Fn(&Locator<N>) -> Result<RecordType<N>>,
        F1: Fn(&Locator<N>) -> Result<FunctionCore<N>>,
        F2: Fn(&Locator<N>) -> Result<FinalizeCore<N>>,
        F3: Fn(&Identifier<N>) -> bool,
    {
        // Helper: does a plaintext type (possibly nested in arrays) reference a struct not defined locally?
        fn plaintext_uses_nonlocal_struct<N: Network>(
            ty: &PlaintextType<N>,
            is_local_struct: &impl Fn(&Identifier<N>) -> bool,
        ) -> bool {
            match ty {
                PlaintextType::Struct(name) => !is_local_struct(name),
                PlaintextType::Array(array_type) => {
                    plaintext_uses_nonlocal_struct(array_type.base_element_type(), is_local_struct)
                }
                _ => false,
            }
        }

        // Helper: does a record contain any struct not defined locally?
        let record_uses_nonlocal_struct = |record: &RecordType<N>| {
            record.entries().iter().any(|(_, member)| match member {
                EntryType::Constant(ty) | EntryType::Private(ty) | EntryType::Public(ty) => {
                    plaintext_uses_nonlocal_struct(ty, is_local_struct)
                }
            })
        };

        for function in self.functions.values() {
            // Scan function inputs for external record types.
            for input in function.inputs() {
                let ValueType::ExternalRecord(locator) = input.value_type() else {
                    continue;
                };
                let Ok(record) = get_external_record(locator) else {
                    continue;
                };
                if record_uses_nonlocal_struct(&record) {
                    return true;
                }
            }

            // Scan instructions for calls to external programs.
            for instruction in function.instructions() {
                let Instruction::Call(call) = instruction else { continue };
                let CallOperator::Locator(locator) = call.operator() else { continue };
                let Ok(external_function) = get_external_function(locator) else {
                    continue;
                };

                // Check if the outputs of the external function reference a struct that is not
                // locally available.
                for output in external_function.outputs() {
                    match output.value_type() {
                        ValueType::Record(identifier) => {
                            let locator = Locator::new(*locator.program_id(), *identifier);
                            let Ok(record) = get_external_record(&locator) else {
                                continue;
                            };
                            if record_uses_nonlocal_struct(&record) {
                                return true;
                            }
                        }

                        ValueType::Future(loc) => {
                            let Ok(future) = get_external_future(loc) else {
                                continue;
                            };
                            for input in future.input_types() {
                                let FinalizeType::Plaintext(ty) = input else {
                                    continue;
                                };

                                // We intentionally ignore `FinalizeType::Future(_)` here. Any such future
                                // originates from another program whose deployment would already have been
                                // validated under the same pre-V13 rules. At this point, only plaintext inputs
                                // can introduce new non-local struct violations.

                                if plaintext_uses_nonlocal_struct(&ty, is_local_struct) {
                                    return true;
                                }
                            }
                        }

                        _ => {}
                    }
                }
            }
        }

        false
    }

    /// Returns `true` if the program contains an array type with a size that exceeds the given maximum.
    pub fn exceeds_max_array_size(&self, max_array_size: u32) -> bool {
        self.mappings.values().any(|mapping| mapping.exceeds_max_array_size(max_array_size))
            || self.structs.values().any(|struct_type| struct_type.exceeds_max_array_size(max_array_size))
            || self.records.values().any(|record_type| record_type.exceeds_max_array_size(max_array_size))
            || self.closures.values().any(|closure| closure.exceeds_max_array_size(max_array_size))
            || self.functions.values().any(|function| function.exceeds_max_array_size(max_array_size))
            || self.constructor.iter().any(|constructor| constructor.exceeds_max_array_size(max_array_size))
    }

    /// Returns `true` if a program contains any V11 syntax.
    /// This includes:
    /// 1. `.raw` hash or signature verification variants
    /// 2. `ecdsa.verify.*` opcodes
    #[inline]
    pub fn contains_v11_syntax(&self) -> bool {
        // Helper to check if any of the opcodes:
        // - start with `ecdsa.verify`, `serialize`, or `deserialize`
        // - end with `.raw` or `.native`
        let has_op = |opcode: &str| {
            opcode.starts_with("ecdsa.verify")
                || opcode.starts_with("serialize")
                || opcode.starts_with("deserialize")
                || opcode.ends_with(".raw")
                || opcode.ends_with(".native")
        };

        // Determine if any function instructions contain the new syntax.
        let function_contains = cfg_iter!(self.functions())
            .flat_map(|(_, function)| function.instructions())
            .any(|instruction| has_op(*instruction.opcode()));

        // Determine if any closure instructions contain the new syntax.
        let closure_contains = cfg_iter!(self.closures())
            .flat_map(|(_, closure)| closure.instructions())
            .any(|instruction| has_op(*instruction.opcode()));

        // Determine if any finalize commands or constructor commands contain the new syntax.
        let command_contains = cfg_iter!(self.functions())
            .flat_map(|(_, function)| function.finalize_logic().map(|finalize| finalize.commands()))
            .flatten()
            .chain(cfg_iter!(self.constructor).flat_map(|constructor| constructor.commands()))
            .any(|command| matches!(command, Command::Instruction(instruction) if has_op(*instruction.opcode())));

        function_contains || closure_contains || command_contains
    }

    /// Returns `true` if a program contains any V12 syntax.
    /// This includes `Operand::BlockTimestamp`.
    /// This is enforced to be `false` for programs before `ConsensusVersion::V12`.
    #[inline]
    pub fn contains_v12_syntax(&self) -> bool {
        // Check each instruction and output in each function's finalize scope for the use of
        // `Operand::BlockTimestamp`.
        cfg_iter!(self.functions()).any(|(_, function)| {
            function.finalize_logic().is_some_and(|finalize_logic| {
                cfg_iter!(finalize_logic.commands()).any(|command| {
                    cfg_iter!(command.operands()).any(|operand| matches!(operand, Operand::BlockTimestamp))
                })
            })
        })
    }

    /// Checks that the program size does not exceed the maximum allowed size for the given block height.
    pub fn check_program_size(&self, block_height: u32) -> Result<()> {
        // Calculate the program size.
        let program_size = self.to_string().len();
        // Determine the maximum allowed program size for the current consensus version.
        let maximum_allowed_program_size = consensus_config_value!(N, MAX_PROGRAM_SIZE, block_height)
            .ok_or(anyhow!("Failed to fetch maximum program size"))?;

        ensure!(
            program_size <= maximum_allowed_program_size,
            "Program size of {program_size} bytes exceeds the maximum allowed size of {maximum_allowed_program_size} bytes for the current height {block_height} (consensus version {}).",
            N::CONSENSUS_VERSION(block_height)?
        );

        Ok(())
    }

    /// Checks that the program writes size does not exceed the maximum allowed size for the given block height.
    pub fn check_program_writes(&self, block_height: u32) -> Result<()> {
        // Determine the maximum allowed number of writes for the current consensus version.
        let max_num_writes = consensus_config_value!(N, MAX_WRITES, block_height)
            .ok_or(anyhow!("Failed to fetch maximum number of writes"))?;

        // Check if the constructor exceeds the maximum number of writes.
        if self.constructor().is_some_and(|constructor| constructor.num_writes() > max_num_writes) {
            bail!(
                "Program constructor exceeds the maximum allowed writes ({max_num_writes}) for the current height {block_height} (consensus version {}).",
                N::CONSENSUS_VERSION(block_height)?
            );
        }

        // Find the first function whose finalize logic exceeds the maximum writes.
        if let Some(name) = cfg_find_map!(self.functions(), |function| {
            function
                .finalize_logic()
                .is_some_and(|finalize| finalize.num_writes() > max_num_writes)
                .then(|| *function.name())
        }) {
            bail!(
                "Program function '{name}' exceeds the maximum allowed writes ({max_num_writes}) for the current height {block_height} (consensus version {}).",
                N::CONSENSUS_VERSION(block_height)?
            );
        }

        Ok(())
    }

    /// Returns `true` if a program contains any V14 syntax.
    /// This includes:
    /// 1. `snark.verify.*` opcodes.
    /// 2. `Operand::AleoGenerator` or `Operand::AleoGeneratorPowers` operands.
    /// 3. `Literal::Identifier` operands.
    /// 4. `call.dynamic` instructions.
    /// 5. `get.record.dynamic` instructions.
    /// 6. `cast` instructions targeting `dynamic.record`.
    /// 7. `dynamic.record` or `dynamic.future` in function input or output types.
    /// 8. `contains.dynamic`, `get.dynamic`, or `get.or_use.dynamic` commands in finalize blocks.
    /// 9. `dynamic.record` or `dynamic.future` in closure input or output types.
    /// 10. `dynamic.future` in finalize block input types.
    /// 11. Identifier types in any type declarations.
    ///
    /// This is enforced to be `false` for programs before `ConsensusVersion::V14`.
    #[inline]
    pub fn contains_v14_syntax(&self) -> Result<bool> {
        /// Returns `true` if the instruction uses V14-only opcodes or operands (infallible checks).
        fn is_v14_instruction<N: Network>(instr: &Instruction<N>) -> bool {
            matches!(instr, Instruction::CallDynamic(_) | Instruction::GetRecordDynamic(_))
                || matches!(instr, Instruction::Cast(cast) if *cast.cast_type() == CastType::DynamicRecord)
                || instr.opcode().starts_with("snark.verify")
                || cfg_iter!(instr.operands()).any(|operand| {
                    matches!(
                        operand,
                        Operand::AleoGenerator
                            | Operand::AleoGeneratorPowers(_)
                            | Operand::Literal(console::program::Literal::Identifier(..))
                    )
                })
        }

        /// Returns `true` if the command uses V14-only syntax (infallible checks).
        fn is_v14_command<N: Network>(cmd: &Command<N>) -> bool {
            matches!(cmd, Command::ContainsDynamic(_) | Command::GetDynamic(_) | Command::GetOrUseDynamic(_))
                || matches!(cmd, Command::Instruction(instr) if is_v14_instruction(instr))
        }

        // Helper to check if a value type is a V14-only dynamic type.
        let is_dynamic_value_type =
            |vt: &ValueType<N>| matches!(vt, ValueType::DynamicRecord | ValueType::DynamicFuture);

        // Helper to check if a register type is a V14-only dynamic type.
        let is_dynamic_register_type =
            |rt: &RegisterType<N>| matches!(rt, RegisterType::DynamicRecord | RegisterType::DynamicFuture);

        // Check functions: instructions, finalize commands, and type declarations.
        for (_, function) in self.functions() {
            if function.instructions().iter().any(is_v14_instruction)
                || function.inputs().iter().any(|input| is_dynamic_value_type(input.value_type()))
                || function.outputs().iter().any(|output| is_dynamic_value_type(output.value_type()))
            {
                return Ok(true);
            }
            if let Some(finalize) = function.finalize_logic() {
                if finalize.inputs().iter().any(|input| matches!(input.finalize_type(), FinalizeType::DynamicFuture))
                    || finalize.commands().iter().any(is_v14_command)
                {
                    return Ok(true);
                }
            }
            if function.contains_identifier_type()? {
                return Ok(true);
            }
        }

        // Check closures: instructions and type declarations.
        for (_, closure) in self.closures() {
            if closure.instructions().iter().any(is_v14_instruction)
                || closure.inputs().iter().any(|input| is_dynamic_register_type(input.register_type()))
                || closure.outputs().iter().any(|output| is_dynamic_register_type(output.register_type()))
            {
                return Ok(true);
            }
            if closure.contains_identifier_type()? {
                return Ok(true);
            }
        }

        // Check constructor commands and identifier types.
        if let Some(constructor) = &self.constructor {
            if constructor.commands().iter().any(is_v14_command) {
                return Ok(true);
            }
            if constructor.contains_identifier_type()? {
                return Ok(true);
            }
        }

        // Check remaining type definitions: mappings, structs, records.
        for mapping in self.mappings.values() {
            if mapping.contains_identifier_type()? {
                return Ok(true);
            }
        }
        for struct_type in self.structs.values() {
            if struct_type.contains_identifier_type()? {
                return Ok(true);
            }
        }
        for record_type in self.records.values() {
            if record_type.contains_identifier_type()? {
                return Ok(true);
            }
        }

        Ok(false)
    }

    /// Returns `true` if a program contains any string type.
    /// Before ConsensusVersion::V12, variable-length string sampling when using them as inputs caused deployment synthesis to be inconsistent and abort with probability 63/64.
    /// After ConsensusVersion::V12, string types are disallowed.
    #[inline]
    pub fn contains_string_type(&self) -> bool {
        self.mappings.values().any(|mapping| mapping.contains_string_type())
            || self.structs.values().any(|struct_type| struct_type.contains_string_type())
            || self.records.values().any(|record_type| record_type.contains_string_type())
            || self.closures.values().any(|closure| closure.contains_string_type())
            || self.functions.values().any(|function| function.contains_string_type())
            || self.constructor.iter().any(|constructor| constructor.contains_string_type())
    }
}

impl<N: Network> TypeName for ProgramCore<N> {
    /// Returns the type name as a string.
    #[inline]
    fn type_name() -> &'static str {
        "program"
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use console::{
        network::MainnetV0,
        program::{Locator, ValueType},
    };

    type CurrentNetwork = MainnetV0;

    #[test]
    fn test_program_mapping() -> Result<()> {
        // Create a new mapping.
        let mapping = Mapping::<CurrentNetwork>::from_str(
            r"
mapping message:
    key as field.public;
    value as field.public;",
        )?;

        // Initialize a new program.
        let program = Program::<CurrentNetwork>::from_str(&format!("program unknown.aleo; {mapping}"))?;
        // Ensure the mapping was added.
        assert!(program.contains_mapping(&Identifier::from_str("message")?));
        // Ensure the retrieved mapping matches.
        assert_eq!(mapping.to_string(), program.get_mapping(&Identifier::from_str("message")?)?.to_string());

        Ok(())
    }

    #[test]
    fn test_program_struct() -> Result<()> {
        // Create a new struct.
        let struct_ = StructType::<CurrentNetwork>::from_str(
            r"
struct message:
    first as field;
    second as field;",
        )?;

        // Initialize a new program.
        let program = Program::<CurrentNetwork>::from_str(&format!("program unknown.aleo; {struct_}"))?;
        // Ensure the struct was added.
        assert!(program.contains_struct(&Identifier::from_str("message")?));
        // Ensure the retrieved struct matches.
        assert_eq!(&struct_, program.get_struct(&Identifier::from_str("message")?)?);

        Ok(())
    }

    #[test]
    fn test_program_record() -> Result<()> {
        // Create a new record.
        let record = RecordType::<CurrentNetwork>::from_str(
            r"
record foo:
    owner as address.private;
    first as field.private;
    second as field.public;",
        )?;

        // Initialize a new program.
        let program = Program::<CurrentNetwork>::from_str(&format!("program unknown.aleo; {record}"))?;
        // Ensure the record was added.
        assert!(program.contains_record(&Identifier::from_str("foo")?));
        // Ensure the retrieved record matches.
        assert_eq!(&record, program.get_record(&Identifier::from_str("foo")?)?);

        Ok(())
    }

    #[test]
    fn test_program_function() -> Result<()> {
        // Create a new function.
        let function = Function::<CurrentNetwork>::from_str(
            r"
function compute:
    input r0 as field.public;
    input r1 as field.private;
    add r0 r1 into r2;
    output r2 as field.private;",
        )?;

        // Initialize a new program.
        let program = Program::<CurrentNetwork>::from_str(&format!("program unknown.aleo; {function}"))?;
        // Ensure the function was added.
        assert!(program.contains_function(&Identifier::from_str("compute")?));
        // Ensure the retrieved function matches.
        assert_eq!(function, program.get_function(&Identifier::from_str("compute")?)?);

        Ok(())
    }

    #[test]
    fn test_program_import() -> Result<()> {
        // Initialize a new program.
        let program = Program::<CurrentNetwork>::from_str(
            r"
import eth.aleo;
import usdc.aleo;

program swap.aleo;

// The `swap` function transfers ownership of the record
// for token A to the record owner of token B, and vice-versa.
function swap:
    // Input the record for token A.
    input r0 as eth.aleo/eth.record;
    // Input the record for token B.
    input r1 as usdc.aleo/usdc.record;

    // Send the record for token A to the owner of token B.
    call eth.aleo/transfer r0 r1.owner r0.amount into r2 r3;

    // Send the record for token B to the owner of token A.
    call usdc.aleo/transfer r1 r0.owner r1.amount into r4 r5;

    // Output the new record for token A.
    output r2 as eth.aleo/eth.record;
    // Output the new record for token B.
    output r4 as usdc.aleo/usdc.record;
    ",
        )
        .unwrap();

        // Ensure the program imports exist.
        assert!(program.contains_import(&ProgramID::from_str("eth.aleo")?));
        assert!(program.contains_import(&ProgramID::from_str("usdc.aleo")?));

        // Retrieve the 'swap' function.
        let function = program.get_function(&Identifier::from_str("swap")?)?;

        // Ensure there are two inputs.
        assert_eq!(function.inputs().len(), 2);
        assert_eq!(function.input_types().len(), 2);

        // Declare the expected input types.
        let expected_input_type_1 = ValueType::ExternalRecord(Locator::from_str("eth.aleo/eth")?);
        let expected_input_type_2 = ValueType::ExternalRecord(Locator::from_str("usdc.aleo/usdc")?);

        // Ensure the inputs are external records.
        assert_eq!(function.input_types()[0], expected_input_type_1);
        assert_eq!(function.input_types()[1], expected_input_type_2);

        // Ensure the input variants are correct.
        assert_eq!(function.input_types()[0].variant(), expected_input_type_1.variant());
        assert_eq!(function.input_types()[1].variant(), expected_input_type_2.variant());

        // Ensure there are two instructions.
        assert_eq!(function.instructions().len(), 2);

        // Ensure the instructions are calls.
        assert_eq!(function.instructions()[0].opcode(), Opcode::Call("call"));
        assert_eq!(function.instructions()[1].opcode(), Opcode::Call("call"));

        // Ensure there are two outputs.
        assert_eq!(function.outputs().len(), 2);
        assert_eq!(function.output_types().len(), 2);

        // Declare the expected output types.
        let expected_output_type_1 = ValueType::ExternalRecord(Locator::from_str("eth.aleo/eth")?);
        let expected_output_type_2 = ValueType::ExternalRecord(Locator::from_str("usdc.aleo/usdc")?);

        // Ensure the outputs are external records.
        assert_eq!(function.output_types()[0], expected_output_type_1);
        assert_eq!(function.output_types()[1], expected_output_type_2);

        // Ensure the output variants are correct.
        assert_eq!(function.output_types()[0].variant(), expected_output_type_1.variant());
        assert_eq!(function.output_types()[1].variant(), expected_output_type_2.variant());

        Ok(())
    }

    #[test]
    fn test_program_with_constructor() {
        // Initialize a new program.
        let program_string = r"import credits.aleo;

program good_constructor.aleo;

constructor:
    assert.eq edition 0u16;
    assert.eq credits.aleo/edition 0u16;
    assert.neq checksum 0field;
    assert.eq credits.aleo/checksum 6192738754253668739186185034243585975029374333074931926190215457304721124008field;
    set 1u8 into data[0u8];

mapping data:
    key as u8.public;
    value as u8.public;

function dummy:

function check:
    async check into r0;
    output r0 as good_constructor.aleo/check.future;

finalize check:
    get data[0u8] into r0;
    assert.eq r0 1u8;
";
        let program = Program::<CurrentNetwork>::from_str(program_string).unwrap();

        // Check that the string and bytes (de)serialization works.
        let serialized = program.to_string();
        let deserialized = Program::<CurrentNetwork>::from_str(&serialized).unwrap();
        assert_eq!(program, deserialized);

        let serialized = program.to_bytes_le().unwrap();
        let deserialized = Program::<CurrentNetwork>::from_bytes_le(&serialized).unwrap();
        assert_eq!(program, deserialized);

        // Check that the display works.
        let display = format!("{program}");
        assert_eq!(display, program_string);

        // Ensure the program contains a constructor.
        assert!(program.contains_constructor());
        assert_eq!(program.constructor().unwrap().commands().len(), 5);
    }

    #[test]
    fn test_program_equality_and_checksum() {
        fn run_test(program1: &str, program2: &str, expected_equal: bool) {
            println!("Comparing programs:\n{program1}\n{program2}");
            let program1 = Program::<CurrentNetwork>::from_str(program1).unwrap();
            let program2 = Program::<CurrentNetwork>::from_str(program2).unwrap();
            assert_eq!(program1 == program2, expected_equal);
            assert_eq!(program1.to_checksum() == program2.to_checksum(), expected_equal);
        }

        // Test two identical programs, with different whitespace.
        run_test(r"program test.aleo; function dummy:    ", r"program  test.aleo;     function dummy:   ", true);

        // Test two programs, one with a different function name.
        run_test(r"program test.aleo; function dummy:    ", r"program test.aleo; function bummy:   ", false);

        // Test two programs, one with a constructor and one without.
        run_test(
            r"program test.aleo; function dummy:    ",
            r"program test.aleo; constructor: assert.eq true true; function dummy: ",
            false,
        );

        // Test two programs, both with a struct and function, but in different order.
        run_test(
            r"program test.aleo; struct foo: data as u8; function dummy:",
            r"program test.aleo; function dummy: struct foo: data as u8;",
            false,
        );
    }

    #[test]
    fn test_contains_v14_syntax() -> Result<()> {
        // A baseline program with no V14 syntax.
        let no_v14 = Program::<CurrentNetwork>::from_str(
            r"program test.aleo;
function foo:
    input r0 as u64.public;
    output r0 as u64.public;",
        )?;
        assert!(!no_v14.contains_v14_syntax()?);

        // A program with `dynamic.record` as a function input type.
        let dynamic_record_input = Program::<CurrentNetwork>::from_str(
            r"program test.aleo;
function foo:
    input r0 as dynamic.record;",
        )?;
        assert!(dynamic_record_input.contains_v14_syntax()?);

        // A program with `dynamic.future` as a function output type.
        let dynamic_future_output = Program::<CurrentNetwork>::from_str(
            r"program test.aleo;
function foo:
    output r0 as dynamic.future;",
        )?;
        assert!(dynamic_future_output.contains_v14_syntax()?);

        // A program with a `call.dynamic` instruction.
        let call_dynamic = Program::<CurrentNetwork>::from_str(
            r"program test.aleo;
function foo:
    input r0 as field.public;
    input r1 as field.public;
    input r2 as field.public;
    call.dynamic r0 r1 r2 into r3 (as u64.public);
    output r3 as u64.public;",
        )?;
        assert!(call_dynamic.contains_v14_syntax()?);

        // A program with a `get.record.dynamic` instruction.
        let get_record_dynamic = Program::<CurrentNetwork>::from_str(
            r"program test.aleo;
function foo:
    input r0 as dynamic.record;
    get.record.dynamic r0.amount into r1 as field;
    output r1 as field.public;",
        )?;
        assert!(get_record_dynamic.contains_v14_syntax()?);

        // A program with a `cast ... as dynamic.record` instruction.
        let cast_dynamic_record = Program::<CurrentNetwork>::from_str(
            r"program test.aleo;
record token:
    owner as address.private;
    amount as u64.private;
function foo:
    input r0 as token.record;
    cast r0 into r1 as dynamic.record;
    output r0.owner as address.private;",
        )?;
        assert!(cast_dynamic_record.contains_v14_syntax()?);

        // A program with `contains.dynamic` in a finalize block.
        let contains_dynamic = Program::<CurrentNetwork>::from_str(
            r"program test.aleo;
function bar:
    input r0 as field.public;
    input r1 as field.public;
    input r2 as field.public;
    input r3 as field.public;
    async bar r0 r1 r2 r3 into r4;
    output r4 as test.aleo/bar.future;
finalize bar:
    input r0 as field.public;
    input r1 as field.public;
    input r2 as field.public;
    input r3 as field.public;
    contains.dynamic r0 r1 r2[r3] into r4;",
        )?;
        assert!(contains_dynamic.contains_v14_syntax()?);

        // A program with `get.dynamic` in a finalize block.
        let get_dynamic = Program::<CurrentNetwork>::from_str(
            r"program test.aleo;
function bar:
    input r0 as field.public;
    input r1 as field.public;
    input r2 as field.public;
    input r3 as field.public;
    async bar r0 r1 r2 r3 into r4;
    output r4 as test.aleo/bar.future;
finalize bar:
    input r0 as field.public;
    input r1 as field.public;
    input r2 as field.public;
    input r3 as field.public;
    get.dynamic r0 r1 r2[r3] into r4 as field;",
        )?;
        assert!(get_dynamic.contains_v14_syntax()?);

        // A program with `dynamic.future` as a finalize input type.
        let dynamic_future_finalize_input = Program::<CurrentNetwork>::from_str(
            r"program test.aleo;
function foo:
    input r0 as field.public;
    async foo r0 into r1;
    output r1 as test.aleo/foo.future;
finalize foo:
    input r0 as dynamic.future;
    await r0;",
        )?;
        assert!(dynamic_future_finalize_input.contains_v14_syntax()?);

        // A program with `dynamic.record` as a closure input type.
        let closure_dynamic_input = Program::<CurrentNetwork>::from_str(
            r"program test.aleo;
closure bar:
    input r0 as dynamic.record;
    input r1 as field;
    add r1 r1 into r2;",
        )?;
        assert!(closure_dynamic_input.contains_v14_syntax()?);

        // A program with `dynamic.record` as a closure output type.
        let closure_dynamic_output = Program::<CurrentNetwork>::from_str(
            r"program test.aleo;
closure bar:
    input r0 as field;
    add r0 r0 into r1;
    output r1 as dynamic.record;",
        )?;
        assert!(closure_dynamic_output.contains_v14_syntax()?);

        // A program with `get.or_use.dynamic` in a finalize block.
        let get_or_use_dynamic = Program::<CurrentNetwork>::from_str(
            r"program test.aleo;
function bar:
    input r0 as field.public;
    input r1 as field.public;
    input r2 as field.public;
    input r3 as field.public;
    input r4 as u64.public;
    async bar r0 r1 r2 r3 r4 into r5;
    output r5 as test.aleo/bar.future;
finalize bar:
    input r0 as field.public;
    input r1 as field.public;
    input r2 as field.public;
    input r3 as field.public;
    input r4 as u64.public;
    get.or_use.dynamic r0 r1 r2[r3] r4 into r5 as u64;",
        )?;
        assert!(get_or_use_dynamic.contains_v14_syntax()?);

        // A program with `snark.verify` in a finalize block.
        let snark_verify = Program::<CurrentNetwork>::from_str(
            r"program test.aleo;
function foo:
    input r0 as [u8; 8u32].public;
    input r1 as [field; 1u32].public;
    input r2 as [u8; 8u32].public;
    async foo r0 r1 r2 into r3;
    output r3 as test.aleo/foo.future;
finalize foo:
    input r0 as [u8; 8u32].public;
    input r1 as [field; 1u32].public;
    input r2 as [u8; 8u32].public;
    snark.verify r0 1u8 r1 r2 into r3;",
        )?;
        assert!(snark_verify.contains_v14_syntax()?);

        // A program with `snark.verify.batch` in a finalize block.
        let snark_verify_batch = Program::<CurrentNetwork>::from_str(
            r"program test.aleo;
function foo:
    input r0 as [[u8; 8u32]; 1u32].public;
    input r1 as [[[field; 1u32]; 1u32]; 1u32].public;
    input r2 as [u8; 8u32].public;
    async foo r0 r1 r2 into r3;
    output r3 as test.aleo/foo.future;
finalize foo:
    input r0 as [[u8; 8u32]; 1u32].public;
    input r1 as [[[field; 1u32]; 1u32]; 1u32].public;
    input r2 as [u8; 8u32].public;
    snark.verify.batch r0 1u8 r1 r2 into r3;",
        )?;
        assert!(snark_verify_batch.contains_v14_syntax()?);

        // A program with `aleo::GENERATOR` as an operand.
        let aleo_generator = Program::<CurrentNetwork>::from_str(
            r"program test.aleo;
function foo:
    input r0 as scalar.public;
    mul aleo::GENERATOR r0 into r1;
    output r1 as group.public;",
        )?;
        assert!(aleo_generator.contains_v14_syntax()?);

        // A program with `aleo::GENERATOR_POWERS` as an operand.
        let aleo_generator_powers = Program::<CurrentNetwork>::from_str(
            r"program test.aleo;
function foo:
    input r0 as scalar.public;
    mul aleo::GENERATOR_POWERS[0u32] r0 into r1;
    output r1 as group.public;",
        )?;
        assert!(aleo_generator_powers.contains_v14_syntax()?);

        // A program with `dynamic.future` as a closure output type.
        let closure_dynamic_future_output = Program::<CurrentNetwork>::from_str(
            r"program test.aleo;
closure bar:
    input r0 as field;
    add r0 r0 into r1;
    output r1 as dynamic.future;",
        )?;
        assert!(closure_dynamic_future_output.contains_v14_syntax()?);

        // A program with a V14 opcode (`aleo::GENERATOR`) in a constructor block.
        let constructor_v14 = Program::<CurrentNetwork>::from_str(
            r"program test.aleo;
function dummy:
    input r0 as field.public;
    output r0 as field.public;
constructor:
    assert.eq aleo::GENERATOR aleo::GENERATOR;",
        )?;
        assert!(constructor_v14.contains_v14_syntax()?);

        Ok(())
    }
}