@@ -4,6 +4,9 @@ use crate::{CheckedSum, Decode, Encode, Error, Reader, Result, Writer};
44use alloc:: { boxed:: Box , vec:: Vec } ;
55use core:: fmt;
66
7+ #[ cfg( feature = "bigint" ) ]
8+ use crate :: { NonZeroUint , OddUint , Uint } ;
9+
710#[ cfg( feature = "subtle" ) ]
811use subtle:: { Choice , ConstantTimeEq } ;
912
@@ -205,45 +208,120 @@ impl fmt::UpperHex for Mpint {
205208}
206209
207210#[ cfg( feature = "bigint" ) ]
208- impl TryFrom < bigint:: BigUint > for Mpint {
211+ impl TryFrom < NonZeroUint > for Mpint {
212+ type Error = Error ;
213+
214+ fn try_from ( uint : NonZeroUint ) -> Result < Mpint > {
215+ Mpint :: try_from ( & uint)
216+ }
217+ }
218+
219+ #[ cfg( feature = "bigint" ) ]
220+ impl TryFrom < & NonZeroUint > for Mpint {
221+ type Error = Error ;
222+
223+ fn try_from ( uint : & NonZeroUint ) -> Result < Mpint > {
224+ Self :: try_from ( uint. as_ref ( ) )
225+ }
226+ }
227+
228+ #[ cfg( feature = "bigint" ) ]
229+ impl TryFrom < OddUint > for Mpint {
230+ type Error = Error ;
231+
232+ fn try_from ( uint : OddUint ) -> Result < Mpint > {
233+ Mpint :: try_from ( & uint)
234+ }
235+ }
236+
237+ #[ cfg( feature = "bigint" ) ]
238+ impl TryFrom < & OddUint > for Mpint {
239+ type Error = Error ;
240+
241+ fn try_from ( uint : & OddUint ) -> Result < Mpint > {
242+ Self :: try_from ( uint. as_ref ( ) )
243+ }
244+ }
245+
246+ #[ cfg( feature = "bigint" ) ]
247+ impl TryFrom < Uint > for Mpint {
209248 type Error = Error ;
210249
211- fn try_from ( uint : bigint :: BigUint ) -> Result < Mpint > {
250+ fn try_from ( uint : Uint ) -> Result < Mpint > {
212251 Mpint :: try_from ( & uint)
213252 }
214253}
215254
216255#[ cfg( feature = "bigint" ) ]
217- impl TryFrom < & bigint :: BigUint > for Mpint {
256+ impl TryFrom < & Uint > for Mpint {
218257 type Error = Error ;
219258
220- fn try_from ( uint : & bigint :: BigUint ) -> Result < Mpint > {
221- let bytes = Zeroizing :: new ( uint. to_bytes_be ( ) ) ;
222- Mpint :: from_positive_bytes ( bytes. as_slice ( ) )
259+ fn try_from ( uint : & Uint ) -> Result < Mpint > {
260+ let bytes = Zeroizing :: new ( uint. to_be_bytes ( ) ) ;
261+ Mpint :: from_positive_bytes ( & bytes)
223262 }
224263}
225264
226265#[ cfg( feature = "bigint" ) ]
227- impl TryFrom < Mpint > for bigint :: BigUint {
266+ impl TryFrom < Mpint > for NonZeroUint {
228267 type Error = Error ;
229268
230- fn try_from ( mpint : Mpint ) -> Result < bigint :: BigUint > {
231- bigint :: BigUint :: try_from ( & mpint)
269+ fn try_from ( mpint : Mpint ) -> Result < NonZeroUint > {
270+ NonZeroUint :: try_from ( & mpint)
232271 }
233272}
234273
235274#[ cfg( feature = "bigint" ) ]
236- impl TryFrom < & Mpint > for bigint :: BigUint {
275+ impl TryFrom < & Mpint > for NonZeroUint {
237276 type Error = Error ;
238277
239- fn try_from ( mpint : & Mpint ) -> Result < bigint :: BigUint > {
240- mpint
241- . as_positive_bytes ( )
242- . map ( bigint :: BigUint :: from_bytes_be )
278+ fn try_from ( mpint : & Mpint ) -> Result < NonZeroUint > {
279+ let uint = Uint :: try_from ( mpint) ? ;
280+ NonZeroUint :: new ( uint )
281+ . into_option ( )
243282 . ok_or ( Error :: MpintEncoding )
244283 }
245284}
246285
286+ #[ cfg( feature = "bigint" ) ]
287+ impl TryFrom < Mpint > for OddUint {
288+ type Error = Error ;
289+
290+ fn try_from ( mpint : Mpint ) -> Result < OddUint > {
291+ OddUint :: try_from ( & mpint)
292+ }
293+ }
294+
295+ #[ cfg( feature = "bigint" ) ]
296+ impl TryFrom < & Mpint > for OddUint {
297+ type Error = Error ;
298+
299+ fn try_from ( mpint : & Mpint ) -> Result < OddUint > {
300+ let uint = Uint :: try_from ( mpint) ?;
301+ OddUint :: new ( uint) . into_option ( ) . ok_or ( Error :: MpintEncoding )
302+ }
303+ }
304+
305+ #[ cfg( feature = "bigint" ) ]
306+ impl TryFrom < Mpint > for Uint {
307+ type Error = Error ;
308+
309+ fn try_from ( mpint : Mpint ) -> Result < Uint > {
310+ Uint :: try_from ( & mpint)
311+ }
312+ }
313+
314+ #[ cfg( feature = "bigint" ) ]
315+ impl TryFrom < & Mpint > for Uint {
316+ type Error = Error ;
317+
318+ fn try_from ( mpint : & Mpint ) -> Result < Uint > {
319+ let bytes = mpint. as_positive_bytes ( ) . ok_or ( Error :: MpintEncoding ) ?;
320+ let bits_precision = bytes. len ( ) as u32 * 8 ;
321+ Ok ( Uint :: from_be_slice ( bytes, bits_precision) ?)
322+ }
323+ }
324+
247325#[ cfg( test) ]
248326mod tests {
249327 use super :: Mpint ;
0 commit comments