Skip to content

Commit

Permalink
[WIP] elliptic-curve: simplify EncodedPoint
Browse files Browse the repository at this point in the history
Changes `EncodedPoint<C>` to be backed by a byte slice which is always
the size of a SEC-1 encoded uncompressed point, eliminating the previous
`CompressedPoint` and `UncompressedPoint` types.

This allows for simplified trait bounds as we no longer need to care
about `CompressedPointSize` in the bounds.
  • Loading branch information
tarcieri committed Aug 20, 2020
1 parent 653f28f commit 337e233
Show file tree
Hide file tree
Showing 4 changed files with 167 additions and 397 deletions.
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.

3 changes: 3 additions & 0 deletions elliptic-curve/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ rand_core = { version = "0.5", optional = true, 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"]
ecdh = ["rand", "weierstrass", "zeroize"]
Expand Down
45 changes: 13 additions & 32 deletions elliptic-curve/src/ecdh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,14 @@

use crate::{
consts::U1,
generic_array::ArrayLength,
point::Generator,
scalar::NonZeroScalar,
sec1::{
self, CompressedPoint, CompressedPointSize, FromEncodedPoint, UncompressedPoint,
UncompressedPointSize,
},
sec1::{self, FromEncodedPoint, UncompressedPointSize, UntaggedPointSize},
weierstrass::Curve,
Arithmetic, ElementBytes, Error, Generate,
};
use core::ops::{Add, Mul};
use generic_array::{typenum::Unsigned, ArrayLength, GenericArray};
use rand_core::{CryptoRng, RngCore};
use zeroize::Zeroize;

Expand All @@ -58,11 +55,8 @@ where
C: Curve + Arithmetic,
C::Scalar: Clone + Generate + Zeroize,
C::AffinePoint: FromEncodedPoint<C> + Mul<NonZeroScalar<C>, Output = C::AffinePoint> + Zeroize,
C::ElementSize: Add<U1>,
<C::ElementSize as Add>::Output: Add<U1>,
CompressedPoint<C>: From<C::AffinePoint>,
UncompressedPoint<C>: From<C::AffinePoint>,
CompressedPointSize<C>: ArrayLength<u8>,
PublicKey<C>: From<C::AffinePoint>,
UntaggedPointSize<C>: Add<U1> + ArrayLength<u8>,
UncompressedPointSize<C>: ArrayLength<u8>,
{
/// Generate a new [`EphemeralSecret`].
Expand All @@ -75,14 +69,8 @@ where
/// Get the public key associated with this ephemeral secret.
///
/// The `compress` flag enables point compression.
pub fn public_key(&self, compress: bool) -> PublicKey<C> {
let affine_point = C::AffinePoint::generator() * self.scalar.clone();

if compress {
PublicKey::Compressed(affine_point.into())
} else {
PublicKey::Uncompressed(affine_point.into())
}
pub fn public_key(&self) -> PublicKey<C> {
PublicKey::from(C::AffinePoint::generator() * self.scalar.clone())
}

/// Compute a Diffie-Hellman shared secret from an ephemeral secret and the
Expand All @@ -104,15 +92,12 @@ where
C: Curve + Arithmetic,
C::Scalar: Clone + Generate + Zeroize,
C::AffinePoint: FromEncodedPoint<C> + Mul<NonZeroScalar<C>, Output = C::AffinePoint> + Zeroize,
C::ElementSize: Add<U1>,
<C::ElementSize as Add>::Output: Add<U1>,
CompressedPoint<C>: From<C::AffinePoint>,
UncompressedPoint<C>: From<C::AffinePoint>,
CompressedPointSize<C>: ArrayLength<u8>,
PublicKey<C>: From<C::AffinePoint>,
UntaggedPointSize<C>: Add<U1> + ArrayLength<u8>,
UncompressedPointSize<C>: ArrayLength<u8>,
{
fn from(ephemeral_secret: &EphemeralSecret<C>) -> Self {
ephemeral_secret.public_key(C::COMPRESS_POINTS)
ephemeral_secret.public_key()
}
}

Expand Down Expand Up @@ -158,17 +143,13 @@ impl<C> SharedSecret<C>
where
C: Curve + Arithmetic,
C::AffinePoint: Zeroize,
C::ElementSize: Add<U1>,
<C::ElementSize as Add>::Output: Add<U1>,
UntaggedPointSize<C>: Add<U1> + ArrayLength<u8>,
UncompressedPointSize<C>: ArrayLength<u8>,
{
/// Create a new shared secret from the given uncompressed curve point
fn new(mut serialized_point: UncompressedPoint<C>) -> Self {
let secret_bytes = GenericArray::clone_from_slice(
&serialized_point.as_ref()[1..(1 + C::ElementSize::to_usize())],
);

serialized_point.zeroize();
fn new(mut encoded_point: sec1::EncodedPoint<C>) -> Self {
let secret_bytes = encoded_point.x().clone();
encoded_point.zeroize();
Self { secret_bytes }
}

Expand Down
Loading

0 comments on commit 337e233

Please sign in to comment.