Commit c1db5ad 1 parent 08bb29e commit c1db5ad Copy full SHA for c1db5ad
File tree 2 files changed +15
-0
lines changed
2 files changed +15
-0
lines changed Original file line number Diff line number Diff line change @@ -28,6 +28,11 @@ impl Encoding {
28
28
}
29
29
30
30
pub fn vartime_decompress ( & self ) -> Result < Element , EncodingError > {
31
+ // Top three bits of last byte should be zero
32
+ if self . 0 [ 31 ] >> 5 != 0u8 {
33
+ return Err ( EncodingError :: InvalidEncoding ) ;
34
+ }
35
+
31
36
// This isn't a constant, only because traits don't have const methods
32
37
// yet and multiplication is only implemented as part of the Mul trait.
33
38
let D4 : Fq = EdwardsParameters :: COEFF_D * Fq :: from ( 4u32 ) ;
@@ -120,6 +125,8 @@ impl Element {
120
125
debug_assert_eq ! ( s. serialized_size( ) , 32 ) ;
121
126
s. serialize ( & mut bytes[ ..] )
122
127
. expect ( "serialization into array should be infallible" ) ;
128
+ // Set top three bits of last byte to zero
129
+ bytes[ 31 ] &= 0b00011111 ;
123
130
124
131
Encoding ( bytes)
125
132
}
Original file line number Diff line number Diff line change @@ -30,10 +30,18 @@ impl FieldExt for Fq {
30
30
debug_assert_eq ! ( self . serialized_size( ) , 32 ) ;
31
31
self . serialize ( & mut bytes[ ..] )
32
32
. expect ( "serialization into array should be infallible" ) ;
33
+ // Set top three bits of last byte to zero
34
+ bytes[ 31 ] &= 0b00011111 ;
35
+
33
36
bytes
34
37
}
35
38
36
39
fn from_bytes ( bytes : [ u8 ; 32 ] ) -> Result < Self , EncodingError > {
40
+ // Top three bits of last byte should be zero
41
+ if bytes[ 31 ] >> 5 != 0u8 {
42
+ return Err ( EncodingError :: InvalidEncoding ) ;
43
+ }
44
+
37
45
Self :: deserialize ( & bytes[ ..] ) . map_err ( |_| EncodingError :: InvalidEncoding )
38
46
}
39
47
}
You can’t perform that action at this time.
0 commit comments