Skip to content
Merged
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
16 changes: 15 additions & 1 deletion elliptic-curve/src/scalar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<C> {
self.scalar.into()
}
}

impl<C> AsRef<C::Scalar> for NonZeroScalar<C>
Expand Down Expand Up @@ -70,14 +75,23 @@ where
}
}

impl<C> From<NonZeroScalar<C>> for ElementBytes<C>
where
C: Curve + Arithmetic,
{
fn from(scalar: NonZeroScalar<C>) -> ElementBytes<C> {
scalar.to_bytes()
}
}

#[cfg(feature = "rand")]
impl<C> Generate for NonZeroScalar<C>
where
C: Curve + Arithmetic,
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));

Expand Down