@@ -367,6 +367,22 @@ impl SecretKey {
367
367
let kp = self . keypair ( secp) ;
368
368
XOnlyPublicKey :: from_keypair ( & kp)
369
369
}
370
+
371
+ /// Constructor for unit testing.
372
+ #[ cfg( test) ]
373
+ #[ cfg( all( feature = "rand" , feature = "std" ) ) ]
374
+ pub fn test_random ( ) -> Self { Self :: new ( & mut rand:: rng ( ) ) }
375
+
376
+ /// Constructor for unit testing.
377
+ #[ cfg( test) ]
378
+ #[ cfg( not( all( feature = "rand" , feature = "std" ) ) ) ]
379
+ pub fn test_random ( ) -> Self {
380
+ loop {
381
+ if let Ok ( ret) = Self :: from_byte_array ( crate :: test_random_32_bytes ( ) ) {
382
+ return ret;
383
+ }
384
+ }
385
+ }
370
386
}
371
387
372
388
#[ cfg( feature = "serde" ) ]
@@ -1036,6 +1052,16 @@ impl Keypair {
1036
1052
/// [`zeroize`](https://docs.rs/zeroize) crate.
1037
1053
#[ inline]
1038
1054
pub fn non_secure_erase ( & mut self ) { self . 0 . non_secure_erase ( ) ; }
1055
+
1056
+ /// Constructor for unit testing.
1057
+ #[ cfg( test) ]
1058
+ pub fn test_random ( ) -> Self {
1059
+ let sk = SecretKey :: test_random ( ) ;
1060
+ crate :: with_global_context (
1061
+ |secp : & Secp256k1 < crate :: AllPreallocated > | Self :: from_secret_key ( secp, & sk) ,
1062
+ Some ( & sk. secret_bytes ( ) ) ,
1063
+ )
1064
+ }
1039
1065
}
1040
1066
1041
1067
impl fmt:: Debug for Keypair {
@@ -1733,11 +1759,8 @@ mod test {
1733
1759
}
1734
1760
1735
1761
#[ test]
1736
- #[ cfg( all( feature = "rand" , feature = "std" ) ) ]
1737
1762
fn keypair_slice_round_trip ( ) {
1738
- let s = Secp256k1 :: new ( ) ;
1739
-
1740
- let ( sk1, pk1) = s. generate_keypair ( & mut rand:: rng ( ) ) ;
1763
+ let ( sk1, pk1) = crate :: test_random_keypair ( ) ;
1741
1764
assert_eq ! ( SecretKey :: from_byte_array( sk1. secret_bytes( ) ) , Ok ( sk1) ) ;
1742
1765
assert_eq ! ( PublicKey :: from_slice( & pk1. serialize( ) [ ..] ) , Ok ( pk1) ) ;
1743
1766
assert_eq ! ( PublicKey :: from_slice( & pk1. serialize_uncompressed( ) [ ..] ) , Ok ( pk1) ) ;
@@ -2010,15 +2033,15 @@ mod test {
2010
2033
}
2011
2034
2012
2035
#[ test]
2013
- #[ cfg( all ( feature = "rand" , feature = " std") ) ]
2036
+ #[ cfg( feature = "std" ) ]
2014
2037
fn tweak_add_arbitrary_data ( ) {
2015
2038
let s = Secp256k1 :: new ( ) ;
2016
2039
2017
- let ( sk, pk) = s . generate_keypair ( & mut rand :: rng ( ) ) ;
2040
+ let ( sk, pk) = crate :: test_random_keypair ( ) ;
2018
2041
assert_eq ! ( PublicKey :: from_secret_key( & s, & sk) , pk) ; // Sanity check.
2019
2042
2020
2043
// TODO: This would be better tested with a _lot_ of different tweaks.
2021
- let tweak = Scalar :: random ( ) ;
2044
+ let tweak = Scalar :: test_random ( ) ;
2022
2045
2023
2046
let tweaked_sk = sk. add_tweak ( & tweak) . unwrap ( ) ;
2024
2047
assert_ne ! ( sk, tweaked_sk) ; // Make sure we did something.
@@ -2029,11 +2052,11 @@ mod test {
2029
2052
}
2030
2053
2031
2054
#[ test]
2032
- #[ cfg( all ( feature = "rand" , feature = " std") ) ]
2055
+ #[ cfg( feature = "std" ) ]
2033
2056
fn tweak_add_zero ( ) {
2034
2057
let s = Secp256k1 :: new ( ) ;
2035
2058
2036
- let ( sk, pk) = s . generate_keypair ( & mut rand :: rng ( ) ) ;
2059
+ let ( sk, pk) = crate :: test_random_keypair ( ) ;
2037
2060
2038
2061
let tweak = Scalar :: ZERO ;
2039
2062
@@ -2044,40 +2067,38 @@ mod test {
2044
2067
}
2045
2068
2046
2069
#[ test]
2047
- #[ cfg( all ( feature = "rand" , feature = " std") ) ]
2070
+ #[ cfg( feature = "std" ) ]
2048
2071
fn tweak_mul_arbitrary_data ( ) {
2049
2072
let s = Secp256k1 :: new ( ) ;
2050
2073
2051
- let ( sk, pk) = s . generate_keypair ( & mut rand :: rng ( ) ) ;
2074
+ let ( sk, pk) = crate :: test_random_keypair ( ) ;
2052
2075
assert_eq ! ( PublicKey :: from_secret_key( & s, & sk) , pk) ; // Sanity check.
2053
2076
2054
- // TODO: This would be better tested with a _lot_ of different tweaks.
2055
- let tweak = Scalar :: random ( ) ;
2056
-
2057
- let tweaked_sk = sk. mul_tweak ( & tweak) . unwrap ( ) ;
2058
- assert_ne ! ( sk, tweaked_sk) ; // Make sure we did something.
2059
- let tweaked_pk = pk. mul_tweak ( & s, & tweak) . unwrap ( ) ;
2060
- assert_ne ! ( pk, tweaked_pk) ;
2077
+ for _ in 0 ..10 {
2078
+ let tweak = Scalar :: test_random ( ) ;
2061
2079
2062
- assert_eq ! ( PublicKey :: from_secret_key( & s, & tweaked_sk) , tweaked_pk) ;
2080
+ let tweaked_sk = sk. mul_tweak ( & tweak) . unwrap ( ) ;
2081
+ assert_ne ! ( sk, tweaked_sk) ; // Make sure we did something.
2082
+ let tweaked_pk = pk. mul_tweak ( & s, & tweak) . unwrap ( ) ;
2083
+ assert_ne ! ( pk, tweaked_pk) ;
2084
+ assert_eq ! ( PublicKey :: from_secret_key( & s, & tweaked_sk) , tweaked_pk) ;
2085
+ }
2063
2086
}
2064
2087
2065
2088
#[ test]
2066
- #[ cfg( all( feature = "rand" , feature = "std" ) ) ]
2067
2089
fn tweak_mul_zero ( ) {
2068
- let s = Secp256k1 :: new ( ) ;
2069
- let ( sk, _) = s. generate_keypair ( & mut rand:: rng ( ) ) ;
2090
+ let ( sk, _) = crate :: test_random_keypair ( ) ;
2070
2091
2071
2092
let tweak = Scalar :: ZERO ;
2072
2093
assert ! ( sk. mul_tweak( & tweak) . is_err( ) )
2073
2094
}
2074
2095
2075
2096
#[ test]
2076
- #[ cfg( all ( feature = "rand" , feature = " std") ) ]
2097
+ #[ cfg( feature = "std" ) ]
2077
2098
fn test_negation ( ) {
2078
2099
let s = Secp256k1 :: new ( ) ;
2079
2100
2080
- let ( sk, pk) = s . generate_keypair ( & mut rand :: rng ( ) ) ;
2101
+ let ( sk, pk) = crate :: test_random_keypair ( ) ;
2081
2102
2082
2103
assert_eq ! ( PublicKey :: from_secret_key( & s, & sk) , pk) ; // Sanity check.
2083
2104
@@ -2095,7 +2116,7 @@ mod test {
2095
2116
}
2096
2117
2097
2118
#[ test]
2098
- #[ cfg( all ( feature = "rand" , feature = " std") ) ]
2119
+ #[ cfg( feature = "std" ) ]
2099
2120
fn pubkey_hash ( ) {
2100
2121
use std:: collections:: hash_map:: DefaultHasher ;
2101
2122
use std:: collections:: HashSet ;
@@ -2107,11 +2128,10 @@ mod test {
2107
2128
s. finish ( )
2108
2129
}
2109
2130
2110
- let s = Secp256k1 :: new ( ) ;
2111
2131
let mut set = HashSet :: new ( ) ;
2112
2132
const COUNT : usize = 1024 ;
2113
2133
for _ in 0 ..COUNT {
2114
- let ( _, pk) = s . generate_keypair ( & mut rand :: rng ( ) ) ;
2134
+ let ( _, pk) = crate :: test_random_keypair ( ) ;
2115
2135
let hash = hash ( & pk) ;
2116
2136
assert ! ( !set. contains( & hash) ) ;
2117
2137
set. insert ( hash) ;
@@ -2178,12 +2198,12 @@ mod test {
2178
2198
}
2179
2199
2180
2200
#[ test]
2181
- #[ cfg( all ( feature = "rand" , feature = " std") ) ]
2201
+ #[ cfg( feature = "std" ) ]
2182
2202
fn create_pubkey_combine ( ) {
2183
2203
let s = Secp256k1 :: new ( ) ;
2184
2204
2185
- let ( sk1, pk1) = s . generate_keypair ( & mut rand :: rng ( ) ) ;
2186
- let ( sk2, pk2) = s . generate_keypair ( & mut rand :: rng ( ) ) ;
2205
+ let ( sk1, pk1) = crate :: test_random_keypair ( ) ;
2206
+ let ( sk2, pk2) = crate :: test_random_keypair ( ) ;
2187
2207
2188
2208
let sum1 = pk1. combine ( & pk2) ;
2189
2209
assert ! ( sum1. is_ok( ) ) ;
@@ -2288,15 +2308,14 @@ mod test {
2288
2308
}
2289
2309
2290
2310
#[ test]
2291
- #[ cfg( all ( feature = "rand" , feature = " std") ) ]
2311
+ #[ cfg( feature = "std" ) ]
2292
2312
fn test_tweak_add_then_tweak_add_check ( ) {
2293
2313
let s = Secp256k1 :: new ( ) ;
2294
2314
2295
- // TODO: 10 times is arbitrary, we should test this a _lot_ of times.
2296
2315
for _ in 0 ..10 {
2297
- let tweak = Scalar :: random ( ) ;
2316
+ let tweak = Scalar :: test_random ( ) ;
2298
2317
2299
- let kp = Keypair :: new ( & s , & mut rand :: rng ( ) ) ;
2318
+ let kp = Keypair :: test_random ( ) ;
2300
2319
let ( xonly, _) = XOnlyPublicKey :: from_keypair ( & kp) ;
2301
2320
2302
2321
let tweaked_kp = kp. add_xonly_tweak ( & s, & tweak) . expect ( "keypair tweak add failed" ) ;
@@ -2548,10 +2567,8 @@ mod test {
2548
2567
}
2549
2568
2550
2569
#[ test]
2551
- #[ cfg( all( feature = "rand" , feature = "std" ) ) ]
2552
2570
fn test_keypair_from_str ( ) {
2553
- let ctx = crate :: Secp256k1 :: new ( ) ;
2554
- let keypair = Keypair :: new ( & ctx, & mut rand:: rng ( ) ) ;
2571
+ let keypair = Keypair :: test_random ( ) ;
2555
2572
let mut buf = [ 0_u8 ; constants:: SECRET_KEY_SIZE * 2 ] ; // Holds hex digits.
2556
2573
let s = to_hex ( & keypair. secret_key ( ) . secret_bytes ( ) , & mut buf) . unwrap ( ) ;
2557
2574
let parsed_key = Keypair :: from_str ( s) . unwrap ( ) ;
0 commit comments