From 615552d68d33dbbbfbc14e9b3ccea367cf3cec94 Mon Sep 17 00:00:00 2001 From: Bob Broderick Date: Thu, 9 Nov 2023 16:20:17 -0500 Subject: [PATCH] makeNibbles --- crypto/statetrie/nibbles/nibbles.go | 60 ++++++++++++------------ crypto/statetrie/nibbles/nibbles_test.go | 12 ++--- 2 files changed, 36 insertions(+), 36 deletions(-) diff --git a/crypto/statetrie/nibbles/nibbles.go b/crypto/statetrie/nibbles/nibbles.go index 42c64b9269..8a8409b6b0 100644 --- a/crypto/statetrie/nibbles/nibbles.go +++ b/crypto/statetrie/nibbles/nibbles.go @@ -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 @@ -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 +} diff --git a/crypto/statetrie/nibbles/nibbles_test.go b/crypto/statetrie/nibbles/nibbles_test.go index 527862fbeb..c088f1dd8a 100644 --- a/crypto/statetrie/nibbles/nibbles_test.go +++ b/crypto/statetrie/nibbles/nibbles_test.go @@ -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) @@ -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) } } @@ -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) } @@ -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{