Skip to content

Commit 1adfd72

Browse files
authored
primefield: add shr methods/trait impls to macro (#1159)
Updates `primefield::field_element_type` to add `shr` methods and trait impls, replacing the copy/pasted definition to one provided by the macro
1 parent 42662ae commit 1adfd72

File tree

3 files changed

+43
-14
lines changed

3 files changed

+43
-14
lines changed

bign256/src/arithmetic/scalar.rs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -96,13 +96,6 @@ impl Scalar {
9696
]);
9797
CtOption::new(sqrt, (sqrt * sqrt).ct_eq(self))
9898
}
99-
100-
/// Right shifts the scalar.
101-
///
102-
/// Note: not constant-time with respect to the `shift` parameter.
103-
pub const fn shr_vartime(&self, shift: u32) -> Scalar {
104-
Self(self.0.wrapping_shr_vartime(shift))
105-
}
10699
}
107100

108101
impl AsRef<Scalar> for Scalar {

p384/src/arithmetic/scalar.rs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -152,13 +152,6 @@ impl Scalar {
152152
}
153153
x
154154
}
155-
156-
/// Right shifts the scalar.
157-
///
158-
/// Note: not constant-time with respect to the `shift` parameter.
159-
pub const fn shr_vartime(&self, shift: u32) -> Scalar {
160-
Self(self.0.wrapping_shr_vartime(shift))
161-
}
162155
}
163156

164157
impl AsRef<Scalar> for Scalar {

primefield/src/lib.rs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ mod fiat;
6161
/// - `Mul`
6262
/// - `MulAssign`
6363
/// - `Neg`
64+
/// - `Shr`
65+
/// - `ShrAssign`
6466
#[macro_export]
6567
macro_rules! field_element_type {
6668
(
@@ -201,6 +203,22 @@ macro_rules! field_element_type {
201203

202204
res
203205
}
206+
207+
/// Right shifts the [`
208+
#[doc = stringify!($fe)]
209+
/// `].
210+
pub const fn shr(&self, shift: u32) -> Self {
211+
Self(self.0.wrapping_shr(shift))
212+
}
213+
214+
/// Right shifts the [`
215+
#[doc = stringify!($fe)]
216+
/// `].
217+
///
218+
/// Note: not constant-time with respect to the `shift` parameter.
219+
pub const fn shr_vartime(&self, shift: u32) -> Self {
220+
Self(self.0.wrapping_shr_vartime(shift))
221+
}
204222
}
205223

206224
impl $crate::ff::Field for $fe {
@@ -293,6 +311,31 @@ macro_rules! field_element_type {
293311
}
294312
}
295313

314+
impl ::core::ops::Shr<u32> for $fe {
315+
type Output = Self;
316+
317+
#[inline]
318+
fn shr(self, rhs: u32) -> Self {
319+
Self::shr(&self, rhs)
320+
}
321+
}
322+
323+
impl ::core::ops::Shr<u32> for &$fe {
324+
type Output = Self;
325+
326+
#[inline]
327+
fn shr(self, rhs: u32) -> Self {
328+
Self::shr(self, rhs)
329+
}
330+
}
331+
332+
impl ::core::ops::ShrAssign<u32> for $fe {
333+
#[inline]
334+
fn shr_assign(&mut self, rhs: u32) {
335+
*self = Self::shr(self, rhs)
336+
}
337+
}
338+
296339
impl Default for $fe {
297340
fn default() -> Self {
298341
Self::ZERO

0 commit comments

Comments
 (0)