Skip to content

Commit

Permalink
Merge branch 'master' into colin/audit-docs
Browse files Browse the repository at this point in the history
  • Loading branch information
colin-axner authored Oct 23, 2024
2 parents c806b37 + 642bc3e commit 7b89657
Show file tree
Hide file tree
Showing 12 changed files with 18 additions and 825 deletions.
1 change: 1 addition & 0 deletions go/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
- fix: guarantee that `spec.InnerSpec.MaxPrefixLength` < `spec.InnerSpec.MinPrefixLength` + `spec.InnerSpec.ChildSize` ([#369](https://github.com/cosmos/ics23/pull/369))
- refactor: support for `BatchProof` and `CompressedBatchProof` is being dropped.
* The API's `BatchVerifyMembership`, `BatchVerifyNonMembership`, and `CombineProofs` have been removed. ([#390](https://github.com/cosmos/ics23/pull/390))
* The API's `IsCompressed`, `Compress`, and `Decompress` have been removed. ([#392](https://github.com/cosmos/ics23/pull/392))

# v0.11.0

Expand Down
159 changes: 0 additions & 159 deletions go/compress.go

This file was deleted.

74 changes: 14 additions & 60 deletions go/ics23.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,25 +23,23 @@ and determine neighbors
*/
package ics23

import (
"bytes"
)

// CommitmentRoot is a byte slice that represents the merkle root of a tree that can be used to validate proofs
type CommitmentRoot []byte

// VerifyMembership returns true iff
// proof is (contains) an ExistenceProof for the given key and value AND
// calculating the root for the ExistenceProof matches the provided CommitmentRoot
// proof is an ExistenceProof for the given key and value AND
// calculating the root for the ExistenceProof matches the provided CommitmentRoot.
func VerifyMembership(spec *ProofSpec, root CommitmentRoot, proof *CommitmentProof, key []byte, value []byte) bool {
// decompress it before running code (no-op if not compressed)
proof = Decompress(proof)
ep := getExistProofForKey(proof, key)
if proof == nil {
return false
}

ep := proof.GetExist()
if ep == nil {
return false
}
err := ep.Verify(spec, root, key, value)
return err == nil

return ep.Verify(spec, root, key, value) == nil
}

// VerifyNonMembership returns true iff
Expand All @@ -50,58 +48,14 @@ func VerifyMembership(spec *ProofSpec, root CommitmentRoot, proof *CommitmentPro
// left and right proofs are neighbors (or left/right most if one is nil)
// provided key is between the keys of the two proofs
func VerifyNonMembership(spec *ProofSpec, root CommitmentRoot, proof *CommitmentProof, key []byte) bool {
// decompress it before running code (no-op if not compressed)
proof = Decompress(proof)
np := getNonExistProofForKey(spec, proof, key)
if np == nil {
return false
}
err := np.Verify(spec, root, key)
return err == nil
}

func getExistProofForKey(proof *CommitmentProof, key []byte) *ExistenceProof {
if proof == nil {
return nil
}

switch p := proof.Proof.(type) {
case *CommitmentProof_Exist:
ep := p.Exist
if bytes.Equal(ep.Key, key) {
return ep
}
case *CommitmentProof_Batch:
for _, sub := range p.Batch.Entries {
if ep := sub.GetExist(); ep != nil && bytes.Equal(ep.Key, key) {
return ep
}
}
return false
}
return nil
}

func getNonExistProofForKey(spec *ProofSpec, proof *CommitmentProof, key []byte) *NonExistenceProof {
switch p := proof.Proof.(type) {
case *CommitmentProof_Nonexist:
np := p.Nonexist
if isLeft(spec, np.Left, key) && isRight(spec, np.Right, key) {
return np
}
case *CommitmentProof_Batch:
for _, sub := range p.Batch.Entries {
if np := sub.GetNonexist(); np != nil && isLeft(spec, np.Left, key) && isRight(spec, np.Right, key) {
return np
}
}
np := proof.GetNonexist()
if np == nil {
return false
}
return nil
}

func isLeft(spec *ProofSpec, left *ExistenceProof, key []byte) bool {
return left == nil || bytes.Compare(keyForComparison(spec, left.Key), keyForComparison(spec, key)) < 0
}

func isRight(spec *ProofSpec, right *ExistenceProof, key []byte) bool {
return right == nil || bytes.Compare(keyForComparison(spec, right.Key), keyForComparison(spec, key)) > 0
return np.Verify(spec, root, key) == nil
}
14 changes: 0 additions & 14 deletions go/proof.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,23 +86,9 @@ func (p *CommitmentProof) Calculate() (CommitmentRoot, error) {
return v.Exist.Calculate()
case *CommitmentProof_Nonexist:
return v.Nonexist.Calculate()
case *CommitmentProof_Batch:
if len(v.Batch.GetEntries()) == 0 || v.Batch.GetEntries()[0] == nil {
return nil, errors.New("batch proof has empty entry")
}
if e := v.Batch.GetEntries()[0].GetExist(); e != nil {
return e.Calculate()
}
if n := v.Batch.GetEntries()[0].GetNonexist(); n != nil {
return n.Calculate()
}
case *CommitmentProof_Compressed:
proof := Decompress(p)
return proof.Calculate()
default:
return nil, errors.New("unrecognized proof type")
}
return nil, errors.New("unrecognized proof type")
}

// Verify does all checks to ensure this proof proves this key, value -> root
Expand Down
38 changes: 0 additions & 38 deletions go/vectors_data_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,22 +73,6 @@ type BatchVectorData struct {
Invalid bool // default is valid
}

func DecompressBatchVectorsTestData(t *testing.T) map[string]*CommitmentProof {
t.Helper()
iavl := filepath.Join("..", "testdata", "iavl")
tendermint := filepath.Join("..", "testdata", "tendermint")
smt := filepath.Join("..", "testdata", "smt")
// note that these batches are already compressed
batchIAVL := loadBatch(t, iavl, "batch_exist.json")
batchTM := loadBatch(t, tendermint, "batch_nonexist.json")
batchSMT := loadBatch(t, smt, "batch_nonexist.json")
return map[string]*CommitmentProof{
"iavl": batchIAVL,
"tendermint": batchTM,
"smt": batchSMT,
}
}

func LoadFile(tb testing.TB, dir string, filename string) (*CommitmentProof, *RefData) {
tb.Helper()
// load the file into a json struct
Expand Down Expand Up @@ -128,25 +112,3 @@ func mustHex(tb testing.TB, data string) []byte {
}
return res
}

func loadBatch(t *testing.T, dir string, filename string) *CommitmentProof {
t.Helper()
// load the file into a json struct
name := filepath.Join(dir, filename)
bz, err := os.ReadFile(name)
if err != nil {
t.Fatalf("Read file: %+v", err)
}
var data BatchVector
err = json.Unmarshal(bz, &data)
if err != nil {
t.Fatalf("Unmarshal json: %+v", err)
}
// parse the protobuf object
var proof CommitmentProof
err = proof.Unmarshal(mustHex(t, data.Proof))
if err != nil {
t.Fatalf("Unmarshal protobuf: %+v", err)
}
return &proof
}
Loading

0 comments on commit 7b89657

Please sign in to comment.