neptune-consensus 0.14.0

Consensus logic and proof abstractions for Neptune Cash.
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
use std::collections::HashMap;
use std::fmt::Display;

use get_size2::GetSize;
use itertools::Itertools;
use neptune_mutator_set::authenticated_item::AuthenticatedItem;
use neptune_mutator_set::ms_membership_proof::MsMembershipProof;
use neptune_mutator_set::mutator_set_accumulator::MutatorSetAccumulator;
use neptune_mutator_set::removal_record::RemovalRecord;
use neptune_primitives::mast_hash::MastHash;
use rand::rngs::StdRng;
use rand::Rng;
use rand::SeedableRng;
use serde::Deserialize;
use serde::Serialize;
use tasm_lib::prelude::Digest;
use tasm_lib::structure::tasm_object::TasmObject;
use tasm_lib::triton_vm::prelude::*;
use tasm_lib::twenty_first::math::b_field_element::BFieldElement;
use tasm_lib::twenty_first::math::bfield_codec::BFieldCodec;
use tracing::warn;

use super::lock_script::LockScriptAndWitness;
use super::transaction_kernel::TransactionKernel;
use super::transaction_kernel::TransactionKernelModifier;
use super::utxo::Utxo;
use crate::block::mutator_set_update::MutatorSetUpdate;
use crate::type_scripts::known_type_scripts;
use crate::type_scripts::known_type_scripts::match_type_script_and_generate_witness;
use crate::type_scripts::TypeScriptAndWitness;

/// A list of UTXOs with an associated salt.
///
/// `SaltedUtxos` is a struct for representing a list of UTXOs in a witness object when it
/// is desirable to associate a random but consistent salt for the entire list of UTXOs.
/// This situation arises when two distinct consensus programs prove different features
/// about the same list of UTXOs.
#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize, GetSize, BFieldCodec, TasmObject)]
pub struct SaltedUtxos {
    pub utxos: Vec<Utxo>,
    pub salt: [BFieldElement; 3],
}

impl Display for SaltedUtxos {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "{}",
            self.utxos
                .iter()
                .enumerate()
                .map(|(i, utxo)| format!("\nutxo {i}: {utxo}"))
                .join("")
        )
    }
}

impl SaltedUtxos {
    /// Takes a Vec of UTXOs and returns a `SaltedUtxos` object. The salt comes from
    /// `thread_rng`.
    pub fn new(utxos: Vec<Utxo>) -> Self {
        Self {
            utxos,
            salt: rand::rng().random(),
        }
    }

    pub fn new_with_rng(utxos: Vec<Utxo>, rng: &mut StdRng) -> Self {
        Self {
            utxos,
            salt: rng.random(),
        }
    }

    /// Generate a `SaltedUtxos` object that contains no UTXOs. There is a random salt
    /// though, which comes from `thread_rng`.
    pub fn empty() -> Self {
        Self {
            utxos: vec![],
            salt: rand::rng().random(),
        }
    }

    /// Concatenate two `SaltedUtxos` objects. Derives the salt from hashing the
    /// concatenation of that of the operands.
    pub fn cat(&self, other: SaltedUtxos) -> Self {
        Self {
            utxos: [self.utxos.clone(), other.utxos].concat(),
            salt: Tip5::hash_varlen(&[self.salt, other.salt].concat().to_vec()).values()[0..3]
                .try_into()
                .unwrap(),
        }
    }
}

impl IntoIterator for SaltedUtxos {
    type Item = Utxo;

    type IntoIter = std::vec::IntoIter<Self::Item>;

    fn into_iter(self) -> Self::IntoIter {
        self.utxos.into_iter()
    }
}

/// The raw witness is the most primitive type of transaction witness.
/// It exposes secret data and is therefore not for broadcasting.
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq, GetSize, BFieldCodec)]
pub struct PrimitiveWitness {
    pub input_utxos: SaltedUtxos,
    pub input_membership_proofs: Vec<MsMembershipProof>,
    pub lock_scripts_and_witnesses: Vec<LockScriptAndWitness>,
    pub type_scripts_and_witnesses: Vec<TypeScriptAndWitness>,
    pub output_utxos: SaltedUtxos,
    pub output_sender_randomnesses: Vec<Digest>,
    pub output_receiver_digests: Vec<Digest>,
    pub mutator_set_accumulator: MutatorSetAccumulator,
    pub kernel: TransactionKernel,
}

impl Display for PrimitiveWitness {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let coinbase_str = match self.kernel.coinbase {
            Some(cb) => format!("Yes: {cb}"),
            None => "No".to_owned(),
        };
        let utxo_digests = self.input_utxos.utxos.iter().map(Tip5::hash);
        let kernel_merkle_tree = self.kernel.merkle_tree();
        let mut kernel_mt_leafs = kernel_merkle_tree.leafs();
        write!(
            f,
            "inputs: [{}]\noutputs: [{}]\ncoinbase: {}\nfee: {}\n\
            txk mast hash: {}\n\ninput canonical commitments:\n{}\n\
            kernel mast hash leafs:\n{}\n\n\n",
            self.input_utxos,
            self.output_utxos,
            coinbase_str,
            self.kernel.fee,
            self.kernel.mast_hash(),
            self.input_membership_proofs
                .iter()
                .zip_eq(utxo_digests)
                .map(|(msmp, utxo_digest)| msmp.addition_record(utxo_digest).canonical_commitment)
                .join("\n"),
            kernel_mt_leafs.join("\n"),
        )
    }
}

impl PrimitiveWitness {
    /// Generate a primitive witness for a transaction from various disparate witness data.
    ///
    /// # Panics
    /// Panics if transaction validity cannot be satisfied.
    pub fn generate_primitive_witness(
        inputs: Vec<(Utxo, LockScriptAndWitness, MsMembershipProof)>,
        output_utxos: Vec<Utxo>,
        sender_randomnesses: Vec<Digest>,
        receiver_digests: Vec<Digest>,
        transaction_kernel: TransactionKernel,
        mutator_set_accumulator: MutatorSetAccumulator,
    ) -> PrimitiveWitness {
        /// Generate a salt to use for [SaltedUtxos], deterministically.
        fn generate_secure_pseudorandom_seed(
            input_utxos: &Vec<Utxo>,
            output_utxos: &Vec<Utxo>,
            sender_randomnesses: &Vec<Digest>,
        ) -> [u8; 32] {
            let preimage = [
                input_utxos.encode(),
                output_utxos.encode(),
                sender_randomnesses.encode(),
            ]
            .concat();
            let seed = Tip5::hash_varlen(&preimage);
            let seed: [u8; Digest::BYTES] = seed.into();

            seed[0..32].try_into().unwrap()
        }

        let (input_utxos, input_lock_scripts_and_witnesses, input_membership_proofs): (
            Vec<Utxo>,
            Vec<LockScriptAndWitness>,
            Vec<MsMembershipProof>,
        ) = inputs.into_iter().multiunzip();

        let salt_seed =
            generate_secure_pseudorandom_seed(&input_utxos, &output_utxos, &sender_randomnesses);

        let mut rng = StdRng::from_seed(salt_seed);
        let salted_output_utxos = SaltedUtxos::new_with_rng(output_utxos.to_vec(), &mut rng);
        let salted_input_utxos = SaltedUtxos::new_with_rng(input_utxos.clone(), &mut rng);

        let type_script_hashes =
            Utxo::type_script_hashes(input_utxos.iter().chain(output_utxos.iter()));
        let type_scripts_and_witnesses = type_script_hashes
            .into_iter()
            .map(|type_script_hash| {
                match_type_script_and_generate_witness(
                    type_script_hash,
                    transaction_kernel.clone(),
                    salted_input_utxos.clone(),
                    salted_output_utxos.clone(),
                )
                .expect("type script hash should be known.")
            })
            .collect_vec();
        PrimitiveWitness {
            input_utxos: salted_input_utxos,
            lock_scripts_and_witnesses: input_lock_scripts_and_witnesses,
            type_scripts_and_witnesses,
            input_membership_proofs,
            output_utxos: salted_output_utxos,
            output_sender_randomnesses: sender_randomnesses.to_vec(),
            output_receiver_digests: receiver_digests.to_vec(),
            mutator_set_accumulator,
            kernel: transaction_kernel,
        }
    }

