From 43e5ffea69ba760c19405f5e436ec8917131d9df Mon Sep 17 00:00:00 2001 From: Tony Arcieri Date: Wed, 26 Aug 2020 12:31:37 -0700 Subject: [PATCH] elliptic-curve: add NonZeroScalar::to_bytes ...and a `From` conversion to `ElementBytes` --- elliptic-curve/src/scalar.rs | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/elliptic-curve/src/scalar.rs b/elliptic-curve/src/scalar.rs index f9cb56996..957d7bd74 100644 --- a/elliptic-curve/src/scalar.rs +++ b/elliptic-curve/src/scalar.rs @@ -36,6 +36,11 @@ where let is_zero = scalar.ct_eq(&zero); CtOption::new(Self { scalar }, !is_zero) } + + /// Serialize this [`NonZeroScalar`] as a byte array + pub fn to_bytes(&self) -> ElementBytes { + self.scalar.into() + } } impl AsRef for NonZeroScalar @@ -70,6 +75,15 @@ where } } +impl From> for ElementBytes +where + C: Curve + Arithmetic, +{ + fn from(scalar: NonZeroScalar) -> ElementBytes { + scalar.to_bytes() + } +} + #[cfg(feature = "rand")] impl Generate for NonZeroScalar where @@ -77,7 +91,7 @@ where C::Scalar: Generate, { fn generate(mut rng: impl CryptoRng + RngCore) -> Self { - // Use rejection sampling to eliminate zeroes + // Use rejection sampling to eliminate zero values loop { let result = Self::new(C::Scalar::generate(&mut rng));