Skip to content

Commit d0f55ba

Browse files
authored
primefield: remove shr support (#1320)
It was noted as buggy in #1319 because `shr` was being computed on values within the Montgomery domain directly. They either need to be converted to canonical form first, or a multiplication needs to be used in place of a bit shift. Turns out it was completely unused, aside from fulfilling a bound in the `elliptic-curve` crate, which has since been removed. This removes `shr` support from all curves whose `Scalar` used an internal Montgomery representation. It has been retained on `k256` and `p256` which use canonical form `Scalar` types. Closes #1319
1 parent e8b3a04 commit d0f55ba

File tree

3 files changed

+2
-116
lines changed

3 files changed

+2
-116
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

p521/src/arithmetic/scalar.rs

Lines changed: 1 addition & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use self::scalar_impl::*;
1818
use crate::{FieldBytes, NistP521, U576};
1919
use core::{
2020
iter::{Product, Sum},
21-
ops::{Add, AddAssign, Mul, MulAssign, Neg, Shr, ShrAssign, SubAssign},
21+
ops::{Add, AddAssign, Mul, MulAssign, Neg, SubAssign},
2222
};
2323
use elliptic_curve::{
2424
Curve as _, Error, FieldBytesEncoding, Result,
@@ -305,32 +305,6 @@ impl Scalar {
305305
res
306306
}
307307

308-
/// Right shifts the scalar.
309-
///
310-
/// Note: not constant-time with respect to the `shift` parameter.
311-
#[cfg(target_pointer_width = "32")]
312-
pub const fn shr_vartime(&self, shift: u32) -> Scalar {
313-
Self(fiat_p521_scalar_montgomery_domain_field_element(
314-
u32x18_to_u64x9(
315-
&U576::from_words(u64x9_to_u32x18(self.as_limbs()))
316-
.wrapping_shr_vartime(shift)
317-
.to_words(),
318-
),
319-
))
320-
}
321-
322-
/// Right shifts the scalar.
323-
///
324-
/// Note: not constant-time with respect to the `shift` parameter.
325-
#[cfg(target_pointer_width = "64")]
326-
pub const fn shr_vartime(&self, shift: u32) -> Scalar {
327-
Self(fiat_p521_scalar_montgomery_domain_field_element(
328-
U576::from_words(self.into_limbs())
329-
.wrapping_shr_vartime(shift)
330-
.to_words(),
331-
))
332-
}
333-
334308
/// Borrow the inner limbs of this scalar.
335309
pub(crate) const fn as_limbs(&self) -> &[u64; 9] {
336310
&self.0.0
@@ -551,28 +525,6 @@ impl IsHigh for Scalar {
551525
}
552526
}
553527

554-
impl Shr<usize> for Scalar {
555-
type Output = Self;
556-
557-
fn shr(self, rhs: usize) -> Self::Output {
558-
self.shr_vartime(rhs as u32)
559-
}
560-
}
561-
562-
impl Shr<usize> for &Scalar {
563-
type Output = Scalar;
564-
565-
fn shr(self, rhs: usize) -> Self::Output {
566-
self.shr_vartime(rhs as u32)
567-
}
568-
}
569-
570-
impl ShrAssign<usize> for Scalar {
571-
fn shr_assign(&mut self, rhs: usize) {
572-
*self = *self >> rhs;
573-
}
574-
}
575-
576528
impl PrimeField for Scalar {
577529
type Repr = FieldBytes;
578530

primefield/src/lib.rs

Lines changed: 0 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -197,22 +197,6 @@ macro_rules! field_element_type {
197197

198198
res
199199
}
200-
201-
/// Right shifts the [`
202-
#[doc = stringify!($fe)]
203-
/// `].
204-
pub const fn shr(&self, shift: u32) -> Self {
205-
Self(self.0.wrapping_shr(shift))
206-
}
207-
208-
/// Right shifts the [`
209-
#[doc = stringify!($fe)]
210-
/// `].
211-
///
212-
/// Note: not constant-time with respect to the `shift` parameter.
213-
pub const fn shr_vartime(&self, shift: u32) -> Self {
214-
Self(self.0.wrapping_shr_vartime(shift))
215-
}
216200
}
217201

218202
impl $crate::ff::Field for $fe {
@@ -323,56 +307,6 @@ macro_rules! field_element_type {
323307
}
324308
}
325309

326-
impl ::core::ops::Shr<u32> for $fe {
327-
type Output = Self;
328-
329-
#[inline]
330-
fn shr(self, rhs: u32) -> Self {
331-
Self::shr(&self, rhs)
332-
}
333-
}
334-
335-
impl ::core::ops::Shr<u32> for &$fe {
336-
type Output = Self;
337-
338-
#[inline]
339-
fn shr(self, rhs: u32) -> Self {
340-
Self::shr(self, rhs)
341-
}
342-
}
343-
344-
impl ::core::ops::ShrAssign<u32> for $fe {
345-
#[inline]
346-
fn shr_assign(&mut self, rhs: u32) {
347-
*self = Self::shr(self, rhs)
348-
}
349-
}
350-
351-
impl ::core::ops::Shr<usize> for $fe {
352-
type Output = Self;
353-
354-
#[inline]
355-
fn shr(self, rhs: usize) -> Self {
356-
Self::shr(&self, rhs as u32)
357-
}
358-
}
359-
360-
impl ::core::ops::Shr<usize> for &$fe {
361-
type Output = Self;
362-
363-
#[inline]
364-
fn shr(self, rhs: usize) -> Self {
365-
Self::shr(self, rhs as u32)
366-
}
367-
}
368-
369-
impl ::core::ops::ShrAssign<usize> for $fe {
370-
#[inline]
371-
fn shr_assign(&mut self, rhs: usize) {
372-
*self = Self::shr(self, rhs as u32)
373-
}
374-
}
375-
376310
impl ::core::fmt::Debug for $fe {
377311
fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
378312
write!(f, "{}(0x{:X})", stringify!($fe), &self.0)

0 commit comments

Comments
 (0)