Skip to content

Commit

Permalink
hashednode: cache commitment resolution (#309)
Browse files Browse the repository at this point in the history
* hashednode: cache commitment resolution

Cache resolved commitment in `HashedNode{resolution:...}` to prevent
the potential for duplicate deserialization. Fixes #307.

* hashednode: rename cached commitment field

Rename `HashedNode{resolution:...}` to `HashedNode{cachedPoint...}`.
  • Loading branch information
bshiluk authored Dec 6, 2022
1 parent 17a818a commit f2d2a77
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 13 deletions.
2 changes: 1 addition & 1 deletion encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ func CreateInternalNode(bitlist []byte, raw []byte, depth byte, comm []byte) (*I
return nil, ErrInvalidNodeEncoding
}
for i, index := range indices {
n.children[index] = &HashedNode{raw[i*32 : (i+1)*32]}
n.children[index] = &HashedNode{commitment: raw[i*32 : (i+1)*32]}
}
n.commitment = new(Point)
n.commitment.SetBytesTrusted(comm)
Expand Down
19 changes: 11 additions & 8 deletions hashednode.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ import (
)

type HashedNode struct {
commitment []byte
commitment []byte
cachedPoint *Point
}

func (*HashedNode) Insert([]byte, []byte, NodeResolverFn) error {
Expand All @@ -54,9 +55,11 @@ func (n *HashedNode) Commit() *Point {
if n.commitment == nil {
panic("nil commitment")
}
c := new(Point)
c.SetBytesTrusted(n.commitment)
return c
if n.cachedPoint == nil {
n.cachedPoint = new(Point)
n.cachedPoint.SetBytesTrusted(n.commitment)
}
return n.cachedPoint
}

func (n *HashedNode) Commitment() *Point {
Expand Down Expand Up @@ -92,8 +95,8 @@ func (*HashedNode) setDepth(_ byte) {
}

func (n *HashedNode) Hash() *Fr {
h := new(Fr)
c := n.Commitment()
toFr(h, c)
return h
comm := n.Commitment()
hash := new(Fr)
toFr(hash, comm)
return hash
}
4 changes: 2 additions & 2 deletions stateless.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ func (n *StatelessNode) InsertAtStem(stem []byte, values [][]byte, resolver Node
return nil
}

n.children[nChild] = &HashedNode{unresolved}
n.children[nChild] = &HashedNode{commitment: unresolved}
// fallthrough to hash resolution
}

Expand Down Expand Up @@ -774,7 +774,7 @@ func (n *StatelessNode) setDepth(d byte) {

func (n *StatelessNode) ToHashedNode() *HashedNode {
b := n.commitment.Bytes()
return &HashedNode{b[:]}
return &HashedNode{commitment: b[:]}
}

func (n *StatelessNode) Flush(flush NodeFlushFn) {
Expand Down
4 changes: 2 additions & 2 deletions tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ func (n *InternalNode) toHashedNode() *HashedNode {
panic("nil commitment")
}
comm := n.commitment.Bytes()
return &HashedNode{comm[:]}
return &HashedNode{commitment: comm[:]}
}
func (n *InternalNode) InsertOrdered(key []byte, value []byte, flush NodeFlushFn) error {
values := make([][]byte, NodeWidth)
Expand Down Expand Up @@ -774,7 +774,7 @@ func (n *LeafNode) ToHashedNode() *HashedNode {
panic("nil commitment")
}
comm := n.commitment.Bytes()
return &HashedNode{comm[:]}
return &HashedNode{commitment: comm[:]}
}

func (n *LeafNode) Insert(k []byte, value []byte, _ NodeResolverFn) error {
Expand Down

0 comments on commit f2d2a77

Please sign in to comment.