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
//! Miniscript Arithmetic expressions:
//! Note that this fragment is only supported for Tapscript context
use std::convert::TryInto;
use std::str::FromStr;
use std::{cmp, error, fmt};

use bitcoin::key::XOnlyPublicKey;
use bitcoin_miniscript::MiniscriptKey;
use elements::opcodes::all::*;
use elements::sighash::Prevouts;
use elements::{opcodes, script, secp256k1_zkp as secp256k1, SchnorrSig, Transaction};

use super::param::{ExtParamTranslator, TranslateExtParam};
use super::{CovExtArgs, CsfsKey, ExtParam, FromTokenIterError, IdxExpr, ParseableExt, TxEnv};
use crate::expression::{FromTree, Tree};
use crate::extensions::check_sig_price_oracle_1;
use crate::miniscript::context::ScriptContextError;
use crate::miniscript::lex::{Token as Tk, TokenIter};
use crate::miniscript::limits::MAX_STANDARD_P2WSH_STACK_ITEM_SIZE;
use crate::miniscript::satisfy::{Satisfaction, Witness};
use crate::miniscript::types::extra_props::{OpLimits, TimelockInfo};
use crate::miniscript::types::{Base, Correctness, Dissat, ExtData, Input, Malleability};
use crate::{
    expression, interpreter, miniscript, script_num_size, Error, Extension, Satisfier, ToPublicKey,
    TranslateExt,
};

/// Enum representing arithmetic operations with transaction amounts.
/// Every variant of this enum pushes a single singed 64 bit BE number on stack top.
/// All of introspection opcodes explicitly assert the amount is explicit.
///
/// This will abort when
///     - Any of operations are on confidential amounts. The Null case is automatically
///       converted to explicit zero.
///     - Supplied index is out of bounds.
///     - Any of the operations overflow. Refer to tapscript opcodes spec for overflow specification
///     - In extreme cases, when recursive operations exceed 400 depth
#[derive(Debug, Eq, PartialEq, Ord, PartialOrd, Hash, Clone)]
pub enum ExprInner<T: ExtParam> {
    /* leaf fragments/terminals */
    /// A constant i64 value
    /// Minimal push of this `<i64>`
    Const(i64),
    /// Value under the current executing input
    /// `INSPECTCURRENTINPUTINDEX INPSECTINPUTVALUE <1> EQUALVERIFY`
    CurrInputIdx,
    /// Explicit amount at the given input index
    /// `i INPSECTINPUTVALUE <1> EQUALVERIFY`
    Input(IdxExpr),
    /// Explicit amount at the given output index
    /// `i INPSECTOUTPUTVALUE <1> EQUALVERIFY`
    Output(IdxExpr),
    /// Explicit issuance amount at this input index
    /// `i OP_INSPECTINPUTISSUANCE DROP DROP <1> EQUALVERIFY NIP NIP`
    // NIP drops the second to top stack item
    // issuance stack after push where the right is stack top
    // [<inflation keys> <inflation_pref> <value> <value_pref> <entropy> <blindingnonce>]
    InputIssue(IdxExpr),
    /// Explicit re-issuance amount at this input index
    /// `i OP_INSPECTINPUTISSUANCE DROP DROP DROP DROP <1> EQUALVERIFY`
    // issuance stack after push where the right is stack top
    // [<inflation keys> <inflation_pref> <value> <value_pref> <entropy> <blindingnonce>]
    InputReIssue(IdxExpr),

    /* Two children */
    /// Add two Arith expressions.
    /// `[X] [Y] ADD64 <1> EQUALVERIFY`
    Add(Box<Expr<T>>, Box<Expr<T>>),
    /// Subtract (X-Y)
    /// `[X] [Y] SUB64 <1> EQUALVERIFY`
    Sub(Box<Expr<T>>, Box<Expr<T>>),
    /// Multiply two Expr expressions. (a*b)
    /// `[X] [Y] MUL64 <1> EQUALVERIFY`
    Mul(Box<Expr<T>>, Box<Expr<T>>),
    /// Divide two Expr expressions. (a//b)
    /// The division operation pushes the quotient(a//b) such that the remainder a%b
    /// (must be non-negative and less than |b|).
    /// `[X] [Y] DIV64 <1> EQUALVERIFY NIP`
    Div(Box<Expr<T>>, Box<Expr<T>>),
    /// Modulo operation (a % b)
    /// The division operation the remainder a%b (must be non-negative and less than |b|).
    /// `[X] [Y] DIV64 <1> EQUALVERIFY DROP`
    Mod(Box<Expr<T>>, Box<Expr<T>>),
    /// BitWise And (a & b)
    /// `[X] [Y] AND` (cannot fail)
    BitAnd(Box<Expr<T>>, Box<Expr<T>>),
    /// BitWise or (a | b)
    /// `[X] [Y] OR` (cannot fail)
    BitOr(Box<Expr<T>>, Box<Expr<T>>),
    /// BitWise or (a ^ b)
    /// `[X] [Y] XOR` (cannot fail)
    Xor(Box<Expr<T>>, Box<Expr<T>>),
    /* One child*/
    /// BitWise invert (!a)
    /// `[X] INVERT` (cannot fail)
    Invert(Box<Expr<T>>),
    /// Negate -a
    /// `[X] NEG64 <1> EQUALVERIFY`
    Negate(Box<Expr<T>>),

    /// Push the price as LE64 signed from oracle.
    /// `2DUP TOALTSTACK <T> OP_GREATERTHANEQ VERIFY CAT SHA256 <K> CHECKSIGFROMSTACKVERIFY OP_FROMATLSTACK`
    /// The fragment checks that the input timestamp is less than time at which the price was signed with
    /// the given oracle key. The asset of which price is being checked is implicitly decided by the
    /// public key
    PriceOracle1(T, u64),
    /// Same as [`Self::PriceOracle1`] but wrapped in an `TOALTSTACK` and `FROMALTSTACK` and SWAP
    /// `TOALTSTACK 2DUP TOALTSTACK <T> OP_GREATERTHANEQ VERIFY CAT SHA256 <K> CHECKSIGFROMSTACKVERIFY OP_FROMATLSTACK FROMALTSTACK SWAP`
    /// We need to swap at the end to make sure that the price pushed by this fragment is on top of the stack
    /// In regular miniscript, all operations are commutative, but here some operations like sub and div are not and hence
    /// we need to maintain the exact order of operations.
    PriceOracle1W(T, u64),
}

/// An iterator over [`ExprInner`] that yields the terminal nodes
/// in the expression tree.
#[derive(Debug, Clone)]
pub struct ExprIter<'a, T: ExtParam> {
    stack: Vec<&'a ExprInner<T>>,
}

impl<'a, T: ExtParam> Iterator for ExprIter<'a, T> {
    type Item = &'a ExprInner<T>;

    fn next(&mut self) -> Option<Self::Item> {
        while let Some(expr) = self.stack.pop() {
            match expr {
                ExprInner::Const(_)
                | ExprInner::CurrInputIdx
                | ExprInner::Input(_)
                | ExprInner::Output(_)
                | ExprInner::InputIssue(_)
                | ExprInner::InputReIssue(_)
                | ExprInner::PriceOracle1(_, _)
                | ExprInner::PriceOracle1W(_, _) => return Some(expr),
                ExprInner::Add(a, b)
                | ExprInner::Sub(a, b)
                | ExprInner::Mul(a, b)
                | ExprInner::Div(a, b)
                | ExprInner::Mod(a, b)
                | ExprInner::BitAnd(a, b)
                | ExprInner::BitOr(a, b)
                | ExprInner::Xor(a, b) => {
                    self.stack.push(b.as_inner());
                    self.stack.push(a.as_inner());
                }
                ExprInner::Invert(a) | ExprInner::Negate(a) => {
                    self.stack.push(a.as_inner());
                }
            }
        }
        None
    }
}

/// [`ExprInner`] with some values cached
#[derive(Eq, PartialEq, Ord, PartialOrd, Hash, Clone)]
pub struct Expr<T: ExtParam> {
    /// The actual inner expression
    inner: ExprInner<T>,
    /// The cached script size
    script_size: usize,
    /// depth of expression thunk/tree
    depth: usize,
}

impl<T: ExtParam> Expr<T> {
    /// Obtains the inner
    pub fn into_inner(self) -> ExprInner<T> {
        self.inner
    }

    /// Obtains the reference to inner
    pub fn as_inner(&self) -> &ExprInner<T> {
        &self.inner
    }

    /// Obtains the script size
    pub fn script_size(&self) -> usize {
        self.script_size
    }

    /// Obtains the depth of this expression thunk
    pub fn depth(&self) -> usize {
        self.depth
    }

