Skip to content

Commit

Permalink
feat: no_std
Browse files Browse the repository at this point in the history
  • Loading branch information
DaniPopes committed Jul 27, 2023
1 parent 4b04138 commit cf74d22
Show file tree
Hide file tree
Showing 24 changed files with 207 additions and 67 deletions.
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 @@ -42,26 +42,29 @@ path = "benches/bench.rs"
[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 +76,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 +92,47 @@ proptest = "1.2"
serde_json = "1.0"

[features]
default = ["std"]
std = [
"thiserror",
"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"]
sqlx = ["dep:sqlx-core", "std"]
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: 4 additions & 0 deletions benches/benches/log.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
use crate::prelude::*;

pub fn group(criterion: &mut Criterion) {
#[cfg(not(feature = "std"))]
let _ = criterion;
#[cfg(feature = "std")]
const_for!(BITS in BENCH {
const LIMBS: usize = nlimbs(BITS);
bench_log::<BITS, LIMBS>(criterion);
});
}

#[cfg(feature = "std")]
fn bench_log<const BITS: usize, const LIMBS: usize>(criterion: &mut Criterion) {
if BITS < 7 {
return;
Expand Down
4 changes: 4 additions & 0 deletions benches/benches/root.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use crate::prelude::*;

pub fn group(criterion: &mut Criterion) {
#[cfg(not(feature = "std"))]
let _ = criterion;
#[cfg(feature = "std")]
const_for!(BITS in BENCH {
const LIMBS: usize = nlimbs(BITS);
bench_root::<BITS, LIMBS>(criterion, 2);
Expand All @@ -10,6 +13,7 @@ pub fn group(criterion: &mut Criterion) {
});
}

#[cfg(feature = "std")]
fn bench_root<const BITS: usize, const LIMBS: usize>(criterion: &mut Criterion, degree: usize) {
let input = Uint::<BITS, LIMBS>::arbitrary();
let mut runner = TestRunner::deterministic();
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")

Check warning on line 28 in src/base_convert.rs

View check run for this annotation

Codecov / codecov/patch

src/base_convert.rs#L24-L28

Added lines #L24 - L28 were not covered by tests
}
Self::InvalidDigit(digit, base) => {
write!(f, "digit {digit} is out of range for base {base}")

Check warning on line 31 in src/base_convert.rs

View check run for this annotation

Codecov / codecov/patch

src/base_convert.rs#L30-L31

Added lines #L30 - L31 were not covered by tests
}
}
}

Check warning on line 34 in src/base_convert.rs

View check run for this annotation

Codecov / codecov/patch

src/base_convert.rs#L34

Added line #L34 was not covered by tests
}

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

0 comments on commit cf74d22

Please sign in to comment.