Crate ruint
Expand description
§Rust uint crate using const-generics
Implements Uint<BITS, LIMBS>, the ring of numbers modulo $2^{\mathsf{BITS}}$. It requires two
generic arguments: the number of bits and the number of 64-bit ‘limbs’ required to store those bits.
let answer: Uint<256, 4> = Uint::from(42);You can compute LIMBS yourself using $\mathsf{LIMBS} = \left\lceil{\mathsf{BITS} / 64}\right\rceil$,
i.e.LIMBS equals BITS divided by $64$ rounded up. Uint will panic! if you try to
construct it with incorrect arguments. Ideally this would be a compile time error, but
that is blocked by Rust issue #60551.
A more convenient method on stable is to use the [uint!] macro, which constructs the right
Uint for you.
let answer = uint!(42_U256);You can also use one of the pre-computed type aliases:
use ruint::aliases::*;
let answer: U256 = Uint::from(42);You can of course also create your own type alias if you need a funny size:
type U1337 = Uint<1337, 21>;
let answer: U1337 = Uint::from(42);§Rust nightly
If you are on nightly, you can use [Uint<BITS>][nightly::Uint] which will
compute the number of limbs for you. Unfortunately this can not be made stable
without generic_const_exprs support (Rust issue #76560).
use ruint::nightly::Uint;
let answer: Uint<256> = Uint::<256>::from(42);Even on nightly, the ergonomics of Rust are limited. In the example above Rust
requires explicit type annotation for Uint::from, where it did not require
it in the stable version. There are a few more subtle issues that make this
less ideal than it appears. It also looks like it may take some time before
these nightly features are stabilized.
§Examples
use ruint::Uint;
let a: Uint<256, 4> = Uint::from(0xf00f_u64);
let b: Uint<256, 4> = Uint::from(42_u64);
let c  = a + b;
assert_eq!(c, Uint::from(0xf039_u64));There is a convenient macro [uint!] to create constants for you. It allows
for arbitrary length constants using standard Rust integer syntax. The size of
the Uint or Bits is specified with a U or B suffix followed by the
number of bits. The standard Rust syntax of decimal, hexadecimal and even binary and octal is
supported using their prefixes 0x, 0b and 0o. Literals can have
underscores _ added for readability.
let cow = uint!(0xc85ef7d79691fe79573b1a7064c19c1a9819ebdbd1faaab1a8ec92344438aaf4_U256);In fact, this macro recurses down the parse tree, so you can apply it to entire source files:
uint!{
let a = 42_U256;
let b = 0xf00f_1337_c0d3_U256;
let c = a + b;
assert_eq!(c, 263947537596669_U256);
}Note that since B is a valid hexadecimal digit there can be ambiguity. To lessen the impact an underscore separator _B is required in this case.
§Supported Rust Versions
Uint will keep a rolling MSRV (minimum supported rust version) policy of at least 6 months. When increasing the MSRV, the new Rust version must have been released at least six months ago. The current MSRV is 1.65.0.
Note that the MSRV is not increased automatically, and only as part of a minor release.
§Feature flags
There is support for a number of crates. These are enabled by setting the identically named feature flag.
- unstableEnable sem-ver unstable features.
- rand: Implements sampling from the- Standarddistribution, i.e.- rng.gen(), alongside some utility- random*methods on- Uintitself.
- rand-09: Implements sampling from the- StandardUniformdistribution, i.e.- rng.random(), alongside some utility- random*methods on- Uintitself.
- arbitrary: Implements the- Arbitrarytrait, allowing- Uints to be generated for fuzz testing.
- quickcheck: Implements the- Arbitrarytrait, allowing- Uints to be generated for property based testing.
- proptest: Implements the- Arbitrarytrait, allowing- Uints to be generated for property based testing. Proptest is used for the- uints own test suite.
- serde: Implements the- Serializeand- Deserializetraits for- Uintand- Bits.Serialization uses big-endian hex in human readable formats and big-endian byte strings in machine readable formats.- Uintuses ethereum- Quantityformat (0x-prefixed minimal string) when serializing in a human readable format.
- rlp: Implements the- Encodableand- Decodabletraits for- Uintto allow serialization to/from RLP.
- fastrlp: Implements the- Encodableand- Decodabletraits for- Uintto allow serialization to/from RLP.
- primitive-types: Implements the- From<_>conversions between corresponding types.
- postgres: Implements the- ToSqltrait supporting many column types.
- num-bigint: Implements conversion to/from- BigUintand- BigInt.
- ark-ff: Implements conversion to/from the- BigInteger*types and the- Fp*types from- ark-ff@0.3.
- ark-ff-04: Implements conversion to/from- BigIntand- Fptypes from- ark-ff@0.4.
- sqlx: Implements database agnostic storage as byte array. Requires- sqlxto be used with the- tokio-native-tlsruntime, due to issue sqlx#1627.
- zeroize: Implements the- Zeroizetrait. This makes- Uintand- Bitscompatible with the- secrecycrate.
- valuable: Implements the- Valuabletrait.
- pyo3: Implements the- IntoPyObjectand- FromPyObjecttraits.
- parity-scale-codec: Implements the- Encode,- Decode,- MaxEncodedLenand- HasCompacttraits.
- bn-rs: Implements conversion to/from the- BNand- BigNumber.
- bytemuck: Implements the- Podand- Zeroabletraits for- Uintwhere the size is a multiple of 64, up to 1024. This allows- Uintto be used where a- Podtrait bound exists.
- num-traits: Implements about forty applicable traits.
- subtle: Implements [- Uint::bit_ct],- ConditionallySelectable,- ConditionallyNegatable,- ConstantTimeEq/- ConstantTimeGreater/- ConstantTimeLess.
- der: Implements- Encode/- Decodeand- TryFrom/- Fromcasting for- Any,- AnyRef,- Int,- IntRef,- Uint,- UintRef.
- diesel: Implements the- ToSqland- FromSqltraits for storing- Uintvalues as byte arrays in databases supported by Diesel.
§Building and testing
Format, lint, build and test everything (I recommend creating a shell alias for this):
cargo fmt &&\
cargo clippy --all-features --all-targets &&\
cargo test --workspace --all-features --doc -- --nocapture &&\
cargo test --workspace --all-features --all-targets -- --nocapture &&\
cargo doc --workspace --all-features --no-depsRun benchmarks with the provided .cargo/config.toml alias
cargo criterionCheck documentation coverage
RUSTDOCFLAGS="-Z unstable-options --show-coverage"  cargo doc --workspace --all-features --no-deps§Features
- All the quality of life features one could want.
- Compatible with std u64, etc types. See Rust’s integer methods.
- Adhere to Rust API Guidelines
- Montgomery REDC and other algo’s for implementing prime fields.
§To do
- Builds no-stdandwasm.
- Fast platform agnostic generic algorithms.
- Target specific assembly optimizations (where available).
- Optional num-traits, etc, support.
- Run-time sized type with compatible interface.
Modules§
- algorithms
- ⚠️ Collection of bignum algorithms.
- aliases
- Type aliases for common bit sizes of UintandBits.
- support
- Support for external crates.
Macros§
- const_for 
- Compile time for loops with a constvariable for testing.
Structs§
- Bits
- A newtype wrapper around Uintthat restricts operations to those relevant for bit arrays.
- Uint
- The ring of numbers modulo $2^{\mathtt{BITS}}$.
Enums§
- BaseConvert Error 
- Error for from_base_leandfrom_base_be.
- FromUint Error 
- Error for TryFrom<Uint>.
- ParseError 
- Error for from_str_radix.
- ToFieldError 
- Error for TryFrom<Uint>forark_ffand others.
- ToUintError 
- Error for TryFrom<T>forUint.
Traits§
- UintTryFrom 
- ⚠️ Workaround for Rust issue #50133.
Use TryFrominstead.
- UintTryTo 
- ⚠️ Workaround for Rust issue #50133.
Use TryFrominstead.