Skip to content

Commit 36d7070

Browse files
committed
fix: improve bigIntToTwosComplement to handle large values and normalize byte representation
1 parent e777a3b commit 36d7070

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

decimal.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -154,11 +154,12 @@ func bigIntToTwosComplement(value *big.Int) ([32]byte, uint8, error) {
154154
return [32]byte{0}, 31, nil
155155
}
156156
if value.Sign() > 0 {
157-
bytes := value.Bytes()
158-
if bytes[0]&0x80 != 0 {
159-
bytes = append([]byte{0x00}, bytes...)
157+
if value.BitLen() > 255 {
158+
return [32]byte{}, 0, fmt.Errorf("decimal unscaled value exceeds 32 bytes")
160159
}
161-
return normalizeTwosComplement(bytes)
160+
buf := [32]byte{}
161+
value.FillBytes(buf[:])
162+
return buf, uint8(trimTwosComplement(buf[:])), nil
162163
}
163164

164165
bitLen := value.BitLen()

0 commit comments

Comments
 (0)