Skip to content

Commit 318acd2

Browse files
committed
Op code cleanup
1 parent 4fe03ec commit 318acd2

File tree

3 files changed

+35
-29
lines changed

3 files changed

+35
-29
lines changed

go/ops.go

+32
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package proofs
22

33
import (
4+
"bytes"
45
"crypto"
56
// adds sha256 capability to crypto.SHA256
67
_ "crypto/sha256"
@@ -32,6 +33,28 @@ func (op *LeafOp) Apply(key []byte, value []byte) ([]byte, error) {
3233
return doHash(op.Hash, data)
3334
}
3435

36+
func (op *LeafOp)CheckAgainstSpec(spec *ProofSpec) error {
37+
lspec := spec.LeafSpec
38+
39+
if op.Hash != lspec.Hash {
40+
return fmt.Errorf("Unexpected HashOp: %d", op.Hash)
41+
}
42+
if op.PrehashKey != lspec.PrehashKey {
43+
return fmt.Errorf("Unexpected PrehashKey: %d", op.PrehashKey)
44+
}
45+
if op.PrehashValue != lspec.PrehashValue {
46+
return fmt.Errorf("Unexpected PrehashValue: %d", op.PrehashValue)
47+
}
48+
if op.Length != lspec.Length {
49+
return fmt.Errorf("Unexpected LengthOp: %d", op.Length)
50+
}
51+
if !bytes.HasPrefix(op.Prefix, lspec.Prefix) {
52+
return fmt.Errorf("Leaf Prefix doesn't start with %X", lspec.Prefix)
53+
}
54+
return nil
55+
}
56+
57+
3558
func (op *InnerOp) Apply(child []byte) ([]byte, error) {
3659
if len(child) == 0 {
3760
return nil, fmt.Errorf("Inner op needs child value")
@@ -41,6 +64,15 @@ func (op *InnerOp) Apply(child []byte) ([]byte, error) {
4164
return doHash(op.Hash, preimage)
4265
}
4366

67+
func (inner *InnerOp)CheckAgainstSpec(spec *ProofSpec) error {
68+
leafPrefix := spec.LeafSpec.Prefix
69+
if bytes.HasPrefix(inner.Prefix, leafPrefix) {
70+
return fmt.Errorf("Inner Prefix starts with %X", leafPrefix)
71+
}
72+
return nil
73+
}
74+
75+
4476
func prepareLeafData(hashOp HashOp, lengthOp LengthOp, data []byte) ([]byte, error) {
4577
// TODO: lengthop before or after hash ???
4678
hdata, err := doHashOrNoop(hashOp, data)

go/proof.go

+2-28
Original file line numberDiff line numberDiff line change
@@ -82,44 +82,18 @@ func (p *ExistenceProof) CheckAgainstSpec(spec *ProofSpec) error {
8282
if p.GetLeaf() == nil {
8383
return fmt.Errorf("Existence Proof needs defined LeafOp")
8484
}
85-
err := checkLeaf(p.Leaf, spec.LeafSpec)
85+
err := p.Leaf.CheckAgainstSpec(spec)
8686
if err != nil {
8787
return err
8888
}
8989
for _, inner := range p.Path {
90-
if err := checkInner(inner, spec.LeafSpec.Prefix); err != nil {
90+
if err := inner.CheckAgainstSpec(spec); err != nil {
9191
return err
9292
}
9393
}
9494
return nil
9595
}
9696

97-
func checkLeaf(leaf *LeafOp, spec *LeafOp) error {
98-
if leaf.Hash != spec.Hash {
99-
return fmt.Errorf("Unexpected HashOp: %d", leaf.Hash)
100-
}
101-
if leaf.PrehashKey != spec.PrehashKey {
102-
return fmt.Errorf("Unexpected PrehashKey: %d", leaf.PrehashKey)
103-
}
104-
if leaf.PrehashValue != spec.PrehashValue {
105-
return fmt.Errorf("Unexpected PrehashValue: %d", leaf.PrehashValue)
106-
}
107-
if leaf.Length != spec.Length {
108-
return fmt.Errorf("Unexpected LengthOp: %d", leaf.Length)
109-
}
110-
if !bytes.HasPrefix(leaf.Prefix, spec.Prefix) {
111-
return fmt.Errorf("Leaf Prefix doesn't start with %X", spec.Prefix)
112-
}
113-
return nil
114-
}
115-
116-
func checkInner(inner *InnerOp, leafPrefix []byte) error {
117-
if bytes.HasPrefix(inner.Prefix, leafPrefix) {
118-
return fmt.Errorf("Inner Prefix starts with %X", leafPrefix)
119-
}
120-
return nil
121-
}
122-
12397
// Verify does all checks to ensure this proof proves this key, value -> root
12498
// and matches the spec.
12599
func (p *NonExistenceProof) Verify(spec *ProofSpec, root CommitmentRoot, key []byte) error {

go/proof_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ func TestCheckLeaf(t *testing.T) {
225225

226226
for name, tc := range cases {
227227
t.Run(name, func(t *testing.T) {
228-
err := checkLeaf(tc.leaf, tc.spec)
228+
err := tc.leaf.CheckAgainstSpec(&ProofSpec{LeafSpec: tc.spec})
229229
if tc.isErr && err == nil {
230230
t.Fatal("Expected error, but got nil")
231231
} else if !tc.isErr && err != nil {

0 commit comments

Comments
 (0)