Skip to content

Commit 2950a74

Browse files
committed
Remove expiry and add period in stateless proof
edits
1 parent dd87e7f commit 2950a74

18 files changed

+245
-395
lines changed

debug.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,11 @@ type (
4242
C [32]byte `json:"commitment"`
4343
C1 [32]byte `json:"c1"`
4444
C2 [32]byte `json:"c2"`
45-
LastPeriod StatePeriod `json:"last_period"`
45+
Period StatePeriod `json:"period"`
4646
}
4747

4848
ExportableExpiredLeafNode struct {
4949
Stem Stem `json:"stem"`
50-
LastPeriod StatePeriod `json:"last_period"`
5150
Commitment [32]byte `json:"commitment"`
5251
}
5352
)

doc.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,12 @@ var (
4343
errReviveValuesMismatch = errors.New("values mismatch in revive")
4444
errReviveStemMismatch = errors.New("stem mismatch in revive")
4545
errRevivePeriodMismatch = errors.New("period mismatch in revive")
46+
errRevivePeriodOlderThanNew = errors.New("revive old period is older than the new period")
4647
)
4748

4849
const (
4950
// Extension status
5051
extStatusAbsentEmpty = byte(iota) // missing child node along the path
5152
extStatusAbsentOther // path led to a node with a different stem
5253
extStatusPresent // stem was present
53-
extStatusExpired // stem was present but expired
5454
)

empty.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ func (Empty) Commitment() *Point {
5757
return &id
5858
}
5959