    /// Verify the transaction directly from primitive witness
    ///
    /// (without proofs or decomposing into subclaims).
    pub async fn validate(&self) -> Result<(), WitnessValidationError> {
        for lock_script_and_witness in &self.lock_scripts_and_witnesses {
            let lock_script = lock_script_and_witness.program.clone();
            let secret_input = lock_script_and_witness.nondeterminism();
            let public_input = Tip5::hash(self).reversed().encode().into();

            // This could be a lengthy, CPU intensive call.
            // Also, the lock script is satisfied if it halts gracefully (i.e., without crashing).
            // The output is irrelevant.
            let result = tokio::task::spawn_blocking(move || {
                VM::run(lock_script.clone(), public_input, secret_input)
            })
            .await;

            let Ok(run_res) = result else {
                let reason = "Failed to spawn task for verifying lock script.";
                let error = WitnessValidationError::Failed(reason.into());
                warn!("{}", error);
                return Err(error);
            };

            if let Err(_e) = run_res {
                // tbd: should we include the VMerror in InvalidLockScript error?
                let error = WitnessValidationError::InvalidLockScript(
                    lock_script_and_witness.program.hash(),
                );
                warn!("{}", error);
                return Err(error);
            }
        }

        // Verify correct computation of removal records. Also, collect the removal
        // records' hashes in order to validate them against those provided in the
        // transaction kernel later.
        let mut witnessed_removal_records = vec![];
        for (input_utxo, membership_proof) in self
            .input_utxos
            .utxos
            .iter()
            .zip_eq(&self.input_membership_proofs)
        {
            let item = Tip5::hash(input_utxo);
            // TODO: write these functions in tasm
            if !self.mutator_set_accumulator.verify(item, membership_proof) {
                let error = WitnessValidationError::InvalidMembershipProof {
                    witness_mutator_set_accumulator_hash: self.mutator_set_accumulator.hash(),
                    kernel_mutator_set_hash: self.kernel.mutator_set_hash,
                };
                warn!("{} - {:#?}", error, error);
                return Err(error);
            }
            let removal_record = self.mutator_set_accumulator.drop(item, membership_proof);
            witnessed_removal_records.push(removal_record);
        }

        // verify that all type required scripts are present.
        let required_type_script_hashes = Utxo::type_script_hashes(
            self.output_utxos
                .utxos
                .iter()
                .chain(&self.input_utxos.utxos),
        );
        let type_script_dictionary = self
            .type_scripts_and_witnesses
            .iter()
            .map(|tsaw| (tsaw.program.hash(), tsaw.program.to_owned()))
            .collect::<HashMap<_, _>>();

        // Verify we don't have too many type script witnesses.
        if type_script_dictionary.len() > required_type_script_hashes.len() {
            let error = WitnessValidationError::TooManyTypeScriptWitnesses {
                expected: required_type_script_hashes.len(),
                got: type_script_dictionary.len(),
            };
            warn!("{}", error);
            return Err(error);
        }

        // all must be in dictionary.  so if we find first that is not then it
        // is already an error.  note that the error only informs caller of the
        // first unknown, not all.
        if let Some(first_missing_type_script_hash) = required_type_script_hashes
            .iter()
            .find(|tsh| !type_script_dictionary.contains_key(tsh))
        {
            let typescript_name =
                known_type_scripts::typescript_name(*first_missing_type_script_hash);
            let error = WitnessValidationError::MissingTypeScriptWitness {
                type_script_hash: *first_missing_type_script_hash,
                type_script_name: typescript_name.to_owned(),
            };
            warn!("{}", error);
            return Err(error);
        }

        // verify type scripts
        for (j, type_script_hash) in required_type_script_hashes.iter().enumerate() {
            let type_script = type_script_dictionary[type_script_hash].clone();
            let nondeterminism = self.type_scripts_and_witnesses[j].nondeterminism();
            let public_input = [
                self.kernel.mast_hash(),
                Tip5::hash(&self.input_utxos),
                Tip5::hash(&self.output_utxos),
            ]
            .into_iter()
            .flat_map(|d| d.reversed().values())
            .collect::<Vec<BFieldElement>>();
            let public_input: PublicInput = public_input.into();

            // Like above: potentially lengthy, CPU intensive call, only thing that matters
            // is error-free completion.
            let result = tokio::task::spawn_blocking(move || {
                VM::run(type_script, public_input, nondeterminism)
            })
            .await;

            let Ok(run_res) = result else {
                let reason = "Failed to spawn task for verifying type script.";
                let error = WitnessValidationError::Failed(reason.into());
                warn!("{}", error);
                return Err(error);
            };

            if let Err(vm_error) = run_res {
                // tbd: should we include the VMError in InvalidTypeScript error?
                let typescript_name = known_type_scripts::typescript_name(*type_script_hash);
                let error = WitnessValidationError::InvalidTypeScript {
                    type_script_hash: *type_script_hash,
                    type_script_name: typescript_name.to_owned(),
                    vm_error: vm_error.to_string(),
                };
                warn!("{}", error);
                return Err(error);
            }
        }

        if witnessed_removal_records != self.kernel.inputs {
            let error = WitnessValidationError::RemovalRecordsMismatch {
                witnessed_removal_records,
                kernel_removal_records: self.kernel.inputs.clone(),
            };
            warn!("{} - {:#?}", error, error);
            return Err(error);
        }

        if self.mutator_set_accumulator.hash() != self.kernel.mutator_set_hash {
            let error = WitnessValidationError::MutatorSetMismatch {
                witness_mutator_set_hash: self.mutator_set_accumulator.hash(),
                transaction_mutator_set_hash: self.kernel.mutator_set_hash,
            };
            warn!("{} - {:#?}", error, error);
            return Err(error);
        }

        if self.kernel.merge_bit {
            let error = WitnessValidationError::MergeBitSet;
            warn!("{} - {:#?}", error, error);
            return Err(error);
        }

        // announcements: there isn't anything to verify

        Ok(())
    }

    /// Update a primitive witness to be valid under a new mutator set.
    ///
    /// Assumes initial primitive witness is valid, and that inputs were not
    /// spent in the supplied mutator set update. Otherwise panics. Also
    /// assumes that the lock script witnesses are still valid under the new
    /// mutator set.
    pub fn update_with_new_ms_data(self, mutator_set_update: MutatorSetUpdate) -> Self {
        let mut msa_state: MutatorSetAccumulator = self.mutator_set_accumulator.clone();
        let mut transaction_removal_records: Vec<RemovalRecord> = self.kernel.inputs.clone();
        let mut transaction_removal_records: Vec<&mut RemovalRecord> =
            transaction_removal_records.iter_mut().collect();

        let mut primitive_witness = self;

        let old_membership_proofs = primitive_witness.input_membership_proofs.clone();
        let own_items = primitive_witness
            .input_utxos
            .utxos
            .iter()
            .map(Tip5::hash)
            .collect_vec();
        let mut authenticated_items = own_items
            .into_iter()
            .zip(old_membership_proofs)
            .map(|(item, msmp)| AuthenticatedItem {
                item,
                ms_membership_proof: msmp,
            })
            .collect_vec();

        // Don't need to check return value because we assume inputs are not
        // spent in supplied mutator set update.
        let _ = mutator_set_update.apply_to_accumulator_and_records(
            &mut msa_state,
            &mut transaction_removal_records,
            &mut authenticated_items.iter_mut().collect_vec(),
        );
        let new_membership_proofs = authenticated_items
            .into_iter()
            .map(|x| x.ms_membership_proof)
            .collect_vec();
        primitive_witness.input_membership_proofs = new_membership_proofs;

        let kernel = TransactionKernelModifier::default()
            .inputs(
                transaction_removal_records
                    .into_iter()
                    .map(|x| x.to_owned())
                    .collect_vec(),
            )
            .mutator_set_hash(msa_state.hash())
            .clone_modify(&primitive_witness.kernel);

        primitive_witness.kernel = kernel.clone();
        primitive_witness.mutator_set_accumulator = msa_state.clone();

        // Set type_scripts_and_witnesses field from other fields in primitive witness.
        let type_scripts_and_witnesses = primitive_witness
            .type_scripts_and_witnesses
            .iter()
            .map(|ts_and_w| {
                match_type_script_and_generate_witness(
                    ts_and_w.program.hash(),
                    kernel.clone(),
                    primitive_witness.input_utxos.clone(),
                    primitive_witness.output_utxos.clone(),
                )
                .expect("type script hash should be known.")
            })
            .collect_vec();
        primitive_witness.type_scripts_and_witnesses = type_scripts_and_witnesses;

        primitive_witness
    }
}

/// enumerates possible witness validation errors
#[derive(Debug, Clone, thiserror::Error, Serialize, Deserialize)]
#[non_exhaustive]
pub enum WitnessValidationError {
    #[error("invalid lock script. error: {0}")]
    InvalidLockScript(Digest),

    #[error("invalid membership proof")]
    InvalidMembershipProof {
        witness_mutator_set_accumulator_hash: Digest,
        kernel_mutator_set_hash: Digest,
    },

    #[error(
        "missing typescript witness for: {type_script_name},\n with hash:\n {type_script_hash}"
    )]
    MissingTypeScriptWitness {
        type_script_hash: Digest,
        type_script_name: String,
    },

    #[error("Too many typescript witnesses. Expected {expected}, got {got}")]
    TooManyTypeScriptWitnesses { expected: usize, got: usize },

    #[error("invalid type script: {type_script_hash}; ({type_script_name}). Error:\n{vm_error}")]
    InvalidTypeScript {
        type_script_hash: Digest,
        type_script_name: String,
        vm_error: String,
    },

    #[error("removal records generated from witness do not match transaction kernel inputs")]
    RemovalRecordsMismatch {
        witnessed_removal_records: Vec<RemovalRecord>,
        kernel_removal_records: Vec<RemovalRecord>,
    },

    #[error("transaction mutator set does not match witness mutator set")]
    MutatorSetMismatch {
        witness_mutator_set_hash: Digest,
        transaction_mutator_set_hash: Digest,
    },

    #[error("Primitive-witness backed transaction cannot have a set merge bit")]
    MergeBitSet,

    // catch-all error, eg for anyhow errors
    #[error("transaction could not be created.  reason: {0}")]
    Failed(String),
}

