Skip to content

Commit

Permalink
Remove std feature
Browse files Browse the repository at this point in the history
All of the existing usages of `std` can be replaced with `alloc`.

They are legacy usages from before when liballoc was stabilized.
  • Loading branch information
tarcieri committed Dec 8, 2022
1 parent 6b56edf commit 58866c5
Show file tree
Hide file tree
Showing 17 changed files with 35 additions and 79 deletions.
7 changes: 3 additions & 4 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,9 @@ jobs:
steps:
- uses: actions/checkout@v3
- uses: dtolnay/rust-toolchain@stable
# This filter selects no benchmarks, so we don't run any, only build them.
- name: Build u32 bench
run: env RUSTFLAGS="--cfg curve25519_dalek_bits=\"32\"" cargo bench "nonexistentbenchmark"
run: env RUSTFLAGS="--cfg curve25519_dalek_bits=\"32\"" cargo build --benches
- name: Build u64 bench
run: env RUSTFLAGS="--cfg curve25519_dalek_bits=\"64\"" cargo bench "nonexistentbenchmark"
run: env RUSTFLAGS="--cfg curve25519_dalek_bits=\"64\"" cargo build --benches
- name: Build default (host native) bench
run: cargo bench "nonexistentbenchmark"
run: cargo build --benches
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,12 @@ bincode = "1"
criterion = { version = "0.4.0", features = ["html_reports"] }
hex = "0.4.2"
rand = "0.8"
rand_core = { version = "0.6", default-features = false }
rand_core = { version = "0.6", default-features = false, features = ["getrandom"] }

[[bench]]
name = "dalek_benchmarks"
harness = false
required-features = ["rand_core"]

[dependencies]
cfg-if = "1"
Expand All @@ -56,8 +57,7 @@ fiat-crypto = { version = "0.1.6", optional = true}

[features]
nightly = ["subtle/nightly"]
default = ["std"]
std = ["alloc", "subtle/std", "rand_core/std"]
default = ["alloc"]
alloc = ["zeroize/alloc"]

# fiat-crypto backend with formally-verified field arithmetic
Expand Down
7 changes: 2 additions & 5 deletions src/backend/serial/scalar_mul/pippenger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,15 @@
#![allow(non_snake_case)]

use alloc::vec::Vec;

use core::borrow::Borrow;
use core::cmp::Ordering;

use crate::edwards::EdwardsPoint;
use crate::scalar::Scalar;
use crate::traits::VartimeMultiscalarMul;

#[allow(unused_imports)]
use crate::prelude::*;

/// Implements a version of Pippenger's algorithm.
///
/// The algorithm works as follows:
Expand Down Expand Up @@ -62,8 +61,6 @@ use crate::prelude::*;
/// This algorithm is adapted from section 4 of <https://eprint.iacr.org/2012/549.pdf>.
pub struct Pippenger;

