File tree 3 files changed +25
-13
lines changed
3 files changed +25
-13
lines changed Original file line number Diff line number Diff line change @@ -99,17 +99,6 @@ impl Field for Scalar {
99
99
const ZERO : Self = Self ( ScalarPrimitive :: ZERO ) ;
100
100
const ONE : Self = Self ( ScalarPrimitive :: ONE ) ;
101
101
102
- fn random < R : RngCore + ?Sized > ( rng : & mut R ) -> Self {
103
- let mut bytes = FieldBytes :: default ( ) ;
104
-
105
- loop {
106
- rng. fill_bytes ( & mut bytes) ;
107
- if let Some ( scalar) = Self :: from_repr ( bytes) . into ( ) {
108
- return scalar;
109
- }
110
- }
111
- }
112
-
113
102
fn try_from_rng < R : TryRngCore + ?Sized > ( rng : & mut R ) -> core:: result:: Result < Self , R :: Error > {
114
103
let mut bytes = FieldBytes :: default ( ) ;
115
104
Original file line number Diff line number Diff line change @@ -34,7 +34,7 @@ use core::{borrow::Borrow, fmt};
34
34
use digest:: { Digest , crypto_common:: BlockSizeUser } ;
35
35
use group:: Curve as _;
36
36
use hkdf:: { Hkdf , hmac:: SimpleHmac } ;
37
- use rand_core:: CryptoRng ;
37
+ use rand_core:: { CryptoRng , TryCryptoRng } ;
38
38
use zeroize:: { Zeroize , ZeroizeOnDrop } ;
39
39
40
40
/// Low-level Elliptic Curve Diffie-Hellman (ECDH) function.
@@ -114,6 +114,13 @@ where
114
114
}
115
115
}
116
116
117
+ /// Generate a cryptographically random [`EphemeralSecret`].
118
+ pub fn try_from_rng < R : TryCryptoRng + ?Sized > ( rng : & mut R ) -> Result < Self , R :: Error > {
119
+ Ok ( Self {
120
+ scalar : NonZeroScalar :: try_from_rng ( rng) ?,
121
+ } )
122
+ }
123
+
117
124
/// Get the public key associated with this ephemeral secret.
118
125
///
119
126
/// The `compress` flag enables point compression.
Original file line number Diff line number Diff line change @@ -15,7 +15,10 @@ use subtle::{Choice, ConstantTimeEq};
15
15
use zeroize:: { Zeroize , ZeroizeOnDrop , Zeroizing } ;
16
16
17
17
#[ cfg( feature = "arithmetic" ) ]
18
- use crate :: { CurveArithmetic , NonZeroScalar , PublicKey , rand_core:: CryptoRng } ;
18
+ use crate :: {
19
+ CurveArithmetic , NonZeroScalar , PublicKey ,
20
+ rand_core:: { CryptoRng , TryCryptoRng } ,
21
+ } ;
19
22
20
23
#[ cfg( feature = "jwk" ) ]
21
24
use crate :: jwk:: { JwkEcKey , JwkParameters } ;
@@ -100,6 +103,19 @@ where
100
103
}
101
104
}
102
105
106
+ /// Generate a random [`SecretKey`].
107
+ #[ cfg( feature = "arithmetic" ) ]
108
+ pub fn try_from_rng < R : TryCryptoRng + ?Sized > (
109
+ rng : & mut R ,
110
+ ) -> core:: result:: Result < Self , R :: Error >
111
+ where
112
+ C : CurveArithmetic ,
113
+ {
114
+ Ok ( Self {
115
+ inner : NonZeroScalar :: < C > :: try_from_rng ( rng) ?. into ( ) ,
116
+ } )
117
+ }
118
+
103
119
/// Create a new secret key from a scalar value.
104
120
pub fn new ( scalar : ScalarPrimitive < C > ) -> Self {
105
121
Self { inner : scalar }
You can’t perform that action at this time.
0 commit comments