    /// Creates [`Expr`] from [`ExprInner`]
    pub fn from_inner(inner: ExprInner<T>) -> Self {
        let (script_size, depth) = match &inner {
            ExprInner::Const(_c) => (8 + 1, 0),
            ExprInner::CurrInputIdx => (4, 0), // INSPECTCURRENTINPUTINDEX INPSECTINPUTVALUE <1> EQUALVERIFY
            ExprInner::Input(i) => (
                i.script_size() + 3, // i INPSECTINPUTVALUE <1> EQUALVERIFY
                0,
            ),
            ExprInner::Output(i) => (
                i.script_size() + 3, // i INPSECTOUTPUTVALUE <1> EQUALVERIFY
                0,
            ),
            ExprInner::InputIssue(i) => (
                i.script_size() + 7, // i OP_INSPECTINPUTISSUANCE DROP DROP <1> EQUALVERIFY NIP NIP
                0,
            ),
            ExprInner::InputReIssue(i) => (
                i.script_size() + 7, // i OP_INSPECTINPUTISSUANCE DROP DROP DROP DROP <1> EQUALVERIFY
                0,
            ),
            ExprInner::Add(x, y) => (
                x.script_size + y.script_size + 3, // [X] [Y] ADD64 <1> EQUALVERIFY
                cmp::max(x.depth, y.depth),
            ),
            ExprInner::Sub(x, y) => (
                x.script_size + y.script_size + 3, // [X] [Y] SUB64 <1> EQUALVERIFY
                cmp::max(x.depth, y.depth),
            ),
            ExprInner::Mul(x, y) => (
                x.script_size + y.script_size + 3, // [X] [Y] MUL64 <1> EQUALVERIFY
                cmp::max(x.depth, y.depth),
            ),
            ExprInner::Div(x, y) => (
                x.script_size + y.script_size + 4, // [X] [Y] DIV64 <1> EQUALVERIFY NIP
                cmp::max(x.depth, y.depth),
            ),
            ExprInner::Mod(x, y) => (
                x.script_size + y.script_size + 4, // [X] [Y] DIV64 <1> EQUALVERIFY DROP
                cmp::max(x.depth, y.depth),
            ),
            ExprInner::BitAnd(x, y) => (
                x.script_size + y.script_size + 1, // [X] [Y] AND
                cmp::max(x.depth, y.depth),
            ),
            ExprInner::BitOr(x, y) => (
                x.script_size + y.script_size + 1, // [X] [Y] OR
                cmp::max(x.depth, y.depth),
            ),
            ExprInner::Xor(x, y) => (
                x.script_size + y.script_size + 1, // [X] [Y] XOR
                cmp::max(x.depth, y.depth),
            ),
            ExprInner::Invert(x) => (
                x.script_size + 1, // [X] INVERT
                x.depth + 1,
            ),
            ExprInner::Negate(x) => (
                x.script_size + 3, // [X] NEG64 <1> EQUALVERIFY
                x.depth + 1,
            ),
            ExprInner::PriceOracle1(_pk, _time) => (
                (32 + 1) // 32 byte key + push
                + (8 + 1) // 8 byte time push
                + 8, // opcodes,
                0,
            ),
            ExprInner::PriceOracle1W(_pk, _time) => (
                (32 + 1) // 32 byte key + push
                + (8 + 1) // 8 byte time push
                + 11, // opcodes,
                0,
            ),
        };
        Self {
            inner,
            script_size,
            depth,
        }
    }

    /// Obtains an iterator over terminals nodes
    pub fn iter_terminals(&self) -> impl Iterator<Item = &ExprInner<T>> {
        ExprIter {
            stack: vec![&self.inner],
        }
    }
}

/// Type Check errors in [`Expr`]
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum TypeError {
    /// PriceOracle1W is the first element in the expression
    PriceOracle1WFirst,
    /// PriceOracle1 is *not* the first element in the expression
    PriceOracle1Missing,
}

impl std::fmt::Display for TypeError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            TypeError::PriceOracle1WFirst => {
                write!(f, "PriceOracle1W is the first element in the expression")
            }
            TypeError::PriceOracle1Missing => write!(
                f,
                "PriceOracle1 is *not* the first element in the expression"
            ),
        }
    }
}

impl std::error::Error for TypeError {}

impl Expr<CovExtArgs> {
    /// Evaluate this expression
    fn eval(&self, env: &TxEnv, s: &mut interpreter::Stack) -> Result<i64, EvalError> {
        match &self.inner {
            ExprInner::Const(c) => Ok(*c),
            ExprInner::CurrInputIdx => {
                if env.idx >= env.spent_utxos.len() {
                    return Err(EvalError::UtxoIndexOutOfBounds(
                        env.idx,
                        env.spent_utxos.len(),
                    ));
                }
                env.spent_utxos[env.idx]
                    .value
                    .explicit()
                    .map(|x| x as i64) // safe conversion bitcoin values from u64 to i64 because 21 mil
                    .ok_or(EvalError::NonExplicitInput(env.idx))
            }
            ExprInner::Input(i) => {
                let i = i.eval(env)?;
                if i >= env.spent_utxos.len() {
                    return Err(EvalError::UtxoIndexOutOfBounds(i, env.spent_utxos.len()));
                }
                env.spent_utxos[i]
                    .value
                    .explicit()
                    .map(|x| x as i64) // safe conversion bitcoin values from u64 to i64 because 21 mil
                    .ok_or(EvalError::NonExplicitInput(i))
            }
            ExprInner::Output(i) => {
                let i = i.eval(env)?;
                if i >= env.tx.output.len() {
                    return Err(EvalError::OutputIndexOutOfBounds(i, env.tx.output.len()));
                }
                env.tx.output[i]
                    .value
                    .explicit()
                    .map(|x| x as i64) // safe conversion bitcoin values from u64 to i64 because 21 mil
                    .ok_or(EvalError::NonExplicitOutput(i))
            }
            ExprInner::InputIssue(i) => {
                let i = i.eval(env)?;
                if i >= env.tx.input.len() {
                    return Err(EvalError::InputIndexOutOfBounds(i, env.tx.input.len()));
                }
                env.tx.input[i]
                    .asset_issuance
                    .amount
                    .explicit()
                    .map(|x| x as i64) // safe conversion bitcoin values from u64 to i64 because 21 mil
                    .ok_or(EvalError::NonExplicitInputIssuance(i))
            }
            ExprInner::InputReIssue(i) => {
                let i = i.eval(env)?;
                if i >= env.tx.input.len() {
                    return Err(EvalError::InputIndexOutOfBounds(i, env.tx.input.len()));
                }
                env.tx.input[i]
                    .asset_issuance
                    .inflation_keys
                    .explicit()
                    .map(|x| x as i64) // safe conversion bitcoin values from u64 to i64 because 21 mil
                    .ok_or(EvalError::NonExplicitInputReIssuance(i))
            }
            ExprInner::Add(x, y) => {
                let x = x.eval(env, s)?;
                let y = y.eval(env, s)?;
                x.checked_add(y).ok_or(EvalError::AddOverflow(x, y))
            }
            ExprInner::Sub(x, y) => {
                let x = x.eval(env, s)?;
                let y = y.eval(env, s)?;
                x.checked_sub(y).ok_or(EvalError::SubOverflow(x, y))
            }
            ExprInner::Mul(x, y) => {
                let x = x.eval(env, s)?;
                let y = y.eval(env, s)?;
                x.checked_mul(y).ok_or(EvalError::MulOverflow(x, y))
            }
            ExprInner::Div(x, y) => {
                let x = x.eval(env, s)?;
                let y = y.eval(env, s)?;
                x.checked_div_euclid(y).ok_or(EvalError::DivOverflow(x, y))
            }
            ExprInner::Mod(x, y) => {
                let x = x.eval(env, s)?;
                let y = y.eval(env, s)?;
                x.checked_rem_euclid(y).ok_or(EvalError::ModOverflow(x, y))
            }
            ExprInner::BitAnd(x, y) => {
                let x = x.eval(env, s)?;
                let y = y.eval(env, s)?;
                Ok(x & y)
            }
            ExprInner::BitOr(x, y) => {
                let x = x.eval(env, s)?;
                let y = y.eval(env, s)?;
                Ok(x | y)
            }
            ExprInner::Xor(x, y) => {
                let x = x.eval(env, s)?;
                let y = y.eval(env, s)?;
                Ok(x ^ y)
            }
            ExprInner::Invert(x) => {
                let x = x.eval(env, s)?;
                Ok(!x)
            }
            ExprInner::Negate(x) => {
                let x = x.eval(env, s)?;
                x.checked_neg().ok_or(EvalError::NegOverflow(x))
            }
            ExprInner::PriceOracle1(pk, timestamp) | ExprInner::PriceOracle1W(pk, timestamp) => {
                let x_only_pk = if let CovExtArgs::XOnlyKey(pk) = pk {
                    pk.0
                } else {
                    unreachable!("Construction ensures that Param is only of type XOnlyKey")
                };
                let price = s.pop().ok_or(EvalError::MissingPrice)?;
                let price = price.try_push().map_err(|_| EvalError::Price8BytePush)?;
                let price_u64 =
                    u64::from_le_bytes(price.try_into().map_err(|_| EvalError::Price8BytePush)?);

                let time_signed = s.pop().ok_or(EvalError::MissingTimestamp)?;
                let time_signed = time_signed
                    .try_push()
                    .map_err(|_| EvalError::Timstamp8BytePush)?;
                let time_signed_u64 = u64::from_le_bytes(
                    time_signed
                        .try_into()
                        .map_err(|_| EvalError::Timstamp8BytePush)?,
                );
                let sig = s.pop().ok_or(EvalError::MissingOracleSignature)?;
                let schnorr_sig_sl = sig.try_push().map_err(|_| EvalError::MalformedSig)?;
                let schnorr_sig = secp256k1::schnorr::Signature::from_slice(schnorr_sig_sl)
                    .map_err(|_| EvalError::MalformedSig)?;
                let secp = secp256k1::Secp256k1::verification_only();

                if *timestamp < time_signed_u64 {
                    return Err(EvalError::TimestampInFuture);
                }

                if check_sig_price_oracle_1(&secp, &schnorr_sig, &x_only_pk, *timestamp, price_u64)
                {
                    let price_i64 =
                        u64::try_into(price_u64).map_err(|_| EvalError::PriceOverflow)?;
                    Ok(price_i64)
                } else {
                    Err(EvalError::InvalidSignature)
                }
            }
        }
    }

