9595from test_framework .util import (
9696 assert_raises_rpc_error ,
9797 assert_equal ,
98- random_bytes ,
9998)
10099from test_framework .wallet_util import generate_keypair
101100from test_framework .key import (
@@ -694,7 +693,7 @@ def spenders_taproot_active():
694693
695694 # Generate an invalid public key
696695 while True :
697- invalid_pub = random_bytes (32 )
696+ invalid_pub = random . randbytes (32 )
698697 if not secp256k1 .GE .is_valid_x (int .from_bytes (invalid_pub , 'big' )):
699698 break
700699
@@ -710,7 +709,7 @@ def spenders_taproot_active():
710709 # == Tests for signature hashing ==
711710
712711 # 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 ))]:
714713 # Non-empty annex is non-standard
715714 no_annex = annex is None
716715
@@ -739,7 +738,7 @@ def spenders_taproot_active():
739738 scripts = [
740739 ("pk_codesep" , CScript (random_checksig_style (pubs [1 ]) + bytes ([OP_CODESEPARATOR ]))), # codesep after checksig
741740 ("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
743742 # Note that the first data push in the "branched_codesep" script has the purpose of
744743 # randomizing the sighash, both by varying script size and content. In order to
745744 # avoid MINIMALDATA script verification errors caused by not-minimal-encoded data
@@ -792,8 +791,8 @@ def spenders_taproot_active():
792791 add_spender (spenders , "siglen/empty_csv" , tap = tap , key = secs [2 ], leaf = "csv" , hashtype = hashtype , ** SINGLE_SIG , failure = {"sign" : b"" }, ** ERR_CHECKSIGVERIFY )
793792 add_spender (spenders , "siglen/empty_cs" , tap = tap , key = secs [2 ], leaf = "cs_pos" , hashtype = hashtype , ** SINGLE_SIG , failure = {"sign" : b"" }, ** ERR_NO_SUCCESS )
794793 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 )
797796 # Appending a zero byte to signatures invalidates them
798797 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 ))
799798 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):
874873 # Test that empty witnesses are invalid.
875874 add_spender (spenders , "spendpath/emptywit" , tap = tap , leaf = "128deep" , ** SINGLE_SIG , key = secs [0 ], failure = {"witness" : []}, ** ERR_EMPTY_WITNESS )
876875 # 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 )
878877 # Test that truncating the control block invalidates it.
879878 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 )
880879
881880 scripts = [("s" , CScript ([pubs [0 ], OP_CHECKSIG ]))]
882881 tap = taproot_construct (pubs [1 ], scripts )
883882 # 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 )
885884 # Test that truncating the control block invalidates it.
886885 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 )
887886 # Test that truncating the control block to 1 byte ("-1 Merkle length") invalidates it
@@ -970,9 +969,9 @@ def big_spend_inputs(ctx):
970969 # 24) Script that expects an input stack of 1001 elements
971970 ("t24" , CScript ([OP_DROP ] * 1000 + [pubs [1 ], OP_CHECKSIG ])),
972971 # 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 ])),
974973 # 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 ])),
976975 # 27) CHECKSIGADD that must fail because numeric argument number is >4 bytes
977976 ("t27" , CScript ([CScriptNum (OVERSIZE_NUMBER ), pubs [1 ], OP_CHECKSIGADD ])),
978977 # 28) Pushes random CScriptNum value, checks OP_CHECKSIGADD result
@@ -1005,9 +1004,9 @@ def big_spend_inputs(ctx):
10051004 "tap" : tap ,
10061005 }
10071006 # 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 )])
10111010 # Test that OP_CHECKMULTISIG and OP_CHECKMULTISIGVERIFY cause failure, but OP_CHECKSIG and OP_CHECKSIGVERIFY work.
10121011 add_spender (spenders , "tapscript/disabled_checkmultisig" , leaf = "t1" , ** common , ** SINGLE_SIG , failure = {"leaf" : "t3" }, ** ERR_TAPSCRIPT_CHECKMULTISIG )
10131012 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):
10621061 # n OP_CHECKSIGVERIFYs and 1 OP_CHECKSIGADD, but also one unexecuted OP_CHECKSIGVERIFY.
10631062 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 ),
10641063 # 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 ),
10661065 # n OP_CHECKSIGVERIFYs and 1 OP_CHECKSIGADD, but also one unexecuted OP_CHECKSIGADD.
10671066 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 ),
10681067 # n+1 OP_CHECKSIGs, but also one OP_CHECKSIG with an empty signature.
10691068 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 ),
10701069 # n OP_CHECKSIGADDs and 1 OP_CHECKSIG, but also an OP_CHECKSIGADD with an empty signature.
10711070 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 ),
10721071 ]
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 ))]:
10741073 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 ]))]:
10761075 for fn_num , fn in enumerate (SIGOPS_RATIO_SCRIPTS ):
10771076 merkledepth = random .randrange (129 )
10781077
@@ -1109,7 +1108,7 @@ def predict_sigops_ratio(n, dummy_size):
11091108 scripts = [scripts , random .choice (PARTNER_MERKLE_FN )]
11101109 tap = taproot_construct (pubs [0 ], scripts )
11111110 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 )
11131112
11141113 # Future leaf versions
11151114 for leafver in range (0 , 0x100 , 2 ):
@@ -1123,8 +1122,8 @@ def predict_sigops_ratio(n, dummy_size):
11231122 ("return_unkver" , CScript ([OP_RETURN ]), leafver ),
11241123 ("undecodable_c0" , CScript ([OP_PUSHDATA1 ])),
11251124 ("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 ),
11281127 ("1001push_c0" , CScript ([OP_0 ] * 1001 )),
11291128 ("1001push_unkver" , CScript ([OP_0 ] * 1001 ), leafver ),
11301129 ]
@@ -1153,8 +1152,8 @@ def predict_sigops_ratio(n, dummy_size):
11531152 ("undecodable_success" , CScript ([opcode , OP_PUSHDATA1 ])),
11541153 ("undecodable_nop" , CScript ([OP_NOP , OP_PUSHDATA1 ])),
11551154 ("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 ])),
11581157 ("1001push_success" , CScript ([OP_0 ] * 1001 + [opcode ])),
11591158 ("1001push_nop" , CScript ([OP_0 ] * 1001 + [OP_NOP ])),
11601159 ]
0 commit comments