Skip to content

Commit 615552d

Browse files
committed
makeNibbles
1 parent 6dc8758 commit 615552d

File tree

2 files changed

+36
-36
lines changed

2 files changed

+36
-36
lines changed

crypto/statetrie/nibbles/nibbles.go

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -32,34 +32,6 @@ const (
3232
evenIndicator = 0x03
3333
)
3434

35-
// MakeNibbles returns a nibble array from the byte array. If oddLength is true,
36-
// the last 4 bits of the last byte of the array are ignored.
37-
//
38-
// [0x12, 0x30], true -> [0x1, 0x2, 0x3]
39-
// [0x12, 0x34], false -> [0x1, 0x2, 0x3, 0x4]
40-
// [0x12, 0x34], true -> [0x1, 0x2, 0x3] <-- last byte last 4 bits ignored
41-
// [], false -> []
42-
// never to be called with [], true
43-
// Allocates a new byte slice.
44-
func MakeNibbles(data []byte, oddLength bool) Nibbles {
45-
length := len(data) * 2
46-
if oddLength {
47-
length = length - 1
48-
}
49-
ns := make([]byte, length)
50-
51-
j := 0
52-
for i := 0; i < length; i++ {
53-
if i%2 == 0 {
54-
ns[i] = data[j] >> 4
55-
} else {
56-
ns[i] = data[j] & 0x0f
57-
j++
58-
}
59-
}
60-
return ns
61-
}
62-
6335
// Pack the nibble array into a byte array.
6436
// Return the byte array and a bool indicating if the last byte is a full byte or
6537
// only the high 4 bits are part of the encoding
@@ -151,11 +123,39 @@ func Deserialize(encoding []byte) (Nibbles, error) {
151123
if length == 1 {
152124
return nil, errors.New("invalid encoding")
153125
}
154-
ns = MakeNibbles(encoding[:length-1], true)
126+
ns = makeNibbles(encoding[:length-1], true)
155127
} else if encoding[length-1] == evenIndicator {
156-
ns = MakeNibbles(encoding[:length-1], false)
128+
ns = makeNibbles(encoding[:length-1], false)
157129
} else {
158130
return nil, errors.New("invalid encoding")
159131
}
160132
return ns, nil
161133
}
134+
135+
// makeNibbles returns a nibble array from the byte array. If oddLength is true,
136+
// the last 4 bits of the last byte of the array are ignored.
137+
//
138+
// [0x12, 0x30], true -> [0x1, 0x2, 0x3]
139+
// [0x12, 0x34], false -> [0x1, 0x2, 0x3, 0x4]
140+
// [0x12, 0x34], true -> [0x1, 0x2, 0x3] <-- last byte last 4 bits ignored
141+
// [], false -> []
142+
// never to be called with [], true
143+
// Allocates a new byte slice.
144+
func makeNibbles(data []byte, oddLength bool) Nibbles {
145+
length := len(data) * 2
146+
if oddLength {
147+
length = length - 1
148+
}
149+
ns := make([]byte, length)
150+
151+
j := 0
152+
for i := 0; i < length; i++ {
153+
if i%2 == 0 {
154+
ns[i] = data[j] >> 4
155+
} else {
156+
ns[i] = data[j] & 0x0f
157+
j++
158+
}
159+
}
160+
return ns
161+
}

crypto/statetrie/nibbles/nibbles_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ func TestNibblesRandom(t *testing.T) {
4848
if half && localRand.Intn(2) == 0 {
4949
data[len(data)-1] &= 0xf0 // sometimes clear the last nibble, sometimes do not
5050
}
51-
nibbles := MakeNibbles(data, half)
51+
nibbles := makeNibbles(data, half)
5252

5353
data2 := Serialize(nibbles)
5454
nibbles2, err := Deserialize(data2)
@@ -61,13 +61,13 @@ func TestNibblesRandom(t *testing.T) {
6161
packed, odd := Pack(nibbles)
6262
require.Equal(t, odd, half)
6363
require.Equal(t, packed, data)
64-
unpacked := MakeNibbles(packed, odd)
64+
unpacked := makeNibbles(packed, odd)
6565
require.Equal(t, nibbles, unpacked)
6666

6767
packed, odd = Pack(nibbles2)
6868
require.Equal(t, odd, half)
6969
require.Equal(t, packed, data)
70-
unpacked = MakeNibbles(packed, odd)
70+
unpacked = makeNibbles(packed, odd)
7171
require.Equal(t, nibbles2, unpacked)
7272
}
7373
}
@@ -130,7 +130,7 @@ func TestNibbles(t *testing.T) {
130130
require.Equal(t, oddLength == (len(n)%2 == 1), true)
131131
require.Equal(t, bytes.Equal(b, sampleNibblesPacked[i]), true)
132132

133-
unp := MakeNibbles(b, oddLength)
133+
unp := makeNibbles(b, oddLength)
134134
require.Equal(t, bytes.Equal(unp, n), true)
135135

136136
}
@@ -183,10 +183,10 @@ func TestNibbles(t *testing.T) {
183183

184184
makeNibblesTestExpected := Nibbles{0x0, 0x1, 0x2, 0x9, 0x2}
185185
makeNibblesTestData := []byte{0x01, 0x29, 0x20}
186-
mntr := MakeNibbles(makeNibblesTestData, true)
186+
mntr := makeNibbles(makeNibblesTestData, true)
187187
require.Equal(t, bytes.Equal(mntr, makeNibblesTestExpected), true)
188188
makeNibblesTestExpectedFW := Nibbles{0x0, 0x1, 0x2, 0x9, 0x2, 0x0}
189-
mntr2 := MakeNibbles(makeNibblesTestData, false)
189+
mntr2 := makeNibbles(makeNibblesTestData, false)
190190
require.Equal(t, bytes.Equal(mntr2, makeNibblesTestExpectedFW), true)
191191

192192
sampleEqualFalse := [][]Nibbles{

0 commit comments

Comments
 (0)