Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

StateProof: State Proof Verification additions #377

Merged
merged 23 commits into from
Aug 31, 2022
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,21 @@ go 1.17

require (
github.com/algorand/avm-abi v0.1.0
github.com/algorand/go-codec/codec v1.1.8
github.com/algorand/go-codec/codec v1.1.9
almog-t marked this conversation as resolved.
Show resolved Hide resolved
github.com/algorand/go-stateproof-verification v0.0.0-20220829135322-3166cac33d5d
github.com/algorand/go-sumhash v1.0.0
github.com/cucumber/godog v0.8.1
github.com/google/go-querystring v1.0.0
github.com/stretchr/testify v1.7.1
github.com/stretchr/testify v1.8.0
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519
)

require (
github.com/algorand/falcon v0.0.0-20220727072124-02a2a64c4414 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
golang.org/x/sys v0.0.0-20211019181941-9d821ace8654 // indirect
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f // indirect
gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
20 changes: 13 additions & 7 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
github.com/algorand/avm-abi v0.1.0 h1:znZFQXpSUVYz37vXbaH5OZG2VK4snTyXwnc/tV9CVr4=
github.com/algorand/avm-abi v0.1.0/go.mod h1:+CgwM46dithy850bpTeHh9MC99zpn2Snirb3QTl2O/g=
github.com/algorand/go-codec v1.1.8 h1:XDSreeeZY8gMst6Edz4RBkl08/DGMJOeHYkoXL2B7wI=
github.com/algorand/go-codec v1.1.8/go.mod h1:XhzVs6VVyWMLu6cApb9/192gBjGRVGm5cX5j203Heg4=
github.com/algorand/go-codec/codec v1.1.8 h1:lsFuhcOH2LiEhpBH3BVUUkdevVmwCRyvb7FCAAPeY6U=
github.com/algorand/go-codec/codec v1.1.8/go.mod h1:tQ3zAJ6ijTps6V+wp8KsGDnPC2uhHVC7ANyrtkIY0bA=
github.com/algorand/falcon v0.0.0-20220727072124-02a2a64c4414 h1:nwYN+GQ7Z5OOfZwqBO1ma7DSlP7S1YrKWICOyjkwqrc=
github.com/algorand/falcon v0.0.0-20220727072124-02a2a64c4414/go.mod h1:OkQyHlGvS0kLNcIWbC21/uQcnbfwSOQm+wiqWwBG9pQ=
github.com/algorand/go-codec/codec v1.1.9 h1:el4HFSPZhP+YCgOZxeFGB/BqlNkaUIs55xcALulUTCM=
github.com/algorand/go-codec/codec v1.1.9/go.mod h1:YkEx5nmr/zuCeaDYOIhlDg92Lxju8tj2d2NrYqP7g7k=
github.com/algorand/go-stateproof-verification v0.0.0-20220829135322-3166cac33d5d h1:xmLcRs4qVnha4VqlmeIzOdcQyks7ZiNpy6hODZeEy58=
github.com/algorand/go-stateproof-verification v0.0.0-20220829135322-3166cac33d5d/go.mod h1:svXZAKwf0vh+Pc/wiBFHGrZU6e/jE56WOaMIEMEqEzY=
github.com/algorand/go-sumhash v1.0.0 h1:00PkvdWAvPGxjd51eF84gZV81hlx3Yt/5sGeHzmcY0U=
github.com/algorand/go-sumhash v1.0.0/go.mod h1:OOe7jdDWUhLkuP1XytkK5gnLu9entAviN5DfDZh6XAc=
github.com/chrismcguire/gobberish v0.0.0-20150821175641-1d8adb509a0e h1:CHPYEbz71w8DqJ7DRIq+MXyCQsdibK08vdcQTY4ufas=
github.com/chrismcguire/gobberish v0.0.0-20150821175641-1d8adb509a0e/go.mod h1:6Xhs0ZlsRjXLIiSMLKafbZxML/j30pg9Z1priLuha5s=
github.com/cucumber/godog v0.8.1 h1:lVb+X41I4YDreE+ibZ50bdXmySxgRviYFgKY6Aw4XE8=
Expand All @@ -21,8 +25,10 @@ github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLA
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/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMTY=
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=
github.com/yuin/goldmark v1.4.1/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519 h1:7I4JAnoQBe7ZtJcBaYHi5UtiO8tQHbUSXxL+pnGRANg=
Expand Down Expand Up @@ -54,5 +60,5 @@ gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f h1:BLraFXnmrev5lT+xlilqcH8XK9/i0At2xKjWk4p6zsU=
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f/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.0-20200615113413-eeeca48fe776 h1:tQIYjPdBoyREyB9XMu+nnTclpTYkz2zFM+lzLJFO4gQ=
gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776/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=
10 changes: 10 additions & 0 deletions stateproofs/newStateProof.json

Large diffs are not rendered by default.

10 changes: 10 additions & 0 deletions stateproofs/prevStateProof.json

Large diffs are not rendered by default.

32 changes: 32 additions & 0 deletions stateproofs/stateProofVerifier.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package stateproofverification
almog-t marked this conversation as resolved.
Show resolved Hide resolved

import (
"github.com/algorand/go-stateproof-verification/stateproof"
"github.com/algorand/go-stateproof-verification/stateprooftypes"

"github.com/algorand/go-algorand-sdk/encoding/msgpack"
"github.com/algorand/go-algorand-sdk/types"
)

const strengthTarget = uint64(256)

type StateProofVerifier struct {
almog-t marked this conversation as resolved.
Show resolved Hide resolved
stateProofVerifier *stateproof.Verifier
}

func InitializeVerifier(votersCommitment types.GenericDigest, lnProvenWeight uint64) *StateProofVerifier {
return &StateProofVerifier{stateProofVerifier: stateproof.MkVerifierWithLnProvenWeight(stateprooftypes.GenericDigest(votersCommitment),
lnProvenWeight, strengthTarget)}
}

func (v *StateProofVerifier) VerifyStateProofMessage(stateProof *types.EncodedStateProof, message *types.Message) error {
almog-t marked this conversation as resolved.
Show resolved Hide resolved
messageHash := message.IntoStateProofMessageHash()

var decodedStateProof stateproof.StateProof
err := msgpack.Decode(*stateProof, &decodedStateProof)
if err != nil {
return err
}

return v.stateProofVerifier.Verify(message.LastAttestedRound, stateprooftypes.MessageHash(messageHash), &decodedStateProof)
}
46 changes: 46 additions & 0 deletions stateproofs/stateProofVerifier_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package stateproofverification

import (
"io/ioutil"
"testing"

"github.com/stretchr/testify/require"

"github.com/algorand/go-algorand-sdk/client/v2/common/models"
"github.com/algorand/go-algorand-sdk/encoding/json"
"github.com/algorand/go-algorand-sdk/types"
)

func readJsonFile(filePath string, target interface{}, assertions *require.Assertions) {
contents, err := ioutil.ReadFile(filePath)
assertions.NoError(err)

err = json.Decode(contents, &target)
assertions.NoError(err)
}

func TestStateProofVerification(t *testing.T) {
a := require.New(t)

prevStateProofFileName := "prevStateProof.json"
newStateProofFileName := "newStateProof.json"
almog-t marked this conversation as resolved.
Show resolved Hide resolved

var prevStateProof models.StateProof
var newStateProof models.StateProof

readJsonFile(prevStateProofFileName, &prevStateProof, a)
readJsonFile(newStateProofFileName, &newStateProof, a)

message := types.Message{
BlockHeadersCommitment: newStateProof.Message.Blockheaderscommitment,
VotersCommitment: newStateProof.Message.Voterscommitment,
LnProvenWeight: newStateProof.Message.Lnprovenweight,
FirstAttestedRound: newStateProof.Message.Firstattestedround,
LastAttestedRound: newStateProof.Message.Lastattestedround,
}
encodedStateProof := types.EncodedStateProof(newStateProof.Stateproof)

verifier := InitializeVerifier(prevStateProof.Message.Voterscommitment, prevStateProof.Message.Lnprovenweight)
err := verifier.VerifyStateProofMessage(&encodedStateProof, &message)
a.NoError(err)
}
37 changes: 37 additions & 0 deletions types/hashes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package types

import "hash"

// GenericDigest is a digest that implements CustomSizeDigest, and can be used as hash output.
//msgp:allocbound GenericDigest MaxHashDigestSize
type GenericDigest []byte

// HashID is a domain separation prefix for an object type that might be hashed
// This ensures, for example, the hash of a transaction will never collide with the hash of a vote
type HashID string

// Hashable is an interface implemented by an object that can be represented
// with a sequence of bytes to be hashed or signed, together with a type ID
// to distinguish different types of objects.
type Hashable interface {
almog-t marked this conversation as resolved.
Show resolved Hide resolved
ToBeHashed() (HashID, []byte)
}

// HashRep appends the correct hashid before the message to be hashed.
func HashRep(h Hashable) []byte {
hashid, data := h.ToBeHashed()
return append([]byte(hashid), data...)
}

// GenericHashObj Makes it easier to sum using hash interface and Hashable interface
func GenericHashObj(hsh hash.Hash, h Hashable) []byte {
rep := HashRep(h)
return hashBytes(hsh, rep)
}

func hashBytes(hash hash.Hash, m []byte) []byte {
hash.Reset()
hash.Write(m)
outhash := hash.Sum(nil)
return outhash
}
28 changes: 28 additions & 0 deletions types/lightBlockHeader.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package types

import (
"github.com/algorand/go-algorand-sdk/encoding/msgpack"
)

const BlockHeader256 HashID = "B256"

// A Seed contains cryptographic entropy which can be used to determine a
// committee.
type Seed [32]byte

// LightBlockHeader represents a minimal block header. It contains all the necessary fields
// for verifying proofs on transactions.
// In addition, this struct is designed to be used on environments where only SHA256 function exists
type LightBlockHeader struct {
_struct struct{} `codec:",omitempty,omitemptyarray"`

Seed Seed `codec:"0"`
RoundNumber Round `codec:"r"`
GenesisHash Digest `codec:"gh"`
Sha256TxnCommitment Digest `codec:"tc,allocbound=Sha256Size"`
}

// ToBeHashed implements the crypto.Hashable interface
almog-t marked this conversation as resolved.
Show resolved Hide resolved
func (bh LightBlockHeader) ToBeHashed() []byte {
return append([]byte(BlockHeader256), msgpack.Encode(&bh)...)
}
26 changes: 26 additions & 0 deletions types/stateproof.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,21 @@
package types

import (
"crypto/sha256"
"github.com/algorand/go-algorand-sdk/encoding/msgpack"
)

// EncodedStateProof represents the msgpack encoded state proof.
type EncodedStateProof []byte

// MessageHash represents the message that a state proof will attest to.
type MessageHash [32]byte

// StateProofType identifies a particular configuration of state proofs.
type StateProofType uint64

const StateProofMessage HashID = "spm"

// Message represents the message that the state proofs are attesting to. This message can be
// used by lightweight client and gives it the ability to verify proofs on the Algorand's state.
// In addition to that proof, this message also contains fields that
Expand All @@ -17,6 +30,19 @@ type Message struct {
LastAttestedRound uint64 `codec:"l"`
}

// ToBeHashed returns the bytes of the message.
func (m Message) ToBeHashed() (HashID, []byte) {
return StateProofMessage, msgpack.Encode(&m)
}

// IntoStateProofMessageHash returns a hashed representation fitting the state proof messages.
func (m Message) IntoStateProofMessageHash() MessageHash {
digest := GenericHashObj(sha256.New(), m)
result := MessageHash{}
copy(result[:], digest)
return result
}

// StateProofTxnFields captures the fields used for stateproof transactions.
type StateProofTxnFields struct {
_struct struct{} `codec:",omitempty,omitemptyarray"`
Expand Down