Skip to content

Commit

Permalink
add lastPeriod to ExpiredLeafNode
Browse files Browse the repository at this point in the history
  • Loading branch information
weiihann committed Dec 27, 2024
1 parent 64151cb commit b670596
Show file tree
Hide file tree
Showing 10 changed files with 36 additions and 15 deletions.
2 changes: 1 addition & 1 deletion config.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const (
NodeWidth = 256
NodeBitWidth byte = 8
StemSize = 31
periodSize = 8
periodSize = 2
)

func equalPaths(key1, key2 []byte) bool {
Expand Down
1 change: 1 addition & 0 deletions debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ type (

ExportableExpiredLeafNode struct {
Stem Stem `json:"stem"`
LastPeriod StatePeriod `json:"last_period"`
Commitment [32]byte `json:"commitment"`
}
)
7 changes: 4 additions & 3 deletions encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ const (
leafValueIndexSize = 1
singleSlotLeafSize = nodeTypeSize + StemSize + 2*banderwagon.UncompressedSize + leafValueIndexSize + leafSlotSize + periodSize
eoaLeafSize = nodeTypeSize + StemSize + 2*banderwagon.UncompressedSize + leafBasicDataSize + periodSize
expiredLeafSize = nodeTypeSize + StemSize + banderwagon.UncompressedSize
expiredLeafSize = nodeTypeSize + StemSize + periodSize + banderwagon.UncompressedSize
)

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

func parseExpiredLeafNode(serialized []byte, depth byte) (VerkleNode, error) {
l := &ExpiredLeafNode{}
l.stem = serialized[leafStemOffset : leafStemOffset+StemSize]
l.setDepth(depth)
l.stem = serialized[leafStemOffset : leafStemOffset+StemSize]
l.lastPeriod = StatePeriodFromBytes(serialized[leafStemOffset+StemSize:leafStemOffset+StemSize+periodSize])
l.commitment = new(Point)
if err := l.commitment.SetBytesUncompressed(serialized[leafStemOffset+StemSize:], true); err != nil {
if err := l.commitment.SetBytesUncompressed(serialized[leafStemOffset+StemSize+periodSize:], true); err != nil {
return nil, fmt.Errorf("setting commitment: %w", err)
}
return l, nil
Expand Down
6 changes: 5 additions & 1 deletion encoding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ func TestParseExpiredLeaf(t *testing.T) {

comm := srs[0]
stem := ffx32KeyTest[:StemSize]
el := NewExpiredLeafNode(stem, &comm)
el := NewExpiredLeafNode(stem, period2, &comm)

serialized, err := el.Serialize()
if err != nil {
Expand All @@ -222,4 +222,8 @@ func TestParseExpiredLeaf(t *testing.T) {
if !el2.commitment.Equal(&comm) {
t.Fatalf("invalid commitment, got %x, expected %x", el2.commitment, comm)
}

if el2.lastPeriod != period2 {
t.Fatalf("invalid last period, got %d, expected %d", el2.lastPeriod, period2)
}
}
12 changes: 9 additions & 3 deletions expired_leaf.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,18 @@ package verkle
import (
"fmt"
"errors"
"encoding/binary"
)

type ExpiredLeafNode struct {
stem Stem
lastPeriod StatePeriod
commitment *Point
depth byte // used for proof only, not commitment calculation
}

func NewExpiredLeafNode(stem Stem, commitment *Point) *ExpiredLeafNode {
return &ExpiredLeafNode{stem: stem, commitment: commitment}
func NewExpiredLeafNode(stem Stem, lastPeriod StatePeriod, commitment *Point) *ExpiredLeafNode {
return &ExpiredLeafNode{stem: stem, lastPeriod: lastPeriod, commitment: commitment}
}

func (n *ExpiredLeafNode) Insert([]byte, []byte, StatePeriod, NodeResolverFn) error {
Expand Down Expand Up @@ -95,7 +97,11 @@ func (n *ExpiredLeafNode) Serialize() ([]byte, error) {
result := buf[:]
result[0] = expiredLeafType
copy(result[leafStemOffset:], n.stem[:StemSize])
copy(result[leafStemOffset+StemSize:], cBytes[:])

lastPeriod := make([]byte, periodSize)
binary.BigEndian.PutUint16(lastPeriod, uint16(n.lastPeriod))
copy(result[leafStemOffset+StemSize:], lastPeriod)
copy(result[leafStemOffset+StemSize+periodSize:], cBytes[:])

return result, nil
}
Expand Down
2 changes: 1 addition & 1 deletion expired_leaf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ func TestExpiredLeafBasic(t *testing.T) {
cfg := GetConfig()
srs := cfg.conf.SRS
comm := srs[0]
leaf := NewExpiredLeafNode(zeroKeyTest[:StemSize], &comm)
leaf := NewExpiredLeafNode(zeroKeyTest[:StemSize], period0, &comm)

err := leaf.Insert(zeroKeyTest, zeroKeyTest, 0, nil)
if !errors.Is(err, errExpired) {
Expand Down
2 changes: 1 addition & 1 deletion expired_tree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ func TestRootCommitExpired(t *testing.T) {
var init Point
init.Set(root.Commit())

expiredLeaf := NewExpiredLeafNode(leaf.stem, leaf.commitment)
expiredLeaf := NewExpiredLeafNode(leaf.stem, leaf.lastPeriod, leaf.commitment)
root.(*InternalNode).children[0] = expiredLeaf

comm := root.Commit()
Expand Down
7 changes: 7 additions & 0 deletions proof_ipa.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,16 +105,23 @@ func (vp *VerkleProof) Equal(other *VerkleProof) error {
return nil
}

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

type PoeInfo struct {
Stem Stem
Period StatePeriod
}

type SuffixStateDiff struct {
Suffix byte `json:"suffix"`
CurrentValue *[32]byte `json:"currentValue"`
Expand Down
8 changes: 4 additions & 4 deletions proof_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -858,7 +858,7 @@ func TestProofOfExpiryOneLeaf(t *testing.T) {

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

expiredLeaf := NewExpiredLeafNode(leaf.stem, leaf.commitment)
expiredLeaf := NewExpiredLeafNode(leaf.stem, leaf.lastPeriod, leaf.commitment)
expiredLeaf.setDepth(1)
root.(*InternalNode).children[0] = expiredLeaf

Expand Down Expand Up @@ -905,12 +905,12 @@ func TestProofOfExpiryMultipleLeaves(t *testing.T) {
init := root.Commit()

leaf0 := root.(*InternalNode).children[0].(*LeafNode)
expiredLeaf0 := NewExpiredLeafNode(leaf0.stem, leaf0.commitment)
expiredLeaf0 := NewExpiredLeafNode(leaf0.stem, leaf0.lastPeriod, leaf0.commitment)
expiredLeaf0.setDepth(1)
root.(*InternalNode).children[0] = expiredLeaf0

leaff := root.(*InternalNode).children[255].(*LeafNode)
expiredLeaff := NewExpiredLeafNode(leaff.stem, leaff.commitment)
expiredLeaff := NewExpiredLeafNode(leaff.stem, leaff.lastPeriod, leaff.commitment)
expiredLeaff.setDepth(1)
root.(*InternalNode).children[255] = expiredLeaff

Expand Down Expand Up @@ -1202,7 +1202,7 @@ func TestProofVerificationPreStateExpiredPostStateResurrected(t *testing.T) {
rootC := preRoot.Commit()

leaf := preRoot.(*InternalNode).children[0].(*LeafNode)
expiredLeaf := NewExpiredLeafNode(leaf.stem, leaf.commitment)
expiredLeaf := NewExpiredLeafNode(leaf.stem, leaf.lastPeriod, leaf.commitment)
expiredLeaf.setDepth(1)
preRoot.(*InternalNode).children[0] = expiredLeaf

Expand Down
4 changes: 3 additions & 1 deletion tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ func (n *InternalNode) toExportable() *ExportableInternalNode {
case *ExpiredLeafNode:
exportable.Children[i] = &ExportableExpiredLeafNode{
Stem: child.stem,
LastPeriod: child.lastPeriod,
Commitment: child.commitment.Bytes(),
}
default:
Expand Down Expand Up @@ -583,6 +584,7 @@ func (n *InternalNode) CreatePath(path []byte, stemInfo stemInfo, comms []*Point
if len(stemInfo.stem) != StemSize {
return comms, fmt.Errorf("invalid stem size %d", len(stemInfo.stem))
}
// TODO(weiihann): add last period
newchild := &ExpiredLeafNode{
commitment: comms[0],
stem: stemInfo.stem,
Expand Down Expand Up @@ -2062,7 +2064,7 @@ func (n *LeafNode) serializeLeafWithUncompressedCommitments(cBytes, c1Bytes, c2B
// Create the serialization.
var result []byte
lastPeriod := make([]byte, periodSize)
binary.BigEndian.PutUint64(lastPeriod, uint64(n.lastPeriod))
binary.BigEndian.PutUint16(lastPeriod, uint16(n.lastPeriod))
switch {
case count == 1:
var buf [singleSlotLeafSize]byte
Expand Down

0 comments on commit b670596

Please sign in to comment.