       1
       2
       3
       4
       5
       6
       7
       8
       9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      37
      38
      39
      40
      41
      42
      43
      44
      45
      46
      47
      48
      49
      50
      51
      52
      53
      54
      55
      56
      57
      58
      59
      60
      61
      62
      63
      64
      65
      66
      67
      68
      69
      70
      71
      72
      73
      74
      75
      76
      77
      78
      79
      80
      81
      82
      83
      84
      85
      86
      87
      88
      89
      90
      91
      92
      93
      94
      95
      96
      97
      98
      99
     100
     101
     102
     103
     104
     105
     106
     107
     108
     109
     110
     111
     112
     113
     114
     115
     116
     117
     118
     119
     120
     121
     122
     123
     124
     125
     126
     127
     128
     129
     130
     131
     132
     133
     134
     135
     136
     137
     138
     139
     140
     141
     142
     143
     144
     145
     146
     147
     148
     149
     150
     151
     152
     153
     154
     155
     156
     157
     158
     159
     160
     161
     162
     163
     164
     165
     166
     167
     168
     169
     170
     171
     172
     173
     174
     175
     176
     177
     178
     179
     180
     181
     182
     183
     184
     185
     186
     187
     188
     189
     190
     191
     192
     193
     194
     195
     196
     197
     198
     199
     200
     201
     202
     203
     204
     205
     206
     207
     208
     209
     210
     211
     212
     213
     214
     215
     216
     217
     218
     219
     220
     221
     222
     223
     224
     225
     226
     227
     228
     229
     230
     231
     232
     233
     234
     235
     236
     237
     238
     239
     240
     241
     242
     243
     244
     245
     246
     247
     248
     249
     250
     251
     252
     253
     254
     255
     256
     257
     258
     259
     260
     261
     262
     263
     264
     265
     266
     267
     268
     269
     270
     271
     272
     273
     274
     275
     276
     277
     278
     279
     280
     281
     282
     283
     284
     285
     286
     287
     288
     289
     290
     291
     292
     293
     294
     295
     296
     297
     298
     299
     300
     301
     302
     303
     304
     305
     306
     307
     308
     309
     310
     311
     312
     313
     314
     315
     316
     317
     318
     319
     320
     321
     322
     323
     324
     325
     326
     327
     328
     329
     330
     331
     332
     333
     334
     335
     336
     337
     338
     339
     340
     341
     342
     343
     344
     345
     346
     347
     348
     349
     350
     351
     352
     353
     354
     355
     356
     357
     358
     359
     360
     361
     362
     363
     364
     365
     366
     367
     368
     369
     370
     371
     372
     373
     374
     375
     376
     377
     378
     379
     380
     381
     382
     383
     384
     385
     386
     387
     388
     389
     390
     391
     392
     393
     394
     395
     396
     397
     398
     399
     400
     401
     402
     403
     404
     405
     406
     407
     408
     409
     410
     411
     412
     413
     414
     415
     416
     417
     418
     419
     420
     421
     422
     423
     424
     425
     426
     427
     428
     429
     430
     431
     432
     433
     434
     435
     436
     437
     438
     439
     440
     441
     442
     443
     444
     445
     446
     447
     448
     449
     450
     451
     452
     453
     454
     455
     456
     457
     458
     459
     460
     461
     462
     463
     464
     465
     466
     467
     468
     469
     470
     471
     472
     473
     474
     475
     476
     477
     478
     479
     480
     481
     482
     483
     484
     485
     486
     487
     488
     489
     490
     491
     492
     493
     494
     495
     496
     497
     498
     499
     500
     501
     502
     503
     504
     505
     506
     507
     508
     509
     510
     511
     512
     513
     514
     515
     516
     517
     518
     519
     520
     521
     522
     523
     524
     525
     526
     527
     528
     529
     530
     531
     532
     533
     534
     535
     536
     537
     538
     539
     540
     541
     542
     543
     544
     545
     546
     547
     548
     549
     550
     551
     552
     553
     554
     555
     556
     557
     558
     559
     560
     561
     562
     563
     564
     565
     566
     567
     568
     569
     570
     571
     572
     573
     574
     575
     576
     577
     578
     579
     580
     581
     582
     583
     584
     585
     586
     587
     588
     589
     590
     591
     592
     593
     594
     595
     596
     597
     598
     599
     600
     601
     602
     603
     604
     605
     606
     607
     608
     609
     610
     611
     612
     613
     614
     615
     616
     617
     618
     619
     620
     621
     622
     623
     624
     625
     626
     627
     628
     629
     630
     631
     632
     633
     634
     635
     636
     637
     638
     639
     640
     641
     642
     643
     644
     645
     646
     647
     648
     649
     650
     651
     652
     653
     654
     655
     656
     657
     658
     659
     660
     661
     662
     663
     664
     665
     666
     667
     668
     669
     670
     671
     672
     673
     674
     675
     676
     677
     678
     679
     680
     681
     682
     683
     684
     685
     686
     687
     688
     689
     690
     691
     692
     693
     694
     695
     696
     697
     698
     699
     700
     701
     702
     703
     704
     705
     706
     707
     708
     709
     710
     711
     712
     713
     714
     715
     716
     717
     718
     719
     720
     721
     722
     723
     724
     725
     726
     727
     728
     729
     730
     731
     732
     733
     734
     735
     736
     737
     738
     739
     740
     741
     742
     743
     744
     745
     746
     747
     748
     749
     750
     751
     752
     753
     754
     755
     756
     757
     758
     759
     760
     761
     762
     763
     764
     765
     766
     767
     768
     769
     770
     771
     772
     773
     774
     775
     776
     777
     778
     779
     780
     781
     782
     783
     784
     785
     786
     787
     788
     789
     790
     791
     792
     793
     794
     795
     796
     797
     798
     799
     800
     801
     802
     803
     804
     805
     806
     807
     808
     809
     810
     811
     812
     813
     814
     815
     816
     817
     818
     819
     820
     821
     822
     823
     824
     825
     826
     827
     828
     829
     830
     831
     832
     833
     834
     835
     836
     837
     838
     839
     840
     841
     842
     843
     844
     845
     846
     847
     848
     849
     850
     851
     852
     853
     854
     855
     856
     857
     858
     859
     860
     861
     862
     863
     864
     865
     866
     867
     868
     869
     870
     871
     872
     873
     874
     875
     876
     877
     878
     879
     880
     881
     882
     883
     884
     885
     886
     887
     888
     889
     890
     891
     892
     893
     894
     895
     896
     897
     898
     899
     900
     901
     902
     903
     904
     905
     906
     907
     908
     909
     910
     911
     912
     913
     914
     915
     916
     917
     918
     919
     920
     921
     922
     923
     924
     925
     926
     927
     928
     929
     930
     931
     932
     933
     934
     935
     936
     937
     938
     939
     940
     941
     942
     943
     944
     945
     946
     947
     948
     949
     950
     951
     952
     953
     954
     955
     956
     957
     958
     959
     960
     961
     962
     963
     964
     965
     966
     967
     968
     969
     970
     971
     972
     973
     974
     975
     976
     977
     978
     979
     980
     981
     982
     983
     984
     985
     986
     987
     988
     989
     990
     991
     992
     993
     994
     995
     996
     997
     998
     999
    1000
    1001
    1002
    1003
    1004
    1005
    1006
    1007
    1008
    1009
    1010
    1011
    1012
    1013
    1014
    1015
    1016
    1017
    1018
    1019
    1020
    1021
    1022
    1023
    1024
    1025
    1026
    1027
    1028
    1029
    1030
    1031
    1032
    1033
    1034
    1035
    1036
    1037
    1038
    1039
    1040
    1041
    1042
    1043
    1044
    1045
    1046
    1047
    1048
    1049
    1050
    1051
    1052
    1053
    1054
    1055
    1056
    1057
    1058
    1059
    1060
    1061
    1062
    1063
    1064
    1065
    1066
    1067
    1068
    1069
    1070
    1071
    1072
    1073
    1074
    1075
    1076
    1077
    1078
    1079
    1080
    1081
    1082
    1083
    1084
    1085
    1086
    1087
    1088
    1089
    1090
    1091
    1092
    1093
    1094
    1095
    1096
    1097
    1098
    1099
    1100
    1101
    1102
    1103
    1104
    1105
    1106
    1107
    1108
    1109
    1110
    1111
    1112
    1113
    1114
    1115
    1116
    1117
    1118
    1119
    1120
    1121
    1122
    1123
    1124
    1125
    1126
    1127
    1128
    1129
    1130
    1131
    1132
    1133
    1134
    1135
    1136
    1137
    1138
    1139
    1140
    1141
    1142
    1143
    1144
    1145
    1146
    1147
    1148
    1149
    1150
    1151
    1152
    1153
    1154
    1155
    1156
    1157
    1158
    1159
    1160
    1161
    1162
    1163
    1164
    1165
    1166
    1167
    1168
    1169
    1170
    1171
    1172
    1173
    1174
    1175
    1176
    1177
    1178
    1179
    1180
    1181
    1182
    1183
    1184
    1185
    1186
    1187
    1188
    1189
    1190
    1191
    1192
    1193
    1194
    1195
    1196
    1197
    1198
    1199
    1200
    1201
    1202
    1203
    1204
    1205
    1206
    1207
    1208
    1209
    1210
    1211
    1212
    1213
    1214
    1215
    1216
    1217
    1218
    1219
    1220
    1221
    1222
    1223
    1224
    1225
    1226
    1227
    1228
    1229
    1230
    1231
    1232
    1233
    1234
    1235
    1236
    1237
    1238
    1239
    1240
    1241
    1242
    1243
    1244
    1245
    1246
    1247
    1248
    1249
    1250
    1251
    1252
    1253
    1254
    1255
    1256
    1257
    1258
    1259
    1260
    1261
    1262
    1263
    1264
    1265
    1266
    1267
    1268
    1269
    1270
    1271
    1272
    1273
    1274
    1275
    1276
    1277
    1278
    1279
    1280
    1281
    1282
    1283
    1284
    1285
    1286
    1287
    1288
    1289
    1290
    1291
    1292
    1293
    1294
    1295
    1296
    1297
    1298
    1299
    1300
    1301
    1302
    1303
    1304
    1305
    1306
    1307
    1308
    1309
    1310
    1311
    1312
    1313
    1314
    1315
    1316
    1317
    1318
    1319
    1320
    1321
    1322
    1323
    1324
    1325
    1326
    1327
    1328
    1329
    1330
    1331
    1332
    1333
    1334
    1335
    1336
    1337
    1338
    1339
    1340
    1341
    1342
    1343
    1344
    1345
    1346
    1347
    1348
    1349
    1350
    1351
    1352
    1353
    1354
    1355
    1356
    1357
    1358
    1359
    1360
    1361
    1362
    1363
    1364
    1365
    1366
    1367
    1368
    1369
    1370
    1371
    1372
    1373
    1374
    1375
    1376
    1377
    1378
    1379
    1380
    1381
    1382
    1383
    1384
    1385
    1386
    1387
    1388
    1389
    1390
    1391
    1392
    1393
    1394
    1395
    1396
    1397
    1398
    1399
    1400
    1401
    1402
    1403
    1404
    1405
    1406
    1407
    1408
    1409
    1410
    1411
    1412
    1413
    1414
    1415
    1416
    1417
    1418
    1419
    1420
    1421
    1422
    1423
    1424
    1425
    1426
    1427
    1428
    1429
    1430
    1431
    1432
    1433
    1434
    1435
    1436
    1437
    1438
    1439
    1440
    1441
    1442
    1443
    1444
    1445
    1446
    1447
    1448
    1449
    1450
    1451
    1452
    1453
    1454
    1455
    1456
    1457
    1458
    1459
    1460
    1461
    1462
    1463
    1464
    1465
    1466
    1467
    1468
    1469
    1470
    1471
    1472
    1473
    1474
    1475
    1476
    1477
    1478
    1479
    1480
    1481
    1482
    1483
    1484
    1485
    1486
    1487
    1488
    1489
    1490
    1491
    1492
    1493
    1494
    1495
    1496
    1497
    1498
    1499
    1500
    1501
    1502
    1503
    1504
    1505
    1506
    1507
    1508
    1509
    1510
    1511
    1512
    1513
    1514
    1515
    1516
    1517
    1518
    1519
    1520
    1521
    1522
    1523
    1524
    1525
    1526
    1527
    1528
    1529
    1530
    1531
    1532
    1533
    1534
    1535
    1536
    1537
    1538
    1539
    1540
    1541
    1542
    1543
    1544
    1545
    1546
    1547
    1548
    1549
    1550
    1551
    1552
    1553
    1554
    1555
    1556
    1557
    1558
    1559
    1560
    1561
    1562
    1563
    1564
    1565
    1566
    1567
    1568
    1569
    1570
    1571
    1572
    1573
    1574
    1575
    1576
    1577
    1578
    1579
    1580
    1581
    1582
    1583
    1584
    1585
    1586
    1587
    1588
    1589
    1590
    1591
    1592
    1593
    1594
    1595
    1596
    1597
    1598
    1599
    1600
    1601
    1602
    1603
    1604
    1605
    1606
    1607
    1608
    1609
    1610
    1611
    1612
    1613
    1614
    1615
    1616
    1617
    1618
    1619
    1620
    1621
    1622
    1623
    1624
    1625
    1626
    1627
    1628
    1629
    1630
    1631
    1632
    1633
    1634
    1635
    1636
    1637
    1638
    1639
    1640
    1641
    1642
    1643
    1644
    1645
    1646
    1647
    1648
    1649
    1650
    1651
    1652
    1653
    1654
    1655
    1656
    1657
    1658
    1659
    1660
    1661
    1662
    1663
    1664
    1665
    1666
    1667
    1668
    1669
    1670
    1671
    1672
    1673
    1674
    1675
    1676
    1677
    1678
    1679
    1680
    1681
    1682
    1683
    1684
    1685
    1686
    1687
    1688
    1689
    1690
    1691
    1692
    1693
    1694
    1695
    1696
    1697
    1698
    1699
    1700
    1701
    1702
    1703
    1704
    1705
    1706
    1707
    1708
    1709
    1710
    1711
    1712
    1713
    1714
    1715
    1716
    1717
    1718
    1719
    1720
    1721
    1722
    1723
    1724
    1725
    1726
    1727
    1728
    1729
    1730
    1731
    1732
    1733
    1734
    1735
    1736
    1737
    1738
    1739
    1740
    1741
    1742
    1743
    1744
    1745
    1746
    1747
    1748
    1749
    1750
    1751
    1752
    1753
    1754
    1755
    1756
    1757
    1758
    1759
    1760
    1761
    1762
    1763
    1764
    1765
    1766
    1767
    1768
    1769
    1770
    1771
    1772
    1773
    1774
    1775
    1776
    1777
    1778
    1779
    1780
    1781
    1782
    1783
    1784
    1785
    1786
    1787
    1788
    1789
    1790
    1791
    1792
    1793
    1794
    1795
    1796
    1797
    1798
    1799
    1800
    1801
    1802
    1803
    1804
    1805
    1806
    1807
    1808
    1809
    1810
    1811
    1812
    1813
    1814
    1815
    1816
    1817
    1818
    1819
    1820
    1821
    1822
    1823
    1824
    1825
    1826
    1827
    1828
    1829
    1830
    1831
    1832
    1833
    1834
    1835
    1836
    1837
    1838
    1839
    1840
    1841
    1842
    1843
    1844
    1845
    1846
    1847
    1848
    1849
    1850
    1851
    1852
    1853
    1854
    1855
    1856
    1857
    1858
    1859
    1860
    1861
    1862
    1863
    1864
    1865
    1866
    1867
    1868
    1869
    1870
    1871
    1872
    1873
    1874
    1875
    1876
    1877
    1878
    1879
    1880
    1881
    1882
    1883
    1884
    1885
    1886
    1887
    1888
    1889
    1890
    1891
    1892
    1893
    1894
    1895
    1896
    1897
    1898
    1899
    1900
    1901
    1902
    1903
    1904
    1905
    1906
    1907
    1908
    1909
    1910
    1911
    1912
    1913
    1914
    1915
    1916
    1917
    1918
    1919
    1920
    1921
    1922
    1923
    1924
    1925
    1926
    1927
    1928
    1929
    1930
    1931
    1932
    1933
    1934
    1935
    1936
    1937
    1938
    1939
    1940
    1941
    1942
    1943
    1944
    1945
    1946
    1947
    1948
    1949
    1950
    1951
    1952
    1953
    1954
    1955
    1956
    1957
    1958
    1959
    1960
    1961
    1962
    1963
    1964
    1965
    1966
    1967
    1968
    1969
    1970
    1971
    1972
    1973
    1974
    1975
    1976
    1977
    1978
    1979
    1980
    1981
    1982
    1983
    1984
    1985
    1986
    1987
    1988
    1989
    1990
    1991
    1992
    1993
    1994
    1995
    1996
    1997
    1998
    1999
    2000
    2001
    2002
    2003
    2004
    2005
    2006
    2007
    2008
    2009
    2010
    2011
    2012
    2013
    2014
    2015
    2016
    2017
    2018
    2019
    2020
    2021
    2022
    2023
    2024
    2025
    2026
    2027
    2028
    2029
    2030
    2031
    2032
    2033
    2034
    2035
    2036
    2037
    2038
    2039
    2040
    2041
    2042
    2043
    2044
    2045
    2046
    2047
    2048
    2049
    2050
    2051
    2052
    2053
    2054
    2055
    2056
    2057
    2058
    2059
    2060
    2061
    2062
    2063
    2064
    2065
    2066
    2067
    2068
    2069
    2070
    2071
    2072
    2073
    2074
    2075
    2076
    2077
    2078
    2079
    2080
    2081
    2082
    2083
    2084
    2085
    2086
    2087
    2088
    2089
    2090
    2091
    2092
    2093
    2094
    2095
    2096
    2097
    2098
    2099
    2100
    2101
    2102
    2103
    2104
    2105
    2106
    2107
    2108
    2109
    2110
    2111
    2112
    2113
    2114
    2115
    2116
    2117
    2118
    2119
    2120
    2121
    2122
    2123
    2124
    2125
    2126
    2127
    2128
    2129
    2130
    2131
    2132
    2133
    2134
    2135
    2136
    2137
    2138
    2139
    2140
    2141
    2142
    2143
    2144
    2145
    2146
    2147
    2148
    2149
    2150
    2151
    2152
    2153
    2154
    2155
    2156
    2157
    2158
    2159
    2160
    2161
    2162
    2163
    2164
    2165
    2166
    2167
    2168
    2169
    2170
    2171
    2172
    2173
    2174
    2175
    2176
    2177
    2178
    2179
    2180
    2181
    2182
    2183
    2184
    2185
    2186
    2187
    2188
    2189
    2190
    2191
    2192
    2193
    2194
    2195
    2196
    2197
    2198
    2199
    2200
    2201
    2202
    2203
    2204
    2205
    2206
    2207
    2208
    2209
    2210
    2211
    2212
    2213
    2214
    2215
    2216
    2217
    2218
    2219
    2220
    2221
    2222
    2223
    2224
    2225
    2226
    2227
    2228
    2229
    2230
    2231
    2232
    2233
    2234
    2235
    2236
    2237
    2238
    2239
    2240
    2241
    2242
    2243
    2244
    2245
    2246
    2247
    2248
    2249
    2250
    2251
    2252
    2253
    2254
    2255
    2256
    2257
    2258
    2259
    2260
    2261
    2262
    2263
    2264
    2265
    2266
    2267
    2268
    2269
    2270
    2271
    2272
    2273
    2274
    2275
    2276
    2277
    2278
    2279
    2280
    2281
    2282
    2283
    2284
    2285
    2286
    2287
    2288
    2289
    2290
    2291
    2292
    2293
    2294
    2295
    2296
    2297
    2298
    2299
    2300
    2301
    2302
    2303
    2304
    2305
    2306
    2307
    2308
    2309
    2310
    2311
    2312
    2313
    2314
    2315
    2316
    2317
    2318
    2319
    2320
    2321
    2322
    2323
    2324
    2325
    2326
    2327
    2328
    2329
    2330
    2331
    2332
    2333
    2334
    2335
    2336
    2337
    2338
    2339
    2340
    2341
    2342
    2343
    2344
    2345
    2346
    2347
    2348
    2349
    2350
    2351
    2352
    2353
    2354
    2355
    2356
    2357
    2358
    2359
    2360
    2361
    2362
    2363
    2364
    2365
    2366
    2367
    2368
    2369
    2370
    2371
    2372
    2373
    2374
    2375
    2376
    2377
    2378
    2379
    2380
    2381
    2382
    2383
    2384
    2385
    2386
    2387
    2388
    2389
    2390
    2391
    2392
    2393
    2394
    2395
    2396
    2397
    2398
    2399
    2400
    2401
    2402
    2403
    2404
    2405
    2406
    2407
    2408
    2409
    2410
    2411
    2412
    2413
    2414
    2415
    2416
    2417
    2418
    2419
    2420
    2421
    2422
    2423
    2424
    2425
    2426
    2427
    2428
    2429
    2430
    2431
    2432
    2433
    2434
    2435
    2436
    2437
    2438
    2439
    2440
    2441
    2442
    2443
    2444
    2445
    2446
    2447
    2448
    2449
    2450
    2451
    2452
    2453
    2454
    2455
    2456
    2457
    2458
    2459
    2460
    2461
    2462
    2463
    2464
    2465
    2466
    2467
    2468
    2469
    2470
    2471
    2472
    2473
    2474
    2475
    2476
    2477
    2478
    2479
    2480
    2481
    2482
    2483
    2484
    2485
    2486
    2487
    2488
    2489
    2490
    2491
    2492
    2493
    2494
    2495
    2496
    2497
    2498
    2499
    2500
    2501
    2502
    2503
    2504
    2505
    2506
    2507
    2508
    2509
    2510
    2511
    2512
    2513
    2514
    2515
    2516
    2517
    2518
    2519
    2520
    2521
    2522
    2523
    2524
    2525
    2526
    2527
    2528
    2529
    2530
    2531
    2532
    2533
    2534
    2535
    2536
    2537
    2538
    2539
    2540
    2541
    2542
    2543
    2544
    2545
    2546
    2547
    2548
    2549
    2550
    2551
    2552
    2553
    2554
    2555
    2556
    2557
    2558
    2559
    2560
    2561
    2562
    2563
    2564
    2565
    2566
    2567
    2568
    2569
    2570
    2571
    2572
    2573
    2574
    2575
    2576
    2577
    2578
    2579
    2580
    2581
    2582
    2583
    2584
    2585
    2586
    2587
    2588
    2589
    2590
    2591
    2592
    2593
    2594
    2595
    2596
    2597
    2598
    2599
    2600
    2601
    2602
    2603
    2604
    2605
    2606
    2607
    2608
    2609
    2610
    2611
    2612
    2613
    2614
    2615
    2616
    2617
    2618
    2619
    2620
    2621
    2622
    2623
    2624
    2625
    2626
    2627
    2628
    2629
    2630
    2631
    2632
    2633
    2634
    2635
    2636
    2637
    2638
    2639
    2640
    2641
    2642
    2643
    2644
    2645
    2646
    2647
    2648
    2649
    2650
    2651
    2652
    2653
    2654
    2655
    2656
    2657
    2658
    2659
    2660
    2661
    2662
    2663
    2664
    2665
    2666
    2667
    2668
    2669
    2670
    2671
    2672
    2673
    2674
    2675
    2676
    2677
    2678
    2679
    2680
    2681
    2682
    2683
    2684
    2685
    2686
    2687
    2688
    2689
    2690
    2691
    2692
    2693
    2694
    2695
    2696
    2697
    2698
    2699
    2700
    2701
    2702
    2703
    2704
    2705
    2706
    2707
    2708
    2709
    2710
    2711
    2712
    2713
    2714
    2715
    2716
    2717
    2718
    2719
    2720
    2721
    2722
    2723
    2724
    2725
    2726
    2727
    2728
    2729
    2730
    2731
    2732
    2733
    2734
    2735
    2736
    2737
    2738
    2739
    2740
    2741
    2742
    2743
    2744
    2745
    2746
    2747
    2748
    2749
    2750
    2751
    2752
    2753
    2754
    2755
    2756
    2757
    2758
    2759
    2760
    2761
    2762
    2763
    2764
    2765
    2766
    2767
    2768
    2769
    2770
    2771
    2772
    2773
    2774
    2775
    2776
    2777
    2778
    2779
    2780
    2781
    2782
    2783
    2784
    2785
    2786
    2787
    2788
    2789
    2790
    2791
    2792
    2793
    2794
    2795
    2796
    2797
    2798
    2799
    2800
    2801
    2802
    2803
    2804
    2805
    2806
    2807
    2808
    2809
    2810
    2811
    2812
    2813
    2814
    2815
    2816
    2817
    2818
    2819
    2820
    2821
    2822
    2823
    2824
    2825
    2826
    2827
    2828
    2829
    2830
    2831
    2832
    2833
    2834
    2835
    2836
    2837
    2838
    2839
    2840
    2841
    2842
    2843
    2844
    2845
    2846
    2847
    2848
    2849
    2850
    2851
    2852
    2853
    2854
    2855
    2856
    2857
    2858
    2859
    2860
    2861
    2862
    2863
    2864
    2865
    2866
    2867
    2868
    2869
    2870
    2871
    2872
    2873
    2874
    2875
    2876
    2877
    2878
    2879
    2880
    2881
    2882
    2883
    2884
    2885
    2886
    2887
    2888
    2889
    2890
    2891
    2892
    2893
    2894
    2895
    2896
    2897
    2898
    2899
    2900
    2901
    2902
    2903
    2904
    2905
    2906
    2907
    2908
    2909
    2910
    2911
    2912
    2913
    2914
    2915
    2916
    2917
    2918
    2919
    2920
    2921
    2922
    2923
    2924
    2925
    2926
    2927
    2928
    2929
    2930
    2931
    2932
    2933
    2934
    2935
    2936
    2937
    2938
    2939
    2940
    2941
    2942
    2943
    2944
    2945
    2946
    2947
    2948
    2949
    2950
    2951
    2952
    2953
    2954
    2955
    2956
    2957
    2958
    2959
    2960
    2961
    2962
    2963
    2964
    2965
    2966
    2967
    2968
    2969
    2970
    2971
    2972
    2973
    2974
    2975
    2976
    2977
    2978
    2979
    2980
    2981
    2982
    2983
    2984
    2985
    2986
    2987
    2988
    2989
    2990
    2991
    2992
    2993
    2994
    2995
    2996
    2997
    2998
    2999
    3000
    3001
    3002
    3003
    3004
    3005
    3006
    3007
    3008
    3009
    3010
    3011
    3012
    3013
    3014
    3015
    3016
    3017
    3018
    3019
    3020
    3021
    3022
    3023
    3024
    3025
    3026
    3027
    3028
    3029
    3030
    3031
    3032
    3033
    3034
    3035
    3036
    3037
    3038
    3039
    3040
    3041
    3042
    3043
    3044
    3045
    3046
    3047
    3048
    3049
    3050
    3051
    3052
    3053
    3054
    3055
    3056
    3057
    3058
    3059
    3060
    3061
    3062
    3063
    3064
    3065
    3066
    3067
    3068
    3069
    3070
    3071
    3072
    3073
    3074
    3075
    3076
    3077
    3078
    3079
    3080
    3081
    3082
    3083
    3084
    3085
    3086
    3087
    3088
    3089
    3090
    3091
    3092
    3093
    3094
    3095
    3096
    3097
    3098
    3099
    3100
    3101
    3102
    3103
    3104
    3105
    3106
    3107
    3108
    3109
    3110
    3111
    3112
    3113
    3114
    3115
    3116
    3117
    3118
    3119
    3120
    3121
    3122
    3123
    3124
    3125
    3126
    3127
    3128
    3129
    3130
    3131
    3132
    3133
    3134
    3135
    3136
    3137
    3138
    3139
    3140
    3141
    3142
    3143
    3144
    3145
    3146
    3147
    3148
    3149
    3150
    3151
    3152
    3153
    3154
    3155
    3156
    3157
    3158
    3159
    3160
    3161
    3162
    3163
    3164
    3165
    3166
    3167
    3168
    3169
    3170
    3171
    3172
    3173
    3174
    3175
    3176
    3177
    3178
    3179
    3180
    3181
    3182
    3183
    3184
    3185
    3186
    3187
    3188
    3189
    3190
    3191
    3192
    3193
    3194
    3195
    3196
    3197
    3198
    3199
    3200
    3201
    3202
    3203
    3204
    3205
    3206
    3207
    3208
    3209
    3210
    3211
    3212
    3213
    3214
    3215
    3216
    3217
    3218
    3219
    3220
    3221
    3222
    3223
    3224
    3225
    3226
    3227
    3228
    3229
    3230
    3231
    3232
    3233
    3234
    3235
    3236
    3237
    3238
    3239
    3240
    3241
    3242
    3243
    3244
    3245
    3246
    3247
    3248
    3249
    3250
    3251
    3252
    3253
    3254
    3255
    3256
    3257
    3258
    3259
    3260
    3261
    3262
    3263
    3264
    3265
    3266
    3267
    3268
    3269
    3270
    3271
    3272
    3273
    3274
    3275
    3276
    3277
    3278
    3279
    3280
    3281
    3282
    3283
    3284
    3285
    3286
    3287
    3288
    3289
    3290
    3291
    3292
    3293
    3294
    3295
    3296
    3297
    3298
    3299
    3300
    3301
    3302
    3303
    3304
    3305
    3306
    3307
    3308
    3309
    3310
    3311
    3312
    3313
    3314
    3315
    3316
    3317
    3318
    3319
    3320
    3321
    3322
    3323
    3324
    3325
    3326
    3327
    3328
    3329
    3330
    3331
    3332
    3333
    3334
    3335
    3336
    3337
    3338
    3339
    3340
    3341
    3342
    3343
    3344
    3345
    3346
    3347
    3348
    3349
    3350
    3351
    3352
    3353
    3354
    3355
    3356
    3357
    3358
    3359
    3360
    3361
    3362
    3363
    3364
    3365
    3366
    3367
    3368
    3369
    3370
    3371
    3372
    3373
    3374
    3375
    3376
    3377
    3378
    3379
    3380
    3381
    3382
    3383
    3384
    3385
    3386
    3387
    3388
    3389
    3390
    3391
    3392
    3393
    3394
    3395
    3396
    3397
    3398
    3399
    3400
    3401
    3402
    3403
    3404
    3405
    3406
    3407
    3408
    3409
    3410
    3411
    3412
    3413
    3414
    3415
    3416
    3417
    3418
    3419
    3420
    3421
    3422
    3423
    3424
    3425
    3426
    3427
    3428
    3429
    3430
    3431
    3432
    3433
    3434
    3435
    3436
    3437
    3438
    3439
    3440
    3441
    3442
    3443
    3444
    3445
    3446
    3447
    3448
    3449
    3450
    3451
    3452
    3453
    3454
    3455
    3456
    3457
    3458
    3459
    3460
    3461
    3462
    3463
    3464
    3465
    3466
    3467
    3468
    3469
    3470
    3471
    3472
    3473
    3474
    3475
    3476
    3477
    3478
    3479
    3480
    3481
    3482
    3483
    3484
    3485
    3486
    3487
    3488
    3489
    3490
    3491
    3492
    3493
    3494
    3495
    3496
    3497
    3498
    3499
    3500
    3501
    3502
    3503
    3504
    3505
    3506
    3507
    3508
    3509
    3510
    3511
    3512
    3513
    3514
    3515
    3516
    3517
    3518
    3519
    3520
    3521
    3522
    3523
    3524
    3525
    3526
    3527
    3528
    3529
    3530
    3531
    3532
    3533
    3534
    3535
    3536
    3537
    3538
    3539
    3540
    3541
    3542
    3543
    3544
    3545
    3546
    3547
    3548
    3549
    3550
    3551
    3552
    3553
    3554
    3555
    3556
    3557
    3558
    3559
    3560
    3561
    3562
    3563
    3564
    3565
    3566
    3567
    3568
    3569
    3570
    3571
    3572
    3573
    3574
    3575
    3576
    3577
    3578
    3579
    3580
    3581
    3582
    3583
    3584
    3585
    3586
    3587
    3588
    3589
    3590
    3591
    3592
    3593
    3594
    3595
    3596
    3597
    3598
    3599
    3600
    3601
    3602
    3603
    3604
    3605
    3606
    3607
    3608
    3609
    3610
    3611
    3612
    3613
    3614
    3615
    3616
    3617
    3618
    3619
    3620
    3621
    3622
    3623
    3624
    3625
    3626
    3627
    3628
    3629
    3630
    3631
    3632
    3633
    3634
    3635
    3636
    3637
    3638
    3639
    3640
    3641
    3642
    3643
    3644
    3645
    3646
    3647
    3648
    3649
    3650
    3651
    3652
    3653
    3654
    3655
    3656
    3657
    3658
    3659
    3660
    3661
    3662
    3663
    3664
    3665
    3666
    3667
    3668
    3669
    3670
    3671
    3672
    3673
    3674
    3675
    3676
    3677
    3678
    3679
    3680
    3681
    3682
    3683
    3684
    3685
    3686
    3687
    3688
    3689
    3690
    3691
    3692
    3693
    3694
    3695
    3696
    3697
    3698
    3699
    3700
    3701
    3702
    3703
    3704
    3705
    3706
    3707
    3708
    3709
    3710
    3711
    3712
    3713
    3714
    3715
    3716
    3717
    3718
    3719
    3720
    3721
    3722
    3723
    3724
    3725
    3726
    3727
    3728
    3729
    3730
    3731
    3732
    3733
    3734
    3735
    3736
    3737
    3738
    3739
    3740
    3741
    3742
    3743
    3744
    3745
    3746
    3747
    3748
    3749
    3750
    3751
    3752
    3753
    3754
    3755
    3756
    3757
    3758
    3759
    3760
    3761
    3762
    3763
    3764
    3765
    3766
    3767
    3768
    3769
    3770
    3771
    3772
    3773
    3774
    3775
    3776
    3777
    3778
    3779
    3780
    3781
    3782
    3783
    3784
    3785
    3786
    3787
    3788
    3789
    3790
    3791
    3792
    3793
    3794
    3795
    3796
    3797
    3798
    3799
    3800
    3801
    3802
    3803
    3804
    3805
    3806
    3807
    3808
    3809
    3810
    3811
    3812
    3813
    3814
    3815
    3816
    3817
    3818
    3819
    3820
    3821
    3822
    3823
    3824
    3825
    3826
    3827
    3828
    3829
    3830
    3831
    3832
    3833
    3834
    3835
    3836
    3837
    3838
    3839
    3840
    3841
    3842
    3843
    3844
    3845
    3846
    3847
    3848
    3849
    3850
    3851
    3852
    3853
    3854
    3855
    3856
    3857
    3858
    3859
    3860
    3861
    3862
    3863
    3864
    3865
    3866
    3867
    3868
    3869
    3870
    3871
    3872
    3873
    3874
    3875
    3876
    3877
    3878
    3879
    3880
    3881
    3882
    3883
    3884
    3885
    3886
    3887
    3888
    3889
    3890
    3891
    3892
    3893
    3894
    3895
    3896
    3897
    3898
    3899
    3900
    3901
    3902
    3903
    3904
    3905
    3906
    3907
    3908
    3909
    3910
    3911
    3912
    3913
    3914
    3915
    3916
    3917
    3918
    3919
    3920
    3921
    3922
    3923
    3924
    3925
    3926
    3927
    3928
    3929
    3930
    3931
    3932
    3933
    3934
    3935
    3936
    3937
    3938
    3939
    3940
    3941
    3942
    3943
    3944
    3945
    3946
    3947
    3948
    3949
    3950
    3951
    3952
    3953
    3954
    3955
    3956
    3957
    3958
    3959
    3960
    3961
    3962
    3963
    3964
    3965
    3966
    3967
    3968
    3969
    3970
    3971
    3972
    3973
    3974
    3975
    3976
    3977
    3978
    3979
    3980
    3981
    3982
    3983
    3984
    3985
    3986
    3987
    3988
    3989
    3990
    3991
    3992
    3993
    3994
    3995
    3996
    3997
    3998
    3999
    4000
    4001
    4002
    4003
    4004
    4005
    4006
    4007
    4008
    4009
    4010
    4011
    4012
    4013
    4014
    4015
    4016
    4017
    4018
    4019
    4020
    4021
    4022
    4023
    4024
    4025
    4026
    4027
    4028
    4029
    4030
    4031
    4032
    4033
    4034
    4035
    4036
    4037
    4038
    4039
    4040
    4041
    4042
    4043
    4044
    4045
    4046
    4047
    4048
    4049

    use std::{cmp, mem};
    use bytes::Bytes;
    use keys::{Message, Signature, Public};
    use chain::constants::SEQUENCE_LOCKTIME_DISABLE_FLAG;
    use crypto::{sha1, sha256, dhash160, dhash256, ripemd160};
    use sign::{SignatureVersion, Sighash};
    use script::MAX_SCRIPT_ELEMENT_SIZE;
    use {
    	script, Builder, Script, ScriptWitness, Num, VerificationFlags, Opcode, Error, SignatureChecker, Stack
    };

    /// Helper function.
    fn check_signature(
    	checker: &SignatureChecker,
    	mut script_sig: Vec<u8>,
    	public: Vec<u8>,
    	script_code: &Script,
    	version: SignatureVersion
    ) -> bool {
    	let public = match Public::from_slice(&public) {
    		Ok(public) => public,
    		_ => return false,
    	};

    	if script_sig.is_empty() {
    		return false;
    	}

    	let hash_type = script_sig.pop().unwrap() as u32;
    	let signature = script_sig.into();

    	checker.check_signature(&signature, &public, script_code, hash_type, version)
    }

    /// Helper function.
    fn verify_signature(
    	checker: &SignatureChecker,
    	signature: Vec<u8>,
    	public: Vec<u8>,
    	message: Message,
    ) -> bool {
    	let public = match Public::from_slice(&public) {
    		Ok(public) => public,
    		_ => return false,
    	};

    	if signature.is_empty() {
    		return false;
    	}

    	checker.verify_signature(&signature.into(), &public, &message.into())
    }

    fn is_public_key(v: &[u8]) -> bool {
    	match v.len() {
    		33 if v[0] == 2 || v[0] == 3 => true,
    		65 if v[0] == 4 => true,
    		_ => false,
    	}
    }

    /// A canonical signature exists of: <30> <total len> <02> <len R> <R> <02> <len S> <S> <hashtype>
    /// Where R and S are not negative (their first byte has its highest bit not set), and not
    /// excessively padded (do not start with a 0 byte, unless an otherwise negative number follows,
    /// in which case a single 0 byte is necessary and even required).
    ///
    /// See https://bitcointalk.org/index.php?topic=8392.msg127623#msg127623
    ///
    /// This function is consensus-critical since BIP66.
    fn is_valid_signature_encoding(sig: &[u8]) -> bool {
    	// Format: 0x30 [total-length] 0x02 [R-length] [R] 0x02 [S-length] [S] [sighash]
    	// * total-length: 1-byte length descriptor of everything that follows,
    	//   excluding the sighash byte.
    	// * R-length: 1-byte length descriptor of the R value that follows.
    	// * R: arbitrary-length big-endian encoded R value. It must use the shortest
    	//   possible encoding for a positive integers (which means no null bytes at
    	//   the start, except a single one when the next byte has its highest bit set).
    	// * S-length: 1-byte length descriptor of the S value that follows.
    	// * S: arbitrary-length big-endian encoded S value. The same rules apply.
    	// * sighash: 1-byte value indicating what data is hashed (not part of the DER
    	//   signature)

    	// Minimum and maximum size constraints
    	if sig.len() < 9 || sig.len() > 73 {
    		return false;
    	}

    	// A signature is of type 0x30 (compound)
    	if sig[0] != 0x30 {
    		return false;
    	}

    	// Make sure the length covers the entire signature.
    	if sig[1] as usize != sig.len() - 3 {
    		return false;
    	}

    	// Extract the length of the R element.
    	let len_r = sig[3] as usize;

    	// Make sure the length of the S element is still inside the signature.
    	if len_r + 5 >= sig.len() {
    		return false;
    	}

    	// Extract the length of the S element.
    	let len_s = sig[len_r + 5] as usize;

    	// Verify that the length of the signature matches the sum of the length
    	if len_r + len_s + 7 != sig.len() {
    		return false;
    	}

    	// Check whether the R element is an integer.
    	if sig[2] != 2 {
    		return false;
    	}

    	// Zero-length integers are not allowed for R.
    	if len_r == 0 {
    		return false;
    	}

    	// Negative numbers are not allowed for R.
    	if (sig[4] & 0x80) != 0 {
    		return false;
    	}

    	// Null bytes at the start of R are not allowed, unless R would
    	// otherwise be interpreted as a negative number.
    	if len_r > 1 && sig[4] == 0 && (sig[5] & 0x80) == 0 {
    		return false;
    	}

    	// Check whether the S element is an integer.
    	if sig[len_r + 4] != 2 {
    		return false;
    	}

    	// Zero-length integers are not allowed for S.
    	if len_s == 0 {
    		return false;
    	}

    	// Negative numbers are not allowed for S.
    	if (sig[len_r + 6] & 0x80) != 0 {
    		return false;
    	}

    	// Null bytes at the start of S are not allowed, unless S would otherwise be
    	// interpreted as a negative number.
    	if len_s > 1 && (sig[len_r + 6] == 0) && (sig[len_r + 7] & 0x80) == 0 {
    		return false;
    	}

    	true
    }

    fn is_low_der_signature(sig: &[u8]) -> Result<(), Error> {
    	if !is_valid_signature_encoding(sig) {
    		return Err(Error::SignatureDer);
    	}

    	let signature: Signature = sig.into();
    	if !signature.check_low_s() {
    		return Err(Error::SignatureHighS);
    	}

    	Ok(())
    }

    fn is_defined_hashtype_signature(version: SignatureVersion, sig: &[u8]) -> bool {
    	if sig.is_empty() {
    		return false;
    	}

    	Sighash::is_defined(version, sig[sig.len() - 1] as u32)
    }

    fn parse_hash_type(version: SignatureVersion, sig: &[u8]) -> Sighash {
    	Sighash::from_u32(version, if sig.is_empty() { 0 } else { sig[sig.len() - 1] as u32 })
    }

    fn check_signature_encoding(sig: &[u8], flags: &VerificationFlags, version: SignatureVersion) -> Result<(), Error> {
    	// Empty signature. Not strictly DER encoded, but allowed to provide a
    	// compact way to provide an invalid signature for use with CHECK(MULTI)SIG

    	if sig.is_empty() {
    		return Ok(());
    	}

    	if (flags.verify_dersig || flags.verify_low_s || flags.verify_strictenc) && !is_valid_signature_encoding(sig) {
    		return Err(Error::SignatureDer);
    	}

    	if flags.verify_low_s {
    		is_low_der_signature(sig)?;
    	}

    	if flags.verify_strictenc && !is_defined_hashtype_signature(version, sig) {
    		return Err(Error::SignatureHashtype)
    	}

    	// verify_strictenc is currently enabled for BitcoinCash only
    	if flags.verify_strictenc {
    		let uses_fork_id = parse_hash_type(version, sig).fork_id;
    		let enabled_fork_id = version == SignatureVersion::ForkId;
    		if uses_fork_id && !enabled_fork_id {
    			return Err(Error::SignatureIllegalForkId)
    		} else if !uses_fork_id && enabled_fork_id {
    			return Err(Error::SignatureMustUseForkId);
    		}
    	}

    	Ok(())
    }

    fn check_pubkey_encoding(v: &[u8], flags: &VerificationFlags) -> Result<(), Error> {
    	if flags.verify_strictenc && !is_public_key(v) {
    		return Err(Error::PubkeyType);
    	}

    	Ok(())
    }

    fn check_minimal_push(data: &[u8], opcode: Opcode) -> bool {
    	if data.is_empty() {
    		// Could have used OP_0.
    		opcode == Opcode::OP_0
    	} else if data.len() == 1 && data[0] >= 1 && data[0] <= 16 {
    		// Could have used OP_1 .. OP_16.
    		opcode as u8 == Opcode::OP_1 as u8 + (data[0] - 1)
    	} else if data.len() == 1 && data[0] == 0x81 {
    		// Could have used OP_1NEGATE
    		opcode == Opcode::OP_1NEGATE
    	} else if data.len() <= 75 {
    		// Could have used a direct push (opcode indicating number of bytes pushed + those bytes).
    		opcode as usize == data.len()
    	} else if data.len() <= 255 {
    		// Could have used OP_PUSHDATA.
    		opcode == Opcode::OP_PUSHDATA1
    	} else if data.len() <= 65535 {
    		// Could have used OP_PUSHDATA2.
    		opcode == Opcode::OP_PUSHDATA2
    	} else {
    		true
    	}
    }

    fn cast_to_bool(data: &[u8]) -> bool {
    	if data.is_empty() {
    		return false;
    	}

    	if data[..data.len() - 1].iter().any(|x| x != &0) {
    		return true;
    	}

    	let last = data[data.len() - 1];
    	!(last == 0 || last == 0x80)
    }

    /// Verifies script signature and pubkey
    pub fn verify_script(
    	script_sig: &Script,
    	script_pubkey: &Script,
    	witness: &ScriptWitness,
    	flags: &VerificationFlags,
    	checker: &SignatureChecker,
    	version: SignatureVersion,
    ) -> Result<(), Error> {
    	if flags.verify_sigpushonly && !script_sig.is_push_only() {
    		return Err(Error::SignaturePushOnly);
    	}

    	let mut stack = Stack::new();
    	let mut stack_copy = Stack::new();
    	let mut had_witness = false;

    	eval_script(&mut stack, script_sig, flags, checker, version)?;

    	if flags.verify_p2sh {
    		stack_copy = stack.clone();
    	}

    	let res = eval_script(&mut stack, script_pubkey, flags, checker, version)?;
    	if !res {
    		return Err(Error::EvalFalse);
    	}

    	// Verify witness program
    	let mut verify_cleanstack = flags.verify_cleanstack;
    	if flags.verify_witness {
    		if let Some((witness_version, witness_program)) = script_pubkey.parse_witness_program() {
    			if !script_sig.is_empty() {
    				return Err(Error::WitnessMalleated);
    			}

    			had_witness = true;
    			verify_cleanstack = false;
    			if !verify_witness_program(witness, witness_version, witness_program, flags, checker)? {
    				return Err(Error::EvalFalse);
    			}
    		}
    	}

    	// Additional validation for spend-to-script-hash transactions:
    	if flags.verify_p2sh && script_pubkey.is_pay_to_script_hash() {
    		if !script_sig.is_push_only() {
    			return Err(Error::SignaturePushOnly);
    		}

    		mem::swap(&mut stack, &mut stack_copy);

    		// stack cannot be empty here, because if it was the
    		// P2SH  HASH <> EQUAL  scriptPubKey would be evaluated with
    		// an empty stack and the EvalScript above would return false.
    		assert!(!stack.is_empty());

    		let pubkey2: Script = stack.pop()?.into();

    		let res = eval_script(&mut stack, &pubkey2, flags, checker, version)?;
    		if !res {
    			return Err(Error::EvalFalse);
    		}

    		if flags.verify_witness {
    			if let Some((witness_version, witness_program)) = pubkey2.parse_witness_program() {
    				if script_sig != &Builder::default().push_data(&pubkey2).into_script() {
    					return Err(Error::WitnessMalleatedP2SH);
    				}

    				had_witness = true;
    				verify_cleanstack = false;
    				if !verify_witness_program(witness, witness_version, witness_program, flags, checker)? {
    					return Err(Error::EvalFalse);
    				}
    			}
    		}
    	}

        // The CLEANSTACK check is only performed after potential P2SH evaluation,
        // as the non-P2SH evaluation of a P2SH script will obviously not result in
        // a clean stack (the P2SH inputs remain). The same holds for witness evaluation.
    	if verify_cleanstack {
            // Disallow CLEANSTACK without P2SH, as otherwise a switch CLEANSTACK->P2SH+CLEANSTACK
            // would be possible, which is not a softfork (and P2SH should be one).
    		assert!(flags.verify_p2sh);
    		if stack.len() != 1 {
    			return Err(Error::Cleanstack);
    		}
    	}

    	if flags.verify_witness {
    		// We can't check for correct unexpected witness data if P2SH was off, so require
    		// that WITNESS implies P2SH. Otherwise, going from WITNESS->P2SH+WITNESS would be
    		// possible, which is not a softfork.
    		assert!(flags.verify_p2sh);
    		if !had_witness && !witness.is_empty() {
    			return Err(Error::WitnessUnexpected);
    		}
    	}

    	Ok(())
    }

    fn verify_witness_program(
    	witness: &ScriptWitness,
    	witness_version: u8,
    	witness_program: &[u8],
    	flags: &VerificationFlags,
    	checker: &SignatureChecker,
    ) -> Result<bool, Error> {
    	if witness_version != 0 {
    		if flags.verify_discourage_upgradable_witness_program {
    			return Err(Error::DiscourageUpgradableWitnessProgram);
    		}

    		return Ok(true);
    	}

    	let witness_stack = witness;
    	let witness_stack_len = witness_stack.len();
    	let (mut stack, script_pubkey): (Stack<_>, Script) = match witness_program.len() {
    		32 => {
    			if witness_stack_len == 0 {
    				return Err(Error::WitnessProgramWitnessEmpty);
    			}

    			let script_pubkey = &witness_stack[witness_stack_len - 1];
    			let stack = &witness_stack[0..witness_stack_len - 1];
    			let script_pubkey_hash = sha256(script_pubkey);

    			if script_pubkey_hash != witness_program[0..32].into() {
    				return Err(Error::WitnessProgramMismatch);
    			}

    			(stack.iter().cloned().collect::<Vec<_>>().into(), Script::new(script_pubkey.clone()))
    		},
    		20 => {
    			if witness_stack_len != 2 {
    				return Err(Error::WitnessProgramMismatch);
    			}

    			let script_pubkey = Builder::default()
    				.push_opcode(Opcode::OP_DUP)
    				.push_opcode(Opcode::OP_HASH160)
    				.push_data(witness_program)
    				.push_opcode(Opcode::OP_EQUALVERIFY)
    				.push_opcode(Opcode::OP_CHECKSIG)
    				.into_script();

    			(witness_stack.clone().into(), script_pubkey)
    		},
    		_ => return Err(Error::WitnessProgramWrongLength),
    	};

    	if stack.iter().any(|s| s.len() > MAX_SCRIPT_ELEMENT_SIZE) {
    		return Err(Error::PushSize);
    	}

    	if !eval_script(&mut stack, &script_pubkey, flags, checker, SignatureVersion::WitnessV0)? {
    		return Ok(false);
    	}

    	if stack.len() != 1 {
    		return Err(Error::EvalFalse);
    	}

    	let success = cast_to_bool(stack.last().expect("stack.len() == 1; last() only returns errors when stack is empty; qed"));
    	Ok(success)
    }

    /// Evaluautes the script
    #[cfg_attr(feature="cargo-clippy", allow(match_same_arms))]
    pub fn eval_script(
    	stack: &mut Stack<Bytes>,
    	script: &Script,
    	flags: &VerificationFlags,
    	checker: &SignatureChecker,
    	version: SignatureVersion
    ) -> Result<bool, Error> {
    	if script.len() > script::MAX_SCRIPT_SIZE {
    		return Err(Error::ScriptSize);
    	}

    	let mut pc = 0;
    	let mut op_count = 0;
    	let mut begincode = 0;
    	let mut exec_stack = Vec::<bool>::new();
    	let mut altstack = Stack::<Bytes>::new();

    	while pc < script.len() {
    		let executing = exec_stack.iter().all(|x| *x);
    		let instruction = match script.get_instruction(pc) {
    			Ok(i) => i,
    			Err(Error::BadOpcode) if !executing => {
    				pc += 1;
    				continue;
    			},
    			Err(err) => return Err(err),
    		};
    		let opcode = instruction.opcode;

    		if let Some(data) = instruction.data {
    			if data.len() > script::MAX_SCRIPT_ELEMENT_SIZE {
    				return Err(Error::PushSize);
    			}

    			if executing && flags.verify_minimaldata && !check_minimal_push(data, opcode) {
    				return Err(Error::Minimaldata);
    			}
    		}

    		if opcode.is_countable() {
    			op_count += 1;
    			if op_count > script::MAX_OPS_PER_SCRIPT {
    				return Err(Error::OpCount);
    			}
    		}

    		if opcode.is_disabled(flags) {
    			return Err(Error::DisabledOpcode(opcode));
    		}

    		pc += instruction.step;
    		if !(executing || (Opcode::OP_IF <= opcode && opcode <= Opcode::OP_ENDIF)) {
    			continue;
    		}

    		match opcode {
    			Opcode::OP_PUSHDATA1 |
    			Opcode::OP_PUSHDATA2 |
    			Opcode::OP_PUSHDATA4 |
    			Opcode::OP_0 |
    			Opcode::OP_PUSHBYTES_1 |
    			Opcode::OP_PUSHBYTES_2 |
    			Opcode::OP_PUSHBYTES_3 |
    			Opcode::OP_PUSHBYTES_4 |
    			Opcode::OP_PUSHBYTES_5 |
    			Opcode::OP_PUSHBYTES_6 |
    			Opcode::OP_PUSHBYTES_7 |
    			Opcode::OP_PUSHBYTES_8 |
    			Opcode::OP_PUSHBYTES_9 |
    			Opcode::OP_PUSHBYTES_10 |
    			Opcode::OP_PUSHBYTES_11 |
    			Opcode::OP_PUSHBYTES_12 |
    			Opcode::OP_PUSHBYTES_13 |
    			Opcode::OP_PUSHBYTES_14 |
    			Opcode::OP_PUSHBYTES_15 |
    			Opcode::OP_PUSHBYTES_16 |
    			Opcode::OP_PUSHBYTES_17 |
    			Opcode::OP_PUSHBYTES_18 |
    			Opcode::OP_PUSHBYTES_19 |
    			Opcode::OP_PUSHBYTES_20 |
    			Opcode::OP_PUSHBYTES_21 |
    			Opcode::OP_PUSHBYTES_22 |
    			Opcode::OP_PUSHBYTES_23 |
    			Opcode::OP_PUSHBYTES_24 |
    			Opcode::OP_PUSHBYTES_25 |
    			Opcode::OP_PUSHBYTES_26 |
    			Opcode::OP_PUSHBYTES_27 |
    			Opcode::OP_PUSHBYTES_28 |
    			Opcode::OP_PUSHBYTES_29 |
    			Opcode::OP_PUSHBYTES_30 |
    			Opcode::OP_PUSHBYTES_31 |
    			Opcode::OP_PUSHBYTES_32 |
    			Opcode::OP_PUSHBYTES_33 |
    			Opcode::OP_PUSHBYTES_34 |
    			Opcode::OP_PUSHBYTES_35 |
    			Opcode::OP_PUSHBYTES_36 |
    			Opcode::OP_PUSHBYTES_37 |
    			Opcode::OP_PUSHBYTES_38 |
    			Opcode::OP_PUSHBYTES_39 |
    			Opcode::OP_PUSHBYTES_40 |
    			Opcode::OP_PUSHBYTES_41 |
    			Opcode::OP_PUSHBYTES_42 |
    			Opcode::OP_PUSHBYTES_43 |
    			Opcode::OP_PUSHBYTES_44 |
    			Opcode::OP_PUSHBYTES_45 |
    			Opcode::OP_PUSHBYTES_46 |
    			Opcode::OP_PUSHBYTES_47 |
    			Opcode::OP_PUSHBYTES_48 |
    			Opcode::OP_PUSHBYTES_49 |
    			Opcode::OP_PUSHBYTES_50 |
    			Opcode::OP_PUSHBYTES_51 |
    			Opcode::OP_PUSHBYTES_52 |
    			Opcode::OP_PUSHBYTES_53 |
    			Opcode::OP_PUSHBYTES_54 |
    			Opcode::OP_PUSHBYTES_55 |
    			Opcode::OP_PUSHBYTES_56 |
    			Opcode::OP_PUSHBYTES_57 |
    			Opcode::OP_PUSHBYTES_58 |
    			Opcode::OP_PUSHBYTES_59 |
    			Opcode::OP_PUSHBYTES_60 |
    			Opcode::OP_PUSHBYTES_61 |
    			Opcode::OP_PUSHBYTES_62 |
    			Opcode::OP_PUSHBYTES_63 |
    			Opcode::OP_PUSHBYTES_64 |
    			Opcode::OP_PUSHBYTES_65 |
    			Opcode::OP_PUSHBYTES_66 |
    			Opcode::OP_PUSHBYTES_67 |
    			Opcode::OP_PUSHBYTES_68 |
    			Opcode::OP_PUSHBYTES_69 |
    			Opcode::OP_PUSHBYTES_70 |
    			Opcode::OP_PUSHBYTES_71 |
    			Opcode::OP_PUSHBYTES_72 |
    			Opcode::OP_PUSHBYTES_73 |
    			Opcode::OP_PUSHBYTES_74 |
    			Opcode::OP_PUSHBYTES_75 => {
    				if let Some(data) = instruction.data {
    					stack.push(data.to_vec().into());
    				}
    			},
    			Opcode::OP_1NEGATE |
    			Opcode::OP_1 |
    			Opcode::OP_2 |
    			Opcode::OP_3 |
    			Opcode::OP_4 |
    			Opcode::OP_5 |
    			Opcode::OP_6 |
    			Opcode::OP_7 |
    			Opcode::OP_8 |
    			Opcode::OP_9 |
    			Opcode::OP_10 |
    			Opcode::OP_11 |
    			Opcode::OP_12 |
    			Opcode::OP_13 |
    			Opcode::OP_14 |
    			Opcode::OP_15 |
    			Opcode::OP_16 => {
    				let value = (opcode as i32).wrapping_sub(Opcode::OP_1 as i32 - 1);
    				stack.push(Num::from(value).to_bytes());
    			},
    			Opcode::OP_CAT if flags.verify_concat => {
    				let mut value_to_append = stack.pop()?;
    				let value_to_update = stack.last_mut()?;
    				if value_to_update.len() + value_to_append.len() > script::MAX_SCRIPT_ELEMENT_SIZE {
    					return Err(Error::PushSize);
    				}
    				value_to_update.append(&mut value_to_append);
    			},
    			// OP_SPLIT replaces OP_SUBSTR
    			Opcode::OP_SUBSTR if flags.verify_split => {
    				let n = Num::from_slice(&stack.pop()?, flags.verify_minimaldata, 4)?;
    				if n.is_negative() {
    					return Err(Error::InvalidStackOperation);
    				}
    				let n: usize = n.into();
    				let splitted_value = {
    					let mut value_to_split = stack.last_mut()?;
    					if n > value_to_split.len() {
    						return Err(Error::InvalidSplitRange);
    					}
    					value_to_split.split_off(n)
    				};
    				stack.push(splitted_value);
    			},
    			Opcode::OP_AND if flags.verify_and => {
    				let mask = stack.pop()?;
    				let mask_len = mask.len();
    				let value_to_update = stack.last_mut()?;
    				if mask_len != value_to_update.len() {
    					return Err(Error::InvalidOperandSize);
    				}
    				for (byte_to_update, byte_mask) in (*value_to_update).iter_mut().zip(mask.iter()) {
    					*byte_to_update = *byte_to_update & byte_mask;
    				}
    			},
    			Opcode::OP_OR if flags.verify_or => {
    				let mask = stack.pop()?;
    				let mask_len = mask.len();
    				let value_to_update = stack.last_mut()?;
    				if mask_len != value_to_update.len() {
    					return Err(Error::InvalidOperandSize);
    				}
    				for (byte_to_update, byte_mask) in (*value_to_update).iter_mut().zip(mask.iter()) {
    					*byte_to_update = *byte_to_update | byte_mask;
    				}
    			},
    			Opcode::OP_XOR if flags.verify_xor => {
    				let mask = stack.pop()?;
    				let mask_len = mask.len();
    				let value_to_update = stack.last_mut()?;
    				if mask_len != value_to_update.len() {
    					return Err(Error::InvalidOperandSize);
    				}
    				for (byte_to_update, byte_mask) in (*value_to_update).iter_mut().zip(mask.iter()) {
    					*byte_to_update = *byte_to_update ^ byte_mask;
    				}
    			},
    			Opcode::OP_DIV if flags.verify_div => {
    				let v1 = Num::from_slice(&stack.pop()?, flags.verify_minimaldata, 4)?;
    				let v2 = Num::from_slice(&stack.pop()?, flags.verify_minimaldata, 4)?;
    				if v2.is_zero() {
    					return Err(Error::DivisionByZero);
    				}
    				stack.push((v1 / v2).to_bytes());
    			},
    			Opcode::OP_MOD if flags.verify_mod => {
    				let v1 = Num::from_slice(&stack.pop()?, flags.verify_minimaldata, 4)?;
    				let v2 = Num::from_slice(&stack.pop()?, flags.verify_minimaldata, 4)?;
    				if v2.is_zero() {
    					return Err(Error::DivisionByZero);
    				}
    				stack.push((v1 % v2).to_bytes());
    			},
    			// OP_BIN2NUM replaces OP_RIGHT
    			Opcode::OP_RIGHT if flags.verify_bin2num => {
    				let bin = stack.pop()?;
    				let n = Num::minimally_encode(&bin, 4)?;
    				stack.push(n.to_bytes());
    			},
    			// OP_NUM2BIN replaces OP_LEFT
    			Opcode::OP_LEFT if flags.verify_num2bin => {
    				let bin_size = Num::from_slice(&stack.pop()?, flags.verify_minimaldata, 4)?;
    				if bin_size.is_negative() || bin_size > MAX_SCRIPT_ELEMENT_SIZE.into() {
    					return Err(Error::PushSize);
    				}

    				let bin_size: usize = bin_size.into();
    				let num = Num::minimally_encode(&stack.pop()?, 4)?;
    				let mut num = num.to_bytes();

    				// check if we can fit number into array of bin_size length
    				if num.len() > bin_size {
    					return Err(Error::ImpossibleEncoding);
    				}

    				// check if we need to extend binary repr with zero-bytes
    				if num.len() < bin_size {
    					let sign_byte = num.last_mut().map(|last_byte| {
    						let sign_byte = *last_byte & 0x80;
    						*last_byte = *last_byte & 0x7f;
    						sign_byte
    					}).unwrap_or(0x00);

    					num.resize(bin_size - 1, 0x00);
    					num.push(sign_byte);
    				}

    				stack.push(num);
    			},
    			Opcode::OP_CAT | Opcode::OP_SUBSTR | Opcode::OP_LEFT | Opcode::OP_RIGHT |
    			Opcode::OP_INVERT | Opcode::OP_AND | Opcode::OP_OR | Opcode::OP_XOR |
    			Opcode::OP_2MUL | Opcode::OP_2DIV | Opcode::OP_MUL | Opcode::OP_DIV |
    			Opcode::OP_MOD | Opcode::OP_LSHIFT | Opcode::OP_RSHIFT => {
    				return Err(Error::DisabledOpcode(opcode));
    			},
    			Opcode::OP_NOP => (),
    			Opcode::OP_CHECKLOCKTIMEVERIFY => {
    				if flags.verify_locktime {
    					// Note that elsewhere numeric opcodes are limited to
    					// operands in the range -2**31+1 to 2**31-1, however it is
    					// legal for opcodes to produce results exceeding that
    					// range. This limitation is implemented by CScriptNum's
    					// default 4-byte limit.
    					//
    					// If we kept to that limit we'd have a year 2038 problem,
    					// even though the nLockTime field in transactions
    					// themselves is uint32 which only becomes meaningless
    					// after the year 2106.
    					//
    					// Thus as a special case we tell CScriptNum to accept up
    					// to 5-byte bignums, which are good until 2**39-1, well
    					// beyond the 2**32-1 limit of the nLockTime field itself.
    					let lock_time = Num::from_slice(stack.last()?, flags.verify_minimaldata, 5)?;

    					// In the rare event that the argument may be < 0 due to
    					// some arithmetic being done first, you can always use
    					// 0 MAX CHECKLOCKTIMEVERIFY.
    					if lock_time.is_negative() {
    						return Err(Error::NegativeLocktime);
    					}

    					if !checker.check_lock_time(lock_time) {
    						return Err(Error::UnsatisfiedLocktime);
    					}
    				} else if flags.verify_discourage_upgradable_nops {
    					return Err(Error::DiscourageUpgradableNops);
    				}
    			},
    			Opcode::OP_CHECKSEQUENCEVERIFY => {
    				if flags.verify_checksequence {
    					let sequence = Num::from_slice(stack.last()?, flags.verify_minimaldata, 5)?;

    					if sequence.is_negative() {
    						return Err(Error::NegativeLocktime);
    					}

    					if (sequence & (SEQUENCE_LOCKTIME_DISABLE_FLAG as i64).into()).is_zero() && !checker.check_sequence(sequence) {
    						return Err(Error::UnsatisfiedLocktime);
    					}

    				} else if flags.verify_discourage_upgradable_nops {
    					return Err(Error::DiscourageUpgradableNops);
    				}
    			},
    			Opcode::OP_NOP1 |
    			Opcode::OP_NOP4 |
    			Opcode::OP_NOP5 |
    			Opcode::OP_NOP6 |
    			Opcode::OP_NOP7 |
    			Opcode::OP_NOP8 |
    			Opcode::OP_NOP9 |
    			Opcode::OP_NOP10 => {
    				if flags.verify_discourage_upgradable_nops {
    					return Err(Error::DiscourageUpgradableNops);
    				}
    			},
    			Opcode::OP_IF | Opcode::OP_NOTIF => {
    				let mut exec_value = false;
    				if executing {
    					exec_value = cast_to_bool(&stack.pop().map_err(|_| Error::UnbalancedConditional)?);
    					if opcode == Opcode::OP_NOTIF {
    						exec_value = !exec_value;
    					}
    				}
    				exec_stack.push(exec_value);
    			},
    			Opcode::OP_ELSE => {
    				if exec_stack.is_empty() {
    					return Err(Error::UnbalancedConditional);
    				}
    				let last_index = exec_stack.len() - 1;
    				let last = exec_stack[last_index];
    				exec_stack[last_index] = !last;
    			},
    			Opcode::OP_ENDIF => {
    				if exec_stack.is_empty() {
    					return Err(Error::UnbalancedConditional);
    				}
    				exec_stack.pop();
    			},
    			Opcode::OP_VERIFY => {
    				let exec_value = cast_to_bool(&stack.pop()?);
    				if !exec_value {
    					return Err(Error::Verify);
    				}
    			},
    			Opcode::OP_RETURN => {
    				return Err(Error::ReturnOpcode);
    			},
    			Opcode::OP_TOALTSTACK => {
    				altstack.push(stack.pop()?);
    			},
    			Opcode::OP_FROMALTSTACK => {
    				stack.push(altstack.pop().map_err(|_| Error::InvalidAltstackOperation)?);
    			},
    			Opcode::OP_2DROP => {
    				stack.drop(2)?;
    			},
    			Opcode::OP_2DUP => {
    				stack.dup(2)?;
    			},
    			Opcode::OP_3DUP => {
    				stack.dup(3)?;
    			},
    			Opcode::OP_2OVER => {
    				stack.over(2)?;
    			},
    			Opcode::OP_2ROT => {
    				stack.rot(2)?;
    			},
    			Opcode::OP_2SWAP => {
    				stack.swap(2)?;
    			},
    			Opcode::OP_IFDUP => {
    				if cast_to_bool(stack.last()?) {
    					stack.dup(1)?;
    				}
    			},
    			Opcode::OP_DEPTH => {
    				let depth = Num::from(stack.len());
    				stack.push(depth.to_bytes());
    			},
    			Opcode::OP_DROP => {
    				stack.pop()?;
    			},
    			Opcode::OP_DUP => {
    				stack.dup(1)?;
    			},
    			Opcode::OP_NIP => {
    				stack.nip()?;
    			},
    			Opcode::OP_OVER => {
    				stack.over(1)?;
    			},
    			Opcode::OP_PICK | Opcode::OP_ROLL => {
    				let n: i64 = Num::from_slice(&stack.pop()?, flags.verify_minimaldata, 4)?.into();
    				if n < 0 || n >= stack.len() as i64 {
    					return Err(Error::InvalidStackOperation);
    				}

    				let v = match opcode {
    					Opcode::OP_PICK => stack.top(n as usize)?.clone(),
    					_ => stack.remove(n as usize)?,
    				};

    				stack.push(v);
    			},
    			Opcode::OP_ROT => {
    				stack.rot(1)?;
    			},
    			Opcode::OP_SWAP => {
    				stack.swap(1)?;
    			},
    			Opcode::OP_TUCK => {
    				stack.tuck()?;
    			},
    			Opcode::OP_SIZE => {
    				let n = Num::from(stack.last()?.len());
    				stack.push(n.to_bytes());
    			},
    			Opcode::OP_EQUAL => {
    				let v1 = stack.pop()?;
    				let v2 = stack.pop()?;
    				if v1 == v2 {
    					stack.push(vec![1].into());
    				} else {
    					stack.push(vec![0].into());
    				}
    			},
    			Opcode::OP_EQUALVERIFY => {
    				let equal = stack.pop()? == stack.pop()?;
    				if !equal {
    					return Err(Error::EqualVerify);
    				}
    			},
    			Opcode::OP_1ADD => {
    				let n = Num::from_slice(&stack.pop()?, flags.verify_minimaldata, 4)? + 1.into();
    				stack.push(n.to_bytes());
    			},
    			Opcode::OP_1SUB => {
    				let n = Num::from_slice(&stack.pop()?, flags.verify_minimaldata, 4)? - 1.into();
    				stack.push(n.to_bytes());
    			},
    			Opcode::OP_NEGATE => {
    				let n = -Num::from_slice(&stack.pop()?, flags.verify_minimaldata, 4)?;
    				stack.push(n.to_bytes());
    			},
    			Opcode::OP_ABS => {
    				let n = Num::from_slice(&stack.pop()?, flags.verify_minimaldata, 4)?.abs();
    				stack.push(n.to_bytes());
    			},
    			Opcode::OP_NOT => {
    				let n = Num::from_slice(&stack.pop()?, flags.verify_minimaldata, 4)?.is_zero();
    				let n = Num::from(n);
    				stack.push(n.to_bytes());
    			},
    			Opcode::OP_0NOTEQUAL => {
    				let n = !Num::from_slice(&stack.pop()?, flags.verify_minimaldata, 4)?.is_zero();
    				let n = Num::from(n);
    				stack.push(n.to_bytes());
    			},
    			Opcode::OP_ADD => {
    				let v1 = Num::from_slice(&stack.pop()?, flags.verify_minimaldata, 4)?;
    				let v2 = Num::from_slice(&stack.pop()?, flags.verify_minimaldata, 4)?;
    				stack.push((v1 + v2).to_bytes());
    			},
    			Opcode::OP_SUB => {
    				let v1 = Num::from_slice(&stack.pop()?, flags.verify_minimaldata, 4)?;
    				let v2 = Num::from_slice(&stack.pop()?, flags.verify_minimaldata, 4)?;
    				stack.push((v2 - v1).to_bytes());
    			},
    			Opcode::OP_BOOLAND => {
    				let v1 = Num::from_slice(&stack.pop()?, flags.verify_minimaldata, 4)?;
    				let v2 = Num::from_slice(&stack.pop()?, flags.verify_minimaldata, 4)?;
    				let v = Num::from(!v1.is_zero() && !v2.is_zero());
    				stack.push(v.to_bytes());
    			},
    			Opcode::OP_BOOLOR => {
    				let v1 = Num::from_slice(&stack.pop()?, flags.verify_minimaldata, 4)?;
    				let v2 = Num::from_slice(&stack.pop()?, flags.verify_minimaldata, 4)?;
    				let v = Num::from(!v1.is_zero() || !v2.is_zero());
    				stack.push(v.to_bytes());
    			},
    			Opcode::OP_NUMEQUAL => {
    				let v1 = Num::from_slice(&stack.pop()?, flags.verify_minimaldata, 4)?;
    				let v2 = Num::from_slice(&stack.pop()?, flags.verify_minimaldata, 4)?;
    				let v = Num::from(v1 == v2);
    				stack.push(v.to_bytes());
    			},
    			Opcode::OP_NUMEQUALVERIFY => {
    				let v1 = Num::from_slice(&stack.pop()?, flags.verify_minimaldata, 4)?;
    				let v2 = Num::from_slice(&stack.pop()?, flags.verify_minimaldata, 4)?;
    				if v1 != v2 {
    					return Err(Error::NumEqualVerify);
    				}
    			},
    			Opcode::OP_NUMNOTEQUAL => {
    				let v1 = Num::from_slice(&stack.pop()?, flags.verify_minimaldata, 4)?;
    				let v2 = Num::from_slice(&stack.pop()?, flags.verify_minimaldata, 4)?;
    				let v = Num::from(v1 != v2);
    				stack.push(v.to_bytes());
    			},
    			Opcode::OP_LESSTHAN => {
    				let v1 = Num::from_slice(&stack.pop()?, flags.verify_minimaldata, 4)?;
    				let v2 = Num::from_slice(&stack.pop()?, flags.verify_minimaldata, 4)?;
    				let v = Num::from(v1 > v2);
    				stack.push(v.to_bytes());
    			},
    			Opcode::OP_GREATERTHAN => {
    				let v1 = Num::from_slice(&stack.pop()?, flags.verify_minimaldata, 4)?;
    				let v2 = Num::from_slice(&stack.pop()?, flags.verify_minimaldata, 4)?;
    				let v = Num::from(v1 < v2);
    				stack.push(v.to_bytes());
    			},
    			Opcode::OP_LESSTHANOREQUAL => {
    				let v1 = Num::from_slice(&stack.pop()?, flags.verify_minimaldata, 4)?;
    				let v2 = Num::from_slice(&stack.pop()?, flags.verify_minimaldata, 4)?;
    				let v = Num::from(v1 >= v2);
    				stack.push(v.to_bytes());
    			},
    			Opcode::OP_GREATERTHANOREQUAL => {
    				let v1 = Num::from_slice(&stack.pop()?, flags.verify_minimaldata, 4)?;
    				let v2 = Num::from_slice(&stack.pop()?, flags.verify_minimaldata, 4)?;
    				let v = Num::from(v1 <= v2);
    				stack.push(v.to_bytes());
    			},
    			Opcode::OP_MIN => {
    				let v1 = Num::from_slice(&stack.pop()?, flags.verify_minimaldata, 4)?;
    				let v2 = Num::from_slice(&stack.pop()?, flags.verify_minimaldata, 4)?;
    				stack.push(cmp::min(v1, v2).to_bytes());
    			},
    			Opcode::OP_MAX => {
    				let v1 = Num::from_slice(&stack.pop()?, flags.verify_minimaldata, 4)?;
    				let v2 = Num::from_slice(&stack.pop()?, flags.verify_minimaldata, 4)?;
    				stack.push(cmp::max(v1, v2).to_bytes());
    			},
    			Opcode::OP_WITHIN => {
    				let v1 = Num::from_slice(&stack.pop()?, flags.verify_minimaldata, 4)?;
    				let v2 = Num::from_slice(&stack.pop()?, flags.verify_minimaldata, 4)?;
    				let v3 = Num::from_slice(&stack.pop()?, flags.verify_minimaldata, 4)?;
    				if v2 <= v3 && v3 < v1 {
    					stack.push(vec![1].into());
    				} else {
    					stack.push(vec![0].into());
    				}
    			},
    			Opcode::OP_RIPEMD160 => {
    				let v = ripemd160(&stack.pop()?);
    				stack.push(v.to_vec().into());
    			},
    			Opcode::OP_SHA1 => {
    				let v = sha1(&stack.pop()?);
    				stack.push(v.to_vec().into());
    			},
    			Opcode::OP_SHA256 => {
    				let v = sha256(&stack.pop()?);
    				stack.push(v.to_vec().into());
    			},
    			Opcode::OP_HASH160 => {
    				let v = dhash160(&stack.pop()?);
    				stack.push(v.to_vec().into());
    			},
    			Opcode::OP_HASH256 => {
    				let v = dhash256(&stack.pop()?);
    				stack.push(v.to_vec().into());
    			},
    			Opcode::OP_CODESEPARATOR => {
    				begincode = pc;
    			},
    			Opcode::OP_CHECKSIG | Opcode::OP_CHECKSIGVERIFY => {
    				let pubkey = stack.pop()?;
    				let signature = stack.pop()?;
    				let sighash = parse_hash_type(version, &signature);
    				let mut subscript = script.subscript(begincode);
    				match version {
    					SignatureVersion::ForkId if sighash.fork_id => (),
    					SignatureVersion::WitnessV0 => (),
    					SignatureVersion::Base | SignatureVersion::ForkId => {
    						let signature_script = Builder::default().push_data(&*signature).into_script();
    						subscript = subscript.find_and_delete(&*signature_script);
    					},
    				}

    				check_signature_encoding(&signature, flags, version)?;
    				check_pubkey_encoding(&pubkey, flags)?;

    				let success = check_signature(checker, signature.into(), pubkey.into(), &subscript, version);
    				match opcode {
    					Opcode::OP_CHECKSIG => {
    						if success {
    							stack.push(vec![1].into());
    						} else {
    							stack.push(vec![0].into());
    						}
    					},
    					Opcode::OP_CHECKSIGVERIFY if !success => {
    						return Err(Error::CheckSigVerify);
    					},
    					_ => {},
    				}
    			},
    			Opcode::OP_CHECKMULTISIG | Opcode::OP_CHECKMULTISIGVERIFY => {
    				let keys_count = Num::from_slice(&stack.pop()?, flags.verify_minimaldata, 4)?;
    				if keys_count < 0.into() || keys_count > script::MAX_PUBKEYS_PER_MULTISIG.into() {
    					return Err(Error::PubkeyCount);
    				}

    				let keys_count: usize = keys_count.into();
    				let keys = (0..keys_count).into_iter().map(|_| stack.pop()).collect::<Result<Vec<_>, _>>()?;

    				let sigs_count = Num::from_slice(&stack.pop()?, flags.verify_minimaldata, 4)?;
    				if sigs_count < 0.into() || sigs_count > keys_count.into() {
    					return Err(Error::SigCount);
    				}

    				let sigs_count: usize = sigs_count.into();
    				let sigs = (0..sigs_count).into_iter().map(|_| stack.pop()).collect::<Result<Vec<_>, _>>()?;

    				let mut subscript = script.subscript(begincode);

    				for signature in &sigs {
    					let sighash = parse_hash_type(version, &signature);
    					match version {
    						SignatureVersion::ForkId if sighash.fork_id => (),
    						SignatureVersion::WitnessV0 => (),
    						SignatureVersion::Base | SignatureVersion::ForkId => {
    							let signature_script = Builder::default().push_data(&*signature).into_script();
    							subscript = subscript.find_and_delete(&*signature_script);
    						},
    					}
    				}

    				let mut success = true;
    				let mut k = 0;
    				let mut s = 0;
    				while s < sigs.len() && success {
    					// TODO: remove redundant copying
    					let key = keys[k].clone();
    					let sig = sigs[s].clone();

    					check_signature_encoding(&sig, flags, version)?;
    					check_pubkey_encoding(&key, flags)?;

    					let ok = check_signature(checker, sig.into(), key.into(), &subscript, version);
    					if ok {
    						s += 1;
    					}
    					k += 1;

    					success = sigs.len() - s <= keys.len() - k;
    				}

    				if !stack.pop()?.is_empty() && flags.verify_nulldummy {
    					return Err(Error::SignatureNullDummy);
    				}

    				match opcode {
    					Opcode::OP_CHECKMULTISIG => {
    						if success {
    							stack.push(vec![1].into());
    						} else {
    							stack.push(vec![0].into());
    						}
    					},
    					Opcode::OP_CHECKMULTISIGVERIFY if !success => {
    						return Err(Error::CheckSigVerify);
    					},
    					_ => {},
    				}
    			},
    			Opcode::OP_RESERVED |
    			Opcode::OP_VER |
    			Opcode::OP_RESERVED1 |
    			Opcode::OP_RESERVED2 => {
    				if executing {
    					return Err(Error::DisabledOpcode(opcode));
    				}
    			},
    			Opcode::OP_VERIF |
    			Opcode::OP_VERNOTIF => {
    				return Err(Error::DisabledOpcode(opcode));
    			},
    			Opcode::OP_CHECKDATASIG | Opcode::OP_CHECKDATASIGVERIFY if flags.verify_checkdatasig => {
    				let pubkey = stack.pop()?;
    				let message = stack.pop()?;
    				let signature = stack.pop()?;

    				check_signature_encoding(&signature, flags, version)?;
    				check_pubkey_encoding(&pubkey, flags)?;

    				let signature: Vec<u8> = signature.into();
    				let message_hash = sha256(&message);
    				let success = verify_signature(checker, signature.into(), pubkey.into(), message_hash);
    				match opcode {
    					Opcode::OP_CHECKDATASIG => {
    						if success {
    							stack.push(vec![1].into());
    						} else {
    							stack.push(vec![0].into());
    						}
    					},
    					Opcode::OP_CHECKDATASIGVERIFY if !success => {
    						return Err(Error::CheckDataSigVerify);
    					},
    					_ => {},
    				}
    			},
    			Opcode::OP_CHECKDATASIG | Opcode::OP_CHECKDATASIGVERIFY => {
    				return Err(Error::DisabledOpcode(opcode));
    			},
    		}

    		if stack.len() + altstack.len() > 1000 {
    			return Err(Error::StackSize);
    		}
    	}

    	if !exec_stack.is_empty() {
    		return Err(Error::UnbalancedConditional);
    	}

    	let success = !stack.is_empty() && {
    		let last = stack.last()?;
    		cast_to_bool(last)
    	};

    	Ok(success)
    }

    #[cfg(test)]
    mod tests {
    	use bytes::Bytes;
    	use chain::Transaction;
    	use crypto::sha256;
    	use keys::{KeyPair, Private, Message, Network};
    	use sign::SignatureVersion;
    	use script::MAX_SCRIPT_ELEMENT_SIZE;
    	use {
    		Opcode, Script, ScriptWitness, VerificationFlags, Builder, Error, Num, TransactionInputSigner,
    		NoopSignatureChecker, TransactionSignatureChecker, Stack
    	};
    	use super::{eval_script, verify_script, is_public_key};

    	#[test]
    	fn tests_is_public_key() {
    		assert!(!is_public_key(&[]));
    		assert!(!is_public_key(&[1]));
    		assert!(is_public_key(&Bytes::from("0495dfb90f202c7d016ef42c65bc010cd26bb8237b06253cc4d12175097bef767ed6b1fcb3caf1ed57c98d92e6cb70278721b952e29a335134857acd4c199b9d2f")));
    		assert!(is_public_key(&[2; 33]));
    		assert!(is_public_key(&[3; 33]));
    		assert!(!is_public_key(&[4; 33]));
    	}

    	// https://github.com/bitcoin/bitcoin/blob/d612837814020ae832499d18e6ee5eb919a87907/src/test/script_tests.cpp#L900
    	#[test]
    	fn test_push_data() {
    		let expected: Stack<Bytes> = vec![vec![0x5a].into()].into();
    		let flags = VerificationFlags::default()
    			.verify_p2sh(true);
    		let checker = NoopSignatureChecker;
    		let version = SignatureVersion::Base;
    		let direct: Script = vec![Opcode::OP_PUSHBYTES_1 as u8, 0x5a].into();
    		let pushdata1: Script = vec![Opcode::OP_PUSHDATA1 as u8, 0x1, 0x5a].into();
    		let pushdata2: Script = vec![Opcode::OP_PUSHDATA2 as u8, 0x1, 0, 0x5a].into();
    		let pushdata4: Script = vec![Opcode::OP_PUSHDATA4 as u8, 0x1, 0, 0, 0, 0x5a].into();

    		let mut direct_stack = Stack::new();
    		let mut pushdata1_stack = Stack::new();
    		let mut pushdata2_stack = Stack::new();
    		let mut pushdata4_stack = Stack::new();
    		assert!(eval_script(&mut direct_stack, &direct, &flags, &checker, version).unwrap());
    		assert!(eval_script(&mut pushdata1_stack, &pushdata1, &flags, &checker, version).unwrap());
    		assert!(eval_script(&mut pushdata2_stack, &pushdata2, &flags, &checker, version).unwrap());
    		assert!(eval_script(&mut pushdata4_stack, &pushdata4, &flags, &checker, version).unwrap());

    		assert_eq!(direct_stack, expected);
    		assert_eq!(pushdata1_stack, expected);
    		assert_eq!(pushdata2_stack, expected);
    		assert_eq!(pushdata4_stack, expected);
    	}

    	fn basic_test_with_flags(script: &Script, flags: &VerificationFlags, expected: Result<bool, Error>, expected_stack: Stack<Bytes>) {
    		let checker = NoopSignatureChecker;
    		let version = SignatureVersion::Base;
    		let mut stack = Stack::new();
    		assert_eq!(eval_script(&mut stack, script, &flags, &checker, version), expected);
    		if expected.is_ok() {
    			assert_eq!(stack, expected_stack);
    		}
    	}

    	fn basic_test(script: &Script, expected: Result<bool, Error>, expected_stack: Stack<Bytes>) {
    		let flags = VerificationFlags::default()
    			.verify_p2sh(true);
    		basic_test_with_flags(script, &flags, expected, expected_stack)
    	}

    	#[test]
    	fn test_equal() {
    		let script = Builder::default()
    			.push_data(&[0x4])
    			.push_data(&[0x4])
    			.push_opcode(Opcode::OP_EQUAL)
    			.into_script();
    		let result = Ok(true);
    		let stack = vec![vec![1].into()].into();
    		basic_test(&script, result, stack);
    	}

    	#[test]
    	fn test_equal_false() {
    		let script = Builder::default()
    			.push_data(&[0x4])
    			.push_data(&[0x3])
    			.push_opcode(Opcode::OP_EQUAL)
    			.into_script();
    		let result = Ok(false);
    		let stack = vec![vec![0].into()].into();
    		basic_test(&script, result, stack);
    	}

    	#[test]
    	fn test_equal_invalid_stack() {
    		let script = Builder::default()
    			.push_data(&[0x4])
    			.push_opcode(Opcode::OP_EQUAL)
    			.into_script();
    		let result = Err(Error::InvalidStackOperation);
    		basic_test(&script, result, Stack::default());
    	}

    	#[test]
    	fn test_equal_verify() {
    		let script = Builder::default()
    			.push_data(&[0x4])
    			.push_data(&[0x4])
    			.push_opcode(Opcode::OP_EQUALVERIFY)
    			.into_script();
    		let result = Ok(false);
    		let stack = Stack::default();
    		basic_test(&script, result, stack);
    	}

    	#[test]
    	fn test_equal_verify_failed() {
    		let script = Builder::default()
    			.push_data(&[0x4])
    			.push_data(&[0x3])
    			.push_opcode(Opcode::OP_EQUALVERIFY)
    			.into_script();
    		let result = Err(Error::EqualVerify);
    		basic_test(&script, result, Stack::default());
    	}

    	#[test]
    	fn test_equal_verify_invalid_stack() {
    		let script = Builder::default()
    			.push_data(&[0x4])
    			.push_opcode(Opcode::OP_EQUALVERIFY)
    			.into_script();
    		let result = Err(Error::InvalidStackOperation);
    		basic_test(&script, result, Stack::default());
    	}

    	#[test]
    	fn test_size() {
    		let script = Builder::default()
    			.push_data(&[0x12, 0x34])
    			.push_opcode(Opcode::OP_SIZE)
    			.into_script();
    		let result = Ok(true);
    		let stack = vec![vec![0x12, 0x34].into(), vec![0x2].into()].into();
    		basic_test(&script, result, stack);
    	}

    	#[test]
    	fn test_size_false() {
    		let script = Builder::default()
    			.push_data(&[])
    			.push_opcode(Opcode::OP_SIZE)
    			.into_script();
    		let result = Ok(false);
    		let stack = vec![vec![].into(), vec![].into()].into();
    		basic_test(&script, result, stack);
    	}

    	#[test]
    	fn test_size_invalid_stack() {
    		let script = Builder::default()
    			.push_opcode(Opcode::OP_SIZE)
    			.into_script();
    		let result = Err(Error::InvalidStackOperation);
    		basic_test(&script, result, Stack::default());
    	}

    	#[test]
    	fn test_hash256() {
    		let script = Builder::default()
    			.push_data(b"hello")
    			.push_opcode(Opcode::OP_HASH256)
    			.into_script();
    		let result = Ok(true);
    		let stack = vec!["9595c9df90075148eb06860365df33584b75bff782a510c6cd4883a419833d50".into()].into();
    		basic_test(&script, result, stack);
    	}

    	#[test]
    	fn test_hash256_invalid_stack() {
    		let script = Builder::default()
    			.push_opcode(Opcode::OP_HASH256)
    			.into_script();
    		let result = Err(Error::InvalidStackOperation);
    		basic_test(&script, result, Stack::default());
    	}

    	#[test]
    	fn test_ripemd160() {
    		let script = Builder::default()
    			.push_data(b"hello")
    			.push_opcode(Opcode::OP_RIPEMD160)
    			.into_script();
    		let result = Ok(true);
    		let stack = vec!["108f07b8382412612c048d07d13f814118445acd".into()].into();
    		basic_test(&script, result, stack);
    	}

    	#[test]
    	fn test_ripemd160_invalid_stack() {
    		let script = Builder::default()
    			.push_opcode(Opcode::OP_RIPEMD160)
    			.into_script();
    		let result = Err(Error::InvalidStackOperation);
    		basic_test(&script, result, Stack::default());
    	}

    	#[test]
    	fn test_sha1() {
    		let script = Builder::default()
    			.push_data(b"hello")
    			.push_opcode(Opcode::OP_SHA1)
    			.into_script();
    		let result = Ok(true);
    		let stack = vec!["aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d".into()].into();
    		basic_test(&script, result, stack);
    	}

    	#[test]
    	fn test_sha1_invalid_stack() {
    		let script = Builder::default()
    			.push_opcode(Opcode::OP_SHA1)
    			.into_script();
    		let result = Err(Error::InvalidStackOperation);
    		basic_test(&script, result, Stack::default());
    	}

    	#[test]
    	fn test_sha256() {
    		let script = Builder::default()
    			.push_data(b"hello")
    			.push_opcode(Opcode::OP_SHA256)
    			.into_script();
    		let result = Ok(true);
    		let stack = vec!["2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824".into()].into();
    		basic_test(&script, result, stack);
    	}

    	#[test]
    	fn test_sha256_invalid_stack() {
    		let script = Builder::default()
    			.push_opcode(Opcode::OP_SHA256)
    			.into_script();
    		let result = Err(Error::InvalidStackOperation);
    		basic_test(&script, result, Stack::default());
    	}

    	#[test]
    	fn test_1add() {
    		let script = Builder::default()
    			.push_num(5.into())
    			.push_opcode(Opcode::OP_1ADD)
    			.into_script();
    		let result = Ok(true);
    		let stack = vec![Num::from(6).to_bytes()].into();
    		basic_test(&script, result, stack);
    	}

    	#[test]
    	fn test_1add_invalid_stack() {
    		let script = Builder::default()
    			.push_opcode(Opcode::OP_1ADD)
    			.into_script();
    		let result = Err(Error::InvalidStackOperation);
    		basic_test(&script, result, Stack::default());
    	}

    	#[test]
    	fn test_1sub() {
    		let script = Builder::default()
    			.push_num(5.into())
    			.push_opcode(Opcode::OP_1SUB)
    			.into_script();
    		let result = Ok(true);
    		let stack = vec![Num::from(4).to_bytes()].into();
    		basic_test(&script, result, stack);
    	}

    	#[test]
    	fn test_1sub_invalid_stack() {
    		let script = Builder::default()
    			.push_opcode(Opcode::OP_1SUB)
    			.into_script();
    		let result = Err(Error::InvalidStackOperation);
    		basic_test(&script, result, Stack::default());
    	}

    	#[test]
    	fn test_negate() {
    		let script = Builder::default()
    			.push_num(5.into())
    			.push_opcode(Opcode::OP_NEGATE)
    			.into_script();
    		let result = Ok(true);
    		let stack = vec![Num::from(-5).to_bytes()].into();
    		basic_test(&script, result, stack);
    	}

    	#[test]
    	fn test_negate_negative() {
    		let script = Builder::default()
    			.push_num((-5).into())
    			.push_opcode(Opcode::OP_NEGATE)
    			.into_script();
    		let result = Ok(true);
    		let stack = vec![Num::from(5).to_bytes()].into();
    		basic_test(&script, result, stack);
    	}

    	#[test]
    	fn test_negate_invalid_stack() {
    		let script = Builder::default()
    			.push_opcode(Opcode::OP_NEGATE)
    			.into_script();
    		let result = Err(Error::InvalidStackOperation);
    		basic_test(&script, result, Stack::default());
    	}

    	#[test]
    	fn test_abs() {
    		let script = Builder::default()
    			.push_num(5.into())
    			.push_opcode(Opcode::OP_ABS)
    			.into_script();
    		let result = Ok(true);
    		let stack = vec![Num::from(5).to_bytes()].into();
    		basic_test(&script, result, stack);
    	}

    	#[test]
    	fn test_abs_negative() {
    		let script = Builder::default()
    			.push_num((-5).into())
    			.push_opcode(Opcode::OP_ABS)
    			.into_script();
    		let result = Ok(true);
    		let stack = vec![Num::from(5).to_bytes()].into();
    		basic_test(&script, result, stack);
    	}

    	#[test]
    	fn test_abs_invalid_stack() {
    		let script = Builder::default()
    			.push_opcode(Opcode::OP_ABS)
    			.into_script();
    		let result = Err(Error::InvalidStackOperation);
    		basic_test(&script, result, Stack::default());
    	}

    	#[test]
    	fn test_not() {
    		let script = Builder::default()
    			.push_num(4.into())
    			.push_opcode(Opcode::OP_NOT)
    			.into_script();
    		let result = Ok(false);
    		let stack = vec![Num::from(0).to_bytes()].into();
    		basic_test(&script, result, stack);
    	}

    	#[test]
    	fn test_not_zero() {
    		let script = Builder::default()
    			.push_num(0.into())
    			.push_opcode(Opcode::OP_NOT)
    			.into_script();
    		let result = Ok(true);
    		let stack = vec![Num::from(1).to_bytes()].into();
    		basic_test(&script, result, stack);
    	}

    	#[test]
    	fn test_not_invalid_stack() {
    		let script = Builder::default()
    			.push_opcode(Opcode::OP_NOT)
    			.into_script();
    		let result = Err(Error::InvalidStackOperation);
    		basic_test(&script, result, Stack::default());
    	}

    	#[test]
    	fn test_0notequal() {
    		let script = Builder::default()
    			.push_num(4.into())
    			.push_opcode(Opcode::OP_0NOTEQUAL)
    			.into_script();
    		let result = Ok(true);
    		let stack = vec![Num::from(1).to_bytes()].into();
    		basic_test(&script, result, stack);
    	}

    	#[test]
    	fn test_0notequal_zero() {
    		let script = Builder::default()
    			.push_num(0.into())
    			.push_opcode(Opcode::OP_0NOTEQUAL)
    			.into_script();
    		let result = Ok(false);
    		let stack = vec![Num::from(0).to_bytes()].into();
    		basic_test(&script, result, stack);
    	}

    	#[test]
    	fn test_0notequal_invalid_stack() {
    		let script = Builder::default()
    			.push_opcode(Opcode::OP_0NOTEQUAL)
    			.into_script();
    		let result = Err(Error::InvalidStackOperation);
    		basic_test(&script, result, Stack::default());
    	}

    	#[test]
    	fn test_add() {
    		let script = Builder::default()
    			.push_num(2.into())
    			.push_num(3.into())
    			.push_opcode(Opcode::OP_ADD)
    			.into_script();
    		let result = Ok(true);
    		let stack = vec![Num::from(5).to_bytes()].into();
    		basic_test(&script, result, stack);
    	}

    	#[test]
    	fn test_add_invalid_stack() {
    		let script = Builder::default()
    			.push_num(2.into())
    			.push_opcode(Opcode::OP_ADD)
    			.into_script();
    		let result = Err(Error::InvalidStackOperation);
    		basic_test(&script, result, Stack::default());
    	}

    	#[test]
    	fn test_sub() {
    		let script = Builder::default()
    			.push_num(3.into())
    			.push_num(2.into())
    			.push_opcode(Opcode::OP_SUB)
    			.into_script();
    		let result = Ok(true);
    		let stack = vec![Num::from(1).to_bytes()].into();
    		basic_test(&script, result, stack);
    	}

    	#[test]
    	fn test_sub_invalid_stack() {
    		let script = Builder::default()
    			.push_num(2.into())
    			.push_opcode(Opcode::OP_SUB)
    			.into_script();
    		let result = Err(Error::InvalidStackOperation);
    		basic_test(&script, result, Stack::default());
    	}

    	#[test]
    	fn test_booland() {
    		let script = Builder::default()
    			.push_num(3.into())
    			.push_num(2.into())
    			.push_opcode(Opcode::OP_BOOLAND)
    			.into_script();
    		let result = Ok(true);
    		let stack = vec![Num::from(1).to_bytes()].into();
    		basic_test(&script, result, stack);
    	}

    	#[test]
    	fn test_booland_first() {
    		let script = Builder::default()
    			.push_num(2.into())
    			.push_num(0.into())
    			.push_opcode(Opcode::OP_BOOLAND)
    			.into_script();
    		let result = Ok(false);
    		let stack = vec![Num::from(0).to_bytes()].into();
    		basic_test(&script, result, stack);
    	}

    	#[test]
    	fn test_booland_second() {
    		let script = Builder::default()
    			.push_num(0.into())
    			.push_num(3.into())
    			.push_opcode(Opcode::OP_BOOLAND)
    			.into_script();
    		let result = Ok(false);
    		let stack = vec![Num::from(0).to_bytes()].into();
    		basic_test(&script, result, stack);
    	}

    	#[test]
    	fn test_booland_none() {
    		let script = Builder::default()
    			.push_num(0.into())
    			.push_num(0.into())
    			.push_opcode(Opcode::OP_BOOLAND)
    			.into_script();
    		let result = Ok(false);
    		let stack = vec![Num::from(0).to_bytes()].into();
    		basic_test(&script, result, stack);
    	}

    	#[test]
    	fn test_booland_invalid_stack() {
    		let script = Builder::default()
    			.push_num(0.into())
    			.push_opcode(Opcode::OP_BOOLAND)
    			.into_script();
    		let result = Err(Error::InvalidStackOperation);
    		basic_test(&script, result, Stack::default());
    	}

    	#[test]
    	fn test_boolor() {
    		let script = Builder::default()
    			.push_num(3.into())
    			.push_num(2.into())
    			.push_opcode(Opcode::OP_BOOLOR)
    			.into_script();
    		let result = Ok(true);
    		let stack = vec![Num::from(1).to_bytes()].into();
    		basic_test(&script, result, stack);
    	}

    	#[test]
    	fn test_boolor_first() {
    		let script = Builder::default()
    			.push_num(2.into())
    			.push_num(0.into())
    			.push_opcode(Opcode::OP_BOOLOR)
    			.into_script();
    		let result = Ok(true);
    		let stack = vec![Num::from(1).to_bytes()].into();
    		basic_test(&script, result, stack);
    	}

    	#[test]
    	fn test_boolor_second() {
    		let script = Builder::default()
    			.push_num(0.into())
    			.push_num(3.into())
    			.push_opcode(Opcode::OP_BOOLOR)
    			.into_script();
    		let result = Ok(true);
    		let stack = vec![Num::from(1).to_bytes()].into();
    		basic_test(&script, result, stack);
    	}

    	#[test]
    	fn test_boolor_none() {
    		let script = Builder::default()
    			.push_num(0.into())
    			.push_num(0.into())
    			.push_opcode(Opcode::OP_BOOLOR)
    			.into_script();
    		let result = Ok(false);
    		let stack = vec![Num::from(0).to_bytes()].into();
    		basic_test(&script, result, stack);
    	}

    	#[test]
    	fn test_boolor_invalid_stack() {
    		let script = Builder::default()
    			.push_num(0.into())
    			.push_opcode(Opcode::OP_BOOLOR)
    			.into_script();
    		let result = Err(Error::InvalidStackOperation);
    		basic_test(&script, result, Stack::default());
    	}

    	#[test]
    	fn test_numequal() {
    		let script = Builder::default()
    			.push_num(2.into())
    			.push_num(2.into())
    			.push_opcode(Opcode::OP_NUMEQUAL)
    			.into_script();
    		let result = Ok(true);
    		let stack = vec![Num::from(1).to_bytes()].into();
    		basic_test(&script, result, stack);
    	}

    	#[test]
    	fn test_numequal_not() {
    		let script = Builder::default()
    			.push_num(2.into())
    			.push_num(3.into())
    			.push_opcode(Opcode::OP_NUMEQUAL)
    			.into_script();
    		let result = Ok(false);
    		let stack = vec![Num::from(0).to_bytes()].into();
    		basic_test(&script, result, stack);
    	}

    	#[test]
    	fn test_numequal_invalid_stack() {
    		let script = Builder::default()
    			.push_num(2.into())
    			.push_opcode(Opcode::OP_NUMEQUAL)
    			.into_script();
    		let result = Err(Error::InvalidStackOperation);
    		basic_test(&script, result, Stack::default());
    	}

    	#[test]
    	fn test_numequalverify() {
    		let script = Builder::default()
    			.push_num(2.into())
    			.push_num(2.into())
    			.push_opcode(Opcode::OP_NUMEQUALVERIFY)
    			.into_script();
    		let result = Ok(false);
    		basic_test(&script, result, Stack::default());
    	}

    	#[test]
    	fn test_numequalverify_failed() {
    		let script = Builder::default()
    			.push_num(2.into())
    			.push_num(3.into())
    			.push_opcode(Opcode::OP_NUMEQUALVERIFY)
    			.into_script();
    		let result = Err(Error::NumEqualVerify);
    		basic_test(&script, result, Stack::default());
    	}

    	#[test]
    	fn test_numequalverify_invalid_stack() {
    		let script = Builder::default()
    			.push_num(2.into())
    			.push_opcode(Opcode::OP_NUMEQUALVERIFY)
    			.into_script();
    		let result = Err(Error::InvalidStackOperation);
    		basic_test(&script, result, Stack::default());
    	}

    	#[test]
    	fn test_numnotequal() {
    		let script = Builder::default()
    			.push_num(2.into())
    			.push_num(3.into())
    			.push_opcode(Opcode::OP_NUMNOTEQUAL)
    			.into_script();
    		let result = Ok(true);
    		let stack = vec![Num::from(1).to_bytes()].into();
    		basic_test(&script, result, stack);
    	}

    	#[test]
    	fn test_numnotequal_not() {
    		let script = Builder::default()
    			.push_num(2.into())
    			.push_num(2.into())
    			.push_opcode(Opcode::OP_NUMNOTEQUAL)
    			.into_script();
    		let result = Ok(false);
    		let stack = vec![Num::from(0).to_bytes()].into();
    		basic_test(&script, result, stack);
    	}

    	#[test]
    	fn test_numnotequal_invalid_stack() {
    		let script = Builder::default()
    			.push_num(2.into())
    			.push_opcode(Opcode::OP_NUMNOTEQUAL)
    			.into_script();
    		let result = Err(Error::InvalidStackOperation);
    		basic_test(&script, result, Stack::default());
    	}

    	#[test]
    	fn test_lessthan() {
    		let script = Builder::default()
    			.push_num(2.into())
    			.push_num(3.into())
    			.push_opcode(Opcode::OP_LESSTHAN)
    			.into_script();
    		let result = Ok(true);
    		let stack = vec![Num::from(1).to_bytes()].into();
    		basic_test(&script, result, stack);
    	}

    	#[test]
    	fn test_lessthan_not() {
    		let script = Builder::default()
    			.push_num(2.into())
    			.push_num(2.into())
    			.push_opcode(Opcode::OP_LESSTHAN)
    			.into_script();
    		let result = Ok(false);
    		let stack = vec![Num::from(0).to_bytes()].into();
    		basic_test(&script, result, stack);
    	}

    	#[test]
    	fn test_lessthan_invalid_stack() {
    		let script = Builder::default()
    			.push_num(2.into())
    			.push_opcode(Opcode::OP_LESSTHAN)
    			.into_script();
    		let result = Err(Error::InvalidStackOperation);
    		basic_test(&script, result, Stack::default());
    	}

    	#[test]
    	fn test_greaterthan() {
    		let script = Builder::default()
    			.push_num(3.into())
    			.push_num(2.into())
    			.push_opcode(Opcode::OP_GREATERTHAN)
    			.into_script();
    		let result = Ok(true);
    		let stack = vec![Num::from(1).to_bytes()].into();
    		basic_test(&script, result, stack);
    	}

    	#[test]
    	fn test_greaterthan_not() {
    		let script = Builder::default()
    			.push_num(2.into())
    			.push_num(2.into())
    			.push_opcode(Opcode::OP_GREATERTHAN)
    			.into_script();
    		let result = Ok(false);
    		let stack = vec![Num::from(0).to_bytes()].into();
    		basic_test(&script, result, stack);
    	}

    	#[test]
    	fn test_greaterthan_invalid_stack() {
    		let script = Builder::default()
    			.push_num(2.into())
    			.push_opcode(Opcode::OP_GREATERTHAN)
    			.into_script();
    		let result = Err(Error::InvalidStackOperation);
    		basic_test(&script, result, Stack::default());
    	}

    	#[test]
    	fn test_lessthanorequal() {
    		let script = Builder::default()
    			.push_num(2.into())
    			.push_num(3.into())
    			.push_opcode(Opcode::OP_LESSTHANOREQUAL)
    			.into_script();
    		let result = Ok(true);
    		let stack = vec![Num::from(1).to_bytes()].into();
    		basic_test(&script, result, stack);
    	}

    	#[test]
    	fn test_lessthanorequal_equal() {
    		let script = Builder::default()
    			.push_num(2.into())
    			.push_num(2.into())
    			.push_opcode(Opcode::OP_LESSTHANOREQUAL)
    			.into_script();
    		let result = Ok(true);
    		let stack = vec![Num::from(1).to_bytes()].into();
    		basic_test(&script, result, stack);
    	}

    	#[test]
    	fn test_lessthanorequal_not() {
    		let script = Builder::default()
    			.push_num(2.into())
    			.push_num(1.into())
    			.push_opcode(Opcode::OP_LESSTHANOREQUAL)
    			.into_script();
    		let result = Ok(false);
    		let stack = vec![Num::from(0).to_bytes()].into();
    		basic_test(&script, result, stack);
    	}

    	#[test]
    	fn test_lessthanorequal_invalid_stack() {
    		let script = Builder::default()
    			.push_num(2.into())
    			.push_opcode(Opcode::OP_LESSTHANOREQUAL)
    			.into_script();
    		let result = Err(Error::InvalidStackOperation);
    		basic_test(&script, result, Stack::default());
    	}

    	#[test]
    	fn test_greaterthanorequal() {
    		let script = Builder::default()
    			.push_num(3.into())
    			.push_num(2.into())
    			.push_opcode(Opcode::OP_GREATERTHANOREQUAL)
    			.into_script();
    		let result = Ok(true);
    		let stack = vec![Num::from(1).to_bytes()].into();
    		basic_test(&script, result, stack);
    	}

    	#[test]
    	fn test_greaterthanorequal_equal() {
    		let script = Builder::default()
    			.push_num(2.into())
    			.push_num(2.into())
    			.push_opcode(Opcode::OP_GREATERTHANOREQUAL)
    			.into_script();
    		let result = Ok(true);
    		let stack = vec![Num::from(1).to_bytes()].into();
    		basic_test(&script, result, stack);
    	}

    	#[test]
    	fn test_greaterthanorequal_not() {
    		let script = Builder::default()
    			.push_num(1.into())
    			.push_num(2.into())
    			.push_opcode(Opcode::OP_GREATERTHANOREQUAL)
    			.into_script();
    		let result = Ok(false);
    		let stack = vec![Num::from(0).to_bytes()].into();
    		basic_test(&script, result, stack);
    	}

    	#[test]
    	fn test_greaterthanorequal_invalid_stack() {
    		let script = Builder::default()
    			.push_num(2.into())
    			.push_opcode(Opcode::OP_GREATERTHANOREQUAL)
    			.into_script();
    		let result = Err(Error::InvalidStackOperation);
    		basic_test(&script, result, Stack::default());
    	}

    	#[test]
    	fn test_min() {
    		let script = Builder::default()
    			.push_num(2.into())
    			.push_num(3.into())
    			.push_opcode(Opcode::OP_MIN)
    			.into_script();
    		let result = Ok(true);
    		let stack = vec![Num::from(2).to_bytes()].into();
    		basic_test(&script, result, stack);
    	}

    	#[test]
    	fn test_min_second() {
    		let script = Builder::default()
    			.push_num(4.into())
    			.push_num(3.into())
    			.push_opcode(Opcode::OP_MIN)
    			.into_script();
    		let result = Ok(true);
    		let stack = vec![Num::from(3).to_bytes()].into();
    		basic_test(&script, result, stack);
    	}

    	#[test]
    	fn test_min_invalid_stack() {
    		let script = Builder::default()
    			.push_num(4.into())
    			.push_opcode(Opcode::OP_MIN)
    			.into_script();
    		let result = Err(Error::InvalidStackOperation);
    		basic_test(&script, result, Stack::default());
    	}

    	#[test]
    	fn test_max() {
    		let script = Builder::default()
    			.push_num(2.into())
    			.push_num(3.into())
    			.push_opcode(Opcode::OP_MAX)
    			.into_script();
    		let result = Ok(true);
    		let stack = vec![Num::from(3).to_bytes()].into();
    		basic_test(&script, result, stack);
    	}

    	#[test]
    	fn test_max_second() {
    		let script = Builder::default()
    			.push_num(4.into())
    			.push_num(3.into())
    			.push_opcode(Opcode::OP_MAX)
    			.into_script();
    		let result = Ok(true);
    		let stack = vec![Num::from(4).to_bytes()].into();
    		basic_test(&script, result, stack);
    	}

    	#[test]
    	fn test_max_invalid_stack() {
    		let script = Builder::default()
    			.push_num(4.into())
    			.push_opcode(Opcode::OP_MAX)
    			.into_script();
    		let result = Err(Error::InvalidStackOperation);
    		basic_test(&script, result, Stack::default());
    	}

    	#[test]
    	fn test_within() {
    		let script = Builder::default()
    			.push_num(3.into())
    			.push_num(2.into())
    			.push_num(4.into())
    			.push_opcode(Opcode::OP_WITHIN)
    			.into_script();
    		let result = Ok(true);
    		let stack = vec![vec![1].into()].into();
    		basic_test(&script, result, stack);
    	}

    	#[test]
    	fn test_within_not() {
    		let script = Builder::default()
    			.push_num(3.into())
    			.push_num(5.into())
    			.push_num(4.into())
    			.push_opcode(Opcode::OP_WITHIN)
    			.into_script();
    		let result = Ok(false);
    		let stack = vec![vec![0].into()].into();
    		basic_test(&script, result, stack);
    	}

    	#[test]
    	fn test_within_invalid_stack() {
    		let script = Builder::default()
    			.push_num(5.into())
    			.push_num(4.into())
    			.push_opcode(Opcode::OP_WITHIN)
    			.into_script();
    		let result = Err(Error::InvalidStackOperation);
    		basic_test(&script, result, Stack::default());
    	}

    	#[test]
    	fn test_within_testnet_block_519() {
    		let script = Builder::default()
    			.push_num(1.into())
    			.push_num(0.into())
    			.push_num(1.into())
    			.push_opcode(Opcode::OP_WITHIN)
    			.push_opcode(Opcode::OP_NOT)
    			.into_script();
    		let result = Ok(true);
    		let stack = vec![vec![1].into()].into();
    		basic_test(&script, result, stack);
    	}

    	// https://blockchain.info/rawtx/3f285f083de7c0acabd9f106a43ec42687ab0bebe2e6f0d529db696794540fea
    	#[test]
    	fn test_check_transaction_signature() {
    		let tx: Transaction = "0100000001484d40d45b9ea0d652fca8258ab7caa42541eb52975857f96fb50cd732c8b481000000008a47304402202cb265bf10707bf49346c3515dd3d16fc454618c58ec0a0ff448a676c54ff71302206c6624d762a1fcef4618284ead8f08678ac05b13c84235f1654e6ad168233e8201410414e301b2328f17442c0b8310d787bf3d8a404cfbd0704f135b6ad4b2d3ee751310f981926e53a6e8c39bd7d3fefd576c543cce493cbac06388f2651d1aacbfcdffffffff0162640100000000001976a914c8e90996c7c6080ee06284600c684ed904d14c5c88ac00000000".into();
    		let signer: TransactionInputSigner = tx.into();
    		let checker = TransactionSignatureChecker {
    			signer: signer,
    			input_index: 0,
    			input_amount: 0,
    		};
    		let input: Script = "47304402202cb265bf10707bf49346c3515dd3d16fc454618c58ec0a0ff448a676c54ff71302206c6624d762a1fcef4618284ead8f08678ac05b13c84235f1654e6ad168233e8201410414e301b2328f17442c0b8310d787bf3d8a404cfbd0704f135b6ad4b2d3ee751310f981926e53a6e8c39bd7d3fefd576c543cce493cbac06388f2651d1aacbfcd".into();
    		let output: Script = "76a914df3bd30160e6c6145baaf2c88a8844c13a00d1d588ac".into();
    		let flags = VerificationFlags::default()
    			.verify_p2sh(true);
    		assert_eq!(verify_script(&input, &output, &ScriptWitness::default(), &flags, &checker, SignatureVersion::Base), Ok(()));
    	}

    	// https://blockchain.info/rawtx/02b082113e35d5386285094c2829e7e2963fa0b5369fb7f4b79c4c90877dcd3d
    	#[test]
    	fn test_check_transaction_multisig() {
    		let tx: Transaction = " ".into();
    		let signer: TransactionInputSigner = tx.into();
    		let checker = TransactionSignatureChecker {
    			signer: signer,
    			input_index: 0,
    			input_amount: 0,
    		};
    		let input: Script = " ".into();
    		let output: Script = "a9141a8b0026343166625c7475f01e48b5ede8c0252e87".into();
    		let flags = VerificationFlags::default()
    			.verify_p2sh(true);
    		assert_eq!(verify_script(&input, &output, &ScriptWitness::default(), &flags, &checker, SignatureVersion::Base), Ok(()));
    	}

    	// https://blockchain.info/en/tx/12b5633bad1f9c167d523ad1aa1947b2732a865bf5414eab2f9e5ae5d5c191ba?show_adv=true
    	#[test]
    	fn test_transaction_with_high_s_signature() {
    		let tx: Transaction = " ".into();
    		let signer: TransactionInputSigner = tx.into();
    		let checker = TransactionSignatureChecker {
    			signer: signer,
    			input_index: 0,
    			input_amount: 0,
    		};
    		let input: Script = "483045022052ffc1929a2d8bd365c6a2a4e3421711b4b1e1b8781698ca9075807b4227abcb0221009984107ddb9e3813782b095d0d84361ed4c76e5edaf6561d252ae162c2341cfb01".into();
    		let output: Script = "410411db93e1dcdb8a016b49840f8c53bc1eb68a382e97b1482ecad7b148a6909a5cb2e0eaddfb84ccf9744464f82e160bfa9b8b64f9d4c03f999b8643f656b412a3ac".into();
    		let flags = VerificationFlags::default()
    			.verify_p2sh(true);
    		assert_eq!(verify_script(&input, &output, &ScriptWitness::default(), &flags, &checker, SignatureVersion::Base), Ok(()));
    	}

    	// https://blockchain.info/rawtx/fb0a1d8d34fa5537e461ac384bac761125e1bfa7fec286fa72511240fa66864d
    	#[test]
    	fn test_transaction_from_124276() {
    		let tx: Transaction = "01000000012316aac445c13ff31af5f3d1e2cebcada83e54ba10d15e01f49ec28bddc285aa000000008e4b3048022200002b83d59c1d23c08efd82ee0662fec23309c3adbcbd1f0b8695378db4b14e736602220000334a96676e58b1bb01784cb7c556dd8ce1c220171904da22e18fe1e7d1510db5014104d0fe07ff74c9ef5b00fed1104fad43ecf72dbab9e60733e4f56eacf24b20cf3b8cd945bcabcc73ba0158bf9ce769d43e94bd58c5c7e331a188922b3fe9ca1f5affffffff01c0c62d00000000001976a9147a2a3b481ca80c4ba7939c54d9278e50189d94f988ac00000000".into();
    		let signer: TransactionInputSigner = tx.into();
    		let checker = TransactionSignatureChecker {
    			signer: signer,
    			input_index: 0,
    			input_amount: 0,
    		};
    		let input: Script = "4b3048022200002b83d59c1d23c08efd82ee0662fec23309c3adbcbd1f0b8695378db4b14e736602220000334a96676e58b1bb01784cb7c556dd8ce1c220171904da22e18fe1e7d1510db5014104d0fe07ff74c9ef5b00fed1104fad43ecf72dbab9e60733e4f56eacf24b20cf3b8cd945bcabcc73ba0158bf9ce769d43e94bd58c5c7e331a188922b3fe9ca1f5a".into();
    		let output: Script = "76a9147a2a3b481ca80c4ba7939c54d9278e50189d94f988ac".into();
    		let flags = VerificationFlags::default()
    			.verify_p2sh(true);
    		assert_eq!(verify_script(&input, &output, &ScriptWitness::default(), &flags, &checker, SignatureVersion::Base), Ok(()));
    	}

    	// https://blockchain.info/rawtx/eb3b82c0884e3efa6d8b0be55b4915eb20be124c9766245bcc7f34fdac32bccb
    	#[test]
    	fn test_transaction_bip65() {
    		let tx: Transaction = " ".into();
    		let signer: TransactionInputSigner = tx.into();
    		let checker = TransactionSignatureChecker {
    			signer: signer,
    			input_index: 1,
    			input_amount: 0,
    		};
    		let input: Script = "004730440220276d6dad3defa37b5f81add3992d510d2f44a317fd85e04f93a1e2daea64660202200f862a0da684249322ceb8ed842fb8c859c0cb94c81e1c5308b4868157a428ee01ab51210232abdc893e7f0631364d7fd01cb33d24da45329a00357b3a7886211ab414d55a51ae".into();
    		let output: Script = "142a9bc5447d664c1d0141392a842d23dba45c4f13b175".into();

    		let flags = VerificationFlags::default()
    			.verify_p2sh(true);
    		assert_eq!(verify_script(&input, &output, &ScriptWitness::default(), &flags, &checker, SignatureVersion::Base), Ok(()));

    		let flags = VerificationFlags::default()
    			.verify_p2sh(true)
    			.verify_locktime(true);
    		assert_eq!(verify_script(&input, &output, &ScriptWitness::default(), &flags, &checker, SignatureVersion::Base), Err(Error::NumberOverflow));
    	}

    	// https://blockchain.info/rawtx/54fabd73f1d20c980a0686bf0035078e07f69c58437e4d586fb29aa0bee9814f
    	#[test]
    	fn test_arithmetic_correct_arguments_order() {
    		let tx: Transaction = "01000000010c0e314bd7bb14721b3cfd8e487cd6866173354f87ca2cf4d13c8d3feb4301a6000000004a483045022100d92e4b61452d91a473a43cde4b469a472467c0ba0cbd5ebba0834e4f4762810402204802b76b7783db57ac1f61d2992799810e173e91055938750815b6d8a675902e014fffffffff0140548900000000001976a914a86e8ee2a05a44613904e18132e49b2448adc4e688ac00000000".into();
    		let signer: TransactionInputSigner = tx.into();
    		let checker = TransactionSignatureChecker {
    			signer: signer,
    			input_index: 0,
    			input_amount: 0,
    		};
    		let input: Script = "483045022100d92e4b61452d91a473a43cde4b469a472467c0ba0cbd5ebba0834e4f4762810402204802b76b7783db57ac1f61d2992799810e173e91055938750815b6d8a675902e014f".into();
    		let output: Script = "76009f69905160a56b210378d430274f8c5ec1321338151e9f27f4c676a008bdf8638d07c0b6be9ab35c71ad6c".into();
    		let flags = VerificationFlags::default();
    		assert_eq!(verify_script(&input, &output, &ScriptWitness::default(), &flags, &checker, SignatureVersion::Base), Ok(()));
    	}

    	#[test]
    	fn test_invalid_opcode_in_dead_execution_path_b83() {
    		let script = Builder::default()
    			.push_opcode(Opcode::OP_0)
    			.push_opcode(Opcode::OP_IF)
    			.push_invalid_opcode()
    			.push_opcode(Opcode::OP_ELSE)
    			.push_opcode(Opcode::OP_1)
    			.push_opcode(Opcode::OP_ENDIF)
    			.into_script();
    		let result = Ok(true);
    		basic_test(&script, result, vec![vec![1].into()].into());
    	}

    	#[test]
    	fn test_skipping_sequencetimeverify() {
    		let script = Builder::default()
    			.push_opcode(Opcode::OP_1)
    			.push_opcode(Opcode::OP_NOP1)
    			.push_opcode(Opcode::OP_CHECKLOCKTIMEVERIFY)
    			.push_opcode(Opcode::OP_CHECKSEQUENCEVERIFY)
    			.push_opcode(Opcode::OP_NOP4)
    			.push_opcode(Opcode::OP_NOP5)
    			.push_opcode(Opcode::OP_NOP6)
    			.push_opcode(Opcode::OP_NOP7)
    			.push_opcode(Opcode::OP_NOP8)
    			.push_opcode(Opcode::OP_NOP9)
    			.push_opcode(Opcode::OP_NOP10)
    			.push_opcode(Opcode::OP_1)
    			.push_opcode(Opcode::OP_EQUAL)
    			.into_script();
    		let result = Ok(true);
    		basic_test(&script, result, vec![vec![1].into()].into());
    	}

    	// https://webbtc.com/tx/5df1375ffe61ac35ca178ebb0cab9ea26dedbd0e96005dfcee7e379fa513232f
    	#[test]
    	fn test_transaction_find_and_delete() {
    		let tx: Transaction = " ".into();
    		let signer: TransactionInputSigner = tx.into();
    		let checker = TransactionSignatureChecker {
    			signer: signer,
    			input_index: 1,
    			input_amount: 0,
    		};
    		let input: Script = " ".into();
    		let output: Script = "A914D8DACDADB7462AE15CD906F1878706D0DA8660E687".into();

    		let flags = VerificationFlags::default()
    			.verify_p2sh(true);
    		assert_eq!(verify_script(&input, &output, &ScriptWitness::default(), &flags, &checker, SignatureVersion::Base), Ok(()));
    	}

    	#[test]
    	fn test_script_with_forkid_signature() {
    		use sign::UnsignedTransactionInput;
    		use chain::{OutPoint, TransactionOutput};

    		let key_pair = KeyPair::from_private(Private { network: Network::Mainnet, secret: 1.into(), compressed: false, }).unwrap();
    		let redeem_script = Builder::default()
    			.push_data(key_pair.public())
    			.push_opcode(Opcode::OP_CHECKSIG)
    			.into_script();

    		let amount = 12345000000000;
    		let sighashtype = 0x41; // All + ForkId
    		let checker = TransactionSignatureChecker {
    			input_index: 0,
    			input_amount: amount,
    			signer: TransactionInputSigner {
    				version: 1,
    				inputs: vec![
    					UnsignedTransactionInput {
    						previous_output: OutPoint {
    							hash: 0u8.into(),
    							index: 0xffffffff,
    						},
    						sequence: 0xffffffff,
    					},
    				],
    				outputs: vec![
    					TransactionOutput {
    						value: amount,
    						script_pubkey: redeem_script.to_bytes(),
    					},
    				],
    				lock_time: 0,
    			},
    		};

    		let script_pubkey = redeem_script;
    		let flags = VerificationFlags::default();

    		// valid signature
    		{
    			let signed_input = checker.signer.signed_input(&key_pair, 0, amount, &script_pubkey, SignatureVersion::ForkId, sighashtype);
    			let script_sig = signed_input.script_sig.into();

    			assert_eq!(verify_script(&script_sig, &script_pubkey, &ScriptWitness::default(), &flags, &checker, SignatureVersion::ForkId), Ok(()));
    		}

    		// signature with wrong amount
    		{
    			let signed_input = checker.signer.signed_input(&key_pair, 0, amount + 1, &script_pubkey, SignatureVersion::ForkId, sighashtype);
    			let script_sig = signed_input.script_sig.into();

    			assert_eq!(verify_script(&script_sig, &script_pubkey, &ScriptWitness::default(), &flags, &checker, SignatureVersion::ForkId), Err(Error::EvalFalse));
    		}

    		// fork-id signature passed when not expected
    		{
    			let signed_input = checker.signer.signed_input(&key_pair, 0, amount + 1, &script_pubkey, SignatureVersion::ForkId, sighashtype);
    			let script_sig = signed_input.script_sig.into();

    			assert_eq!(verify_script(&script_sig, &script_pubkey, &ScriptWitness::default(), &flags, &checker, SignatureVersion::Base), Err(Error::EvalFalse));
    		}

    		// non-fork-id signature passed when expected
    		{
    			let signed_input = checker.signer.signed_input(&key_pair, 0, amount + 1, &script_pubkey, SignatureVersion::Base, 1);
    			let script_sig = signed_input.script_sig.into();

    			assert_eq!(verify_script(&script_sig, &script_pubkey, &ScriptWitness::default(), &flags.verify_strictenc(true), &checker, SignatureVersion::ForkId), Err(Error::SignatureMustUseForkId));
    		}
    	}

    	fn run_witness_test(script_sig: Script, script_pubkey: Script, script_witness: Vec<Bytes>, flags: VerificationFlags, amount: u64) -> Result<(), Error> {
    		use chain::{TransactionInput, OutPoint, TransactionOutput};

    		let tx1 = Transaction {
    			version: 1,
    			inputs: vec![TransactionInput {
    				previous_output: OutPoint {
    					hash: Default::default(),
    					index: 0xffffffff,
    				},
    				script_sig: Builder::default().push_num(0.into()).push_num(0.into()).into_bytes(),
    				sequence: 0xffffffff,
    				script_witness: vec![],
    			}],
    			outputs: vec![TransactionOutput {
    				value: amount,
    				script_pubkey: script_pubkey.to_bytes(),
    			}],
    			lock_time: 0,
    		};
    		let tx2 = Transaction {
    			version: 1,
    			inputs: vec![TransactionInput {
    				previous_output: OutPoint {
    					hash: tx1.hash(),
    					index: 0,
    				},
    				script_sig: script_sig.to_bytes(),
    				sequence: 0xffffffff,
    				script_witness: script_witness.clone(),
    			}],
    			outputs: vec![TransactionOutput {
    				value: amount,
    				script_pubkey: Builder::default().into_bytes(),
    			}],
    			lock_time: 0,
    		};

    		let checker = TransactionSignatureChecker {
    			input_index: 0,
    			input_amount: amount,
    			signer: tx2.into(),
    		};

    		verify_script(&script_sig,
    			&script_pubkey,
    			&script_witness,
    			&flags,
    			&checker,
    			SignatureVersion::Base)
    	}

    	// https://github.com/bitcoin/bitcoin/blob/7ee6c434ce8df9441abcf1718555cc7728a4c575/src/test/data/script_tests.json#L1257
    	#[test]
    	fn witness_invalid_script() {
    		assert_eq!(Err(Error::EvalFalse),
    			run_witness_test("".into(),
    				"00206e340b9cffb37a989ca544e6bb780a2c78901d3fb33738768511a30617afa01d".into(),
    				vec!["00".into()],
    				VerificationFlags::default().verify_p2sh(true).verify_witness(true),
    				0,
    			));
    	}

    	// https://github.com/bitcoin/bitcoin/blob/7ee6c434ce8df9441abcf1718555cc7728a4c575/src/test/data/script_tests.json#L1258
    	#[test]
    	fn witness_script_hash_mismatch() {
    		assert_eq!(Err(Error::WitnessProgramMismatch),
    			run_witness_test("".into(),
    				"00206e340b9cffb37a989ca544e6bb780a2c78901d3fb33738768511a30617afa01d".into(),
    				vec!["51".into()],
    				VerificationFlags::default().verify_p2sh(true).verify_witness(true),
    				0,
    			));
    	}

    	// https://github.com/bitcoin/bitcoin/blob/7ee6c434ce8df9441abcf1718555cc7728a4c575/src/test/data/script_tests.json#L1259
    	#[test]
    	fn witness_invalid_script_check_skipped() {
    		assert_eq!(Ok(()),
    			run_witness_test("".into(),
    				"00206e340b9cffb37a989ca544e6bb780a2c78901d3fb33738768511a30617afa01d".into(),
    				vec!["00".into()],
    				VerificationFlags::default(),
    				0,
    			));
    	}

    	// https://github.com/bitcoin/bitcoin/blob/7ee6c434ce8df9441abcf1718555cc7728a4c575/src/test/data/script_tests.json#L1260
    	#[test]
    	fn witness_script_hash_mismatch_check_skipped() {
    		assert_eq!(Ok(()),
    			run_witness_test("".into(),
    				"00206e340b9cffb37a989ca544e6bb780a2c78901d3fb33738768511a30617afa01d".into(),
    				vec!["51".into()],
    				VerificationFlags::default(),
    				0,
    			));
    	}

    	// https://github.com/bitcoin/bitcoin/blob/7ee6c434ce8df9441abcf1718555cc7728a4c575/src/test/data/script_tests.json#L1860
    	#[test]
    	fn witness_basic_p2wsh() {
    		assert_eq!(Ok(()),
    			run_witness_test("".into(),
    				"0020b95237b48faaa69eb078e1170be3b5cbb3fddf16d0a991e14ad274f7b33a4f64".into(),
    				vec!["304402200d461c140cfdfcf36b94961db57ae8c18d1cb80e9d95a9e47ac22470c1bf125502201c8dc1cbfef6a3ef90acbbb992ca22fe9466ee6f9d4898eda277a7ac3ab4b25101".into(),
    					"410479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8ac".into()],
    				VerificationFlags::default().verify_p2sh(true).verify_witness(true),
    				1,
    			));
    	}

    	// https://github.com/bitcoin/bitcoin/blob/7ee6c434ce8df9441abcf1718555cc7728a4c575/src/test/data/script_tests.json#L1872
    	#[test]
    	fn witness_basic_p2wpkh() {
    		assert_eq!(Ok(()),
    			run_witness_test("".into(),
    				"001491b24bf9f5288532960ac687abb035127b1d28a5".into(),
    				vec!["304402201e7216e5ccb3b61d46946ec6cc7e8c4e0117d13ac2fd4b152197e4805191c74202203e9903e33e84d9ee1dd13fb057afb7ccfb47006c23f6a067185efbc9dd780fc501".into(),
    					"0479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8".into()],
    				VerificationFlags::default().verify_p2sh(true).verify_witness(true),
    				1,
    			));
    	}

    	// https://github.com/bitcoin/bitcoin/blob/7ee6c434ce8df9441abcf1718555cc7728a4c575/src/test/data/script_tests.json#L1884
    	#[test]
    	fn witness_basic_p2sh_p2wsh() {
    		assert_eq!(Ok(()),
    			run_witness_test("220020b95237b48faaa69eb078e1170be3b5cbb3fddf16d0a991e14ad274f7b33a4f64".into(),
    				"a914f386c2ba255cc56d20cfa6ea8b062f8b5994551887".into(),
    				vec!["3044022066e02c19a513049d49349cf5311a1b012b7c4fae023795a18ab1d91c23496c22022025e216342c8e07ce8ef51e8daee88f84306a9de66236cab230bb63067ded1ad301".into(),
    					"410479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8ac".into()],
    				VerificationFlags::default().verify_p2sh(true).verify_witness(true),
    				1,
    			));
    	}

    	// https://github.com/bitcoin/bitcoin/blob/7ee6c434ce8df9441abcf1718555cc7728a4c575/src/test/data/script_tests.json#L1896
    	#[test]
    	fn witness_basic_p2sh_p2wpkh() {
    		assert_eq!(Ok(()),
    			run_witness_test("16001491b24bf9f5288532960ac687abb035127b1d28a5".into(),
    				"a91417743beb429c55c942d2ec703b98c4d57c2df5c687".into(),
    				vec!["304402200929d11561cd958460371200f82e9cae64c727a495715a31828e27a7ad57b36d0220361732ced04a6f97351ecca21a56d0b8cd4932c1da1f8f569a2b68e5e48aed7801".into(),
    					"0479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8".into()],
    				VerificationFlags::default().verify_p2sh(true).verify_witness(true),
    				1,
    			));
    	}

    	// https://github.com/bitcoin/bitcoin/blob/7ee6c434ce8df9441abcf1718555cc7728a4c575/src/test/data/script_tests.json#L1908
    	#[test]
    	fn witness_basic_p2wsh_with_wrong_key() {
    		assert_eq!(Err(Error::EvalFalse),
    			run_witness_test("".into(),
    				"0020ac8ebd9e52c17619a381fa4f71aebb696087c6ef17c960fd0587addad99c0610".into(),
    				vec!["304402202589f0512cb2408fb08ed9bd24f85eb3059744d9e4f2262d0b7f1338cff6e8b902206c0978f449693e0578c71bc543b11079fd0baae700ee5e9a6bee94db490af9fc01".into(),
    					"41048282263212c609d9ea2a6e3e172de238d8c39cabd5ac1ca10646e23fd5f5150811f8a8098557dfe45e8256e830b60ace62d613ac2f7b17bed31b6eaff6e26cafac".into()],
    				VerificationFlags::default().verify_p2sh(true).verify_witness(true),
    				0,
    			));
    	}

    	// https://github.com/bitcoin/bitcoin/blob/7ee6c434ce8df9441abcf1718555cc7728a4c575/src/test/data/script_tests.json#L1920
    	#[test]
    	fn witness_basic_p2wpkh_with_wrong_key() {
    		assert_eq!(Err(Error::EvalFalse),
    			run_witness_test("".into(),
    				"00147cf9c846cd4882efec4bf07e44ebdad495c94f4b".into(),
    				vec!["304402206ef7fdb2986325d37c6eb1a8bb24aeb46dede112ed8fc76c7d7500b9b83c0d3d02201edc2322c794fe2d6b0bd73ed319e714aa9b86d8891961530d5c9b7156b60d4e01".into(),
    					"048282263212c609d9ea2a6e3e172de238d8c39cabd5ac1ca10646e23fd5f5150811f8a8098557dfe45e8256e830b60ace62d613ac2f7b17bed31b6eaff6e26caf".into()],
    				VerificationFlags::default().verify_p2sh(true).verify_witness(true),
    				0,
    			));
    	}

    	// https://github.com/bitcoin/bitcoin/blob/7ee6c434ce8df9441abcf1718555cc7728a4c575/src/test/data/script_tests.json#L1920
    	#[test]
    	fn witness_basic_p2sh_p2wsh_with_wrong_key() {
    		assert_eq!(Err(Error::EvalFalse),
    			run_witness_test("220020ac8ebd9e52c17619a381fa4f71aebb696087c6ef17c960fd0587addad99c0610".into(),
    				"a91461039a003883787c0d6ebc66d97fdabe8e31449d87".into(),
    				vec!["30440220069ea3581afaf8187f63feee1fd2bd1f9c0dc71ea7d6e8a8b07ee2ebcf824bf402201a4fdef4c532eae59223be1eda6a397fc835142d4ddc6c74f4aa85b766a5c16f01".into(),
    					"41048282263212c609d9ea2a6e3e172de238d8c39cabd5ac1ca10646e23fd5f5150811f8a8098557dfe45e8256e830b60ace62d613ac2f7b17bed31b6eaff6e26cafac".into()],
    				VerificationFlags::default().verify_p2sh(true).verify_witness(true),
    				0,
    			));
    	}

    	// https://github.com/bitcoin/bitcoin/blob/7ee6c434ce8df9441abcf1718555cc7728a4c575/src/test/data/script_tests.json#L1944
    	#[test]
    	fn witness_basic_p2sh_p2wpkh_with_wrong_key() {
    		assert_eq!(Err(Error::EvalFalse),
    			run_witness_test("1600147cf9c846cd4882efec4bf07e44ebdad495c94f4b".into(),
    				"a9144e0c2aed91315303fc6a1dc4c7bc21c88f75402e87".into(),
    				vec!["304402204209e49457c2358f80d0256bc24535b8754c14d08840fc4be762d6f5a0aed80b02202eaf7d8fc8d62f60c67adcd99295528d0e491ae93c195cec5a67e7a09532a88001".into(),
    					"048282263212c609d9ea2a6e3e172de238d8c39cabd5ac1ca10646e23fd5f5150811f8a8098557dfe45e8256e830b60ace62d613ac2f7b17bed31b6eaff6e26caf".into()],
    				VerificationFlags::default().verify_p2sh(true).verify_witness(true),
    				0,
    			));
    	}

    	// https://github.com/bitcoin/bitcoin/blob/7ee6c434ce8df9441abcf1718555cc7728a4c575/src/test/data/script_tests.json#L1956
    	#[test]
    	fn witness_basic_p2wsh_with_wrong_key_check_skipped() {
    		assert_eq!(Ok(()),
    			run_witness_test("".into(),
    				"0020ac8ebd9e52c17619a381fa4f71aebb696087c6ef17c960fd0587addad99c0610".into(),
    				vec!["304402202589f0512cb2408fb08ed9bd24f85eb3059744d9e4f2262d0b7f1338cff6e8b902206c0978f449693e0578c71bc543b11079fd0baae700ee5e9a6bee94db490af9fc01".into(),
    					"41048282263212c609d9ea2a6e3e172de238d8c39cabd5ac1ca10646e23fd5f5150811f8a8098557dfe45e8256e830b60ace62d613ac2f7b17bed31b6eaff6e26cafac".into()],
    				VerificationFlags::default().verify_p2sh(true),
    				0,
    			));
    	}

    	// https://github.com/bitcoin/bitcoin/blob/7ee6c434ce8df9441abcf1718555cc7728a4c575/src/test/data/script_tests.json#L1968
    	#[test]
    	fn witness_basic_p2wpkh_with_wrong_key_check_skipped() {
    		assert_eq!(Ok(()),
    			run_witness_test("".into(),
    				"00147cf9c846cd4882efec4bf07e44ebdad495c94f4b".into(),
    				vec!["304402206ef7fdb2986325d37c6eb1a8bb24aeb46dede112ed8fc76c7d7500b9b83c0d3d02201edc2322c794fe2d6b0bd73ed319e714aa9b86d8891961530d5c9b7156b60d4e01".into(),
    					"4104828048282263212c609d9ea2a6e3e172de238d8c39cabd5ac1ca10646e23fd5f5150811f8a8098557dfe45e8256e830b60ace62d613ac2f7b17bed31b6eaff6e26caf2263212c609d9ea2a6e3e172de238d8c39cabd5ac1ca10646e23fd5f5150811f8a8098557dfe45e8256e830b60ace62d613ac2f7b17bed31b6eaff6e26cafac".into()],
    				VerificationFlags::default().verify_p2sh(true),
    				0,
    			));
    	}

    	// https://github.com/bitcoin/bitcoin/blob/7ee6c434ce8df9441abcf1718555cc7728a4c575/src/test/data/script_tests.json#L1980
    	#[test]
    	fn witness_basic_p2sh_p2wsh_with_wrong_key_check_skipped() {
    		assert_eq!(Ok(()),
    			run_witness_test("220020ac8ebd9e52c17619a381fa4f71aebb696087c6ef17c960fd0587addad99c0610".into(),
    				"a91461039a003883787c0d6ebc66d97fdabe8e31449d87".into(),
    				vec!["30440220069ea3581afaf8187f63feee1fd2bd1f9c0dc71ea7d6e8a8b07ee2ebcf824bf402201a4fdef4c532eae59223be1eda6a397fc835142d4ddc6c74f4aa85b766a5c16f01".into(),
    					"41048282263212c609d9ea2a6e3e172de238d8c39cabd5ac1ca10646e23fd5f5150811f8a8098557dfe45e8256e830b60ace62d613ac2f7b17bed31b6eaff6e26cafac".into()],
    				VerificationFlags::default().verify_p2sh(true),
    				0,
    			));
    	}

    	// https://github.com/bitcoin/bitcoin/blob/7ee6c434ce8df9441abcf1718555cc7728a4c575/src/test/data/script_tests.json#L1992
    	#[test]
    	fn witness_basic_p2sh_p2wpkh_with_wrong_key_check_skipped() {
    		assert_eq!(Ok(()),
    			run_witness_test("1600147cf9c846cd4882efec4bf07e44ebdad495c94f4b".into(),
    				"a9144e0c2aed91315303fc6a1dc4c7bc21c88f75402e87".into(),
    				vec!["304402204209e49457c2358f80d0256bc24535b8754c14d08840fc4be762d6f5a0aed80b02202eaf7d8fc8d62f60c67adcd99295528d0e491ae93c195cec5a67e7a09532a88001".into(),
    					"048282263212c609d9ea2a6e3e172de238d8c39cabd5ac1ca10646e23fd5f5150811f8a8098557dfe45e8256e830b60ace62d613ac2f7b17bed31b6eaff6e26caf".into()],
    				VerificationFlags::default().verify_p2sh(true),
    				0,
    			));
    	}

    	// https://github.com/bitcoin/bitcoin/blob/7ee6c434ce8df9441abcf1718555cc7728a4c575/src/test/data/script_tests.json#L2004
    	#[test]
    	fn witness_basic_p2wsh_with_wrong_value() {
    		assert_eq!(Err(Error::EvalFalse),
    			run_witness_test("".into(),
    				"0020b95237b48faaa69eb078e1170be3b5cbb3fddf16d0a991e14ad274f7b33a4f64".into(),
    				vec!["3044022066faa86e74e8b30e82691b985b373de4f9e26dc144ec399c4f066aa59308e7c202204712b86f28c32503faa051dbeabff2c238ece861abc36c5e0b40b1139ca222f001".into(),
    					"410479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8ac".into()],
    				VerificationFlags::default().verify_p2sh(true).verify_witness(true),
    				0,
    			));
    	}

    	// https://github.com/bitcoin/bitcoin/blob/7ee6c434ce8df9441abcf1718555cc7728a4c575/src/test/data/script_tests.json#L2016
    	#[test]
    	fn witness_basic_p2wpkh_with_wrong_value() {
    		assert_eq!(Err(Error::EvalFalse),
    			run_witness_test("".into(),
    				"001491b24bf9f5288532960ac687abb035127b1d28a5".into(),
    				vec!["304402203b3389b87448d7dfdb5e82fb854fcf92d7925f9938ea5444e36abef02c3d6a9602202410bc3265049abb07fd2e252c65ab7034d95c9d5acccabe9fadbdc63a52712601".into(),
    					"0479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8".into()],
    				VerificationFlags::default().verify_p2sh(true).verify_witness(true),
    				0,
    			));
    	}

    	// https://github.com/bitcoin/bitcoin/blob/7ee6c434ce8df9441abcf1718555cc7728a4c575/src/test/data/script_tests.json#L2028
    	#[test]
    	fn witness_basic_p2sh_p2wsh_with_wrong_value() {
    		assert_eq!(Err(Error::EvalFalse),
    			run_witness_test("220020b95237b48faaa69eb078e1170be3b5cbb3fddf16d0a991e14ad274f7b33a4f64".into(),
    				"a914f386c2ba255cc56d20cfa6ea8b062f8b5994551887".into(),
    				vec!["3044022000a30c4cfc10e4387be528613575434826ad3c15587475e0df8ce3b1746aa210022008149265e4f8e9dafe1f3ea50d90cb425e9e40ea7ebdd383069a7cfa2b77004701".into(),
    					"410479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8ac".into()],
    				VerificationFlags::default().verify_p2sh(true).verify_witness(true),
    				0,
    			));
    	}

    	// https://github.com/bitcoin/bitcoin/blob/7ee6c434ce8df9441abcf1718555cc7728a4c575/src/test/data/script_tests.json#L2040
    	#[test]
    	fn witness_basic_p2sh_p2wpkh_with_wrong_value() {
    		assert_eq!(Err(Error::EvalFalse),
    			run_witness_test("16001491b24bf9f5288532960ac687abb035127b1d28a5".into(),
    				"a91417743beb429c55c942d2ec703b98c4d57c2df5c687".into(),
    				vec!["304402204fc3a2cd61a47913f2a5f9107d0ad4a504c7b31ee2d6b3b2f38c2b10ee031e940220055d58b7c3c281aaa381d8f486ac0f3e361939acfd568046cb6a311cdfa974cf01".into(),
    					"0479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8".into()],
    				VerificationFlags::default().verify_p2sh(true).verify_witness(true),
    				0,
    			));
    	}

    	// https://github.com/bitcoin/bitcoin/blob/7ee6c434ce8df9441abcf1718555cc7728a4c575/src/test/data/script_tests.json#L2052
    	#[test]
    	fn witness_p2wpkh_with_future_version() {
    		assert_eq!(Err(Error::DiscourageUpgradableWitnessProgram),
    			run_witness_test("".into(),
    				"511491b24bf9f5288532960ac687abb035127b1d28a5".into(),
    				vec!["304402205ae57ae0534c05ca9981c8a6cdf353b505eaacb7375f96681a2d1a4ba6f02f84022056248e68643b7d8ce7c7d128c9f1f348bcab8be15d094ad5cadd24251a28df8001".into(),
    					"0479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8".into()],
    				VerificationFlags::default().verify_p2sh(true).verify_witness(true).verify_discourage_upgradable_witness_program(true),
    				0,
    			));
    	}

    	// https://github.com/bitcoin/bitcoin/blob/7ee6c434ce8df9441abcf1718555cc7728a4c575/src/test/data/script_tests.json#L2064
    	#[test]
    	fn witness_p2wpkh_with_wrong_witness_program_length() {
    		assert_eq!(Err(Error::WitnessProgramWrongLength),
    			run_witness_test("".into(),
    				"001fb34b78da162751647974d5cb7410aa428ad339dbf7d1e16e833f68a0cbf1c3".into(),
    				vec!["3044022064100ca0e2a33332136775a86cd83d0230e58b9aebb889c5ac952abff79a46ef02205f1bf900e022039ad3091bdaf27ac2aef3eae9ed9f190d821d3e508405b9513101".into(),
    					"0479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8".into()],
    				VerificationFlags::default().verify_p2sh(true).verify_witness(true),
    				0,
    			));
    	}

    	// https://github.com/bitcoin/bitcoin/blob/7ee6c434ce8df9441abcf1718555cc7728a4c575/src/test/data/script_tests.json#L2076
    	#[test]
    	fn witness_p2wsh_with_empty_witness() {
    		assert_eq!(Err(Error::WitnessProgramWitnessEmpty),
    			run_witness_test("".into(),
    				"0020b95237b48faaa69eb078e1170be3b5cbb3fddf16d0a991e14ad274f7b33a4f64".into(),
    				vec![],
    				VerificationFlags::default().verify_p2sh(true).verify_witness(true),
    				0,
    			));
    	}

    	// https://github.com/bitcoin/bitcoin/blob/7ee6c434ce8df9441abcf1718555cc7728a4c575/src/test/data/script_tests.json#L2083
    	#[test]
    	fn witness_p2wsh_with_witness_program_mismatch() {
    		assert_eq!(Err(Error::WitnessProgramMismatch),
    			run_witness_test("".into(),
    				"0020b95237b48faaa69eb078e1170be3b5cbb3fddf16d0a991e14ad274f7b33a4f64".into(),
    				vec!["3044022039105b995a5f448639a997a5c90fda06f50b49df30c3bdb6663217bf79323db002206fecd54269dec569fcc517178880eb58bb40f381a282bb75766ff3637d5f4b4301".into(),
    					"400479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8ac".into()],
    				VerificationFlags::default().verify_p2sh(true).verify_witness(true),
    				0,
    			));
    	}

    	// https://github.com/bitcoin/bitcoin/blob/7ee6c434ce8df9441abcf1718555cc7728a4c575/src/test/data/script_tests.json#L2095
    	#[test]
    	fn witness_p2wpkh_with_witness_program_mismatch() {
    		assert_eq!(Err(Error::WitnessProgramMismatch),
    			run_witness_test("".into(),
    				"001491b24bf9f5288532960ac687abb035127b1d28a5".into(),
    				vec!["304402201a96950593cb0af32d080b0f193517f4559241a8ebd1e95e414533ad64a3f423022047f4f6d3095c23235bdff3aeff480d0529c027a3f093cb265b7cbf148553b85101".into(),
    					"0479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8".into(),
    					"".into()],
    				VerificationFlags::default().verify_p2sh(true).verify_witness(true),
    				0,
    			));
    	}

    	// https://github.com/bitcoin/bitcoin/blob/7ee6c434ce8df9441abcf1718555cc7728a4c575/src/test/data/script_tests.json#L2108
    	#[test]
    	fn witness_p2wpkh_with_non_empty_script_sig() {
    		assert_eq!(Err(Error::WitnessMalleated),
    			run_witness_test("5b".into(),
    				"001491b24bf9f5288532960ac687abb035127b1d28a5".into(),
    				vec!["304402201a96950593cb0af32d080b0f193517f4559241a8ebd1e95e414533ad64a3f423022047f4f6d3095c23235bdff3aeff480d0529c027a3f093cb265b7cbf148553b85101".into(),
    					"0479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8".into()],
    				VerificationFlags::default().verify_p2sh(true).verify_witness(true),
    				0,
    			));
    	}

    	// https://github.com/bitcoin/bitcoin/blob/7ee6c434ce8df9441abcf1718555cc7728a4c575/src/test/data/script_tests.json#L2120
    	#[test]
    	fn witness_p2sh_p2wpkh_with_superfluous_push_in_script_sig() {
    		assert_eq!(Err(Error::WitnessMalleatedP2SH),
    			run_witness_test("5b1600147cf9c846cd4882efec4bf07e44ebdad495c94f4b".into(),
    				"a9144e0c2aed91315303fc6a1dc4c7bc21c88f75402e87".into(),
    				vec!["304402204209e49457c2358f80d0256bc24535b8754c14d08840fc4be762d6f5a0aed80b02202eaf7d8fc8d62f60c67adcd99295528d0e491ae93c195cec5a67e7a09532a88001".into(),
    					"048282263212c609d9ea2a6e3e172de238d8c39cabd5ac1ca10646e23fd5f5150811f8a8098557dfe45e8256e830b60ace62d613ac2f7b17bed31b6eaff6e26caf".into()],
    				VerificationFlags::default().verify_p2sh(true).verify_witness(true),
    				0,
    			));
    	}

    	// https://github.com/bitcoin/bitcoin/blob/7ee6c434ce8df9441abcf1718555cc7728a4c575/src/test/data/script_tests.json#L2132
    	#[test]
    	fn witness_p2pk_with_witness() {
    		assert_eq!(Err(Error::WitnessUnexpected),
    			run_witness_test("47304402200a5c6163f07b8d3b013c4d1d6dba25e780b39658d79ba37af7057a3b7f15ffa102201fd9b4eaa9943f734928b99a83592c2e7bf342ea2680f6a2bb705167966b742001".into(),
    				"410479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8ac".into(),
    				vec!["".into()],
    				VerificationFlags::default().verify_p2sh(true).verify_witness(true),
    				0,
    			));
    	}

    	// https://github.com/bitcoin/bitcoin/blob/7ee6c434ce8df9441abcf1718555cc7728a4c575/src/test/data/script_tests.json#L2299
    	#[test]
    	fn witness_p2wsh_checkmultisig() {
    		assert_eq!(Ok(()),
    			run_witness_test("".into(),
    				"002008a6665ebfd43b02323423e764e185d98d1587f903b81507dbb69bfc41005efa".into(),
    				vec!["".into(),
    					"304402202d092ededd1f060609dbf8cb76950634ff42b3e62cf4adb69ab92397b07d742302204ff886f8d0817491a96d1daccdcc820f6feb122ee6230143303100db37dfa79f01".into(),
    					"5121038282263212c609d9ea2a6e3e172de238d8c39cabd5ac1ca10646e23fd5f51508410479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b852ae".into()],
    				VerificationFlags::default().verify_p2sh(true).verify_witness(true),
    				1,
    			));
    	}

    	// https://github.com/bitcoin/bitcoin/blob/7ee6c434ce8df9441abcf1718555cc7728a4c575/src/test/data/script_tests.json#L2312
    	#[test]
    	fn witness_p2sh_p2wsh_checkmultisig() {
    		assert_eq!(Ok(()),
    			run_witness_test("22002008a6665ebfd43b02323423e764e185d98d1587f903b81507dbb69bfc41005efa".into(),
    				"a9146f5ecd4b83b77f3c438f5214eff96454934fc5d187".into(),
    				vec!["".into(),
    					"304402202dd7e91243f2235481ffb626c3b7baf2c859ae3a5a77fb750ef97b99a8125dc002204960de3d3c3ab9496e218ec57e5240e0e10a6f9546316fe240c216d45116d29301".into(),
    					"5121038282263212c609d9ea2a6e3e172de238d8c39cabd5ac1ca10646e23fd5f51508410479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b852ae".into()],
    				VerificationFlags::default().verify_p2sh(true).verify_witness(true),
    				1,
    			));
    	}

    	// https://github.com/bitcoin/bitcoin/blob/7ee6c434ce8df9441abcf1718555cc7728a4c575/src/test/data/script_tests.json#L2351
    	#[test]
    	fn witness_p2wsh_checkmultisig_using_key2() {
    		assert_eq!(Ok(()),
    			run_witness_test("".into(),
    				"002008a6665ebfd43b02323423e764e185d98d1587f903b81507dbb69bfc41005efa".into(),
    				vec!["".into(),
    					"304402201e9e6f7deef5b2f21d8223c5189b7d5e82d237c10e97165dd08f547c4e5ce6ed02206796372eb1cc6acb52e13ee2d7f45807780bf96b132cb6697f69434be74b1af901".into(),
    					"5121038282263212c609d9ea2a6e3e172de238d8c39cabd5ac1ca10646e23fd5f51508410479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b852ae".into()],
    				VerificationFlags::default().verify_p2sh(true).verify_witness(true),
    				1,
    			));
    	}

    	// https://github.com/bitcoin/bitcoin/blob/7ee6c434ce8df9441abcf1718555cc7728a4c575/src/test/data/script_tests.json#L2364
    	#[test]
    	fn witness_p2sh_p2wsh_checkmultisig_using_key2() {
    		assert_eq!(Ok(()),
    			run_witness_test("22002008a6665ebfd43b02323423e764e185d98d1587f903b81507dbb69bfc41005efa".into(),
    				"a9146f5ecd4b83b77f3c438f5214eff96454934fc5d187".into(),
    				vec!["".into(),
    					"3044022045e667f3f0f3147b95597a24babe9afecea1f649fd23637dfa7ed7e9f3ac18440220295748e81005231135289fe3a88338dabba55afa1bdb4478691337009d82b68d01".into(),
    					"5121038282263212c609d9ea2a6e3e172de238d8c39cabd5ac1ca10646e23fd5f51508410479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b852ae".into()],
    				VerificationFlags::default().verify_p2sh(true).verify_witness(true),
    				1,
    			));
    	}

    	fn run_witness_test_tx_test(script_pubkey: Script, tx: &Transaction, flags: &VerificationFlags, amount: u64, index: usize) -> Result<(), Error> {
    		let checker = TransactionSignatureChecker {
    			input_index: index,
    			input_amount: amount,
    			signer: tx.clone().into(),
    		};

    		verify_script(&tx.inputs[index].script_sig.clone().into(),
    			&script_pubkey,
    			&tx.inputs[index].script_witness,
    			flags,
    			&checker,
    			SignatureVersion::Base)
    	}

    	// https://github.com/bitcoin/bitcoin/blob/7ee6c434ce8df9441abcf1718555cc7728a4c575/src/test/data/tx_invalid.json#L254
    	#[test]
    	fn witness_unknown_program_version() {
    		let tx = " ".into();
    		let flags = VerificationFlags::default().verify_witness(true).verify_p2sh(true).verify_discourage_upgradable_witness_program(true);
    		assert_eq!(Err(Error::DiscourageUpgradableWitnessProgram), run_witness_test_tx_test("51".into(), &tx, &flags, 1000, 0)
    			.and_then(|_| run_witness_test_tx_test("60144c9c3dfac4207d5d8cb89df5722cb3d712385e3f".into(), &tx, &flags, 2000, 1))
    			.and_then(|_| run_witness_test_tx_test("51".into(), &tx, &flags, 3000, 2)));
    	}

    	// https://github.com/bitcoin/bitcoin/blob/7ee6c434ce8df9441abcf1718555cc7728a4c575/src/test/data/tx_invalid.json#L260
    	#[test]
    	fn witness_unknown_program0_lengh() {
    		let tx = " ".into();
    		let flags = VerificationFlags::default().verify_witness(true).verify_p2sh(true);
    		assert_eq!(Err(Error::WitnessProgramWrongLength), run_witness_test_tx_test("51".into(), &tx, &flags, 1000, 0)
    			.and_then(|_| run_witness_test_tx_test("00154c9c3dfac4207d5d8cb89df5722cb3d712385e3fff".into(), &tx, &flags, 2000, 1))
    			.and_then(|_| run_witness_test_tx_test("51".into(), &tx, &flags, 3000, 2)));
    	}

    	// https://github.com/bitcoin/bitcoin/blob/7ee6c434ce8df9441abcf1718555cc7728a4c575/src/test/data/tx_invalid.json#L260
    	#[test]
    	fn witness_single_anyone_same_index_value_changed() {
    		let tx = " ".into();
    		let flags = VerificationFlags::default().verify_witness(true).verify_p2sh(true);
    		assert_eq!(Err(Error::EvalFalse), run_witness_test_tx_test("51".into(), &tx, &flags, 1000, 0)
    			.and_then(|_| run_witness_test_tx_test("00144c9c3dfac4207d5d8cb89df5722cb3d712385e3f".into(), &tx, &flags, 2000, 1))
    			.and_then(|_| run_witness_test_tx_test("51".into(), &tx, &flags, 3000, 2)));
    	}

    	// https://github.com/bitcoin/bitcoin/blob/7ee6c434ce8df9441abcf1718555cc7728a4c575/src/test/data/tx_invalid.json#L272
    	#[test]
    	fn witness_none_anyone_same_index_value_changed() {
    		let tx = "0100000000010300010000000000000000000000000000000000000000000000000000000000000000000000ffffffff000100000000000000000000000000000000000000000000000000000000000001000000000100000000010000000000000000000000000000000000000000000000000000000000000200000000ffffffff00000248304502210091b32274295c2a3fa02f5bce92fb2789e3fc6ea947fbe1a76e52ea3f4ef2381a022079ad72aefa3837a2e0c033a8652a59731da05fa4a813f4fc48e87c075037256b822103596d3451025c19dbbdeb932d6bf8bfb4ad499b95b6f88db8899efac102e5fc710000000000".into();
    		let flags = VerificationFlags::default().verify_witness(true).verify_p2sh(true);
    		assert_eq!(Err(Error::EvalFalse), run_witness_test_tx_test("51".into(), &tx, &flags, 1000, 0)
    			.and_then(|_| run_witness_test_tx_test("00144c9c3dfac4207d5d8cb89df5722cb3d712385e3f".into(), &tx, &flags, 2000, 1))
    			.and_then(|_| run_witness_test_tx_test("51".into(), &tx, &flags, 3000, 2)));
    	}

    	// https://github.com/bitcoin/bitcoin/blob/7ee6c434ce8df9441abcf1718555cc7728a4c575/src/test/data/tx_invalid.json#L278
    	#[test]
    	fn witness_all_anyone_third_value_changed() {
    		let tx = " ".into();
    		let flags = VerificationFlags::default().verify_witness(true).verify_p2sh(true);
    		assert_eq!(Err(Error::EvalFalse), run_witness_test_tx_test("51".into(), &tx, &flags, 1000, 0)
    			.and_then(|_| run_witness_test_tx_test("00144c9c3dfac4207d5d8cb89df5722cb3d712385e3f".into(), &tx, &flags, 2000, 1))
    			.and_then(|_| run_witness_test_tx_test("51".into(), &tx, &flags, 3000, 2)));
    	}

    	// https://github.com/bitcoin/bitcoin/blob/7ee6c434ce8df9441abcf1718555cc7728a4c575/src/test/data/tx_invalid.json#L284
    	#[test]
    	fn witness_with_push_of_521_bytes() {
    		let tx = " ".into();
    		let flags = VerificationFlags::default().verify_witness(true).verify_p2sh(true);
    		assert_eq!(Err(Error::PushSize), run_witness_test_tx_test("002033198a9bfef674ebddb9ffaa52928017b8472791e54c609cb95f278ac6b1e349".into(), &tx, &flags, 1000, 0));
    	}

    	// https://github.com/bitcoin/bitcoin/blob/7ee6c434ce8df9441abcf1718555cc7728a4c575/src/test/data/tx_invalid.json#L288
    	#[test]
    	fn witness_unknown_version_with_false_on_stack() {
    		let tx = "0100000000010100010000000000000000000000000000000000000000000000000000000000000000000000ffffffff010000000000000000015101010100000000".into();
    		let flags = VerificationFlags::default().verify_witness(true).verify_p2sh(true);
    		assert_eq!(Err(Error::EvalFalse), run_witness_test_tx_test("60020000".into(), &tx, &flags, 2000, 0));
    	}

    	// https://github.com/bitcoin/bitcoin/blob/7ee6c434ce8df9441abcf1718555cc7728a4c575/src/test/data/tx_invalid.json#L292
    	#[test]
    	fn witness_unknown_version_with_non_empty_stack() {
    		let tx = "0100000000010100010000000000000000000000000000000000000000000000000000000000000000000000ffffffff01000000000000000001510102515100000000".into();
    		let flags = VerificationFlags::default().verify_witness(true).verify_p2sh(true);
    		assert_eq!(Err(Error::EvalFalse), run_witness_test_tx_test("00202f04a3aa051f1f60d695f6c44c0c3d383973dfd446ace8962664a76bb10e31a8".into(), &tx, &flags, 2000, 0));
    	}

    	// https://github.com/bitcoin/bitcoin/blob/7ee6c434ce8df9441abcf1718555cc7728a4c575/src/test/data/tx_invalid.json#L296
    	#[test]
    	fn witness_program0_with_push_of_2_bytes() {
    		let tx = "0100000000010100010000000000000000000000000000000000000000000000000000000000000000000000ffffffff010000000000000000015101040002000100000000".into();
    		let flags = VerificationFlags::default().verify_witness(true).verify_p2sh(true);
    		assert_eq!(Err(Error::WitnessProgramWrongLength), run_witness_test_tx_test("00020001".into(), &tx, &flags, 2000, 0));
    	}

    	// https://github.com/bitcoin/bitcoin/blob/7ee6c434ce8df9441abcf1718555cc7728a4c575/src/test/data/tx_invalid.json#L300
    	#[test]
    	fn witness_unknown_version_with_non_empty_script_sig() {
    		let tx = "01000000010001000000000000000000000000000000000000000000000000000000000000000000000151ffffffff010000000000000000015100000000".into();
    		let flags = VerificationFlags::default().verify_witness(true).verify_p2sh(true);
    		assert_eq!(Err(Error::WitnessMalleated), run_witness_test_tx_test("60020001".into(), &tx, &flags, 2000, 0));
    	}

    	// https://github.com/bitcoin/bitcoin/blob/7ee6c434ce8df9441abcf1718555cc7728a4c575/src/test/data/tx_invalid.json#L304
    	#[test]
    	fn witness_non_witness_single_anyone_hash_input_position() {
    		let tx = " ".into();
    		let flags = VerificationFlags::default().verify_witness(true).verify_p2sh(true);
    		assert_eq!(Err(Error::EvalFalse), run_witness_test_tx_test("2103596d3451025c19dbbdeb932d6bf8bfb4ad499b95b6f88db8899efac102e5fc71ac".into(), &tx, &flags, 1000, 0)
    			.and_then(|_| run_witness_test_tx_test("2103596d3451025c19dbbdeb932d6bf8bfb4ad499b95b6f88db8899efac102e5fc71ac".into(), &tx, &flags, 1001, 1)));
    	}

    	// https://github.com/bitcoin/bitcoin/blob/7ee6c434ce8df9441abcf1718555cc7728a4c575/src/test/data/tx_invalid.json#L313
    	#[test]
    	fn witness_33_bytes_witness_script_pubkey() {
    		let tx = "010000000100010000000000000000000000000000000000000000000000000000000000000000000000ffffffff01e803000000000000015100000000".into();
    		let flags = VerificationFlags::default().verify_witness(true).verify_p2sh(true).verify_discourage_upgradable_witness_program(true);
    		assert_eq!(Err(Error::DiscourageUpgradableWitnessProgram), run_witness_test_tx_test("6021ff25429251b5a84f452230a3c75fd886b7fc5a7865ce4a7bb7a9d7c5be6da3dbff".into(), &tx, &flags, 1000, 0));
    	}

    	// https://github.com/bitcoin/bitcoin/blob/7ee6c434ce8df9441abcf1718555cc7728a4c575/src/test/data/tx_valid.json#L320
    	#[test]
    	fn witness_valid_p2wpkh() {
    		let tx = "0100000000010100010000000000000000000000000000000000000000000000000000000000000000000000ffffffff01e8030000000000001976a9144c9c3dfac4207d5d8cb89df5722cb3d712385e3f88ac02483045022100cfb07164b36ba64c1b1e8c7720a56ad64d96f6ef332d3d37f9cb3c96477dc44502200a464cd7a9cf94cd70f66ce4f4f0625ef650052c7afcfe29d7d7e01830ff91ed012103596d3451025c19dbbdeb932d6bf8bfb4ad499b95b6f88db8899efac102e5fc7100000000".into();
    		let flags = VerificationFlags::default().verify_witness(true).verify_p2sh(true);
    		assert_eq!(Ok(()), run_witness_test_tx_test("00144c9c3dfac4207d5d8cb89df5722cb3d712385e3f".into(), &tx, &flags, 1000, 0));
    	}

    	// https://github.com/bitcoin/bitcoin/blob/7ee6c434ce8df9441abcf1718555cc7728a4c575/src/test/data/tx_valid.json#L324
    	#[test]
    	fn witness_valid_p2wsh() {
    		let tx = "0100000000010100010000000000000000000000000000000000000000000000000000000000000000000000ffffffff01e8030000000000001976a9144c9c3dfac4207d5d8cb89df5722cb3d712385e3f88ac02483045022100aa5d8aa40a90f23ce2c3d11bc845ca4a12acd99cbea37de6b9f6d86edebba8cb022022dedc2aa0a255f74d04c0b76ece2d7c691f9dd11a64a8ac49f62a99c3a05f9d01232103596d3451025c19dbbdeb932d6bf8bfb4ad499b95b6f88db8899efac102e5fc71ac00000000".into();
    		let flags = VerificationFlags::default().verify_witness(true).verify_p2sh(true);
    		assert_eq!(Ok(()), run_witness_test_tx_test("0020ff25429251b5a84f452230a3c75fd886b7fc5a7865ce4a7bb7a9d7c5be6da3db".into(), &tx, &flags, 1000, 0));
    	}

    	// https://github.com/bitcoin/bitcoin/blob/7ee6c434ce8df9441abcf1718555cc7728a4c575/src/test/data/tx_valid.json#L328
    	#[test]
    	fn witness_valid_p2sh_p2wpkh() {
    		let tx = "01000000000101000100000000000000000000000000000000000000000000000000000000000000000000171600144c9c3dfac4207d5d8cb89df5722cb3d712385e3fffffffff01e8030000000000001976a9144c9c3dfac4207d5d8cb89df5722cb3d712385e3f88ac02483045022100cfb07164b36ba64c1b1e8c7720a56ad64d96f6ef332d3d37f9cb3c96477dc44502200a464cd7a9cf94cd70f66ce4f4f0625ef650052c7afcfe29d7d7e01830ff91ed012103596d3451025c19dbbdeb932d6bf8bfb4ad499b95b6f88db8899efac102e5fc7100000000".into();
    		let flags = VerificationFlags::default().verify_witness(true).verify_p2sh(true);
    		assert_eq!(Ok(()), run_witness_test_tx_test("a914fe9c7dacc9fcfbf7e3b7d5ad06aa2b28c5a7b7e387".into(), &tx, &flags, 1000, 0));
    	}

    	// https://github.com/bitcoin/bitcoin/blob/7ee6c434ce8df9441abcf1718555cc7728a4c575/src/test/data/tx_valid.json#L328
    	#[test]
    	fn witness_valid_p2sh_p2wsh() {
    		let tx = "0100000000010100010000000000000000000000000000000000000000000000000000000000000000000023220020ff25429251b5a84f452230a3c75fd886b7fc5a7865ce4a7bb7a9d7c5be6da3dbffffffff01e8030000000000001976a9144c9c3dfac4207d5d8cb89df5722cb3d712385e3f88ac02483045022100aa5d8aa40a90f23ce2c3d11bc845ca4a12acd99cbea37de6b9f6d86edebba8cb022022dedc2aa0a255f74d04c0b76ece2d7c691f9dd11a64a8ac49f62a99c3a05f9d01232103596d3451025c19dbbdeb932d6bf8bfb4ad499b95b6f88db8899efac102e5fc71ac00000000".into();
    		let flags = VerificationFlags::default().verify_witness(true).verify_p2sh(true);
    		assert_eq!(Ok(()), run_witness_test_tx_test("a9142135ab4f0981830311e35600eebc7376dce3a91487".into(), &tx, &flags, 1000, 0));
    	}

    	// https://github.com/bitcoin/bitcoin/blob/7ee6c434ce8df9441abcf1718555cc7728a4c575/src/test/data/tx_valid.json#L328
    	#[test]
    	fn witness_valid_single_anyoune() {
    		let tx = " ".into();
    		let flags = VerificationFlags::default().verify_witness(true).verify_p2sh(true);
    		assert_eq!(Ok(()), run_witness_test_tx_test("51".into(), &tx, &flags, 3100, 0)
    			.and_then(|_| run_witness_test_tx_test("00144c9c3dfac4207d5d8cb89df5722cb3d712385e3f".into(), &tx, &flags, 2000, 1))
    			.and_then(|_| run_witness_test_tx_test("51".into(), &tx, &flags, 1100, 2))
    			.and_then(|_| run_witness_test_tx_test("51".into(), &tx, &flags, 4100, 2)));
    	}

    	// https://github.com/bitcoin/bitcoin/blob/7ee6c434ce8df9441abcf1718555cc7728a4c575/src/test/data/tx_valid.json#L343
    	#[test]
    	fn witness_valid_single_anyoune_same_signature() {
    		let tx = " ".into();
    		let flags = VerificationFlags::default().verify_witness(true).verify_p2sh(true);
    		assert_eq!(Ok(()), run_witness_test_tx_test("51".into(), &tx, &flags, 3100, 0)
    			.and_then(|_| run_witness_test_tx_test("00144c9c3dfac4207d5d8cb89df5722cb3d712385e3f".into(), &tx, &flags, 2000, 1))
    			.and_then(|_| run_witness_test_tx_test("51".into(), &tx, &flags, 1100, 2))
    			.and_then(|_| run_witness_test_tx_test("51".into(), &tx, &flags, 4100, 2)));
    	}

    	// https://github.com/bitcoin/bitcoin/blob/7ee6c434ce8df9441abcf1718555cc7728a4c575/src/test/data/tx_valid.json#L349
    	#[test]
    	fn witness_valid_single() {
    		let tx = " ".into();
    		let flags = VerificationFlags::default().verify_witness(true).verify_p2sh(true);
    		assert_eq!(Ok(()), run_witness_test_tx_test("51".into(), &tx, &flags, 1000, 0)
    			.and_then(|_| run_witness_test_tx_test("00144c9c3dfac4207d5d8cb89df5722cb3d712385e3f".into(), &tx, &flags, 2000, 1))
    			.and_then(|_| run_witness_test_tx_test("51".into(), &tx, &flags, 3000, 2)));
    	}

    	// https://github.com/bitcoin/bitcoin/blob/7ee6c434ce8df9441abcf1718555cc7728a4c575/src/test/data/tx_valid.json#L355
    	#[test]
    	fn witness_valid_single_same_signature() {
    		let tx = " ".into();
    		let flags = VerificationFlags::default().verify_witness(true).verify_p2sh(true);
    		assert_eq!(Ok(()), run_witness_test_tx_test("51".into(), &tx, &flags, 1000, 0)
    			.and_then(|_| run_witness_test_tx_test("00144c9c3dfac4207d5d8cb89df5722cb3d712385e3f".into(), &tx, &flags, 2000, 1))
    			.and_then(|_| run_witness_test_tx_test("51".into(), &tx, &flags, 3000, 2)));
    	}

    	// https://github.com/bitcoin/bitcoin/blob/7ee6c434ce8df9441abcf1718555cc7728a4c575/src/test/data/tx_valid.json#L361
    	#[test]
    	fn witness_valid_none_anyone() {
    		let tx = " ".into();
    		let flags = VerificationFlags::default().verify_witness(true).verify_p2sh(true);
    		assert_eq!(Ok(()), run_witness_test_tx_test("51".into(), &tx, &flags, 3100, 0)
    			.and_then(|_| run_witness_test_tx_test("51".into(), &tx, &flags, 1100, 1))
    			.and_then(|_| run_witness_test_tx_test("00144c9c3dfac4207d5d8cb89df5722cb3d712385e3f".into(), &tx, &flags, 2000, 2))
    			.and_then(|_| run_witness_test_tx_test("51".into(), &tx, &flags, 4100, 3)));
    	}

    	// https://github.com/bitcoin/bitcoin/blob/7ee6c434ce8df9441abcf1718555cc7728a4c575/src/test/data/tx_valid.json#L368
    	#[test]
    	fn witness_valid_none_anyone_same_signature() {
    		let tx = " ".into();
    		let flags = VerificationFlags::default().verify_witness(true).verify_p2sh(true);
    		assert_eq!(Ok(()), run_witness_test_tx_test("51".into(), &tx, &flags, 1000, 0)
    			.and_then(|_| run_witness_test_tx_test("00144c9c3dfac4207d5d8cb89df5722cb3d712385e3f".into(), &tx, &flags, 2000, 1))
    			.and_then(|_| run_witness_test_tx_test("51".into(), &tx, &flags, 3000, 2)));
    	}

    	// https://github.com/bitcoin/bitcoin/blob/7ee6c434ce8df9441abcf1718555cc7728a4c575/src/test/data/tx_valid.json#L374
    	#[test]
    	fn witness_none() {
    		let tx = " ".into();
    		let flags = VerificationFlags::default().verify_witness(true).verify_p2sh(true);
    		assert_eq!(Ok(()), run_witness_test_tx_test("51".into(), &tx, &flags, 1000, 0)
    			.and_then(|_| run_witness_test_tx_test("00144c9c3dfac4207d5d8cb89df5722cb3d712385e3f".into(), &tx, &flags, 2000, 1))
    			.and_then(|_| run_witness_test_tx_test("51".into(), &tx, &flags, 3000, 2)));
    	}

    	// https://github.com/bitcoin/bitcoin/blob/7ee6c434ce8df9441abcf1718555cc7728a4c575/src/test/data/tx_valid.json#L380
    	#[test]
    	fn witness_none_same_signature() {
    		let tx = " ".into();
    		let flags = VerificationFlags::default().verify_witness(true).verify_p2sh(true);
    		assert_eq!(Ok(()), run_witness_test_tx_test("51".into(), &tx, &flags, 1000, 0)
    			.and_then(|_| run_witness_test_tx_test("00144c9c3dfac4207d5d8cb89df5722cb3d712385e3f".into(), &tx, &flags, 2000, 1))
    			.and_then(|_| run_witness_test_tx_test("51".into(), &tx, &flags, 3000, 2)));
    	}

    	// https://github.com/bitcoin/bitcoin/blob/7ee6c434ce8df9441abcf1718555cc7728a4c575/src/test/data/tx_valid.json#L386
    	#[test]
    	fn witness_none_same_signature_sequence_changed() {
    		let tx = " ".into();
    		let flags = VerificationFlags::default().verify_witness(true).verify_p2sh(true);
    		assert_eq!(Ok(()), run_witness_test_tx_test("51".into(), &tx, &flags, 1000, 0)
    			.and_then(|_| run_witness_test_tx_test("00144c9c3dfac4207d5d8cb89df5722cb3d712385e3f".into(), &tx, &flags, 2000, 1))
    			.and_then(|_| run_witness_test_tx_test("51".into(), &tx, &flags, 3000, 2)));
    	}

    	// https://github.com/bitcoin/bitcoin/blob/7ee6c434ce8df9441abcf1718555cc7728a4c575/src/test/data/tx_valid.json#L392
    	#[test]
    	fn witness_all_anyone() {
    		let tx = " ".into();
    		let flags = VerificationFlags::default().verify_witness(true).verify_p2sh(true);
    		assert_eq!(Ok(()), run_witness_test_tx_test("51".into(), &tx, &flags, 3100, 0)
    			.and_then(|_| run_witness_test_tx_test("51".into(), &tx, &flags, 1100, 1))
    			.and_then(|_| run_witness_test_tx_test("00144c9c3dfac4207d5d8cb89df5722cb3d712385e3f".into(), &tx, &flags, 2000, 2))
    			.and_then(|_| run_witness_test_tx_test("51".into(), &tx, &flags, 4100, 3)));
    	}

    	// https://github.com/bitcoin/bitcoin/blob/7ee6c434ce8df9441abcf1718555cc7728a4c575/src/test/data/tx_valid.json#L399
    	#[test]
    	fn witness_all_anyone_same_signature() {
    		let tx = " ".into();
    		let flags = VerificationFlags::default().verify_witness(true).verify_p2sh(true);
    		assert_eq!(Ok(()), run_witness_test_tx_test("51".into(), &tx, &flags, 1000, 0)
    			.and_then(|_| run_witness_test_tx_test("00144c9c3dfac4207d5d8cb89df5722cb3d712385e3f".into(), &tx, &flags, 2000, 1))
    			.and_then(|_| run_witness_test_tx_test("51".into(), &tx, &flags, 3000, 2)));
    	}

    	// https://github.com/bitcoin/bitcoin/blob/7ee6c434ce8df9441abcf1718555cc7728a4c575/src/test/data/tx_valid.json#L405
    	#[test]
    	fn witness_unknown_witness_program_version() {
    		let tx = " ".into();
    		let flags = VerificationFlags::default().verify_witness(true).verify_p2sh(true);
    		assert_eq!(Ok(()), run_witness_test_tx_test("51".into(), &tx, &flags, 1000, 0)
    			.and_then(|_| run_witness_test_tx_test("60144c9c3dfac4207d5d8cb89df5722cb3d712385e3f".into(), &tx, &flags, 2000, 1))
    			.and_then(|_| run_witness_test_tx_test("51".into(), &tx, &flags, 3000, 2)));
    	}

    	// https://github.com/bitcoin/bitcoin/blob/7ee6c434ce8df9441abcf1718555cc7728a4c575/src/test/data/tx_valid.json#L411
    	#[test]
    	fn witness_push_520_bytes() {
    		let tx = " ".into();
    		let flags = VerificationFlags::default().verify_witness(true).verify_p2sh(true);
    		assert_eq!(Ok(()), run_witness_test_tx_test("002033198a9bfef674ebddb9ffaa52928017b8472791e54c609cb95f278ac6b1e349".into(), &tx, &flags, 1000, 0));
    	}

    	// https://github.com/bitcoin/bitcoin/blob/7ee6c434ce8df9441abcf1718555cc7728a4c575/src/test/data/tx_valid.json#L415
    	#[test]
    	fn witness_mixed_transaction() {
    		let tx = " ".into();
    		let flags = VerificationFlags::default().verify_witness(true).verify_p2sh(true);
    		assert_eq!(Ok(()), run_witness_test_tx_test("00144c9c3dfac4207d5d8cb89df5722cb3d712385e3f".into(), &tx, &flags, 1000, 0)
    			.and_then(|_| run_witness_test_tx_test("00144c9c3dfac4207d5d8cb89df5722cb3d712385e3f".into(), &tx, &flags, 1001, 1))
    			.and_then(|_| run_witness_test_tx_test("76a9144c9c3dfac4207d5d8cb89df5722cb3d712385e3f88ac".into(), &tx, &flags, 1002, 2))
    			.and_then(|_| run_witness_test_tx_test("76a9144c9c3dfac4207d5d8cb89df5722cb3d712385e3f88ac".into(), &tx, &flags, 1003, 3))
    			.and_then(|_| run_witness_test_tx_test("76a9144c9c3dfac4207d5d8cb89df5722cb3d712385e3f88ac".into(), &tx, &flags, 1004, 4))
    			.and_then(|_| run_witness_test_tx_test("76a9144c9c3dfac4207d5d8cb89df5722cb3d712385e3f88ac".into(), &tx, &flags, 1005, 5))
    			.and_then(|_| run_witness_test_tx_test("76a9144c9c3dfac4207d5d8cb89df5722cb3d712385e3f88ac".into(), &tx, &flags, 1006, 6))
    			.and_then(|_| run_witness_test_tx_test("00144c9c3dfac4207d5d8cb89df5722cb3d712385e3f".into(), &tx, &flags, 1007, 7))
    			.and_then(|_| run_witness_test_tx_test("00144c9c3dfac4207d5d8cb89df5722cb3d712385e3f".into(), &tx, &flags, 1008, 8))
    			.and_then(|_| run_witness_test_tx_test("00144c9c3dfac4207d5d8cb89df5722cb3d712385e3f".into(), &tx, &flags, 1009, 9))
    			.and_then(|_| run_witness_test_tx_test("00144c9c3dfac4207d5d8cb89df5722cb3d712385e3f".into(), &tx, &flags, 1010, 10))
    			.and_then(|_| run_witness_test_tx_test("76a9144c9c3dfac4207d5d8cb89df5722cb3d712385e3f88ac".into(), &tx, &flags, 1011, 11)));
    	}

    	// https://github.com/bitcoin/bitcoin/blob/7ee6c434ce8df9441abcf1718555cc7728a4c575/src/test/data/tx_valid.json#L430
    	#[test]
    	fn witness_unknown_version_with_empty_witness() {
    		let tx = "010000000100010000000000000000000000000000000000000000000000000000000000000000000000ffffffff01e803000000000000015100000000".into();
    		let flags = VerificationFlags::default().verify_witness(true).verify_p2sh(true);
    		assert_eq!(Ok(()), run_witness_test_tx_test("60144c9c3dfac4207d5d8cb89df5722cb3d712385e3f".into(), &tx, &flags, 1000, 0));
    	}

    	// https://github.com/bitcoin/bitcoin/blob/7ee6c434ce8df9441abcf1718555cc7728a4c575/src/test/data/tx_valid.json#L434
    	#[test]
    	fn witness_single_output_oob() {
    		let tx = "0100000000010200010000000000000000000000000000000000000000000000000000000000000000000000ffffffff00010000000000000000000000000000000000000000000000000000000000000100000000ffffffff01d00700000000000001510003483045022100e078de4e96a0e05dcdc0a414124dd8475782b5f3f0ed3f607919e9a5eeeb22bf02201de309b3a3109adb3de8074b3610d4cf454c49b61247a2779a0bcbf31c889333032103596d3451025c19dbbdeb932d6bf8bfb4ad499b95b6f88db8899efac102e5fc711976a9144c9c3dfac4207d5d8cb89df5722cb3d712385e3f88ac00000000".into();
    		let flags = VerificationFlags::default().verify_witness(true).verify_p2sh(true);
    		assert_eq!(Ok(()), run_witness_test_tx_test("51".into(), &tx, &flags, 1000, 0)
    			.and_then(|_| run_witness_test_tx_test("00204d6c2a32c87821d68fc016fca70797abdb80df6cd84651d40a9300c6bad79e62".into(), &tx, &flags, 1000, 1)));
    	}

    	// https://github.com/bitcoin/bitcoin/blob/7ee6c434ce8df9441abcf1718555cc7728a4c575/src/test/data/tx_valid.json#L439
    	#[test]
    	fn witness_1_byte_push_not_witness_script_pubkey() {
    		let tx = "010000000100010000000000000000000000000000000000000000000000000000000000000000000000ffffffff01e803000000000000015100000000".into();
    		let flags = VerificationFlags::default().verify_witness(true).verify_p2sh(true);
    		assert_eq!(Ok(()), run_witness_test_tx_test("600101".into(), &tx, &flags, 1000, 0));
    	}

    	// https://github.com/bitcoin/bitcoin/blob/7ee6c434ce8df9441abcf1718555cc7728a4c575/src/test/data/tx_valid.json#L443
    	#[test]
    	fn witness_41_byte_push_not_witness_script_pubkey() {
    		let tx = "010000000100010000000000000000000000000000000000000000000000000000000000000000000000ffffffff01e803000000000000015100000000".into();
    		let flags = VerificationFlags::default().verify_witness(true).verify_p2sh(true);
    		assert_eq!(Ok(()), run_witness_test_tx_test("6029ff25429251b5a84f452230a3c75fd886b7fc5a7865ce4a7bb7a9d7c5be6da3dbff0000000000000000".into(), &tx, &flags, 1000, 0));
    	}

    	// https://github.com/bitcoin/bitcoin/blob/7ee6c434ce8df9441abcf1718555cc7728a4c575/src/test/data/tx_valid.json#L447
    	#[test]
    	fn witness_version_must_use_op1_to_op16() {
    		let tx = "010000000100010000000000000000000000000000000000000000000000000000000000000000000000ffffffff01e803000000000000015100000000".into();
    		let flags = VerificationFlags::default().verify_witness(true).verify_p2sh(true);
    		assert_eq!(Ok(()), run_witness_test_tx_test("0110020001".into(), &tx, &flags, 1000, 0));
    	}

    	// https://github.com/bitcoin/bitcoin/blob/7ee6c434ce8df9441abcf1718555cc7728a4c575/src/test/data/tx_valid.json#L451
    	#[test]
    	fn witness_program_push_must_be_canonical() {
    		let tx = "010000000100010000000000000000000000000000000000000000000000000000000000000000000000ffffffff01e803000000000000015100000000".into();
    		let flags = VerificationFlags::default().verify_witness(true).verify_p2sh(true);
    		assert_eq!(Ok(()), run_witness_test_tx_test("604c020001".into(), &tx, &flags, 1000, 0));
    	}

    	// https://github.com/bitcoin/bitcoin/blob/7ee6c434ce8df9441abcf1718555cc7728a4c575/src/test/data/tx_valid.json#L455
    	#[test]
    	fn witness_single_anyone_does_not_hash_input_position() {
    		let tx = " ".into();
    		let flags = VerificationFlags::default().verify_witness(true).verify_p2sh(true);
    		assert_eq!(Ok(()), run_witness_test_tx_test("00144c9c3dfac4207d5d8cb89df5722cb3d712385e3f".into(), &tx, &flags, 1000, 0)
    			.and_then(|_| run_witness_test_tx_test("00144c9c3dfac4207d5d8cb89df5722cb3d712385e3f".into(), &tx, &flags, 1001, 1)));
    	}

    	// https://github.com/bitcoin/bitcoin/blob/7ee6c434ce8df9441abcf1718555cc7728a4c575/src/test/data/tx_valid.json#L460
    	#[test]
    	fn witness_single_anyone_does_not_hash_input_position_permutation() {
    		let tx = " ".into();
    		let flags = VerificationFlags::default().verify_witness(true).verify_p2sh(true);
    		assert_eq!(Ok(()), run_witness_test_tx_test("00144c9c3dfac4207d5d8cb89df5722cb3d712385e3f".into(), &tx, &flags, 1001, 0)
    			.and_then(|_| run_witness_test_tx_test("00144c9c3dfac4207d5d8cb89df5722cb3d712385e3f".into(), &tx, &flags, 1000, 1)));
    	}

    	// https://github.com/bitcoin/bitcoin/blob/7ee6c434ce8df9441abcf1718555cc7728a4c575/src/test/data/tx_valid.json#L465
    	#[test]
    	fn witness_non_witness_single_anyone_hash_input_position_ok() {
    		let tx = " ".into();
    		let flags = VerificationFlags::default().verify_witness(true).verify_p2sh(true);
    		assert_eq!(Ok(()), run_witness_test_tx_test("2103596d3451025c19dbbdeb932d6bf8bfb4ad499b95b6f88db8899efac102e5fc71ac".into(), &tx, &flags, 1000, 0)
    			.and_then(|_| run_witness_test_tx_test("2103596d3451025c19dbbdeb932d6bf8bfb4ad499b95b6f88db8899efac102e5fc71ac".into(), &tx, &flags, 1001, 1)));
    	}

    	// https://github.com/bitcoin/bitcoin/blob/7ee6c434ce8df9441abcf1718555cc7728a4c575/src/test/data/tx_valid.json#L471
    	#[test]
    	fn witness_bip143_example1() {
    		let tx = " ".into();
    		let flags = VerificationFlags::default().verify_witness(true).verify_p2sh(true);
    		assert_eq!(Ok(()), run_witness_test_tx_test("21036d5c20fa14fb2f635474c1dc4ef5909d4568e5569b79fc94d3448486e14685f8ac".into(), &tx, &flags, 156250000, 0)
    			.and_then(|_| run_witness_test_tx_test("00205d1b56b63d714eebe542309525f484b7e9d6f686b3781b6f61ef925d66d6f6a0".into(), &tx, &flags, 4900000000, 1)));
    	}

    	// https://github.com/bitcoin/bitcoin/blob/7ee6c434ce8df9441abcf1718555cc7728a4c575/src/test/data/tx_valid.json#L476
    	#[test]
    	fn witness_bip143_example2() {
    		let tx = " ".into();
    		let flags = VerificationFlags::default().verify_witness(true).verify_p2sh(true);
    		assert_eq!(Ok(()), run_witness_test_tx_test("0020ba468eea561b26301e4cf69fa34bde4ad60c81e70f059f045ca9a79931004a4d".into(), &tx, &flags, 16777215, 0)
    			.and_then(|_| run_witness_test_tx_test("0020d9bbfbe56af7c4b7f960a70d7ea107156913d9e5a26b0a71429df5e097ca6537".into(), &tx, &flags, 16777215, 1)));
    	}

    	// https://github.com/bitcoin/bitcoin/blob/7ee6c434ce8df9441abcf1718555cc7728a4c575/src/test/data/tx_valid.json#L481
    	#[test]
    	fn witness_bip143_example3() {
    		let tx = " ".into();
    		let flags = VerificationFlags::default().verify_witness(true).verify_p2sh(true);
    		assert_eq!(Ok(()), run_witness_test_tx_test("0020d9bbfbe56af7c4b7f960a70d7ea107156913d9e5a26b0a71429df5e097ca6537".into(), &tx, &flags, 16777215, 0)
    			.and_then(|_| run_witness_test_tx_test("0020ba468eea561b26301e4cf69fa34bde4ad60c81e70f059f045ca9a79931004a4d".into(), &tx, &flags, 16777215, 1)));
    	}

    	// https://github.com/bitcoin/bitcoin/blob/7ee6c434ce8df9441abcf1718555cc7728a4c575/src/test/data/tx_valid.json#L486
    	#[test]
    	fn witness_bip143_example4() {
    		let tx = " ".into();
    		let flags = VerificationFlags::default().verify_witness(true).verify_p2sh(true);
    		assert_eq!(Ok(()), run_witness_test_tx_test("a9149993a429037b5d912407a71c252019287b8d27a587".into(), &tx, &flags, 987654321, 0));
    	}

    	#[test]
    	fn op_cat_disabled_by_default() {
    		let script = Builder::default()
    			.push_data(&[1; 1])
    			.push_data(&[1; 1])
    			.push_opcode(Opcode::OP_CAT)
    			.into_script();
    		let result = Err(Error::DisabledOpcode(Opcode::OP_CAT));
    		basic_test_with_flags(&script, &VerificationFlags::default(), result,
    			vec![].into());
    	}

    	#[test]
    	fn op_cat_max_and_non_empty_succeeds() {
    		// maxlen_x empty OP_CAT → ok
    		let script = Builder::default()
    			.push_data(&[1; MAX_SCRIPT_ELEMENT_SIZE])
    			.push_data(&[1; 0])
    			.push_opcode(Opcode::OP_CAT)
    			.into_script();
    		let result = Ok(true);
    		basic_test_with_flags(&script, &VerificationFlags::default().verify_concat(true), result,
    			vec![vec![1; MAX_SCRIPT_ELEMENT_SIZE].into()].into());
    	}

    	#[test]
    	fn op_cat_max_and_non_empty_fails() {
    		// maxlen_x y OP_CAT → failure
    		let script = Builder::default()
    			.push_data(&[1; MAX_SCRIPT_ELEMENT_SIZE])
    			.push_data(&[1; 1])
    			.push_opcode(Opcode::OP_CAT)
    			.into_script();
    		let result = Err(Error::PushSize);
    		basic_test_with_flags(&script, &VerificationFlags::default().verify_concat(true), result,
    			vec![].into());
    	}

    	#[test]
    	fn op_cat_large_and_large_fails() {
    		// large_x large_y OP_CAT → failure
    		let script = Builder::default()
    			.push_data(&[1; MAX_SCRIPT_ELEMENT_SIZE / 2 + 1])
    			.push_data(&[1; MAX_SCRIPT_ELEMENT_SIZE / 2 + 1])
    			.push_opcode(Opcode::OP_CAT)
    			.into_script();
    		let result = Err(Error::PushSize);
    		basic_test_with_flags(&script, &VerificationFlags::default().verify_concat(true), result,
    			vec![].into());
    	}

    	#[test]
    	fn op_cat_empty_and_empty_succeeds() {
    		// OP_0 OP_0 OP_CAT → OP_0
    		let script = Builder::default()
    			.push_opcode(Opcode::OP_0)
    			.push_opcode(Opcode::OP_0)
    			.push_opcode(Opcode::OP_CAT)
    			.into_script();
    		let result = Ok(false);
    		basic_test_with_flags(&script, &VerificationFlags::default().verify_concat(true), result,
    			vec![Bytes::default()].into());
    	}

    	#[test]
    	fn op_cat_non_empty_and_empty_succeeds() {
    		// x OP_0 OP_CAT → x
    		let script = Builder::default()
    			.push_data(&[1; 1])
    			.push_opcode(Opcode::OP_0)
    			.push_opcode(Opcode::OP_CAT)
    			.into_script();
    		let result = Ok(true);
    		basic_test_with_flags(&script, &VerificationFlags::default().verify_concat(true), result,
    			vec![vec![1; 1].into()].into());
    	}

    	#[test]
    	fn op_cat_empty_and_non_empty_succeeds() {
    		// OP_0 x OP_CAT → x
    		let script = Builder::default()
    			.push_opcode(Opcode::OP_0)
    			.push_data(&[1; 1])
    			.push_opcode(Opcode::OP_CAT)
    			.into_script();
    		let result = Ok(true);
    		basic_test_with_flags(&script, &VerificationFlags::default().verify_concat(true), result,
    			vec![vec![1; 1].into()].into());
    	}

    	#[test]
    	fn op_cat_non_empty_and_non_empty_succeeds() {
    		// x y OP_CAT → concat(x,y)
    		let script = Builder::default()
    			.push_data(&[0x11])
    			.push_data(&[0x22, 0x33])
    			.push_opcode(Opcode::OP_CAT)
    			.into_script();
    		let result = Ok(true);
    		basic_test_with_flags(&script, &VerificationFlags::default().verify_concat(true), result,
    			vec![vec![0x11, 0x22, 0x33].into()].into());
    	}

    	#[test]
    	fn op_split_disabled_by_default() {
    		let script = Builder::default()
    			.push_data(&[0x11, 0x22])
    			.push_num(1.into())
    			.push_opcode(Opcode::OP_SUBSTR)
    			.into_script();
    		let result = Err(Error::DisabledOpcode(Opcode::OP_SUBSTR));
    		basic_test_with_flags(&script, &VerificationFlags::default(), result,
    			vec![].into());
    	}

    	#[test]
    	fn op_split_empty_at_zero_succeeds() {
    		// OP_0 0 OP_SPLIT -> OP_0 OP_0
    		let script = Builder::default()
    			.push_opcode(Opcode::OP_0)
    			.push_num(0.into())
    			.push_opcode(Opcode::OP_SUBSTR)
    			.into_script();
    		let result = Ok(false);
    		basic_test_with_flags(&script, &VerificationFlags::default().verify_split(true), result,
    			vec![vec![0; 0].into(), vec![0; 0].into()].into());
    	}

    	#[test]
    	fn op_split_non_empty_at_zero_succeeds() {
    		// x 0 OP_SPLIT -> OP_0 x
    		let script = Builder::default()
    			.push_data(&[0x00, 0x11, 0x22])
    			.push_num(0.into())
    			.push_opcode(Opcode::OP_SUBSTR)
    			.into_script();
    		let result = Ok(true);
    		basic_test_with_flags(&script, &VerificationFlags::default().verify_split(true), result,
    			vec![vec![0; 0].into(), vec![0x00, 0x11, 0x22].into()].into());
    	}

    	#[test]
    	fn op_split_non_empty_at_len_succeeds() {
    		// x len(x) OP_SPLIT -> x OP_0
    		let script = Builder::default()
    			.push_data(&[0x00, 0x11, 0x22])
    			.push_num(3.into())
    			.push_opcode(Opcode::OP_SUBSTR)
    			.into_script();
    		let result = Ok(false);
    		basic_test_with_flags(&script, &VerificationFlags::default().verify_split(true), result,
    			vec![vec![0x00, 0x11, 0x22].into(), vec![0; 0].into()].into());
    	}

    	#[test]
    	fn op_split_non_empty_at_post_len_fails() {
    		// x (len(x) + 1) OP_SPLIT -> FAIL
    		let script = Builder::default()
    			.push_data(&[0x00, 0x11, 0x22])
    			.push_num(4.into())
    			.push_opcode(Opcode::OP_SUBSTR)
    			.into_script();
    		let result = Err(Error::InvalidSplitRange);
    		basic_test_with_flags(&script, &VerificationFlags::default().verify_split(true), result,
    			vec![].into());
    	}

    	#[test]
    	fn op_split_non_empty_at_mid_succeeds() {
    		let script = Builder::default()
    			.push_data(&[0x00, 0x11, 0x22])
    			.push_num(2.into())
    			.push_opcode(Opcode::OP_SUBSTR)
    			.into_script();
    		let result = Ok(true);
    		basic_test_with_flags(&script, &VerificationFlags::default().verify_split(true), result,
    			vec![vec![0x00, 0x11].into(), vec![0x22].into()].into());
    	}

    	#[test]
    	fn op_split_fails_if_position_is_nan() {
    		let script = Builder::default()
    			.push_data(&[0x00, 0x11, 0x22])
    			.push_opcode(Opcode::OP_1NEGATE) // NaN
    			.push_opcode(Opcode::OP_SUBSTR)
    			.into_script();
    		let result = Err(Error::InvalidStackOperation);
    		basic_test_with_flags(&script, &VerificationFlags::default().verify_split(true), result,
    			vec![].into());
    	}

    	#[test]
    	fn op_split_fails_if_position_is_negative() {
    		let script = Builder::default()
    			.push_data(&[0x00, 0x11, 0x22])
    			.push_num((-10).into())
    			.push_opcode(Opcode::OP_SUBSTR)
    			.into_script();
    		let result = Err(Error::InvalidStackOperation);
    		basic_test_with_flags(&script, &VerificationFlags::default().verify_split(true), result,
    			vec![].into());
    	}

    	#[test]
    	fn op_and_disabled_by_default() {
    		let script = Builder::default()
    			.push_data(&[0x11])
    			.push_data(&[0x22])
    			.push_opcode(Opcode::OP_AND)
    			.into_script();
    		let result = Err(Error::DisabledOpcode(Opcode::OP_AND));
    		basic_test_with_flags(&script, &VerificationFlags::default(), result,
    			vec![].into());
    	}

    	#[test]
    	fn op_and_fails_with_different_len_args() {
    		let script = Builder::default()
    			.push_data(&[0x11, 0x22])
    			.push_data(&[0x22])
    			.push_opcode(Opcode::OP_AND)
    			.into_script();
    		let result = Err(Error::InvalidOperandSize);
    		basic_test_with_flags(&script, &VerificationFlags::default().verify_and(true), result,
    			vec![].into());
    	}

    	#[test]
    	fn op_and_succeeds() {
    		let script = Builder::default()
    			.push_data(&[0x34, 0x56])
    			.push_data(&[0x56, 0x78])
    			.push_opcode(Opcode::OP_AND)
    			.into_script();
    		let result = Ok(true);
    		basic_test_with_flags(&script, &VerificationFlags::default().verify_and(true), result,
    			vec![vec![0x14, 0x50].into()].into());
    	}

    	#[test]
    	fn op_or_disabled_by_default() {
    		let script = Builder::default()
    			.push_data(&[0x11])
    			.push_data(&[0x22])
    			.push_opcode(Opcode::OP_OR)
    			.into_script();
    		let result = Err(Error::DisabledOpcode(Opcode::OP_OR));
    		basic_test_with_flags(&script, &VerificationFlags::default(), result,
    			vec![].into());
    	}

    	#[test]
    	fn op_or_fails_with_different_len_args() {
    		let script = Builder::default()
    			.push_data(&[0x11, 0x22])
    			.push_data(&[0x22])
    			.push_opcode(Opcode::OP_OR)
    			.into_script();
    		let result = Err(Error::InvalidOperandSize);
    		basic_test_with_flags(&script, &VerificationFlags::default().verify_or(true), result,
    			vec![].into());
    	}

    	#[test]
    	fn op_or_succeeds() {
    		let script = Builder::default()
    			.push_data(&[0x34, 0x56])
    			.push_data(&[0x56, 0x78])
    			.push_opcode(Opcode::OP_OR)
    			.into_script();
    		let result = Ok(true);
    		basic_test_with_flags(&script, &VerificationFlags::default().verify_or(true), result,
    			vec![vec![0x76, 0x7e].into()].into());
    	}

    	#[test]
    	fn op_xor_disabled_by_default() {
    		let script = Builder::default()
    			.push_data(&[0x11])
    			.push_data(&[0x22])
    			.push_opcode(Opcode::OP_XOR)
    			.into_script();
    		let result = Err(Error::DisabledOpcode(Opcode::OP_XOR));
    		basic_test_with_flags(&script, &VerificationFlags::default(), result,
    			vec![].into());
    	}

    	#[test]
    	fn op_xor_fails_with_different_len_args() {
    		let script = Builder::default()
    			.push_data(&[0x11, 0x22])
    			.push_data(&[0x22])
    			.push_opcode(Opcode::OP_XOR)
    			.into_script();
    		let result = Err(Error::InvalidOperandSize);
    		basic_test_with_flags(&script, &VerificationFlags::default().verify_xor(true), result,
    			vec![].into());
    	}

    	#[test]
    	fn op_xor_succeeds() {
    		let script = Builder::default()
    			.push_data(&[0x34, 0x56])
    			.push_data(&[0x56, 0x78])
    			.push_opcode(Opcode::OP_XOR)
    			.into_script();
    		let result = Ok(true);
    		basic_test_with_flags(&script, &VerificationFlags::default().verify_xor(true), result,
    			vec![vec![0x62, 0x2e].into()].into());
    	}

    	#[test]
    	fn op_div_disabled_by_default() {
    		let script = Builder::default()
    			.push_num(13.into())
    			.push_num(5.into())
    			.push_opcode(Opcode::OP_DIV)
    			.into_script();
    		let result = Err(Error::DisabledOpcode(Opcode::OP_DIV));
    		basic_test_with_flags(&script, &VerificationFlags::default(), result,
    			vec![].into());
    	}

    	#[test]
    	fn op_div_num_by_nan_fails() {
    		// a b OP_DIV -> failure where !isnum(a) or !isnum(b). Both operands must be valid numbers
    		let script = Builder::default()
    			.push_opcode(Opcode::OP_1SUB)
    			.push_num(5.into())
    			.push_opcode(Opcode::OP_DIV)
    			.into_script();
    		let result = Err(Error::InvalidStackOperation);
    		basic_test_with_flags(&script, &VerificationFlags::default().verify_div(true), result,
    			vec![].into());
    	}

    	#[test]
    	fn op_div_nan_by_num_fails() {
    		// a b OP_DIV -> failure where !isnum(a) or !isnum(b). Both operands must be valid numbers
    		let script = Builder::default()
    			.push_num(5.into())
    			.push_opcode(Opcode::OP_1SUB)
    			.push_opcode(Opcode::OP_DIV)
    			.into_script();
    		let result = Err(Error::InvalidStackOperation);
    		basic_test_with_flags(&script, &VerificationFlags::default().verify_div(true), result,
    			vec![].into());
    	}

    	#[test]
    	fn op_div_num_by_zero_fails() {
    		// a 0 OP_DIV -> failure. Division by positive zero (all sizes), negative zero (all sizes), OP_0
    		let script = Builder::default()
    			.push_num(0.into())
    			.push_num(5.into())
    			.push_opcode(Opcode::OP_DIV)
    			.into_script();
    		let result = Err(Error::DivisionByZero);
    		basic_test_with_flags(&script, &VerificationFlags::default().verify_div(true), result,
    			vec![].into());
    	}

    	#[test]
    	fn op_div_negative_by_negative_succeeds() {
    		let script = Builder::default()
    			.push_num((-5).into())
    			.push_num((-13).into())
    			.push_opcode(Opcode::OP_DIV)
    			.into_script();
    		let result = Ok(true);
    		basic_test_with_flags(&script, &VerificationFlags::default().verify_div(true), result,
    			vec![Num::from(2).to_bytes()].into());
    	}

    	#[test]
    	fn op_div_negative_by_positive_succeeds() {
    		let script = Builder::default()
    			.push_num(5.into())
    			.push_num((-13).into())
    			.push_opcode(Opcode::OP_DIV)
    			.into_script();
    		let result = Ok(true);
    		basic_test_with_flags(&script, &VerificationFlags::default().verify_div(true), result,
    			vec![Num::from(-2).to_bytes()].into());
    	}

    	#[test]
    	fn op_div_positive_by_negative_succeeds() {
    		let script = Builder::default()
    			.push_num((-5).into())
    			.push_num(13.into())
    			.push_opcode(Opcode::OP_DIV)
    			.into_script();
    		let result = Ok(true);
    		basic_test_with_flags(&script, &VerificationFlags::default().verify_div(true), result,
    			vec![Num::from(-2).to_bytes()].into());
    	}

    	#[test]
    	fn op_div_positive_by_positive_succeeds() {
    		let script = Builder::default()
    			.push_num(5.into())
    			.push_num(13.into())
    			.push_opcode(Opcode::OP_DIV)
    			.into_script();
    		let result = Ok(true);
    		basic_test_with_flags(&script, &VerificationFlags::default().verify_div(true), result,
    			vec![Num::from(2).to_bytes()].into());
    	}

    	#[test]
    	fn op_mod_disabled_by_default() {
    		let script = Builder::default()
    			.push_num(13.into())
    			.push_num(5.into())
    			.push_opcode(Opcode::OP_MOD)
    			.into_script();
    		let result = Err(Error::DisabledOpcode(Opcode::OP_MOD));
    		basic_test_with_flags(&script, &VerificationFlags::default(), result,
    			vec![].into());
    	}

    	#[test]
    	fn op_mod_num_by_nan_fails() {
    		// a b OP_MOD -> failure where !isnum(a) or !isnum(b). Both operands must be valid numbers
    		let script = Builder::default()
    			.push_opcode(Opcode::OP_1SUB)
    			.push_num(5.into())
    			.push_opcode(Opcode::OP_MOD)
    			.into_script();
    		let result = Err(Error::InvalidStackOperation);
    		basic_test_with_flags(&script, &VerificationFlags::default().verify_mod(true), result,
    			vec![].into());
    	}

    	#[test]
    	fn op_mod_nan_by_num_fails() {
    		// a b OP_MOD -> failure where !isnum(a) or !isnum(b). Both operands must be valid numbers
    		let script = Builder::default()
    			.push_num(5.into())
    			.push_opcode(Opcode::OP_1SUB)
    			.push_opcode(Opcode::OP_MOD)
    			.into_script();
    		let result = Err(Error::InvalidStackOperation);
    		basic_test_with_flags(&script, &VerificationFlags::default().verify_mod(true), result,
    			vec![].into());
    	}

    	#[test]
    	fn op_mod_num_by_zero_fails() {
    		// a 0 OP_MOD -> failure. Division by positive zero (all sizes), negative zero (all sizes), OP_0
    		let script = Builder::default()
    			.push_num(0.into())
    			.push_num(5.into())
    			.push_opcode(Opcode::OP_MOD)
    			.into_script();
    		let result = Err(Error::DivisionByZero);
    		basic_test_with_flags(&script, &VerificationFlags::default().verify_mod(true), result,
    			vec![].into());
    	}

    	#[test]
    	fn op_mod_negative_by_negative_succeeds() {
    		let script = Builder::default()
    			.push_num((-5).into())
    			.push_num((-13).into())
    			.push_opcode(Opcode::OP_MOD)
    			.into_script();
    		let result = Ok(true);
    		basic_test_with_flags(&script, &VerificationFlags::default().verify_mod(true), result,
    			vec![Num::from(-3).to_bytes()].into());
    	}

    	#[test]
    	fn op_mod_negative_by_positive_succeeds() {
    		let script = Builder::default()
    			.push_num(5.into())
    			.push_num((-13).into())
    			.push_opcode(Opcode::OP_MOD)
    			.into_script();
    		let result = Ok(true);
    		basic_test_with_flags(&script, &VerificationFlags::default().verify_mod(true), result,
    			vec![Num::from(-3).to_bytes()].into());
    	}

    	#[test]
    	fn op_mod_positive_by_negative_succeeds() {
    		let script = Builder::default()
    			.push_num((-5).into())
    			.push_num(13.into())
    			.push_opcode(Opcode::OP_MOD)
    			.into_script();
    		let result = Ok(true);
    		basic_test_with_flags(&script, &VerificationFlags::default().verify_mod(true), result,
    			vec![Num::from(3).to_bytes()].into());
    	}

    	#[test]
    	fn op_mod_positive_by_positive_succeeds() {
    		let script = Builder::default()
    			.push_num(5.into())
    			.push_num(13.into())
    			.push_opcode(Opcode::OP_MOD)
    			.into_script();
    		let result = Ok(true);
    		basic_test_with_flags(&script, &VerificationFlags::default().verify_mod(true), result,
    			vec![Num::from(3).to_bytes()].into());
    	}

    	#[test]
    	fn op_bin2num_disabled_by_default() {
    		let script = Builder::default()
    			.push_num(0.into())
    			.push_opcode(Opcode::OP_RIGHT)
    			.into_script();
    		let result = Err(Error::DisabledOpcode(Opcode::OP_RIGHT));
    		basic_test_with_flags(&script, &VerificationFlags::default(), result,
    			vec![].into());
    	}

    	#[test]
    	fn test_bin2num_all() {
    		fn test_bin2num(input: &[u8], result: Result<bool, Error>, output: Vec<u8>) {
    			let script = Builder::default()
    				.push_bytes(input)
    				.push_opcode(Opcode::OP_RIGHT)
    				.into_script();
    			let stack = if result.is_ok() {
    				vec![output.into()].into()
    			} else {
    				vec![]
    			}.into();
    			let flags = VerificationFlags::default()
    				.verify_bin2num(true);
    			basic_test_with_flags(&script, &flags, result, stack);
    		}

    		test_bin2num(&[0x02, 0x00, 0x00, 0x00, 0x00], Ok(true), vec![0x02]);
    		test_bin2num(&[0x05, 0x00, 0x80], Ok(true), vec![0x85]);
    		test_bin2num(&[0x02, 0x02, 0x02, 0x02, 0x02], Err(Error::NumberOverflow), vec![]);
    		test_bin2num(&[0x00], Ok(false), vec![]);
    		test_bin2num(&[0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00], Ok(true), vec![0x01]);
    		test_bin2num(&[0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80], Ok(true), vec![0x81]);
    		test_bin2num(&[0x80], Ok(false), vec![]);
    		test_bin2num(&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80], Ok(false), vec![]);
    	}

    	#[test]
    	fn op_num2bin_disabled_by_default() {
    		let script = Builder::default()
    			.push_num(1.into())
    			.push_num(1.into())
    			.push_opcode(Opcode::OP_LEFT)
    			.into_script();
    		let result = Err(Error::DisabledOpcode(Opcode::OP_LEFT));
    		basic_test_with_flags(&script, &VerificationFlags::default(), result,
    			vec![].into());
    	}

    	#[test]
    	fn test_num2bin_all() {
    		fn test_num2bin(num: &[u8], size: &[u8], result: Result<bool, Error>, output: Vec<u8>) {
    			let script = Builder::default()
    				.push_data(num)
    				.push_data(size)
    				.push_opcode(Opcode::OP_LEFT)
    				.into_script();
    			let stack = if result.is_ok() {
    				vec![output.into()].into()
    			} else {
    				vec![]
    			}.into();

    			let flags = VerificationFlags::default()
    				.verify_num2bin(true);
    			basic_test_with_flags(&script, &flags, result, stack);
    		}

    		fn test_num2bin_num(num: Num, size: Num, result: Result<bool, Error>, output: Vec<u8>) {
    			test_num2bin(&*num.to_bytes(), &*size.to_bytes(), result, output)
    		}

    		test_num2bin_num(256.into(), 1.into(), Err(Error::ImpossibleEncoding), vec![0x00]);
    		test_num2bin_num(1.into(), (MAX_SCRIPT_ELEMENT_SIZE + 1).into(), Err(Error::PushSize), vec![0x00]);

    		test_num2bin_num(0.into(), 0.into(), Ok(false), vec![]);
    		test_num2bin_num(0.into(), 1.into(), Ok(false), vec![0x00]);
    		test_num2bin_num(0.into(), 7.into(), Ok(false), vec![0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]);
    		test_num2bin_num(1.into(), 1.into(), Ok(true), vec![0x01]);
    		test_num2bin_num((-42).into(), 1.into(), Ok(true), Num::from(-42).to_bytes().to_vec());
    		test_num2bin_num((-42).into(), 2.into(), Ok(true), vec![0x2a, 0x80]);
    		test_num2bin_num((-42).into(), 10.into(), Ok(true), vec![0x2a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80]);
    		test_num2bin_num((-42).into(), 520.into(), Ok(true), ::std::iter::once(0x2a)
    			.chain(::std::iter::repeat(0x00).take(518))
    			.chain(::std::iter::once(0x80)).collect());
    		test_num2bin_num((-42).into(), 521.into(), Err(Error::PushSize), vec![]);
    		test_num2bin_num((-42).into(), (-3).into(), Err(Error::PushSize), vec![]);

    		test_num2bin(&vec![0xab, 0xcd, 0xef, 0x42, 0x80], &vec![0x04], Ok(true), vec![0xab, 0xcd, 0xef, 0xc2]);
    		test_num2bin(&vec![0x80], &vec![0x00], Ok(false), vec![]);
    		test_num2bin(&vec![0x80], &vec![0x03], Ok(false), vec![0x00, 0x00, 0x00]);
    	}

    	#[test]
    	fn test_num_bin_conversions_are_reverse_ops() {
    		let script = Builder::default()
    			// convert num2bin
    			.push_num(123456789.into())
    			.push_num(8.into())
    			.push_opcode(Opcode::OP_LEFT)
    			// and then back bin2num
    			.push_opcode(Opcode::OP_RIGHT)
    			// check that numbers are the same
    			.push_num(123456789.into())
    			.push_opcode(Opcode::OP_EQUAL)
    			.into_script();

    		let flags = VerificationFlags::default()
    			.verify_num2bin(true)
    			.verify_bin2num(true);
    		basic_test_with_flags(&script, &flags, Ok(true), vec![vec![0x01].into()].into());
    	}

    	#[test]
    	fn test_split_cat_are_reverse_ops() {
    		let script = Builder::default()
    			// split array
    			.push_data(&vec![0x01, 0x02, 0x03, 0x04, 0x05])
    			.push_num(2.into())
    			.push_opcode(Opcode::OP_SUBSTR)
    			// and then concat again
    			.push_opcode(Opcode::OP_CAT)
    			// check that numbers are the same
    			.push_data(&vec![0x01, 0x02, 0x03, 0x04, 0x05])
    			.push_opcode(Opcode::OP_EQUAL)
    			.into_script();

    		let flags = VerificationFlags::default()
    			.verify_concat(true)
    			.verify_split(true);
    		basic_test_with_flags(&script, &flags, Ok(true), vec![vec![0x01].into()].into());
    	}

    	#[test]
    	fn checkdatasig_spec_tests() {
    		// official tests from:
    		// https://github.com/bitcoincashorg/bitcoincash.org/blob/0c6f91b0b713aae3bc6c9834b46e80e247ff5fab/spec/op_checkdatasig.md

    		let kp = KeyPair::from_private(Private { network: Network::Mainnet, secret: 1.into(), compressed: false, }).unwrap();

    		let pubkey = kp.public().clone();
    		let message = vec![42u8; 32];
    		let correct_signature = kp.private().sign(&Message::from(sha256(&message))).unwrap();
    		let correct_signature_for_other_message = kp.private().sign(&[43u8; 32].into()).unwrap();
    		let mut correct_signature = correct_signature.to_vec();
    		let mut correct_signature_for_other_message = correct_signature_for_other_message.to_vec();
    		correct_signature.push(0x81);
    		correct_signature_for_other_message.push(0x81);

    		let correct_flags = VerificationFlags::default()
    			.verify_checkdatasig(true)
    			.verify_dersig(true)
    			.verify_strictenc(true);
    		let incorrect_flags = VerificationFlags::default().verify_checkdatasig(false);

    		let correct_signature_script = Builder::default()
    			.push_data(&*correct_signature)
    			.push_data(&*message)
    			.push_data(&*pubkey)
    			.push_opcode(Opcode::OP_CHECKDATASIG)
    			.into_script();

    		// <sig> <msg> <pubKey> OP_CHECKDATASIG fails if 15 November 2018 protocol upgrade is not yet activated.
    		basic_test_with_flags(&correct_signature_script, &incorrect_flags, Err(Error::DisabledOpcode(Opcode::OP_CHECKDATASIG)), vec![].into());

    		// <sig> <msg> OP_CHECKDATASIG fails if there are fewer than 3 items on stack.
    		let too_few_args_sig_script = Builder::default()
    			.push_data(&[1u8; 32])
    			.push_data(&*message)
    			.push_opcode(Opcode::OP_CHECKDATASIG)
    			.into_script();
    		basic_test_with_flags(&too_few_args_sig_script, &correct_flags, Err(Error::InvalidStackOperation), vec![].into());

    		// <sig> <msg> <pubKey> OP_CHECKDATASIG fails if <pubKey> is not a validly encoded public key.
    		let incorrect_pubkey_script = Builder::default()
    			.push_data(&*correct_signature)
    			.push_data(&*message)
    			.push_data(&[77u8; 15])
    			.push_opcode(Opcode::OP_CHECKDATASIG)
    			.into_script();
    		basic_test_with_flags(&incorrect_pubkey_script, &correct_flags, Err(Error::PubkeyType), vec![].into());

    		// assuming that check_signature_encoding correctness is proved by other tests:
    		// <sig> <msg> <pubKey> OP_CHECKDATASIG fails if <sig> is not a validly encoded signature with strict DER encoding.
    		// <sig> <msg> <pubKey> OP_CHECKDATASIG fails if signature <sig> is not empty and does not pass the Low S check.
    		let incorrectly_encoded_signature_script = Builder::default()
    			.push_data(&[0u8; 65])
    			.push_data(&*message)
    			.push_data(&*pubkey)
    			.push_opcode(Opcode::OP_CHECKDATASIG)
    			.into_script();
    		basic_test_with_flags(&incorrectly_encoded_signature_script, &correct_flags, Err(Error::SignatureDer), vec![].into());

    		// <sig> <msg> <pubKey> OP_CHECKDATASIG fails if signature <sig> is not empty and does not pass signature validation of <msg> and <pubKey>.
    		let incorrect_signature_script = Builder::default()
    			.push_data(&*correct_signature_for_other_message)
    			.push_data(&*message)
    			.push_data(&*pubkey)
    			.push_opcode(Opcode::OP_CHECKDATASIG)
    			.into_script();
    		basic_test_with_flags(&incorrect_signature_script, &correct_flags, Ok(false), vec![vec![0].into()].into());

    		// <sig> <msg> <pubKey> OP_CHECKDATASIG pops three elements and pushes false onto the stack if <sig> is an empty byte array.
    		let empty_signature_script = Builder::default()
    			.push_data(&[])
    			.push_data(&*message)
    			.push_data(&*pubkey)
    			.push_opcode(Opcode::OP_CHECKDATASIG)
    			.into_script();
    		basic_test_with_flags(&empty_signature_script, &correct_flags, Ok(false), vec![vec![0].into()].into());

    		// <sig> <msg> <pubKey> OP_CHECKDATASIG pops three elements and pushes true onto the stack if <sig> is a valid signature of <msg> with respect to <pubKey>.
    		basic_test_with_flags(&correct_signature_script, &correct_flags, Ok(true), vec![vec![1].into()].into());
    	}

    	#[test]
    	fn checkdatasigverify_spec_tests() {
    		// official tests from:
    		// https://github.com/bitcoincashorg/bitcoincash.org/blob/0c6f91b0b713aae3bc6c9834b46e80e247ff5fab/spec/op_checkdatasig.md

    		let kp = KeyPair::from_private(Private { network: Network::Mainnet, secret: 1.into(), compressed: false, }).unwrap();

    		let pubkey = kp.public().clone();
    		let message = vec![42u8; 32];
    		let correct_signature = kp.private().sign(&Message::from(sha256(&message))).unwrap();
    		let correct_signature_for_other_message = kp.private().sign(&[43u8; 32].into()).unwrap();
    		let mut correct_signature = correct_signature.to_vec();
    		let mut correct_signature_for_other_message = correct_signature_for_other_message.to_vec();
    		correct_signature.push(0x81);
    		correct_signature_for_other_message.push(0x81);

    		let correct_flags = VerificationFlags::default()
    			.verify_checkdatasig(true)
    			.verify_dersig(true)
    			.verify_strictenc(true);
    		let incorrect_flags = VerificationFlags::default().verify_checkdatasig(false);

    		let correct_signature_script = Builder::default()
    			.push_data(&*correct_signature)
    			.push_data(&*message)
    			.push_data(&*pubkey)
    			.push_opcode(Opcode::OP_CHECKDATASIGVERIFY)
    			.into_script();

    		// <sig> <msg> <pubKey> OP_CHECKDATASIGVERIFY fails if 15 November 2018 protocol upgrade is not yet activated.
    		basic_test_with_flags(&correct_signature_script, &incorrect_flags, Err(Error::DisabledOpcode(Opcode::OP_CHECKDATASIGVERIFY)), vec![].into());

    		// <sig> <msg> OP_CHECKDATASIGVERIFY fails if there are fewer than 3 item on stack.
    		let too_few_args_sig_script = Builder::default()
    			.push_data(&[1u8; 32])
    			.push_data(&*message)
    			.push_opcode(Opcode::OP_CHECKDATASIGVERIFY)
    			.into_script();
    		basic_test_with_flags(&too_few_args_sig_script, &correct_flags, Err(Error::InvalidStackOperation), vec![].into());

    		// <sig> <msg> <pubKey> OP_CHECKDATASIGVERIFYfails if <pubKey> is not a validly encoded public key.
    		let incorrect_pubkey_script = Builder::default()
    			.push_data(&*correct_signature)
    			.push_data(&*message)
    			.push_data(&[77u8; 15])
    			.push_opcode(Opcode::OP_CHECKDATASIGVERIFY)
    			.into_script();
    		basic_test_with_flags(&incorrect_pubkey_script, &correct_flags, Err(Error::PubkeyType), vec![].into());

    		// assuming that check_signature_encoding correctness is proved by other tests:
    		// <sig> <msg> <pubKey> OP_CHECKDATASIGVERIFY fails if <sig> is not a validly encoded signature with strict DER encoding.
    		// <sig> <msg> <pubKey> OP_CHECKDATASIGVERIFY fails if signature <sig> is not empty and does not pass the Low S check.
    		let incorrectly_encoded_signature_script = Builder::default()
    			.push_data(&[0u8; 65])
    			.push_data(&*message)
    			.push_data(&*pubkey)
    			.push_opcode(Opcode::OP_CHECKDATASIGVERIFY)
    			.into_script();
    		basic_test_with_flags(&incorrectly_encoded_signature_script, &correct_flags, Err(Error::SignatureDer), vec![].into());

    		// <sig> <msg> <pubKey> OP_CHECKDATASIGVERIFY fails if <sig> is not a valid signature of <msg> with respect to <pubKey>.
    		let incorrect_signature_script = Builder::default()
    			.push_data(&*correct_signature_for_other_message)
    			.push_data(&*message)
    			.push_data(&*pubkey)
    			.push_opcode(Opcode::OP_CHECKDATASIGVERIFY)
    			.into_script();
    		basic_test_with_flags(&incorrect_signature_script, &correct_flags, Err(Error::CheckDataSigVerify), vec![].into());

    		// <sig> <msg> <pubKey> OP_CHECKDATASIGVERIFY pops the top three stack elements if <sig> is a valid signature of <msg> with respect to <pubKey>.
    		// Ok(false) means success here, because OP_CHECKDATASIGVERIFY leaves empty stack
    		basic_test_with_flags(&correct_signature_script, &correct_flags, Ok(false), vec![].into());
    	}
    }

# Help

## Keyboard Shortcuts

`?`
  Show this help dialog
`S`
  Focus the search field
`↑`
  Move up in search results
`↓`
  Move down in search results
`↹`
  Switch tab
`⏎`
  Go to active search result
`+`
  Expand all sections
`-`
  Collapse all sections

## Search Tricks

Prefix searches with a type followed by a colon (e.g. `fn:`) to restrict the search to a given type.

Accepted types are: `fn`, `mod`, `struct`, `enum`, `trait`, `type`, `macro`, and `const`.

Search functions by type signature (e.g. `vec -> usize` or `* -> vec`)

Search multiple things at once by splitting your query with comma (e.g. `str,u8` or `String,struct:Vec,test`)