    /// Evaluate this expression
    fn satisfy<Pk: MiniscriptKey + ToPublicKey>(
        &self,
        env: &TxEnv,
        s: &dyn Satisfier<Pk>,
    ) -> Result<(i64, Satisfaction), EvalError> {
        match &self.inner {
            ExprInner::Const(c) => Ok((*c, Satisfaction::empty())),
            ExprInner::CurrInputIdx => {
                if env.idx >= env.spent_utxos.len() {
                    return Err(EvalError::UtxoIndexOutOfBounds(
                        env.idx,
                        env.spent_utxos.len(),
                    ));
                }
                let res = env.spent_utxos[env.idx]
                    .value
                    .explicit()
                    .map(|x| x as i64) // safe conversion bitcoin values from u64 to i64 because 21 mil
                    .ok_or(EvalError::NonExplicitInput(env.idx))?;
                Ok((res, Satisfaction::empty()))
            }
            ExprInner::Input(i) => {
                let i = i.eval(env)?;
                if i >= env.spent_utxos.len() {
                    return Err(EvalError::UtxoIndexOutOfBounds(i, env.spent_utxos.len()));
                }
                let res = env.spent_utxos[i]
                    .value
                    .explicit()
                    .map(|x| x as i64) // safe conversion bitcoin values from u64 to i64 because 21 mil
                    .ok_or(EvalError::NonExplicitInput(i))?;
                Ok((res, Satisfaction::empty()))
            }
            ExprInner::Output(i) => {
                let i = i.eval(env)?;
                if i >= env.tx.output.len() {
                    return Err(EvalError::OutputIndexOutOfBounds(i, env.tx.output.len()));
                }
                let res = env.tx.output[i]
                    .value
                    .explicit()
                    .map(|x| x as i64) // safe conversion bitcoin values from u64 to i64 because 21 mil
                    .ok_or(EvalError::NonExplicitOutput(i))?;
                Ok((res, Satisfaction::empty()))
            }
            ExprInner::InputIssue(i) => {
                let i = i.eval(env)?;
                if i >= env.tx.input.len() {
                    return Err(EvalError::InputIndexOutOfBounds(i, env.tx.input.len()));
                }
                let res = env.tx.input[i]
                    .asset_issuance
                    .amount
                    .explicit()
                    .map(|x| x as i64) // safe conversion bitcoin values from u64 to i64 because 21 mil
                    .ok_or(EvalError::NonExplicitInputIssuance(i))?;
                Ok((res, Satisfaction::empty()))
            }
            ExprInner::InputReIssue(i) => {
                let i = i.eval(env)?;
                if i >= env.tx.input.len() {
                    return Err(EvalError::InputIndexOutOfBounds(i, env.tx.input.len()));
                }
                let res = env.tx.input[i]
                    .asset_issuance
                    .inflation_keys
                    .explicit()
                    .map(|x| x as i64) // safe conversion bitcoin values from u64 to i64 because 21 mil
                    .ok_or(EvalError::NonExplicitInputReIssuance(i))?;
                Ok((res, Satisfaction::empty()))
            }
            ExprInner::Add(x, y) => {
                let (x, sat_x) = x.satisfy(env, s)?;
                let (y, sat_y) = y.satisfy(env, s)?;
                let res = x.checked_add(y).ok_or(EvalError::AddOverflow(x, y))?;
                let sat = Satisfaction::combine(sat_y, sat_x);
                Ok((res, sat))
            }
            ExprInner::Sub(x, y) => {
                let (x, sat_x) = x.satisfy(env, s)?;
                let (y, sat_y) = y.satisfy(env, s)?;
                let res = x.checked_sub(y).ok_or(EvalError::SubOverflow(x, y))?;
                let sat = Satisfaction::combine(sat_y, sat_x);
                Ok((res, sat))
            }
            ExprInner::Mul(x, y) => {
                let (x, sat_x) = x.satisfy(env, s)?;
                let (y, sat_y) = y.satisfy(env, s)?;
                let res = x.checked_mul(y).ok_or(EvalError::MulOverflow(x, y))?;
                let sat = Satisfaction::combine(sat_y, sat_x);
                Ok((res, sat))
            }
            ExprInner::Div(x, y) => {
                let (x, sat_x) = x.satisfy(env, s)?;
                let (y, sat_y) = y.satisfy(env, s)?;
                let res = x
                    .checked_div_euclid(y)
                    .ok_or(EvalError::DivOverflow(x, y))?;
                let sat = Satisfaction::combine(sat_y, sat_x);
                Ok((res, sat))
            }
            ExprInner::Mod(x, y) => {
                let (x, sat_x) = x.satisfy(env, s)?;
                let (y, sat_y) = y.satisfy(env, s)?;
                let res = x
                    .checked_rem_euclid(y)
                    .ok_or(EvalError::ModOverflow(x, y))?;
                let sat = Satisfaction::combine(sat_y, sat_x);
                Ok((res, sat))
            }
            ExprInner::BitAnd(x, y) => {
                let (x, sat_x) = x.satisfy(env, s)?;
                let (y, sat_y) = y.satisfy(env, s)?;
                let sat = Satisfaction::combine(sat_y, sat_x);
                Ok((x & y, sat))
            }
            ExprInner::BitOr(x, y) => {
                let (x, sat_x) = x.satisfy(env, s)?;
                let (y, sat_y) = y.satisfy(env, s)?;
                let sat = Satisfaction::combine(sat_y, sat_x);
                Ok((x | y, sat))
            }
            ExprInner::Xor(x, y) => {
                let (x, sat_x) = x.satisfy(env, s)?;
                let (y, sat_y) = y.satisfy(env, s)?;
                let sat = Satisfaction::combine(sat_y, sat_x);
                Ok((x ^ y, sat))
            }
            ExprInner::Invert(x) => {
                let (x, sat_x) = x.satisfy(env, s)?;
                Ok((!x, sat_x))
            }
            ExprInner::Negate(x) => {
                let (x, sat_x) = x.satisfy(env, s)?;
                let res = x.checked_neg().ok_or(EvalError::NegOverflow(x))?;
                Ok((res, sat_x))
            }
            ExprInner::PriceOracle1(pk, time) | ExprInner::PriceOracle1W(pk, time) => {
                let pk = if let CovExtArgs::XOnlyKey(xpk) = pk {
                    xpk.0
                } else {
                    unreachable!("PriceOracle1 constructed with only xonly key")
                };
                match s.lookup_price_oracle_sig(&pk, *time) {
                    Some((sig, price, time)) => {
                        let wit = Witness::Stack(vec![
                            sig.as_ref().to_vec(),
                            time.to_le_bytes().to_vec(),
                            price.to_le_bytes().to_vec(),
                        ]);
                        let sat = Satisfaction {
                            stack: wit,
                            has_sig: false, // Even though this has sig, it is not a signature over the tx and the tx is still malleable
                        };
                        Ok((price, sat))
                    }
                    None => Err(EvalError::MissingOracleSignature),
                }
            }
        }
    }

    /// Push this script to builder
    fn push_to_builder(&self, builder: script::Builder) -> script::Builder {
        match &self.inner {
            ExprInner::Const(c) => builder.push_slice(&c.to_le_bytes()),
            ExprInner::CurrInputIdx => builder
                .push_opcode(OP_PUSHCURRENTINPUTINDEX)
                .push_opcode(OP_INSPECTINPUTVALUE)
                .push_int(1)
                .push_opcode(OP_EQUALVERIFY),
            ExprInner::Input(i) => i
                .push_to_builder(builder)
                .push_opcode(OP_INSPECTINPUTVALUE)
                .push_int(1)
                .push_opcode(OP_EQUALVERIFY),
            ExprInner::Output(i) => i
                .push_to_builder(builder)
                .push_opcode(OP_INSPECTOUTPUTVALUE)
                .push_int(1)
                .push_opcode(OP_EQUALVERIFY),
            ExprInner::InputIssue(i) => i
                .push_to_builder(builder)
                .push_opcode(OP_INSPECTINPUTISSUANCE)
                .push_opcode(OP_DROP)
                .push_opcode(OP_DROP)
                .push_int(1)
                .push_opcode(OP_EQUALVERIFY)
                .push_opcode(OP_NIP)
                .push_opcode(OP_NIP),
            ExprInner::InputReIssue(i) => i
                .push_to_builder(builder)
                .push_opcode(OP_INSPECTINPUTISSUANCE)
                .push_opcode(OP_DROP)
                .push_opcode(OP_DROP)
                .push_opcode(OP_DROP)
                .push_opcode(OP_DROP)
                .push_int(1)
                .push_opcode(OP_EQUALVERIFY),
            ExprInner::Add(x, y) => {
                let builder = x.push_to_builder(builder);
                let builder = y.push_to_builder(builder);
                builder
                    .push_opcode(OP_ADD64)
                    .push_int(1)
                    .push_opcode(OP_EQUALVERIFY)
            }
            ExprInner::Sub(x, y) => {
                let builder = x.push_to_builder(builder);
                let builder = y.push_to_builder(builder);
                builder
                    .push_opcode(OP_SUB64)
                    .push_int(1)
                    .push_opcode(OP_EQUALVERIFY)
            }
            ExprInner::Mul(x, y) => {
                let builder = x.push_to_builder(builder);
                let builder = y.push_to_builder(builder);
                builder
                    .push_opcode(OP_MUL64)
                    .push_int(1)
                    .push_opcode(OP_EQUALVERIFY)
            }
            ExprInner::Div(x, y) => {
                let builder = x.push_to_builder(builder);
                let builder = y.push_to_builder(builder);
                builder
                    .push_opcode(OP_DIV64)
                    .push_int(1)
                    .push_opcode(OP_EQUALVERIFY)
                    .push_opcode(OP_NIP)
            }
            ExprInner::Mod(x, y) => {
                let builder = x.push_to_builder(builder);
                let builder = y.push_to_builder(builder);
                builder
                    .push_opcode(OP_DIV64)
                    .push_int(1)
                    .push_opcode(OP_EQUALVERIFY)
                    .push_opcode(OP_DROP)
            }
            ExprInner::BitAnd(x, y) => {
                let builder = x.push_to_builder(builder);
                let builder = y.push_to_builder(builder);
                builder.push_opcode(OP_AND)
            }
            ExprInner::BitOr(x, y) => {
                let builder = x.push_to_builder(builder);
                let builder = y.push_to_builder(builder);
                builder.push_opcode(OP_OR)
            }
            ExprInner::Xor(x, y) => {
                let builder = x.push_to_builder(builder);
                let builder = y.push_to_builder(builder);
                builder.push_opcode(OP_XOR)
            }
            ExprInner::Invert(x) => x.push_to_builder(builder).push_opcode(OP_INVERT),
            ExprInner::Negate(x) => x
                .push_to_builder(builder)
                .push_opcode(OP_NEG64)
                .push_int(1)
                .push_opcode(OP_EQUALVERIFY),
            ExprInner::PriceOracle1(pk, t) => {
                let xpk = if let CovExtArgs::XOnlyKey(xpk) = pk {
                    xpk.0
                } else {
                    unreachable!("PriceOracle1 constructor ensures that CovExtArgs is XOnlyKey");
                };
                // `2DUP TOALTSTACK <T> OP_GREATERTHANEQ VERIFY CAT SHA256 <K> CHECKSIGFROMSTACKVERIFY OP_FROMATLSTACK`
                builder
                    .push_opcode(OP_2DUP)
                    .push_opcode(OP_TOALTSTACK)
                    .push_slice(&t.to_le_bytes())
                    .push_opcode(OP_GREATERTHANOREQUAL64)
                    .push_opcode(OP_VERIFY)
                    .push_opcode(OP_CAT)
                    .push_opcode(OP_SHA256)
                    .push_slice(&xpk.serialize())
                    .push_opcode(OP_CHECKSIGFROMSTACKVERIFY)
                    .push_opcode(OP_FROMALTSTACK)
            }
            ExprInner::PriceOracle1W(pk, t) => {
                let xpk = if let CovExtArgs::XOnlyKey(xpk) = pk {
                    xpk.0
                } else {
                    unreachable!("PriceOracle1 constructor ensures that CovExtArgs is XOnlyKey");
                };
                // `2DUP TOALTSTACK <T> OP_GREATERTHANEQ VERIFY CAT SHA256 <K> CHECKSIGFROMSTACKVERIFY OP_FROMATLSTACK OP_SWAP`
                builder
                    .push_opcode(OP_TOALTSTACK)
                    .push_opcode(OP_2DUP)
                    .push_opcode(OP_TOALTSTACK)
                    .push_slice(&t.to_le_bytes())
                    .push_opcode(OP_GREATERTHANOREQUAL64)
                    .push_opcode(OP_VERIFY)
                    .push_opcode(OP_CAT)
                    .push_opcode(OP_SHA256)
                    .push_slice(&xpk.serialize())
                    .push_opcode(OP_CHECKSIGFROMSTACKVERIFY)
                    .push_opcode(OP_FROMALTSTACK)
                    .push_opcode(OP_FROMALTSTACK)
                    .push_opcode(OP_SWAP)
            }
        }
    }

