Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added

- Support for `no_std` environments (#274)

[#274]: https://github.com/recmo/uint/pulls/274

## [1.9.0] - 2023-07-25

### Added
Expand Down
72 changes: 47 additions & 25 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,30 +38,34 @@ ruint-macro = { version = "1.1.0", path = "ruint-macro" }
name = "bench_uint"
harness = false
path = "benches/bench.rs"
required-features = ["std"]

[dependencies]
ruint-macro.workspace = true

thiserror = "1.0"
thiserror = { version = "1.0", optional = true }

# support
alloy-rlp = { version = "0.3", optional = true }
arbitrary = { version = "1", optional = true }
ark-ff-04 = { version = "0.4.0", package = "ark-ff", optional = true }
ark-ff-03 = { version = "0.3.0", package = "ark-ff", optional = true }
bn-rs = { version = "0.2", optional = true }
fastrlp = { version = "0.3", optional = true }
num-bigint = { version = "0.4", optional = true }
parity-scale-codec = { version = "3", optional = true, features = ["derive", "max-encoded-len"] }
primitive-types = { version = "0.12", optional = true }
proptest = { version = "1", optional = true }
pyo3 = { version = "0.19", optional = true }
quickcheck = { version = "1", optional = true }
rand = { version = "0.8", optional = true }
rlp = { version = "0.5", optional = true }
serde = { version = "1", optional = true }
valuable = { version = "0.1", optional = true }
zeroize = { version = "1.6", optional = true }
alloy-rlp = { version = "0.3", optional = true, default-features = false }
arbitrary = { version = "1", optional = true, default-features = false }
ark-ff-03 = { version = "0.3.0", package = "ark-ff", optional = true, default-features = false }
ark-ff-04 = { version = "0.4.0", package = "ark-ff", optional = true, default-features = false }
bn-rs = { version = "0.2", optional = true, default-features = true }
fastrlp = { version = "0.3", optional = true, default-features = false }
num-bigint = { version = "0.4", optional = true, default-features = false }
parity-scale-codec = { version = "3", optional = true, features = [
"derive",
"max-encoded-len",
], default-features = false }
primitive-types = { version = "0.12", optional = true, default-features = false }
proptest = { version = "1", optional = true, default-features = false }
pyo3 = { version = "0.19", optional = true, default-features = false }
quickcheck = { version = "1", optional = true, default-features = false }
rand = { version = "0.8", optional = true, default-features = false }
rlp = { version = "0.5", optional = true, default-features = false }
serde = { version = "1", optional = true, default-features = false }
valuable = { version = "0.1", optional = true, default-features = false }
zeroize = { version = "1.6", optional = true, default-features = false }

# postgres
bytes = { version = "1.4", optional = true }
Expand All @@ -73,8 +77,8 @@ sqlx-core = { version = "0.7", optional = true }
[dev-dependencies]
ruint = { workspace = true, features = ["arbitrary", "proptest"] }

ark-bn254-04 = { version = "0.4.0", package = "ark-bn254" }
ark-bn254-03 = { version = "0.3.0", package = "ark-bn254" }
ark-bn254-04 = { version = "0.4.0", package = "ark-bn254" }

criterion = "0.5"
rand = "0.8"
Expand All @@ -89,28 +93,46 @@ proptest = "1.2"
serde_json = "1.0"

[features]
default = ["std"]
std = [
"alloy-rlp?/std",
"ark-ff-03?/std",
"ark-ff-04?/std",
"bytes?/std",
"fastrlp?/std",
"num-bigint?/std",
"parity-scale-codec?/std",
"primitive-types?/std",
"proptest?/std",
"rand?/std",
"rlp?/std",
"serde?/std",
"valuable?/std",
"zeroize?/std",
]

# nightly-only features
nightly = []
generic_const_exprs = ["nightly"]

# support
alloy-rlp = ["dep:alloy-rlp"]
arbitrary = ["dep:arbitrary"]
arbitrary = ["dep:arbitrary", "std"]
ark-ff = ["dep:ark-ff-03"]
ark-ff-04 = ["dep:ark-ff-04"]
bn-rs = ["dep:bn-rs"]
bn-rs = ["dep:bn-rs", "std"]
fastrlp = ["dep:fastrlp"]
num-bigint = ["dep:num-bigint"]
parity-scale-codec = ["dep:parity-scale-codec"]
primitive-types = ["dep:primitive-types"]
proptest = ["dep:proptest"]
pyo3 = ["dep:pyo3"]
quickcheck = ["dep:quickcheck"]
pyo3 = ["dep:pyo3", "std"]
quickcheck = ["dep:quickcheck", "std"]
rand = ["dep:rand"]
rlp = ["dep:rlp"]
serde = ["dep:serde"]
valuable = ["dep:valuable"]
zeroize = ["dep:zeroize"]

postgres = ["dep:postgres-types", "dep:bytes"]
sqlx = ["dep:sqlx-core"]
postgres = ["dep:postgres-types", "dep:bytes", "std", "dep:thiserror"]
sqlx = ["dep:sqlx-core", "std", "dep:thiserror"]
2 changes: 1 addition & 1 deletion benches/benches/algorithms/gcd.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::prelude::*;
use core::cmp::{max, min};
use ruint::algorithms::LehmerMatrix as Matrix;
use std::cmp::{max, min};

pub fn group(criterion: &mut Criterion) {
bench_from_u64(criterion);
Expand Down
4 changes: 2 additions & 2 deletions ruint-macro/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#![doc = include_str!("../README.md")]
#![warn(clippy::all, clippy::pedantic, clippy::cargo, clippy::nursery)]

use proc_macro::{Delimiter, Group, Ident, Literal, Punct, Spacing, Span, TokenStream, TokenTree};
use std::{
use core::{
fmt::{Display, Formatter, Write},
str::FromStr,
};
use proc_macro::{Delimiter, Group, Ident, Literal, Punct, Spacing, Span, TokenStream, TokenTree};

#[derive(Copy, Clone, PartialEq, Debug)]
enum LiteralBaseType {
Expand Down
3 changes: 2 additions & 1 deletion src/algorithms/div/knuth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,11 +194,12 @@ mod tests {
add::{cmp, sbb_n},
addmul,
};
use alloc::vec::Vec;
use core::cmp::Ordering;
use proptest::{
collection, num, proptest,
strategy::{Just, Strategy},
};
use std::cmp::Ordering;

// Basic test without exceptional paths
#[test]
Expand Down
2 changes: 1 addition & 1 deletion src/algorithms/div/reciprocal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
//! [new]: https://gmplib.org/list-archives/gmp-devel/2019-October/005590.html
#![allow(dead_code, clippy::cast_possible_truncation, clippy::cast_lossless)]

use std::num::Wrapping;
use core::num::Wrapping;

pub use self::{reciprocal_2_mg10 as reciprocal_2, reciprocal_mg10 as reciprocal};

Expand Down
2 changes: 1 addition & 1 deletion src/algorithms/gcd/matrix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -336,9 +336,9 @@ mod tests {
use core::{
cmp::{max, min},
mem::swap,
str::FromStr,
};
use proptest::{proptest, test_runner::Config};
use std::str::FromStr;

fn gcd(mut a: u128, mut b: u128) -> u128 {
while b != 0 {
Expand Down
2 changes: 1 addition & 1 deletion src/algorithms/gcd/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ mod tests {

#[test]
fn test_gcd_one() {
use std::str::FromStr;
use core::str::FromStr;
const BITS: usize = 129;
const LIMBS: usize = nlimbs(BITS);
type U = Uint<BITS, LIMBS>;
Expand Down
26 changes: 21 additions & 5 deletions src/base_convert.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,39 @@
use crate::Uint;
use thiserror::Error;
use alloc::vec::Vec;
use core::fmt;

/// Error for [`from_base_le`][Uint::from_base_le] and
/// [`from_base_be`][Uint::from_base_be].
#[allow(clippy::module_name_repetitions)]
#[derive(Clone, Copy, Debug, PartialEq, Eq, Error)]
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum BaseConvertError {
/// The value is too large to fit the target type.
#[error("The value is too large to fit the target type")]
Overflow,

/// The requested number base `.0` is less than two.
#[error("The requested number base {0} is less than two")]
InvalidBase(u64),

/// The provided digit `.0` is out of range for requested base `.1`.
#[error("digit {0} is out of range for base {1}")]
InvalidDigit(u64, u64),
}

#[cfg(feature = "std")]
impl std::error::Error for BaseConvertError {}

impl fmt::Display for BaseConvertError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Overflow => write!(f, "The value is too large to fit the target type"),
Self::InvalidBase(base) => {
write!(f, "The requested number base {base} is less than two")
}
Self::InvalidDigit(digit, base) => {
write!(f, "digit {digit} is out of range for base {base}")
}
}
}
}

impl<const BITS: usize, const LIMBS: usize> Uint<BITS, LIMBS> {
/// Returns an iterator over the base `base` digits of the number in
/// little-endian order.
Expand Down Expand Up @@ -68,6 +83,7 @@ impl<const BITS: usize, const LIMBS: usize> Uint<BITS, LIMBS> {
base: u64,
digits: I,
) -> Result<Self, BaseConvertError> {
// TODO: Do not allocate.
let mut digits: Vec<_> = digits.into_iter().collect();
digits.reverse();
Self::from_base_be(base, digits)
Expand Down
11 changes: 7 additions & 4 deletions src/bit_arr.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
use crate::{ParseError, Uint};
use core::ops::{
BitAnd, BitAndAssign, BitOr, BitOrAssign, BitXor, BitXorAssign, Index, Not, Shl, ShlAssign,
Shr, ShrAssign,
use alloc::{borrow::Cow, vec::Vec};
use core::{
ops::{
BitAnd, BitAndAssign, BitOr, BitOrAssign, BitXor, BitXorAssign, Index, Not, Shl, ShlAssign,
Shr, ShrAssign,
},
str::FromStr,
};
use std::{borrow::Cow, str::FromStr};

/// A newtype wrapper around [`Uint`] that restricts operations to those
/// relevant for bit arrays.
Expand Down
2 changes: 1 addition & 1 deletion src/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ use crate::{
utils::{trim_end_slice, trim_end_vec},
Uint,
};
use alloc::{borrow::Cow, vec::Vec};
use core::{
ptr::{addr_of, addr_of_mut},
slice,
};
use std::borrow::Cow;

// OPT: *_to_smallvec to avoid allocation.
impl<const BITS: usize, const LIMBS: usize> Uint<BITS, LIMBS> {
Expand Down
Loading