Skip to content

Commit

Permalink
makeNibbles
Browse files Browse the repository at this point in the history
  • Loading branch information
bbroder-uji committed Nov 9, 2023
1 parent 6dc8758 commit 615552d
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 36 deletions.
60 changes: 30 additions & 30 deletions crypto/statetrie/nibbles/nibbles.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,34 +32,6 @@ const (
evenIndicator = 0x03
)

// MakeNibbles returns a nibble array from the byte array. If oddLength is true,
// the last 4 bits of the last byte of the array are ignored.
//
// [0x12, 0x30], true -> [0x1, 0x2, 0x3]
// [0x12, 0x34], false -> [0x1, 0x2, 0x3, 0x4]
// [0x12, 0x34], true -> [0x1, 0x2, 0x3] <-- last byte last 4 bits ignored
// [], false -> []
// never to be called with [], true
// Allocates a new byte slice.
func MakeNibbles(data []byte, oddLength bool) Nibbles {
length := len(data) * 2
if oddLength {
length = length - 1
}
ns := make([]byte, length)

j := 0
for i := 0; i < length; i++ {
if i%2 == 0 {
ns[i] = data[j] >> 4
} else {
ns[i] = data[j] & 0x0f
j++
}
}
return ns
}

// Pack the nibble array into a byte array.
// Return the byte array and a bool indicating if the last byte is a full byte or
// only the high 4 bits are part of the encoding
Expand Down Expand Up @@ -151,11 +123,39 @@ func Deserialize(encoding []byte) (Nibbles, error) {
if length == 1 {
return nil, errors.New("invalid encoding")
}
ns = MakeNibbles(encoding[:length-1], true)
ns = makeNibbles(encoding[:length-1], true)
} else if encoding[length-1] == evenIndicator {
ns = MakeNibbles(encoding[:length-1], false)
ns = makeNibbles(encoding[:length-1], false)
} else {
return nil, errors.New("invalid encoding")
}
return ns, nil
}

// makeNibbles returns a nibble array from the byte array. If oddLength is true,
// the last 4 bits of the last byte of the array are ignored.
//
// [0x12, 0x30], true -> [0x1, 0x2, 0x3]
// [0x12, 0x34], false -> [0x1, 0x2, 0x3, 0x4]
// [0x12, 0x34], true -> [0x1, 0x2, 0x3] <-- last byte last 4 bits ignored
// [], false -> []
// never to be called with [], true
// Allocates a new byte slice.
func makeNibbles(data []byte, oddLength bool) Nibbles {
length := len(data) * 2
if oddLength {
length = length - 1
}
ns := make([]byte, length)

j := 0
for i := 0; i < length; i++ {
if i%2 == 0 {
ns[i] = data[j] >> 4
} else {
ns[i] = data[j] & 0x0f
j++
}
}
return ns
}
12 changes: 6 additions & 6 deletions crypto/statetrie/nibbles/nibbles_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func TestNibblesRandom(t *testing.T) {
if half && localRand.Intn(2) == 0 {
data[len(data)-1] &= 0xf0 // sometimes clear the last nibble, sometimes do not
}
nibbles := MakeNibbles(data, half)
nibbles := makeNibbles(data, half)

data2 := Serialize(nibbles)
nibbles2, err := Deserialize(data2)
Expand All @@ -61,13 +61,13 @@ func TestNibblesRandom(t *testing.T) {
packed, odd := Pack(nibbles)
require.Equal(t, odd, half)
require.Equal(t, packed, data)
unpacked := MakeNibbles(packed, odd)
unpacked := makeNibbles(packed, odd)
require.Equal(t, nibbles, unpacked)

packed, odd = Pack(nibbles2)
require.Equal(t, odd, half)
require.Equal(t, packed, data)
unpacked = MakeNibbles(packed, odd)
unpacked = makeNibbles(packed, odd)
require.Equal(t, nibbles2, unpacked)
}
}
Expand Down Expand Up @@ -130,7 +130,7 @@ func TestNibbles(t *testing.T) {
require.Equal(t, oddLength == (len(n)%2 == 1), true)
require.Equal(t, bytes.Equal(b, sampleNibblesPacked[i]), true)

unp := MakeNibbles(b, oddLength)
unp := makeNibbles(b, oddLength)
require.Equal(t, bytes.Equal(unp, n), true)

}
Expand Down Expand Up @@ -183,10 +183,10 @@ func TestNibbles(t *testing.T) {

makeNibblesTestExpected := Nibbles{0x0, 0x1, 0x2, 0x9, 0x2}
makeNibblesTestData := []byte{0x01, 0x29, 0x20}
mntr := MakeNibbles(makeNibblesTestData, true)
mntr := makeNibbles(makeNibblesTestData, true)
require.Equal(t, bytes.Equal(mntr, makeNibblesTestExpected), true)
makeNibblesTestExpectedFW := Nibbles{0x0, 0x1, 0x2, 0x9, 0x2, 0x0}
mntr2 := MakeNibbles(makeNibblesTestData, false)
mntr2 := makeNibbles(makeNibblesTestData, false)
require.Equal(t, bytes.Equal(mntr2, makeNibblesTestExpectedFW), true)

sampleEqualFalse := [][]Nibbles{
Expand Down

0 comments on commit 615552d

Please sign in to comment.