Skip to content
Closed
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
6 changes: 3 additions & 3 deletions elliptic-curve/src/ecdh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use crate::{
scalar::NonZeroScalar,
sec1::{self, FromEncodedPoint, UncompressedPointSize, UntaggedPointSize},
weierstrass::Curve,
Arithmetic, ElementBytes, Error, Generate,
Arithmetic, Error, FEBytes, Generate,
};
use core::ops::{Add, Mul};
use rand_core::{CryptoRng, RngCore};
Expand Down Expand Up @@ -135,7 +135,7 @@ where
/// Function (KDF) or cryptographic hash function to produce a symmetric key.
pub struct SharedSecret<C: Curve + Arithmetic> {
/// Computed secret value
secret_bytes: ElementBytes<C>,
secret_bytes: FEBytes<C>,
}

impl<C> SharedSecret<C>
Expand All @@ -158,7 +158,7 @@ where
/// should not be used directly as a symmetric encryption key, but instead
/// as input to a KDF (or failing that, a hash function) used to produce
/// a symmetric key.
pub fn as_bytes(&self) -> &ElementBytes<C> {
pub fn as_bytes(&self) -> &FEBytes<C> {
&self.secret_bytes
}
}
Expand Down
7 changes: 4 additions & 3 deletions elliptic-curve/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,9 @@ use generic_array::{typenum::Unsigned, ArrayLength, GenericArray};
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>;
/// Byte array containing a serialized field element, i.e. an element
/// of the given curve's base or scalar fields.
pub type FEBytes<C> = GenericArray<u8, <C as Curve>::FieldSize>;

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@str4d I've noticed the ff and group crates use a "Repr" naming convention here... do you think something like FERepr would be better?


/// Elliptic curve.
///
Expand All @@ -89,7 +90,7 @@ pub trait Arithmetic: Curve {
+ ConstantTimeEq
+ Default
+ FromBytes<Size = Self::FieldSize>
+ Into<ElementBytes<Self>>;
+ Into<FEBytes<Self>>;

/// Affine point type for a given curve
type AffinePoint: ConditionallySelectable
Expand Down
10 changes: 5 additions & 5 deletions elliptic-curve/src/scalar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use crate::{
ops::Invert,
rand_core::{CryptoRng, RngCore},
Arithmetic, Curve, ElementBytes, FromBytes, Generate,
Arithmetic, Curve, FEBytes, FromBytes, Generate,
};
use core::ops::Deref;
use subtle::{Choice, ConditionallySelectable, ConstantTimeEq, CtOption};
Expand Down Expand Up @@ -37,7 +37,7 @@ where
}

