Skip to main content

Crate fixed_bigint

Crate fixed_bigint 

Source
Expand description

A fixed-size big integer implementation, unsigned only.

FixedUInt<T, N, P> is N limbs of a primitive T (u8/u16/u32/u64) with a compile-time Personality (Nct or Ct) that selects between value-dependent and constant-time impl bodies at every operator.

§Personality and constant-time operations

P is a typestate: Nct (non-constant-time, the default) or Ct (constant-time). Nct bodies may branch on operand values; Ct bodies may not. Operations whose only sensible implementation has data-dependent control flow are therefore provided for Nct only — the Ct type simply does not implement the trait, so a misuse is a compile-time “trait bound not satisfied” error, never a silent timing leak. That error names the missing std/num-traits trait (e.g. FixedUInt<u8, 4, Ct>: Div is not satisfied); this table is the explanation, since a custom #[diagnostic] message cannot be attached to a trait defined in another crate.

operationNctCtwhy Ct omits it
+ - *, wrapping/overflowing/carrying, bit ops, shifts, compare, byte I/Obranchless
/ % (Div/Rem/*Assign), CheckedDiv, Euclidlong division early-exits on operand bits
Ilog/Ilog2/Ilog10, Isqrtdata-dependent iteration / division
Num::from_str_radix, FromStrparse length and digit branches depend on the input
CheckedAdd/CheckedMul on HeaplessBigIntvalue-aware overflow report scans content

A Ct value that needs x mod modulus uses Montgomery reduction (the CIOS driver), not / / %. Every operator not in the omitted rows has one body shared by both personalities.

Basic usage:

use fixed_bigint::FixedUInt;

let a : FixedUInt<u8,2> = 200u8.into();
assert_eq!( a + a , 400u16.into() );
assert_eq!( a * &100u8.into(), 20000u16.into() )

With the num-traits feature (default), FixedUInt also implements num_integer::Integer and the num_traits::PrimInt bundle:

use fixed_bigint::FixedUInt;
use num_integer::Integer;

let a : FixedUInt<u8,2> = 400u16.into();
assert_eq!( a.is_multiple_of( &(8u8.into()) ) , true );
assert_eq!( a.gcd( &(300u16.into() )) , 100u8.into() );
assert_eq!( a.lcm( &(440u16.into() )) , 4400u16.into() );

Re-exports§

pub use crate::fixeduint::FixedUInt;
pub use crate::fixeduint::NonZeroFixedUInt;
pub use crate::heapless::HeaplessBigInt;
pub use crate::heapless::NonZeroHeaplessBigInt;

Modules§

fixeduint
Fixed-size big integer implementation
heapless
Fixed-capacity, runtime-length unsigned bignum. See the module header. Unsigned integer whose width is chosen at runtime, not by the type.

Traits§

MachineWord
Represents a CPU native word, from 8-bit to 64-bit, with corresponding double-word to hold multiplication/division products.