File tree 2 files changed +17
-4
lines changed
2 files changed +17
-4
lines changed Original file line number Diff line number Diff line change @@ -176,13 +176,14 @@ func MustDecodeBig(input string) *big.Int {
176
176
}
177
177
178
178
// EncodeBig encodes bigint as a hex string with 0x prefix.
179
- // The sign of the integer is ignored.
180
179
func EncodeBig (bigint * big.Int ) string {
181
- nbits := bigint .BitLen ()
182
- if nbits == 0 {
180
+ if sign := bigint .Sign (); sign == 0 {
183
181
return "0x0"
182
+ } else if sign > 0 {
183
+ return "0x" + bigint .Text (16 )
184
+ } else {
185
+ return "-0x" + bigint .Text (16 )[1 :]
184
186
}
185
- return fmt .Sprintf ("%#x" , bigint )
186
187
}
187
188
188
189
func has0xPrefix (input string ) bool {
Original file line number Diff line number Diff line change @@ -201,3 +201,15 @@ func TestDecodeUint64(t *testing.T) {
201
201
}
202
202
}
203
203
}
204
+
205
+ func BenchmarkEncodeBig (b * testing.B ) {
206
+ for _ , bench := range encodeBigTests {
207
+ b .Run (bench .want , func (b * testing.B ) {
208
+ b .ReportAllocs ()
209
+ bigint := bench .input .(* big.Int )
210
+ for i := 0 ; i < b .N ; i ++ {
211
+ EncodeBig (bigint )
212
+ }
213
+ })
214
+ }
215
+ }
You can’t perform that action at this time.
0 commit comments