Skip to content

Commit 34d5afb

Browse files
authored
Merge pull request #81 from pk910/pk910/eip7916-right-shape
change progressive tree shape (EIP7916 update)
2 parents 52d0158 + 4150dbb commit 34d5afb

File tree

6 files changed

+49
-49
lines changed

6 files changed

+49
-49
lines changed

codegen/tests/codegen_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ var testMatrix = []TestPayload{
5151
Name: "ProgressiveTypes",
5252
Payload: ProgressiveTypes_Payload,
5353
Specs: map[string]any{},
54-
Hash: "e4491c6133bb0e40f21224f73ccfcb3a6a7d8fc32816fa5a5f8b5f35265b5854",
54+
Hash: "317f412cd2d042f367c4f2fb6447828ef9524396428eb2ed0837524bcc70433c",
5555
},
5656
}
5757

hasher/hasher.go

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -614,43 +614,43 @@ func (h *Hasher) merkleizeProgressiveImpl(dst []byte, chunks []byte, depth uint8
614614
// return zero_node(0)
615615
// base_size = 1 << depth
616616
// return PairNode(
617-
// subtree_fill_progressive(nodes[base_size:], depth + 2),
618617
// subtree_fill_to_contents(nodes[:base_size], depth),
618+
// subtree_fill_progressive(nodes[base_size:], depth + 2),
619619
// )
620620

621621
// Calculate base_size = 1 << depth (1, 4, 16, 64, 256...)
622622
baseSize := uint64(1) << depth
623623

624-
// Split chunks: first baseSize chunks go to RIGHT (binary), rest go to LEFT (progressive)
624+
// Split chunks: first baseSize chunks go to LEFT (binary), rest go to RIGHT (progressive)
625625
splitPoint := int(baseSize * 32)
626626
if splitPoint > len(chunks) {
627627
splitPoint = len(chunks)
628628
}
629629

630-
// Right child: subtree_fill_to_contents(nodes[:base_size], depth) - binary merkleization
631-
rightChunks := chunks[:splitPoint]
630+
// Left child: subtree_fill_to_contents(nodes[:base_size], depth) - binary merkleization
631+
leftChunks := chunks[:splitPoint]
632632

633-
// Ensure rightChunks are properly padded to 32-byte boundaries
634-
if len(rightChunks) > 0 && len(rightChunks)%32 != 0 {
635-
padNeeded := 32 - (len(rightChunks) % 32)
636-
rightChunks = append(rightChunks, zeroBytes[:padNeeded]...)
633+
// Ensure leftChunks are properly padded to 32-byte boundaries
634+
if len(leftChunks) > 0 && len(leftChunks)%32 != 0 {
635+
padNeeded := 32 - (len(leftChunks) % 32)
636+
leftChunks = append(leftChunks, zeroBytes[:padNeeded]...)
637637
}
638638

639-
rightRoot := h.merkleizeImpl(rightChunks[:0], rightChunks, baseSize)
639+
leftRoot := h.merkleizeImpl(leftChunks[:0], leftChunks, baseSize)
640640

641-
// Left child: subtree_fill_progressive(nodes[base_size:], depth + 2) - recursive progressive
642-
leftChunks := chunks[splitPoint:]
643-
var leftRoot []byte
644-
if len(leftChunks) == 0 {
645-
leftRoot = zeroHashes[0][:]
641+
// Right child: subtree_fill_progressive(nodes[base_size:], depth + 2) - recursive progressive
642+
rightChunks := chunks[splitPoint:]
643+
var rightRoot []byte
644+
if len(rightChunks) == 0 {
645+
rightRoot = zeroHashes[0][:]
646646
} else {
647-
// Ensure leftChunks are properly padded to 32-byte boundaries
648-
if len(leftChunks)%32 != 0 {
649-
padNeeded := 32 - (len(leftChunks) % 32)
650-
leftChunks = append(leftChunks, zeroBytes[:padNeeded]...)
647+
// Ensure rightChunks are properly padded to 32-byte boundaries
648+
if len(rightChunks)%32 != 0 {
649+
padNeeded := 32 - (len(rightChunks) % 32)
650+
rightChunks = append(rightChunks, zeroBytes[:padNeeded]...)
651651
}
652652

653-
leftRoot = h.merkleizeProgressiveImpl(leftChunks[:0], leftChunks, depth+2)
653+
rightRoot = h.merkleizeProgressiveImpl(rightChunks[:0], rightChunks, depth+2)
654654
}
655655

656656
if len(h.tmp) < 64 {

reflection/common_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ var commonTestMatrix = []struct {
259259
}{list}
260260
}(),
261261
fromHex("0x040000007b0000008e0200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000de000000"),
262-
fromHex("0xcafb653b8b774afa1a755897c6afc68bb08af48b30a3c08ca5b72ddf79bdb20f"),
262+
fromHex("0xc48a5e54d811bcdeedb92af969f5d009d68ac2c7d84dc494aad21811552878c4"),
263263
},
264264

265265
// progressive bitlist test - matches Python test_progressive_bitlist.py output
@@ -288,7 +288,7 @@ var commonTestMatrix = []struct {
288288
}{bl}
289289
}(),
290290
fromHex("04000000244992244992244992244992244992244992244992244992244992244992244992244992244992244992244992244992244992244992244992244992244992244992244992244992244992244992244992244992244992244992244992244992244992244992244992244992244992244992244992244992244992244901"),
291-
fromHex("0xba990efa7343179a41d01614759e0ab696a8869fade3f576a8abe6e9880eeaa3"),
291+
fromHex("0xa50e608b50a1e39805e9853213b59bc2795b067a995d1194a27bc46121292ee1"),
292292
},
293293

294294
// progressive list with 100 uint16 elements
@@ -298,7 +298,7 @@ var commonTestMatrix = []struct {
298298
F1 []uint16 `ssz-type:"progressive-list"`
299299
}{[]uint16{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100}},
300300
fromHex("0x040000000100020003000400050006000700080009000a000b000c000d000e000f0010001100120013001400150016001700180019001a001b001c001d001e001f0020002100220023002400250026002700280029002a002b002c002d002e002f0030003100320033003400350036003700380039003a003b003c003d003e003f0040004100420043004400450046004700480049004a004b004c004d004e004f0050005100520053005400550056005700580059005a005b005c005d005e005f0060006100620063006400"),
301-
fromHex("0xafc3646489c444662626be91d6630ba5671cb302733bd50822544f8c6be96005"),
301+
fromHex("0x443e98ea305c91aa4288ed56e29c6eaaceacc8da101c0387d0bd5fc7b423e28d"),
302302
},
303303

