Skip to main content

Mpint

Struct Mpint 

Source
pub struct Mpint { /* private fields */ }
Available on crate feature alloc only.
Expand description

Multiple precision integer, a.k.a. mpint.

This type supports the non-negative subset of RFC4251 § 5, matching OpenSSH. Values are limited to 16384-bits. Negative SSH encodings are rejected.

Self::from_bytes and Self::as_bytes use an unsigned big endian magnitude, without a length prefix or sign padding. Leading zeros are stripped and zero is represented by an empty slice.

Decode accepts redundant leading zeros like OpenSSH, but limits the input to Self::MAX_SIZE bytes plus one leading zero byte before stripping zeros. Encode always emits the canonical SSH encoding, including a leading zero when the most significant bit is set and an empty string for zero.

With the ctutils feature, constant-time order and equality operations on equal-length magnitudes are supported. Lengths are not treated as secret. Byte conversion, decoding, and encoding are variable-time.

§Examples

value (hex)SSH encoding (hex)
000 00 00 00
9a378f9b2e332a700 00 00 08 09 a3 78 f9 b2 e3 32 a7
8000 00 00 02 00 80

Implementations§

Source§

impl Mpint

Source

pub const MAX_SIZE: usize = MpintRef::MAX_SIZE

Maximum magnitude size in bytes, excluding SSH sign padding.

This 16384-bit limit is enforced by every constructor and decoder.

Source

pub fn from_bytes(bytes: &[u8]) -> Result<Self>

Create an integer from its unsigned big endian magnitude.

Leading zeros are stripped. The most significant bit is part of the magnitude, not a sign bit. This method does not expect an SSH length prefix or sign padding.

§Errors

Returns Error::Length if the magnitude exceeds Self::MAX_SIZE. Returns Error::Alloc if reserving capacity fails.

Source

pub fn as_bytes(&self) -> &[u8] ⓘ

Get the unsigned big endian magnitude, without leading zeros or SSH sign padding.

Returns an empty slice for zero.

Source

pub fn is_zero(&self) -> bool

Is this integer zero?

Trait Implementations§

Source§

impl AsRef<[u8]> for Mpint

Source§

fn as_ref(&self) -> &[u8] ⓘ

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl Clone for Mpint

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl CtEq for Mpint

Available on crate feature ctutils only.
Source§

fn ct_eq(&self, other: &Self) -> Choice

Determine if self is equal to other in constant-time.
Source§

fn ct_ne(&self, other: &Rhs) -> Choice

Determine if self is NOT equal to other in constant-time.
Source§

impl Debug for Mpint

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Decode for Mpint

Source§

type Error = Error

Type returned in the event of a decoding error.
Source§

fn decode(reader: &mut Decoder<impl Reader>) -> Result<Self>

Attempt to decode a value of this type using the provided Decoder. Read more
Source§

impl Display for Mpint

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Encode for Mpint

Source§

fn encoded_len(&self) -> Result<usize>

Get the length of this type encoded in bytes, prior to Base64 encoding. Read more
Source§

fn encode(&self, writer: &mut impl Writer) -> Result<()>

Encode this value using the provided Writer. Read more
Source§

fn encoded_len_prefixed(&self) -> Result<usize, Error>

Return the length of this type after encoding when prepended with a uint32 length prefix. Read more
Source§

fn encode_prefixed(&self, writer: &mut impl Writer) -> Result<(), Error>

Encode this value, first prepending a uint32 length prefix set to Encode::encoded_len. Read more
Source§

fn encode_vec(&self) -> Result<Vec<u8>, Error>

Encode this value, returning a Vec<u8> containing the encoded message. Read more
Source§

fn encode_bytes(&self) -> Result<BytesMut, Error>

Available on crate feature bytes only.
Encode this value, returning a BytesMut containing the encoded message. Read more
Source§

impl Eq for Mpint

Available on crate feature ctutils only.
Source§

impl<'a> From<&'a Mpint> for MpintRef<'a>

Source§

fn from(mpint: &'a Mpint) -> Self

Converts to this type from the input type.
Source§

impl From<&Mpint> for Uint

Available on crate feature bigint only.
Source§

fn from(mpint: &Mpint) -> Uint

Converts to this type from the input type.
Source§

impl From<Mpint> for Uint

Available on crate feature bigint only.
Source§

fn from(mpint: Mpint) -> Uint

Converts to this type from the input type.
Source§

impl LowerHex for Mpint

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Ord for Mpint

Available on crate feature ctutils only.
Source§

fn cmp(&self, other: &Self) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 (const: unstable) · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 (const: unstable) · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 (const: unstable) · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

fn clamp_to<R>(self, range: R) -> Self
where Self: Sized, R: ClampBounds<Self>,

🔬This is a nightly-only experimental API. (clamp_to)
Restrict a value to a certain range. Read more
Source§

impl PartialEq for Mpint

Available on crate feature ctutils only.
Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl PartialOrd for Mpint

Available on crate feature ctutils only.
Source§

fn partial_cmp(&self, other: &Self) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 (const: unstable) · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 (const: unstable) · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 (const: unstable) · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 (const: unstable) · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl TryFrom<&BoxedUint> for Mpint

Available on crate feature bigint only.
Source§

type Error = Error

The type returned in the event of a conversion error.
Source§

fn try_from(uint: &Uint) -> Result<Self>

Performs the conversion.
Source§

impl TryFrom<&[u8]> for Mpint

Source§

type Error = Error

The type returned in the event of a conversion error.
Source§

fn try_from(bytes: &[u8]) -> Result<Self>

Performs the conversion.
Source§

impl TryFrom<Box<[u8]>> for Mpint

Source§

type Error = Error

The type returned in the event of a conversion error.
Source§

fn try_from(bytes: Box<[u8]>) -> Result<Self>

Performs the conversion.
Source§

impl TryFrom<BoxedUint> for Mpint

Available on crate feature bigint only.
Source§

type Error = Error

The type returned in the event of a conversion error.
Source§

fn try_from(uint: Uint) -> Result<Self>

Performs the conversion.
Source§

impl UpperHex for Mpint

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Zeroize for Mpint

Available on crate feature zeroize only.
Source§

fn zeroize(&mut self)

Zero out this object from memory using Rust intrinsics which ensure the zeroization operation is not “optimized away” by the compiler.

Auto Trait Implementations§

§

impl Forget for Mpint

§

impl Freeze for Mpint
where *const [u8] is !null: Freeze, PhantomData<[u8]>: Freeze,

§

impl Move for Mpint

§

impl RefUnwindSafe for Mpint

§

impl Send for Mpint
where BoxRaw<[u8]>: Send,

§

impl Sync for Mpint
where BoxRaw<[u8]>: Sync,

§

impl Unpin for Mpint
where Box<[u8]>: Unpin,

§

impl UnsafeUnpin for Mpint

§

impl UnwindSafe for Mpint
where BoxRaw<[u8]>: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.