1
+ mod num_traits_impl;
1
2
mod primitive_conversions;
2
- mod qm31_num_traits_impl;
3
3
4
4
use core:: fmt;
5
5
@@ -39,7 +39,7 @@ impl fmt::Display for QM31Error {
39
39
}
40
40
41
41
/// Definition of a Quad M31 in its reduced form. The internal representation
42
- /// is composed by the coordinates of the QM31, following a little endian ordering.
42
+ /// is composed by the coordinates of the QM31, following a big endian ordering.
43
43
#[ repr( transparent) ]
44
44
#[ derive( Debug , Clone , Copy , PartialEq , Eq , PartialOrd , Ord , Hash ) ]
45
45
pub struct QM31 ( [ u64 ; 4 ] ) ;
@@ -68,26 +68,26 @@ impl QM31 {
68
68
/// Packs the [QM31] coordinates into a Felt.
69
69
fn pack_into_felt ( & self ) -> Felt {
70
70
let mut felt_bytes = [ 0 ; 32 ] ;
71
- let bytes = self . to_le_bytes ( ) ;
71
+ let bytes = self . to_bytes_le ( ) ;
72
72
73
73
felt_bytes[ 0 ..18 ] . copy_from_slice ( & bytes) ;
74
74
75
75
Felt :: from_bytes_le ( & felt_bytes)
76
76
}
77
77
78
78
fn to_biguint ( & self ) -> BigUint {
79
- let bytes = self . to_le_bytes ( ) ;
80
-
81
- BigUint :: from_bytes_le ( & bytes)
79
+ dbg ! ( self ) ;
80
+ let bytes = self . to_bytes_be ( ) ;
81
+ BigUint :: from_bytes_be ( & bytes)
82
82
}
83
83
84
84
fn to_bigint ( & self ) -> BigInt {
85
85
self . to_biguint ( ) . into ( )
86
86
}
87
87
88
88
/// Convert `self`'s inner into an array of bytes,
89
- /// following the little endian ordering.
90
- pub fn to_le_bytes ( & self ) -> [ u8 ; 18 ] {
89
+ /// following the big endian ordering.
90
+ pub fn to_bytes_le ( & self ) -> [ u8 ; 18 ] {
91
91
let coordinates = self . inner ( ) ;
92
92
93
93
let mut result_bytes = [ 0u8 ; 18 ] ;
@@ -99,6 +99,22 @@ impl QM31 {
99
99
result_bytes
100
100
}
101
101
102
+ /// Convert `self`'s inner into an array of bytes,
103
+ /// following the big endian ordering.
104
+ pub fn to_bytes_be ( & self ) -> [ u8 ; 18 ] {
105
+ let coordinates = self . inner ( ) ;
106
+
107
+ let mut result_bytes = [ 0u8 ; 18 ] ;
108
+ let bytes_part1 =
109
+ ( ( ( coordinates[ 0 ] as u128 ) << 36 ) + ( coordinates[ 1 ] as u128 ) ) . to_be_bytes ( ) ;
110
+ let bytes_part2 =
111
+ ( ( ( coordinates[ 2 ] as u128 ) << 36 ) + ( coordinates[ 3 ] as u128 ) ) . to_be_bytes ( ) ;
112
+ result_bytes[ 0 ..9 ] . copy_from_slice ( & bytes_part1[ 7 ..16 ] ) ;
113
+ result_bytes[ 9 ..18 ] . copy_from_slice ( & bytes_part2[ 7 ..16 ] ) ;
114
+
115
+ result_bytes
116
+ }
117
+
102
118
/// Computes the addition of two [QM31] elements in reduced form.
103
119
pub fn add ( & self , rhs : & QM31 ) -> QM31 {
104
120
let coordinates1 = self . inner ( ) ;
0 commit comments