@@ -19,7 +19,7 @@ use crate::traits::AsBytes;
1919use crate :: traits:: ByteConversion ;
2020use crate :: unsigned_integer:: traits:: IsUnsignedInteger ;
2121
22- use core:: fmt:: { self , Debug , Display } ;
22+ use core:: fmt:: { self , Debug , Display , LowerHex , UpperHex } ;
2323
2424pub type U384 = UnsignedInteger < 6 > ;
2525pub type U256 = UnsignedInteger < 4 > ;
@@ -109,12 +109,37 @@ impl<const NUM_LIMBS: usize> From<&str> for UnsignedInteger<NUM_LIMBS> {
109109
110110impl < const NUM_LIMBS : usize > Display for UnsignedInteger < NUM_LIMBS > {
111111 fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
112+ write ! ( f, "0x" ) ?;
113+
112114 let mut limbs_iterator = self . limbs . iter ( ) . skip_while ( |limb| * * limb == 0 ) . peekable ( ) ;
113115
114116 if limbs_iterator. peek ( ) . is_none ( ) {
115- write ! ( f, "0x0 " ) ?;
117+ write ! ( f, "0 " ) ?;
116118 } else {
119+ if let Some ( most_significant_limb) = limbs_iterator. next ( ) {
120+ write ! ( f, "{most_significant_limb:x}" ) ?;
121+ }
122+
123+ for limb in limbs_iterator {
124+ write ! ( f, "{limb:016x}" ) ?;
125+ }
126+ }
127+
128+ Ok ( ( ) )
129+ }
130+ }
131+
132+ impl < const NUM_LIMBS : usize > LowerHex for UnsignedInteger < NUM_LIMBS > {
133+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
134+ if f. alternate ( ) {
117135 write ! ( f, "0x" ) ?;
136+ }
137+
138+ let mut limbs_iterator = self . limbs . iter ( ) . skip_while ( |limb| * * limb == 0 ) . peekable ( ) ;
139+
140+ if limbs_iterator. peek ( ) . is_none ( ) {
141+ write ! ( f, "0" ) ?;
142+ } else {
118143 if let Some ( most_significant_limb) = limbs_iterator. next ( ) {
119144 write ! ( f, "{most_significant_limb:x}" ) ?;
120145 }
@@ -128,6 +153,30 @@ impl<const NUM_LIMBS: usize> Display for UnsignedInteger<NUM_LIMBS> {
128153 }
129154}
130155
156+ impl < const NUM_LIMBS : usize > UpperHex for UnsignedInteger < NUM_LIMBS > {
157+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
158+ if f. alternate ( ) {
159+ write ! ( f, "0X" ) ?;
160+ }
161+
162+ let mut limbs_iterator = self . limbs . iter ( ) . skip_while ( |limb| * * limb == 0 ) . peekable ( ) ;
163+
164+ if limbs_iterator. peek ( ) . is_none ( ) {
165+ write ! ( f, "0" ) ?;
166+ } else {
167+ if let Some ( most_significant_limb) = limbs_iterator. next ( ) {
168+ write ! ( f, "{most_significant_limb:X}" ) ?;
169+ }
170+
171+ for limb in limbs_iterator {
172+ write ! ( f, "{limb:016X}" ) ?;
173+ }
174+ }
175+
176+ Ok ( ( ) )
177+ }
178+ }
179+
131180// impl Add for both references and variables
132181
133182impl < const NUM_LIMBS : usize > Add < & UnsignedInteger < NUM_LIMBS > > for & UnsignedInteger < NUM_LIMBS > {
0 commit comments