This repository has been archived by the owner on Feb 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ec6439c
commit 1186539
Showing
10 changed files
with
5,649 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
.PHONY: protoc test | ||
|
||
# make sure we turn on go modules | ||
export GO111MODULE := on | ||
|
||
# PROTOC_FLAGS := -I=.. -I=./vendor -I=$(GOPATH)/src | ||
PROTOC_FLAGS := -I=.. -I=$(GOPATH)/src | ||
|
||
test: | ||
go test . | ||
|
||
protoc: | ||
# @go mod vendor | ||
protoc --gocosmos_out=plugins=interfacetype+grpc,Mgoogle/protobuf/any.proto=github.com/gogo/protobuf/types:. $(PROTOC_FLAGS) ../proofs.proto | ||
|
||
install-proto-dep: | ||
@echo "Installing protoc-gen-gocosmos..." | ||
@go install github.com/regen-network/cosmos-proto/protoc-gen-gocosmos | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
package ics23 | ||
|
||
// IsCompressed returns true if the proof was compressed | ||
func IsCompressed(proof *CommitmentProof) bool { | ||
return proof.GetCompressed() != nil | ||
} | ||
|
||
// Compress will return a CompressedBatchProof if the input is BatchProof | ||
// Otherwise it will return the input. | ||
// This is safe to call multiple times (idempotent) | ||
func Compress(proof *CommitmentProof) *CommitmentProof { | ||
batch := proof.GetBatch() | ||
if batch == nil { | ||
return proof | ||
} | ||
return &CommitmentProof{ | ||
Proof: &CommitmentProof_Compressed{ | ||
Compressed: compress(batch), | ||
}, | ||
} | ||
} | ||
|
||
// Decompress will return a BatchProof if the input is CompressedBatchProof | ||
// Otherwise it will return the input. | ||
// This is safe to call multiple times (idempotent) | ||
func Decompress(proof *CommitmentProof) *CommitmentProof { | ||
comp := proof.GetCompressed() | ||
if comp != nil { | ||
return &CommitmentProof{ | ||
Proof: &CommitmentProof_Batch{ | ||
Batch: decompress(comp), | ||
}, | ||
} | ||
} | ||
return proof | ||
} | ||
|
||
func compress(batch *BatchProof) *CompressedBatchProof { | ||
var centries []*CompressedBatchEntry | ||
var lookup []*InnerOp | ||
registry := make(map[string]int32) | ||
|
||
for _, entry := range batch.Entries { | ||
centry := compressEntry(entry, &lookup, registry) | ||
centries = append(centries, centry) | ||
} | ||
|
||
return &CompressedBatchProof{ | ||
Entries: centries, | ||
LookupInners: lookup, | ||
} | ||
} | ||
|
||
func compressEntry(entry *BatchEntry, lookup *[]*InnerOp, registry map[string]int32) *CompressedBatchEntry { | ||
if exist := entry.GetExist(); exist != nil { | ||
return &CompressedBatchEntry{ | ||
Proof: &CompressedBatchEntry_Exist{ | ||
Exist: compressExist(exist, lookup, registry), | ||
}, | ||
} | ||
} | ||
|
||
non := entry.GetNonexist() | ||
return &CompressedBatchEntry{ | ||
Proof: &CompressedBatchEntry_Nonexist{ | ||
Nonexist: &CompressedNonExistenceProof{ | ||
Key: non.Key, | ||
Left: compressExist(non.Left, lookup, registry), | ||
Right: compressExist(non.Right, lookup, registry), | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func compressExist(exist *ExistenceProof, lookup *[]*InnerOp, registry map[string]int32) *CompressedExistenceProof { | ||
if exist == nil { | ||
return nil | ||
} | ||
res := &CompressedExistenceProof{ | ||
Key: exist.Key, | ||
Value: exist.Value, | ||
Leaf: exist.Leaf, | ||
Path: make([]int32, len(exist.Path)), | ||
} | ||
for i, step := range exist.Path { | ||
res.Path[i] = compressStep(step, lookup, registry) | ||
} | ||
return res | ||
} | ||
|
||
func compressStep(step *InnerOp, lookup *[]*InnerOp, registry map[string]int32) int32 { | ||
bz, err := step.Marshal() | ||
if err != nil { | ||
panic(err) | ||
} | ||
sig := string(bz) | ||
|
||
// load from cache if there | ||
if num, ok := registry[sig]; ok { | ||
return num | ||
} | ||
|
||
// create new step if not there | ||
num := int32(len(*lookup)) | ||
*lookup = append(*lookup, step) | ||
registry[sig] = num | ||
return num | ||
} | ||
|
||
func decompress(comp *CompressedBatchProof) *BatchProof { | ||
lookup := comp.LookupInners | ||
|
||
var entries []*BatchEntry | ||
|
||
for _, centry := range comp.Entries { | ||
entry := decompressEntry(centry, lookup) | ||
entries = append(entries, entry) | ||
} | ||
|
||
return &BatchProof{ | ||
Entries: entries, | ||
} | ||
} | ||
|
||
// TendermintSpec constrains the format from proofs-tendermint (crypto/merkle SimpleProof) | ||
var TendermintSpec = &ProofSpec{ | ||
LeafSpec: &LeafOp{ | ||
Prefix: []byte{0}, | ||
PrehashKey: HashOp_NO_HASH, | ||
Hash: HashOp_SHA256, | ||
PrehashValue: HashOp_SHA256, | ||
Length: LengthOp_VAR_PROTO, | ||
}, | ||
InnerSpec: &InnerSpec{ | ||
ChildOrder: []int32{0, 1}, | ||
MinPrefixLength: 1, | ||
MaxPrefixLength: 1, | ||
ChildSize: 32, // (no length byte) | ||
Hash: HashOp_SHA256, | ||
}, | ||
} | ||
|
||
func decompressExist(exist *CompressedExistenceProof, lookup []*InnerOp) *ExistenceProof { | ||
if exist == nil { | ||
return nil | ||
} | ||
res := &ExistenceProof{ | ||
Key: exist.Key, | ||
Value: exist.Value, | ||
Leaf: exist.Leaf, | ||
Path: make([]*InnerOp, len(exist.Path)), | ||
} | ||
for i, step := range exist.Path { | ||
res.Path[i] = lookup[step] | ||
} | ||
return res | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
module github.com/confio/ics23/go | ||
|
||
go 1.14 | ||
|
||
require ( | ||
github.com/gogo/protobuf v1.3.1 | ||
github.com/pkg/errors v0.8.1 | ||
github.com/stretchr/testify v1.8.0 // indirect | ||
golang.org/x/crypto v0.0.0-20190701094942-4def268fd1a4 | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | ||
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/gogo/protobuf v1.3.1 h1:DqDEcV5aeaTmdFBePNpYsp3FlcVH/2ISVVM9Qf8PSls= | ||
github.com/gogo/protobuf v1.3.1/go.mod h1:SlYgWuQ5SjCEi6WLHjHCa1yvBfUnHcTbrrZtXPKa29o= | ||
github.com/kisielk/errcheck v1.2.0/go.mod h1:/BMXB+zMLi60iA8Vv6Ksmxu/1UDYcXs4uQLJ+jE2L00= | ||
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= | ||
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I= | ||
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= | ||
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/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= | ||
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= | ||
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= | ||
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk= | ||
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= | ||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= | ||
golang.org/x/crypto v0.0.0-20190701094942-4def268fd1a4 h1:HuIa8hRrWRSrqYzx1qI49NNxhdi2PrY7gxVSq1JjLDc= | ||
golang.org/x/crypto v0.0.0-20190701094942-4def268fd1a4/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= | ||
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= | ||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= | ||
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= | ||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= | ||
golang.org/x/tools v0.0.0-20181030221726-6c7e314b6563/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= | ||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= | ||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= | ||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= | ||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= |
Oops, something went wrong.