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
1 change: 1 addition & 0 deletions .github/workflows/elliptic-curve.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ jobs:
target: ${{ matrix.target }}
override: true
- run: cargo build --no-default-features --release --target ${{ matrix.target }}
- run: cargo build --no-default-features --release --target ${{ matrix.target }} --features arithmetic
test:
runs-on: ubuntu-latest
strategy:
Expand Down
1 change: 1 addition & 0 deletions Cargo.lock

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

10 changes: 6 additions & 4 deletions elliptic-curve/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ categories = ["cryptography", "no-std"]
keywords = ["crypto", "ecc", "elliptic", "weierstrass"]

[dependencies]
bitvec = { version = "0.18", optional = true, default-features = false }
digest = { version = "0.9", optional = true }
ff = { version = "0.8", default-features = false }
group = { version = "0.8", default-features = false }
ff = { version = "0.8", optional = true, default-features = false }
group = { version = "0.8", optional = true, 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", default-features = false }
Expand All @@ -28,9 +29,10 @@ zeroize = { version = "1", optional = true, default-features = false }
hex-literal = "0.2"

[features]
default = []
default = ["arithmetic"]
alloc = []
ecdh = ["zeroize"]
arithmetic = ["bitvec", "ff", "group"]
ecdh = ["arithmetic", "zeroize"]
std = ["alloc"]

[package.metadata.docs.rs]
Expand Down
39 changes: 30 additions & 9 deletions elliptic-curve/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,33 @@ extern crate std;
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 = "arithmetic")]
#[cfg_attr(docsrs, doc(cfg(feature = "arithmetic")))]
pub mod scalar;

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

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;

// TODO(tarcieri): source this via ff crate: https://github.com/zkcrypto/ff/pull/40
#[cfg(feature = "arithmetic")]
pub use bitvec::view::BitView;
#[cfg(feature = "arithmetic")]
pub use ff;
#[cfg(feature = "arithmetic")]
pub use group;

#[cfg(feature = "digest")]
pub use digest::{self, Digest};

Expand All @@ -54,15 +64,24 @@ pub use oid;
#[cfg(feature = "zeroize")]
pub use zeroize;

use core::{
fmt::Debug,
ops::{Add, Mul},
};
use core::{fmt::Debug, ops::Add};
use generic_array::{typenum::Unsigned, ArrayLength, GenericArray};
use rand_core::{CryptoRng, RngCore};
use subtle::{ConditionallySelectable, ConstantTimeEq, CtOption};
use subtle::{ConditionallySelectable, CtOption};

#[cfg(feature = "arithmetic")]
use bitvec::{array::BitArray, order::Lsb0};
#[cfg(feature = "arithmetic")]
use core::ops::Mul;
#[cfg(feature = "arithmetic")]
use subtle::ConstantTimeEq;

/// Bit representation of a scalar field element of a given curve.
#[cfg(feature = "arithmetic")]
#[cfg_attr(docsrs, doc(cfg(feature = "arithmetic")))]
pub type FieldBits<C> = BitArray<Lsb0, <<C as Arithmetic>::Scalar as ff::PrimeField>::ReprBits>;

/// Byte array containing a serialized scalar value (i.e. an integer)
/// Byte representation of a base/scalar field element of a given curve.
pub type ElementBytes<C> = GenericArray<u8, <C as Curve>::FieldSize>;

/// Elliptic curve.
Expand All @@ -83,6 +102,8 @@ pub trait Curve: Clone + Debug + Default + Eq + Ord + Send + Sync {
}

/// Elliptic curve with arithmetic implementation.
#[cfg(feature = "arithmetic")]
#[cfg_attr(docsrs, doc(cfg(feature = "arithmetic")))]
pub trait Arithmetic: Curve {
/// Scalar field element modulo the curve's order.
type Scalar: ff::PrimeField
Expand Down
2 changes: 2 additions & 0 deletions elliptic-curve/src/ops.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//! Traits for arithmetic operations on elliptic curve field elements

pub use core::ops::{Add, Mul};

use subtle::CtOption;

/// Perform an inversion on a field element (i.e. base field element or scalar)
Expand Down
21 changes: 13 additions & 8 deletions elliptic-curve/src/sec1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,26 @@
//!
//! <https://www.secg.org/sec1-v2.pdf>

use crate::{
point::Generator,
scalar::NonZeroScalar,
weierstrass::{point::Decompress, Curve},
Arithmetic, ElementBytes, Error, FromBytes, SecretKey,
};
use crate::{weierstrass::Curve, ElementBytes, Error};
use core::{
fmt::{self, Debug},
ops::{Add, Mul},
ops::Add,
};
use generic_array::{
typenum::{Unsigned, U1},
ArrayLength, GenericArray,
};
use subtle::{Choice, CtOption};
use subtle::CtOption;

#[cfg(feature = "alloc")]
use alloc::boxed::Box;

#[cfg(feature = "arithmetic")]
use crate::{
ops::Mul, point::Generator, scalar::NonZeroScalar, subtle::Choice,
weierstrass::point::Decompress, Arithmetic, FromBytes, SecretKey,
};

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

Expand Down Expand Up @@ -124,6 +125,8 @@ where
/// [`SecretKey`].
///
/// The `compress` flag requests point compression.
#[cfg(feature = "arithmetic")]
#[cfg_attr(docsrs, doc(cfg(feature = "arithmetic")))]
pub fn from_secret_key(secret_key: &SecretKey<C>, compress: bool) -> Result<Self, Error>
where
C: Arithmetic,
Expand Down Expand Up @@ -170,6 +173,8 @@ where
}

/// Decompress this [`EncodedPoint`], returning a new [`EncodedPoint`].
#[cfg(feature = "arithmetic")]
#[cfg_attr(docsrs, doc(cfg(feature = "arithmetic")))]
pub fn decompress(&self) -> CtOption<Self>
where
C: Arithmetic,
Expand Down
8 changes: 6 additions & 2 deletions elliptic-curve/src/secret_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,15 @@
//! 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 = "arithmetic")]
use crate::{scalar::NonZeroScalar, Arithmetic, Generate};
#[cfg(feature = "arithmetic")]
use rand_core::{CryptoRng, RngCore};

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

#[cfg(feature = "arithmetic")]
impl<C> Generate for SecretKey<C>
where
C: Curve + Arithmetic,
Expand All @@ -72,7 +76,7 @@ where
/// Generate a new [`SecretKey`]
fn generate(rng: impl CryptoRng + RngCore) -> Self {
Self {
scalar: C::Scalar::generate(rng).into(),
scalar: NonZeroScalar::<C>::generate(rng).into(),
}
}
}
Expand Down