@@ -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,125 @@ impl fmt::UpperHex for Mpint {
205208}
206209
207210#[ cfg( feature = "bigint" ) ]
208- impl TryFrom < bigint :: BigUint > for Mpint {
211+ impl TryFrom < NonZeroUint > for Mpint {
209212 type Error = Error ;
210213
211- fn try_from ( uint : bigint :: BigUint ) -> Result < Mpint > {
214+ fn try_from ( uint : NonZeroUint ) -> Result < Mpint > {
212215 Mpint :: try_from ( & uint)
213216 }
214217}
215218
216219#[ cfg( feature = "bigint" ) ]
217- impl TryFrom < & bigint:: BigUint > for Mpint {
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 {
218230 type Error = Error ;
219231
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 ( ) )
232+ fn try_from ( uint : OddUint ) -> Result < Mpint > {
233+ Mpint :: try_from ( & uint)
223234 }
224235}
225236
226237#[ cfg( feature = "bigint" ) ]
227- impl TryFrom < Mpint > for bigint :: BigUint {
238+ impl TryFrom < & OddUint > for Mpint {
228239 type Error = Error ;
229240
230- fn try_from ( mpint : Mpint ) -> Result < bigint :: BigUint > {
231- bigint :: BigUint :: try_from ( & mpint )
241+ fn try_from ( uint : & OddUint ) -> Result < Mpint > {
242+ Self :: try_from ( uint . as_ref ( ) )
232243 }
233244}
234245
235246#[ cfg( feature = "bigint" ) ]
236- impl TryFrom < & Mpint > for bigint :: BigUint {
247+ impl TryFrom < Uint > for Mpint {
237248 type Error = Error ;
238249
239- fn try_from ( mpint : & Mpint ) -> Result < bigint:: BigUint > {
240- mpint
241- . as_positive_bytes ( )
242- . map ( bigint:: BigUint :: from_bytes_be)
250+ fn try_from ( uint : Uint ) -> Result < Mpint > {
251+ Mpint :: try_from ( & uint)
252+ }
253+ }
254+
255+ #[ cfg( feature = "bigint" ) ]
256+ impl TryFrom < & Uint > for Mpint {
257+ type Error = Error ;
258+
259+ fn try_from ( uint : & Uint ) -> Result < Mpint > {
260+ let bytes = Zeroizing :: new ( uint. to_be_bytes ( ) ) ;
261+ Mpint :: from_positive_bytes ( & bytes)
262+ }
263+ }
264+
265+ #[ cfg( feature = "bigint" ) ]
266+ impl TryFrom < Mpint > for NonZeroUint {
267+ type Error = Error ;
268+
269+ fn try_from ( mpint : Mpint ) -> Result < NonZeroUint > {
270+ NonZeroUint :: try_from ( & mpint)
271+ }
272+ }
273+
274+ #[ cfg( feature = "bigint" ) ]
275+ impl TryFrom < & Mpint > for NonZeroUint {
276+ type Error = Error ;
277+
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
321+ . len ( )
322+ . checked_mul ( 8 )
323+ . and_then ( |n| u32:: try_from ( n) . ok ( ) )
324+ . ok_or ( Error :: MpintEncoding ) ?;
325+
326+ Ok ( Uint :: from_be_slice ( bytes, bits_precision) ?)
327+ }
328+ }
329+
247330#[ cfg( test) ]
248331mod tests {
249332 use super :: Mpint ;
0 commit comments