Skip to content

Commit

Permalink
minor chore
Browse files Browse the repository at this point in the history
  • Loading branch information
weiihann committed Jan 17, 2025
1 parent 3929edc commit 70bef42
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 13 deletions.
17 changes: 15 additions & 2 deletions core/trie/bitarray.go
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,7 @@ func (b *BitArray) setFelt(f *felt.Felt) {
}

func (b *BitArray) setBytes32(data []byte) {
_ = data[31]
_ = data[31] // bound check hint, see https://golang.org/issue/14808
b.words[3] = binary.BigEndian.Uint64(data[0:8])
b.words[2] = binary.BigEndian.Uint64(data[8:16])
b.words[1] = binary.BigEndian.Uint64(data[16:24])
Expand All @@ -543,7 +543,6 @@ func (b *BitArray) setBytes32(data []byte) {
// It rounds up to the nearest byte.
func (b *BitArray) byteCount() uint {
const bits8 = 8
// Cast to uint16 to avoid overflow
return (uint(b.len) + (bits8 - 1)) / uint(bits8)
}

Expand Down Expand Up @@ -590,6 +589,20 @@ func (b *BitArray) clear() *BitArray {

// Truncates the bit array to the specified length, ensuring that any unused bits are all zeros.
//
// Example:
//
// b := &BitArray{
// len: 5,
// words: [4]uint64{
// 0xFFFFFFFFFFFFFFFF, // Before: all bits are 1
// 0x0, 0x0, 0x0,
// },
// }
// b.truncateToLength()
// // After: only first 5 bits remain
// // words[0] = 0x000000000000001F
// // words[1..3] = 0x0
//
//nolint:mnd
func (b *BitArray) truncateToLength() {
switch {
Expand Down
20 changes: 9 additions & 11 deletions core/trie/bitarray_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package trie
import (
"bytes"
"encoding/binary"
"math"
"math/bits"
"testing"

Expand All @@ -12,8 +11,6 @@ import (
"github.com/stretchr/testify/require"
)

var maxBits = [4]uint64{math.MaxUint64, math.MaxUint64, math.MaxUint64, math.MaxUint64}

const (
ones63 = 0x7FFFFFFFFFFFFFFF // 63 bits of 1
)
Expand All @@ -26,12 +23,12 @@ func TestBytes(t *testing.T) {
}{
{
name: "length == 0",
ba: BitArray{len: 0, words: maxBits},
ba: BitArray{len: 0, words: [4]uint64{0, 0, 0, 0}},
want: [32]byte{},
},
{
name: "length < 64",
ba: BitArray{len: 38, words: maxBits},
ba: BitArray{len: 38, words: [4]uint64{0x3FFFFFFFFF, 0, 0, 0}},
want: func() [32]byte {
var b [32]byte
binary.BigEndian.PutUint64(b[24:32], 0x3FFFFFFFFF)
Expand All @@ -40,7 +37,7 @@ func TestBytes(t *testing.T) {
},
{
name: "64 <= length < 128",
ba: BitArray{len: 100, words: maxBits},
ba: BitArray{len: 100, words: [4]uint64{maxUint64, 0xFFFFFFFFF, 0, 0}},
want: func() [32]byte {
var b [32]byte
binary.BigEndian.PutUint64(b[16:24], 0xFFFFFFFFF)
Expand All @@ -50,7 +47,7 @@ func TestBytes(t *testing.T) {
},
{
name: "128 <= length < 192",
ba: BitArray{len: 130, words: maxBits},
ba: BitArray{len: 130, words: [4]uint64{maxUint64, maxUint64, 0x3, 0}},
want: func() [32]byte {
var b [32]byte
binary.BigEndian.PutUint64(b[8:16], 0x3)
Expand All @@ -61,7 +58,7 @@ func TestBytes(t *testing.T) {
},
{
name: "192 <= length < 255",
ba: BitArray{len: 201, words: maxBits},
ba: BitArray{len: 201, words: [4]uint64{maxUint64, maxUint64, maxUint64, 0x1FF}},
want: func() [32]byte {
var b [32]byte
binary.BigEndian.PutUint64(b[0:8], 0x1FF)
Expand All @@ -73,7 +70,7 @@ func TestBytes(t *testing.T) {
},
{
name: "length == 254",
ba: BitArray{len: 254, words: maxBits},
ba: BitArray{len: 254, words: [4]uint64{maxUint64, maxUint64, maxUint64, 0x3FFFFFFFFFFFFFFF}},
want: func() [32]byte {
var b [32]byte
binary.BigEndian.PutUint64(b[0:8], 0x3FFFFFFFFFFFFFFF)
Expand All @@ -85,7 +82,7 @@ func TestBytes(t *testing.T) {
},
{
name: "length == 255",
ba: BitArray{len: 255, words: maxBits},
ba: BitArray{len: 255, words: [4]uint64{maxUint64, maxUint64, maxUint64, ones63}},
want: func() [32]byte {
var b [32]byte
binary.BigEndian.PutUint64(b[0:8], ones63)
Expand Down Expand Up @@ -1118,7 +1115,8 @@ func TestWriteAndUnmarshalBinary(t *testing.T) {
}

var gotBitArray BitArray
gotBitArray.UnmarshalBinary(got)
err = gotBitArray.UnmarshalBinary(got)
require.NoError(t, err)
if !gotBitArray.Equal(&tt.ba) {
t.Errorf("UnmarshalBinary() = %v, want %v", gotBitArray, tt.ba)
}
Expand Down

0 comments on commit 70bef42

Please sign in to comment.