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 generic_const_exprs,
26 const_unsigned_bigint_helpers,
27 widening_mul
28 )
29)]
30#![cfg_attr(feature = "nightly", allow(incomplete_features))]
31
32//! A fixed-size big integer implementation, unsigned only.
33//!
34//! `FixedUInt<T, N, P>` is `N` limbs of a primitive `T` (`u8`/`u16`/`u32`/`u64`)
35//! with a compile-time `Personality` (`Nct` or `Ct`) that selects between
36//! value-dependent and constant-time impl bodies at every operator.
37//!
38//! ## Personality and constant-time operations
39//!
40//! `P` is a typestate: `Nct` (non-constant-time, the default) or `Ct`
41//! (constant-time). `Nct` bodies may branch on operand *values*; `Ct` bodies
42//! may not. Operations whose only sensible implementation has **data-dependent
43//! control flow** are therefore provided for `Nct` only — the `Ct` type simply
44//! does not implement the trait, so a misuse is a compile-time *"trait bound
45//! not satisfied"* error, never a silent timing leak. That error names the
46//! missing std/`num-traits` trait (e.g. `FixedUInt<u8, 4, Ct>: Div is not
47//! satisfied`); this table is the explanation, since a custom `#[diagnostic]`
48//! message cannot be attached to a trait defined in another crate.
49//!
50//! | operation | `Nct` | `Ct` | why `Ct` omits it |
51//! |---|---|---|---|
52//! | `+` `-` `*`, `wrapping`/`overflowing`/`carrying`, bit ops, shifts, compare, byte I/O | ✅ | ✅ | branchless |
53//! | `/` `%` (`Div`/`Rem`/`*Assign`), `CheckedDiv`, `Euclid` | ✅ | — | long division early-exits on operand bits |
54//! | `Ilog`/`Ilog2`/`Ilog10`, `Isqrt` | ✅ | — | data-dependent iteration / division |
55//! | `Num::from_str_radix`, `FromStr` | ✅ | — | parse length and digit branches depend on the input |
56//! | `CheckedAdd`/`CheckedMul` on `HeaplessBigInt` | ✅ | — | value-aware overflow report scans content |
57//!
58//! A `Ct` value that needs `x mod modulus` uses Montgomery reduction (the CIOS
59//! driver), not `/` / `%`. Every operator not in the omitted rows has one body
60//! shared by both personalities.
61//!
62//! Basic usage:
63//! ```
64//! use fixed_bigint::FixedUInt;
65//!
66//! let a : FixedUInt<u8,2> = 200u8.into();
67//! assert_eq!( a + a , 400u16.into() );
68//! assert_eq!( a * &100u8.into(), 20000u16.into() )
69//! ```
70//!
71//! With the `num-traits` feature (default), `FixedUInt` also implements
72//! `num_integer::Integer` and the `num_traits::PrimInt` bundle:
73//! ```
74//! # #[cfg(feature = "num-traits")] {
75//! use fixed_bigint::FixedUInt;
76//! use num_integer::Integer;
77//!
78//! let a : FixedUInt<u8,2> = 400u16.into();
79//! assert_eq!( a.is_multiple_of( &(8u8.into()) ) , true );
80//! assert_eq!( a.gcd( &(300u16.into() )) , 100u8.into() );
81//! assert_eq!( a.lcm( &(440u16.into() )) , 4400u16.into() );
82//! # }
83//! ```
84
85/// Fixed-size big integer implementation
86pub mod fixeduint;
87
88/// Machine word and doubleword
89mod machineword;
90
91/// Fixed-capacity, runtime-length unsigned bignum. See the module header.
92pub mod heapless;
93
94pub use crate::fixeduint::{FixedUInt, NonZeroFixedUInt};
95pub use crate::machineword::MachineWord;
96
97pub use crate::heapless::HeaplessBigInt;