304304
// Progressive container tests
@@ -311,7 +311,7 @@ var commonTestMatrix = []struct {
311311
Field3 uint16 `ssz-index:"3"`
312312
}{12345, 67890, true, 999},
313313
fromHex("0x39300000000000003209010001e703"),
314-
fromHex("0x0e4ca0d5f6b209257cdaa08a60240a3043fb0ab891fa32f5d483d569605bb4df"),
314+
fromHex("0x9469a2aa7c9b292a752e7d24e87172424de30ce738116e373fd89a57e6d467d1"),
315315
},
316316
{
317317
"progressive_container_2",
@@ -322,7 +322,7 @@ var commonTestMatrix = []struct {
322322
Field3 uint16 `ssz-index:"3"`
323323
}{0, 0, false, 0},
324324
fromHex("0x000000000000000000000000000000"),
325-
fromHex("0x7e3741b0db51cdff09176571314e17e2e216bf4264841eb3d6aa78c7c435658e"),
325+
fromHex("0xef9da2c4e49e0acc0933a072ab744573e1c1e20806904bf0dd4b7eef0a9da836"),
326326
},
327327
// progressive container with sparse indices
328328
{
@@ -334,7 +334,7 @@ var commonTestMatrix = []struct {
334334
Field3 uint16 `ssz-index:"5"`
335335
}{12345, 67890, true, 999},
336336
fromHex("0x39300000000000003209010001e703"),
337-
fromHex("0xa022dead859d4c67b19c5caa2cd26b1f004479465133ae8f2decd234f41df8f5"),
337+
fromHex("0x69cf970cadf7ef2055070f37237e7d9b1e9807beb9c245fc04fc81532e021b20"),
338338
},
339339

340340
// CompatibleUnion tests
@@ -658,7 +658,7 @@ var commonTestMatrix = []struct {
658658
Data string `ssz-type:"progressive-list"`
659659
}{"abcdefghijklmnopqrstuvwxyz123456"},
660660
fromHex("0x040000006162636465666768696a6b6c6d6e6f707172737475767778797a313233343536"),
661-
fromHex("0x41ba7be636dd08b32cca499285494e18f8849fbba06a7ced2d0d692777228e10"),
661+
fromHex("0x31ca2594351d5fbcc4b8d74e73682987929cf7e2b6f07d5677e38a6bedaf41c7"),
662662
},
663663

664664
// TypeWrapper test cases