/// Serialize this [`NonZeroScalar`] as a byte array
pub fn to_bytes(&self) -> ElementBytes<C> {
pub fn to_bytes(&self) -> FEBytes<C> {
self.scalar.into()
}
}
Expand Down Expand Up @@ -80,16 +80,16 @@ where
{
type Size = C::FieldSize;

fn from_bytes(bytes: &ElementBytes<C>) -> CtOption<Self> {
fn from_bytes(bytes: &FEBytes<C>) -> CtOption<Self> {
C::Scalar::from_bytes(bytes).and_then(Self::new)
}
}

impl<C> From<NonZeroScalar<C>> for ElementBytes<C>
impl<C> From<NonZeroScalar<C>> for FEBytes<C>
where
C: Curve + Arithmetic,
{
fn from(scalar: NonZeroScalar<C>) -> ElementBytes<C> {
fn from(scalar: NonZeroScalar<C>) -> FEBytes<C> {
scalar.to_bytes()
}
}
Expand Down
18 changes: 7 additions & 11 deletions elliptic-curve/src/sec1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::{
point::Generator,
scalar::NonZeroScalar,
weierstrass::{point::Decompress, Curve},
Arithmetic, ElementBytes, Error, FromBytes, SecretKey,
Arithmetic, Error, FEBytes, FromBytes, SecretKey,
};
use core::{
fmt::{self, Debug},
Expand Down Expand Up @@ -96,11 +96,7 @@ where

/// Encode an elliptic curve point from big endian serialized coordinates
/// (with optional point compression)
pub fn from_affine_coordinates(
x: &ElementBytes<C>,
y: &ElementBytes<C>,
compress: bool,
) -> Self {
pub fn from_affine_coordinates(x: &FEBytes<C>, y: &FEBytes<C>, compress: bool) -> Self {
let tag = if compress {
Tag::compress_y(y.as_slice())
} else {
Expand Down Expand Up @@ -225,7 +221,7 @@ where
}

/// Get the x-coordinate for this [`EncodedPoint`]
pub fn x(&self) -> &ElementBytes<C> {
pub fn x(&self) -> &FEBytes<C> {
match self.coordinates() {
Coordinates::Compressed { x, .. } => x,
Coordinates::Uncompressed { x, .. } => x,
Expand All @@ -235,7 +231,7 @@ where
/// Get the y-coordinate for this [`EncodedPoint`].
///
/// Returns `None` if this point is compressed.
pub fn y(&self) -> Option<&ElementBytes<C>> {
pub fn y(&self) -> Option<&FEBytes<C>> {
match self.coordinates() {
Coordinates::Compressed { .. } => None,
Coordinates::Uncompressed { y, .. } => Some(y),
Expand Down Expand Up @@ -294,7 +290,7 @@ pub enum Coordinates<'a, C: Curve> {
/// Compressed curve point
Compressed {
/// x-coordinate
x: &'a ElementBytes<C>,
x: &'a FEBytes<C>,

/// Is the y-coordinate odd?
y_is_odd: bool,
Expand All @@ -303,10 +299,10 @@ pub enum Coordinates<'a, C: Curve> {
/// Uncompressed curve point
Uncompressed {
/// x-coordinate
x: &'a ElementBytes<C>,
x: &'a FEBytes<C>,

/// y-coordinate
y: &'a ElementBytes<C>,
y: &'a FEBytes<C>,
},
}

Expand Down
10 changes: 5 additions & 5 deletions elliptic-curve/src/secret_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
//! When the `zeroize` feature of this crate is enabled, it also handles
//! zeroing it out of memory securely on drop.

use crate::{error::Error, Curve, ElementBytes};
use crate::{error::Error, Curve, FEBytes};
use crate::{Arithmetic, Generate};
use core::{
convert::{TryFrom, TryInto},
Expand All @@ -24,12 +24,12 @@ use rand_core::{CryptoRng, RngCore};
#[derive(Clone)]
pub struct SecretKey<C: Curve> {
/// Private scalar value
scalar: ElementBytes<C>,
scalar: FEBytes<C>,
}

impl<C: Curve> SecretKey<C> {
/// Create a new secret key from a serialized scalar value
pub fn new(bytes: ElementBytes<C>) -> Self {
pub fn new(bytes: FEBytes<C>) -> Self {
Self { scalar: bytes }
}

Expand All @@ -39,7 +39,7 @@ impl<C: Curve> SecretKey<C> {
}

/// Expose the byte serialization of the value this [`SecretKey`] wraps
pub fn as_bytes(&self) -> &ElementBytes<C> {
pub fn as_bytes(&self) -> &FEBytes<C> {
&self.scalar
}
}
Expand Down Expand Up @@ -67,7 +67,7 @@ impl<C: Curve> Debug for SecretKey<C> {
impl<C> Generate for SecretKey<C>
where
C: Curve + Arithmetic,
C::Scalar: Generate + Into<ElementBytes<C>>,
C::Scalar: Generate + Into<FEBytes<C>>,
{
/// Generate a new [`SecretKey`]
fn generate(rng: impl CryptoRng + RngCore) -> Self {
Expand Down
4 changes: 2 additions & 2 deletions elliptic-curve/src/weierstrass/point.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
//! Traits for Weierstrass elliptic curve points

use super::Curve;
use crate::ElementBytes;
use crate::FEBytes;
use subtle::{Choice, CtOption};

/// Attempt to decompress an elliptic curve point from its x-coordinate and
/// a boolean flag indicating whether or not the y-coordinate is odd.
pub trait Decompress<C: Curve>: Sized {
/// Attempt to decompress an elliptic curve point
fn decompress(x: &ElementBytes<C>, y_is_odd: Choice) -> CtOption<Self>;
fn decompress(x: &FEBytes<C>, y_is_odd: Choice) -> CtOption<Self>;
}