| //! Random blinding support for [`Scalar`] |
| use crate::{ops::Invert, CurveArithmetic}; |
| use rand_core::CryptoRngCore; |
| /// Scalar blinded with a randomly generated masking value. |
| /// This provides a randomly blinded impl of [`Invert`] which is useful for |
| /// e.g. ECDSA ephemeral (`k`) scalars. |
| /// It implements masked variable-time inversions using Stein's algorithm, which |
| /// may be helpful for performance on embedded platforms. |
| pub struct BlindedScalar<C> |
| /// Create a new [`BlindedScalar`] from a scalar and a [`CryptoRngCore`]. |
| pub fn new(scalar: Scalar<C>, rng: &mut impl CryptoRngCore) -> Self { |
| mask: Scalar::<C>::random(rng), |
| impl<C> AsRef<Scalar<C>> for BlindedScalar<C> |
| fn as_ref(&self) -> &Scalar<C> { |
| impl<C> Invert for BlindedScalar<C> |
| type Output = CtOption<Scalar<C>>; |
| fn invert(&self) -> CtOption<Scalar<C>> { |
| // prevent side channel analysis of scalar inversion by pre-and-post-multiplying |
| // with the random masking scalar |
| (self.scalar * self.mask) |
| impl<C> Drop for BlindedScalar<C> |