Skip to content

Commit fdf1e92

Browse files
committed
init go mod, copy proto files and modify directory path
1 parent d3b2b9e commit fdf1e92

21 files changed

+683
-82
lines changed

crypto/merkle/hash.go

-26
This file was deleted.

go.work

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
go 1.21.5
2+
3+
use (
4+
.
5+
./merkle
6+
)

go.work.sum

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
2+
golang.org/x/mod v0.14.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
3+
golang.org/x/tools v0.16.0/go.mod h1:kYVVN6I1mBNoB1OX+noeBjbRk4IUEPa7JJ+TJMEooJ0=
+3-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
# Merkle Tree
22

3-
For smaller static data structures that don't require immutable snapshots or mutability;
3+
For smaller static data structures that don't require immutable snapshots or mutability;
44
for instance the transactions and validation signatures of a block can be hashed using this simple merkle tree logic.
5+
6+
This is a forked copy of github.com/cometbft/cometbft/crypto/merkle

crypto/merkle/doc.go merkle/doc.go

File renamed without changes.

merkle/go.mod

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
module github.com/celestiaorg/go-square/merkle
2+
3+
go 1.21.5
4+
5+
require (
6+
github.com/stretchr/testify v1.8.4
7+
google.golang.org/protobuf v1.31.0
8+
)
9+
10+
require (
11+
github.com/davecgh/go-spew v1.1.1 // indirect
12+
github.com/google/go-cmp v0.5.9 // indirect
13+
github.com/pmezard/go-difflib v1.0.0 // indirect
14+
gopkg.in/yaml.v3 v3.0.1 // indirect
15+
)

merkle/go.sum

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
2+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
3+
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
4+
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
5+
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
6+
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
7+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
8+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
9+
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
10+
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
11+
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
12+
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
13+
google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs8=
14+
google.golang.org/protobuf v1.31.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
15+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
16+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
17+
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
18+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

merkle/hash.go

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package merkle
2+
3+
import (
4+
"crypto/sha256"
5+
)
6+
7+
// TODO: make these have a large predefined capacity
8+
var (
9+
leafPrefix = []byte{0}
10+
innerPrefix = []byte{1}
11+
)
12+
13+
// returns empty sha256 hash
14+
func emptyHash() []byte {
15+
return hash([]byte{})
16+
}
17+
18+
// returns sha256(0x00 || leaf)
19+
func leafHash(leaf []byte) []byte {
20+
return hash(append(leafPrefix, leaf...))
21+
}
22+
23+
// returns sha256(0x01 || left || right)
24+
func innerHash(left []byte, right []byte) []byte {
25+
return hash(append(innerPrefix, append(left, right...)...))
26+
}
27+
28+
func hash(bz []byte) []byte {
29+
h := sha256.Sum256(bz)
30+
return h[:]
31+
}

crypto/merkle/proof.go merkle/proof.go

+9-9
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ package merkle
22

33
import (
44
"bytes"
5+
"crypto/sha256"
56
"errors"
67
"fmt"
78

8-
"github.com/cometbft/cometbft/crypto/tmhash"
9-
cmtcrypto "github.com/cometbft/cometbft/proto/tendermint/crypto"
9+
wire "github.com/celestiaorg/go-square/merkle/proto/gen/merkle/v1"
1010
)
1111

