Skip to content

Commit

Permalink
refactor(tree): replace key/val size with const
Browse files Browse the repository at this point in the history
  • Loading branch information
weiihann committed Dec 4, 2023
1 parent ce852f0 commit e0e5470
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 16 deletions.
1 change: 1 addition & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
)

const (
KeySize = 32
LeafValueSize = 32
NodeWidth = 256
NodeBitWidth byte = 8
Expand Down
2 changes: 1 addition & 1 deletion encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func parseLeafNode(serialized []byte, depth byte) (VerkleNode, error) {
for i := 0; i < NodeWidth; i++ {
if bit(bitlist, i) {
if offset+LeafValueSize > len(serialized) {
return nil, fmt.Errorf("verkle payload is too short, need at least %d and only have %d, payload = %x (%w)", offset+32, len(serialized), serialized, errSerializedPayloadTooShort)
return nil, fmt.Errorf("verkle payload is too short, need at least %d and only have %d, payload = %x (%w)", offset+LeafValueSize, len(serialized), serialized, errSerializedPayloadTooShort)
}
values[i] = serialized[offset : offset+LeafValueSize]
offset += LeafValueSize
Expand Down
4 changes: 2 additions & 2 deletions ipa.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ type (
)

func FromLEBytes(fr *Fr, data []byte) error {
if len(data) > 32 {
if len(data) > LeafValueSize {
return errors.New("data is too long")
}
var aligned [32]byte
var aligned [LeafValueSize]byte
copy(aligned[:], data)
fr.SetBytesLE(aligned[:])
return nil
Expand Down
4 changes: 2 additions & 2 deletions tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -1392,7 +1392,7 @@ func leafToComms(poly []Fr, val []byte) error {
if len(val) == 0 {
return nil
}
if len(val) > 32 {
if len(val) > LeafValueSize {
return fmt.Errorf("invalid leaf length %d, %v", len(val), val)
}
var (
Expand Down Expand Up @@ -1602,7 +1602,7 @@ func (n *LeafNode) Copy() VerkleNode {
}

func (n *LeafNode) Key(i int) []byte {
var ret [32]byte
var ret [KeySize]byte
copy(ret[:], n.stem)
ret[StemSize] = byte(i)
return ret[:]
Expand Down
21 changes: 10 additions & 11 deletions tree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ func TestOffset2key8BitsWide(t *testing.T) {
t.Parallel()

key, _ := hex.DecodeString("000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f")
for i := byte(0); i < 32; i++ {
for i := byte(0); i < KeySize; i++ {
childId := offset2key(key, i)
if childId != i {
t.Fatalf("error getting child number in key %d != %d", childId, i)
Expand Down Expand Up @@ -579,11 +579,10 @@ func BenchmarkCommitFullNode(b *testing.B) {
nChildren := 256
keys := make([][]byte, nChildren)
for i := 0; i < nChildren; i++ {
key := make([]byte, 32)
key := make([]byte, KeySize)
key[0] = uint8(i)
keys[i] = key
}

b.ResetTimer()
b.ReportAllocs()

Expand All @@ -607,8 +606,8 @@ func benchmarkCommitNLeaves(b *testing.B, n int) {
sortedKVs := make([]kv, n)

for i := 0; i < n; i++ {
key := make([]byte, 32)
val := make([]byte, 32)
key := make([]byte, KeySize)
val := make([]byte, KeySize)
if _, err := rand.Read(key); err != nil {
b.Fatalf("failed to generate random key: %v", err)
}
Expand Down Expand Up @@ -650,7 +649,7 @@ func BenchmarkModifyLeaves(b *testing.B) {
keys := make([][]byte, n)
root := New()
for i := 0; i < n; i++ {
key := make([]byte, 32)
key := make([]byte, KeySize)
if _, err := rand.Read(key); err != nil {
b.Fatalf("failed to generate random key: %v", err)
}
Expand Down Expand Up @@ -681,7 +680,7 @@ func BenchmarkModifyLeaves(b *testing.B) {
func randomKeys(t *testing.T, n int) [][]byte {
keys := make([][]byte, n)
for i := 0; i < n; i++ {
key := make([]byte, 32)
key := make([]byte, KeySize)
if _, err := rand.Read(key); err != nil {
t.Fatalf("failed to generate random key: %v", err)
}
Expand Down Expand Up @@ -1111,7 +1110,7 @@ func TestInsertStem(t *testing.T) {
}
r1c := root1.Commit()

var key5, key192 [32]byte
var key5, key192 [KeySize]byte
copy(key5[:], KeyToStem(fourtyKeyTest))
copy(key192[:], KeyToStem(fourtyKeyTest))
key5[StemSize] = 5
Expand Down Expand Up @@ -1502,8 +1501,8 @@ func genRandomKeyValues(rand *mRand.Rand, count int) []keyValue {
for i := 0; i < count; i++ {
keyval := make([]byte, 64)
rand.Read(keyval)
ret[i].key = keyval[:32]
ret[i].value = keyval[32:]
ret[i].key = keyval[:KeySize]
ret[i].value = keyval[KeySize:]
}
return ret
}
Expand Down Expand Up @@ -1702,7 +1701,7 @@ func generateSteps(finished func() bool, r io.Reader) randTest {
// we create a new key or return an existing key otherwise.
if len(allKeys) < 2 || tmp[0]%100 > 90 {
// new key
key := make([]byte, 32)
key := make([]byte, KeySize)
_, err := r.Read(key)
if err != nil {
panic(err)
Expand Down

0 comments on commit e0e5470

Please sign in to comment.