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.
# use Uint;
let answer: = from;
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.
# use ;
let answer = uint!;
You can also use one of the pre-computed type [aliases]:
# use Uint;
use *;
let answer: U256 = from;
You can of course also create your own type alias if you need a funny size:
# use Uint;
type U1337 = ;
let answer: U1337 = from;
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).
#
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 Uint;
let a: = from;
let b: = from;
let c = a + b;
assert_eq!;
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.
# use uint;
let cow = uint!;
In fact, this macro recurses down the parse tree, so you can apply it to entire source files:
# use uint;
uint!
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.
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 theStandarddistribution, i.e.rng.gen().arbitrary: Implements theArbitrarytrait, allowing [Uint]s to be generated for fuzz testing.quickcheck: Implements theArbitrarytrait, allowing [Uint]s to be generated for property based testing.proptest: Implements theArbitrarytrait, allowing [Uint]s to be generated for property based testing. Proptest is used for theuints own test suite.serde: Implements theSerializeandDeserializetraits for [Uint] and [Bits].Serialization uses big-endian hex in human readable formats and big-endian byte strings in machine readable formats. [Uint] uses ethereumQuantityformat (0x-prefixed minimal string) when serializing in a human readable format.rlp: Implements theEncodableandDecodabletraits for [Uint] to allow serialization to/from RLP.fastrlp: Implements theEncodableandDecodabletraits for [Uint] to allow serialization to/from RLP.primitive-types: Implements the [From<_>] conversions between corresponding types.postgres: Implements theToSqltrait supporting many column types.num-bigint: Implements conversion to/fromBigUintandBigInt.ark-ff: Implements conversion to/fromBigIntandFptypes.sqlx: Implements database agnostic storage as byte array. Requiressqlxto be used with thetokio-native-tlsruntime, due to issue sqlx#1627.zeroize: Implements theZeroizetrait. This makes [Uint] and [Bits] compatible with thesecrecycrate.valuable: Implements theValuabletrait.pyo3: Implements theToPyObject,IntoPyandFromPyObjecttraits.parity-scale-codec: Implements theEncode,Decode,MaxEncodedLenandHasCompacttraits.bn-rs: Implements conversion to/from theBNandBigNumber.
Building and testing
Format, lint, build and test everything (I recommend creating a shell alias for this):
&&\
&&\
&&\
&&\
Run benchmarks with the provided .cargo/config.toml alias
Check documentation coverage
RUSTDOCFLAGS="-Z unstable-options --show-coverage"
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.