forked from the729/lcs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
big.go
33 lines (25 loc) · 727 Bytes
/
big.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package lcs
import "math/big"
// Convert BgiInt to bytes with little endian (required by Libra).
func BigToBytes(val *big.Int, bytesLen int) []byte {
bytes := val.Bytes()
if len(bytes) < bytesLen {
diff := bytesLen - len(bytes)
for i := 0; i < diff; i++ {
bytes = append([]byte{0}, bytes...)
}
}
for i := 0; i < len(bytes)/2; i++ {
bytes[i], bytes[len(bytes)-i-1] = bytes[len(bytes)-i-1], bytes[i]
}
return bytes
}
// Convert little endian bytes to big endian bytes and then to sdk.Int.
func LeToBig(bytes []byte) *big.Int {
for i := 0; i < len(bytes)/2; i++ {
bytes[len(bytes)-i-1], bytes[i] = bytes[i], bytes[len(bytes)-i-1]
}
bigValue := &big.Int{}
bigValue.SetBytes(bytes)
return bigValue
}