/
bigint.go
91 lines (80 loc) · 2.05 KB
/
bigint.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/*
* Copyright (C) 2018 The ontology Authors
* This file is part of The ontology library.
*
* The ontology is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* The ontology is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with The ontology. If not, see <http://www.gnu.org/licenses/>.
*/
package ont
import "math/big"
func bytesReverse(u []byte) []byte {
for i, j := 0, len(u)-1; i < j; i, j = i+1, j-1 {
u[i], u[j] = u[j], u[i]
}
return u
}
var bigOne = big.NewInt(1)
// neo encoding: https://docs.microsoft.com/en-us/dotnet/api/system.numerics.biginteger.tobytearray?view=netframework-4.7.2
func BigIntToNeoBytes(data *big.Int) []byte {
bs := data.Bytes()
if len(bs) == 0 {
return []byte{}
}
// golang big.Int use big-endian
bytesReverse(bs)
// bs now is little-endian
if data.Sign() < 0 {
for i, b := range bs {
bs[i] = ^b
}
for i := 0; i < len(bs); i++ {
if bs[i] == 255 {
bs[i] = 0
} else {
bs[i] += 1
break
}
}
if bs[len(bs)-1] < 128 {
bs = append(bs, 255)
}
} else {
if bs[len(bs)-1] >= 128 {
bs = append(bs, 0)
}
}
return bs
}
func BigIntFromNeoBytes(ba []byte) *big.Int {
res := big.NewInt(0)
l := len(ba)
if l == 0 {
return res
}
bytes := make([]byte, 0, l)
bytes = append(bytes, ba...)
bytesReverse(bytes)
if bytes[0]>>7 == 1 {
for i, b := range bytes {
bytes[i] = ^b
}
temp := big.NewInt(0)
temp.SetBytes(bytes)
temp.Add(temp, bigOne)
bytes = temp.Bytes()
res.SetBytes(bytes)
return res.Neg(res)
}
res.SetBytes(bytes)
return res
}