1212
const (
@@ -117,25 +117,25 @@ func (sp *Proof) ValidateBasic() error {
117117
if sp.Index < 0 {
118118
return errors.New("negative Index")
119119
}
120-
if len(sp.LeafHash) != tmhash.Size {
121-
return fmt.Errorf("expected LeafHash size to be %d, got %d", tmhash.Size, len(sp.LeafHash))
120+
if len(sp.LeafHash) != sha256.Size {
121+
return fmt.Errorf("expected LeafHash size to be %d, got %d", sha256.Size, len(sp.LeafHash))
122122
}
123123
if len(sp.Aunts) > MaxAunts {
124124
return fmt.Errorf("expected no more than %d aunts, got %d", MaxAunts, len(sp.Aunts))
125125
}
126126
for i, auntHash := range sp.Aunts {
127-
if len(auntHash) != tmhash.Size {
128-
return fmt.Errorf("expected Aunts#%d size to be %d, got %d", i, tmhash.Size, len(auntHash))
127+
if len(auntHash) != sha256.Size {
128+
return fmt.Errorf("expected Aunts#%d size to be %d, got %d", i, sha256.Size, len(auntHash))
129129
}
130130
}
131131
return nil
132132
}
133133

134-
func (sp *Proof) ToProto() *cmtcrypto.Proof {
134+
func (sp *Proof) ToProto() *wire.Proof {
135135
if sp == nil {
136136
return nil
137137
}
138-
pb := new(cmtcrypto.Proof)
138+
pb := new(wire.Proof)
139139

140140
pb.Total = sp.Total
141141
pb.Index = sp.Index
@@ -145,7 +145,7 @@ func (sp *Proof) ToProto() *cmtcrypto.Proof {
145145
return pb
146146
}
147147

148-
func ProofFromProto(pb *cmtcrypto.Proof) (*Proof, error) {
148+
func ProofFromProto(pb *wire.Proof) (*Proof, error) {
149149
if pb == nil {
150150
return nil, errors.New("nil proof")
151151
}
File renamed without changes.
File renamed without changes.

crypto/merkle/proof_op.go merkle/proof_op.go

+13-10
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"errors"
66
"fmt"
77

8-
cmtcrypto "github.com/cometbft/cometbft/proto/tendermint/crypto"
8+
wire "github.com/celestiaorg/go-square/merkle/proto/gen/merkle/v1"
99
)
1010

1111
//----------------------------------------
@@ -21,7 +21,7 @@ import (
2121
type ProofOperator interface {
2222
Run([][]byte) ([][]byte, error)
2323
GetKey() []byte
24-
ProofOp() cmtcrypto.ProofOp
24+
ProofOp() wire.ProofOp
2525
}
2626

2727
//----------------------------------------
@@ -101,7 +101,7 @@ func (poz ProofOperators) VerifyFromKeys(root []byte, keys [][]byte, args [][]by
101101
//----------------------------------------
102102
// ProofRuntime - main entrypoint
103103

104-
type OpDecoder func(cmtcrypto.ProofOp) (ProofOperator, error)
104+
type OpDecoder func(*wire.ProofOp) (ProofOperator, error)
105105

106106
type ProofRuntime struct {
107107
decoders map[string]OpDecoder
@@ -121,15 +121,18 @@ func (prt *ProofRuntime) RegisterOpDecoder(typ string, dec OpDecoder) {
121121
prt.decoders[typ] = dec
122122
}
123123

124-
func (prt *ProofRuntime) Decode(pop cmtcrypto.ProofOp) (ProofOperator, error) {
124+
func (prt *ProofRuntime) Decode(pop *wire.ProofOp) (ProofOperator, error) {
125125
decoder := prt.decoders[pop.Type]
126126
if decoder == nil {
127127
return nil, fmt.Errorf("unrecognized proof type %v", pop.Type)
128128
}
129+
if pop == nil {
130+
return nil, errors.New("nil ProofOp")
131+
}
129132
return decoder(pop)
130133
}
131134

132-
func (prt *ProofRuntime) DecodeProof(proof *cmtcrypto.ProofOps) (ProofOperators, error) {
135+
func (prt *ProofRuntime) DecodeProof(proof *wire.ProofOps) (ProofOperators, error) {
133136
poz := make(ProofOperators, 0, len(proof.Ops))
134137
for _, pop := range proof.Ops {
135138
operator, err := prt.Decode(pop)
@@ -141,21 +144,21 @@ func (prt *ProofRuntime) DecodeProof(proof *cmtcrypto.ProofOps) (ProofOperators,
141144
return poz, nil
142145
}
143146

144-
func (prt *ProofRuntime) VerifyValue(proof *cmtcrypto.ProofOps, root []byte, keypath string, value []byte) (err error) {
147+
func (prt *ProofRuntime) VerifyValue(proof *wire.ProofOps, root []byte, keypath string, value []byte) (err error) {
145148
return prt.Verify(proof, root, keypath, [][]byte{value})
146149
}
147150

148-
func (prt *ProofRuntime) VerifyValueFromKeys(proof *cmtcrypto.ProofOps, root []byte, keys [][]byte, value []byte) (err error) {
151+
func (prt *ProofRuntime) VerifyValueFromKeys(proof *wire.ProofOps, root []byte, keys [][]byte, value []byte) (err error) {
149152
return prt.VerifyFromKeys(proof, root, keys, [][]byte{value})
150153
}
151154

152155
// TODO In the long run we'll need a method of classification of ops,
153156
// whether existence or absence or perhaps a third?
154-
func (prt *ProofRuntime) VerifyAbsence(proof *cmtcrypto.ProofOps, root []byte, keypath string) (err error) {
157+
func (prt *ProofRuntime) VerifyAbsence(proof *wire.ProofOps, root []byte, keypath string) (err error) {
155158
return prt.Verify(proof, root, keypath, nil)
156159
}
157160

158-
func (prt *ProofRuntime) Verify(proof *cmtcrypto.ProofOps, root []byte, keypath string, args [][]byte) (err error) {
161+
func (prt *ProofRuntime) Verify(proof *wire.ProofOps, root []byte, keypath string, args [][]byte) (err error) {
159162
poz, err := prt.DecodeProof(proof)
160163
if err != nil {
161164
return fmt.Errorf("decoding proof: %w", err)
@@ -166,7 +169,7 @@ func (prt *ProofRuntime) Verify(proof *cmtcrypto.ProofOps, root []byte, keypath
166169
// VerifyFromKeys performs the same verification logic as the normal Verify
167170
// method, except it does not perform any processing on the keypath. This is
168171
// useful when using keys that have split or escape points as a part of the key.
169-
func (prt *ProofRuntime) VerifyFromKeys(proof *cmtcrypto.ProofOps, root []byte, keys [][]byte, args [][]byte) (err error) {
172+
func (prt *ProofRuntime) VerifyFromKeys(proof *wire.ProofOps, root []byte, keys [][]byte, args [][]byte) (err error) {
170173
poz, err := prt.DecodeProof(proof)
171174
if err != nil {
172175
return fmt.Errorf("decoding proof: %w", err)

crypto/merkle/proof_test.go merkle/proof_test.go

+8-9
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,10 @@ import (
66
"fmt"
77
"testing"
88

9+
wire "github.com/celestiaorg/go-square/merkle/proto/gen/merkle/v1"
910
"github.com/stretchr/testify/assert"
1011
"github.com/stretchr/testify/require"
11-
12-
"github.com/cometbft/cometbft/crypto/tmhash"
13-
cmtcrypto "github.com/cometbft/cometbft/proto/tendermint/crypto"
12+
"google.golang.org/protobuf/proto"
1413
)
1514

1615
const ProofOpDomino = "test:domino"
@@ -31,18 +30,18 @@ func NewDominoOp(key, input, output string) DominoOp {
3130
}
3231
}
3332

34-
func (dop DominoOp) ProofOp() cmtcrypto.ProofOp {
35-
dopb := cmtcrypto.DominoOp{
33+
func (dop DominoOp) ProofOp() wire.ProofOp {
34+
dopb := &wire.DominoOp{
3635
Key: dop.key,
3736
Input: dop.Input,
3837
Output: dop.Output,
3938
}
40-
bz, err := dopb.Marshal()
39+
bz, err := proto.Marshal(dopb)
4140
if err != nil {
4241
panic(err)
4342
}
4443

45-
return cmtcrypto.ProofOp{
44+
return wire.ProofOp{
4645
Type: ProofOpDomino,
4746
Key: []byte(dop.key),
4847
Data: bz,
@@ -270,11 +269,11 @@ func TestVsa2022_100(t *testing.T) {
270269
// a fake key-value pair and its hash
271270
key := []byte{0x13}
272271
value := []byte{0x37}
273-
vhash := tmhash.Sum(value)
272+
vhash := hash(value)
274273
bz := new(bytes.Buffer)
275274
_ = encodeByteSlice(bz, key)
276275
_ = encodeByteSlice(bz, vhash)
277-
kvhash := tmhash.Sum(append([]byte{0}, bz.Bytes()...))
276+
kvhash := hash(append([]byte{0}, bz.Bytes()...))
278277

279278
// the malicious `op`
280279
op := NewValueOp(

crypto/merkle/proof_value.go merkle/proof_value.go

+11-10
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ package merkle
22

33
import (
44
"bytes"
5+
"crypto/sha256"
56
"fmt"
67

7-
"github.com/cometbft/cometbft/crypto/tmhash"
8-
cmtcrypto "github.com/cometbft/cometbft/proto/tendermint/crypto"
8+
wire "github.com/celestiaorg/go-square/merkle/proto/gen/merkle/v1"
9+
"google.golang.org/protobuf/proto"
910
)
1011

1112
const ProofOpValue = "simple:v"
@@ -37,12 +38,12 @@ func NewValueOp(key []byte, proof *Proof) ValueOp {
3738
}
3839
}
3940

40-
func ValueOpDecoder(pop cmtcrypto.ProofOp) (ProofOperator, error) {
41+
func ValueOpDecoder(pop *wire.ProofOp) (ProofOperator, error) {
4142
if pop.Type != ProofOpValue {
4243
return nil, fmt.Errorf("unexpected ProofOp.Type; got %v, want %v", pop.Type, ProofOpValue)
4344
}
44-
var pbop cmtcrypto.ValueOp // a bit strange as we'll discard this, but it works.
45-
err := pbop.Unmarshal(pop.Data)
45+
var pbop wire.ValueOp // a bit strange as we'll discard this, but it works.
46+
err := proto.Unmarshal(pop.Data, &pbop)
4647
if err != nil {
4748
return nil, fmt.Errorf("decoding ProofOp.Data into ValueOp: %w", err)
4849
}
@@ -54,16 +55,16 @@ func ValueOpDecoder(pop cmtcrypto.ProofOp) (ProofOperator, error) {
5455
return NewValueOp(pop.Key, sp), nil
5556
}
5657

57-
func (op ValueOp) ProofOp() cmtcrypto.ProofOp {
58-
pbval := cmtcrypto.ValueOp{
58+
func (op ValueOp) ProofOp() wire.ProofOp {
59+
pbval := &wire.ValueOp{
5960
Key: op.key,
6061
Proof: op.Proof.ToProto(),
6162
}
62-
bz, err := pbval.Marshal()
63+
bz, err := proto.Marshal(pbval)
6364
if err != nil {
6465
panic(err)
6566
}
66-
return cmtcrypto.ProofOp{
67+
return wire.ProofOp{
6768
Type: ProofOpValue,
6869
Key: op.key,
6970
Data: bz,
@@ -79,7 +80,7 @@ func (op ValueOp) Run(args [][]byte) ([][]byte, error) {
7980
return nil, fmt.Errorf("expected 1 arg, got %v", len(args))
8081
}
8182
value := args[0]
82-
hasher := tmhash.New()
83+
hasher := sha256.New()
8384
hasher.Write(value)
8485
vhash := hasher.Sum(nil)
8586

merkle/proto/buf.gen.yaml

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
version: v1
2+
plugins:
3+
- plugin: buf.build/protocolbuffers/go
4+
out: gen
5+
opt:
6+
- paths=source_relative

0 commit comments

Comments
 (0)