#[cfg(any(test, feature = "arbitrary-impls"))]
pub mod neptune_arbitrary {
    use itertools::izip;
    use neptune_mutator_set::msa_and_records::MsaAndRecords;
    use neptune_primitives::timestamp::Timestamp;
    use num_traits::CheckedAdd;
    use num_traits::CheckedSub;
    use num_traits::Zero;
    use proptest::arbitrary::Arbitrary;
    use proptest::collection::vec;
    use proptest::strategy::BoxedStrategy;
    use proptest::strategy::Strategy;
    use proptest_arbitrary_interop::arb;

    use super::super::announcement::Announcement;
    use super::*;
    use crate::block::MINING_REWARD_TIME_LOCK_PERIOD;
    use crate::proof_abstractions::tasm::program::TritonProgram;
    use crate::transaction::transaction_kernel::TransactionKernelProxy;
    use crate::transaction::utxo_triple::UtxoTriple;
    use crate::type_scripts::native_currency::NativeCurrency;
    use crate::type_scripts::native_currency::NativeCurrencyWitness;
    use crate::type_scripts::native_currency_amount::NativeCurrencyAmount;
    use crate::type_scripts::time_lock::TimeLock;
    use crate::type_scripts::time_lock::TimeLockWitness;
    use crate::type_scripts::TypeScriptWitness;

    impl PrimitiveWitness {
        /// Strategy for generating a `PrimitiveWitness` with the given number of
        /// inputs, outputs, and announcements. If `num_inputs` is set to
        /// `None`, then the `PrimitiveWitness` is for a coinbase transaction.
        pub fn arbitrary_with_size_numbers(
            num_inputs: Option<usize>,
            num_outputs: usize,
            num_announcements: usize,
        ) -> BoxedStrategy<Self> {
            Self::arbitrary_with_size_numbers_and_merge_bit(
                num_inputs,
                num_outputs,
                num_announcements,
                false,
            )
        }

        /// Strategy for generating a `PrimitiveWitness` with the given number of
        /// inputs, outputs, and announcements. If `num_inputs` is set to
        /// `None`, then the `PrimitiveWitness` is for a coinbase transaction.
        pub(crate) fn arbitrary_with_size_numbers_and_merge_bit(
            num_inputs: Option<usize>,
            num_outputs: usize,
            num_announcements: usize,
            merge_bit: bool,
        ) -> BoxedStrategy<Self> {
            // Primitive witnesses may not simultaneously have inputs and set a
            // coinbase.
            let (num_inputs, set_coinbase) = match num_inputs {
                Some(number) => (number, false),
                None => (0, true),
            };

            // unwrap:
            //  - total amount
            //  - lock script preimages (inputs)
            //  - amounts (inputs)
            //  - lock script preimages (outputs)
            //  - amounts (outputs)
            //  - publiannouncements
            //  - fee
            //  - timestamp
            (
                NativeCurrencyAmount::arbitrary_non_negative(),
                vec(arb::<Digest>(), num_inputs),
                vec(arb::<u64>(), num_inputs),
                vec(arb::<Digest>(), num_outputs),
                vec(arb::<u64>(), num_outputs),
                vec(arb::<Announcement>(), num_announcements),
                arb::<u64>(),
                arb::<Timestamp>(),
            )
                .prop_flat_map(
                    move |(
                        mut total_amount,
                        input_address_seeds,
                        input_dist,
                        output_address_seeds,
                        output_dist,
                        announcements,
                        fee_dist,
                        timestamp,
                    )| {
                        let (maybe_coinbase, input_utxos, input_lock_scripts_and_witnesses) =
                            if set_coinbase {
                                (Some(total_amount), vec![], vec![])
                            } else {
                                // distribute total amount across inputs (+ coinbase)
                                let input_denominator =
                                    input_dist.iter().map(|u| *u as f64).sum::<f64>();
                                let input_weights = input_dist
                                    .into_iter()
                                    .map(|u| (u as f64) / input_denominator)
                                    .collect_vec();
                                let mut input_amounts = input_weights
                                    .into_iter()
                                    .map(|w| total_amount.to_nau_f64() * w)
                                    .map(|f| NativeCurrencyAmount::try_from(f).unwrap())
                                    .collect_vec();

                                let sum_of_all_but_last = input_amounts
                                    .iter()
                                    .rev()
                                    .skip(1)
                                    .copied()
                                    .sum::<NativeCurrencyAmount>();
                                if let Some(last_input) = input_amounts.last_mut() {
                                    *last_input =
                                        total_amount.checked_sub(&sum_of_all_but_last).unwrap();
                                } else {
                                    total_amount = NativeCurrencyAmount::zero();
                                }

                                let (input_utxos, input_lock_scripts_and_witnesses) =
                                    Self::transaction_inputs_from_address_seeds_and_amounts(
                                        &input_address_seeds,
                                        &input_amounts,
                                    );

                                (None, input_utxos, input_lock_scripts_and_witnesses)
                            };

                        // distribute total amount across outputs
                        let output_denominator =
                            output_dist.iter().map(|u| *u as f64).sum::<f64>() + (fee_dist as f64);
                        let output_weights = output_dist
                            .into_iter()
                            .map(|u| (u as f64) / output_denominator)
                            .collect_vec();
                        let output_amounts = output_weights
                            .into_iter()
                            .map(|w| total_amount.to_nau_f64() * w)
                            .map(|f| NativeCurrencyAmount::try_from(f).unwrap())
                            .collect_vec();
                        let total_outputs =
                            output_amounts.iter().copied().sum::<NativeCurrencyAmount>();
                        let fee = total_amount.checked_sub(&total_outputs).unwrap();
                        let total_inputs = input_utxos
                            .iter()
                            .map(|utxo| utxo.get_native_currency_amount())
                            .sum::<NativeCurrencyAmount>();

                        assert_eq!(
                            maybe_coinbase.unwrap_or(total_inputs),
                            total_outputs + fee,
                            "total outputs: {total_outputs:?} fee: {fee:?}"
                        );

                        let output_utxos = Self::valid_tx_outputs_from_amounts_and_address_seeds(
                            &output_amounts,
                            &output_address_seeds,
                            maybe_coinbase.map(|_| timestamp + MINING_REWARD_TIME_LOCK_PERIOD),
                        );
                        Self::arbitrary_primitive_witness_with_timestamp_and(
                            &input_utxos,
                            &input_lock_scripts_and_witnesses,
                            &output_utxos,
                            &announcements,
                            fee,
                            maybe_coinbase,
                            timestamp,
                            merge_bit,
                        )
                    },
                )
                .boxed()
        }

        pub(crate) fn arbitrary_primitive_witness_with(
            input_utxos: &[Utxo],
            input_lock_scripts_and_witnesses: &[LockScriptAndWitness],
            output_utxos: &[Utxo],
            announcements: &[Announcement],
            fee: NativeCurrencyAmount,
            coinbase: Option<NativeCurrencyAmount>,
        ) -> BoxedStrategy<PrimitiveWitness> {
            let input_utxos = input_utxos.to_vec();
            let input_lock_scripts_and_witnesses = input_lock_scripts_and_witnesses.to_vec();
            let output_utxos = output_utxos.to_vec();
            let announcements = announcements.to_vec();

            let merge_bit = false;
            arb::<Timestamp>()
                .prop_flat_map(move |now| {
                    Self::arbitrary_primitive_witness_with_timestamp_and(
                        &input_utxos,
                        &input_lock_scripts_and_witnesses,
                        &output_utxos,
                        &announcements,
                        fee,
                        coinbase,
                        now,
                        merge_bit,
                    )
                })
                .boxed()
        }

        #[expect(clippy::too_many_arguments)]
        pub(crate) fn arbitrary_primitive_witness_with_timestamp_and(
            input_utxos: &[Utxo],
            input_lock_scripts_and_witnesses: &[LockScriptAndWitness],
            output_utxos: &[Utxo],
            announcements: &[Announcement],
            fee: NativeCurrencyAmount,
            coinbase: Option<NativeCurrencyAmount>,
            timestamp: Timestamp,
            merge_bit: bool,
        ) -> BoxedStrategy<PrimitiveWitness> {
            let num_inputs = input_utxos.len();
            let num_outputs = output_utxos.len();
            let input_utxos = input_utxos.to_vec();
            let output_utxos = output_utxos.to_vec();
            let announcements = announcements.to_vec();
            let input_lock_scripts_and_witnesses = input_lock_scripts_and_witnesses.to_vec();

            // unwrap:
            //  - sender randomness (input)
            //  - receiver preimage (input)
            //  - salt (input)
            //  - sender randomness (output)
            //  - receiver preimage (output)
            //  - salt (output)
            //  - aocl size
            (
                vec(arb::<Digest>(), num_inputs),
                vec(arb::<Digest>(), num_inputs),
                [arb::<BFieldElement>(); 3],
                vec(arb::<Digest>(), num_outputs),
                vec(arb::<Digest>(), num_outputs),
                [arb::<BFieldElement>(); 3],
                0u64..=(u64::MAX >> 1),
            )
                .prop_flat_map(
                    move |(
                        mut sender_randomnesses_input,
                        mut receiver_preimages_input,
                        inputs_salt,
                        output_sender_randomnesses,
                        output_receiver_digests,
                        outputs_salt,
                        aocl_size,
                    )| {
                        let input_triples = input_utxos
                            .iter()
                            .map(|utxo| {
                                (
                                    Tip5::hash(utxo),
                                    sender_randomnesses_input.pop().unwrap(),
                                    receiver_preimages_input.pop().unwrap(),
                                )
                            })
                            .collect_vec();

                        // prepare to unwrap
                        let input_triples = input_triples.clone();
                        let input_lock_scripts_and_witnesses =
                            input_lock_scripts_and_witnesses.clone();
                        let input_utxos = input_utxos.clone();
                        let output_utxos = output_utxos.clone();
                        let announcements = announcements.clone();

                        // unwrap random mutator set accumulator with membership proofs and removal records
                        MsaAndRecords::arbitrary_with((input_triples, aocl_size))
                            .prop_map(move |msa_and_records| {
                                Self::from_msa_and_records(
                                    msa_and_records,
                                    input_utxos.clone(),
                                    input_lock_scripts_and_witnesses.clone(),
                                    output_utxos.clone(),
                                    announcements.clone(),
                                    output_sender_randomnesses.clone(),
                                    output_receiver_digests.clone(),
                                    fee,
                                    coinbase,
                                    timestamp,
                                    inputs_salt,
                                    outputs_salt,
                                    merge_bit,
                                )
                            })
                            .boxed()
                    },
                )
                .boxed()
        }

