Skip to content

Commit

Permalink
elliptic-curve: remove generic invert_vartime implementation
Browse files Browse the repository at this point in the history
It's mathematically unsafe in that it relies on field element
representations outside the curve's modulus, which doesn't work in a
generic context.

The newly added `Invert::invert_vartime` method allows plugging in
generic variable-time inversions.
  • Loading branch information
tarcieri committed Feb 3, 2023
1 parent d19d50f commit 24562ae
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 88 deletions.
4 changes: 1 addition & 3 deletions elliptic-curve/src/scalar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,12 @@
#[cfg(feature = "arithmetic")]
mod blinded;
#[cfg(feature = "arithmetic")]
mod invert;
#[cfg(feature = "arithmetic")]
mod nonzero;
mod primitive;

pub use self::primitive::ScalarPrimitive;
#[cfg(feature = "arithmetic")]
pub use self::{blinded::BlindedScalar, invert::invert_vartime, nonzero::NonZeroScalar};
pub use self::{blinded::BlindedScalar, nonzero::NonZeroScalar};

use crypto_bigint::Integer;
use subtle::Choice;
Expand Down
7 changes: 4 additions & 3 deletions elliptic-curve/src/scalar/blinded.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Random blinding support for [`Scalar`]
use super::{invert_vartime, Scalar};
use super::Scalar;
use crate::{ops::Invert, CurveArithmetic};
use group::ff::Field;
use rand_core::CryptoRngCore;
Expand Down Expand Up @@ -57,8 +57,9 @@ where
fn invert(&self) -> CtOption<Scalar<C>> {
// prevent side channel analysis of scalar inversion by pre-and-post-multiplying
// with the random masking scalar
let masked_scalar = self.scalar * self.mask;
invert_vartime::<C>(&masked_scalar).map(|s| s * self.mask)
(self.scalar * self.mask)
.invert_vartime()
.map(|s| s * self.mask)
}
}

Expand Down
69 changes: 0 additions & 69 deletions elliptic-curve/src/scalar/invert.rs

This file was deleted.

13 changes: 0 additions & 13 deletions elliptic-curve/src/scalar/nonzero.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,19 +67,6 @@ where
pub fn from_uint(uint: C::Uint) -> CtOption<Self> {
ScalarPrimitive::new(uint).and_then(|scalar| Self::new(scalar.into()))
}

/// Perform an inversion in variable-time.
///
/// ⚠️ WARNING!
///
/// This method should not be used with (unblinded) secret scalars, as its
/// variable-time operation can potentially leak secrets through
/// sidechannels.
pub fn invert_vartime(&self) -> Self {
Self {
scalar: super::invert_vartime::<C>(&self.scalar).unwrap(),
}
}
}

impl<C> AsRef<Scalar<C>> for NonZeroScalar<C>
Expand Down

0 comments on commit 24562ae

Please sign in to comment.