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.

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)
000 00 00 00
9a378f9b2e332a700 00 00 08 09 a3 78 f9 b2 e3 32 a7
8000 00 00 02 00 80
-123400 00 00 02 ed cc
-deadbeef00 00 00 05 ff 21 52 41 11

Implementations§

Source§

impl Mpint

Source

pub fn from_bytes(bytes: &[u8]) -> Result<Mpint, Error>

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.

Source

pub fn from_positive_bytes(bytes: &[u8]) -> Mpint

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.

Source

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.

Source

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.
  • None if the value is negative
Source

pub fn is_positive(&self) -> bool

Is this Mpint positive?

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) -> Mpint

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: &Mpint) -> 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<(), Error>

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 impl Reader) -> Result<Mpint, Error>

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

impl Display for Mpint

Source§

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

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

impl Encode for Mpint

Source§

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

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

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

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>

Available on crate feature alloc only.
Encode this value, returning a Vec<u8> containing the encoded message. Read more
Source§

impl Eq for Mpint

Available on crate feature ctutils only.
Source§

impl From<&BoxedUint> for Mpint

Available on crate feature bigint only.
Source§

fn from(uint: &BoxedUint) -> Mpint

Converts to this type from the input type.
Source§

impl From<BoxedUint> for Mpint

Available on crate feature bigint only.
Source§

fn from(uint: BoxedUint) -> Mpint

Converts to this type from the input type.
Source§

impl LowerHex for Mpint

Source§

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

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

impl Ord for Mpint

Source§

fn cmp(&self, other: &Mpint) -> 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§

impl PartialEq for Mpint

Available on crate feature ctutils only.
Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialOrd for Mpint

Source§

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 (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<&[u8]> for Mpint

Source§

type Error = Error

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

fn try_from(bytes: &[u8]) -> Result<Mpint, Error>

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<Mpint, Error>

Performs the conversion.
Source§

impl TryFrom<Mpint> for DsaPrivateKey

Source§

type Error = Error

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

fn try_from(x: Mpint) -> Result<Self>

Performs the conversion.
Source§

impl UpperHex for Mpint

Source§

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

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 Freeze for Mpint

§

impl RefUnwindSafe for Mpint

§

impl Send for Mpint

§

impl Sync for Mpint

§

impl Unpin for Mpint

§

impl UnsafeUnpin for Mpint

§

impl UnwindSafe for Mpint

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> ToHex for T
where T: AsRef<[u8]>,

Source§

fn encode_hex<U>(&self) -> U
where U: FromIterator<char>,

Encode the hex strict representing self into the result. Lower case letters are used (e.g. f9b4ca)
Source§

fn encode_hex_upper<U>(&self) -> U
where U: FromIterator<char>,

Encode the hex strict representing self into the result. Upper case letters are used (e.g. F9B4CA)
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 = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.