Skip to main content

MpUIntCommon

Trait MpUIntCommon 

Source
pub trait MpUIntCommon: MpUIntCommonPriv + LowerHex {
    // Required methods
    fn is_empty(&self) -> bool;
    fn load_l_full(&self, i: usize) -> LimbType;
    fn load_l(&self, i: usize) -> LimbType;

    // Provided methods
    fn len(&self) -> usize { ... }
    fn nlimbs(&self) -> usize { ... }
    fn test_bit(&self, pos: usize) -> LimbChoice { ... }
    fn fmt_lower_hex(&self, f: &mut Formatter<'_>) -> Result { ... }
    fn len_is_compatible_with(&self, len: usize) -> bool { ... }
    fn try_into_u8(&self) -> Result<u8, MpUIntCommonTryIntoNativeError> { ... }
    fn try_into_u16(&self) -> Result<u16, MpUIntCommonTryIntoNativeError> { ... }
    fn try_into_u32(&self) -> Result<u32, MpUIntCommonTryIntoNativeError> { ... }
    fn try_into_u64(&self) -> Result<u64, MpUIntCommonTryIntoNativeError> { ... }
}
Expand description

Common interface to multiprecision integers of any of the supported representations.

MpUIntCommon provides read access to a multiprecision integer in units of LimbType, independent of how it’s stored in the underlying buffer. Limbs are indexed from least to most significant.

A multiprecision integer’s length in units of bytes doesn’t need to be a multiple of the LimbType size for the byte slice representations. If it isn’t, the underlying buffer holds only the low bytes of the most significant limb, and its missing high bytes read as zero.

This trait is implemented by both the read-only and the mutable representation-specific types, e.g. MpBigEndianUIntByteSlice and MpMutBigEndianUIntByteSlice, c.f. the module level documentation. The read-only ones implement MpUInt in addition, the mutable ones MpMutUInt. Arithmetic primitives taking an operand they only read from are generic over MpUIntCommon, so that either kind can be passed.

The trait is sealed, i.e. it cannot be implemented outside of this crate.

Required Methods§

Source

fn is_empty(&self) -> bool

Whether the multiprecision integer’s length in units of bytes is zero.

Source

fn load_l_full(&self, i: usize) -> LimbType

Load a limb fully covered by the underlying buffer.

Returns the specified limb in the host’s native endianess. Use load_l() instead if the limb might be the partially covered most significant one.

Runs in constant time as far as branching is concerned.

§Arguments
  • i - The index of the limb to load, counted from least to most significant. Must be less than nlimbs(). The limb must be fully contained in the underlying buffer, i.e. if i refers to the most significant limb, the latter must not be a partially covered one.
Source

fn load_l(&self, i: usize) -> LimbType

Load a limb, which might be the partially covered most significant one.

Returns the specified limb in the host’s native endianess. Any of its high bytes not covered by the underlying buffer read as zero.

Execution time depends only on the multiprecision integer’s length in units of bytes and the i argument and is otherwise constant as far as branching is concerned.

§Arguments
  • i - The index of the limb to load, counted from least to most significant. Must be less than nlimbs().

Provided Methods§

Source

fn len(&self) -> usize

The multiprecision integer’s length in units of bytes.

Source

fn nlimbs(&self) -> usize

The number of LimbType limbs spanned by the multiprecision integer.

That is, its length in units of bytes converted to units of LimbType limbs, rounding up, c.f. MpNativeEndianUIntLimbsSlice::nlimbs_for_len().

Source

fn test_bit(&self, pos: usize) -> LimbChoice

Test a bit of the multiprecision integer.

Returns a LimbChoice representing true if the bit is set and false otherwise. Bits at positions at or beyond 8 times the multiprecision integer’s length in units of bytes read as unset.

Execution time depends only on the multiprecision integer’s length in units of bytes and the pos argument and is otherwise constant as far as branching is concerned.

§Arguments
  • pos - The position of the bit to test, counted from least to most significant.
Source

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

Format the multiprecision integer in lowercase hexadecimal.

Helper for implementing fmt::LowerHex. The digits are written from most to least significant, with a _ separating the limbs. The most significant limb gets only as many digits as it has bytes covered by the underlying buffer. With the alternate flag ({:#x}), a 0x prefix is prepended. An empty multiprecision integer is written as (empty).

No constant-time guarantees are made.

§Arguments
Source

fn len_is_compatible_with(&self, len: usize) -> bool

Check whether the multiprecision integer fits into a given length in units of bytes.

Returns true if the multiprecision integer’s length in units of bytes is at most len. For representations whose length in units of bytes is always a multiple of the LimbType size, like MpNativeEndianUIntLimbsSlice, the length may also exceed len by less than a LimbType’s size, as long as all of the excess high bytes are zero. This accommodates the alignment excess of a buffer allocated for a length of len.

Note that the relation isn’t symmetric: a.len_is_compatible_with(b.len()) doesn’t imply b.len_is_compatible_with(a.len()). Requiring two multiprecision integers to be of equal length in units of bytes, within the alignment excess, needs both checks.

In the alignment excess case, the result and the execution time depend on the value of the multiprecision integer’s excess high bytes.

§Arguments
Source

fn try_into_u8(&self) -> Result<u8, MpUIntCommonTryIntoNativeError>

Convert the multiprecision integer to a u8.

Execution time depends only on the multiprecision integer’s length in units of bytes and on whether its value fits, and is otherwise constant as far as branching is concerned.

§Errors

Returns MpUIntCommonTryIntoNativeError if the multiprecision integer’s value exceeds the native type’s range.

Source

fn try_into_u16(&self) -> Result<u16, MpUIntCommonTryIntoNativeError>

Convert the multiprecision integer to a u16.

Execution time depends only on the multiprecision integer’s length in units of bytes and on whether its value fits, and is otherwise constant as far as branching is concerned.

§Errors

Returns MpUIntCommonTryIntoNativeError if the multiprecision integer’s value exceeds the native type’s range.

Source

fn try_into_u32(&self) -> Result<u32, MpUIntCommonTryIntoNativeError>

Convert the multiprecision integer to a u32.

Execution time depends only on the multiprecision integer’s length in units of bytes and on whether its value fits, and is otherwise constant as far as branching is concerned.

§Errors

Returns MpUIntCommonTryIntoNativeError if the multiprecision integer’s value exceeds the native type’s range.

Source

fn try_into_u64(&self) -> Result<u64, MpUIntCommonTryIntoNativeError>

Convert the multiprecision integer to a u64.

Execution time depends only on the multiprecision integer’s length in units of bytes and on whether its value fits, and is otherwise constant as far as branching is concerned.

§Errors

Returns MpUIntCommonTryIntoNativeError if the multiprecision integer’s value exceeds the native type’s range.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§