From 21fd9e2c1bbd2d049bfe95588d77cb884e9f93ab Mon Sep 17 00:00:00 2001 From: Jack Grigg Date: Wed, 22 Dec 2021 05:41:04 +0000 Subject: [PATCH] Remove `std` feature flag Closes zcash/pasta_curves#27. --- Cargo.toml | 7 +++---- src/arithmetic.rs | 2 -- src/arithmetic/curves.rs | 36 ++++++++++++++++++------------------ src/curves.rs | 22 ++++++++++++---------- src/lib.rs | 6 +++--- src/pallas.rs | 8 ++++---- src/vesta.rs | 4 ++-- 7 files changed, 42 insertions(+), 43 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index bbbe635..b5f27ee 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -25,7 +25,7 @@ rand_xorshift = "0.3" [[bench]] name = "hashtocurve" harness = false -required-features = ["std"] +required-features = ["alloc"] [[bench]] name = "fp" @@ -38,7 +38,7 @@ harness = false [[bench]] name = "point" harness = false -required-features = ["std"] +required-features = ["alloc"] [dependencies] blake2b_simd = { version = "0.5", default-features = false } @@ -52,8 +52,7 @@ subtle = { version = "2.3", default-features = false } lazy_static = { version = "1.4.0", optional = true } [features] -default = ["bits", "sqrt-table", "std"] +default = ["bits", "sqrt-table"] alloc = ["group/alloc"] bits = ["ff/bits"] sqrt-table = ["alloc", "lazy_static"] -std = ["alloc"] diff --git a/src/arithmetic.rs b/src/arithmetic.rs index e96a074..0de6600 100644 --- a/src/arithmetic.rs +++ b/src/arithmetic.rs @@ -7,8 +7,6 @@ mod curves; mod fields; -pub(crate) use fields::*; - pub use curves::*; pub use fields::*; diff --git a/src/arithmetic/curves.rs b/src/arithmetic/curves.rs index 2ada7bc..ca94e58 100644 --- a/src/arithmetic/curves.rs +++ b/src/arithmetic/curves.rs @@ -1,28 +1,26 @@ //! This module contains the `Curve`/`CurveAffine` abstractions that allow us to //! write code that generalizes over a pair of groups. -#[cfg(feature = "std")] +#[cfg(feature = "alloc")] use group::prime::{PrimeCurve, PrimeCurveAffine}; -#[cfg(feature = "std")] +#[cfg(feature = "alloc")] use subtle::{Choice, ConditionallySelectable, ConstantTimeEq, CtOption}; -#[cfg(feature = "std")] +#[cfg(feature = "alloc")] use super::{FieldExt, Group}; -#[cfg(feature = "std")] -use std::{ - boxed::Box, - ops::{Add, Mul, Sub}, -}; +#[cfg(feature = "alloc")] +use alloc::boxed::Box; +#[cfg(feature = "alloc")] +use core::ops::{Add, Mul, Sub}; /// This trait is a common interface for dealing with elements of an elliptic /// curve group in a "projective" form, where that arithmetic is usually more /// efficient. /// -/// Currently requires the `std` feature flag because of `hash_to_curve`, and -/// `CurveAffine::{read, write}`. -#[cfg(feature = "std")] -#[cfg_attr(docsrs, doc(cfg(feature = "std")))] +/// Requires the `alloc` feature flag because of `hash_to_curve`. +#[cfg(feature = "alloc")] +#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))] pub trait CurveExt: PrimeCurve::AffineExt> + group::Group::ScalarExt> @@ -89,8 +87,10 @@ pub trait CurveExt: /// This trait is the affine counterpart to `Curve` and is used for /// serialization, storage in memory, and inspection of $x$ and $y$ coordinates. -#[cfg(feature = "std")] -#[cfg_attr(docsrs, doc(cfg(feature = "std")))] +/// +/// Requires the `alloc` feature flag because of `hash_to_curve` on [`CurveExt`]. +#[cfg(feature = "alloc")] +#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))] pub trait CurveAffine: PrimeCurveAffine< Scalar = ::ScalarExt, @@ -130,15 +130,15 @@ pub trait CurveAffine: } /// The affine coordinates of a point on an elliptic curve. -#[cfg(feature = "std")] -#[cfg_attr(docsrs, doc(cfg(feature = "std")))] +#[cfg(feature = "alloc")] +#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))] #[derive(Clone, Copy, Debug, Default)] pub struct Coordinates { pub(crate) x: C::Base, pub(crate) y: C::Base, } -#[cfg(feature = "std")] +#[cfg(feature = "alloc")] impl Coordinates { /// Returns the x-coordinate. /// @@ -169,7 +169,7 @@ impl Coordinates { } } -#[cfg(feature = "std")] +#[cfg(feature = "alloc")] impl ConditionallySelectable for Coordinates { fn conditional_select(a: &Self, b: &Self, choice: Choice) -> Self { Coordinates { diff --git a/src/curves.rs b/src/curves.rs index 7a0db20..91c489c 100644 --- a/src/curves.rs +++ b/src/curves.rs @@ -6,8 +6,8 @@ use core::fmt; use core::iter::Sum; use core::ops::{Add, Mul, Neg, Sub}; -#[cfg(feature = "std")] -use std::boxed::Box; +#[cfg(feature = "alloc")] +use alloc::boxed::Box; use ff::{Field, PrimeField}; use group::{ @@ -19,9 +19,10 @@ use rand::RngCore; use subtle::{Choice, ConditionallySelectable, ConstantTimeEq, CtOption}; use super::{Fp, Fq}; +use crate::arithmetic::Group; -#[cfg(feature = "std")] -use crate::arithmetic::{Coordinates, CurveAffine, CurveExt, FieldExt, Group}; +#[cfg(feature = "alloc")] +use crate::arithmetic::{Coordinates, CurveAffine, CurveExt, FieldExt}; macro_rules! new_curve_impl { (($($privacy:tt)*), $name:ident, $name_affine:ident, $iso:ident, $base:ident, $scalar:ident, @@ -102,8 +103,8 @@ macro_rules! new_curve_impl { } } - #[cfg(feature = "std")] - #[cfg_attr(docsrs, doc(cfg(feature = "std")))] + #[cfg(feature = "alloc")] + #[cfg_attr(docsrs, doc(cfg(feature = "alloc")))] impl group::WnafGroup for $name { fn recommended_wnaf_for_num_scalars(num_scalars: usize) -> usize { // Copied from bls12_381::g1, should be updated. @@ -123,7 +124,8 @@ macro_rules! new_curve_impl { } } - #[cfg(feature = "std")] + #[cfg(feature = "alloc")] + #[cfg_attr(docsrs, doc(cfg(feature = "alloc")))] impl CurveExt for $name { type ScalarExt = $scalar; type Base = $base; @@ -695,7 +697,8 @@ macro_rules! new_curve_impl { } } - #[cfg(feature = "std")] + #[cfg(feature = "alloc")] + #[cfg_attr(docsrs, doc(cfg(feature = "alloc")))] impl CurveAffine for $name_affine { type ScalarExt = $scalar; type Base = $base; @@ -779,7 +782,6 @@ macro_rules! new_curve_impl { impl_binops_multiplicative!($name, $scalar); impl_binops_multiplicative_mixed!($name_affine, $scalar, $name); - #[cfg(feature = "std")] impl Group for $name { type Scalar = $scalar; @@ -880,7 +882,7 @@ macro_rules! impl_projective_curve_specific { }; } -#[cfg(feature = "std")] +#[cfg(feature = "alloc")] macro_rules! impl_projective_curve_ext { ($name:ident, $iso:ident, $base:ident, special_a0_b5) => { fn hash_to_curve<'a>(domain_prefix: &'a str) -> Box Self + 'a> { diff --git a/src/lib.rs b/src/lib.rs index c87d047..04ebf0b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -12,7 +12,7 @@ #[cfg(feature = "alloc")] extern crate alloc; -#[cfg(any(feature = "std", test))] +#[cfg(test)] #[macro_use] extern crate std; @@ -25,7 +25,7 @@ pub mod arithmetic; pub mod pallas; pub mod vesta; -#[cfg(feature = "std")] +#[cfg(feature = "alloc")] mod hashtocurve; pub use curves::*; @@ -33,7 +33,7 @@ pub use fields::*; pub extern crate group; -#[cfg(feature = "std")] +#[cfg(feature = "alloc")] #[test] fn test_endo_consistency() { use crate::arithmetic::{CurveExt, FieldExt}; diff --git a/src/pallas.rs b/src/pallas.rs index c9754ee..b24a72f 100644 --- a/src/pallas.rs +++ b/src/pallas.rs @@ -14,7 +14,7 @@ pub type Point = Ep; /// A Pallas point in the affine coordinate space (or the point at infinity). pub type Affine = EpAffine; -#[cfg(feature = "std")] +#[cfg(feature = "alloc")] #[test] #[allow(clippy::many_single_char_names)] fn test_iso_map() { @@ -65,7 +65,7 @@ fn test_iso_map() { assert!(p2 == p.double()); } -#[cfg(feature = "std")] +#[cfg(feature = "alloc")] #[test] fn test_iso_map_identity() { use crate::arithmetic::CurveExt; @@ -100,7 +100,7 @@ fn test_iso_map_identity() { assert!(bool::from(p.is_identity())); } -#[cfg(feature = "std")] +#[cfg(feature = "alloc")] #[test] fn test_map_to_curve_simple_swu() { use crate::arithmetic::CurveExt; @@ -135,7 +135,7 @@ fn test_map_to_curve_simple_swu() { ); } -#[cfg(feature = "std")] +#[cfg(feature = "alloc")] #[test] fn test_hash_to_curve() { use crate::arithmetic::CurveExt; diff --git a/src/vesta.rs b/src/vesta.rs index cafee9e..965dbd9 100644 --- a/src/vesta.rs +++ b/src/vesta.rs @@ -14,7 +14,7 @@ pub type Point = Eq; /// A Vesta point in the affine coordinate space (or the point at infinity). pub type Affine = EqAffine; -#[cfg(feature = "std")] +#[cfg(feature = "alloc")] #[test] fn test_map_to_curve_simple_swu() { use crate::arithmetic::CurveExt; @@ -49,7 +49,7 @@ fn test_map_to_curve_simple_swu() { ); } -#[cfg(feature = "std")] +#[cfg(feature = "alloc")] #[test] fn test_hash_to_curve() { use crate::arithmetic::CurveExt;