        #[expect(clippy::too_many_arguments)]
        pub(crate) fn from_msa_and_records(
            msa_and_records: MsaAndRecords,
            input_utxos: Vec<Utxo>,
            input_lock_scripts_and_witnesses: Vec<LockScriptAndWitness>,
            output_utxos: Vec<Utxo>,
            announcements: Vec<Announcement>,
            output_sender_randomnesses: Vec<Digest>,
            output_receiver_digests: Vec<Digest>,
            fee: NativeCurrencyAmount,
            coinbase: Option<NativeCurrencyAmount>,
            timestamp: Timestamp,
            inputs_salt: [BFieldElement; 3],
            outputs_salt: [BFieldElement; 3],
            merge_bit: bool,
        ) -> Self {
            let output_commitments = output_utxos
                .iter()
                .zip(output_sender_randomnesses.clone())
                .zip(output_receiver_digests.clone())
                .map(|((utxo, sender_randomness), receiver_digest)| {
                    UtxoTriple {
                        utxo: utxo.clone(),
                        sender_randomness,
                        receiver_digest,
                    }
                    .addition_record()
                })
                .collect_vec();

            // Removal records are only packed in blocks, never in transactions.
            let input_removal_records = msa_and_records.unpacked_removal_records();
            let mutator_set_accumulator = msa_and_records.mutator_set_accumulator;
            let input_membership_proofs = msa_and_records.membership_proofs;

            let kernel = TransactionKernelProxy {
                inputs: input_removal_records.clone(),
                outputs: output_commitments.clone(),
                announcements: announcements.to_vec(),
                fee,
                coinbase,
                timestamp,
                mutator_set_hash: mutator_set_accumulator.hash(),
                merge_bit,
            }
            .into_kernel();

            let salted_input_utxos = SaltedUtxos {
                utxos: input_utxos.clone(),
                salt: inputs_salt,
            };
            let salted_output_utxos = SaltedUtxos {
                utxos: output_utxos.clone(),
                salt: outputs_salt,
            };

            let native_currency_type_script_witness = NativeCurrencyWitness {
                salted_input_utxos: salted_input_utxos.clone(),
                salted_output_utxos: salted_output_utxos.clone(),
                kernel: kernel.clone(),
            };
            let mut type_scripts_and_witnesses =
                vec![native_currency_type_script_witness.type_script_and_witness()];

            let all_utxos = salted_input_utxos
                .utxos
                .iter()
                .chain(salted_output_utxos.utxos.iter());

            if all_utxos.clone().any(|utxo| utxo.release_date().is_some()) {
                let time_lock_witness = TimeLockWitness::new(
                    kernel.clone(),
                    salted_input_utxos.clone(),
                    salted_output_utxos.clone(),
                );
                type_scripts_and_witnesses.push(time_lock_witness.type_script_and_witness());
            }

            PrimitiveWitness {
                lock_scripts_and_witnesses: input_lock_scripts_and_witnesses.to_owned(),
                input_utxos: salted_input_utxos,
                input_membership_proofs: input_membership_proofs.clone(),
                type_scripts_and_witnesses,
                output_utxos: salted_output_utxos,
                output_sender_randomnesses,
                output_receiver_digests: output_receiver_digests.to_owned(),
                mutator_set_accumulator: mutator_set_accumulator.clone(),
                kernel,
            }
        }

        // this is only used by arbitrary-impls
        pub(crate) fn transaction_inputs_from_address_seeds_and_amounts(
            address_seeds: &[Digest],
            input_amounts: &[NativeCurrencyAmount],
        ) -> (Vec<Utxo>, Vec<LockScriptAndWitness>) {
            let input_lock_scripts_and_witnesses = address_seeds
                .iter()
                .map(|address_seed| {
                    LockScriptAndWitness::genaddr_like_hash_lock_from_seed(*address_seed)
                })
                .collect_vec();

            let input_utxos = input_lock_scripts_and_witnesses
                .iter()
                .zip(input_amounts)
                .map(|(lock_script_and_witness, amount)| {
                    Utxo::new(
                        lock_script_and_witness.program.hash(),
                        amount.to_native_coins(),
                    )
                })
                .collect_vec();
            (input_utxos, input_lock_scripts_and_witnesses)
        }

        /// Obtain a *balanced* set of outputs (and fee) given a fixed total input amount
        /// and (optional) coinbase. This function takes a suggestion for the output
        /// amounts and fee and mutates these values until they satisfy the no-inflation
        /// requirement. This method assumes that the total input amount and coinbase (if
        /// set) can be safely added.
        pub(crate) fn find_balanced_output_amounts_and_fee(
            total_input_amount: NativeCurrencyAmount,
            coinbase: Option<NativeCurrencyAmount>,
            output_amounts_suggestion: &mut [NativeCurrencyAmount],
            fee_suggestion: &mut NativeCurrencyAmount,
        ) {
            assert!(
                coinbase.is_none_or(|x| !x.is_negative()),
                "If coinbase is set, it must be non-negative. Got:\n{coinbase:?}"
            );
            assert!(
                !fee_suggestion.is_negative(),
                "Amount balancer only accepts non-negative fee suggestions. Got:\n{fee_suggestion}"
            );
            assert!(
            !total_input_amount.is_negative(),
            "Amount balancer only accepts non-negative total input amount. Got:\n{total_input_amount}"
        );
            assert!(
            output_amounts_suggestion
                .iter()
                .all(|input_amount_sugg| !input_amount_sugg.is_negative()),
            "Amount balancer only accepts non-negative output amount suggestsions. Got:\n\n{output_amounts_suggestion:?}"
        );
            let mut total_output_amount = output_amounts_suggestion
                .iter()
                .copied()
                .sum::<NativeCurrencyAmount>();
            let total_input_plus_coinbase =
                total_input_amount + coinbase.unwrap_or_else(|| NativeCurrencyAmount::coins(0));
            let mut inflationary = total_output_amount.checked_add(fee_suggestion).is_none()
                || (total_output_amount + *fee_suggestion != total_input_plus_coinbase);
            while inflationary {
                for amount in output_amounts_suggestion.iter_mut() {
                    amount.div_two();
                }
                total_output_amount = output_amounts_suggestion
                    .iter()
                    .copied()
                    .sum::<NativeCurrencyAmount>();
                match total_input_plus_coinbase.checked_sub(&total_output_amount) {
                    Some(number) => {
                        *fee_suggestion = number;
                        inflationary = false;
                    }
                    None => {
                        inflationary = true;
                    }
                }
            }
        }

        /// Generate valid output UTXOs from the amounts and seeds for the
        /// addresses. If some release date is supplied, generate twice as many
        /// UTXOs such that half the total amount is time-locked.
        pub(crate) fn valid_tx_outputs_from_amounts_and_address_seeds(
            output_amounts: &[NativeCurrencyAmount],
            address_seeds: &[Digest],
            timelock_until: Option<Timestamp>,
        ) -> Vec<Utxo> {
            address_seeds
                .iter()
                .zip(output_amounts)
                .flat_map(|(seed, amount)| {
                    let mut amount = *amount;
                    if timelock_until.is_some() {
                        amount.div_two();
                    }
                    let liquid_utxo = Utxo::new(
                        LockScriptAndWitness::genaddr_like_hash_lock_from_seed(*seed)
                            .program
                            .hash(),
                        amount.to_native_coins(),
                    );
                    let mut utxos = vec![liquid_utxo];
                    if let Some(release_date) = timelock_until {
                        let lock_script_hash =
                            LockScriptAndWitness::genaddr_like_hash_lock_from_seed(*seed)
                                .program
                                .hash();
                        let timelocked_utxo = Utxo::new(
                            lock_script_hash,
                            [
                                amount.to_native_coins(),
                                vec![TimeLock::until(release_date)],
                            ]
                            .concat(),
                        );
                        utxos.push(timelocked_utxo);
                    }
                    utxos
                })
                .collect_vec()
        }
    }

