File tree Expand file tree Collapse file tree 3 files changed +28
-1
lines changed Expand file tree Collapse file tree 3 files changed +28
-1
lines changed Original file line number Diff line number Diff line change @@ -12,6 +12,17 @@ pub trait Invert {
12
12
13
13
/// Invert a field element.
14
14
fn invert ( & self ) -> Self :: Output ;
15
+
16
+ /// Invert a field element in variable time.
17
+ ///
18
+ /// ⚠️ WARNING!
19
+ ///
20
+ /// This method should not be used with secret values, as its variable-time
21
+ /// operation can potentially leak secrets through sidechannels.
22
+ fn invert_vartime ( & self ) -> Self :: Output {
23
+ // Fall back on constant-time implementation by default.
24
+ self . invert ( )
25
+ }
15
26
}
16
27
17
28
impl < F : ff:: Field > Invert for F {
Original file line number Diff line number Diff line change @@ -8,6 +8,14 @@ use subtle::CtOption;
8
8
/// Returns none if the scalar is zero.
9
9
///
10
10
/// <https://link.springer.com/article/10.1007/s13389-016-0135-4>
11
+ ///
12
+ /// ⚠️ WARNING!
13
+ ///
14
+ /// This generic implementation relies on special properties of the scalar
15
+ /// field implementation and may not work correctly! Please ensure your use
16
+ /// cases are well-tested!
17
+ ///
18
+ /// USE AT YOUR OWN RISK!
11
19
#[ allow( non_snake_case) ]
12
20
pub fn invert_vartime < C > ( scalar : & Scalar < C > ) -> CtOption < Scalar < C > >
13
21
where
Original file line number Diff line number Diff line change @@ -184,13 +184,21 @@ where
184
184
impl < C > Invert for NonZeroScalar < C >
185
185
where
186
186
C : CurveArithmetic ,
187
+ Scalar < C > : Invert < Output = CtOption < Scalar < C > > > ,
187
188
{
188
189
type Output = Self ;
189
190
190
191
fn invert ( & self ) -> Self {
191
192
Self {
192
193
// This will always succeed since `scalar` will never be 0
193
- scalar : ff:: Field :: invert ( & self . scalar ) . unwrap ( ) ,
194
+ scalar : Invert :: invert ( & self . scalar ) . unwrap ( ) ,
195
+ }
196
+ }
197
+
198
+ fn invert_vartime ( & self ) -> Self :: Output {
199
+ Self {
200
+ // This will always succeed since `scalar` will never be 0
201
+ scalar : Invert :: invert_vartime ( & self . scalar ) . unwrap ( ) ,
194
202
}
195
203
}
196
204
}
You can’t perform that action at this time.
0 commit comments