• Stars
    star
    169
  • Rank 223,228 (Top 5 %)
  • Language
    Rust
  • License
    MIT License
  • Created over 2 years ago
  • Updated 14 days ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Rust Uint crate using const-generics

Rust uint crate using const-generics

crates.io docs.rs MIT License dependency status codecov CI

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 ruint::Uint;
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.

# use ruint::{Uint, uint};
let answer = uint!(42_U256);

You can also use one of the pre-computed type [aliases]:

# use ruint::Uint;
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:

# use ruint::Uint;
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).

# #[cfg(feature = "generic_const_exprs")] {
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.

# use ruint::uint;
let cow = uint!(0xc85ef7d79691fe79573b1a7064c19c1a9819ebdbd1faaab1a8ec92344438aaf4_U256);

In fact, this macro recurses down the parse tree, so you can apply it to entire source files:

# use ruint::uint;
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.

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-deps

Run benchmarks with the provided .cargo/config.toml alias

cargo criterion

Check 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-std and wasm.
  • Fast platform agnostic generic algorithms.
  • Target specific assembly optimizations (where available).
  • Optional num-traits, etc, support.
  • Run-time sized type with compatible interface.

lines of code GitHub contributors GitHub issues GitHub pull requests GitHub Repo stars crates.io

More Repositories

1

PostgrestSkeleton

Skeleton web stack using Docker Compose, PostgreSQL, PostgREST, Nginx and Auth0.
JavaScript
109
star
2

yul

Rust
101
star
3

cria

Tiny inference-only implementation of LLaMA
Python
91
star
4

experiment-solexp

Fixed point solidity exponentiation.
Jupyter Notebook
74
star
5

goldilocks

A library for fast NTTs over the Goldilocks prime field
Rust
71
star
6

pc-bench

Benchmarking Polynomial Commitments
Jupyter Notebook
47
star
7

rust-service-template

Rust
22
star
8

cli-batteries

Batteries included command line interfaces.
Rust
21
star
9

evm-groth16

Groth16 verifier in EVM
Solidity
19
star
10

binius

Fork of binius
Rust
19
star
11

kzg-ceremony-coordinator

Moved to https://github.com/ethereum/kzg-ceremony-sequencer
Rust
17
star
12

XCompile

Specify compose key sequences in a simple language.
Python
9
star
13

zkp-u256

Rust 256-bit unsigned integers.
Rust
9
star
14

rust-static-build

Rust build image to create statically compiled binaries.
Dockerfile
8
star
15

proto-ecdsa-plonky2

Rust
8
star
16

mpc-iris-code

Rust
7
star
17

delegated-spartan

Mobile delegated R1CS proving using SpartanNIZK
Rust
6
star
18

proto-goldilocks-webgpu

Prototype WebGPU compute implementation of Goldilocks
Rust
5
star
19

dancing-cells

Rust
4
star
20

fhe-iris-code

Rust
4
star
21

Codecup-2014-Poly-Y

My entry for the 2014 Codecup Poly-Y competition. See http://www.codecup.nl/intro.php.
C++
3
star
22

EntropyCoder

Library for optimal compression using entropy/arithmetic/range coding.
C++
3
star
23

Codecup-2013-Symple

My entry for the 2013 Codecup competition. See http://archive.codecup.nl/2013/.
C++
2
star
24

shardtoken

Ethereum token contract for a post-state-rent, post-sharding world.
JavaScript
2
star
25

BitGovPrototype

BitGov prototype interface
CSS
1
star
26

remgo

Computer Go player using MCTS and HashLife inspired information sharing
C++
1
star
27

Codecup-2015-Ayu

C++
1
star
28

Principia

Futuristic programming, reasoning and proof language
C++
1
star
29

cotracker

Tool to fetch Aranet4 data
Rust
1
star
30

khinchin

Computes Khinchin's constant
Rust
1
star
31

extended-postgresql

Docker image of PostgreSQL with popular extensions
Makefile
1
star
32

pookie-bouncer

ESP32+TMC2209+NEMA17 motor controller for bouncing pookies.
C++
1
star