    /// Returns (self, start_pos) parsed reversed form tokens starting with index end_pos
    /// Expression is parsed from tokens[start:end_pos]
    fn from_tokens(tokens: &[Tk], end_pos: usize) -> Option<(Self, usize)> {
        let tks = tokens;
        let e = end_pos; // short abbreviations for succinct readable code
                         //
                         // The order of arms if else is critical to the code logic. Arms are sorted
                         // in order of the tokens required to check and early return with checked_sub?.
                         // In other words, If the tokens array is not sufficient length to parse ith
                         // arm of if statement, it tokens array cannot parse any jth arm with j > i.
                         // This significantly cleans up the code as it does not require nested else if.
                         // But care must be taken when introducing new arms.
        if let Some(Tk::Bytes8(bytes)) = tks.get(e.checked_sub(1)?) {
            let mut le_bytes = [0u8; 8];
            le_bytes.copy_from_slice(bytes);
            let expr = Expr::from_inner(ExprInner::Const(i64::from_le_bytes(le_bytes)));
            Some((expr, e - 1))
        } else if let Some(Tk::Invert) = tks.get(e.checked_sub(1)?) {
            let (x, end_pos) = Self::from_tokens(tokens, e - 1)?;
            let expr = Expr::from_inner(ExprInner::Invert(Box::new(x)));
            Some((expr, end_pos))
        } else if let Some(Tk::And) = tks.get(e.checked_sub(1)?) {
            let (y, end_pos) = Self::from_tokens(tokens, e - 1)?;
            let (x, end_pos) = Self::from_tokens(tokens, end_pos)?;
            let expr = Expr::from_inner(ExprInner::BitAnd(Box::new(x), Box::new(y)));
            Some((expr, end_pos))
        } else if let Some(Tk::Or) = tks.get(e.checked_sub(1)?) {
            let (y, end_pos) = Self::from_tokens(tokens, e - 1)?;
            let (x, end_pos) = Self::from_tokens(tokens, end_pos)?;
            let expr = Expr::from_inner(ExprInner::BitOr(Box::new(x), Box::new(y)));
            Some((expr, end_pos))
        } else if let Some(Tk::Xor) = tks.get(e.checked_sub(1)?) {
            let (y, end_pos) = Self::from_tokens(tokens, e - 1)?;
            let (x, end_pos) = Self::from_tokens(tokens, end_pos)?;
            let expr = Expr::from_inner(ExprInner::Xor(Box::new(x), Box::new(y)));
            Some((expr, end_pos))
        } else if let Some(&[Tk::Neg64, Tk::Num(1), Tk::Equal, Tk::Verify]) =
            tks.get(e.checked_sub(4)?..e)
        {
            let (x, end_pos) = Self::from_tokens(tokens, e - 4)?;
            let expr = Expr::from_inner(ExprInner::Negate(Box::new(x)));
            Some((expr, end_pos))
        } else if let Some(&[Tk::Add64, Tk::Num(1), Tk::Equal, Tk::Verify]) =
            tks.get(e.checked_sub(4)?..e)
        {
            let (y, end_pos) = Self::from_tokens(tokens, e - 4)?;
            let (x, end_pos) = Self::from_tokens(tokens, end_pos)?;
            let expr = Expr::from_inner(ExprInner::Add(Box::new(x), Box::new(y)));
            Some((expr, end_pos))
        } else if let Some(&[Tk::Sub64, Tk::Num(1), Tk::Equal, Tk::Verify]) =
            tks.get(e.checked_sub(4)?..e)
        {
            let (y, end_pos) = Self::from_tokens(tokens, e - 4)?;
            let (x, end_pos) = Self::from_tokens(tokens, end_pos)?;
            let expr = Expr::from_inner(ExprInner::Sub(Box::new(x), Box::new(y)));
            Some((expr, end_pos))
        } else if let Some(&[Tk::Mul64, Tk::Num(1), Tk::Equal, Tk::Verify]) =
            tks.get(e.checked_sub(4)?..e)
        {
            let (y, end_pos) = Self::from_tokens(tokens, e - 4)?;
            let (x, end_pos) = Self::from_tokens(tokens, end_pos)?;
            let expr = Expr::from_inner(ExprInner::Mul(Box::new(x), Box::new(y)));
            Some((expr, end_pos))
        } else if let Some(&[Tk::CurrInp, Tk::InpValue, Tk::Num(1), Tk::Equal, Tk::Verify]) =
            tks.get(e.checked_sub(5)?..e)
        {
            Some((Expr::from_inner(ExprInner::CurrInputIdx), e - 5))
        } else if let Some(&[Tk::Div64, Tk::Num(1), Tk::Equal, Tk::Verify, Tk::Nip]) =
            tks.get(e.checked_sub(5)?..e)
        {
            let (y, end_pos) = Self::from_tokens(tokens, e - 5)?;
            let (x, end_pos) = Self::from_tokens(tokens, end_pos)?;
            let expr = Expr::from_inner(ExprInner::Div(Box::new(x), Box::new(y)));
            Some((expr, end_pos))
        } else if let Some(&[Tk::Div64, Tk::Num(1), Tk::Equal, Tk::Verify, Tk::Drop]) =
            tks.get(e.checked_sub(5)?..e)
        {
            let (y, end_pos) = Self::from_tokens(tokens, e - 5)?;
            let (x, end_pos) = Self::from_tokens(tokens, end_pos)?;
            let expr = Expr::from_inner(ExprInner::Mod(Box::new(x), Box::new(y)));
            Some((expr, end_pos))
        } else if let Some(&[Tk::InpValue, Tk::Num(1), Tk::Equal, Tk::Verify]) =
            tks.get(e.checked_sub(4)?..e)
        {
            let (i, e) = IdxExpr::from_tokens(tks, e - 4)?;
            Some((Expr::from_inner(ExprInner::Input(i)), e))
        } else if let Some(&[Tk::OutValue, Tk::Num(1), Tk::Equal, Tk::Verify]) =
            tks.get(e.checked_sub(4)?..e)
        {
            let (i, e) = IdxExpr::from_tokens(tks, e - 4)?;
            Some((Expr::from_inner(ExprInner::Output(i)), e))
        } else if let Some(
            &[Tk::InpIssue, Tk::Drop, Tk::Drop, Tk::Num(1), Tk::Equal, Tk::Verify, Tk::Nip, Tk::Nip],
        ) = tks.get(e.checked_sub(8)?..e)
        {
            let (i, e) = IdxExpr::from_tokens(tks, e - 8)?;
            Some((Expr::from_inner(ExprInner::InputIssue(i)), e))
        } else if let Some(
            &[Tk::InpIssue, Tk::Drop, Tk::Drop, Tk::Drop, Tk::Drop, Tk::Num(1), Tk::Equal, Tk::Verify],
        ) = tks.get(e.checked_sub(8)?..e)
        {
            let (i, e) = IdxExpr::from_tokens(tks, e - 8)?;
            Some((Expr::from_inner(ExprInner::InputReIssue(i)), e))
        } else if let Some(
            &[Tk::Dup2, Tk::ToAltStack, Tk::Bytes8(time), Tk::Geq64, Tk::Verify, Tk::Cat, Tk::Sha256, Tk::Bytes32(xpk), Tk::CheckSigFromStackVerify, Tk::FromAltStack],
        ) = tks.get(e.checked_sub(10)?..e)
        {
            let time = u64::from_le_bytes(time.try_into().expect("8 bytes"));
            let xpk = XOnlyPublicKey::from_slice(xpk).ok()?;
            let key = CovExtArgs::csfs_key(xpk);
            let expr = Expr::from_inner(ExprInner::PriceOracle1(key, time));
            Some((expr, e - 10))
        } else if let Some(
            &[Tk::ToAltStack, Tk::Dup2, Tk::ToAltStack, Tk::Bytes8(time), Tk::Geq64, Tk::Verify, Tk::Cat, Tk::Sha256, Tk::Bytes32(xpk), Tk::CheckSigFromStackVerify, Tk::FromAltStack, Tk::FromAltStack, Tk::Swap],
        ) = tks.get(e.checked_sub(13)?..e)
        {
            let time = u64::from_le_bytes(time.try_into().expect("8 bytes"));
            let xpk = XOnlyPublicKey::from_slice(xpk).ok()?;
            let key = CovExtArgs::csfs_key(xpk);
            let expr = Expr::from_inner(ExprInner::PriceOracle1W(key, time));
            Some((expr, e - 13))
        } else {
            None
        }
    }
}

