Skip to content

Commit

Permalink
go: implement leafop key/value prefixes
Browse files Browse the repository at this point in the history
  • Loading branch information
hdevalence committed Feb 28, 2022
1 parent dd68522 commit 3672d40
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
18 changes: 13 additions & 5 deletions go/ops.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ func (op *LeafOp) Apply(key []byte, value []byte) ([]byte, error) {
if len(value) == 0 {
return nil, errors.New("Leaf op needs value")
}
pkey, err := prepareLeafData(op.PrehashKey, op.Length, key)
pkey, err := prepareLeafData(op.PrehashKey, op.Length, op.PrefixPrehashKey, key)
if err != nil {
return nil, errors.Wrap(err, "prehash key")
}
pvalue, err := prepareLeafData(op.PrehashValue, op.Length, value)
pvalue, err := prepareLeafData(op.PrehashValue, op.Length, op.PrefixPrehashValue, value)
if err != nil {
return nil, errors.Wrap(err, "prehash value")
}
Expand Down Expand Up @@ -56,6 +56,12 @@ func (op *LeafOp) CheckAgainstSpec(spec *ProofSpec) error {
if !bytes.HasPrefix(op.Prefix, lspec.Prefix) {
return errors.Errorf("Leaf Prefix doesn't start with %X", lspec.Prefix)
}
if !bytes.HasPrefix(op.PrefixPrehashKey, lspec.PrefixPrehashKey) {
return errors.Errorf("Leaf PrefixPrehashKey doesn't start with %X", lspec.PrefixPrehashKey)
}
if !bytes.HasPrefix(op.PrefixPrehashValue, lspec.PrefixPrehashKey) {
return errors.Errorf("Leaf PrefixPrehashValue doesn't start with %X", lspec.PrefixPrehashValue)
}
return nil
}

Expand Down Expand Up @@ -89,9 +95,11 @@ func (op *InnerOp) CheckAgainstSpec(spec *ProofSpec) error {
return nil
}

func prepareLeafData(hashOp HashOp, lengthOp LengthOp, data []byte) ([]byte, error) {
// TODO: lengthop before or after hash ???
hdata, err := doHashOrNoop(hashOp, data)
func prepareLeafData(hashOp HashOp, lengthOp LengthOp, prefix []byte, data []byte) ([]byte, error) {
prefixed := make([]byte, 0, len(prefix)+len(data))
prefixed = append(prefixed, prefix...)
prefixed = append(prefixed, data...)
hdata, err := doHashOrNoop(hashOp, prefixed)
if err != nil {
return nil, err
}
Expand Down
12 changes: 12 additions & 0 deletions testdata/TestLeafOpData.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,18 @@
"IsErr": false,
"Expected": "h+BIPo+2JK7y4vexP0FmzaSFuqjjn0N8g9dMlL7bFI8="
},
"hash with prefixed prehash and length prefix": {
"Op": {
"hash": 1,
"prehash_value": 1,
"length": 1,
"prefix_prehash_value": "eWU="
},
"Key": "Zm9vZA==",
"Value": "dCBhbm90aGVyIGxvbmcgc3RyaW5n",
"IsErr": false,
"Expected": "h+BIPo+2JK7y4vexP0FmzaSFuqjjn0N8g9dMlL7bFI8="
},
"requires key": {
"Op": {
"hash": 1
Expand Down

0 comments on commit 3672d40

Please sign in to comment.