@@ -14,17 +14,28 @@ pub struct EncodingFlags {
1414}
1515
1616impl EncodingFlags {
17- pub fn get_flags ( bytes : & [ u8 ] ) -> Self {
17+ /// Fetches the flags from the byte-string
18+ pub fn get_flags ( bytes : & [ u8 ] ) -> Result < Self , SerializationError > {
1819 let compression_flag_set = ( bytes[ 0 ] >> 7 ) & 1 ;
1920 let infinity_flag_set = ( bytes[ 0 ] >> 6 ) & 1 ;
2021 let sort_flag_set = ( bytes[ 0 ] >> 5 ) & 1 ;
2122
22- Self {
23- is_compressed : compression_flag_set == 1 ,
24- is_infinity : infinity_flag_set == 1 ,
25- is_lexographically_largest : sort_flag_set == 1 ,
23+ let is_compressed = compression_flag_set == 1 ;
24+ let is_infinity = infinity_flag_set == 1 ;
25+ let is_lexographically_largest = sort_flag_set == 1 ;
26+
27+ if is_lexographically_largest && ( !is_compressed || is_infinity) {
28+ return Err ( SerializationError :: InvalidData ) ;
2629 }
30+
31+ Ok ( Self {
32+ is_compressed,
33+ is_infinity,
34+ is_lexographically_largest,
35+ } )
2736 }
37+
38+ /// Encodes the flags into the byte-string
2839 pub fn encode_flags ( & self , bytes : & mut [ u8 ] ) {
2940 if self . is_compressed {
3041 bytes[ 0 ] |= 1 << 7 ;
@@ -38,6 +49,13 @@ impl EncodingFlags {
3849 bytes[ 0 ] |= 1 << 5 ;
3950 }
4051 }
52+
53+ /// Removes the flags from the byte-string.
54+ ///
55+ /// This reverses the effects of `encode_flags`.
56+ pub fn remove_flags ( bytes : & mut [ u8 ] ) {
57+ bytes[ 0 ] &= 0b0001_1111 ;
58+ }
4159}
4260
4361pub ( crate ) fn deserialize_fq ( bytes : [ u8 ; 48 ] ) -> Option < Fq > {
@@ -71,20 +89,15 @@ pub(crate) fn serialize_fq(field: Fq) -> [u8; 48] {
7189 result
7290}
7391
74- pub ( crate ) fn read_fq_with_offset (
75- bytes : & [ u8 ] ,
76- offset : usize ,
77- mask : bool ,
78- ) -> Result < Fq , ark_serialize:: SerializationError > {
92+ fn read_bytes_with_offset ( bytes : & [ u8 ] , offset : usize , mask : bool ) -> [ u8 ; G1_SERIALIZED_SIZE ] {
7993 let mut tmp = [ 0 ; G1_SERIALIZED_SIZE ] ;
8094 // read `G1_SERIALIZED_SIZE` bytes
8195 tmp. copy_from_slice ( & bytes[ offset * G1_SERIALIZED_SIZE ..G1_SERIALIZED_SIZE * ( offset + 1 ) ] ) ;
8296
8397 if mask {
84- // Mask away the flag bits
85- tmp[ 0 ] &= 0b0001_1111 ;
98+ EncodingFlags :: remove_flags ( & mut tmp) ;
8699 }
87- deserialize_fq ( tmp) . ok_or ( SerializationError :: InvalidData )
100+ tmp
88101}
89102
90103pub ( crate ) fn read_g1_compressed < R : ark_serialize:: Read > (
@@ -97,20 +110,26 @@ pub(crate) fn read_g1_compressed<R: ark_serialize::Read>(
97110 . ok_or ( SerializationError :: InvalidData ) ?;
98111
99112 // Obtain the three flags from the start of the byte sequence
100- let flags = EncodingFlags :: get_flags ( & bytes[ ..] ) ;
113+ let flags = EncodingFlags :: get_flags ( & bytes[ ..] ) ? ;
101114
102- // we expect to be deserializing a compressed point
115+ // We expect to be deserializing a compressed point
103116 if !flags. is_compressed {
104117 return Err ( SerializationError :: UnexpectedFlags ) ;
105118 }
106119
120+ // Attempt to obtain the x-coordinate
121+ let x_bytes = read_bytes_with_offset ( & bytes, 0 , true ) ;
122+
107123 if flags. is_infinity {
124+ // Check that the `x` co-ordinate was `0`
125+ if x_bytes != [ 0u8 ; 48 ] {
126+ return Err ( SerializationError :: InvalidData ) ;
127+ }
128+
108129 return Ok ( G1Affine :: zero ( ) ) ;
109130 }
110131
111- // Attempt to obtain the x-coordinate
112- let x = read_fq_with_offset ( & bytes, 0 , true ) ?;
113-
132+ let x = deserialize_fq ( x_bytes) . ok_or ( SerializationError :: InvalidData ) ?;
114133 let p = G1Affine :: get_point_from_x_unchecked ( x, flags. is_lexographically_largest )
115134 . ok_or ( SerializationError :: InvalidData ) ?;
116135
@@ -126,22 +145,27 @@ pub(crate) fn read_g1_uncompressed<R: ark_serialize::Read>(
126145 . map_err ( |_| SerializationError :: InvalidData ) ?;
127146
128147 // Obtain the three flags from the start of the byte sequence
129- let flags = EncodingFlags :: get_flags ( & bytes[ ..] ) ;
148+ let flags = EncodingFlags :: get_flags ( & bytes[ ..] ) ? ;
130149
131150 // we expect to be deserializing an uncompressed point
132151 if flags. is_compressed {
133152 return Err ( SerializationError :: UnexpectedFlags ) ;
134153 }
135154
155+ let x_bytes = read_bytes_with_offset ( & bytes, 0 , true ) ;
156+ let y_bytes = read_bytes_with_offset ( & bytes, 1 , false ) ;
157+
136158 if flags. is_infinity {
159+ if x_bytes != [ 0u8 ; 48 ] || y_bytes != [ 0u8 ; 48 ] {
160+ return Err ( SerializationError :: InvalidData ) ;
161+ }
137162 return Ok ( G1Affine :: zero ( ) ) ;
138163 }
139164
140165 // Attempt to obtain the x-coordinate
141- let x = read_fq_with_offset ( & bytes , 0 , true ) ?;
166+ let x = deserialize_fq ( x_bytes ) . ok_or ( SerializationError :: InvalidData ) ?;
142167 // Attempt to obtain the y-coordinate
143- let y = read_fq_with_offset ( & bytes, 1 , false ) ?;
144-
168+ let y = deserialize_fq ( y_bytes) . ok_or ( SerializationError :: InvalidData ) ?;
145169 let p = G1Affine :: new_unchecked ( x, y) ;
146170
147171 Ok ( p)
@@ -156,21 +180,26 @@ pub(crate) fn read_g2_compressed<R: ark_serialize::Read>(
156180 . map_err ( |_| SerializationError :: InvalidData ) ?;
157181
158182 // Obtain the three flags from the start of the byte sequence
159- let flags = EncodingFlags :: get_flags ( & bytes) ;
183+ let flags = EncodingFlags :: get_flags ( & bytes) ? ;
160184
161185 // we expect to be deserializing a compressed point
162186 if !flags. is_compressed {
163187 return Err ( SerializationError :: UnexpectedFlags ) ;
164188 }
165189
190+ let xc1_bytes = read_bytes_with_offset ( & bytes, 0 , true ) ;
191+ let xc0_bytes = read_bytes_with_offset ( & bytes, 1 , false ) ;
192+
166193 if flags. is_infinity {
194+ if xc1_bytes != [ 0u8 ; 48 ] || xc0_bytes != [ 0u8 ; 48 ] {
195+ return Err ( SerializationError :: InvalidData ) ;
196+ }
167197 return Ok ( G2Affine :: zero ( ) ) ;
168198 }
169199
170200 // Attempt to obtain the x-coordinate
171- let xc1 = read_fq_with_offset ( & bytes, 0 , true ) ?;
172- let xc0 = read_fq_with_offset ( & bytes, 1 , false ) ?;
173-
201+ let xc1 = deserialize_fq ( xc1_bytes) . ok_or ( SerializationError :: InvalidData ) ?;
202+ let xc0 = deserialize_fq ( xc0_bytes) . ok_or ( SerializationError :: InvalidData ) ?;
174203 let x = Fq2 :: new ( xc0, xc1) ;
175204
176205 let p = G2Affine :: get_point_from_x_unchecked ( x, flags. is_lexographically_largest )
@@ -188,25 +217,39 @@ pub(crate) fn read_g2_uncompressed<R: ark_serialize::Read>(
188217 . map_err ( |_| SerializationError :: InvalidData ) ?;
189218
190219 // Obtain the three flags from the start of the byte sequence
191- let flags = EncodingFlags :: get_flags ( & bytes) ;
220+ let flags = EncodingFlags :: get_flags ( & bytes) ? ;
192221
193222 // we expect to be deserializing an uncompressed point
194223 if flags. is_compressed {
195224 return Err ( SerializationError :: UnexpectedFlags ) ;
196225 }
197226
227+ let xc1_bytes = read_bytes_with_offset ( & bytes, 0 , true ) ;
228+ let xc0_bytes = read_bytes_with_offset ( & bytes, 1 , false ) ;
229+
230+ let yc1_bytes = read_bytes_with_offset ( & bytes, 2 , false ) ;
231+ let yc0_bytes = read_bytes_with_offset ( & bytes, 3 , false ) ;
232+
198233 if flags. is_infinity {
234+ if xc1_bytes != [ 0u8 ; 48 ]
235+ || xc0_bytes != [ 0u8 ; 48 ]
236+ || yc1_bytes != [ 0u8 ; 48 ]
237+ || yc0_bytes != [ 0u8 ; 48 ]
238+ {
239+ return Err ( SerializationError :: InvalidData ) ;
240+ }
199241 return Ok ( G2Affine :: zero ( ) ) ;
200242 }
201243
244+ let xc1 = deserialize_fq ( xc1_bytes) . ok_or ( SerializationError :: InvalidData ) ?;
245+ let xc0 = deserialize_fq ( xc0_bytes) . ok_or ( SerializationError :: InvalidData ) ?;
246+ let yc1 = deserialize_fq ( yc1_bytes) . ok_or ( SerializationError :: InvalidData ) ?;
247+ let yc0 = deserialize_fq ( yc0_bytes) . ok_or ( SerializationError :: InvalidData ) ?;
248+
202249 // Attempt to obtain the x-coordinate
203- let xc1 = read_fq_with_offset ( & bytes, 0 , true ) ?;
204- let xc0 = read_fq_with_offset ( & bytes, 1 , false ) ?;
205250 let x = Fq2 :: new ( xc0, xc1) ;
206251
207252 // Attempt to obtain the y-coordinate
208- let yc1 = read_fq_with_offset ( & bytes, 2 , false ) ?;
209- let yc0 = read_fq_with_offset ( & bytes, 3 , false ) ?;
210253 let y = Fq2 :: new ( yc0, yc1) ;
211254
212255 let p = G2Affine :: new_unchecked ( x, y) ;
0 commit comments