reflection/treeroot_test.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -74,17 +74,17 @@ var treerootTestMatrix = append(commonTestMatrix, []struct {
7474
func TestTreeRoot(t *testing.T) {
7575
dynssz := NewDynSsz(nil)
7676

77-
for idx, test := range treerootTestMatrix {
77+
for _, test := range treerootTestMatrix {
7878
t.Run(test.name, func(t *testing.T) {
7979
buf, err := dynssz.HashTreeRoot(test.payload)
8080

8181
switch {
8282
case test.htr == nil && err != nil:
8383
// expected error
8484
case err != nil:
85-
t.Errorf("test %v error: %v", idx, err)
85+
t.Errorf("test %v error: %v", test.name, err)
8686
case !bytes.Equal(buf[:], test.htr):
87-
t.Errorf("test %v failed: got 0x%x, wanted 0x%x", idx, buf, test.htr)
87+
t.Errorf("test %v failed: got 0x%x, wanted 0x%x", test.name, buf, test.htr)
8888
}
8989
})
9090
}
@@ -93,17 +93,17 @@ func TestTreeRoot(t *testing.T) {
9393
func TestTreeRootNoFastSsz(t *testing.T) {
9494
dynssz := NewDynSsz(nil, WithNoFastSsz())
9595

96-
for idx, test := range treerootTestMatrix {
96+
for _, test := range treerootTestMatrix {
9797
t.Run(test.name, func(t *testing.T) {
9898
buf, err := dynssz.HashTreeRoot(test.payload)
9999

100100
switch {
101101
case test.htr == nil && err != nil:
102102
// expected error
103103
case err != nil:
104-
t.Errorf("test %v error: %v", idx, err)
104+
t.Errorf("test %v error: %v", test.name, err)
105105
case !bytes.Equal(buf[:], test.htr):
106-
t.Errorf("test %v failed: got 0x%x, wanted 0x%x", idx, buf, test.htr)
106+
t.Errorf("test %v failed: got 0x%x, wanted 0x%x", test.name, buf, test.htr)
107107
}
108108
})
109109
}
@@ -112,17 +112,17 @@ func TestTreeRootNoFastSsz(t *testing.T) {
112112
func TestTreeRootNoFastHash(t *testing.T) {
113113
dynssz := NewDynSsz(nil, WithNoFastHash())
114114

115-
for idx, test := range treerootTestMatrix {
115+
for _, test := range treerootTestMatrix {
116116
t.Run(test.name, func(t *testing.T) {
117117
buf, err := dynssz.HashTreeRoot(test.payload)
118118

119119
switch {
120120
case test.htr == nil && err != nil:
121121
// expected error
122122
case err != nil:
123-
t.Errorf("test %v error: %v", idx, err)
123+
t.Errorf("test %v error: %v", test.name, err)
124124
case !bytes.Equal(buf[:], test.htr):
125-
t.Errorf("test %v failed: got 0x%x, wanted 0x%x", idx, buf, test.htr)
125+
t.Errorf("test %v failed: got 0x%x, wanted 0x%x", test.name, buf, test.htr)
126126
}
127127
})
128128
}

treeproof/tree.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -369,26 +369,26 @@ func treeFromNodesProgressiveImpl(leaves []*Node, depth int) (*Node, error) {
369369
// Calculate base_size = 1 << depth (1, 4, 16, 64, 256...)
370370
baseSize := 1 << depth
371371

372-
// Split nodes: first baseSize nodes go to RIGHT (binary), rest go to LEFT (progressive)
372+
// Split nodes: first baseSize nodes go to LEFT (binary), rest go to RIGHT (progressive)
373373
splitPoint := baseSize
374374
if splitPoint > len(leaves) {
375375
splitPoint = len(leaves)
376376
}
377377

378-
// Right child: binary merkleization of first baseSize nodes
379-
rightNodes := leaves[:splitPoint]
380-
rightChild, err := TreeFromNodes(rightNodes, baseSize)
378+
// Left child: binary merkleization of first baseSize nodes
379+
leftNodes := leaves[:splitPoint]
380+
leftChild, err := TreeFromNodes(leftNodes, baseSize)
381381
if err != nil {
382382
return nil, err
383383
}
384384

385-
// Left child: recursive progressive merkleization of remaining nodes
386-
leftNodes := leaves[splitPoint:]
387-
var leftChild *Node
388-
if len(leftNodes) == 0 {
389-
leftChild = NewEmptyNode(sszutils.ZeroBytes()[:32])
385+
// Right child: recursive progressive merkleization of remaining nodes
386+
rightNodes := leaves[splitPoint:]
387+
var rightChild *Node
388+
if len(rightNodes) == 0 {
389+
rightChild = NewEmptyNode(sszutils.ZeroBytes()[:32])
390390
} else {
391-
leftChild, err = treeFromNodesProgressiveImpl(leftNodes, depth+2)
391+
rightChild, err = treeFromNodesProgressiveImpl(rightNodes, depth+2)
392392
if err != nil {
393393
return nil, err
394394
}

treeproof/tree_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -328,12 +328,12 @@ func TestTreeFromNodesProgressive(t *testing.T) {
328328
name: "single node",
329329
nodes: []*Node{NewNodeWithValue([]byte{1})},
330330
validateFn: func(t *testing.T, n *Node) {
331-
// Progressive tree with 1 node: base_size=1, so right gets the node, left is empty
331+
// Progressive tree with 1 node: base_size=1, so left gets the node, right is empty
332332
if n.IsLeaf() {
333333
t.Error("expected branch node")
334334
}
335-
if !n.left.IsEmpty() {
336-
t.Error("expected empty left child")
335+
if !n.right.IsEmpty() {
336+
t.Error("expected empty right child")
337337
}
338338
},
339339
},

0 commit comments

Comments
 (0)