    #[cfg(any(test, feature = "test-helpers"))]
    impl Utxo {
        /// returns a new Utxo with properties:
        /// Set the number of NativeCurrencyAmount, overriding the pre-existing number attached
        /// to the type script `NativeCurrency`, or adding a new coin with that amount
        /// and type script hash to the coins list.
        pub fn new_with_native_currency_amount(&self, amount: NativeCurrencyAmount) -> Utxo {
            let mut coins = self.coins().to_vec();
            assert!(
                coins
                    .iter()
                    .filter(|x| x.type_script_hash == NativeCurrency.hash())
                    .count()
                    <= 1,
                "Cannot have repeated native currency coins"
            );
            let new_coin = amount.to_native_coins().first().unwrap().clone();
            if let Some(coin) = coins
                .iter_mut()
                .find(|coin| coin.type_script_hash == NativeCurrency.hash())
            {
                *coin = new_coin;
            } else {
                coins.push(new_coin);
            }

            Utxo::new(self.lock_script_hash(), coins)
        }
    }

    #[cfg(any(test, feature = "test-helpers"))]
    impl PrimitiveWitness {
        /// Arbitrary with: (num inputs, num outputs, num pub announcements)
        pub fn arbitrary_tuple_with_matching_mutator_sets<const N: usize>(
            param_sets: [(usize, usize, usize); N],
        ) -> BoxedStrategy<[PrimitiveWitness; N]> {
            (
                arb::<Option<NativeCurrencyAmount>>(),
                0..N,
                arb::<Timestamp>(),
            )
                .prop_flat_map(
                    move |(mut maybe_coinbase, coinbase_index, coinbase_timestamp)| {
                        // Force coinbase to be non-negative, if set
                        maybe_coinbase = maybe_coinbase.map(|x| x.abs());

                        Self::arbitrary_tuple_with_matching_mutator_sets_and_given_coinbase(
                            param_sets,
                            maybe_coinbase
                                .map(|coinbase| (coinbase, coinbase_index, coinbase_timestamp)),
                        )
                    },
                )
                .boxed()
        }

        /// Arbitrary with:
        /// (num inputs, num outputs, num pub announcements) and optional
        /// coinbase, with timestamp.
        pub fn arbitrary_tuple_with_matching_mutator_sets_and_given_coinbase<const N: usize>(
            param_sets: [(usize, usize, usize); N],
            coinbase_index_timestamp: Option<(NativeCurrencyAmount, usize, Timestamp)>,
        ) -> BoxedStrategy<[PrimitiveWitness; N]> {
            if let Some((_, index, _)) = coinbase_index_timestamp {
                // assert that index lies in the range [0;N)
                assert!(index < N);
            }

            let nested_vec_strategy_digests =
                |counts: [usize; N]| counts.map(|count| vec(arb::<Digest>(), count));
            let nested_vec_strategy_pubann =
                |counts: [usize; N]| counts.map(|count| vec(arb::<Announcement>(), count));
            let nested_vec_strategy_amounts = |counts: [usize; N]| {
                counts.map(|count| vec(NativeCurrencyAmount::arbitrary_non_negative(), count))
            };
            let nested_vec_strategy_utxos =
                |counts: [usize; N]| counts.map(|count| vec(arb::<Utxo>(), count));
            let input_counts: [usize; N] = param_sets.map(|p| p.0);
            let output_counts: [usize; N] = param_sets.map(|p| p.1);
            let announcement_counts: [usize; N] = param_sets.map(|p| p.2);
            let total_num_inputs: usize = input_counts.iter().sum();

            (
                (
                    nested_vec_strategy_amounts(input_counts),
                    nested_vec_strategy_digests(input_counts),
                    nested_vec_strategy_utxos(output_counts),
                    nested_vec_strategy_pubann(announcement_counts),
                    vec(NativeCurrencyAmount::arbitrary_non_negative(), N),
                    vec(arb::<Digest>(), total_num_inputs),
                    vec(arb::<Digest>(), total_num_inputs),
                ),
                // we broke the derive macro of Arbitrary because it only supports
                // tuples of size up to twelve
                (
                    0..(u64::MAX / 2),
                    nested_vec_strategy_digests(output_counts),
                    nested_vec_strategy_digests(output_counts),
                    [arb::<Timestamp>(); N],
                    [arb::<[BFieldElement; 3]>(); N],
                    [arb::<[BFieldElement; 3]>(); N],
                ),
            )
                .prop_flat_map(
                    move |(
                        (
                            input_amountss,
                            input_address_seedss,
                            mut output_utxos,
                            announcements_nested,
                            mut fees,
                            mut input_sender_randomnesses,
                            mut input_receiver_preimages,
                        ),
                        (
                            aocl_size,
                            output_sender_randomnesses_nested,
                            output_receiver_digests_nested,
                            timestamps,
                            inputs_salts,
                            outputs_salts,
                        ),
                    )| {
                        let input_amounts_per_tx: [NativeCurrencyAmount; N] = input_amountss
                            .clone()
                            .map(|amounts| amounts.iter().copied().sum::<NativeCurrencyAmount>());
                        let mut output_utxo_amounts_per_tx = output_utxos.clone().map(|utxos| {
                            utxos
                                .iter()
                                .map(|utxo| utxo.get_native_currency_amount())
                                .collect_vec()
                        });

                        let coinbase = |i: usize| {
                            coinbase_index_timestamp.and_then(|(coinbase, index, _)| {
                                if index == i {
                                    Some(coinbase)
                                } else {
                                    None
                                }
                            })
                        };
                        let timestamps = match coinbase_index_timestamp {
                            None => timestamps,
                            Some((_, _, ts)) => [ts; N],
                        };

                        for i in 0..N {
                            Self::find_balanced_output_amounts_and_fee(
                                input_amounts_per_tx[i],
                                coinbase(i),
                                &mut output_utxo_amounts_per_tx[i],
                                &mut fees[i],
                            );
                        }

                        output_utxos
                            .iter_mut()
                            .zip(output_utxo_amounts_per_tx)
                            .enumerate()
                            .for_each(|(i, (utxos, amounts))| {
                                // If coinbase transaction, then timelock at least half of total
                                // output value.
                                let min_timelocked_cb: NativeCurrencyAmount =
                                    if coinbase(i).is_some() {
                                        let mut min_timelocked_cb: NativeCurrencyAmount =
                                            amounts.iter().copied().sum();
                                        min_timelocked_cb.div_two();
                                        min_timelocked_cb
                                    } else {
                                        NativeCurrencyAmount::zero()
                                    };

                                let mut timelocked_cb_acc = NativeCurrencyAmount::zero();
                                for (utxo, amount) in utxos.iter_mut().zip_eq(amounts) {
                                    *utxo = utxo.new_with_native_currency_amount(amount);
                                    if timelocked_cb_acc < min_timelocked_cb {
                                        // Notice that we're in the general case timelocking more than we have to here.
                                        let max_timestamp = *timestamps.iter().max().unwrap();
                                        *utxo = utxo.clone().with_time_lock(
                                            max_timestamp + MINING_REWARD_TIME_LOCK_PERIOD,
                                        );
                                        timelocked_cb_acc += amount;
                                    }
                                }
                            });

                        let utxos_and_lock_scripts_and_witnesses = input_amountss
                            .iter()
                            .zip_eq(input_address_seedss)
                            .map(|(input_amounts, input_address_seeds)| {
                                Self::transaction_inputs_from_address_seeds_and_amounts(
                                    &input_address_seeds,
                                    input_amounts,
                                )
                            })
                            .collect_vec();
                        let (input_utxoss, input_lock_scripts_and_witnesses): (Vec<_>, Vec<_>) =
                            utxos_and_lock_scripts_and_witnesses.into_iter().unzip();
                        let input_utxoss: [_; N] = input_utxoss.try_into().unwrap();
                        let input_lock_scripts_and_witnesses: [_; N] =
                            input_lock_scripts_and_witnesses.try_into().unwrap();

                        let mut all_input_triples = vec![];
                        for input_utxos in &input_utxoss {
                            for input_utxo in input_utxos {
                                all_input_triples.push((
                                    Tip5::hash(input_utxo),
                                    input_sender_randomnesses.pop().unwrap(),
                                    input_receiver_preimages.pop().unwrap(),
                                ));
                            }
                        }

                        MsaAndRecords::arbitrary_with((all_input_triples, aocl_size))
                            .prop_map(move |msa_and_records| {
                                let split_msa_and_records = msa_and_records.split_by(input_counts);
                                izip!(
                                    0..N,
                                    split_msa_and_records,
                                    timestamps,
                                    announcements_nested.clone(),
                                    output_sender_randomnesses_nested.clone(),
                                    output_receiver_digests_nested.clone(),
                                    fees.clone(),
                                    inputs_salts,
                                    outputs_salts,
                                    input_utxoss.clone(),
                                    input_lock_scripts_and_witnesses.clone(),
                                    output_utxos.clone(),
                                )
                                .map(
                                    |(
                                        index,
                                        msaar,
                                        timestamp,
                                        announcements,
                                        output_sender_randomnesses,
                                        output_receiver_digests,
                                        fee,
                                        inputs_salt,
                                        outputs_salt,
                                        input_utxos,
                                        input_lock_scripts_and_witnesses_,
                                        output_utxos_,
                                    )| {
                                        let maybe_coinbase =
                                            coinbase_index_timestamp.and_then(|(cb, i, _)| {
                                                if index == i {
                                                    Some(cb)
                                                } else {
                                                    None
                                                }
                                            });

                                        let merge_bit = false;
                                        Self::from_msa_and_records(
                                            msaar,
                                            input_utxos,
                                            input_lock_scripts_and_witnesses_,
                                            output_utxos_,
                                            announcements,
                                            output_sender_randomnesses,
                                            output_receiver_digests,
                                            fee,
                                            maybe_coinbase,
                                            timestamp,
                                            inputs_salt,
                                            outputs_salt,
                                            merge_bit,
                                        )
                                    },
                                )
                                .collect_vec()
                                .try_into()
                                .unwrap()
                            })
                            .boxed()
                    },
                )
                .boxed()
        }
    }
}

