Skip to content

Commit

Permalink
init go mod, copy proto files and modify directory path
Browse files Browse the repository at this point in the history
  • Loading branch information
cmwaters committed Dec 19, 2023
1 parent d3b2b9e commit fdf1e92
Show file tree
Hide file tree
Showing 21 changed files with 683 additions and 82 deletions.
26 changes: 0 additions & 26 deletions crypto/merkle/hash.go

This file was deleted.

6 changes: 6 additions & 0 deletions go.work
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
go 1.21.5

use (
.
./merkle
)
3 changes: 3 additions & 0 deletions go.work.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
golang.org/x/mod v0.14.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/tools v0.16.0/go.mod h1:kYVVN6I1mBNoB1OX+noeBjbRk4IUEPa7JJ+TJMEooJ0=
4 changes: 3 additions & 1 deletion crypto/merkle/README.md → merkle/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# Merkle Tree

For smaller static data structures that don't require immutable snapshots or mutability;
For smaller static data structures that don't require immutable snapshots or mutability;
for instance the transactions and validation signatures of a block can be hashed using this simple merkle tree logic.

This is a forked copy of github.com/cometbft/cometbft/crypto/merkle
File renamed without changes.
15 changes: 15 additions & 0 deletions merkle/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module github.com/celestiaorg/go-square/merkle

go 1.21.5

