11//! Multiple precision integer
22
3- use crate :: { Error , Result } ;
3+ use crate :: { CheckedSum , Decode , Encode , Error , Reader , Result , Writer } ;
44use alloc:: { boxed:: Box , vec:: Vec } ;
55use core:: fmt;
6- use encoding:: { CheckedSum , Decode , Encode , Reader , Writer } ;
6+
7+ #[ cfg( feature = "subtle" ) ]
78use subtle:: { Choice , ConstantTimeEq } ;
8- use zeroize:: Zeroize ;
99
10- #[ cfg( any( feature = "dsa" , feature = "rsa" ) ) ]
10+ #[ cfg( any( feature = "bigint" , feature = "zeroize" ) ) ]
11+ use zeroize:: Zeroize ;
12+ #[ cfg( feature = "bigint" ) ]
1113use zeroize:: Zeroizing ;
1214
1315/// Multiple precision integer, a.k.a. "mpint".
@@ -38,7 +40,7 @@ use zeroize::Zeroizing;
3840/// | 80 | `00 00 00 02 00 80`
3941/// |-1234 | `00 00 00 02 ed cc`
4042/// | -deadbeef | `00 00 00 05 ff 21 52 41 11`
41- #[ derive( Clone , PartialOrd , Ord ) ]
43+ #[ derive( Clone , Ord , PartialOrd ) ] // TODO(tarcieri): constant time `Ord`?
4244pub struct Mpint {
4345 /// Inner big endian-serialized integer value
4446 inner : Box < [ u8 ] > ,
@@ -109,14 +111,17 @@ impl AsRef<[u8]> for Mpint {
109111 }
110112}
111113
114+ #[ cfg( feature = "subtle" ) ]
112115impl ConstantTimeEq for Mpint {
113116 fn ct_eq ( & self , other : & Self ) -> Choice {
114117 self . as_ref ( ) . ct_eq ( other. as_ref ( ) )
115118 }
116119}
117120
121+ #[ cfg( feature = "subtle" ) ]
118122impl Eq for Mpint { }
119123
124+ #[ cfg( feature = "subtle" ) ]
120125impl PartialEq for Mpint {
121126 fn eq ( & self , other : & Self ) -> bool {
122127 self . ct_eq ( other) . into ( )
@@ -132,11 +137,11 @@ impl Decode for Mpint {
132137}
133138
134139impl Encode for Mpint {
135- fn encoded_len ( & self ) -> encoding :: Result < usize > {
140+ fn encoded_len ( & self ) -> Result < usize > {
136141 [ 4 , self . as_bytes ( ) . len ( ) ] . checked_sum ( )
137142 }
138143
139- fn encode ( & self , writer : & mut impl Writer ) -> encoding :: Result < ( ) > {
144+ fn encode ( & self , writer : & mut impl Writer ) -> Result < ( ) > {
140145 self . as_bytes ( ) . encode ( writer) ?;
141146 Ok ( ( ) )
142147 }
@@ -156,14 +161,15 @@ impl TryFrom<Box<[u8]>> for Mpint {
156161 fn try_from ( bytes : Box < [ u8 ] > ) -> Result < Self > {
157162 match & * bytes {
158163 // Unnecessary leading 0
159- [ 0x00 ] => Err ( Error :: FormatEncoding ) ,
164+ [ 0x00 ] => Err ( Error :: MpintEncoding ) ,
160165 // Unnecessary leading 0
161- [ 0x00 , n, ..] if * n < 0x80 => Err ( Error :: FormatEncoding ) ,
166+ [ 0x00 , n, ..] if * n < 0x80 => Err ( Error :: MpintEncoding ) ,
162167 _ => Ok ( Self { inner : bytes } ) ,
163168 }
164169 }
165170}
166171
172+ #[ cfg( feature = "zeroize" ) ]
167173impl Zeroize for Mpint {
168174 fn zeroize ( & mut self ) {
169175 self . inner . zeroize ( ) ;
@@ -200,7 +206,7 @@ impl fmt::UpperHex for Mpint {
200206 }
201207}
202208
203- #[ cfg( any ( feature = "dsa" , feature = "rsa" ) ) ]
209+ #[ cfg( feature = "bigint" ) ]
204210impl TryFrom < bigint:: BigUint > for Mpint {
205211 type Error = Error ;
206212
@@ -209,7 +215,7 @@ impl TryFrom<bigint::BigUint> for Mpint {
209215 }
210216}
211217
212- #[ cfg( any ( feature = "dsa" , feature = "rsa" ) ) ]
218+ #[ cfg( feature = "bigint" ) ]
213219impl TryFrom < & bigint:: BigUint > for Mpint {
214220 type Error = Error ;
215221
@@ -219,7 +225,7 @@ impl TryFrom<&bigint::BigUint> for Mpint {
219225 }
220226}
221227
222- #[ cfg( any ( feature = "dsa" , feature = "rsa" ) ) ]
228+ #[ cfg( feature = "bigint" ) ]
223229impl TryFrom < Mpint > for bigint:: BigUint {
224230 type Error = Error ;
225231
@@ -228,15 +234,15 @@ impl TryFrom<Mpint> for bigint::BigUint {
228234 }
229235}
230236
231- #[ cfg( any ( feature = "dsa" , feature = "rsa" ) ) ]
237+ #[ cfg( feature = "bigint" ) ]
232238impl TryFrom < & Mpint > for bigint:: BigUint {
233239 type Error = Error ;
234240
235241 fn try_from ( mpint : & Mpint ) -> Result < bigint:: BigUint > {
236242 mpint
237243 . as_positive_bytes ( )
238244 . map ( bigint:: BigUint :: from_bytes_be)
239- . ok_or ( Error :: Crypto )
245+ . ok_or ( Error :: MpintEncoding )
240246 }
241247}
242248
0 commit comments