#[cfg(any(test, feature = "test-helpers"))]
mod test_support {
    use itertools::izip;
    use neptune_mutator_set::msa_and_records::MsaAndRecords;
    use neptune_primitives::network::Network;
    use neptune_primitives::timestamp::Timestamp;
    use num_traits::CheckedAdd;
    use num_traits::CheckedSub;
    use num_traits::Zero;
    use proptest::collection::vec;
    use proptest::prelude::BoxedStrategy;
    use proptest::strategy::Strategy;
    use proptest_arbitrary_interop::arb;

    use super::*;
    use crate::block::MINING_REWARD_TIME_LOCK_PERIOD;
    use crate::proof_abstractions::tasm::program::TritonProgram;
    use crate::transaction::announcement::Announcement;
    use crate::transaction::lock_script::LockScript;
    use crate::transaction::transaction_kernel::TransactionKernelProxy;
    use crate::transaction::utxo_triple::UtxoTriple;
    use crate::type_scripts::native_currency::NativeCurrencyWitness;
    use crate::type_scripts::native_currency_amount::NativeCurrencyAmount;
    use crate::type_scripts::time_lock::TimeLock;
    use crate::type_scripts::time_lock::TimeLockWitness;
    use crate::type_scripts::TypeScriptWitness;

    impl PrimitiveWitness {
        /// Return a primitive witness that's guaranteed to be invalid
        pub fn invalid() -> Self {
            let mutator_set_accumulator = MutatorSetAccumulator::default();
            // An empty (no inputs, no outputs, zero-fee) transaction kernel.
            let kernel = TransactionKernelProxy {
                inputs: vec![],
                outputs: vec![],
                announcements: vec![],
                fee: NativeCurrencyAmount::coins(0),
                coinbase: None,
                timestamp: Network::Main.launch_date(),
                mutator_set_hash: mutator_set_accumulator.hash(),
                merge_bit: false,
            }
            .into_kernel();
            Self {
                input_utxos: SaltedUtxos::empty(),
                input_membership_proofs: vec![],
                lock_scripts_and_witnesses: vec![],
                // Invalid because there is no type script. All transactions
                // must prove to be satisfying the native currency type script.
                type_scripts_and_witnesses: vec![],
                output_utxos: SaltedUtxos::empty(),
                output_sender_randomnesses: vec![],
                output_receiver_digests: vec![],
                mutator_set_accumulator: MutatorSetAccumulator::default(),
                kernel,
            }
        }

        pub fn arbitrary_pair_with_coinbase_and_inputs_respectively(
            num_inputs: usize,
            total_num_outputs: usize,
            total_num_announcements: usize,
        ) -> BoxedStrategy<(Self, Self)> {
            (
                (0..total_num_outputs),
                (0..total_num_announcements),
                NativeCurrencyAmount::arbitrary_non_negative(),
                arb::<Timestamp>(),
            )
                .prop_flat_map(
                    move |(num_outputs, num_announcements, coinbase_amount, timestamp)| {
                        let parameter_sets = [
                            (
                                0,
                                total_num_outputs - num_outputs,
                                total_num_announcements - num_announcements,
                            ),
                            (num_inputs, num_outputs, num_announcements),
                        ];
                        Self::arbitrary_tuple_with_matching_mutator_sets_and_given_coinbase(
                            parameter_sets,
                            Some((coinbase_amount, 0, timestamp)),
                        )
                        .prop_map(|primwit| (primwit[0].clone(), primwit[1].clone()))
                    },
                )
                .boxed()
        }

        pub fn arbitrary_pair_with_coinbase_and_inputs_respectively_from_msa_and_records(
            total_num_outputs: usize,
            total_num_announcements: usize,
            msa_and_records: MsaAndRecords,
            input_utxos: Vec<Utxo>,
            lock_scripts_and_witnesses: Vec<LockScriptAndWitness>,
            coinbase_amount: NativeCurrencyAmount,
            timestamp: Timestamp,
        ) -> BoxedStrategy<(Self, Self)> {
            // Always unpacked in tx-context
            let input_removal_records = msa_and_records.unpacked_removal_records();

            let input_membership_proofs = msa_and_records.membership_proofs;
            let mutator_set_accumulator = msa_and_records.mutator_set_accumulator;
            ((0..total_num_outputs), (0..total_num_announcements))
                .prop_flat_map(move |(num_outputs, num_announcements)| {
                    (
                        Self::arbitrary_given_mutator_set_accumulator_and_inputs(
                            total_num_outputs - num_outputs,
                            total_num_announcements - num_announcements,
                            Some(coinbase_amount),
                            vec![],
                            vec![],
                            vec![],
                            vec![],
                            mutator_set_accumulator.clone(),
                            timestamp,
                        ),
                        Self::arbitrary_given_mutator_set_accumulator_and_inputs(
                            num_outputs,
                            num_announcements,
                            None,
                            input_utxos.clone(),
                            input_removal_records.clone(),
                            input_membership_proofs.clone(),
                            lock_scripts_and_witnesses.clone(),
                            mutator_set_accumulator.clone(),
                            timestamp,
                        ),
                    )
                })
                .boxed()
        }

        #[expect(clippy::too_many_arguments)]
        pub fn arbitrary_given_mutator_set_accumulator_and_inputs(
            num_outputs: usize,
            num_announcements: usize,
            coinbase: Option<NativeCurrencyAmount>,
            input_utxos: Vec<Utxo>,
            input_removal_records: Vec<RemovalRecord>,
            input_membership_proofs: Vec<MsMembershipProof>,
            lock_scripts_and_witnesses: Vec<LockScriptAndWitness>,
            mutator_set_accumulator: MutatorSetAccumulator,
            timestamp: Timestamp,
        ) -> BoxedStrategy<Self> {
            (
                vec(NativeCurrencyAmount::arbitrary_non_negative(), num_outputs),
                NativeCurrencyAmount::arbitrary_non_negative(),
                vec(arb::<Digest>(), num_outputs),
                vec(arb::<Digest>(), num_outputs),
                vec(arb::<Digest>(), num_outputs),
                arb::<[BFieldElement; 3]>(),
                arb::<[BFieldElement; 3]>(),
                vec(arb::<Announcement>(), num_announcements),
            )
                .prop_map(
                    move |(
                        mut output_amounts,
                        mut fee,
                        lock_script_hashes,
                        output_sender_randomnesses,
                        output_receiver_digests,
                        input_salt,
                        output_salt,
                        announcements,
                    )| {
                        let input_amount = input_utxos
                            .iter()
                            .map(|utxo| utxo.get_native_currency_amount())
                            .sum::<NativeCurrencyAmount>();
                        PrimitiveWitness::find_balanced_output_amounts_and_fee(
                            input_amount,
                            coinbase,
                            &mut output_amounts,
                            &mut fee,
                        );

                        assert_eq!(
                            input_amount + coinbase.unwrap_or(NativeCurrencyAmount::from_nau(0)),
                            output_amounts.iter().copied().sum::<NativeCurrencyAmount>() + fee
                        );
                        assert!(!fee.is_negative());

                        let mut output_utxos: Vec<_> = output_amounts
                            .into_iter()
                            .zip(lock_script_hashes)
                            .map(|(amount, lock_script_hash)| {
                                Utxo::new(lock_script_hash, amount.to_native_coins())
                            })
                            .collect_vec();

                        // If coinbase is set, add timelock type script, with
                        // sufficiently long timelock to output UTXOs for at
                        // least half the value.
                        if let Some(coinbase) = coinbase {
                            let release_date = timestamp + MINING_REWARD_TIME_LOCK_PERIOD;
                            let mut timelocked = NativeCurrencyAmount::zero();
                            let mut required_timelocked = coinbase;
                            required_timelocked.div_two();
                            let mut i = 0;
                            while timelocked < required_timelocked {
                                output_utxos[i] =
                                    output_utxos[i].clone().with_time_lock(release_date);
                                timelocked += output_utxos[i].get_native_currency_amount();
                                i += 1;
                            }
                        };

                        let salted_input_utxos = SaltedUtxos {
                            utxos: input_utxos.clone(),
                            salt: input_salt,
                        };
                        let salted_output_utxos = SaltedUtxos {
                            utxos: output_utxos.clone(),
                            salt: output_salt,
                        };

                        let output_addition_records = izip!(
                            output_utxos,
                            output_sender_randomnesses.clone(),
                            output_receiver_digests.clone(),
                        )
                        .map(|(utxo, sender_randomness, receiver_digest)| {
                            UtxoTriple {
                                utxo,
                                sender_randomness,
                                receiver_digest,
                            }
                            .addition_record()
                        })
                        .collect_vec();

                        let kernel = TransactionKernelProxy {
                            inputs: input_removal_records.clone(),
                            outputs: output_addition_records,
                            announcements,
                            fee,
                            coinbase,
                            timestamp,
                            mutator_set_hash: mutator_set_accumulator.hash(),
                            merge_bit: false,
                        }
                        .into_kernel();

                        let mut type_scripts_and_witnesses = vec![NativeCurrencyWitness {
                            salted_input_utxos: salted_input_utxos.clone(),
                            salted_output_utxos: salted_output_utxos.clone(),
                            kernel: kernel.clone(),
                        }
                        .type_script_and_witness()];
                        let type_script_hashes = Utxo::type_script_hashes(
                            salted_input_utxos
                                .utxos
                                .iter()
                                .chain(&salted_output_utxos.utxos),
                        );
                        if type_script_hashes.contains(&TimeLock.hash()) {
                            type_scripts_and_witnesses.push(
                                TimeLockWitness::new(
                                    kernel.clone(),
                                    salted_input_utxos.clone(),
                                    salted_output_utxos.clone(),
                                )
                                .type_script_and_witness(),
                            );
                        }

                        Self {
                            input_utxos: salted_input_utxos,
                            input_membership_proofs: input_membership_proofs.clone(),
                            lock_scripts_and_witnesses: lock_scripts_and_witnesses.clone(),
                            type_scripts_and_witnesses,
                            output_utxos: salted_output_utxos,
                            output_sender_randomnesses,
                            output_receiver_digests,
                            mutator_set_accumulator: mutator_set_accumulator.clone(),
                            kernel,
                        }
                    },
                )
                .boxed()
        }

