pub struct MPInt { /* private fields */ }
alloc
only.Expand description
Multiple precision integer, a.k.a. “mpint”.
This type is used for representing the big integer components of DSA and RSA keys.
Described in RFC4251 § 5:
Represents multiple precision integers in two’s complement format, stored as a string, 8 bits per byte, MSB first. Negative numbers have the value 1 as the most significant bit of the first byte of the data partition. If the most significant bit would be set for a positive number, the number MUST be preceded by a zero byte. Unnecessary leading bytes with the value 0 or 255 MUST NOT be included. The value zero MUST be stored as a string with zero bytes of data.
By convention, a number that is used in modular computations in Z_n SHOULD be represented in the range 0 <= x < n.
Examples
value (hex) | representation (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 |
-1234 | 00 00 00 02 ed cc |
-deadbeef | 00 00 00 05 ff 21 52 41 11 |
Implementations
sourceimpl MPInt
impl MPInt
sourcepub fn from_bytes(bytes: &[u8]) -> Result<Self>
pub fn from_bytes(bytes: &[u8]) -> Result<Self>
Create a new multiple precision integer from the given big endian-encoded byte slice.
Note that this method expects a leading zero on positive integers whose MSB is set, but does NOT expect a 4-byte length prefix.
sourcepub fn from_positive_bytes(bytes: &[u8]) -> Result<Self>
pub fn from_positive_bytes(bytes: &[u8]) -> Result<Self>
Create a new multiple precision integer from the given big endian encoded byte slice representing a positive integer.
The integer should not start with any leading zeroes.
sourcepub fn as_bytes(&self) -> &[u8]ⓘNotable traits for &'_ [u8]impl<'_> Read for &'_ [u8]impl<'_> Write for &'_ mut [u8]
pub fn as_bytes(&self) -> &[u8]ⓘNotable traits for &'_ [u8]impl<'_> Read for &'_ [u8]impl<'_> Write for &'_ mut [u8]
Get the big integer data encoded as big endian bytes.
This slice will contain a leading zero if the value is positive but the
MSB is also set. Use MPInt::as_positive_bytes
to ensure the number
is positive and strip the leading zero byte if it exists.
Trait Implementations
sourceimpl ConstantTimeEq for MPInt
This is supported on crate feature subtle
only.
impl ConstantTimeEq for MPInt
subtle
only.sourceimpl Ord for MPInt
impl Ord for MPInt
sourceimpl PartialOrd<MPInt> for MPInt
impl PartialOrd<MPInt> for MPInt
sourcefn partial_cmp(&self, other: &MPInt) -> Option<Ordering>
fn partial_cmp(&self, other: &MPInt) -> Option<Ordering>
This method returns an ordering between self
and other
values if one exists. Read more
1.0.0 · sourcefn lt(&self, other: &Rhs) -> bool
fn lt(&self, other: &Rhs) -> bool
This method tests less than (for self
and other
) and is used by the <
operator. Read more
1.0.0 · sourcefn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
This method tests less than or equal to (for self
and other
) and is used by the <=
operator. Read more
impl Eq for MPInt
impl StructuralEq for MPInt
impl StructuralPartialEq for MPInt
Auto Trait Implementations
impl RefUnwindSafe for MPInt
impl Send for MPInt
impl Sync for MPInt
impl Unpin for MPInt
impl UnwindSafe for MPInt
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> ToOwned for T where
T: Clone,
impl<T> ToOwned for T where
T: Clone,
type Owned = T
type Owned = T
The resulting type after obtaining ownership.
sourcefn clone_into(&self, target: &mut T)
fn clone_into(&self, target: &mut T)
toowned_clone_into
)Uses borrowed data to replace owned data, usually by cloning. Read more