pub struct Mpint { /* private fields */ }alloc only.Expand description
Multiple precision integer, a.k.a. mpint.
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§
Source§impl 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.
§Errors
Returns Error::MpintEncoding in the event of an unnecessary leading 0.
Sourcepub fn from_positive_bytes(bytes: &[u8]) -> Self
pub fn from_positive_bytes(bytes: &[u8]) -> Self
Create a new multiple precision integer from the given big endian encoded byte slice representing a positive integer.
The input may begin with leading zeros, which will be stripped when converted to Mpint
encoding.
Sourcepub fn as_bytes(&self) -> &[u8]
pub fn as_bytes(&self) -> &[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.
Sourcepub fn as_positive_bytes(&self) -> Option<&[u8]>
pub fn as_positive_bytes(&self) -> Option<&[u8]>
Get the bytes of a positive integer.
§Returns
Some(bytes)if the number is positive. The leading zero byte will be stripped.Noneif the value is negative
Sourcepub fn is_positive(&self) -> bool
pub fn is_positive(&self) -> bool
Is this Mpint positive?
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 moreSource§impl Ord for Mpint
impl Ord for Mpint
1.21.0 (const: unstable) · Source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
Source§impl PartialOrd for Mpint
impl PartialOrd for Mpint
impl Eq for Mpint
ctutils only.