Skip to content

Commit

Permalink
elliptic-curve: add ff and group crate dependencies
Browse files Browse the repository at this point in the history
Adds the `ff` and `group` crates as dependencies, with the goal of
replacing some of the existing traits and trait relationships in
`elliptic-curve` with ones from `ff`/`group`.

This PR also removes the `rand` and `weierstrass` features from the
`elliptic-curve` crate, making `rand_core` a hard requirement, since it
seems at least `rand_core` will probably be a hard requirement for
`group`, and requiring an RNG as a hard dependency makes sense.
  • Loading branch information
tarcieri committed Sep 4, 2020
1 parent 3df75fc commit 7c78793
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 49 deletions.
1 change: 0 additions & 1 deletion .github/workflows/elliptic-curve.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,5 @@ jobs:
toolchain: ${{ matrix.rust }}
- run: cargo check --all-features
- run: cargo test --no-default-features
- run: cargo test --no-default-features --features weierstrass
- run: cargo test
- run: cargo test --all-features
60 changes: 56 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,7 @@ members = [
"stream-cipher",
"universal-hash",
]

[patch.crates-io]
ff = { git = "https://github.com/zkcrypto/ff.git" }
group = { git = "https://github.com/zkcrypto/group.git" }
10 changes: 5 additions & 5 deletions elliptic-curve/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,21 @@ keywords = ["crypto", "ecc", "elliptic", "weierstrass"]

[dependencies]
digest = { version = "0.9", optional = true }
ff = { version = "0.7", default-features = false }
group = { version = "0.7", default-features = false }
generic-array = { version = "0.14", default-features = false }
oid = { package = "const-oid", version = "0.1", optional = true }
rand_core = { version = "0.5", optional = true, default-features = false }
rand_core = { version = "0.5", default-features = false }
subtle = { version = "2.2", default-features = false }
zeroize = { version = "1", optional = true, default-features = false }

[dev-dependencies]
hex-literal = "0.2"

[features]
default = ["rand"]
default = []
alloc = []
ecdh = ["rand", "weierstrass", "zeroize"]
rand = ["rand_core"]
weierstrass = []
ecdh = ["zeroize"]
std = ["alloc"]

[package.metadata.docs.rs]
Expand Down
38 changes: 14 additions & 24 deletions elliptic-curve/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,23 +29,20 @@ pub mod error;
pub mod ops;
pub mod point;
pub mod scalar;
pub mod sec1;
pub mod secret_key;
pub mod util;
pub mod weierstrass;

#[cfg(feature = "ecdh")]
#[cfg_attr(docsrs, doc(cfg(feature = "ecdh")))]
pub mod ecdh;

#[cfg(feature = "weierstrass")]
#[cfg_attr(docsrs, doc(cfg(feature = "weierstrass")))]
pub mod sec1;

#[cfg(feature = "weierstrass")]
#[cfg_attr(docsrs, doc(cfg(feature = "weierstrass")))]
pub mod weierstrass;

pub use self::{error::Error, secret_key::SecretKey};
pub use ff;
pub use generic_array::{self, typenum::consts};
pub use group;
pub use rand_core;
pub use subtle;

#[cfg(feature = "digest")]
Expand All @@ -54,9 +51,6 @@ pub use digest::{self, Digest};
#[cfg(feature = "oid")]
pub use oid;

#[cfg(feature = "rand")]
pub use rand_core;

#[cfg(feature = "zeroize")]
pub use zeroize;

Expand All @@ -65,10 +59,8 @@ use core::{
ops::{Add, Mul},
};
use generic_array::{typenum::Unsigned, ArrayLength, GenericArray};
use subtle::{ConditionallySelectable, ConstantTimeEq, CtOption};

#[cfg(feature = "rand")]
use rand_core::{CryptoRng, RngCore};
use subtle::{ConditionallySelectable, ConstantTimeEq, CtOption};

/// Byte array containing a serialized scalar value (i.e. an integer)
pub type ElementBytes<C> = GenericArray<u8, <C as Curve>::FieldSize>;
Expand Down Expand Up @@ -114,6 +106,14 @@ pub trait FromBytes: ConditionallySelectable + Sized {
fn from_bytes(bytes: &GenericArray<u8, Self::Size>) -> CtOption<Self>;
}

/// Randomly generate a value.
///
/// Primarily intended for use with scalar types for a particular curve.
pub trait Generate {
/// Generate a random element of this type using the provided [`CryptoRng`]
fn generate(rng: impl CryptoRng + RngCore) -> Self;
}

/// Instantiate this type from the output of a digest.
///
/// This can be used for implementing hash-to-scalar (e.g. as in ECDSA) or
Expand All @@ -127,16 +127,6 @@ pub trait FromDigest<C: Curve> {
D: Digest<OutputSize = C::FieldSize>;
}

/// Randomly generate a value.
///
/// Primarily intended for use with scalar types for a particular curve.
#[cfg(feature = "rand")]
#[cfg_attr(docsrs, doc(cfg(feature = "rand")))]
pub trait Generate {
/// Generate a random element of this type using the provided [`CryptoRng`]
fn generate(rng: impl CryptoRng + RngCore) -> Self;
}

/// Associate an object identifier (OID) with a curve
#[cfg(feature = "oid")]
#[cfg_attr(docsrs, doc(cfg(feature = "oid")))]
Expand Down
11 changes: 4 additions & 7 deletions elliptic-curve/src/scalar.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
//! Scalar types

use crate::{ops::Invert, Arithmetic, Curve, ElementBytes, FromBytes};
use core::ops::Deref;
use subtle::{Choice, ConditionallySelectable, ConstantTimeEq, CtOption};

#[cfg(feature = "rand")]
use crate::{
ops::Invert,
rand_core::{CryptoRng, RngCore},
Generate,
Arithmetic, Curve, ElementBytes, FromBytes, Generate,
};
use core::ops::Deref;
use subtle::{Choice, ConditionallySelectable, ConstantTimeEq, CtOption};

#[cfg(feature = "zeroize")]
use zeroize::Zeroize;
Expand Down Expand Up @@ -109,7 +107,6 @@ where
}
}

#[cfg(feature = "rand")]
impl<C> Generate for NonZeroScalar<C>
where
C: Curve + Arithmetic,
Expand Down
10 changes: 2 additions & 8 deletions elliptic-curve/src/secret_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,13 @@
//! zeroing it out of memory securely on drop.

use crate::{error::Error, Curve, ElementBytes};
use crate::{Arithmetic, Generate};
use core::{
convert::{TryFrom, TryInto},
fmt::{self, Debug},
};
use generic_array::{typenum::Unsigned, GenericArray};

#[cfg(feature = "rand")]
use {
crate::{Arithmetic, Generate},
rand_core::{CryptoRng, RngCore},
};
use rand_core::{CryptoRng, RngCore};

/// Elliptic curve secret keys.
///
Expand Down Expand Up @@ -68,8 +64,6 @@ impl<C: Curve> Debug for SecretKey<C> {
}
}

#[cfg(feature = "rand")]
#[cfg_attr(docsrs, doc(cfg(feature = "rand")))]
impl<C> Generate for SecretKey<C>
where
C: Curve + Arithmetic,
Expand Down

0 comments on commit 7c78793

Please sign in to comment.