@@ -8,6 +8,9 @@ use crate::field::{
8
8
#[ cfg( feature = "lambdaworks-serde-binary" ) ]
9
9
use crate :: traits:: ByteConversion ;
10
10
11
+ #[ cfg( all( feature = "lambdaworks-serde-binary" , feature = "alloc" ) ) ]
12
+ use crate :: traits:: AsBytes ;
13
+
11
14
/// We are implementig the extension of Baby Bear of degree 4 using the irreducible polynomial x^4 + 11.
12
15
/// BETA = 11 and -BETA = -11 is the non-residue.
13
16
pub const BETA : FieldElement < Babybear31PrimeField > =
@@ -262,6 +265,59 @@ impl ByteConversion for [FieldElement<Babybear31PrimeField>; 4] {
262
265
}
263
266
}
264
267
268
+ #[ cfg( feature = "lambdaworks-serde-binary" ) ]
269
+ impl ByteConversion for FieldElement < Degree4BabyBearExtensionField > {
270
+ fn to_bytes_be ( & self ) -> alloc:: vec:: Vec < u8 > {
271
+ let mut byte_slice = ByteConversion :: to_bytes_be ( & self . value ( ) [ 0 ] ) ;
272
+ byte_slice. extend ( ByteConversion :: to_bytes_be ( & self . value ( ) [ 1 ] ) ) ;
273
+ byte_slice. extend ( ByteConversion :: to_bytes_be ( & self . value ( ) [ 2 ] ) ) ;
274
+ byte_slice. extend ( ByteConversion :: to_bytes_be ( & self . value ( ) [ 3 ] ) ) ;
275
+ byte_slice
276
+ }
277
+
278
+ fn to_bytes_le ( & self ) -> alloc:: vec:: Vec < u8 > {
279
+ let mut byte_slice = ByteConversion :: to_bytes_le ( & self . value ( ) [ 0 ] ) ;
280
+ byte_slice. extend ( ByteConversion :: to_bytes_le ( & self . value ( ) [ 1 ] ) ) ;
281
+ byte_slice. extend ( ByteConversion :: to_bytes_le ( & self . value ( ) [ 2 ] ) ) ;
282
+ byte_slice. extend ( ByteConversion :: to_bytes_le ( & self . value ( ) [ 3 ] ) ) ;
283
+ byte_slice
284
+ }
285
+
286
+ fn from_bytes_be ( bytes : & [ u8 ] ) -> Result < Self , crate :: errors:: ByteConversionError >
287
+ where
288
+ Self : Sized ,
289
+ {
290
+ const BYTES_PER_FIELD : usize = 8 ;
291
+ let x0 = FieldElement :: from_bytes_be ( & bytes[ 0 ..BYTES_PER_FIELD ] ) ?;
292
+ let x1 = FieldElement :: from_bytes_be ( & bytes[ BYTES_PER_FIELD ..BYTES_PER_FIELD * 2 ] ) ?;
293
+ let x2 = FieldElement :: from_bytes_be ( & bytes[ BYTES_PER_FIELD * 2 ..BYTES_PER_FIELD * 3 ] ) ?;
294
+ let x3 = FieldElement :: from_bytes_be ( & bytes[ BYTES_PER_FIELD * 3 ..BYTES_PER_FIELD * 4 ] ) ?;
295
+
296
+ Ok ( Self :: new ( [ x0, x1, x2, x3] ) )
297
+ }
298
+
299
+ fn from_bytes_le ( bytes : & [ u8 ] ) -> Result < Self , crate :: errors:: ByteConversionError >
300
+ where
301
+ Self : Sized ,
302
+ {
303
+ const BYTES_PER_FIELD : usize = 8 ;
304
+ let x0 = FieldElement :: from_bytes_le ( & bytes[ 0 ..BYTES_PER_FIELD ] ) ?;
305
+ let x1 = FieldElement :: from_bytes_le ( & bytes[ BYTES_PER_FIELD ..BYTES_PER_FIELD * 2 ] ) ?;
306
+ let x2 = FieldElement :: from_bytes_le ( & bytes[ BYTES_PER_FIELD * 2 ..BYTES_PER_FIELD * 3 ] ) ?;
307
+ let x3 = FieldElement :: from_bytes_le ( & bytes[ BYTES_PER_FIELD * 3 ..BYTES_PER_FIELD * 4 ] ) ?;
308
+
309
+ Ok ( Self :: new ( [ x0, x1, x2, x3] ) )
310
+ }
311
+ }
312
+
313
+ #[ cfg( feature = "lambdaworks-serde-binary" ) ]
314
+ #[ cfg( feature = "alloc" ) ]
315
+ impl AsBytes for FieldElement < Degree4BabyBearExtensionField > {
316
+ fn as_bytes ( & self ) -> alloc:: vec:: Vec < u8 > {
317
+ self . to_bytes_be ( )
318
+ }
319
+ }
320
+
265
321
impl IsFFTField for Degree4BabyBearExtensionField {
266
322
const TWO_ADICITY : u64 = 29 ;
267
323
const TWO_ADIC_PRIMITVE_ROOT_OF_UNITY : Self :: BaseType = [
0 commit comments