@@ -7,7 +7,10 @@ mod der;
7
7
mod rlp;
8
8
9
9
use super :: Uint ;
10
- use crate :: { Encoding , Limb , Word } ;
10
+ use crate :: { Limb , Word } ;
11
+
12
+ #[ cfg( feature = "generic-array" ) ]
13
+ use crate :: Encoding ;
11
14
12
15
impl < const LIMBS : usize > Uint < LIMBS > {
13
16
/// Create a new [`Uint`] from the provided big endian bytes.
@@ -124,6 +127,7 @@ impl<const LIMBS: usize> Uint<LIMBS> {
124
127
125
128
/// Serialize this [`Uint`] as big-endian, writing it into the provided
126
129
/// byte slice.
130
+ #[ cfg( feature = "generic-array" ) ]
127
131
#[ inline]
128
132
pub ( crate ) fn write_be_bytes ( & self , out : & mut [ u8 ] ) {
129
133
debug_assert_eq ! ( out. len( ) , Limb :: BYTES * LIMBS ) ;
@@ -141,6 +145,7 @@ impl<const LIMBS: usize> Uint<LIMBS> {
141
145
142
146
/// Serialize this [`Uint`] as little-endian, writing it into the provided
143
147
/// byte slice.
148
+ #[ cfg( feature = "generic-array" ) ]
144
149
#[ inline]
145
150
pub ( crate ) fn write_le_bytes ( & self , out : & mut [ u8 ] ) {
146
151
debug_assert_eq ! ( out. len( ) , Limb :: BYTES * LIMBS ) ;
@@ -156,6 +161,58 @@ impl<const LIMBS: usize> Uint<LIMBS> {
156
161
}
157
162
}
158
163
164
+ /// Encode a [`Uint`] to a big endian byte array of the given size.
165
+ pub ( crate ) const fn uint_to_be_bytes < const LIMBS : usize , const BYTES : usize > (
166
+ uint : & Uint < LIMBS > ,
167
+ ) -> [ u8 ; BYTES ] {
168
+ if BYTES != LIMBS * Limb :: BYTES {
169
+ panic ! ( "BYTES != LIMBS * Limb::BYTES" ) ;
170
+ }
171
+
172
+ let mut ret = [ 0u8 ; BYTES ] ;
173
+ let mut i = 0 ;
174
+
175
+ while i < LIMBS {
176
+ let limb_bytes = uint. limbs [ LIMBS - i - 1 ] . 0 . to_be_bytes ( ) ;
177
+ let mut j = 0 ;
178
+
179
+ while j < Limb :: BYTES {
180
+ ret[ i * Limb :: BYTES + j] = limb_bytes[ j] ;
181
+ j += 1 ;
182
+ }
183
+
184
+ i += 1 ;
185
+ }
186
+
187
+ ret
188
+ }
189
+
190
+ /// Encode a [`Uint`] to a little endian byte array of the given size.
191
+ pub ( crate ) const fn uint_to_le_bytes < const LIMBS : usize , const BYTES : usize > (
192
+ uint : & Uint < LIMBS > ,
193
+ ) -> [ u8 ; BYTES ] {
194
+ if BYTES != LIMBS * Limb :: BYTES {
195
+ panic ! ( "BYTES != LIMBS * Limb::BYTES" ) ;
196
+ }
197
+
198
+ let mut ret = [ 0u8 ; BYTES ] ;
199
+ let mut i = 0 ;
200
+
201
+ while i < LIMBS {
202
+ let limb_bytes = uint. limbs [ i] . 0 . to_le_bytes ( ) ;
203
+ let mut j = 0 ;
204
+
205
+ while j < Limb :: BYTES {
206
+ ret[ i * Limb :: BYTES + j] = limb_bytes[ j] ;
207
+ j += 1 ;
208
+ }
209
+
210
+ i += 1 ;
211
+ }
212
+
213
+ ret
214
+ }
215
+
159
216
/// Decode a single nibble of upper or lower hex
160
217
#[ inline( always) ]
161
218
const fn decode_nibble ( src : u8 ) -> u16 {
0 commit comments