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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
|
package bitarray
import (
"fmt"
"math/big"
"math/bits"
)
// BitArray holds an array of bits.
type BitArray struct {
raw []byte
avail uint8
size uint64
}
// New creates a BitArray with the given []byte and count bits.
// Count is from position 0 of b.
func New(b []byte, count uint64) *BitArray {
out := &BitArray{}
out.raw = b
out.size = count
out.avail = uint8(uint64(len(b))*8 - count)
return out
}
// Bytes returns the BitArray as bytes.
func (ba BitArray) Bytes() []byte {
return ba.raw
}
// Len returns the BitArray length.
func (ba BitArray) Len() uint64 {
return ba.size
}
// Bytes returns the BitArray as bytes.
// The remaining bits of the last byte are set to zero.
func (ba BitArray) String() string {
return fmt.Sprintf("%08b", ba.raw)
}
// Test returns true if bit at offset i is 1, false otherwise.
func (ba BitArray) Test(i uint64) bool {
if i > ba.size {
return false
}
idx := i / 8
offset := i % 8
if idx >= uint64(len(ba.raw)) {
return false
}
mask := 1 << (7 - offset)
return (ba.raw[idx] & byte(mask)) != 0
}
// Set a single bit to 1 at position n.
func (ba *BitArray) Set(n uint64) {
if n > ba.size {
return
}
idx := n / 8
offset := n % 8
mask := 1 << (7 - offset)
ba.raw[idx] |= byte(mask)
}
// Unset a single bit to 0 at position n.
func (ba *BitArray) Unset(n uint64) {
if n > ba.size {
return
}
idx := n / 8
offset := n % 8
mask := 1 << (7 - offset)
ba.raw[idx] &^= byte(mask)
}
// Pad adds n zeros as padding.
func (ba *BitArray) Pad(n uint64) {
for i := uint64(0); i < n; i++ {
ba.Add8(uint8(0))
}
}
// AddBit adds a single bit to the array.
func (ba *BitArray) AddBit(u uint8) {
ba.grow()
if u == 0 {
ba.Unset(ba.size)
} else {
ba.Set(ba.size)
}
ba.size++
ba.avail--
}
// Add8 adds a uint8 to the BitArray with leading zeros removed.
func (ba *BitArray) Add8(u uint8) {
if u == 0 {
ba.AddBit(0)
return
}
ba.add(u)
}
// Add8N adds a uint8 with a fixed width of n, left padded with zeros.
func (ba *BitArray) Add8N(u, s uint8) {
n := uint8(bits.Len8(u))
for i := uint8(0); i < (s - n); i++ {
ba.AddBit(0)
}
if n != 0 {
ba.Add8(u)
}
}
// Add16 adds a uint8 to the BitArray.
func (ba *BitArray) Add16(u uint16) {
if u == 0 {
ba.AddBit(0)
return
}
ba.add(uint8(u >> 8))
ba.add(uint8(u))
}
// Add16N adds a uint16 with a fixed width of n.
func (ba *BitArray) Add16N(u, s uint16) {
n := uint16(bits.Len16(u))
for i := uint16(0); i < (s - n); i++ {
ba.AddBit(0)
}
if n != 0 {
ba.Add16(u)
}
}
// Add32 adds a uint32 to the BitArray.
func (ba *BitArray) Add32(u uint32) {
if u == 0 {
ba.AddBit(0)
return
}
ba.add(uint8(u >> 24))
ba.add(uint8(u >> 16))
ba.add(uint8(u >> 8))
ba.add(uint8(u))
}
// Add32N adds a uint32 with a fixed width of n.
func (ba *BitArray) Add32N(u, s uint32) {
n := uint32(bits.Len32(u))
for i := uint32(0); i < (s - n); i++ {
ba.AddBit(0)
}
if n != 0 {
ba.Add32(u)
}
}
// Add64 adds a uint64 to the BitArray.
func (ba *BitArray) Add64(u uint64) {
if u == 0 {
ba.AddBit(0)
return
}
ba.add(uint8(u >> 56))
ba.add(uint8(u >> 48))
ba.add(uint8(u >> 40))
ba.add(uint8(u >> 32))
ba.add(uint8(u >> 24))
ba.add(uint8(u >> 16))
ba.add(uint8(u >> 8))
ba.add(uint8(u))
}
// Add64N adds a uint64 with a fixed width of n.
func (ba *BitArray) Add64N(u, s uint64) {
n := uint64(bits.Len64(u))
for i := uint64(0); i < (s - n); i++ {
ba.AddBit(0)
}
if n != 0 {
ba.Add64(u)
}
}
// Pack stuff together into existing BitArray.
func (ba *BitArray) Pack(fields ...interface{}) error {
for _, f := range fields {
switch c := f.(type) {
case uint:
ba.Add32(uint32(c))
case uint8:
ba.Add8(c)
case uint16:
ba.Add16(c)
case uint32:
ba.Add32(c)
case uint64:
ba.Add64(c)
case []uint:
for _, i := range c {
ba.Add32(uint32(i))
}
case []uint8:
for _, i := range c {
ba.Add8(i)
}
case []uint16:
for _, i := range c {
ba.Add16(i)
}
case []uint32:
for _, i := range c {
ba.Add32(i)
}
case []uint64:
for _, i := range c {
ba.Add64(i)
}
case int:
ba.Add32(uint32(c))
case int8:
ba.Add8(uint8(c))
case int16:
ba.Add16(uint16(c))
case int32:
ba.Add32(uint32(c))
case int64:
ba.Add64(uint64(c))
case []int:
for _, i := range c {
ba.Add32(uint32(i))
}
case []int8:
for _, i := range c {
ba.Add8(uint8(i))
}
case []int16:
for _, i := range c {
ba.Add16(uint16(i))
}
case []int32:
for _, i := range c {
ba.Add32(uint32(i))
}
case []int64:
for _, i := range c {
ba.Add64(uint64(i))
}
case []interface{}:
return ba.Pack(c...)
default:
return fmt.Errorf("unable to pack %T", c)
}
}
return nil
}
// Pack stuff together into a BitArray.
func Pack(fields ...interface{}) (*BitArray, error) {
out := new(BitArray)
err := out.Pack(fields...)
return out, err
}
// Slice reads a range from the BitArray.
func (ba *BitArray) Slice(start, length uint64) (*BitArray, error) {
out := new(BitArray)
for i := start; i < (start + length); i++ {
if ba.Test(i) {
out.Add8(1)
} else {
out.Add8(0)
}
}
return out, nil
}
// ReadBig reads a big.Int from the BitArray.
func (ba *BitArray) ReadBig(start, length uint64) (*big.Int, error) {
b, err := ba.Slice(start, length)
if err != nil {
return nil, err
}
out := new(big.Int).SetBytes(b.Bytes())
shifted := out.Rsh(out, uint(b.avail))
//return out.Rsh(out, b.avail), nil
return shifted, nil
}
func (ba *BitArray) add(u uint8) {
n := uint8(bits.Len8(u))
ba.addN(u, n)
}
func (ba *BitArray) addN(u, n uint8) {
ba.grow()
var mask uint8
shift := int8(uint8(ba.avail) - n)
if shift < 0 {
// It doesn't fit
mask = u >> abs(shift)
ba.raw[len(ba.raw)-1] |= byte(mask)
ba.raw = append(ba.raw, byte(0))
shift = 8 + shift
}
mask = u << abs(shift)
ba.raw[len(ba.raw)-1] |= byte(mask)
ba.avail = abs(shift)
ba.size += uint64(n)
ba.norm()
}
func (ba *BitArray) grow() {
if ba.avail <= 0 {
ba.raw = append(ba.raw, byte(0))
ba.avail += 8
}
}
// Remove extraneous bits.
func (ba *BitArray) norm() {
n := len(ba.raw)
if ba.avail > 8 {
ba.raw = ba.raw[:n]
}
ba.avail = uint8(uint64(n*8) - ba.size)
}
// ShiftL shifts all bits to the left and returns those
// shifted off. s cannot be larger than 8.
func (ba *BitArray) ShiftL(s uint8) (r byte) {
if s > 8 {
return
}
if n := len(ba.raw); n > 0 {
_s := 8 - s
b1 := ba.raw[n-1]
r = b1 >> _s
for i := 0; i < n-1; i++ {
b := b1
b1 = ba.raw[i+1]
ba.raw[i] = b<<s | b1>>_s
}
ba.raw[n-1] = b1 << s
}
return
}
// ShiftR shifts all bits to the right and returns those
// shifted off. s cannot be larger than 8.
func (ba *BitArray) ShiftR(s uint8) (r byte) {
if s > 8 {
return
}
if n := len(ba.raw); n > 0 {
_s := 8 - s
b1 := ba.raw[0]
r = b1 << _s
for i := n - 1; i > 0; i-- {
b := b1
b1 = ba.raw[i-1]
ba.raw[i] = b>>s | b1<<_s
}
ba.raw[0] = b1 >> s
}
return
}
func abs(i int8) uint8 {
if i < 0 {
return uint8(-i)
}
return uint8(i)
}
func min(a, b uint64) uint64 {
if a < b {
return a
}
return b
}
|