@@ -7,7 +7,10 @@ mod der;
77mod rlp;
88
99use super :: Uint ;
10- use crate :: { Encoding , Limb , Word } ;
10+ use crate :: { Limb , Word } ;
11+
12+ #[ cfg( feature = "generic-array" ) ]
13+ use crate :: Encoding ;
1114
1215impl < const LIMBS : usize > Uint < LIMBS > {
1316 /// Create a new [`Uint`] from the provided big endian bytes.
@@ -124,6 +127,7 @@ impl<const LIMBS: usize> Uint<LIMBS> {
124127
125128 /// Serialize this [`Uint`] as big-endian, writing it into the provided
126129 /// byte slice.
130+ #[ cfg( feature = "generic-array" ) ]
127131 #[ inline]
128132 pub ( crate ) fn write_be_bytes ( & self , out : & mut [ u8 ] ) {
129133 debug_assert_eq ! ( out. len( ) , Limb :: BYTES * LIMBS ) ;
@@ -141,6 +145,7 @@ impl<const LIMBS: usize> Uint<LIMBS> {
141145
142146 /// Serialize this [`Uint`] as little-endian, writing it into the provided
143147 /// byte slice.
148+ #[ cfg( feature = "generic-array" ) ]
144149 #[ inline]
145150 pub ( crate ) fn write_le_bytes ( & self , out : & mut [ u8 ] ) {
146151 debug_assert_eq ! ( out. len( ) , Limb :: BYTES * LIMBS ) ;
@@ -156,6 +161,58 @@ impl<const LIMBS: usize> Uint<LIMBS> {
156161 }
157162}
158163
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+
159216/// Decode a single nibble of upper or lower hex
160217#[ inline( always) ]
161218const fn decode_nibble ( src : u8 ) -> u16 {
0 commit comments