Skip to main content

Crate big_number

Crate big_number 

Source
Expand description

Fixed-size unsigned and signed big integers with u64 limbs.

The central type is Uint<BITS, LIMBS>, a LIMBS * 64-bit stack value used by the crypto crate as the backing type for RSA, the NIST prime curves and Curve25519 arithmetic. The crate is no_std-compatible; heap-allocating helpers (to_be_bytes, to_string_radix, Display) require the alloc feature.

§Representation

LIMBS must equal ceil(BITS / 64) (enforced by a compile-time check). Values are stored little-endian, base 2^64. When BITS is not a multiple of 64 the top limb may hold up to BITS % 64 significant bits; the byte conversion helpers (Uint::from_be_slice and friends) and Uint::from_str_radix reject or mask anything above BITS.

Arithmetic operators (+, -, *) are wrapping: carry/borrow past LIMBS limbs is silently discarded, matching primitive integer semantics. Use the explicit *_raw, mul_word and div_rem_word methods when the carry/borrow matters.

§Constant time

add_mod, sub_mod, double_mod, mul_mod, mul_mod_barrett, ct_select, ct_eq and ct_ge branch only on public bounds, not on limb values. In contrast Uint::modpow and Uint::modpow_barrett iterate over the exponent bits with a data-dependent branch and are therefore only suitable for a public exponent (e.g. the RSA public exponent e).

§Errors, panics and limits

  • Uint::from_str_radix returns Error for invalid input; it never panics and does not allocate.
  • The byte conversion helpers and the modular methods assert their documented length/limb preconditions (see each method).
  • No method allocates unless it returns a Vec/String.

Structs§

Int
A fixed-size two’s-complement signed integer of BITS bits.
Uint
A fixed-size unsigned integer of BITS bits stored in LIMBS 64-bit limbs.

Enums§

Error
Errors returned by Uint::from_str_radix.

Constants§

MAX_BARRETT_LIMBS
Largest effective limb count accepted by Barrett reduction, i.e. the largest eff_limbs for which 2 * (eff_limbs + 1) <= MAX_LIMBS.
MAX_LIMBS

Functions§

adc
mac
sbb