/// Miniscript Fragment containing arith expressions
/// Expr cannot be directly used a miniscript fragment because it pushes a 64 bit
/// value on stack. Two expressions can be combined with Arith to something is
/// of Base type B to be used in miniscript expressions
///
/// This struct represents unchecked arith expressions that could be invalid.
/// As of now, [`Expr`] can be invalid only if
///     - PriceOracle1 is not the first leaf in the tree
///     - PriceOracle1W is the first leaf in the tree
#[derive(Eq, PartialEq, Ord, PartialOrd, Hash, Clone, Debug)]
pub enum ArithInner<T: ExtParam> {
    /// Eq
    /// `[X] [Y] EQUAL`
    Eq(Expr<T>, Expr<T>),
    /// Lt
    /// `[X] [Y] LESSTHAN`
    Lt(Expr<T>, Expr<T>),
    /// Leq
    /// `[X] [Y] LESSTHANOREQUAL`
    Leq(Expr<T>, Expr<T>),
    /// Gt
    /// `[X] [Y] GREATERTHAN`
    Gt(Expr<T>, Expr<T>),
    /// Geq
    /// `[X] [Y] GREATERTHANOREQUAL`
    Geq(Expr<T>, Expr<T>),
}

/// Wrapper around [`ArithInner`] that ensures that the expression is valid.
/// See [`ArithInner`] for more details.
///
/// Note that the library allows construction of unchecked [`Expr], but
/// [`Arith`] is always checked.
#[derive(Eq, PartialEq, Ord, PartialOrd, Hash, Clone)]
pub struct Arith<T: ExtParam> {
    /// The underlying expression
    expr: ArithInner<T>,
}

impl<T: ExtParam> Arith<T> {
    /// Create a new Arith expression. This is the only constructor
    pub fn new(expr: ArithInner<T>) -> Result<Self, TypeError> {
        {
            // Borrow checker scope
            let (a, b) = match &expr {
                ArithInner::Eq(ref a, ref b)
                | ArithInner::Lt(ref a, ref b)
                | ArithInner::Leq(ref a, ref b)
                | ArithInner::Gt(ref a, ref b)
                | ArithInner::Geq(ref a, ref b) => (a, b),
            };
            let mut iter = a.iter_terminals();
            if let Some(ExprInner::PriceOracle1W(_, _)) = iter.next() {
                return Err(TypeError::PriceOracle1WFirst);
            }
            // Note iter here has consumed the first element
            if iter.any(|x| matches!(x, ExprInner::PriceOracle1(..))) {
                return Err(TypeError::PriceOracle1Missing);
            }
            // All the elements in b should be PriceOracle1W
            if b.iter_terminals()
                .any(|x| matches!(x, ExprInner::PriceOracle1(..)))
            {
                return Err(TypeError::PriceOracle1Missing);
            }
        }
        Ok(Arith { expr })
    }

    /// Obtains the inner expression
    pub fn inner(&self) -> &ArithInner<T> {
        &self.expr
    }
}

impl<T: ExtParam> Arith<T> {
    /// Obtains the depth of this expression
    pub fn depth(&self) -> usize {
        match &self.expr {
            ArithInner::Eq(x, y)
            | ArithInner::Lt(x, y)
            | ArithInner::Leq(x, y)
            | ArithInner::Gt(x, y)
            | ArithInner::Geq(x, y) => cmp::max(x.depth, y.depth),
        }
    }

    /// Obtains the script size
    pub fn script_size(&self) -> usize {
        match &self.expr {
            ArithInner::Eq(x, y)
            | ArithInner::Lt(x, y)
            | ArithInner::Leq(x, y)
            | ArithInner::Gt(x, y)
            | ArithInner::Geq(x, y) => x.script_size + y.script_size + 1,
        }
    }
}

impl Arith<CovExtArgs> {
    /// Evaluate this expression with context given transaction and spent utxos
    pub fn eval(&self, env: &TxEnv, s: &mut interpreter::Stack) -> Result<bool, EvalError> {
        let res = match &self.expr {
            ArithInner::Eq(x, y) => x.eval(env, s)? == y.eval(env, s)?,
            ArithInner::Lt(x, y) => x.eval(env, s)? < y.eval(env, s)?,
            ArithInner::Leq(x, y) => x.eval(env, s)? <= y.eval(env, s)?,
            ArithInner::Gt(x, y) => x.eval(env, s)? > y.eval(env, s)?,
            ArithInner::Geq(x, y) => x.eval(env, s)? >= y.eval(env, s)?,
        };
        Ok(res)
    }

    /// Internal satisfaction helper for Arith.
    /// This allows us to cleanly write code that we can use "?" for early
    /// returns.
    /// The trait implementation of satisfy just calls this function with unwrap_or
    /// impossible.
    pub fn satisfy_helper<Pk: ToPublicKey>(
        &self,
        env: &TxEnv,
        sat: &dyn Satisfier<Pk>,
    ) -> Result<Satisfaction, EvalError> {
        let (res, sat_a, sat_b) = match &self.expr {
            ArithInner::Eq(a, b) => {
                let (a, sat_a) = a.satisfy(env, sat)?;
                let (b, sat_b) = b.satisfy(env, sat)?;
                (a == b, sat_a, sat_b)
            }
            ArithInner::Lt(a, b) => {
                let (a, sat_a) = a.satisfy(env, sat)?;
                let (b, sat_b) = b.satisfy(env, sat)?;
                (a < b, sat_a, sat_b)
            }
            ArithInner::Leq(a, b) => {
                let (a, sat_a) = a.satisfy(env, sat)?;
                let (b, sat_b) = b.satisfy(env, sat)?;
                (a <= b, sat_a, sat_b)
            }
            ArithInner::Gt(a, b) => {
                let (a, sat_a) = a.satisfy(env, sat)?;
                let (b, sat_b) = b.satisfy(env, sat)?;
                (a > b, sat_a, sat_b)
            }
            ArithInner::Geq(a, b) => {
                let (a, sat_a) = a.satisfy(env, sat)?;
                let (b, sat_b) = b.satisfy(env, sat)?;
                (a >= b, sat_a, sat_b)
            }
        };
        if res {
            Ok(Satisfaction::combine(sat_b, sat_a))
        } else {
            Ok(Satisfaction::impossible())
        }
    }

    /// Push this script to builder
    pub fn push_to_builder(&self, builder: script::Builder) -> script::Builder {
        match &self.expr {
            ArithInner::Eq(x, y) => {
                let builder = x.push_to_builder(builder);
                let builder = y.push_to_builder(builder);
                builder.push_opcode(OP_EQUAL)
            }
            ArithInner::Lt(x, y) => {
                let builder = x.push_to_builder(builder);
                let builder = y.push_to_builder(builder);
                builder.push_opcode(OP_LESSTHAN64)
            }
            ArithInner::Leq(x, y) => {
                let builder = x.push_to_builder(builder);
                let builder = y.push_to_builder(builder);
                builder.push_opcode(OP_LESSTHANOREQUAL64)
            }
            ArithInner::Gt(x, y) => {
                let builder = x.push_to_builder(builder);
                let builder = y.push_to_builder(builder);
                builder.push_opcode(OP_GREATERTHAN64)
            }
            ArithInner::Geq(x, y) => {
                let builder = x.push_to_builder(builder);
                let builder = y.push_to_builder(builder);
                builder.push_opcode(OP_GREATERTHANOREQUAL64)
            }
        }
    }

    /// Parse from [elements::Script]
    /// Parsing cannot roundtrip because of associative properties, similar to and_v
    /// mul(mul(a,b),c) == mul(a,mul(b,c))
    ///
    /// Returns the tokens consumed if it is possible for the object to the parsed
    /// tokens parsing reverse starting from index ind
    fn from_tokens(tokens: &[Tk]) -> Option<(Self, usize)> {
        let last_opcode = tokens.last()?;
        let (y, pos) = Expr::from_tokens(tokens, tokens.len() - 1)?;
        let (x, pos) = Expr::from_tokens(tokens, pos)?;
        let (inner, pos) = match last_opcode {
            Tk::Equal => (ArithInner::Eq(x, y), pos),
            Tk::Le64 => (ArithInner::Lt(x, y), pos),
            Tk::Leq64 => (ArithInner::Leq(x, y), pos),
            Tk::Ge64 => (ArithInner::Gt(x, y), pos),
            Tk::Geq64 => (ArithInner::Geq(x, y), pos),
            _ => return None,
        };
        Some((Arith::new(inner).ok()?, pos))
    }
}

