1
1
package proofs
2
2
3
3
import (
4
+ "bytes"
4
5
"crypto"
5
6
// adds sha256 capability to crypto.SHA256
6
7
_ "crypto/sha256"
@@ -32,6 +33,28 @@ func (op *LeafOp) Apply(key []byte, value []byte) ([]byte, error) {
32
33
return doHash (op .Hash , data )
33
34
}
34
35
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
+
35
58
func (op * InnerOp ) Apply (child []byte ) ([]byte , error ) {
36
59
if len (child ) == 0 {
37
60
return nil , fmt .Errorf ("Inner op needs child value" )
@@ -41,6 +64,15 @@ func (op *InnerOp) Apply(child []byte) ([]byte, error) {
41
64
return doHash (op .Hash , preimage )
42
65
}
43
66
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
+
44
76
func prepareLeafData (hashOp HashOp , lengthOp LengthOp , data []byte ) ([]byte , error ) {
45
77
// TODO: lengthop before or after hash ???
46
78
hdata , err := doHashOrNoop (hashOp , data )
0 commit comments