        /// A strategy for primitive witnesses with 1 input, 2 outputs, and the
        /// given fee. The fee can be negative or even an invalid amount:
        /// greater than the maximum number of nau. It does *not* work for fees
        /// smaller than the minimum number of nau.
        pub fn arbitrary_with_fee(fee: NativeCurrencyAmount) -> BoxedStrategy<Self> {
            let fee_as_i128 = std::convert::TryInto::<i128>::try_into(fee.to_nau()).unwrap();
            let total_amount_strategy =
                match (fee.is_negative(), fee.abs() > NativeCurrencyAmount::max()) {
                    (false, false) => {
                        // positive or zero fee, valid amount
                        // ensure that total amount > fee
                        fee_as_i128..NativeCurrencyAmount::MAX_NAU
                    }
                    (false, true) => {
                        // positive fee, greater than max nau
                        // ensure that total_amount > fee
                        fee_as_i128..i128::MAX
                    }
                    (true, false) => {
                        // negative fee, valid amount
                        // timelocked_amount = -fee/2
                        // liquid_amount = total_amount - timelocked_amount - fee
                        // so:
                        //  * total_amount > timelocked_amount
                        //  * total_amount - timelocked_amount - fee <= NativeCurrencyAmount::max
                        // or rephrased:
                        //  * -fee/2  <  total_amount  <=  NativeCurrencyAmount::max + fee/2
                        // ensure that total_amount - fee < MAX_NAU
                        (-fee_as_i128 >> 1)..(NativeCurrencyAmount::MAX_NAU + fee_as_i128 + 1)
                    }
                    (true, true) => {
                        // negative fee, smaller than min nau
                        // timelocked_amount = -fee/2
                        // liquid_amount = total_amount - timelocked_amount - fee
                        // so:
                        //  * total_amount > timelocked_amount  (otherwise bad sub)
                        //  * total_amount - timelocked_amount - fee <= NativeCurrencyAmount::max  (otherwise bad add)
                        // or rephrased:
                        //  * -fee/2  <  total_amount  <=  NativeCurrencyAmount::max + fee/2
                        // except, this can only work if 0 < NativeCurrencyAmount::max + fee
                        // which would imply that fee was a valid amount. So in
                        // other words, this case should never happen.
                        panic!("fees smaller than minimum amount of nau are not supported");
                    }
                };
            let num_outputs = 2;

            (
                total_amount_strategy,
                arb::<Digest>(),
                vec(arb::<Digest>(), num_outputs),
                arb::<Timestamp>(),
                NativeCurrencyAmount::arbitrary_non_negative(),
            )
                .prop_flat_map(
                    move |(
                        amount,
                        input_address_seed,
                        output_seeds,
                        mut timestamp,
                        extra_amount,
                    )| {
                        while timestamp + MINING_REWARD_TIME_LOCK_PERIOD < timestamp {
                            timestamp = Timestamp::millis(timestamp.to_millis() >> 1);
                        }

                        let total_amount = NativeCurrencyAmount::from_raw_i128(amount);

                        let (input_utxos, input_lock_scripts_and_witnesses) =
                            Self::transaction_inputs_from_address_seeds_and_amounts(
                                &[input_address_seed],
                                &[total_amount],
                            );

                        // populate outputs differently depending on sign of fee
                        let output_utxos = if fee.is_negative() {
                            // If you set a negative fee, then half of the
                            // absolute value of that fee must be time-locked.
                            let mut timelocked_amount = -fee;
                            timelocked_amount.div_two();
                            assert!(total_amount >= timelocked_amount);
                            let timelocked_output = Utxo::new_native_currency(
                                LockScript::standard_hash_lock_from_after_image(output_seeds[0])
                                    .hash(),
                                timelocked_amount,
                            )
                            .with_time_lock(timestamp + MINING_REWARD_TIME_LOCK_PERIOD);

                            let mut liquid_amount =
                                total_amount.checked_sub(&timelocked_amount).unwrap();
                            liquid_amount = liquid_amount.checked_add(&(-fee)).unwrap();
                            let liquid_output = Utxo::new_native_currency(
                                LockScript::standard_hash_lock_from_after_image(output_seeds[0])
                                    .hash(),
                                liquid_amount,
                            );

                            assert_eq!(timelocked_amount + liquid_amount + fee, total_amount);

                            vec![timelocked_output, liquid_output]
                        } else {
                            // positive fee
                            let mut first_amount = extra_amount;
                            while total_amount
                                .checked_sub(&fee)
                                .unwrap()
                                .checked_sub(&first_amount)
                                .is_none()
                            {
                                first_amount.div_two();
                            }
                            let first_output = Utxo::new_native_currency(
                                LockScript::standard_hash_lock_from_after_image(output_seeds[0])
                                    .hash(),
                                first_amount,
                            )
                            .with_time_lock(timestamp + MINING_REWARD_TIME_LOCK_PERIOD);

                            let second_amount = total_amount
                                .checked_sub(&first_amount)
                                .unwrap()
                                .checked_sub(&fee)
                                .unwrap();
                            let second_output = Utxo::new_native_currency(
                                LockScript::standard_hash_lock_from_after_image(output_seeds[1])
                                    .hash(),
                                second_amount,
                            );

                            vec![first_output, second_output]
                        };

                        let merge_bit = false;
                        Self::arbitrary_primitive_witness_with_timestamp_and(
                            &input_utxos,
                            &input_lock_scripts_and_witnesses,
                            &output_utxos,
                            &[],
                            fee,
                            None,
                            timestamp,
                            merge_bit,
                        )
                    },
                )
                .boxed()
        }
    }
}

#[cfg(test)]
#[cfg_attr(coverage_nightly, coverage(off))]
mod tests {

    use itertools::Itertools;
    use macro_rules_attr::apply;
    use neptune_primitives::timestamp::Timestamp;
    use num_traits::CheckedAdd;
    use proptest::collection::vec;
    use proptest::prop_assert;
    use proptest::prop_assert_eq;
    use proptest::strategy::Strategy;
    use proptest::test_runner::TestRunner;
    use proptest_arbitrary_interop::arb;
    use test_strategy::proptest;
    use tracing_test::traced_test;

    use super::*;
    use crate::proof_abstractions::tasm::program::TritonProgram;
    use crate::proof_abstractions::test_runtime::shared_tokio_runtime;
    use crate::type_scripts::native_currency_amount::NativeCurrencyAmount;
    use crate::type_scripts::time_lock::neptune_arbitrary::arbitrary_primitive_witness_with_active_timelocks;
    use crate::type_scripts::time_lock::neptune_arbitrary::arbitrary_primitive_witness_with_expired_timelocks;
    use crate::type_scripts::time_lock::TimeLock;

    #[apply(shared_tokio_runtime)]
    async fn invalid_pw_is_invalid() {
        let pw = PrimitiveWitness::invalid();
        assert!(pw.validate().await.is_err());
    }

    #[traced_test]
    #[apply(shared_tokio_runtime)]
    async fn arbitrary_primitive_witness_tuple_is_valid_deterministic() {
        for num_inputs_a in 0..=2 {
            for num_outputs_a in 0..=2 {
                for num_inputs_b in 0..=2 {
                    for num_outputs_b in 0..=2 {
                        let mut test_runner = TestRunner::deterministic();
                        let [pw1, pw2] =
                            PrimitiveWitness::arbitrary_tuple_with_matching_mutator_sets([
                                (num_inputs_a, num_outputs_a, 0),
                                (num_inputs_b, num_outputs_b, 0),
                            ])
                            .new_tree(&mut test_runner)
                            .unwrap()
                            .current();
                        assert!(pw1.validate().await.is_ok());
                        assert!(pw2.validate().await.is_ok());
                    }
                }
            }
        }
    }

