/
word256.go
197 lines (155 loc) · 3.69 KB
/
word256.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
// Copyright Monax Industries Limited
// SPDX-License-Identifier: Apache-2.0
package binary
import (
"bytes"
"encoding/binary"
"fmt"
"math/big"
"sort"
"github.com/tmthrgd/go-hex"
)
var (
Zero256 = Word256{}
One256 = LeftPadWord256([]byte{1})
)
const Word256Bytes = 32
const Word256Bits = Word256Bytes * 8
var BigWord256Bytes = big.NewInt(Word256Bytes)
var trimCutSet = string([]byte{0})
type Word256 [Word256Bytes]byte
func (w *Word256) UnmarshalText(hexBytes []byte) error {
bs, err := hex.DecodeString(string(hexBytes))
if err != nil {
return err
}
copy(w[:], bs)
return nil
}
func (w Word256) MarshalText() ([]byte, error) {
return []byte(w.String()), nil
}
func (w Word256) String() string {
return hex.EncodeUpperToString(w[:])
}
func (w Word256) Copy() Word256 {
return w
}
func (w Word256) Bytes() []byte {
return w[:]
}
// copied.
func (w Word256) Prefix(n int) []byte {
return w[:n]
}
func (w Word256) Postfix(n int) []byte {
return w[32-n:]
}
// Get a Word160 embedded a Word256 and padded on the left (as it is for account addresses in EVM)
func (w Word256) Word160() (w160 Word160) {
copy(w160[:], w[Word256Word160Delta:])
return
}
func (w Word256) IsZero() bool {
accum := byte(0)
for _, byt := range w {
accum |= byt
}
return accum == 0
}
func (w Word256) Compare(other Word256) int {
return bytes.Compare(w[:], other[:])
}
func (w Word256) UnpadLeft() []byte {
return bytes.TrimLeft(w[:], trimCutSet)
}
func (w Word256) UnpadRight() []byte {
return bytes.TrimRight(w[:], trimCutSet)
}
// Gogo proto support
func (w *Word256) Marshal() ([]byte, error) {
if w == nil {
return nil, nil
}
return w.Bytes(), nil
}
func (w *Word256) Unmarshal(data []byte) error {
if len(data) == 0 {
return nil
}
if len(data) != Word256Bytes {
return fmt.Errorf("error unmarshallling Word256 '%X' from bytes: %d bytes but should have %d bytes",
data, len(data), Word256Bytes)
}
copy(w[:], data)
return nil
}
func (w *Word256) MarshalTo(data []byte) (int, error) {
if w == nil {
return 0, nil
}
return copy(data, w[:]), nil
}
func (w Word256) Size() int {
return Word256Bytes
}
func Uint64ToWord256(i uint64) (word Word256) {
binary.BigEndian.PutUint64(word[24:], i)
return
}
func Uint64FromWord256(word Word256) uint64 {
return binary.BigEndian.Uint64(word.Postfix(8))
}
func Int64ToWord256(i int64) Word256 {
return BigIntToWord256(SignExtend(big.NewInt(i), Word256Bits))
}
func Int64FromWord256(word Word256) int64 {
return BigIntFromWord256(word).Int64()
}
func BigIntToWord256(x *big.Int) Word256 {
return LeftPadWord256(U256(x).Bytes())
}
func BigIntFromWord256(word Word256) *big.Int {
return S256(new(big.Int).SetBytes(word[:]))
}
func RightPadWord256(bz []byte) (word Word256) {
copy(word[:], bz)
return
}
func LeftPadWord256(bz []byte) (word Word256) {
copy(word[32-len(bz):], bz)
return
}
//-------------------------------------
type Words256 []Word256
func (ws Words256) Len() int {
return len(ws)
}
func (ws Words256) Less(i, j int) bool {
return ws[i].Compare(ws[j]) < 0
}
func (ws Words256) Swap(i, j int) {
ws[i], ws[j] = ws[j], ws[i]
}
type Tuple256 struct {
First Word256
Second Word256
}
func (tuple Tuple256) Compare(other Tuple256) int {
firstCompare := tuple.First.Compare(other.First)
if firstCompare == 0 {
return tuple.Second.Compare(other.Second)
} else {
return firstCompare
}
}
func Tuple256Split(t Tuple256) (Word256, Word256) {
return t.First, t.Second
}
type Tuple256Slice []Tuple256
func (p Tuple256Slice) Len() int { return len(p) }
func (p Tuple256Slice) Less(i, j int) bool {
return p[i].Compare(p[j]) < 0
}
func (p Tuple256Slice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
func (p Tuple256Slice) Sort() { sort.Sort(p) }