Skip to content

Commit

Permalink
elliptic-curve: rename and simplify FromFieldBytes (#298)
Browse files Browse the repository at this point in the history
  • Loading branch information
tarcieri authored Sep 10, 2020
1 parent 7dd78e0 commit a9bd70b
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 24 deletions.
11 changes: 4 additions & 7 deletions elliptic-curve/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ pub trait Arithmetic: Curve {
type Scalar: ff::PrimeField
+ ConstantTimeEq
+ Default
+ FromBytes<Size = Self::FieldSize>
+ FromFieldBytes<Self>
+ Into<FieldBytes<Self>>;

/// Elliptic curve point in affine coordinates.
Expand All @@ -119,13 +119,10 @@ pub trait Arithmetic: Curve {
+ group::Group<Scalar = Self::Scalar>;
}

/// Try to decode the given bytes into a curve element
pub trait FromBytes: ConditionallySelectable + Sized {
/// Size of the serialized byte array
type Size: ArrayLength<u8>;

/// Decode the given serialized field element
pub trait FromFieldBytes<C: Curve>: ConditionallySelectable + Sized {
/// Try to decode this object from bytes
fn from_bytes(bytes: &GenericArray<u8, Self::Size>) -> CtOption<Self>;
fn from_field_bytes(bytes: &FieldBytes<C>) -> CtOption<Self>;
}

/// Instantiate this type from the output of a digest.
Expand Down
12 changes: 5 additions & 7 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, FieldBytes, FromBytes,
Arithmetic, Curve, FieldBytes, FromFieldBytes,
};
use bitvec::{array::BitArray, order::Lsb0};
use core::ops::Deref;
Expand Down Expand Up @@ -48,7 +48,7 @@ where
/// Create a [`NonZeroScalar`] from a scalar, performing a constant-time
/// check that it's non-zero.
pub fn new(scalar: C::Scalar) -> CtOption<Self> {
let zero = C::Scalar::from_bytes(&Default::default()).unwrap();
let zero = C::Scalar::from_field_bytes(&Default::default()).unwrap();
let is_zero = scalar.ct_eq(&zero);
CtOption::new(Self { scalar }, !is_zero)
}
Expand Down Expand Up @@ -91,14 +91,12 @@ where
}
}

impl<C> FromBytes for NonZeroScalar<C>
impl<C> FromFieldBytes<C> for NonZeroScalar<C>
where
C: Curve + Arithmetic,
{
type Size = C::FieldSize;

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

Expand Down
18 changes: 10 additions & 8 deletions elliptic-curve/src/sec1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ use alloc::boxed::Box;
use crate::{subtle::Choice, weierstrass::point::Decompress, Arithmetic};

#[cfg(all(feature = "arithmetic", feature = "zeroize"))]
use crate::{ops::Mul, point::Generator, scalar::NonZeroScalar, secret_key::SecretKey, FromBytes};
use crate::{
ops::Mul, point::Generator, scalar::NonZeroScalar, secret_key::SecretKey, FromFieldBytes,
};

#[cfg(feature = "zeroize")]
use zeroize::Zeroize;
Expand Down Expand Up @@ -130,14 +132,14 @@ where
C::AffinePoint: Mul<NonZeroScalar<C>, Output = C::AffinePoint> + ToEncodedPoint<C>,
C::Scalar: Zeroize,
{
let ct_option = C::Scalar::from_bytes(&secret_key.to_bytes()).and_then(NonZeroScalar::new);

if ct_option.is_none().into() {
return Err(Error);
if let Some(scalar) = C::Scalar::from_field_bytes(&secret_key.to_bytes())
.and_then(NonZeroScalar::new)
.into()
{
Ok(Self::encode(C::AffinePoint::generator() * scalar, compress))
} else {
Err(Error)
}

let affine_point = C::AffinePoint::generator() * ct_option.unwrap();
Ok(Self::encode(affine_point, compress))
}

/// Get the length of the encoded point in bytes
Expand Down
4 changes: 2 additions & 2 deletions elliptic-curve/src/secret_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use subtle::CtOption;
use zeroize::Zeroize;

#[cfg(feature = "arithmetic")]
use crate::{scalar::NonZeroScalar, Arithmetic, FromBytes};
use crate::{scalar::NonZeroScalar, Arithmetic, FromFieldBytes};
#[cfg(feature = "arithmetic")]
use rand_core::{CryptoRng, RngCore};

Expand All @@ -37,7 +37,7 @@ where
type Secret = NonZeroScalar<C>;

fn from_secret_bytes(bytes: &FieldBytes<C>) -> CtOption<NonZeroScalar<C>> {
NonZeroScalar::from_bytes(bytes)
NonZeroScalar::from_field_bytes(bytes)
}
}

Expand Down

0 comments on commit a9bd70b

Please sign in to comment.