#[cfg(feature = "alloc")]
#[cfg_attr(docsrs, doc(cfg(any(feature = "alloc", feature = "std"))))]
impl VartimeMultiscalarMul for Pippenger {
type Point = EdwardsPoint;

Expand Down
5 changes: 2 additions & 3 deletions src/backend/serial/scalar_mul/precomputed_straus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
#![allow(non_snake_case)]

use alloc::vec::Vec;

use core::borrow::Borrow;
use core::cmp::Ordering;

Expand All @@ -23,9 +25,6 @@ use crate::traits::Identity;
use crate::traits::VartimePrecomputedMultiscalarMul;
use crate::window::{NafLookupTable5, NafLookupTable8};

#[allow(unused_imports)]
use crate::prelude::*;

#[allow(missing_docs)]
pub struct VartimePrecomputedStraus {
static_lookup_tables: Vec<NafLookupTable8<AffineNielsPoint>>,
Expand Down
5 changes: 2 additions & 3 deletions src/backend/serial/scalar_mul/straus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
#![allow(non_snake_case)]

use alloc::vec::Vec;

use core::borrow::Borrow;
use core::cmp::Ordering;

Expand All @@ -21,9 +23,6 @@ use crate::scalar::Scalar;
use crate::traits::MultiscalarMul;
use crate::traits::VartimeMultiscalarMul;

#[allow(unused_imports)]
use crate::prelude::*;

/// Perform multiscalar multiplication by the interleaved window
/// method, also known as Straus' method (since it was apparently
/// [first published][solution] by Straus in 1964, as a solution to [a
Expand Down
2 changes: 1 addition & 1 deletion src/backend/vector/ifma/edwards.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

use crate::traits::Identity;

use std::ops::{Add, Neg, Sub};
use core::ops::{Add, Neg, Sub};

use subtle::Choice;
use subtle::ConditionallySelectable;
Expand Down
3 changes: 0 additions & 3 deletions src/backend/vector/scalar_mul/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,10 @@ pub mod variable_base;
pub mod vartime_double_base;

#[cfg(feature = "alloc")]
#[cfg_attr(docsrs, doc(cfg(any(feature = "alloc", feature = "std"))))]
pub mod straus;

#[cfg(feature = "alloc")]
#[cfg_attr(docsrs, doc(cfg(any(feature = "alloc", feature = "std"))))]
pub mod precomputed_straus;

#[cfg(feature = "alloc")]
#[cfg_attr(docsrs, doc(cfg(any(feature = "alloc", feature = "std"))))]
pub mod pippenger;
7 changes: 2 additions & 5 deletions src/backend/vector/scalar_mul/pippenger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

#![allow(non_snake_case)]

use alloc::vec::Vec;

use core::borrow::Borrow;
use core::cmp::Ordering;

Expand All @@ -17,16 +19,11 @@ use crate::edwards::EdwardsPoint;
use crate::scalar::Scalar;
use crate::traits::{Identity, VartimeMultiscalarMul};

#[allow(unused_imports)]
use crate::prelude::*;

/// Implements a version of Pippenger's algorithm.
///
/// See the documentation in the serial `scalar_mul::pippenger` module for details.
pub struct Pippenger;

#[cfg(feature = "alloc")]
#[cfg_attr(docsrs, doc(cfg(any(feature = "alloc", feature = "std"))))]
impl VartimeMultiscalarMul for Pippenger {
type Point = EdwardsPoint;

Expand Down
5 changes: 2 additions & 3 deletions src/backend/vector/scalar_mul/precomputed_straus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
#![allow(non_snake_case)]

use alloc::vec::Vec;

use core::borrow::Borrow;
use core::cmp::Ordering;

Expand All @@ -21,9 +23,6 @@ use crate::traits::Identity;
use crate::traits::VartimePrecomputedMultiscalarMul;
use crate::window::{NafLookupTable5, NafLookupTable8};

#[allow(unused_imports)]
use crate::prelude::*;

pub struct VartimePrecomputedStraus {
static_lookup_tables: Vec<NafLookupTable8<CachedPoint>>,
}
Expand Down
5 changes: 2 additions & 3 deletions src/backend/vector/scalar_mul/straus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

#![allow(non_snake_case)]

use alloc::vec::Vec;

use core::borrow::Borrow;
use core::cmp::Ordering;

Expand All @@ -22,9 +24,6 @@ use crate::scalar::Scalar;
use crate::traits::{Identity, MultiscalarMul, VartimeMultiscalarMul};
use crate::window::{LookupTable, NafLookupTable5};

#[allow(unused_imports)]
use crate::prelude::*;

/// Multiscalar multiplication using interleaved window / Straus'
/// method. See the `Straus` struct in the serial backend for more
/// details.
Expand Down
10 changes: 3 additions & 7 deletions src/edwards.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,6 @@ use crate::window::LookupTableRadix256;
use crate::window::LookupTableRadix32;
use crate::window::LookupTableRadix64;

#[allow(unused_imports)]
use crate::prelude::*;

use crate::traits::BasepointTable;
use crate::traits::ValidityCheck;
use crate::traits::{Identity, IsIdentity};
Expand Down Expand Up @@ -710,7 +707,6 @@ impl<'a, 'b> Mul<&'b EdwardsPoint> for &'a Scalar {
// forward to a specific backend implementation.

#[cfg(feature = "alloc")]
#[cfg_attr(docsrs, doc(cfg(any(feature = "alloc", feature = "std"))))]
impl MultiscalarMul for EdwardsPoint {
type Point = EdwardsPoint;

Expand Down Expand Up @@ -743,7 +739,6 @@ impl MultiscalarMul for EdwardsPoint {
}

#[cfg(feature = "alloc")]
#[cfg_attr(docsrs, doc(cfg(any(feature = "alloc", feature = "std"))))]
impl VartimeMultiscalarMul for EdwardsPoint {
type Point = EdwardsPoint;

Expand Down Expand Up @@ -783,11 +778,9 @@ impl VartimeMultiscalarMul for EdwardsPoint {
// decouple stability of the inner type from the stability of the
// outer type.
#[cfg(feature = "alloc")]
#[cfg_attr(docsrs, doc(cfg(any(feature = "alloc", feature = "std"))))]
pub struct VartimeEdwardsPrecomputation(scalar_mul::precomputed_straus::VartimePrecomputedStraus);

#[cfg(feature = "alloc")]
#[cfg_attr(docsrs, doc(cfg(any(feature = "alloc", feature = "std"))))]
impl VartimePrecomputedMultiscalarMul for VartimeEdwardsPrecomputation {
type Point = EdwardsPoint;

Expand Down Expand Up @@ -1126,6 +1119,9 @@ mod test {
use crate::scalar::Scalar;
use subtle::ConditionallySelectable;

#[cfg(feature = "alloc")]
use alloc::vec::Vec;

/// X coordinate of the basepoint.
/// = 15112221349535400772501151409588531511454012693041857206046113283949847762202
static BASE_X_COORD_BYTES: [u8; 32] = [
Expand Down
1 change: 0 additions & 1 deletion src/field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,6 @@ impl FieldElement {
///
/// When an input `FieldElement` is zero, its value is unchanged.
#[cfg(feature = "alloc")]
#[cfg_attr(docsrs, doc(cfg(any(feature = "alloc", feature = "std"))))]
pub fn batch_invert(inputs: &mut [FieldElement]) {
// Montgomery’s Trick and Fast Implementation of Masked AES
// Genelle, Prouff and Quisquater
Expand Down
7 changes: 2 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
#[macro_use]
extern crate alloc;

#[cfg(feature = "std")]
// TODO: move std-dependent tests to `tests/`
#[cfg(test)]
#[macro_use]
extern crate std;

Expand Down Expand Up @@ -78,10 +79,6 @@ pub mod backend;
#[cfg(not(docsrs))]
pub(crate) mod backend;

// Crate-local prelude (for alloc-dependent features like `Vec`)

pub(crate) mod prelude;

// Generic code for window lookups
pub(crate) mod window;

Expand Down
7 changes: 5 additions & 2 deletions src/montgomery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,9 @@ mod test {
use super::*;
use crate::constants;

#[cfg(feature = "alloc")]
use alloc::vec::Vec;

#[cfg(feature = "rand_core")]
use rand_core::OsRng;

Expand Down Expand Up @@ -480,9 +483,9 @@ mod test {
];

#[test]
#[cfg(feature = "alloc")] // Vec
#[cfg(feature = "alloc")]
fn montgomery_elligator_correct() {
let bytes: alloc::vec::Vec<u8> = (0u8..32u8).collect();
let bytes: Vec<u8> = (0u8..32u8).collect();
let bits_in: [u8; 32] = (&bytes[..]).try_into().expect("Range invariant broken");

let fe = FieldElement::from_bytes(&bits_in);
Expand Down
19 changes: 0 additions & 19 deletions src/prelude.rs

This file was deleted.

11 changes: 3 additions & 8 deletions src/ristretto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,9 @@
//! [ristretto_main]:
//! https://ristretto.group/
#[cfg(feature = "alloc")]
use alloc::vec::Vec;

use core::borrow::Borrow;
use core::fmt::Debug;
use core::iter::Sum;
Expand Down Expand Up @@ -186,9 +189,6 @@ use zeroize::Zeroize;
use crate::edwards::EdwardsBasepointTable;
use crate::edwards::EdwardsPoint;

#[allow(unused_imports)]
use crate::prelude::*;

use crate::scalar::Scalar;

use crate::traits::BasepointTable;
Expand Down Expand Up @@ -525,7 +525,6 @@ impl RistrettoPoint {
/// # }
/// ```
#[cfg(feature = "alloc")]
#[cfg_attr(docsrs, doc(cfg(any(feature = "alloc", feature = "std"))))]
pub fn double_and_compress_batch<'a, I>(points: I) -> Vec<CompressedRistretto>
where
I: IntoIterator<Item = &'a RistrettoPoint>,
Expand Down Expand Up @@ -932,7 +931,6 @@ define_mul_variants!(LHS = Scalar, RHS = RistrettoPoint, Output = RistrettoPoint
// forward to the EdwardsPoint implementations.

#[cfg(feature = "alloc")]
#[cfg_attr(docsrs, doc(cfg(any(feature = "alloc", feature = "std"))))]
impl MultiscalarMul for RistrettoPoint {
type Point = RistrettoPoint;

Expand All @@ -949,7 +947,6 @@ impl MultiscalarMul for RistrettoPoint {
}

#[cfg(feature = "alloc")]
#[cfg_attr(docsrs, doc(cfg(any(feature = "alloc", feature = "std"))))]
impl VartimeMultiscalarMul for RistrettoPoint {
type Point = RistrettoPoint;

Expand All @@ -970,11 +967,9 @@ impl VartimeMultiscalarMul for RistrettoPoint {
// decouple stability of the inner type from the stability of the
// outer type.
#[cfg(feature = "alloc")]
#[cfg_attr(docsrs, doc(cfg(any(feature = "alloc", feature = "std"))))]
pub struct VartimeRistrettoPrecomputation(scalar_mul::precomputed_straus::VartimePrecomputedStraus);

#[cfg(feature = "alloc")]
#[cfg_attr(docsrs, doc(cfg(any(feature = "alloc", feature = "std"))))]
impl VartimePrecomputedMultiscalarMul for VartimeRistrettoPrecomputation {
type Point = RistrettoPoint;

Expand Down
7 changes: 3 additions & 4 deletions src/scalar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,6 @@ use core::ops::{Add, AddAssign};
use core::ops::{Mul, MulAssign};
use core::ops::{Sub, SubAssign};

#[allow(unused_imports)]
use crate::prelude::*;

use cfg_if::cfg_if;

#[cfg(feature = "rand_core")]
Expand Down Expand Up @@ -797,7 +794,6 @@ impl Scalar {
/// # }
/// ```
#[cfg(feature = "alloc")]
#[cfg_attr(docsrs, doc(cfg(any(feature = "alloc", feature = "std"))))]
pub fn batch_invert(inputs: &mut [Scalar]) -> Scalar {
// This code is essentially identical to the FieldElement
// implementation, and is documented there. Unfortunately,
Expand Down Expand Up @@ -1257,6 +1253,9 @@ mod test {
use super::*;
use crate::constants;

#[cfg(feature = "alloc")]
use alloc::vec::Vec;

/// x = 2238329342913194256032495932344128051776374960164957527413114840482143558222
pub static X: Scalar = Scalar {
bytes: [
Expand Down

0 comments on commit 58866c5

Please sign in to comment.