impl<T: ExtParam> fmt::Display for Expr<T> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match &self.inner {
            ExprInner::Const(c) => write!(f, "{}", c),
            ExprInner::CurrInputIdx => write!(f, "curr_inp_v"),
            ExprInner::Input(i) => write!(f, "inp_v({})", i),
            ExprInner::Output(i) => write!(f, "out_v({})", i),
            ExprInner::InputIssue(i) => write!(f, "inp_issue_v({})", i),
            ExprInner::InputReIssue(i) => write!(f, "inp_reissue_v({})", i),
            ExprInner::Add(x, y) => write!(f, "add({},{})", x, y),
            ExprInner::Sub(x, y) => write!(f, "sub({},{})", x, y),
            ExprInner::Mul(x, y) => write!(f, "mul({},{})", x, y),
            ExprInner::Div(x, y) => write!(f, "div({},{})", x, y),
            ExprInner::Mod(x, y) => write!(f, "mod({},{})", x, y),
            ExprInner::BitAnd(x, y) => write!(f, "bitand({},{})", x, y), // Use 'bit' prefix to clearly separate from miniscript And/OR
            ExprInner::BitOr(x, y) => write!(f, "bitor({},{})", x, y),
            ExprInner::Xor(x, y) => write!(f, "bitxor({},{})", x, y),
            ExprInner::Invert(x) => write!(f, "bitinv({})", x),
            ExprInner::Negate(x) => write!(f, "neg({})", x),
            ExprInner::PriceOracle1(pk, t) => write!(f, "price_oracle1({},{})", pk, t),
            ExprInner::PriceOracle1W(pk, t) => write!(f, "price_oracle1_w({},{})", pk, t), // same syntax
        }
    }
}

impl<T: ExtParam> fmt::Debug for Expr<T> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match &self.inner {
            ExprInner::Const(c) => write!(f, "{:?}", c),
            ExprInner::CurrInputIdx => write!(f, "curr_inp_v"),
            ExprInner::Input(i) => write!(f, "inp_v({:?})", i),
            ExprInner::Output(i) => write!(f, "out_v({:?})", i),
            ExprInner::InputIssue(i) => write!(f, "inp_issue_v({:?})", i),
            ExprInner::InputReIssue(i) => write!(f, "inp_reissue_v({:?})", i),
            ExprInner::Add(x, y) => write!(f, "add({:?},{:?})", x, y),
            ExprInner::Sub(x, y) => write!(f, "sub({:?},{:?})", x, y),
            ExprInner::Mul(x, y) => write!(f, "mul({:?},{:?})", x, y),
            ExprInner::Div(x, y) => write!(f, "div({:?},{:?})", x, y),
            ExprInner::Mod(x, y) => write!(f, "mod({:?},{:?})", x, y),
            ExprInner::BitAnd(x, y) => write!(f, "bitand({:?},{:?})", x, y), // Use 'bit' prefix to clearly separate from miniscript And/OR
            ExprInner::BitOr(x, y) => write!(f, "bitor({:?},{:?})", x, y),
            ExprInner::Xor(x, y) => write!(f, "bitxor({:?},{:?})", x, y),
            ExprInner::Invert(x) => write!(f, "bitinv({:?})", x),
            ExprInner::Negate(x) => write!(f, "neg({:?})", x),
            ExprInner::PriceOracle1(pk, t) => write!(f, "price_oracle1({:?},{:?})", pk, t),
            ExprInner::PriceOracle1W(pk, t) => write!(f, "price_oracle1_w({:?},{:?})", pk, t), // same syntax as price_oracle1
        }
    }
}

impl<T: ExtParam> FromStr for Expr<T> {
    type Err = Error;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let top = expression::Tree::from_str(s)?;
        Self::from_tree(&top)
    }
}

impl<T: ExtParam> FromTree for Box<Expr<T>> {
    fn from_tree(top: &expression::Tree<'_>) -> Result<Self, Error> {
        expression::FromTree::from_tree(top).map(Box::new)
    }
}

impl<T: ExtParam> FromTree for Expr<T> {
    fn from_tree(top: &expression::Tree<'_>) -> Result<Self, Error> {
        fn unary<F, T: ExtParam>(top: &expression::Tree<'_>, frag: F) -> Result<Expr<T>, Error>
        where
            F: FnOnce(Box<Expr<T>>) -> ExprInner<T>,
        {
            let l: Expr<T> = FromTree::from_tree(&top.args[0])?;
            Ok(Expr::from_inner(frag(Box::new(l))))
        }

        fn binary<F, T: ExtParam>(top: &expression::Tree<'_>, frag: F) -> Result<Expr<T>, Error>
        where
            F: FnOnce(Box<Expr<T>>, Box<Expr<T>>) -> ExprInner<T>,
        {
            let l: Expr<T> = FromTree::from_tree(&top.args[0])?;
            let r: Expr<T> = FromTree::from_tree(&top.args[1])?;
            Ok(Expr::from_inner(frag(Box::new(l), Box::new(r))))
        }
        match (top.name, top.args.len()) {
            ("inp_v", 1) => Ok(Expr::from_inner(expression::unary(top, ExprInner::Input)?)),
            ("curr_inp_v", 0) => Ok(Expr::from_inner(ExprInner::CurrInputIdx)),
            ("out_v", 1) => Ok(Expr::from_inner(expression::unary(top, ExprInner::Output)?)),
            ("inp_issue_v", 1) => Ok(Expr::from_inner(expression::unary(
                top,
                ExprInner::InputIssue,
            )?)),
            ("inp_reissue_v", 1) => Ok(Expr::from_inner(expression::unary(
                top,
                ExprInner::InputReIssue,
            )?)),
            ("price_oracle1", 2) | ("price_oracle1_w", 2) => {
                if !top.args[0].args.is_empty() || !top.args[1].args.is_empty() {
                    return Err(Error::Unexpected(String::from(
                        "price_oracle1 expects 2 terminal arguments",
                    )));
                }
                let pk = T::arg_from_str(top.args[0].name, top.name, 0)?;
                let t: u64 = expression::parse_num::<u64>(top.args[1].name)?;
                if top.name == "price_oracle1" {
                    Ok(Expr::from_inner(ExprInner::PriceOracle1(pk, t)))
                } else {
                    Ok(Expr::from_inner(ExprInner::PriceOracle1W(pk, t)))
                }
            }
            ("add", 2) => binary(top, ExprInner::Add),
            ("sub", 2) => binary(top, ExprInner::Sub),
            ("mul", 2) => binary(top, ExprInner::Mul),
            ("div", 2) => binary(top, ExprInner::Div),
            ("mod", 2) => binary(top, ExprInner::Mod),
            ("bitand", 2) => binary(top, ExprInner::BitAnd),
            ("bitor", 2) => binary(top, ExprInner::BitOr),
            ("bitxor", 2) => binary(top, ExprInner::Xor),
            ("bitinv", 1) => unary(top, ExprInner::Invert),
            ("neg", 1) => unary(top, ExprInner::Negate),
            (_num, 0) => {
                Ok(Expr {
                    inner: expression::terminal(top, expression::parse_num::<i64>)
                        .map(ExprInner::Const)?,
                    script_size: 8 + 1, // 8 byte push
                    depth: 0,
                })
            }
            _ => Err(Error::Unexpected(format!(
                "{}({} args) while parsing Extension",
                top.name,
                top.args.len(),
            ))),
        }
    }
}

impl<T: ExtParam> FromStr for ArithInner<T> {
    type Err = Error;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let top = expression::Tree::from_str(s)?;
        Self::from_tree(&top)
    }
}

impl<T: ExtParam> FromStr for Arith<T> {
    type Err = Error;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let inner = ArithInner::from_str(s)?;
        Arith::new(inner).map_err(|_| Error::Unexpected(String::from("Arith::new")))
    }
}

impl<T: ExtParam> FromTree for Box<ArithInner<T>> {
    fn from_tree(top: &expression::Tree<'_>) -> Result<Self, Error> {
        ArithInner::from_tree(top).map(Box::new)
    }
}

impl<T: ExtParam> FromTree for ArithInner<T> {
    fn from_tree(top: &expression::Tree<'_>) -> Result<Self, Error> {
        match (top.name, top.args.len()) {
            // Disambiguiate with num64_eq to avoid confusion with asset_eq
            ("num64_eq", 2) => expression::binary(top, ArithInner::Eq),
            ("num64_geq", 2) => expression::binary(top, ArithInner::Geq),
            ("num64_gt", 2) => expression::binary(top, ArithInner::Gt),
            ("num64_lt", 2) => expression::binary(top, ArithInner::Lt),
            ("num64_leq", 2) => expression::binary(top, ArithInner::Leq),
            _ => Err(Error::Unexpected(format!(
                "{}({} args) while parsing Extension",
                top.name,
                top.args.len(),
            ))),
        }
    }
}

impl<T: ExtParam> fmt::Display for Arith<T> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match &self.expr {
            ArithInner::Eq(x, y) => write!(f, "num64_eq({},{})", x, y),
            ArithInner::Leq(x, y) => write!(f, "num64_leq({},{})", x, y),
            ArithInner::Lt(x, y) => write!(f, "num64_lt({},{})", x, y),
            ArithInner::Geq(x, y) => write!(f, "num64_geq({},{})", x, y),
            ArithInner::Gt(x, y) => write!(f, "num64_gt({},{})", x, y),
        }
    }
}

