95
95
from test_framework .util import (
96
96
assert_raises_rpc_error ,
97
97
assert_equal ,
98
- random_bytes ,
99
98
)
100
99
from test_framework .wallet_util import generate_keypair
101
100
from test_framework .key import (
@@ -694,7 +693,7 @@ def spenders_taproot_active():
694
693
695
694
# Generate an invalid public key
696
695
while True :
697
- invalid_pub = random_bytes (32 )
696
+ invalid_pub = random . randbytes (32 )
698
697
if not secp256k1 .GE .is_valid_x (int .from_bytes (invalid_pub , 'big' )):
699
698
break
700
699
@@ -710,7 +709,7 @@ def spenders_taproot_active():
710
709
# == Tests for signature hashing ==
711
710
712
711
# Run all tests once with no annex, and once with a valid random annex.
713
- for annex in [None , lambda _ : bytes ([ANNEX_TAG ]) + random_bytes (random .randrange (0 , 250 ))]:
712
+ for annex in [None , lambda _ : bytes ([ANNEX_TAG ]) + random . randbytes (random .randrange (0 , 250 ))]:
714
713
# Non-empty annex is non-standard
715
714
no_annex = annex is None
716
715
@@ -739,7 +738,7 @@ def spenders_taproot_active():
739
738
scripts = [
740
739
("pk_codesep" , CScript (random_checksig_style (pubs [1 ]) + bytes ([OP_CODESEPARATOR ]))), # codesep after checksig
741
740
("codesep_pk" , CScript (bytes ([OP_CODESEPARATOR ]) + random_checksig_style (pubs [1 ]))), # codesep before checksig
742
- ("branched_codesep" , CScript ([random_bytes (random .randrange (2 , 511 )), OP_DROP , OP_IF , OP_CODESEPARATOR , pubs [0 ], OP_ELSE , OP_CODESEPARATOR , pubs [1 ], OP_ENDIF , OP_CHECKSIG ])), # branch dependent codesep
741
+ ("branched_codesep" , CScript ([random . randbytes (random .randrange (2 , 511 )), OP_DROP , OP_IF , OP_CODESEPARATOR , pubs [0 ], OP_ELSE , OP_CODESEPARATOR , pubs [1 ], OP_ENDIF , OP_CHECKSIG ])), # branch dependent codesep
743
742
# Note that the first data push in the "branched_codesep" script has the purpose of
744
743
# randomizing the sighash, both by varying script size and content. In order to
745
744
# avoid MINIMALDATA script verification errors caused by not-minimal-encoded data
@@ -792,8 +791,8 @@ def spenders_taproot_active():
792
791
add_spender (spenders , "siglen/empty_csv" , tap = tap , key = secs [2 ], leaf = "csv" , hashtype = hashtype , ** SINGLE_SIG , failure = {"sign" : b"" }, ** ERR_CHECKSIGVERIFY )
793
792
add_spender (spenders , "siglen/empty_cs" , tap = tap , key = secs [2 ], leaf = "cs_pos" , hashtype = hashtype , ** SINGLE_SIG , failure = {"sign" : b"" }, ** ERR_NO_SUCCESS )
794
793
add_spender (spenders , "siglen/empty_csa" , tap = tap , key = secs [2 ], leaf = "csa_pos" , hashtype = hashtype , ** SINGLE_SIG , failure = {"sign" : b"" }, ** ERR_NO_SUCCESS )
795
- add_spender (spenders , "siglen/empty_cs_neg" , tap = tap , key = secs [2 ], leaf = "cs_neg" , hashtype = hashtype , ** SINGLE_SIG , sign = b"" , failure = {"sign" : lambda _ : random_bytes (random .randrange (1 , 63 ))}, ** ERR_SIG_SIZE )
796
- add_spender (spenders , "siglen/empty_csa_neg" , tap = tap , key = secs [2 ], leaf = "csa_neg" , hashtype = hashtype , ** SINGLE_SIG , sign = b"" , failure = {"sign" : lambda _ : random_bytes (random .randrange (66 , 100 ))}, ** ERR_SIG_SIZE )
794
+ add_spender (spenders , "siglen/empty_cs_neg" , tap = tap , key = secs [2 ], leaf = "cs_neg" , hashtype = hashtype , ** SINGLE_SIG , sign = b"" , failure = {"sign" : lambda _ : random . randbytes (random .randrange (1 , 63 ))}, ** ERR_SIG_SIZE )
795
+ add_spender (spenders , "siglen/empty_csa_neg" , tap = tap , key = secs [2 ], leaf = "csa_neg" , hashtype = hashtype , ** SINGLE_SIG , sign = b"" , failure = {"sign" : lambda _ : random . randbytes (random .randrange (66 , 100 ))}, ** ERR_SIG_SIZE )
797
796
# Appending a zero byte to signatures invalidates them
798
797
add_spender (spenders , "siglen/padzero_keypath" , tap = tap , key = secs [3 ], hashtype = hashtype , ** SIG_ADD_ZERO , ** (ERR_SIG_HASHTYPE if hashtype == SIGHASH_DEFAULT else ERR_SIG_SIZE ))
799
798
add_spender (spenders , "siglen/padzero_csv" , tap = tap , key = secs [2 ], leaf = "csv" , hashtype = hashtype , ** SINGLE_SIG , ** SIG_ADD_ZERO , ** (ERR_SIG_HASHTYPE if hashtype == SIGHASH_DEFAULT else ERR_SIG_SIZE ))
@@ -874,14 +873,14 @@ def mutate(spk):
874
873
# Test that empty witnesses are invalid.
875
874
add_spender (spenders , "spendpath/emptywit" , tap = tap , leaf = "128deep" , ** SINGLE_SIG , key = secs [0 ], failure = {"witness" : []}, ** ERR_EMPTY_WITNESS )
876
875
# Test that adding garbage to the control block invalidates it.
877
- add_spender (spenders , "spendpath/padlongcontrol" , tap = tap , leaf = "128deep" , ** SINGLE_SIG , key = secs [0 ], failure = {"controlblock" : lambda ctx : default_controlblock (ctx ) + random_bytes (random .randrange (1 , 32 ))}, ** ERR_CONTROLBLOCK_SIZE )
876
+ add_spender (spenders , "spendpath/padlongcontrol" , tap = tap , leaf = "128deep" , ** SINGLE_SIG , key = secs [0 ], failure = {"controlblock" : lambda ctx : default_controlblock (ctx ) + random . randbytes (random .randrange (1 , 32 ))}, ** ERR_CONTROLBLOCK_SIZE )
878
877
# Test that truncating the control block invalidates it.
879
878
add_spender (spenders , "spendpath/trunclongcontrol" , tap = tap , leaf = "128deep" , ** SINGLE_SIG , key = secs [0 ], failure = {"controlblock" : lambda ctx : default_merklebranch (ctx )[0 :random .randrange (1 , 32 )]}, ** ERR_CONTROLBLOCK_SIZE )
880
879
881
880
scripts = [("s" , CScript ([pubs [0 ], OP_CHECKSIG ]))]
882
881
tap = taproot_construct (pubs [1 ], scripts )
883
882
# Test that adding garbage to the control block invalidates it.
884
- add_spender (spenders , "spendpath/padshortcontrol" , tap = tap , leaf = "s" , ** SINGLE_SIG , key = secs [0 ], failure = {"controlblock" : lambda ctx : default_controlblock (ctx ) + random_bytes (random .randrange (1 , 32 ))}, ** ERR_CONTROLBLOCK_SIZE )
883
+ add_spender (spenders , "spendpath/padshortcontrol" , tap = tap , leaf = "s" , ** SINGLE_SIG , key = secs [0 ], failure = {"controlblock" : lambda ctx : default_controlblock (ctx ) + random . randbytes (random .randrange (1 , 32 ))}, ** ERR_CONTROLBLOCK_SIZE )
885
884
# Test that truncating the control block invalidates it.
886
885
add_spender (spenders , "spendpath/truncshortcontrol" , tap = tap , leaf = "s" , ** SINGLE_SIG , key = secs [0 ], failure = {"controlblock" : lambda ctx : default_merklebranch (ctx )[0 :random .randrange (1 , 32 )]}, ** ERR_CONTROLBLOCK_SIZE )
887
886
# Test that truncating the control block to 1 byte ("-1 Merkle length") invalidates it
@@ -970,9 +969,9 @@ def big_spend_inputs(ctx):
970
969
# 24) Script that expects an input stack of 1001 elements
971
970
("t24" , CScript ([OP_DROP ] * 1000 + [pubs [1 ], OP_CHECKSIG ])),
972
971
# 25) Script that pushes a MAX_SCRIPT_ELEMENT_SIZE-bytes element
973
- ("t25" , CScript ([random_bytes (MAX_SCRIPT_ELEMENT_SIZE ), OP_DROP , pubs [1 ], OP_CHECKSIG ])),
972
+ ("t25" , CScript ([random . randbytes (MAX_SCRIPT_ELEMENT_SIZE ), OP_DROP , pubs [1 ], OP_CHECKSIG ])),
974
973
# 26) Script that pushes a (MAX_SCRIPT_ELEMENT_SIZE+1)-bytes element
975
- ("t26" , CScript ([random_bytes (MAX_SCRIPT_ELEMENT_SIZE + 1 ), OP_DROP , pubs [1 ], OP_CHECKSIG ])),
974
+ ("t26" , CScript ([random . randbytes (MAX_SCRIPT_ELEMENT_SIZE + 1 ), OP_DROP , pubs [1 ], OP_CHECKSIG ])),
976
975
# 27) CHECKSIGADD that must fail because numeric argument number is >4 bytes
977
976
("t27" , CScript ([CScriptNum (OVERSIZE_NUMBER ), pubs [1 ], OP_CHECKSIGADD ])),
978
977
# 28) Pushes random CScriptNum value, checks OP_CHECKSIGADD result
@@ -1005,9 +1004,9 @@ def big_spend_inputs(ctx):
1005
1004
"tap" : tap ,
1006
1005
}
1007
1006
# Test that MAX_SCRIPT_ELEMENT_SIZE byte stack element inputs are valid, but not one more (and 80 bytes is standard but 81 is not).
1008
- add_spender (spenders , "tapscript/inputmaxlimit" , leaf = "t0" , ** common , standard = False , inputs = [getter ("sign" ), random_bytes (MAX_SCRIPT_ELEMENT_SIZE )], failure = {"inputs" : [getter ("sign" ), random_bytes (MAX_SCRIPT_ELEMENT_SIZE + 1 )]}, ** ERR_PUSH_LIMIT )
1009
- add_spender (spenders , "tapscript/input80limit" , leaf = "t0" , ** common , inputs = [getter ("sign" ), random_bytes (80 )])
1010
- add_spender (spenders , "tapscript/input81limit" , leaf = "t0" , ** common , standard = False , inputs = [getter ("sign" ), random_bytes (81 )])
1007
+ add_spender (spenders , "tapscript/inputmaxlimit" , leaf = "t0" , ** common , standard = False , inputs = [getter ("sign" ), random . randbytes (MAX_SCRIPT_ELEMENT_SIZE )], failure = {"inputs" : [getter ("sign" ), random . randbytes (MAX_SCRIPT_ELEMENT_SIZE + 1 )]}, ** ERR_PUSH_LIMIT )
1008
+ add_spender (spenders , "tapscript/input80limit" , leaf = "t0" , ** common , inputs = [getter ("sign" ), random . randbytes (80 )])
1009
+ add_spender (spenders , "tapscript/input81limit" , leaf = "t0" , ** common , standard = False , inputs = [getter ("sign" ), random . randbytes (81 )])
1011
1010
# Test that OP_CHECKMULTISIG and OP_CHECKMULTISIGVERIFY cause failure, but OP_CHECKSIG and OP_CHECKSIGVERIFY work.
1012
1011
add_spender (spenders , "tapscript/disabled_checkmultisig" , leaf = "t1" , ** common , ** SINGLE_SIG , failure = {"leaf" : "t3" }, ** ERR_TAPSCRIPT_CHECKMULTISIG )
1013
1012
add_spender (spenders , "tapscript/disabled_checkmultisigverify" , leaf = "t2" , ** common , ** SINGLE_SIG , failure = {"leaf" : "t4" }, ** ERR_TAPSCRIPT_CHECKMULTISIG )
@@ -1062,17 +1061,17 @@ def big_spend_inputs(ctx):
1062
1061
# n OP_CHECKSIGVERIFYs and 1 OP_CHECKSIGADD, but also one unexecuted OP_CHECKSIGVERIFY.
1063
1062
lambda n , pk : (CScript ([OP_DROP , pk , OP_0 , OP_IF , OP_2DUP , OP_CHECKSIGVERIFY , OP_ENDIF ] + [OP_2DUP , OP_CHECKSIGVERIFY ] * n + [OP_2 , OP_SWAP , OP_CHECKSIGADD , OP_3 , OP_EQUAL ]), n + 1 ),
1064
1063
# n OP_CHECKSIGVERIFYs and 1 OP_CHECKSIGADD, but also one unexecuted OP_CHECKSIG.
1065
- lambda n , pk : (CScript ([random_bytes (220 ), OP_2DROP , pk , OP_1 , OP_NOTIF , OP_2DUP , OP_CHECKSIG , OP_VERIFY , OP_ENDIF ] + [OP_2DUP , OP_CHECKSIGVERIFY ] * n + [OP_4 , OP_SWAP , OP_CHECKSIGADD , OP_5 , OP_EQUAL ]), n + 1 ),
1064
+ lambda n , pk : (CScript ([random . randbytes (220 ), OP_2DROP , pk , OP_1 , OP_NOTIF , OP_2DUP , OP_CHECKSIG , OP_VERIFY , OP_ENDIF ] + [OP_2DUP , OP_CHECKSIGVERIFY ] * n + [OP_4 , OP_SWAP , OP_CHECKSIGADD , OP_5 , OP_EQUAL ]), n + 1 ),
1066
1065
# n OP_CHECKSIGVERIFYs and 1 OP_CHECKSIGADD, but also one unexecuted OP_CHECKSIGADD.
1067
1066
lambda n , pk : (CScript ([OP_DROP , pk , OP_1 , OP_IF , OP_ELSE , OP_2DUP , OP_6 , OP_SWAP , OP_CHECKSIGADD , OP_7 , OP_EQUALVERIFY , OP_ENDIF ] + [OP_2DUP , OP_CHECKSIGVERIFY ] * n + [OP_8 , OP_SWAP , OP_CHECKSIGADD , OP_9 , OP_EQUAL ]), n + 1 ),
1068
1067
# n+1 OP_CHECKSIGs, but also one OP_CHECKSIG with an empty signature.
1069
1068
lambda n , pk : (CScript ([OP_DROP , OP_0 , pk , OP_CHECKSIG , OP_NOT , OP_VERIFY , pk ] + [OP_2DUP , OP_CHECKSIG , OP_VERIFY ] * n + [OP_CHECKSIG ]), n + 1 ),
1070
1069
# n OP_CHECKSIGADDs and 1 OP_CHECKSIG, but also an OP_CHECKSIGADD with an empty signature.
1071
1070
lambda n , pk : (CScript ([OP_DROP , OP_0 , OP_10 , pk , OP_CHECKSIGADD , OP_10 , OP_EQUALVERIFY , pk ] + [OP_2DUP , OP_16 , OP_SWAP , OP_CHECKSIGADD , b'\x11 ' , OP_EQUALVERIFY ] * n + [OP_CHECKSIG ]), n + 1 ),
1072
1071
]
1073
- for annex in [None , bytes ([ANNEX_TAG ]) + random_bytes (random .randrange (1000 ))]:
1072
+ for annex in [None , bytes ([ANNEX_TAG ]) + random . randbytes (random .randrange (1000 ))]:
1074
1073
for hashtype in [SIGHASH_DEFAULT , SIGHASH_ALL ]:
1075
- for pubkey in [pubs [1 ], random_bytes (random .choice ([x for x in range (2 , 81 ) if x != 32 ]))]:
1074
+ for pubkey in [pubs [1 ], random . randbytes (random .choice ([x for x in range (2 , 81 ) if x != 32 ]))]:
1076
1075
for fn_num , fn in enumerate (SIGOPS_RATIO_SCRIPTS ):
1077
1076
merkledepth = random .randrange (129 )
1078
1077
@@ -1109,7 +1108,7 @@ def predict_sigops_ratio(n, dummy_size):
1109
1108
scripts = [scripts , random .choice (PARTNER_MERKLE_FN )]
1110
1109
tap = taproot_construct (pubs [0 ], scripts )
1111
1110
standard = annex is None and dummylen <= 80 and len (pubkey ) == 32
1112
- add_spender (spenders , "tapscript/sigopsratio_%i" % fn_num , tap = tap , leaf = "s" , annex = annex , hashtype = hashtype , key = secs [1 ], inputs = [getter ("sign" ), random_bytes (dummylen )], standard = standard , failure = {"inputs" : [getter ("sign" ), random_bytes (dummylen - 1 )]}, ** ERR_SIGOPS_RATIO )
1111
+ add_spender (spenders , "tapscript/sigopsratio_%i" % fn_num , tap = tap , leaf = "s" , annex = annex , hashtype = hashtype , key = secs [1 ], inputs = [getter ("sign" ), random . randbytes (dummylen )], standard = standard , failure = {"inputs" : [getter ("sign" ), random . randbytes (dummylen - 1 )]}, ** ERR_SIGOPS_RATIO )
1113
1112
1114
1113
# Future leaf versions
1115
1114
for leafver in range (0 , 0x100 , 2 ):
@@ -1123,8 +1122,8 @@ def predict_sigops_ratio(n, dummy_size):
1123
1122
("return_unkver" , CScript ([OP_RETURN ]), leafver ),
1124
1123
("undecodable_c0" , CScript ([OP_PUSHDATA1 ])),
1125
1124
("undecodable_unkver" , CScript ([OP_PUSHDATA1 ]), leafver ),
1126
- ("bigpush_c0" , CScript ([random_bytes (MAX_SCRIPT_ELEMENT_SIZE + 1 ), OP_DROP ])),
1127
- ("bigpush_unkver" , CScript ([random_bytes (MAX_SCRIPT_ELEMENT_SIZE + 1 ), OP_DROP ]), leafver ),
1125
+ ("bigpush_c0" , CScript ([random . randbytes (MAX_SCRIPT_ELEMENT_SIZE + 1 ), OP_DROP ])),
1126
+ ("bigpush_unkver" , CScript ([random . randbytes (MAX_SCRIPT_ELEMENT_SIZE + 1 ), OP_DROP ]), leafver ),
1128
1127
("1001push_c0" , CScript ([OP_0 ] * 1001 )),
1129
1128
("1001push_unkver" , CScript ([OP_0 ] * 1001 ), leafver ),
1130
1129
]
@@ -1153,8 +1152,8 @@ def predict_sigops_ratio(n, dummy_size):
1153
1152
("undecodable_success" , CScript ([opcode , OP_PUSHDATA1 ])),
1154
1153
("undecodable_nop" , CScript ([OP_NOP , OP_PUSHDATA1 ])),
1155
1154
("undecodable_bypassed_success" , CScript ([OP_PUSHDATA1 , OP_2 , opcode ])),
1156
- ("bigpush_success" , CScript ([random_bytes (MAX_SCRIPT_ELEMENT_SIZE + 1 ), OP_DROP , opcode ])),
1157
- ("bigpush_nop" , CScript ([random_bytes (MAX_SCRIPT_ELEMENT_SIZE + 1 ), OP_DROP , OP_NOP ])),
1155
+ ("bigpush_success" , CScript ([random . randbytes (MAX_SCRIPT_ELEMENT_SIZE + 1 ), OP_DROP , opcode ])),
1156
+ ("bigpush_nop" , CScript ([random . randbytes (MAX_SCRIPT_ELEMENT_SIZE + 1 ), OP_DROP , OP_NOP ])),
1158
1157
("1001push_success" , CScript ([OP_0 ] * 1001 + [opcode ])),
1159
1158
("1001push_nop" , CScript ([OP_0 ] * 1001 + [OP_NOP ])),
1160
1159
]
0 commit comments