Skip to content

Commit 4cee9df

Browse files
committed
add lastPeriod to ExpiredLeafNode
1 parent 971c099 commit 4cee9df

File tree

9 files changed

+34
-13
lines changed

9 files changed

+34
-13
lines changed

debug.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ type (
4747

4848
ExportableExpiredLeafNode struct {
4949
Stem Stem `json:"stem"`
50+
LastPeriod StatePeriod `json:"last_period"`
5051
Commitment [32]byte `json:"commitment"`
5152
}
5253
)

encoding.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ const (
6262
leafValueIndexSize = 1
6363
singleSlotLeafSize = nodeTypeSize + StemSize + 2*banderwagon.UncompressedSize + leafValueIndexSize + leafSlotSize + periodSize
6464
eoaLeafSize = nodeTypeSize + StemSize + 2*banderwagon.UncompressedSize + leafBasicDataSize + periodSize
65-
expiredLeafSize = nodeTypeSize + StemSize + banderwagon.UncompressedSize
65+
expiredLeafSize = nodeTypeSize + StemSize + periodSize + banderwagon.UncompressedSize
6666
)
6767

6868
func bit(bitlist []byte, nr int) bool {
@@ -198,10 +198,11 @@ func parseSingleSlotNode(serialized []byte, depth byte) (VerkleNode, error) {
198198

199199
func parseExpiredLeafNode(serialized []byte, depth byte) (VerkleNode, error) {
200200
l := &ExpiredLeafNode{}
201-
l.stem = serialized[leafStemOffset : leafStemOffset+StemSize]
202201
l.setDepth(depth)
202+
l.stem = serialized[leafStemOffset : leafStemOffset+StemSize]
203+
l.lastPeriod = StatePeriodFromBytes(serialized[leafStemOffset+StemSize:leafStemOffset+StemSize+periodSize])
203204
l.commitment = new(Point)
204-
if err := l.commitment.SetBytesUncompressed(serialized[leafStemOffset+StemSize:], true); err != nil {
205+
if err := l.commitment.SetBytesUncompressed(serialized[leafStemOffset+StemSize+periodSize:], true); err != nil {
205206
return nil, fmt.Errorf("setting commitment: %w", err)
206207
}
207208
return l, nil

encoding_test.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ func TestParseExpiredLeaf(t *testing.T) {
198198

199199
comm := srs[0]
200200
stem := ffx32KeyTest[:StemSize]
201-
el := NewExpiredLeafNode(stem, &comm)
201+
el := NewExpiredLeafNode(stem, period2, &comm)
202202

203203
serialized, err := el.Serialize()
204204
if err != nil {
@@ -222,4 +222,8 @@ func TestParseExpiredLeaf(t *testing.T) {
222222
if !el2.commitment.Equal(&comm) {
223223
t.Fatalf("invalid commitment, got %x, expected %x", el2.commitment, comm)
224224
}
225+
226+
if el2.lastPeriod != period2 {
227+
t.Fatalf("invalid last period, got %d, expected %d", el2.lastPeriod, period2)
228+
}
225229
}

expired_leaf.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,18 @@ package verkle
2828
import (
2929
"fmt"
3030
"errors"
31+
"encoding/binary"
3132
)
3233

3334
type ExpiredLeafNode struct {
3435
stem Stem
36+
lastPeriod StatePeriod
3537
commitment *Point
3638
depth byte // used for proof only, not commitment calculation
3739
}
3840

39-
func NewExpiredLeafNode(stem Stem, commitment *Point) *ExpiredLeafNode {
40-
return &ExpiredLeafNode{stem: stem, commitment: commitment}
41+
func NewExpiredLeafNode(stem Stem, lastPeriod StatePeriod, commitment *Point) *ExpiredLeafNode {
42+
return &ExpiredLeafNode{stem: stem, lastPeriod: lastPeriod, commitment: commitment}
4143
}
4244

4345
func (n *ExpiredLeafNode) Insert([]byte, []byte, StatePeriod, NodeResolverFn) error {
@@ -95,7 +97,11 @@ func (n *ExpiredLeafNode) Serialize() ([]byte, error) {
9597
result := buf[:]
9698
result[0] = expiredLeafType
9799
copy(result[leafStemOffset:], n.stem[:StemSize])
98-
copy(result[leafStemOffset+StemSize:], cBytes[:])
100+
101+
lastPeriod := make([]byte, periodSize)
102+
binary.BigEndian.PutUint16(lastPeriod, uint16(n.lastPeriod))
103+
copy(result[leafStemOffset+StemSize:], lastPeriod)
104+
copy(result[leafStemOffset+StemSize+periodSize:], cBytes[:])
99105

100106
return result, nil
101107
}

expired_leaf_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ func TestExpiredLeafBasic(t *testing.T) {
1111
cfg := GetConfig()
1212
srs := cfg.conf.SRS
1313
comm := srs[0]
14-
leaf := NewExpiredLeafNode(zeroKeyTest[:StemSize], &comm)
14+
leaf := NewExpiredLeafNode(zeroKeyTest[:StemSize], period0, &comm)
1515

1616
err := leaf.Insert(zeroKeyTest, zeroKeyTest, 0, nil)
1717
if !errors.Is(err, errExpired) {

expired_tree_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ func TestRootCommitExpired(t *testing.T) {
213213
var init Point
214214
init.Set(root.Commit())
215215

216-
expiredLeaf := NewExpiredLeafNode(leaf.stem, leaf.commitment)
216+
expiredLeaf := NewExpiredLeafNode(leaf.stem, leaf.lastPeriod, leaf.commitment)
217217
root.(*InternalNode).children[0] = expiredLeaf
218218

219219
comm := root.Commit()

proof_ipa.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,16 +105,23 @@ func (vp *VerkleProof) Equal(other *VerkleProof) error {
105105
return nil
106106
}
107107

108+
// TODO(weiihann): add PoeStems for proof of expiry? It should be a list of (stem, lastPeriod)
108109
type Proof struct {
109110
Multipoint *ipa.MultiProof // multipoint argument
110111
ExtStatus []byte // the extension status of each stem
111112
Cs []*Point // commitments, sorted by their path in the tree
112113
PoaStems []Stem // stems proving another stem is absent
114+
// PoeInfos []PoeInfo // stems proving another stem is expired
113115
Keys [][]byte
114116
PreValues [][]byte
115117
PostValues [][]byte
116118
}
117119

120+
type PoeInfo struct {
121+
Stem Stem
122+
Period StatePeriod
123+
}
124+
118125
type SuffixStateDiff struct {
119126
Suffix byte `json:"suffix"`
120127
CurrentValue *[32]byte `json:"currentValue"`

proof_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -857,7 +857,7 @@ func TestProofOfExpiryOneLeaf(t *testing.T) {
857857

858858
leaf := root.(*InternalNode).children[0].(*LeafNode)
859859

860-
expiredLeaf := NewExpiredLeafNode(leaf.stem, leaf.commitment)
860+
expiredLeaf := NewExpiredLeafNode(leaf.stem, leaf.lastPeriod, leaf.commitment)
861861
expiredLeaf.setDepth(1)
862862
root.(*InternalNode).children[0] = expiredLeaf
863863

@@ -904,12 +904,12 @@ func TestProofOfExpiryMultipleLeaves(t *testing.T) {
904904
init := root.Commit()
905905

906906
leaf0 := root.(*InternalNode).children[0].(*LeafNode)
907-
expiredLeaf0 := NewExpiredLeafNode(leaf0.stem, leaf0.commitment)
907+
expiredLeaf0 := NewExpiredLeafNode(leaf0.stem, leaf0.lastPeriod, leaf0.commitment)
908908
expiredLeaf0.setDepth(1)
909909
root.(*InternalNode).children[0] = expiredLeaf0
910910

911911
leaff := root.(*InternalNode).children[255].(*LeafNode)
912-
expiredLeaff := NewExpiredLeafNode(leaff.stem, leaff.commitment)
912+
expiredLeaff := NewExpiredLeafNode(leaff.stem, leaff.lastPeriod, leaff.commitment)
913913
expiredLeaff.setDepth(1)
914914
root.(*InternalNode).children[255] = expiredLeaff
915915

@@ -1201,7 +1201,7 @@ func TestProofVerificationPreStateExpiredPostStateResurrected(t *testing.T) {
12011201
rootC := preRoot.Commit()
12021202

12031203
leaf := preRoot.(*InternalNode).children[0].(*LeafNode)
1204-
expiredLeaf := NewExpiredLeafNode(leaf.stem, leaf.commitment)
1204+
expiredLeaf := NewExpiredLeafNode(leaf.stem, leaf.lastPeriod, leaf.commitment)
12051205
expiredLeaf.setDepth(1)
12061206
preRoot.(*InternalNode).children[0] = expiredLeaf
12071207

tree.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,7 @@ func (n *InternalNode) toExportable() *ExportableInternalNode {
235235
case *ExpiredLeafNode:
236236
exportable.Children[i] = &ExportableExpiredLeafNode{
237237
Stem: child.stem,
238+
LastPeriod: child.lastPeriod,
238239
Commitment: child.commitment.Bytes(),
239240
}
240241
default:
@@ -585,6 +586,7 @@ func (n *InternalNode) CreatePath(path []byte, stemInfo stemInfo, comms []*Point
585586
if len(stemInfo.stem) != StemSize {
586587
return comms, fmt.Errorf("invalid stem size %d", len(stemInfo.stem))
587588
}
589+
// TODO(weiihann): add last period
588590
newchild := &ExpiredLeafNode{
589591
commitment: comms[0],
590592
stem: stemInfo.stem,

0 commit comments

Comments
 (0)