fixed_bigint/lib.rs
1// Copyright 2021 Google LLC
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#![no_std]
16#![cfg_attr(
17 feature = "nightly",
18 feature(
19 const_trait_impl,
20 const_ops,
21 const_cmp,
22 const_convert,
23 const_default,
24 const_clone,
25 const_index,
26 generic_const_exprs,
27 const_unsigned_bigint_helpers,
28 widening_mul
29 )
30)]
31#![cfg_attr(feature = "nightly", allow(incomplete_features))]
32
33//! A fixed-size big integer implementation, unsigned only.
34//!
35//! `FixedUInt<T, N, P>` is `N` limbs of a primitive `T` (`u8`/`u16`/`u32`/`u64`)
36//! with a compile-time `Personality` (`Nct` or `Ct`) that selects between
37//! value-dependent and constant-time impl bodies at every operator.
38//!
39//! ## Personality and constant-time operations
40//!
41//! `P` is a typestate: `Nct` (non-constant-time, the default) or `Ct`
42//! (constant-time). `Nct` bodies may branch on operand *values*; `Ct` bodies
43//! may not. Operations whose only sensible implementation has **data-dependent
44//! control flow** are therefore provided for `Nct` only — the `Ct` type simply
45//! does not implement the trait, so a misuse is a compile-time *"trait bound
46//! not satisfied"* error, never a silent timing leak. That error names the
47//! missing std/`num-traits` trait (e.g. `FixedUInt<u8, 4, Ct>: Div is not
48//! satisfied`); this table is the explanation, since a custom `#[diagnostic]`
49//! message cannot be attached to a trait defined in another crate.
50//!
51//! | operation | `Nct` | `Ct` | why `Ct` omits it |
52//! |---|---|---|---|
53//! | `+` `-` `*`, `wrapping`/`overflowing`/`carrying`, bit ops, shifts, compare, byte I/O | ✅ | ✅ | branchless |
54//! | `/` `%` (`Div`/`Rem`/`*Assign`), `CheckedDiv`, `Euclid` | ✅ | — | long division early-exits on operand bits |
55//! | `Ilog`/`Ilog2`/`Ilog10`, `Isqrt` | ✅ | — | data-dependent iteration / division |
56//! | `Num::from_str_radix`, `FromStr` | ✅ | — | parse length and digit branches depend on the input |
57//! | `CheckedAdd`/`CheckedMul` on `HeaplessBigInt` | ✅ | — | value-aware overflow report scans content |
58//!
59//! A `Ct` value that needs `x mod modulus` uses Montgomery reduction (the CIOS
60//! driver), not `/` / `%`. Every operator not in the omitted rows has one body
61//! shared by both personalities.
62//!
63//! Basic usage:
64//! ```
65//! use fixed_bigint::FixedUInt;
66//!
67//! let a : FixedUInt<u8,2> = 200u8.into();
68//! assert_eq!( a + a , 400u16.into() );
69//! assert_eq!( a * &100u8.into(), 20000u16.into() )
70//! ```
71//!
72//! With the `num-traits` feature (default), `FixedUInt` also implements
73//! `num_integer::Integer` and the `num_traits::PrimInt` bundle:
74//! ```
75//! # #[cfg(feature = "num-traits")] {
76//! use fixed_bigint::FixedUInt;
77//! use num_integer::Integer;
78//!
79//! let a : FixedUInt<u8,2> = 400u16.into();
80//! assert_eq!( a.is_multiple_of( &(8u8.into()) ) , true );
81//! assert_eq!( a.gcd( &(300u16.into() )) , 100u8.into() );
82//! assert_eq!( a.lcm( &(440u16.into() )) , 4400u16.into() );
83//! # }
84//! ```
85
86/// Fixed-size big integer implementation
87pub mod fixeduint;
88
89/// Machine word and doubleword
90mod machineword;
91
92/// Fixed-capacity, runtime-length unsigned bignum. See the module header.
93pub mod heapless;
94
95pub use crate::fixeduint::{FixedUInt, NonZeroFixedUInt};
96pub use crate::machineword::MachineWord;
97
98pub use crate::heapless::{HeaplessBigInt, NonZeroHeaplessBigInt};