dashu-int-0.6.0-rc.4 has been yanked.
dashu-int
Arbitrary precision integer implementation, as a part of the dashu library for arbitrary-precision mathematics. See Docs.rs for the full documentation.
The majority of the code is based on the ibig crate. The modification notice based on the the original
ibigrepo is included in the NOTICE file.
Features
- Supports
no_stdand written in pure Rust. - Support for both unsigned and signed big integers.
- Small integers are inlined on stack with specialized algorithms.
- Efficient implementation for basic arithmetic operations (
+,-,*,/,%,<<,>>). - Support advanced arithmetic operations including
pow,ilog,gcd,gcd_ext. - Bit operations for signed big integers follow the 2's complement rule.
- Efficient implementation for modular arithmetics (e.g. modular powering and inverse).
- Efficient integer parsing and printing with base 2~36.
- Developer friendly debug printing for big integers.
- Direct access to underlying machine word array.
Quick example
Construct integers with the compile-time literal macros (beyond u128, no precision
loss), mix them with built-in integers in arithmetic, and parse from any base:
use BitTest;
use ;
use ;
// Compile-time literal beyond u128
let n = ubig!;
// Mixed-type arithmetic with primitive integers
let e = 2 * &from - 1;
// Parse a signed integer in an arbitrary base
let c = from_str_radix.unwrap;
assert_eq!;
assert_eq!;
// Bit operations follow the two's-complement rule
assert_eq!;
assert_eq!; // arithmetic shift
// bit_len reports the magnitude in bits
assert_eq!;
For modular arithmetic, MontgomeryRepr reduces a modulus into Montgomery form so
multiplication, squaring, and exponentiation avoid the expensive division:
use ;
let p = from.pow - ONE; // a Mersenne prime
let ring = new;
// reduce values into Montgomery form, then multiply / square / pow
let a = ring.reduce;
assert_eq!; // Fermat: a^(p-1) = 1 (mod p)
// + - * stay in Montgomery form, avoiding the division
assert_eq!;
assert_eq!;
// inversion works too (3·3⁻¹ ≡ 1 mod p)
assert_eq!;
Optional dependencies
std(default): forstd::error::Error.num-traits(default): integral traits.rand(default): random number generation.serde: serialization and deserialization.
Performance
See the built-in benchmark.
License
See the top-level readme.