require (
github.com/stretchr/testify v1.8.4
google.golang.org/protobuf v1.31.0
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
18 changes: 18 additions & 0 deletions merkle/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs8=
google.golang.org/protobuf v1.31.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
31 changes: 31 additions & 0 deletions merkle/hash.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package merkle

import (
"crypto/sha256"
)

// TODO: make these have a large predefined capacity
var (
leafPrefix = []byte{0}
innerPrefix = []byte{1}
)

// returns empty sha256 hash
func emptyHash() []byte {
return hash([]byte{})
}

// returns sha256(0x00 || leaf)
func leafHash(leaf []byte) []byte {
return hash(append(leafPrefix, leaf...))
}

// returns sha256(0x01 || left || right)
func innerHash(left []byte, right []byte) []byte {
return hash(append(innerPrefix, append(left, right...)...))
}

func hash(bz []byte) []byte {
h := sha256.Sum256(bz)
return h[:]
}
18 changes: 9 additions & 9 deletions crypto/merkle/proof.go → merkle/proof.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ package merkle

import (
"bytes"
"crypto/sha256"
"errors"
"fmt"

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

const (
Expand Down Expand Up @@ -117,25 +117,25 @@ func (sp *Proof) ValidateBasic() error {
if sp.Index < 0 {
return errors.New("negative Index")
}
if len(sp.LeafHash) != tmhash.Size {
return fmt.Errorf("expected LeafHash size to be %d, got %d", tmhash.Size, len(sp.LeafHash))
if len(sp.LeafHash) != sha256.Size {
return fmt.Errorf("expected LeafHash size to be %d, got %d", sha256.Size, len(sp.LeafHash))
}
if len(sp.Aunts) > MaxAunts {
return fmt.Errorf("expected no more than %d aunts, got %d", MaxAunts, len(sp.Aunts))
}
for i, auntHash := range sp.Aunts {
if len(auntHash) != tmhash.Size {
return fmt.Errorf("expected Aunts#%d size to be %d, got %d", i, tmhash.Size, len(auntHash))
if len(auntHash) != sha256.Size {
return fmt.Errorf("expected Aunts#%d size to be %d, got %d", i, sha256.Size, len(auntHash))
}
}
return nil
}

func (sp *Proof) ToProto() *cmtcrypto.Proof {
func (sp *Proof) ToProto() *wire.Proof {
if sp == nil {
return nil
}
pb := new(cmtcrypto.Proof)
pb := new(wire.Proof)

pb.Total = sp.Total
pb.Index = sp.Index
Expand All @@ -145,7 +145,7 @@ func (sp *Proof) ToProto() *cmtcrypto.Proof {
return pb
}

func ProofFromProto(pb *cmtcrypto.Proof) (*Proof, error) {
func ProofFromProto(pb *wire.Proof) (*Proof, error) {
if pb == nil {
return nil, errors.New("nil proof")
}
Expand Down
File renamed without changes.
File renamed without changes.
23 changes: 13 additions & 10 deletions crypto/merkle/proof_op.go → merkle/proof_op.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"errors"
"fmt"

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

//----------------------------------------
Expand All @@ -21,7 +21,7 @@ import (
type ProofOperator interface {
Run([][]byte) ([][]byte, error)
GetKey() []byte
ProofOp() cmtcrypto.ProofOp
ProofOp() wire.ProofOp
}

//----------------------------------------
Expand Down Expand Up @@ -101,7 +101,7 @@ func (poz ProofOperators) VerifyFromKeys(root []byte, keys [][]byte, args [][]by
//----------------------------------------
// ProofRuntime - main entrypoint

type OpDecoder func(cmtcrypto.ProofOp) (ProofOperator, error)
type OpDecoder func(*wire.ProofOp) (ProofOperator, error)

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

func (prt *ProofRuntime) Decode(pop cmtcrypto.ProofOp) (ProofOperator, error) {
func (prt *ProofRuntime) Decode(pop *wire.ProofOp) (ProofOperator, error) {
decoder := prt.decoders[pop.Type]
if decoder == nil {
return nil, fmt.Errorf("unrecognized proof type %v", pop.Type)
}
if pop == nil {
return nil, errors.New("nil ProofOp")
}
return decoder(pop)
}

func (prt *ProofRuntime) DecodeProof(proof *cmtcrypto.ProofOps) (ProofOperators, error) {
func (prt *ProofRuntime) DecodeProof(proof *wire.ProofOps) (ProofOperators, error) {
poz := make(ProofOperators, 0, len(proof.Ops))
for _, pop := range proof.Ops {
operator, err := prt.Decode(pop)
Expand All @@ -141,21 +144,21 @@ func (prt *ProofRuntime) DecodeProof(proof *cmtcrypto.ProofOps) (ProofOperators,
return poz, nil
}

func (prt *ProofRuntime) VerifyValue(proof *cmtcrypto.ProofOps, root []byte, keypath string, value []byte) (err error) {
func (prt *ProofRuntime) VerifyValue(proof *wire.ProofOps, root []byte, keypath string, value []byte) (err error) {
return prt.Verify(proof, root, keypath, [][]byte{value})
}

func (prt *ProofRuntime) VerifyValueFromKeys(proof *cmtcrypto.ProofOps, root []byte, keys [][]byte, value []byte) (err error) {
func (prt *ProofRuntime) VerifyValueFromKeys(proof *wire.ProofOps, root []byte, keys [][]byte, value []byte) (err error) {
return prt.VerifyFromKeys(proof, root, keys, [][]byte{value})
}

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

func (prt *ProofRuntime) Verify(proof *cmtcrypto.ProofOps, root []byte, keypath string, args [][]byte) (err error) {
func (prt *ProofRuntime) Verify(proof *wire.ProofOps, root []byte, keypath string, args [][]byte) (err error) {
poz, err := prt.DecodeProof(proof)
if err != nil {
return fmt.Errorf("decoding proof: %w", err)
Expand All @@ -166,7 +169,7 @@ func (prt *ProofRuntime) Verify(proof *cmtcrypto.ProofOps, root []byte, keypath
// VerifyFromKeys performs the same verification logic as the normal Verify
// method, except it does not perform any processing on the keypath. This is
// useful when using keys that have split or escape points as a part of the key.
func (prt *ProofRuntime) VerifyFromKeys(proof *cmtcrypto.ProofOps, root []byte, keys [][]byte, args [][]byte) (err error) {
func (prt *ProofRuntime) VerifyFromKeys(proof *wire.ProofOps, root []byte, keys [][]byte, args [][]byte) (err error) {
poz, err := prt.DecodeProof(proof)
if err != nil {
return fmt.Errorf("decoding proof: %w", err)
Expand Down
17 changes: 8 additions & 9 deletions crypto/merkle/proof_test.go → merkle/proof_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@ import (
"fmt"
"testing"

wire "github.com/celestiaorg/go-square/merkle/proto/gen/merkle/v1"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/cometbft/cometbft/crypto/tmhash"
cmtcrypto "github.com/cometbft/cometbft/proto/tendermint/crypto"
"google.golang.org/protobuf/proto"
)

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

func (dop DominoOp) ProofOp() cmtcrypto.ProofOp {
dopb := cmtcrypto.DominoOp{
func (dop DominoOp) ProofOp() wire.ProofOp {
dopb := &wire.DominoOp{
Key: dop.key,
Input: dop.Input,
Output: dop.Output,
}
bz, err := dopb.Marshal()
bz, err := proto.Marshal(dopb)
if err != nil {
panic(err)
}

return cmtcrypto.ProofOp{
return wire.ProofOp{
Type: ProofOpDomino,
Key: []byte(dop.key),
Data: bz,
Expand Down Expand Up @@ -270,11 +269,11 @@ func TestVsa2022_100(t *testing.T) {
// a fake key-value pair and its hash
key := []byte{0x13}
value := []byte{0x37}
vhash := tmhash.Sum(value)
vhash := hash(value)
bz := new(bytes.Buffer)
_ = encodeByteSlice(bz, key)
_ = encodeByteSlice(bz, vhash)
kvhash := tmhash.Sum(append([]byte{0}, bz.Bytes()...))
kvhash := hash(append([]byte{0}, bz.Bytes()...))

// the malicious `op`
op := NewValueOp(
Expand Down
21 changes: 11 additions & 10 deletions crypto/merkle/proof_value.go → merkle/proof_value.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ package merkle

import (
"bytes"
"crypto/sha256"
"fmt"

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

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

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

func (op ValueOp) ProofOp() cmtcrypto.ProofOp {
pbval := cmtcrypto.ValueOp{
func (op ValueOp) ProofOp() wire.ProofOp {
pbval := &wire.ValueOp{
Key: op.key,
Proof: op.Proof.ToProto(),
}
bz, err := pbval.Marshal()
bz, err := proto.Marshal(pbval)
if err != nil {
panic(err)
}
return cmtcrypto.ProofOp{
return wire.ProofOp{
Type: ProofOpValue,
Key: op.key,
Data: bz,
Expand All @@ -79,7 +80,7 @@ func (op ValueOp) Run(args [][]byte) ([][]byte, error) {
return nil, fmt.Errorf("expected 1 arg, got %v", len(args))
}
value := args[0]
hasher := tmhash.New()
hasher := sha256.New()
hasher.Write(value)
vhash := hasher.Sum(nil)

Expand Down
6 changes: 6 additions & 0 deletions merkle/proto/buf.gen.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
version: v1
plugins:
- plugin: buf.build/protocolbuffers/go
out: gen
opt:
- paths=source_relative
Loading

0 comments on commit fdf1e92

Please sign in to comment.