60-
func (Empty) GetProofItems(keylist, NodeResolverFn, StatePeriod) (*ProofElements, []byte, []Stem, []StatePeriod, error) {
60+
func (Empty) GetProofItems(keylist, NodeResolverFn) (*ProofElements, []byte, []Stem, []StatePeriod, error) {
6161
return nil, nil, nil, nil, errors.New("trying to produce a commitment for an empty subtree")
6262
}
6363

empty_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func TestEmptyFuncs(t *testing.T) {
5151
t.Fatal("commitment and commit mismatch")
5252
}
5353

54-
if _, _, _, _, err := e.GetProofItems(nil, nil, period0); err == nil {
54+
if _, _, _, _, err := e.GetProofItems(nil, nil); err == nil {
5555
t.Fatal("get proof items should error")
5656
}
5757

encoding.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,13 @@ const (
5656
leafC1CommitmentOffset = leafCommitmentOffset + banderwagon.UncompressedSize
5757
leafC2CommitmentOffset = leafC1CommitmentOffset + banderwagon.UncompressedSize
5858
leafChildrenOffset = leafC2CommitmentOffset + banderwagon.UncompressedSize
59-
leafLastEpochOffset = leafChildrenOffset + periodSize
59+
leafLastPeriodOffset = leafChildrenOffset + periodSize
6060
leafBasicDataSize = 32
6161
leafSlotSize = 32
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 + periodSize + banderwagon.UncompressedSize
65+
expiredLeafSize = nodeTypeSize + StemSize + banderwagon.UncompressedSize
6666
)
6767

6868
func bit(bitlist []byte, nr int) bool {
@@ -77,9 +77,9 @@ var errSerializedPayloadTooShort = errors.New("verkle payload is too short")
7777
// ParseNode deserializes a node into its proper VerkleNode instance.
7878
// The serialized bytes have the format:
7979
// - Internal nodes: <nodeType><bitlist><commitment>
80-
// - Leaf nodes: <nodeType><stem><bitlist><comm><c1comm><c2comm><lastPeriod><children...>
81-
// - EoA nodes: <nodeType><stem><comm><c1comm><lastPeriod><balance><nonce>
82-
// - single slot node: <nodeType><stem><comm><cncomm><lastPeriod><leaf index><slot>
80+
// - Leaf nodes: <nodeType><stem><bitlist><comm><c1comm><c2comm><period><children...>
81+
// - EoA nodes: <nodeType><stem><comm><c1comm><period><balance><nonce>
82+
// - single slot node: <nodeType><stem><comm><cncomm><period><leaf index><slot>
8383
// - Expired leaf nodes: <nodeType><stem><commitment>
8484
func ParseNode(serializedNode []byte, depth byte) (VerkleNode, error) {
8585
// Check that the length of the serialized node is at least the smallest possible serialized node.
@@ -119,7 +119,7 @@ func parseLeafNode(serialized []byte, depth byte) (VerkleNode, error) {
119119
ln := NewLeafNodeWithNoComms(
120120
serialized[leafStemOffset:leafStemOffset+StemSize],
121121
values[:],
122-
StatePeriodFromBytes(serialized[leafLastEpochOffset:leafLastEpochOffset+periodSize]))
122+
StatePeriodFromBytes(serialized[leafLastPeriodOffset:leafLastPeriodOffset+periodSize]))
123123
ln.setDepth(depth)
124124
ln.c1 = new(Point)
125125

@@ -200,9 +200,8 @@ func parseExpiredLeafNode(serialized []byte, depth byte) (VerkleNode, error) {
200200
l := &ExpiredLeafNode{}
201201
l.setDepth(depth)
202202
l.stem = serialized[leafStemOffset : leafStemOffset+StemSize]
203-
l.lastPeriod = StatePeriodFromBytes(serialized[leafStemOffset+StemSize:leafStemOffset+StemSize+periodSize])
204203
l.commitment = new(Point)
205-
if err := l.commitment.SetBytesUncompressed(serialized[leafStemOffset+StemSize+periodSize:], true); err != nil {
204+
if err := l.commitment.SetBytesUncompressed(serialized[leafStemOffset+StemSize:], true); err != nil {
206205
return nil, fmt.Errorf("setting commitment: %w", err)
207206
}
208207
return l, nil
@@ -225,6 +224,7 @@ func CreateInternalNode(bitlist []byte, raw []byte, depth byte) (*InternalNode,
225224
if b&mask[j] != 0 {
226225
node.children[8*i+j] = HashedNode{}
227226
} else {
227+
228228
node.children[8*i+j] = Empty(struct{}{})
229229
}
230230
}

encoding_test.go

Lines changed: 1 addition & 5 deletions
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, period2, &comm)
201+
el := NewExpiredLeafNode(stem, &comm)
202202

203203
serialized, err := el.Serialize()
204204
if err != nil {
@@ -222,8 +222,4 @@ 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-
}
229225
}

expired_leaf.go

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

3433
type ExpiredLeafNode struct {
3534
stem Stem
36-
lastPeriod StatePeriod
3735
commitment *Point
3836
depth byte // used for proof only, not commitment calculation
3937
}
4038

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

4543
func (n *ExpiredLeafNode) Insert([]byte, []byte, StatePeriod, NodeResolverFn) error {
@@ -69,25 +67,8 @@ func (n *ExpiredLeafNode) Commitment() *Point {
6967
return n.commitment
7068
}
7169

72-
func (n *ExpiredLeafNode) GetProofItems(keys keylist, resolver NodeResolverFn, _ StatePeriod) (*ProofElements, []byte, []Stem, []StatePeriod, error) {
73-
var (
74-
pe = &ProofElements{
75-
Vals: make([][]byte, len(keys)),
76-
ByPath: map[string]*Point{},
77-
}
78-
esses []byte = nil
79-
poass []Stem
80-
)
81-
82-
for i := range keys {
83-
pe.ByPath[string(keys[i][:n.depth])] = n.commitment
84-
pe.Vals[i] = nil
85-
86-
esses = append(esses, extStatusExpired|(n.depth<<3))
87-
poass = append(poass, n.stem)
88-
}
89-
90-
return pe, esses, poass, []StatePeriod{n.lastPeriod}, nil
70+
func (n *ExpiredLeafNode) GetProofItems(keylist, NodeResolverFn) (*ProofElements, []byte, []Stem, []StatePeriod, error) {
71+
return nil, nil, nil, nil, errors.New("cannot get proof items from expired leaf node")
9172
}
9273

9374
func (n *ExpiredLeafNode) Serialize() ([]byte, error) {
@@ -97,11 +78,7 @@ func (n *ExpiredLeafNode) Serialize() ([]byte, error) {
9778
result := buf[:]
9879
result[0] = expiredLeafType
9980
copy(result[leafStemOffset:], n.stem[:StemSize])
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[:])
81+
copy(result[leafStemOffset+StemSize:], cBytes[:])
10582

10683
return result, nil
10784
}
@@ -112,8 +89,7 @@ func (n *ExpiredLeafNode) Copy() VerkleNode {
11289
l.depth = n.depth
11390
copy(l.stem, n.stem)
11491
if n.commitment != nil {
115-
l.commitment = new(Point)
116-
l.commitment.Set(n.commitment)
92+
l.commitment = new(Point).Set(n.commitment)
11793
}
11894

11995
return l

expired_leaf_test.go

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

16-
err := leaf.Insert(zeroKeyTest, zeroKeyTest, 0, nil)
16+
err := leaf.Insert(zeroKeyTest, zeroKeyTest, period2, nil)
1717
if !errors.Is(err, errExpired) {
1818
t.Fatalf("expected period expired error when inserting, got %v", err)
1919
}
2020

21-
_, err = leaf.Delete(zeroKeyTest, 0, nil)
21+
_, err = leaf.Delete(zeroKeyTest, period2, nil)
2222
if !errors.Is(err, errExpired) {
2323
t.Fatalf("expected period expired error when deleting, got %v", err)
2424
}
2525

26-
v, err := leaf.Get(zeroKeyTest, 0, nil)
26+
v, err := leaf.Get(zeroKeyTest, period2, nil)
2727
if !errors.Is(err, errExpired) {
2828
t.Fatalf("expected period expired error when getting, got %v", err)
2929
}

expired_tree_test.go

Lines changed: 57 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ func TestInsertSameLeafNoExpired(t *testing.T) {
3131
t.Fatalf("expected value %x, got %x", testValue, leaf.values[oneKeyTest[StemSize]])
3232
}
3333

34-
if leaf.lastPeriod != period1 {
35-
t.Fatalf("expected last accessed to be 1, got %d", leaf.lastPeriod)
34+
if leaf.period != period1 {
35+
t.Fatalf("expected last accessed to be 1, got %d", leaf.period)
3636
}
3737
}
3838

@@ -58,8 +58,8 @@ func TestInsertSameLeafExpired(t *testing.T) {
5858
t.Fatalf("expected value %x, got %x", testValue, leaf.values[zeroKeyTest[StemSize]])
5959
}
6060

61-
if leaf.lastPeriod != period0 {
62-
t.Fatalf("expected last accessed to be 0, got %d", leaf.lastPeriod)
61+
if leaf.period != period0 {
62+
t.Fatalf("expected last accessed to be 0, got %d", leaf.period)
6363
}
6464
}
6565

@@ -93,12 +93,12 @@ func TestInsertDiffLeaf(t *testing.T) {
9393
t.Fatalf("expected value %x, got %x", testValue, leaff.values[ffx32KeyTest[StemSize]])
9494
}
9595

96-
if leaf0.lastPeriod != period0 {
97-
t.Fatalf("expected last accessed to be 0, got %d", leaf0.lastPeriod)
96+
if leaf0.period != period0 {
97+
t.Fatalf("expected last accessed to be 0, got %d", leaf0.period)
9898
}
9999

100-
if leaff.lastPeriod != period2 {
101-
t.Fatalf("expected last accessed to be 2, got %d", leaff.lastPeriod)
100+
if leaff.period != period2 {
101+
t.Fatalf("expected last accessed to be 2, got %d", leaff.period)
102102
}
103103
}
104104

@@ -115,7 +115,7 @@ func TestInsertExpiredLeafSibling(t *testing.T) {
115115
t.Fatalf("expected leaf node, got %T", root.(*InternalNode).children[0])
116116
}
117117

118-
root.(*InternalNode).children[0] = NewExpiredLeafNode(leaf.stem, leaf.lastPeriod, leaf.commitment)
118+
root.(*InternalNode).children[0] = NewExpiredLeafNode(leaf.stem, leaf.commitment)
119119

120120
if err := root.Insert(forkOneKeyTest, testValue, period2, nil); err != nil {
121121
t.Fatalf("error inserting: %v", err)
@@ -163,8 +163,8 @@ func TestGetNoExpired(t *testing.T) {
163163
t.Fatalf("expected value %x, got %x", testValue, val)
164164
}
165165

166-
if leaf.lastPeriod != period0 {
167-
t.Fatalf("expected last accessed to be 0, got %d", leaf.lastPeriod)
166+
if leaf.period != period0 {
167+
t.Fatalf("expected last accessed to be 0, got %d", leaf.period)
168168
}
169169
}
170170

@@ -190,8 +190,8 @@ func TestGetExpired(t *testing.T) {
190190
t.Fatalf("expected leaf node, got %T", root.(*InternalNode).children[0])
191191
}
192192

193-
if leaf.lastPeriod != period0 {
194-
t.Fatalf("expected last accessed to be 0, got %d", leaf.lastPeriod)
193+
if leaf.period != period0 {
194+
t.Fatalf("expected last accessed to be 0, got %d", leaf.period)
195195
}
196196
}
197197

@@ -231,8 +231,28 @@ func TestDelLeafExpired(t *testing.T) {
231231
t.Fatalf("expected empty node, got %T", root.(*InternalNode).children[0])
232232
}
233233

234-
if leaf.lastPeriod != period0 {
235-
t.Fatalf("expected last accessed to be 0, got %d", leaf.lastPeriod)
234+
if leaf.period != period0 {
235+
t.Fatalf("expected last accessed to be 0, got %d", leaf.period)
236+
}
237+
}
238+
239+
func TestUpdatePeriod(t *testing.T) {
240+
values := make([][]byte, NodeWidth)
241+
values[0] = testValue
242+
leaf1, err := NewLeafNode(zeroKeyTest, values, period1)
243+
if err != nil {
244+
t.Fatalf("error creating leaf node: %v", err)
245+
}
246+
247+
leaf1.updatePeriod(period2)
248+
249+
leaf2, err := NewLeafNode(zeroKeyTest, values, period2)
250+
if err != nil {
251+
t.Fatalf("error creating leaf node: %v", err)
252+
}
253+
254+
if !leaf1.Commitment().Equal(leaf2.Commitment()) {
255+
t.Fatalf("expected commitment to be %x, got %x", leaf1.Commitment(), leaf2.Commitment())
236256
}
237257
}
238258

@@ -244,25 +264,30 @@ func TestReviveExpired(t *testing.T) {
244264
t.Fatalf("error inserting: %v", err)
245265
}
246266

247-
leaf, ok := root.(*InternalNode).children[0].(*LeafNode)
248-
if !ok {
249-
t.Fatalf("expected leaf node, got %T", root.(*InternalNode).children[0])
250-
}
251-
252-
expiredLeaf := NewExpiredLeafNode(leaf.stem, leaf.lastPeriod, leaf.commitment)
267+
leaf := root.(*InternalNode).children[0].(*LeafNode)
268+
expiredLeaf := NewExpiredLeafNode(leaf.stem, new(Point).Set(leaf.commitment))
269+
expiredLeaf.setDepth(1)
253270
root.(*InternalNode).children[0] = expiredLeaf
254271

255-
if err := root.Revive(leaf.stem, leaf.values, leaf.lastPeriod, period2, false, nil); err != nil {
272+
if err := root.Revive(leaf.stem, leaf.values, leaf.period, period2, false, nil); err != nil {
256273
t.Fatalf("error reviving: %v", err)
257274
}
275+
comm1 := root.Commit()
258276

259-
rLeaf, ok := root.(*InternalNode).children[0].(*LeafNode)
260-
if !ok {
261-
t.Fatalf("expected leaf node, got %T", root.(*InternalNode).children[0])
277+
rLeaf := root.(*InternalNode).children[0].(*LeafNode)
278+
if rLeaf.period != period2 {
279+
t.Fatalf("expected last accessed to be 2, got %d", rLeaf.period)
280+
}
281+
282+
// Create a new root and insert the same key-value with the post-revive period
283+
root2 := New()
284+
if err := root2.Insert(zeroKeyTest, testValue, period2, nil); err != nil {
285+
t.Fatalf("error inserting: %v", err)
262286
}
287+
comm2 := root2.Commit()
263288

264-
if rLeaf.lastPeriod != period2 {
265-
t.Fatalf("expected last accessed to be 2, got %d", rLeaf.lastPeriod)
289+
if !comm1.Equal(comm2) {
290+
t.Fatalf("expected commitment to be %x, got %x", comm1, comm2)
266291
}
267292
}
268293

@@ -281,7 +306,7 @@ func TestReviveNoExpired(t *testing.T) {
281306

282307
comm := root.Commit()
283308

284-
if err := root.Revive(leaf.stem, leaf.values, leaf.lastPeriod, period0, false, nil); err != nil {
309+
if err := root.Revive(leaf.stem, leaf.values, leaf.period, period0, false, nil); err != nil {
285310
t.Fatalf("error reviving: %v", err)
286311
}
287312

@@ -290,8 +315,8 @@ func TestReviveNoExpired(t *testing.T) {
290315
t.Fatalf("expected leaf node, got %T", root.(*InternalNode).children[0])
291316
}
292317

293-
if rLeaf.lastPeriod != period0 {
294-
t.Fatalf("expected last accessed to be 0, got %d", rLeaf.lastPeriod)
318+
if rLeaf.period != period0 {
319+
t.Fatalf("expected last accessed to be 0, got %d", rLeaf.period)
295320
}
296321

297322
rComm := root.Commit()
@@ -316,7 +341,7 @@ func TestRootCommitExpired(t *testing.T) {
316341
var init Point
317342
init.Set(root.Commit())
318343

319-
expiredLeaf := NewExpiredLeafNode(leaf.stem, leaf.lastPeriod, leaf.commitment)
344+
expiredLeaf := NewExpiredLeafNode(leaf.stem, leaf.commitment)
320345
root.(*InternalNode).children[0] = expiredLeaf
321346

322347
comm := root.Commit()
@@ -343,4 +368,4 @@ func TestRootCommitDiffEpoch(t *testing.T) {
343368
if comm1.Equal(comm2) {
344369
t.Fatalf("expected different commitments, got %x", comm1)
345370
}
346-
}
371+
}

0 commit comments

Comments
 (0)