@@ -44,10 +44,11 @@ pub struct SigningKey {
44
44
45
45
impl SigningKey {
46
46
/// Construct a new private key from the public key and private component
47
- pub fn from_components (
48
- verifying_key : VerifyingKey ,
49
- x : NonZero < BoxedUint > ,
50
- ) -> signature:: Result < Self > {
47
+ pub fn from_components ( verifying_key : VerifyingKey , x : BoxedUint ) -> signature:: Result < Self > {
48
+ let x = NonZero :: new ( x)
49
+ . into_option ( )
50
+ . ok_or_else ( signature:: Error :: new) ?;
51
+
51
52
if x > * verifying_key. components ( ) . q ( ) {
52
53
return Err ( signature:: Error :: new ( ) ) ;
53
54
}
@@ -116,26 +117,19 @@ impl SigningKey {
116
117
debug_assert_eq ! ( key_size. l_aligned( ) , r. bits_precision( ) ) ;
117
118
118
119
let r_short = r. clone ( ) . resize ( key_size. n_aligned ( ) ) ;
119
- let r_short = NonZero :: new ( r_short)
120
- . expect ( "[bug] invalid value of k used here, the secret number computed was invalid" ) ;
121
- let r = NonZero :: new ( r)
122
- . expect ( "[bug] invalid value of k used here, the secret number computed was invalid" ) ;
123
-
124
120
let n = q. bits ( ) / 8 ;
125
121
let block_size = hash. len ( ) ; // Hash function output size
126
122
127
123
let z_len = min ( n as usize , block_size) ;
128
124
let z = BoxedUint :: from_be_slice ( & hash[ ..z_len] , z_len as u32 * 8 )
129
125
. expect ( "invariant violation" ) ;
130
126
131
- let s = inv_k. mul_mod ( & ( z + & * * x * & * r) , & q. resize ( key_size. l_aligned ( ) ) ) ;
127
+ let s = inv_k. mul_mod ( & ( z + & * * x * & r) , & q. resize ( key_size. l_aligned ( ) ) ) ;
132
128
let s = s. resize ( key_size. n_aligned ( ) ) ;
133
- let s = NonZero :: new ( s)
134
- . expect ( "[bug] invalid value of k used here, the secret number computed was invalid" ) ;
135
129
136
130
debug_assert_eq ! ( key_size. n_aligned( ) , r_short. bits_precision( ) ) ;
137
131
debug_assert_eq ! ( key_size. n_aligned( ) , s. bits_precision( ) ) ;
138
- let signature = Signature :: from_components ( r_short, s) ;
132
+ let signature = Signature :: from_components ( r_short, s) . ok_or_else ( signature :: Error :: new ) ? ;
139
133
140
134
if signature. r ( ) < q && signature. s ( ) < q {
141
135
Ok ( signature)
@@ -260,21 +254,19 @@ impl<'a> TryFrom<PrivateKeyInfoRef<'a>> for SigningKey {
260
254
261
255
let y = if let Some ( y_bytes) = value. public_key . as_ref ( ) . and_then ( |bs| bs. as_bytes ( ) ) {
262
256
let y = UintRef :: from_der ( y_bytes) ?;
263
- let y = BoxedUint :: from_be_slice ( y. as_bytes ( ) , precision)
264
- . map_err ( |_| pkcs8:: Error :: KeyMalformed ) ?;
265
- NonZero :: new ( y)
266
- . into_option ( )
267
- . ok_or ( pkcs8:: Error :: KeyMalformed ) ?
257
+ BoxedUint :: from_be_slice ( y. as_bytes ( ) , precision)
258
+ . map_err ( |_| pkcs8:: Error :: KeyMalformed ) ?
268
259
} else {
269
260
crate :: generate:: public_component ( & components, & x)
270
261
. into_option ( )
271
262
. ok_or ( pkcs8:: Error :: KeyMalformed ) ?
263
+ . get ( )
272
264
} ;
273
265
274
266
let verifying_key =
275
267
VerifyingKey :: from_components ( components, y) . map_err ( |_| pkcs8:: Error :: KeyMalformed ) ?;
276
268
277
- SigningKey :: from_components ( verifying_key, x) . map_err ( |_| pkcs8:: Error :: KeyMalformed )
269
+ SigningKey :: from_components ( verifying_key, x. get ( ) ) . map_err ( |_| pkcs8:: Error :: KeyMalformed )
278
270
}
279
271
}
280
272
0 commit comments