@@ -7,6 +7,43 @@ use crate::{
77 error:: Error ,
88} ;
99
10+ /// Compresse an G1 point to a buffer.
11+ ///
12+ /// This is a reveresed function against `unchecked_compressed_x_to_g1_point`, return the compressed
13+ /// G1 point which hardcoded the sign flag of y coordinate.
14+ pub ( crate ) fn compress_g1_point_to_x ( g1 : & AffineG1 ) -> Result < [ u8 ; 32 ] , Error > {
15+ let mut x_bytes = [ 0u8 ; 32 ] ;
16+ g1. x ( ) . to_big_endian ( & mut x_bytes) . map_err ( Error :: Field ) ?;
17+
18+ if g1. y ( ) > -g1. y ( ) {
19+ x_bytes[ 0 ] |= CompressedPointFlag :: Negative as u8 ;
20+ } else {
21+ x_bytes[ 0 ] = ( x_bytes[ 0 ] & !MASK ) | ( CompressedPointFlag :: Positive as u8 ) ;
22+ }
23+
24+ Ok ( x_bytes)
25+ }
26+
27+ /// Compresse an G2 point to a buffer.
28+ ///
29+ /// This is a reveresed function against `unchecked_compressed_x_to_g1_point`, return the compressed
30+ /// G2 point which hardcoded the sign flag of y coordinate.
31+ pub ( crate ) fn compress_g2_point_to_x ( g2 : & AffineG2 ) -> Result < [ u8 ; 64 ] , Error > {
32+ let mut x_bytes = [ 0u8 ; 64 ] ;
33+ let x1 = Fq :: from_u256 ( g2. x ( ) . 0 . imaginary ( ) . 0 ) . map_err ( Error :: Field ) ?;
34+ let x0 = Fq :: from_u256 ( g2. x ( ) . 0 . real ( ) . 0 ) . map_err ( Error :: Field ) ?;
35+ x1. to_big_endian ( & mut x_bytes[ ..32 ] ) . map_err ( Error :: Field ) ?;
36+ x0. to_big_endian ( & mut x_bytes[ 32 ..64 ] ) . map_err ( Error :: Field ) ?;
37+
38+ if g2. y ( ) . 0 > -g2. y ( ) . 0 {
39+ x_bytes[ 0 ] |= CompressedPointFlag :: Negative as u8 ;
40+ } else {
41+ x_bytes[ 0 ] = ( x_bytes[ 0 ] & !MASK ) | ( CompressedPointFlag :: Positive as u8 ) ;
42+ }
43+
44+ Ok ( x_bytes)
45+ }
46+
1047/// Deserializes an Fq element from a buffer.
1148///
1249/// If this Fq element is part of a compressed point, the flag that indicates the sign of the
@@ -70,6 +107,16 @@ pub(crate) fn uncompressed_bytes_to_g1_point(buf: &[u8]) -> Result<AffineG1, Err
70107 AffineG1 :: new ( x, y) . map_err ( Error :: Group )
71108}
72109
110+ /// Converts an AffineG1 point to an uncompressed byte array.
111+ ///
112+ /// The uncompressed byte array is represented as two fq elements.
113+ pub ( crate ) fn g1_point_to_uncompressed_bytes ( point : & AffineG1 ) -> Result < [ u8 ; 64 ] , Error > {
114+ let mut buffer = [ 0u8 ; 64 ] ;
115+ point. x ( ) . to_big_endian ( & mut buffer[ ..32 ] ) . map_err ( Error :: Field ) ?;
116+ point. y ( ) . to_big_endian ( & mut buffer[ 32 ..] ) . map_err ( Error :: Field ) ?;
117+ Ok ( buffer)
118+ }
119+
73120/// Converts a compressed G2 point to an AffineG2 point.
74121///
75122/// Asserts that the compressed point is represented as a single fq2 element: the x coordinate
@@ -120,3 +167,28 @@ pub(crate) fn uncompressed_bytes_to_g2_point(buf: &[u8]) -> Result<AffineG2, Err
120167
121168 AffineG2 :: new ( x, y) . map_err ( Error :: Group )
122169}
170+
171+ /// Converts an AffineG2 point to an uncompressed byte array.
172+ ///
173+ /// The uncompressed byte array is represented as two fq2 elements.
174+ pub ( crate ) fn g2_point_to_uncompressed_bytes ( point : & AffineG2 ) -> Result < [ u8 ; 128 ] , Error > {
175+ let mut buffer = [ 0u8 ; 128 ] ;
176+ Fq :: from_u256 ( point. x ( ) . 0 . imaginary ( ) . 0 )
177+ . unwrap ( )
178+ . to_big_endian ( & mut buffer[ ..32 ] )
179+ . map_err ( Error :: Field ) ?;
180+ Fq :: from_u256 ( point. x ( ) . 0 . real ( ) . 0 )
181+ . unwrap ( )
182+ . to_big_endian ( & mut buffer[ 32 ..64 ] )
183+ . map_err ( Error :: Field ) ?;
184+ Fq :: from_u256 ( point. y ( ) . 0 . imaginary ( ) . 0 )
185+ . unwrap ( )
186+ . to_big_endian ( & mut buffer[ 64 ..96 ] )
187+ . map_err ( Error :: Field ) ?;
188+ Fq :: from_u256 ( point. y ( ) . 0 . real ( ) . 0 )
189+ . unwrap ( )
190+ . to_big_endian ( & mut buffer[ 96 ..128 ] )
191+ . map_err ( Error :: Field ) ?;
192+
193+ Ok ( buffer)
194+ }
0 commit comments