impl<T: ExtParam> fmt::Debug for Arith<T> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match &self.expr {
            ArithInner::Eq(x, y) => write!(f, "num64_eq({:?},{:?})", x, y),
            ArithInner::Leq(x, y) => write!(f, "num64_leq({:?},{:?})", x, y),
            ArithInner::Lt(x, y) => write!(f, "num64_lt({:?},{:?})", x, y),
            ArithInner::Geq(x, y) => write!(f, "num64_geq({:?},{:?})", x, y),
            ArithInner::Gt(x, y) => write!(f, "num64_gt({:?},{:?})", x, y),
        }
    }
}

impl<T: ExtParam> Extension for Arith<T> {
    fn corr_prop(&self) -> Correctness {
        Correctness {
            base: Base::B,
            input: Input::Zero,    // No input from stack
            dissatisfiable: false, // No dissatisfactions from stack
            unit: true,
        }
    }

    fn mall_prop(&self) -> Malleability {
        Malleability {
            dissat: Dissat::None, // No dissatisfactions from stack inputs
            safe: false,          // Unsafe as a top fragment
            non_malleable: true, // There can exist multiple satisfactions for expressions. inp_v(0) = out_v(0), but
                                 // we only deal with script satisfactions here.
        }
    }

    fn extra_prop(&self) -> ExtData {
        ExtData {
            pk_cost: self.script_size(), // 1 opcodes, 1 key push, msg, 1 msg push
            has_free_verify: false,
            stack_elem_count_sat: Some(0),
            stack_elem_count_dissat: Some(0),
            max_sat_size: Some((0, 0)),
            max_dissat_size: Some((0, 0)),
            timelock_info: TimelockInfo::default(),
            exec_stack_elem_count_sat: Some(self.depth()),
            exec_stack_elem_count_dissat: Some(self.depth()),
            ops: OpLimits {
                // Opcodes are really not relevant in tapscript as BIP342 removes all rules on them
                // So, don't make any effort in trying to compute and cache them.
                count: 0,
                sat: Some(0),
                nsat: Some(0),
            },
        }
    }

    fn script_size(&self) -> usize {
        self.script_size()
    }

    fn segwit_ctx_checks(&self) -> Result<(), miniscript::context::ScriptContextError> {
        // New opcodes only supported in taproot context
        Err(ScriptContextError::ExtensionError(
            "Arith opcodes only available in Taproot".to_string(),
        ))
    }

    fn from_name_tree(
        name: &str,
        children: &[expression::Tree<'_>],
    ) -> Result<Self, FromTokenIterError> {
        let tree = Tree {
            name,
            args: children.to_vec(), // Cloning two references here, it is possible to avoid the to_vec() here,
                                     // but it requires lot of refactor.
        };
        let inner = ArithInner::from_tree(&tree).map_err(|_| FromTokenIterError)?;
        Arith::new(inner).map_err(|_e| FromTokenIterError)
    }
}

impl ParseableExt for Arith<CovExtArgs> {
    fn satisfy<Pk, S>(&self, sat: &S) -> Satisfaction
    where
        Pk: ToPublicKey,
        S: Satisfier<Pk>,
    {
        let (tx, utxos, curr_idx) = match (
            sat.lookup_tx(),
            sat.lookup_spent_utxos(),
            sat.lookup_curr_inp(),
        ) {
            (Some(tx), Some(utxos), Some(curr_idx)) => (tx, utxos, curr_idx),
            _ => return Satisfaction::impossible(),
        };
        let env = match TxEnv::new(tx, utxos, curr_idx) {
            Some(env) => env,
            None => return Satisfaction::impossible(),
        };
        self.satisfy_helper(&env, sat)
            .unwrap_or(Satisfaction::empty())
    }

    fn dissatisfy<Pk, S>(&self, _sat: &S) -> Satisfaction
    where
        Pk: ToPublicKey,
        S: Satisfier<Pk>,
    {
        // Impossible
        Satisfaction::impossible()
    }

    fn push_to_builder(&self, builder: elements::script::Builder) -> elements::script::Builder {
        self.push_to_builder(builder)
    }

    fn from_token_iter(tokens: &mut TokenIter<'_>) -> Result<Self, FromTokenIterError> {
        let len = tokens.len();
        match Self::from_tokens(tokens.as_inner_mut()) {
            Some((res, last_pos)) => {
                tokens.advance(len - last_pos).ok_or(FromTokenIterError)?;
                Ok(res)
            }
            None => Err(FromTokenIterError),
        }
    }

    fn evaluate(
        &self,
        stack: &mut interpreter::Stack,
        txenv: Option<&TxEnv>,
    ) -> Result<bool, interpreter::Error> {
        let txenv = txenv
            .as_ref()
            .ok_or(interpreter::Error::ArithError(EvalError::TxEnvNotPresent))?;

        match self.eval(txenv, stack) {
            Ok(true) => {
                stack.push(interpreter::Element::Satisfied);
                Ok(true)
            }
            Ok(false) => {
                stack.push(interpreter::Element::Dissatisfied);
                Ok(false)
            }
            Err(e) => Err(interpreter::Error::ArithError(e)),
        }
    }
}

/// Evaluation Errors
#[derive(Debug, Eq, PartialEq, Ord, PartialOrd, Hash, Clone)]
pub enum EvalError {
    /// Transaction and utxos not supplied in interpreter
    TxEnvNotPresent,
    /// Utxo index out of bounds (index, uxtos.len())
    UtxoIndexOutOfBounds(usize, usize),
    /// Input at index must be explicit
    NonExplicitInput(usize),
    /// Output index out of bounds (index, tx.outputs.len())
    OutputIndexOutOfBounds(usize, usize),
    /// Output at index must be explicit
    NonExplicitOutput(usize),
    /// Output index out of bounds (index, tx.inputs.len())
    InputIndexOutOfBounds(usize, usize),
    /// Input issuance at index must be explicit
    NonExplicitInputIssuance(usize),
    /// Input reissuance at index must be explicit
    NonExplicitInputReIssuance(usize),
    /// Addition overflow
    AddOverflow(i64, i64),
    /// Addition overflow
    SubOverflow(i64, i64),
    /// Sub overflow
    MulOverflow(i64, i64),
    /// Mul overflow
    DivOverflow(i64, i64),
    /// Mod overflow
    ModOverflow(i64, i64),
    /// Neg overflow
    NegOverflow(i64),
    /// Missing price
    MissingPrice,
    /// Price 8 byte push
    Price8BytePush,
    /// Missing timestamp
    MissingTimestamp,
    /// Timestamp 8 byte push
    Timstamp8BytePush,
    /// Missing Oracle signature
    MissingOracleSignature,
    /// Missing Oracle pubkey
    MalformedSig,
    /// Timestamp in future
    TimestampInFuture,
    /// Invalid oracle signature
    InvalidSignature,
    /// Price overflow
    PriceOverflow,
}

impl error::Error for EvalError {}

impl fmt::Display for EvalError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            EvalError::UtxoIndexOutOfBounds(i, len) => {
                write!(f, "Utxo index {} out of bounds {}", i, len)
            }
            EvalError::NonExplicitInput(i) => write!(f, "Non explicit input {}", i),
            EvalError::OutputIndexOutOfBounds(i, len) => {
                write!(f, "Output index {} out of bounds {}", i, len)
            }
            EvalError::NonExplicitOutput(i) => {
                write!(f, "Non explicit output amount at index {}", i)
            }
            EvalError::InputIndexOutOfBounds(i, len) => {
                write!(f, "Input index {} out of bounds {}", i, len)
            }
            EvalError::NonExplicitInputIssuance(i) => {
                write!(f, "Non explicit input issuance amount at index {}", i)
            }
            EvalError::NonExplicitInputReIssuance(i) => {
                write!(f, "Non explicit input reissuance amount at index {}", i)
            }
            EvalError::AddOverflow(x, y) => write!(f, "Add overflow {} {}", x, y),
            EvalError::SubOverflow(x, y) => write!(f, "Sub overflow {} {}", x, y),
            EvalError::MulOverflow(x, y) => write!(f, "Mul overflow {} {}", x, y),
            EvalError::DivOverflow(x, y) => write!(f, "Div overflow {} {}", x, y),
            EvalError::ModOverflow(x, y) => write!(f, "Mod overflow {} {}", x, y),
            EvalError::NegOverflow(x) => write!(f, "Neg overflow {}", x),
            EvalError::TxEnvNotPresent => write!(
                f,
                "Transaction must be supplied to extension to arithmetic evaluation"
            ),
            EvalError::MissingPrice => write!(f, "Missing price"),
            EvalError::Price8BytePush => write!(f, "Price 8 byte push"),
            EvalError::MissingTimestamp => write!(f, "Missing timestamp"),
            EvalError::Timstamp8BytePush => write!(f, "Timestamp 8 byte push"),
            EvalError::MissingOracleSignature => write!(f, "Missing price oracle signature"),
            EvalError::MalformedSig => write!(f, "Malformed price oracle signature"),
            EvalError::TimestampInFuture => write!(f, "Oracle Timestamp in future"),
            EvalError::InvalidSignature => write!(f, "Invalid price oracle signature"),
            EvalError::PriceOverflow => write!(f, "Price overflow (must be 64 bit integer)"),
        }
    }
}

impl<PArg, QArg> TranslateExtParam<PArg, QArg> for Arith<PArg>
where
    PArg: ExtParam,
    QArg: ExtParam,
{
    type Output = Arith<QArg>;

    fn translate_ext<T, E>(&self, t: &mut T) -> Result<Self::Output, E>
    where
        T: ExtParamTranslator<PArg, QArg, E>,
    {
        let res = match &self.expr {
            ArithInner::Eq(a, b) => ArithInner::Eq(a.translate_ext(t)?, b.translate_ext(t)?),
            ArithInner::Lt(a, b) => ArithInner::Lt(a.translate_ext(t)?, b.translate_ext(t)?),
            ArithInner::Leq(a, b) => ArithInner::Leq(a.translate_ext(t)?, b.translate_ext(t)?),
            ArithInner::Gt(a, b) => ArithInner::Gt(a.translate_ext(t)?, b.translate_ext(t)?),
            ArithInner::Geq(a, b) => ArithInner::Geq(a.translate_ext(t)?, b.translate_ext(t)?),
        };
        Ok(Arith::new(res).expect("Type check must succeed"))
    }
}