    #[traced_test]
    #[proptest(cases = 10, async = "tokio")]
    async fn updating_primitive_witness_with_ms_data_works(
        // Notice only SingleProof-backed txs need inputs to allow updating, not PW-backed ones.
        #[strategy(0usize..20)] _num_inputs_own: usize,
        #[strategy(0usize..20)] _num_outputs_own: usize,
        #[strategy(0usize..20)] _num_announcements_own: usize,
        #[strategy(0usize..20)] _num_inputs_mined: usize,
        #[strategy(0usize..20)] _num_outputs_mined: usize,
        #[strategy(0usize..20)] _num_announcements_mined: usize,
        #[strategy(PrimitiveWitness::arbitrary_tuple_with_matching_mutator_sets(
            [(#_num_inputs_own, #_num_outputs_own, #_num_announcements_own),
            (#_num_inputs_mined, #_num_outputs_mined, #_num_announcements_mined),],
    ))]
        pws: [PrimitiveWitness; 2],
    ) {
        let [own_pw, mined_pw] = pws;

        prop_assert!(own_pw.validate().await.is_ok());
        prop_assert!(mined_pw.validate().await.is_ok());

        let ms_update = MutatorSetUpdate::new(
            mined_pw.kernel.inputs.clone(),
            mined_pw.kernel.outputs.clone(),
        );
        let updated_pw = PrimitiveWitness::update_with_new_ms_data(own_pw, ms_update);

        prop_assert!(updated_pw.validate().await.is_ok());
    }

    #[traced_test]
    #[apply(shared_tokio_runtime)]
    async fn arb_is_valid_unit_test_small() {
        for num_inputs in 0..=2 {
            for num_outputs in 0..=2 {
                for num_announcements in 0..=2 {
                    let mut test_runner = TestRunner::deterministic();
                    let primitive_witness = PrimitiveWitness::arbitrary_with_size_numbers(
                        Some(num_inputs),
                        num_outputs,
                        num_announcements,
                    )
                    .new_tree(&mut test_runner)
                    .unwrap()
                    .current();
                    assert!(primitive_witness.validate().await.is_ok());
                }
            }
        }
    }

    #[proptest(cases = 5, async = "tokio")]
    async fn lock_script_failure_negative_test(
        #[strategy(3usize..10)] _num_inputs: usize,
        #[strategy(3usize..10)] _num_outputs: usize,
        #[strategy(3usize..10)] _num_announcements: usize,
        #[strategy(0usize..#_num_inputs)] mutated_lockscript_witness: usize,
        #[strategy(arb())] bad_preimage: Digest,
        #[strategy(PrimitiveWitness::arbitrary_with_size_numbers(Some(#_num_inputs), #_num_outputs, #_num_announcements
        ))]
        mut transaction_primitive_witness: PrimitiveWitness,
    ) {
        // Assumes that the witness for lock scripts live in `nd_tokens`
        transaction_primitive_witness.lock_scripts_and_witnesses[mutated_lockscript_witness]
            .set_nd_tokens(bad_preimage.values().to_vec());

        prop_assert!(transaction_primitive_witness.validate().await.is_err());
    }

    #[proptest(cases = 5, async = "tokio")]
    async fn type_script_failure_negative_test(
        #[strategy(3usize..10)] _num_inputs: usize,
        #[strategy(3usize..10)] _num_outputs: usize,
        #[strategy(3usize..10)] _num_announcements: usize,
        #[strategy(arb())] rng_seed: u64,
        #[strategy(PrimitiveWitness::arbitrary_with_size_numbers(Some(#_num_inputs), #_num_outputs, #_num_announcements
        ))]
        mut transaction_primitive_witness: PrimitiveWitness,
    ) {
        // Mess up witness data for one of the type scripts, assumed to be the
        // native currency type script. But actually doesn't matter which one it
        // is as the goal is simply to get one of the type scripts to fail.
        transaction_primitive_witness.type_scripts_and_witnesses[0]
            .scramble_non_determinism(rng_seed);
        prop_assert!(transaction_primitive_witness.validate().await.is_err());
    }

    #[proptest(cases = 5, async = "tokio")]
    async fn arbitrary_transaction_is_valid(
        #[strategy(3usize..10)] _num_inputs: usize,
        #[strategy(3usize..10)] _num_outputs: usize,
        #[strategy(3usize..10)] _num_announcements: usize,
        #[strategy(PrimitiveWitness::arbitrary_with_size_numbers(Some(#_num_inputs), #_num_outputs, #_num_announcements
        ))]
        transaction_primitive_witness: PrimitiveWitness,
    ) {
        prop_assert!(transaction_primitive_witness.validate().await.is_ok());
    }

    #[proptest(cases = 3, async = "tokio")]
    async fn not_valid_with_active_timelocks(
        #[strategy(1usize..10)] _num_inputs: usize,
        #[strategy(1usize..10)] _num_outputs: usize,
        #[strategy(1usize..10)] _num_public_announcements: usize,
        #[strategy(arb())] _now: Timestamp,
        #[strategy(arbitrary_primitive_witness_with_active_timelocks(#_num_inputs, #_num_outputs, #_num_public_announcements, #_now
        ))]
        primitive_witness: PrimitiveWitness,
    ) {
        let Err(err) = primitive_witness.validate().await else {
            panic!("Must fail when timelocks are active");
        };

        let WitnessValidationError::InvalidTypeScript {
            type_script_hash, ..
        } = err
        else {
            panic!("Must fail during type script validation");
        };

        prop_assert_eq!(
            TimeLock.hash(),
            type_script_hash,
            "Must fail in time lock type script"
        );
    }

    #[proptest(cases = 3, async = "tokio")]
    async fn timelock_witness_present_with_timelock_typescript_in_inputs(
        #[strategy(3usize..10)] _num_inputs: usize,
        #[strategy(3usize..10)] _num_outputs: usize,
        #[strategy(3usize..10)] _num_public_announcements: usize,
        #[strategy(arb())] _now: Timestamp,
        #[strategy(arbitrary_primitive_witness_with_expired_timelocks(#_num_inputs, #_num_outputs, #_num_public_announcements, #_now
        ))]
        primitive_witness: PrimitiveWitness,
    ) {
        prop_assert!(primitive_witness.validate().await.is_ok());
        prop_assert!(
            primitive_witness
                .type_scripts_and_witnesses
                .iter()
                .map(|ts_and_witness| ts_and_witness.program.hash())
                .any(|ts_digest| ts_digest == TimeLock.hash()),
            "Type scripts witness must contain timelock"
        );
    }

    #[proptest]
    fn amounts_balancer_works_with_coinbase(
        #[strategy(NativeCurrencyAmount::arbitrary_non_negative())]
        total_input_amount: NativeCurrencyAmount,
        #[strategy(NativeCurrencyAmount::arbitrary_non_negative())] coinbase: NativeCurrencyAmount,
        #[strategy(vec(NativeCurrencyAmount::arbitrary_non_negative(), 1..4))]
        mut output_amounts: Vec<NativeCurrencyAmount>,
        #[strategy(NativeCurrencyAmount::arbitrary_non_negative())] mut fee: NativeCurrencyAmount,
    ) {
        PrimitiveWitness::find_balanced_output_amounts_and_fee(
            total_input_amount,
            Some(coinbase),
            &mut output_amounts,
            &mut fee,
        );
        prop_assert!(
            total_input_amount.checked_add(&coinbase).unwrap()
                == output_amounts
                    .iter()
                    .copied()
                    .sum::<NativeCurrencyAmount>()
                    .checked_add(&fee)
                    .unwrap()
        );
    }

    #[proptest]
    fn amounts_balancer_works_without_coinbase(
        #[strategy(NativeCurrencyAmount::arbitrary_non_negative())]
        total_input_amount: NativeCurrencyAmount,
        #[strategy(vec(NativeCurrencyAmount::arbitrary_non_negative(), 1..4))]
        mut output_amounts: Vec<NativeCurrencyAmount>,
        #[strategy(NativeCurrencyAmount::arbitrary_non_negative())] mut fee: NativeCurrencyAmount,
    ) {
        PrimitiveWitness::find_balanced_output_amounts_and_fee(
            total_input_amount,
            None,
            &mut output_amounts,
            &mut fee,
        );
        prop_assert!(
            total_input_amount
                == output_amounts
                    .iter()
                    .copied()
                    .sum::<NativeCurrencyAmount>()
                    .checked_add(&fee)
                    .unwrap()
        );
    }

    #[proptest(cases = 5)]
    fn total_amount_is_valid(
        #[strategy(PrimitiveWitness::arbitrary_with_size_numbers(Some(2), 2, 2,))]
        primitive_witness: PrimitiveWitness,
    ) {
        let mut total = if let Some(amount) = primitive_witness.kernel.coinbase {
            amount
        } else {
            NativeCurrencyAmount::coins(0)
        };
        for input in primitive_witness.input_utxos.utxos {
            let u32s = input.coins()[0]
                .state
                .iter()
                .map(|b| b.value() as u32)
                .collect_vec();
            let amount = u128::from(u32s[0])
                | (u128::from(u32s[1]) << 32)
                | (u128::from(u32s[2]) << 64)
                | (u128::from(u32s[3]) << 96);
            total += NativeCurrencyAmount::from_nau(amount.try_into().unwrap());
        }
        prop_assert!(total <= NativeCurrencyAmount::coins(42000000));
    }
}