Skip to content

Commit a80b12b

Browse files
authoredOct 25, 2024··
add proof Equal methods (#458)
* add proof Equal methods Signed-off-by: Ignacio Hagopian <[email protected]> * execution spec test workaround Signed-off-by: Ignacio Hagopian <[email protected]> * add nil check Signed-off-by: Ignacio Hagopian <[email protected]> * revert workaround Signed-off-by: Ignacio Hagopian <[email protected]> * readme: nit Signed-off-by: Ignacio Hagopian <[email protected]> --------- Signed-off-by: Ignacio Hagopian <[email protected]>
1 parent dffa756 commit a80b12b

File tree

2 files changed

+66
-1
lines changed

2 files changed

+66
-1
lines changed
 

‎README.md

-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
> A Go implementation of Verkle Tree datastructure defined in the [spec](https://github.com/crate-crypto/verkle-trie-ref/tree/master/verkle).
99
10-
1110
## Test & Benchmarks
1211

1312
To run the tests and benchmarks, run the following commands:

‎proof_ipa.go

+66
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ type VerkleProof struct {
5353
}
5454

5555
func (vp *VerkleProof) Copy() *VerkleProof {
56+
if vp == nil {
57+
return nil
58+
}
5659
ret := &VerkleProof{
5760
OtherStems: make([][StemSize]byte, len(vp.OtherStems)),
5861
DepthExtensionPresent: make([]byte, len(vp.DepthExtensionPresent)),
@@ -73,6 +76,35 @@ func (vp *VerkleProof) Copy() *VerkleProof {
7376
return ret
7477
}
7578

79+
func (vp *VerkleProof) Equal(other *VerkleProof) error {
80+
if len(vp.OtherStems) != len(other.OtherStems) {
81+
return fmt.Errorf("different number of other stems: %d != %d", len(vp.OtherStems), len(other.OtherStems))
82+
}
83+
for i := range vp.OtherStems {
84+
if vp.OtherStems[i] != other.OtherStems[i] {
85+
return fmt.Errorf("different other stem: %x != %x", vp.OtherStems[i], other.OtherStems[i])
86+
}
87+
}
88+
if len(vp.DepthExtensionPresent) != len(other.DepthExtensionPresent) {
89+
return fmt.Errorf("different number of depth extension present: %d != %d", len(vp.DepthExtensionPresent), len(other.DepthExtensionPresent))
90+
}
91+
if !bytes.Equal(vp.DepthExtensionPresent, other.DepthExtensionPresent) {
92+
return fmt.Errorf("different depth extension present: %x != %x", vp.DepthExtensionPresent, other.DepthExtensionPresent)
93+
}
94+
if len(vp.CommitmentsByPath) != len(other.CommitmentsByPath) {
95+
return fmt.Errorf("different number of commitments by path: %d != %d", len(vp.CommitmentsByPath), len(other.CommitmentsByPath))
96+
}
97+
for i := range vp.CommitmentsByPath {
98+
if vp.CommitmentsByPath[i] != other.CommitmentsByPath[i] {
99+
return fmt.Errorf("different commitment by path: %x != %x", vp.CommitmentsByPath[i], other.CommitmentsByPath[i])
100+
}
101+
}
102+
if vp.D != other.D {
103+
return fmt.Errorf("different D: %x != %x", vp.D, other.D)
104+
}
105+
return nil
106+
}
107+
76108
type Proof struct {
77109
Multipoint *ipa.MultiProof // multipoint argument
78110
ExtStatus []byte // the extension status of each stem
@@ -118,6 +150,40 @@ func (sd StateDiff) Copy() StateDiff {
118150
return ret
119151
}
120152

153+
func (sd StateDiff) Equal(other StateDiff) error {
154+
if len(sd) != len(other) {
155+
return fmt.Errorf("different number of stem state diffs: %d != %d", len(sd), len(other))
156+
}
157+
for i := range sd {
158+
if sd[i].Stem != other[i].Stem {
159+
return fmt.Errorf("different stem: %x != %x", sd[i].Stem, other[i].Stem)
160+
}
161+
if len(sd[i].SuffixDiffs) != len(other[i].SuffixDiffs) {
162+
return fmt.Errorf("different number of suffix state diffs: %d != %d", len(sd[i].SuffixDiffs), len(other[i].SuffixDiffs))
163+
}
164+
for j := range sd[i].SuffixDiffs {
165+
if sd[i].SuffixDiffs[j].Suffix != other[i].SuffixDiffs[j].Suffix {
166+
return fmt.Errorf("different suffix: %x != %x", sd[i].SuffixDiffs[j].Suffix, other[i].SuffixDiffs[j].Suffix)
167+
}
168+
if sd[i].SuffixDiffs[j].CurrentValue != nil && other[i].SuffixDiffs[j].CurrentValue != nil {
169+
if *sd[i].SuffixDiffs[j].CurrentValue != *other[i].SuffixDiffs[j].CurrentValue {
170+
return fmt.Errorf("different current value: %x != %x", *sd[i].SuffixDiffs[j].CurrentValue, *other[i].SuffixDiffs[j].CurrentValue)
171+
}
172+
} else if sd[i].SuffixDiffs[j].CurrentValue != nil || other[i].SuffixDiffs[j].CurrentValue != nil {
173+
return fmt.Errorf("different current value: %x != %x", sd[i].SuffixDiffs[j].CurrentValue, other[i].SuffixDiffs[j].CurrentValue)
174+
}
175+
if sd[i].SuffixDiffs[j].NewValue != nil && other[i].SuffixDiffs[j].NewValue != nil {
176+
if *sd[i].SuffixDiffs[j].NewValue != *other[i].SuffixDiffs[j].NewValue {
177+
return fmt.Errorf("different new value: %x != %x", *sd[i].SuffixDiffs[j].NewValue, *other[i].SuffixDiffs[j].NewValue)
178+
}
179+
} else if sd[i].SuffixDiffs[j].NewValue != nil || other[i].SuffixDiffs[j].NewValue != nil {
180+
return fmt.Errorf("different new value: %x != %x", sd[i].SuffixDiffs[j].NewValue, other[i].SuffixDiffs[j].NewValue)
181+
}
182+
}
183+
}
184+
return nil
185+
}
186+
121187
func GetCommitmentsForMultiproof(root VerkleNode, keys [][]byte, resolver NodeResolverFn) (*ProofElements, []byte, []Stem, error) {
122188
sort.Sort(keylist(keys))
123189
return root.GetProofItems(keylist(keys), resolver)

0 commit comments

Comments
 (0)
Please sign in to comment.