impl<PArg, QArg> TranslateExtParam<PArg, QArg> for Expr<PArg>
where
    PArg: ExtParam,
    QArg: ExtParam,
{
    type Output = Expr<QArg>;

    fn translate_ext<T, E>(&self, t: &mut T) -> Result<Self::Output, E>
    where
        T: ExtParamTranslator<PArg, QArg, E>,
    {
        match &self.inner {
            ExprInner::Const(c) => Ok(Expr::from_inner(ExprInner::Const(*c))),
            ExprInner::CurrInputIdx => Ok(Expr::from_inner(ExprInner::CurrInputIdx)),
            ExprInner::Input(i) => Ok(Expr::from_inner(ExprInner::Input(i.clone()))),
            ExprInner::Output(i) => Ok(Expr::from_inner(ExprInner::Output(i.clone()))),
            ExprInner::InputIssue(i) => Ok(Expr::from_inner(ExprInner::InputIssue(i.clone()))),
            ExprInner::InputReIssue(i) => Ok(Expr::from_inner(ExprInner::InputReIssue(i.clone()))),
            ExprInner::Add(a, b) => Ok(Expr::from_inner(ExprInner::Add(
                Box::new(a.translate_ext(t)?),
                Box::new(b.translate_ext(t)?),
            ))),
            ExprInner::Sub(a, b) => Ok(Expr::from_inner(ExprInner::Sub(
                Box::new(a.translate_ext(t)?),
                Box::new(b.translate_ext(t)?),
            ))),
            ExprInner::Mul(a, b) => Ok(Expr::from_inner(ExprInner::Mul(
                Box::new(a.translate_ext(t)?),
                Box::new(b.translate_ext(t)?),
            ))),
            ExprInner::Div(a, b) => Ok(Expr::from_inner(ExprInner::Div(
                Box::new(a.translate_ext(t)?),
                Box::new(b.translate_ext(t)?),
            ))),
            ExprInner::Mod(a, b) => Ok(Expr::from_inner(ExprInner::Mod(
                Box::new(a.translate_ext(t)?),
                Box::new(b.translate_ext(t)?),
            ))),
            ExprInner::BitAnd(a, b) => Ok(Expr::from_inner(ExprInner::BitAnd(
                Box::new(a.translate_ext(t)?),
                Box::new(b.translate_ext(t)?),
            ))),
            ExprInner::BitOr(a, b) => Ok(Expr::from_inner(ExprInner::BitOr(
                Box::new(a.translate_ext(t)?),
                Box::new(b.translate_ext(t)?),
            ))),
            ExprInner::Xor(a, b) => Ok(Expr::from_inner(ExprInner::Xor(
                Box::new(a.translate_ext(t)?),
                Box::new(b.translate_ext(t)?),
            ))),
            ExprInner::Invert(a) => Ok(Expr::from_inner(ExprInner::Invert(Box::new(
                a.translate_ext(t)?,
            )))),
            ExprInner::Negate(a) => Ok(Expr::from_inner(ExprInner::Negate(Box::new(
                a.translate_ext(t)?,
            )))),
            ExprInner::PriceOracle1(pk, time) => {
                Ok(Expr::from_inner(ExprInner::PriceOracle1(t.ext(pk)?, *time)))
            }
            ExprInner::PriceOracle1W(pk, time) => Ok(Expr::from_inner(ExprInner::PriceOracle1W(
                t.ext(pk)?,
                *time,
            ))),
        }
    }
}

#[cfg(test)]
mod tests {
    use bitcoin::hashes::Hash;
    use bitcoin::key::XOnlyPublicKey;

    use super::*;
    use crate::extensions::check_sig_price_oracle_1;
    use crate::test_utils::{StrExtTranslator, StrXOnlyKeyTranslator};
    use crate::{CovenantExt, Miniscript, Segwitv0, Tap, TranslatePk};

    #[test]
    fn test_index_ops_with_arith() {
        // index ops tests with different index types
        _arith_parse("num64_eq(out_v(idx_sub(5,curr_idx)),inp_v(idx_add(0,curr_idx)))");
        _arith_parse("num64_eq(out_v(idx_mul(5,curr_idx)),inp_v(idx_div(0,curr_idx)))");

        _arith_parse(
            "num64_eq(inp_issue_v(idx_sub(5,curr_idx)),inp_reissue_v(idx_add(0,curr_idx)))",
        );
        _arith_parse(
            "num64_eq(inp_issue_v(idx_sub(5,curr_idx)),inp_reissue_v(idx_add(0,curr_idx)))",
        );
    }

    #[test]
    fn arith_parse() {
        _arith_parse("num64_geq(sub(mul(1,0),mul(0,curr_inp_v)),0)");
        _arith_parse("num64_gt(curr_inp_v,mul(1,out_v(0)))");
        // This does not test the evaluation
        _arith_parse("num64_eq(8,8)");
        _arith_parse("num64_gt(9223372036854775807,9223372036854775806)"); // 2**63-1

        // negatives and comparisons
        _arith_parse("num64_eq(-8,-8)"); // negative nums
        _arith_parse("num64_gt(-8,-9)");
        _arith_parse("num64_geq(-8,-8)");
        _arith_parse("num64_leq(-8,-7)");
        _arith_parse("num64_lt(-8,-7)");

        // test terminals parsing
        _arith_parse("num64_eq(inp_v(0),100)");
        _arith_parse("num64_eq(out_v(0),100)");
        _arith_parse("num64_eq(inp_issue_v(0),100)");
        _arith_parse("num64_eq(inp_reissue_v(0),100)");
        _arith_parse("num64_eq(inp_v(0),out_v(0))");
        _arith_parse("num64_eq(inp_issue_v(1),inp_reissue_v(1))");

        // test combinator
        _arith_parse("num64_eq(add(4,3),mul(1,7))");
        _arith_parse("num64_eq(sub(3,3),div(0,9))");
        _arith_parse("num64_eq(mod(9,3),0)");
        _arith_parse("num64_eq(bitand(0,134),0)");
        _arith_parse("num64_eq(bitor(1,3),3)");
        _arith_parse("num64_eq(bitxor(1,3),2)");
        _arith_parse("num64_eq(bitinv(0),-9223372036854775808)");
        _arith_parse("num64_eq(neg(1),-1)");

        // test some misc combinations with other miniscript fragments
        _arith_parse("and_v(v:pk(K),num64_gt(8,7))");
        _arith_parse(
            "and_v(v:pk(K),num64_eq(mul(inp_v(0),out_v(1)),sub(add(3,inp_issue_v(1)),-9)))",
        );

        // test price oracles
        _arith_parse("num64_eq(price_oracle1(K,123213),28004)");
        _arith_parse("num64_eq(price_oracle1(K,123213),price_oracle1_w(K,4318743))");
        _arith_parse(
            "and_v(v:pk(K),num64_eq(mul(inp_v(0),out_v(1)),sub(add(3,inp_issue_v(1)),price_oracle1_w(K,123213))))",
        );
        _arith_parse("and_v(v:pk(X2),num64_eq(add(price_oracle1(K,1),0),50000))");
    }

    fn _arith_parse(s: &str) {
        type MsExtStr = Miniscript<String, Tap, CovenantExt<String>>;
        type MsExt = Miniscript<XOnlyPublicKey, Tap, CovenantExt<CovExtArgs>>;
        type MsExtSegwitv0 = Miniscript<String, Segwitv0, CovenantExt<String>>;

        // Make sure that parsing this errors in segwit context
        assert!(MsExtSegwitv0::from_str_insane(s).is_err());

        let ms = MsExtStr::from_str_insane(s).unwrap();
        // test string rtt
        assert_eq!(ms.to_string(), s);
        let mut t = StrXOnlyKeyTranslator::default();
        let mut ext_t = StrExtTranslator::default();
        ext_t.ext_map.insert(
            String::from("K"),
            CovExtArgs::csfs_key(
                XOnlyPublicKey::from_str(
                    "c304c3b5805eecff054c319c545dc6ac2ad44eb70f79dd9570e284c5a62c0f9e",
                )
                .unwrap(),
            ),
        );
        // use crate::extensions::param::TranslateExtParam;
        let ms = ms.translate_pk(&mut t).unwrap();
        let ms = TranslateExt::translate_ext(&ms, &mut ext_t).unwrap();
        // script rtt
        assert_eq!(ms, MsExt::parse_insane(&ms.encode()).unwrap());
    }

    #[test]
    fn test_fuji_fixed_signs() {
        // Test Vector obtained from curl queries
        let sig = elements::secp256k1_zkp::schnorr::Signature::from_str("8fc6e217b0e1d3481855cdb97cfe333999d4cf48b9f58b4f299ad86fd768a345e97a953d6efa1ca5971f18810deedcfddc4c2bd4e8f9d1431c1ad6ebafa013a9").unwrap();
        let pk = elements::secp256k1_zkp::XOnlyPublicKey::from_str(
            "c304c3b5805eecff054c319c545dc6ac2ad44eb70f79dd9570e284c5a62c0f9e",
        )
        .unwrap();

        let timestamp: u64 = 1679531858733;
        let price: u64 = 27365;
        let secp = elements::secp256k1_zkp::Secp256k1::new();
        assert!(check_sig_price_oracle_1(&secp, &sig, &pk, timestamp, price))
    }
}