pub struct Mpint { /* private fields */ }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) |
|---|---|
| 0 | 00 00 00 00 |
| 9a378f9b2e332a7 | 00 00 00 08 09 a3 78 f9 b2 e3 32 a7 |
| 80 | 00 00 00 02 00 80 |
Implementations§
Source§impl Mpint
impl Mpint
Sourcepub const MAX_SIZE: usize = MpintRef::MAX_SIZE
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.
Sourcepub fn from_bytes(bytes: &[u8]) -> Result<Self>
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.
Trait Implementations§
Source§impl Encode for Mpint
impl Encode for Mpint
Source§fn encoded_len(&self) -> Result<usize>
fn encoded_len(&self) -> Result<usize>
Source§fn encoded_len_prefixed(&self) -> Result<usize, Error>
fn encoded_len_prefixed(&self) -> Result<usize, Error>
uint32 length prefix. Read moreimpl Eq for Mpint
ctutils only.Source§impl Ord for Mpint
Available on crate feature ctutils only.
impl Ord for Mpint
ctutils only.1.21.0 (const: unstable) · Source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
1.21.0 (const: unstable) · Source§fn min(self, other: Self) -> Selfwhere
Self: Sized,
fn min(self, other: Self) -> Selfwhere
Self: Sized,
Source§impl PartialOrd for Mpint
Available on crate feature ctutils only.
impl PartialOrd for Mpint
ctutils only.