forked from erigontech/erigon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbytes.go
218 lines (183 loc) · 4.74 KB
/
bytes.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
// Copyright 2022 The Erigon Authors
// This file is part of Erigon.
//
// Erigon 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.
//
// Erigon 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 Erigon. If not, see <http://www.gnu.org/licenses/>.
package utils
import (
"encoding/binary"
"errors"
"math/bits"
"unsafe"
"github.com/erigontech/erigon-lib/types/ssz"
"github.com/golang/snappy"
)
var IsSysLittleEndian bool
func init() {
buf := [2]byte{}
*(*uint16)(unsafe.Pointer(&buf[0])) = uint16(0xABCD)
switch buf {
case [2]byte{0xCD, 0xAB}:
IsSysLittleEndian = true
case [2]byte{0xAB, 0xCD}:
IsSysLittleEndian = false
default:
panic("Could not determine native endianness.")
}
}
func Uint32ToBytes4(n uint32) (ret [4]byte) {
binary.BigEndian.PutUint32(ret[:], n)
return
}
func Bytes4ToUint32(bytes4 [4]byte) uint32 {
return binary.BigEndian.Uint32(bytes4[:])
}
func BytesToBytes4(b []byte) (ret [4]byte) {
copy(ret[:], b)
return
}
func Uint64ToLE(i uint64) []byte {
buf := make([]byte, 8)
binary.LittleEndian.PutUint64(buf, i)
return buf
}
func DecompressSnappy(data []byte) ([]byte, error) {
// Decode the snappy
lenDecoded, err := snappy.DecodedLen(data)
if err != nil {
return nil, err
}
decodedData := make([]byte, lenDecoded)
return snappy.Decode(decodedData, data)
}
func CompressSnappy(data []byte) []byte {
return snappy.Encode(nil, data)
}
func EncodeSSZSnappy(data ssz.Marshaler) ([]byte, error) {
var (
enc = make([]byte, 0, data.EncodingSizeSSZ())
err error
)
enc, err = data.EncodeSSZ(enc)
if err != nil {
return nil, err
}
return snappy.Encode(nil, enc), nil
}
func DecodeSSZSnappy(dst ssz.Unmarshaler, src []byte, version int) error {
dec, err := snappy.Decode(nil, src)
if err != nil {
return err
}
err = dst.DecodeSSZ(dec, version)
if err != nil {
return err
}
return nil
}
// getBitlistLength return the amount of bits in given bitlist.
func GetBitlistLength(b []byte) int {
if len(b) == 0 {
return 0
}
// The most significant bit is present in the last byte in the array.
last := b[len(b)-1]
// Determine the position of the most significant bit.
msb := bits.Len8(last)
if msb == 0 {
return 0
}
// The absolute position of the most significant bit will be the number of
// bits in the preceding bytes plus the position of the most significant
// bit. Subtract this value by 1 to determine the length of the bitlist.
return 8*(len(b)-1) + msb - 1
}
func ReverseOfByteSlice(b []byte) (out []byte) {
out = make([]byte, len(b))
for i := range b {
out[i] = b[len(b)-1-i]
}
return
}
func FlipBitOn(b []byte, i int) {
b[i/8] |= 1 << (i % 8)
}
func IsBitOn(b []byte, idx int) bool {
i := uint8(1 << (idx % 8))
return b[idx/8]&i == i
}
// IsNonStrictSupersetBitlist checks if bitlist 'a' is a non-strict superset of bitlist 'b'
func IsNonStrictSupersetBitlist(a, b []byte) bool {
// Ensure 'a' is at least as long as 'b'
if len(a) < len(b) {
return false
}
// Check each bit in 'b' to ensure it is also set in 'a'
for i := 0; i < len(b); i++ {
if (a[i] & b[i]) != b[i] {
return false
}
}
// If all bits required by 'b' are present in 'a', return true
return true
}
// IsOverlappingSSZBitlist checks if bitlist 'a' and bitlist 'b' have any overlapping bits
// However, it ignores the last bits in the last byte.
func IsOverlappingSSZBitlist(a, b []byte) bool {
length := min(len(a), len(b))
for i := range length {
if a[i]&b[i] != 0 {
if i != length-1 {
return true
}
var foundOverlap bool
// check the overlap bit by bit
for j := 0; j < 8; j++ {
if (a[i]>>j)&(b[i]>>j)&1 == 1 {
if foundOverlap {
return true
}
foundOverlap = true
}
}
}
}
return false
}
// func IsOverlappingBitlist(a, b []byte) bool {
// length := min(len(a), len(b))
// for i := range length {
// if a[i]&b[i] != 0 {
// return true
// }
// }
// return false
// }
func BitsOnCount(b []byte) int {
count := 0
for _, v := range b {
count += bits.OnesCount8(v)
}
return count
}
func MergeBitlists(a, b []byte) {
for i := range b {
a[i] |= b[i]
}
}
func ExtractSlotFromSerializedBeaconState(beaconState []byte) (uint64, error) {
if len(beaconState) < 48 {
return 0, errors.New("checkpoint sync read failed, too short")
}
return binary.LittleEndian.Uint64(beaconState[40:48]), nil
}