From 3799d366058d0478129a1f488981a0927c4a98b5 Mon Sep 17 00:00:00 2001 From: Mdaiki0730 Date: Fri, 25 Nov 2022 13:51:00 +0900 Subject: [PATCH 01/15] Add ProofLength to vrf interface --- crypto/vrf/README.md | 1 + crypto/vrf/vrf.go | 5 +++++ crypto/vrf/vrf_coniks.go | 6 ++++++ crypto/vrf/vrf_libsodium.go | 6 ++++++ crypto/vrf/vrf_r2ishiguro.go | 6 ++++++ 5 files changed, 24 insertions(+) diff --git a/crypto/vrf/README.md b/crypto/vrf/README.md index 6caaf6477..596d1d650 100644 --- a/crypto/vrf/README.md +++ b/crypto/vrf/README.md @@ -12,6 +12,7 @@ VRF implementation is set by `func init()` with `build` option type vrfEd25519 interface { Prove(privateKey []byte, message []byte) (Proof, error) Verify(publicKey []byte, proof Proof, message []byte) (bool, error) + ProofLength() int ProofToHash(proof Proof) (Output, error) ``` diff --git a/crypto/vrf/vrf.go b/crypto/vrf/vrf.go index 348ba897d..d84407772 100644 --- a/crypto/vrf/vrf.go +++ b/crypto/vrf/vrf.go @@ -16,6 +16,7 @@ type Output []byte type vrfEd25519 interface { Prove(privateKey []byte, message []byte) (Proof, error) Verify(publicKey []byte, proof Proof, message []byte) (bool, error) + ProofLength() int ProofToHash(proof Proof) (Output, error) } @@ -33,6 +34,10 @@ func Verify(publicKey []byte, proof Proof, message []byte) (bool, error) { return defaultVrf.Verify(publicKey, proof, message) } +func ProofLength() int { + return defaultVrf.ProofLength() +} + func ProofToHash(proof Proof) (Output, error) { return defaultVrf.ProofToHash(proof) } diff --git a/crypto/vrf/vrf_coniks.go b/crypto/vrf/vrf_coniks.go index bb6126b91..2cfb95acc 100644 --- a/crypto/vrf/vrf_coniks.go +++ b/crypto/vrf/vrf_coniks.go @@ -19,6 +19,8 @@ func init() { defaultVrf = newVrfEd25519coniks() } +const ConiksProofLength = 96 + func newVrfEd25519coniks() *vrfEd25519coniks { return &vrfEd25519coniks{nil, nil} } @@ -54,6 +56,10 @@ func (base *vrfEd25519coniks) Verify(publicKey []byte, proof Proof, message []by return coniksPubKey.Verify(message, base.generatedHash, proof), nil } +func (base *vrfEd25519coniks) ProofLength() int { + return ConiksProofLength +} + func (base *vrfEd25519coniks) ProofToHash(proof Proof) (Output, error) { if base.generatedHash == nil { return nil, errors.New("vrf hash was not given") diff --git a/crypto/vrf/vrf_libsodium.go b/crypto/vrf/vrf_libsodium.go index 033ca496f..1249ed60b 100644 --- a/crypto/vrf/vrf_libsodium.go +++ b/crypto/vrf/vrf_libsodium.go @@ -18,6 +18,8 @@ func init() { defaultVrf = newVrfEd25519libsodium() } +const LibsodiumProofLength = 80 + func newVrfEd25519libsodium() vrfEd25519libsodium { return vrfEd25519libsodium{} } @@ -46,6 +48,10 @@ func (base vrfEd25519libsodium) Verify(publicKey []byte, proof Proof, message [] return bytes.Compare(op[:], hash) == 0, nil } +func (base vrfEd25519libsodium) ProofLength() int { + return LibsodiumProofLength +} + func (base vrfEd25519libsodium) ProofToHash(proof Proof) (Output, error) { op, err := libsodium.ProofToHash(toArray(proof)) if err != nil { diff --git a/crypto/vrf/vrf_r2ishiguro.go b/crypto/vrf/vrf_r2ishiguro.go index 07b9b2c61..541ab1edc 100644 --- a/crypto/vrf/vrf_r2ishiguro.go +++ b/crypto/vrf/vrf_r2ishiguro.go @@ -19,6 +19,8 @@ func init() { } } +const R2ishiguroProofLength = 81 + func newVrfEd25519r2ishiguro() vrfEd25519r2ishiguro { return vrfEd25519r2ishiguro{} } @@ -32,6 +34,10 @@ func (base vrfEd25519r2ishiguro) Verify(publicKey []byte, proof Proof, message [ return r2ishiguro.ECVRF_verify(publicKey, proof, message) } +func (base vrfEd25519r2ishiguro) ProofLength() int { + return R2ishiguroProofLength +} + func (base vrfEd25519r2ishiguro) ProofToHash(proof Proof) (Output, error) { // validate proof with ECVRF_decode_proof _, _, _, err := r2ishiguro.ECVRF_decode_proof(proof) From 3aa3e47a008bfc809ff329bdda2a6a037dd5fa01 Mon Sep 17 00:00:00 2001 From: Mdaiki0730 Date: Fri, 25 Nov 2022 14:01:05 +0900 Subject: [PATCH 02/15] Add validation of the ValidatorsHash, Round and Proof --- types/block.go | 10 ++++++++++ types/validation.go | 13 +++++++++++++ 2 files changed, 23 insertions(+) diff --git a/types/block.go b/types/block.go index 58824b3c5..d881bd491 100644 --- a/types/block.go +++ b/types/block.go @@ -423,6 +423,9 @@ func (h Header) ValidateBasic() error { if len(h.ChainID) > MaxChainIDLen { return fmt.Errorf("chainID is too long; got: %d, max: %d", len(h.ChainID), MaxChainIDLen) } + if h.Round < 0 { + return errors.New("negative Round") + } if h.Height < 0 { return errors.New("negative Height") @@ -453,8 +456,15 @@ func (h Header) ValidateBasic() error { ) } + if err := ValidateProof(h.Proof); err != nil { + return fmt.Errorf("wrong Proof: %v", err) + } + // Basic validation of hashes related to application data. // Will validate fully against state in state#ValidateBlock. + if err := ValidateHash(h.ValidatorsHash); err != nil { + return fmt.Errorf("wrong ValidatorsHash: %v", err) + } if err := ValidateHash(h.VotersHash); err != nil { return fmt.Errorf("wrong VotersHash: %v", err) } diff --git a/types/validation.go b/types/validation.go index bb086ce79..36477400a 100644 --- a/types/validation.go +++ b/types/validation.go @@ -5,6 +5,7 @@ import ( "time" "github.com/line/ostracon/crypto/tmhash" + "github.com/line/ostracon/crypto/vrf" tmtime "github.com/line/ostracon/types/time" ) @@ -38,3 +39,15 @@ func ValidateHash(h []byte) error { } return nil } + +// ValidateProof returns an error if the proof is not empty, but its +// size != vrf.ProofLength(). +func ValidateProof(h []byte) error { + if len(h) > 0 && len(h) != vrf.ProofLength() { + return fmt.Errorf("expected size to be %d bytes, got %d bytes", + vrf.ProofLength(), + len(h), + ) + } + return nil +} From 9f098174341381aa72bbf119b6dc633ccd9aa26a Mon Sep 17 00:00:00 2001 From: Mdaiki0730 Date: Fri, 25 Nov 2022 14:07:22 +0900 Subject: [PATCH 03/15] Add test case of the ValidatorsHash, Round and Proof --- types/block_test.go | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/types/block_test.go b/types/block_test.go index 7493d5397..e105047af 100644 --- a/types/block_test.go +++ b/types/block_test.go @@ -638,6 +638,15 @@ func TestHeaderValidateBasic(t *testing.T) { {"Invalid Results Hash", func(header *Header) { header.LastResultsHash = []byte(strings.Repeat("h", invalidHashLength)) }, true}, + {"Negative Round", func(header *Header) { + header.Round = -1 + }, true}, + {"Invalid Proof", func(header *Header) { + header.Proof = make([]byte, vrf.ProofLength()-1) + }, true}, + {"Invalid Validators Hash", func(header *Header) { + header.ValidatorsHash = []byte(strings.Repeat("h", invalidHashLength)) + }, true}, } for i, tc := range testCases { tc := tc @@ -660,7 +669,7 @@ func TestHeaderValidateBasic(t *testing.T) { EvidenceHash: tmhash.Sum([]byte("evidence_hash")), ProposerAddress: crypto.AddressHash([]byte("proposer_address")), Round: 1, - Proof: tmhash.Sum([]byte("proof")), + Proof: make([]byte, vrf.ProofLength()), } tc.malleateHeader(header) err := header.ValidateBasic() From 556224ec2f7fb72d967abdaf8d72a36a6fa735ce Mon Sep 17 00:00:00 2001 From: Mdaiki0730 Date: Fri, 25 Nov 2022 15:09:10 +0900 Subject: [PATCH 04/15] Fix makeRandHeader to generate rand proof --- types/block_test.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/types/block_test.go b/types/block_test.go index e105047af..e0163a929 100644 --- a/types/block_test.go +++ b/types/block_test.go @@ -1277,6 +1277,7 @@ func makeRandHeader() Header { height := tmrand.Int63() randBytes := tmrand.Bytes(tmhash.Size) randAddress := tmrand.Bytes(crypto.AddressSize) + randProof := tmrand.Bytes(vrf.ProofLength()) h := Header{ Version: tmversion.Consensus{Block: version.BlockProtocol, App: 1}, ChainID: chainID, @@ -1294,6 +1295,7 @@ func makeRandHeader() Header { EvidenceHash: randBytes, ProposerAddress: randAddress, + Proof: randProof, } return h From 75fb3c3be40c22165155019e9cf32e10d3f0cd7e Mon Sep 17 00:00:00 2001 From: Mdaiki0730 Date: Fri, 25 Nov 2022 17:40:59 +0900 Subject: [PATCH 05/15] Make libsodium compatible with M1 Mac --- crypto/vrf/internal/vrf/vrf.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/crypto/vrf/internal/vrf/vrf.go b/crypto/vrf/internal/vrf/vrf.go index aebbde992..48c6694cd 100644 --- a/crypto/vrf/internal/vrf/vrf.go +++ b/crypto/vrf/internal/vrf/vrf.go @@ -8,6 +8,8 @@ package vrf #cgo CFLAGS: -Wall -std=c99 #cgo darwin,amd64 CFLAGS: -I./sodium/darwin_amd64/include/ #cgo darwin,amd64 LDFLAGS: -L./sodium/darwin_amd64/lib -lsodium +#cgo darwin,arm64 CFLAGS: -I./sodium/darwin_arm64/include/ +#cgo darwin,arm64 LDFLAGS: -L./sodium/darwin_arm64/lib -lsodium #cgo linux,amd64 CFLAGS: -I./sodium/linux_amd64/include #cgo linux,amd64 LDFLAGS: -L./sodium/linux_amd64/lib -lsodium #cgo linux,arm64 CFLAGS: -I./sodium/linux_arm64/include From b322fdffd7623bfad6337f26c4366cd9ba2cdcd2 Mon Sep 17 00:00:00 2001 From: Mdaiki0730 Date: Fri, 25 Nov 2022 19:37:23 +0900 Subject: [PATCH 06/15] Fix to run test with correct Header.Proof --- evidence/pool_test.go | 13 +++++++------ evidence/verify_test.go | 5 ++--- types/block_test.go | 2 ++ 3 files changed, 11 insertions(+), 9 deletions(-) diff --git a/evidence/pool_test.go b/evidence/pool_test.go index 8f696a640..38b0b8f86 100644 --- a/evidence/pool_test.go +++ b/evidence/pool_test.go @@ -6,6 +6,7 @@ import ( "testing" "time" + "github.com/line/ostracon/crypto" "github.com/line/ostracon/crypto/bls" "github.com/line/ostracon/crypto/composite" "github.com/line/ostracon/crypto/ed25519" @@ -325,12 +326,11 @@ func TestLightClientAttackEvidenceLifecycle(t *testing.T) { func TestRecoverPendingEvidence(t *testing.T) { height := int64(10) val := types.NewMockPV(types.PrivKeyComposite) // TODO 🏺 need to test by all key types - valAddress := val.PrivKey.PubKey().Address() evidenceDB := dbm.NewMemDB() stateStore := initializeValidatorState(val, height) state, err := stateStore.Load() require.NoError(t, err) - blockStore := initializeBlockStore(dbm.NewMemDB(), state, valAddress) + blockStore := initializeBlockStore(dbm.NewMemDB(), state, val.PrivKey) // create previous pool and populate it pool, err := evidence.NewPool(evidenceDB, stateStore, blockStore) require.NoError(t, err) @@ -424,13 +424,15 @@ func initializeValidatorState(privVal types.PrivValidator, height int64) sm.Stor // initializeBlockStore creates a block storage and populates it w/ a dummy // block at +height+. -func initializeBlockStore(db dbm.DB, state sm.State, valAddr []byte) *store.BlockStore { +func initializeBlockStore(db dbm.DB, state sm.State, valPrivkey crypto.PrivKey) *store.BlockStore { blockStore := store.NewBlockStore(db) + valAddr := valPrivkey.PubKey().Address() for i := int64(1); i <= state.LastBlockHeight; i++ { round := int32(0) lastCommit := makeCommit(i-1, valAddr) - proof := state.MakeHashMessage(round) + message := state.MakeHashMessage(round) + proof, _ := valPrivkey.VRFProve(message) block, _ := state.MakeBlock(i, []types.Tx{}, lastCommit, nil, state.Validators.SelectProposer(proof, i, round).Address, round, proof) block.Header.Time = defaultEvidenceTime.Add(time.Duration(i) * time.Minute) @@ -457,11 +459,10 @@ func makeCommit(height int64, valAddr []byte) *types.Commit { func defaultTestPool(height int64) (*evidence.Pool, types.MockPV) { val := types.NewMockPV(types.PrivKeyComposite) // TODO 🏺 need to test by all key types - valAddress := val.PrivKey.PubKey().Address() evidenceDB := dbm.NewMemDB() stateStore := initializeValidatorState(val, height) state, _ := stateStore.Load() - blockStore := initializeBlockStore(dbm.NewMemDB(), state, valAddress) + blockStore := initializeBlockStore(dbm.NewMemDB(), state, val.PrivKey) pool, err := evidence.NewPool(evidenceDB, stateStore, blockStore) if err != nil { panic("test evidence pool could not be created") diff --git a/evidence/verify_test.go b/evidence/verify_test.go index d8a3b5fbc..44d81a3e2 100644 --- a/evidence/verify_test.go +++ b/evidence/verify_test.go @@ -8,8 +8,6 @@ import ( "github.com/line/ostracon/light" - "github.com/coniks-sys/coniks-go/crypto/vrf" - "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -17,6 +15,7 @@ import ( "github.com/line/ostracon/crypto" "github.com/line/ostracon/crypto/tmhash" + "github.com/line/ostracon/crypto/vrf" "github.com/line/ostracon/evidence" "github.com/line/ostracon/evidence/mocks" "github.com/line/ostracon/libs/log" @@ -735,7 +734,7 @@ func makeHeaderRandom(height int64) *types.Header { LastResultsHash: crypto.CRandBytes(tmhash.Size), EvidenceHash: crypto.CRandBytes(tmhash.Size), ProposerAddress: crypto.CRandBytes(crypto.AddressSize), - Proof: crypto.CRandBytes(vrf.ProofSize), + Proof: crypto.CRandBytes(vrf.ProofLength()), } } diff --git a/types/block_test.go b/types/block_test.go index e0163a929..b392439a2 100644 --- a/types/block_test.go +++ b/types/block_test.go @@ -495,6 +495,8 @@ func TestCommitHash(t *testing.T) { }) } +// The Proof defined here does not depend on the vrf ProofLength, +// but it is a fixed value for the purpose of calculating the Hash value. func TestHeaderHash(t *testing.T) { testCases := []struct { desc string From 409c903c6ef952af0089a36c9b8dbef54bba8027 Mon Sep 17 00:00:00 2001 From: Mdaiki0730 Date: Mon, 28 Nov 2022 10:03:22 +0900 Subject: [PATCH 07/15] Resolve e2e test failure due to build tag diff --- test/e2e/Makefile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/e2e/Makefile b/test/e2e/Makefile index dc9f2f30a..aafb91839 100644 --- a/test/e2e/Makefile +++ b/test/e2e/Makefile @@ -15,9 +15,9 @@ maverick: go build -o build/maverick -tags libsodium,badgerdb,boltdb,cleveldb,rocksdb ../maverick generator: - go build -o build/generator ./generator + go build -o build/generator -tags libsodium ./generator runner: - go build -o build/runner ./runner + go build -o build/runner -tags libsodium ./runner .PHONY: all node docker generator maverick runner From b44ab05d13b82d6fcc01abd1ddfa10f89d332f2b Mon Sep 17 00:00:00 2001 From: Mdaiki0730 Date: Mon, 28 Nov 2022 10:56:54 +0900 Subject: [PATCH 08/15] Change ProofLength() to const ProofSize --- crypto/vrf/README.md | 4 +++- crypto/vrf/vrf.go | 5 ----- crypto/vrf/vrf_coniks.go | 6 +----- crypto/vrf/vrf_libsodium.go | 6 +----- crypto/vrf/vrf_r2ishiguro.go | 6 +----- evidence/verify_test.go | 2 +- types/block_test.go | 6 +++--- types/validation.go | 6 +++--- 8 files changed, 13 insertions(+), 28 deletions(-) diff --git a/crypto/vrf/README.md b/crypto/vrf/README.md index 596d1d650..19f47c147 100644 --- a/crypto/vrf/README.md +++ b/crypto/vrf/README.md @@ -12,7 +12,6 @@ VRF implementation is set by `func init()` with `build` option type vrfEd25519 interface { Prove(privateKey []byte, message []byte) (Proof, error) Verify(publicKey []byte, proof Proof, message []byte) (bool, error) - ProofLength() int ProofToHash(proof Proof) (Output, error) ``` @@ -26,17 +25,20 @@ Use `func init()` with `build` option * `//go:build libsodium` * `// +build !libsodium,!coniks` * `func init() { defaultVrf = newVrfEd25519r2ishiguro() }` + * `const ProofSize = 81` * vrf_r2ishiguro.go * (coniks) * `//go:build coniks` * `// +build coniks` * `func init() { defaultVrf = newVrfEd25519coniks() }` + * `const ProofSize = 96` * vrf_coniks.go * vrf_coniks_test.go * (libsodium) * `//go:build libsodium` * `// +build libsodium` * `func init() { defaultVrf = newVrfEd25519libsodium() }` + * `const ProofSize = int(libsodium.PROOFBYTES)` * vrf_libsodium.go * vrf_libsodium_test.go diff --git a/crypto/vrf/vrf.go b/crypto/vrf/vrf.go index d84407772..348ba897d 100644 --- a/crypto/vrf/vrf.go +++ b/crypto/vrf/vrf.go @@ -16,7 +16,6 @@ type Output []byte type vrfEd25519 interface { Prove(privateKey []byte, message []byte) (Proof, error) Verify(publicKey []byte, proof Proof, message []byte) (bool, error) - ProofLength() int ProofToHash(proof Proof) (Output, error) } @@ -34,10 +33,6 @@ func Verify(publicKey []byte, proof Proof, message []byte) (bool, error) { return defaultVrf.Verify(publicKey, proof, message) } -func ProofLength() int { - return defaultVrf.ProofLength() -} - func ProofToHash(proof Proof) (Output, error) { return defaultVrf.ProofToHash(proof) } diff --git a/crypto/vrf/vrf_coniks.go b/crypto/vrf/vrf_coniks.go index 2cfb95acc..bbc7b33d5 100644 --- a/crypto/vrf/vrf_coniks.go +++ b/crypto/vrf/vrf_coniks.go @@ -19,7 +19,7 @@ func init() { defaultVrf = newVrfEd25519coniks() } -const ConiksProofLength = 96 +const ProofSize = 96 func newVrfEd25519coniks() *vrfEd25519coniks { return &vrfEd25519coniks{nil, nil} @@ -56,10 +56,6 @@ func (base *vrfEd25519coniks) Verify(publicKey []byte, proof Proof, message []by return coniksPubKey.Verify(message, base.generatedHash, proof), nil } -func (base *vrfEd25519coniks) ProofLength() int { - return ConiksProofLength -} - func (base *vrfEd25519coniks) ProofToHash(proof Proof) (Output, error) { if base.generatedHash == nil { return nil, errors.New("vrf hash was not given") diff --git a/crypto/vrf/vrf_libsodium.go b/crypto/vrf/vrf_libsodium.go index 1249ed60b..babab44f8 100644 --- a/crypto/vrf/vrf_libsodium.go +++ b/crypto/vrf/vrf_libsodium.go @@ -18,7 +18,7 @@ func init() { defaultVrf = newVrfEd25519libsodium() } -const LibsodiumProofLength = 80 +const ProofSize = int(libsodium.PROOFBYTES) func newVrfEd25519libsodium() vrfEd25519libsodium { return vrfEd25519libsodium{} @@ -48,10 +48,6 @@ func (base vrfEd25519libsodium) Verify(publicKey []byte, proof Proof, message [] return bytes.Compare(op[:], hash) == 0, nil } -func (base vrfEd25519libsodium) ProofLength() int { - return LibsodiumProofLength -} - func (base vrfEd25519libsodium) ProofToHash(proof Proof) (Output, error) { op, err := libsodium.ProofToHash(toArray(proof)) if err != nil { diff --git a/crypto/vrf/vrf_r2ishiguro.go b/crypto/vrf/vrf_r2ishiguro.go index 541ab1edc..b89337a17 100644 --- a/crypto/vrf/vrf_r2ishiguro.go +++ b/crypto/vrf/vrf_r2ishiguro.go @@ -19,7 +19,7 @@ func init() { } } -const R2ishiguroProofLength = 81 +const ProofSize = 81 func newVrfEd25519r2ishiguro() vrfEd25519r2ishiguro { return vrfEd25519r2ishiguro{} @@ -34,10 +34,6 @@ func (base vrfEd25519r2ishiguro) Verify(publicKey []byte, proof Proof, message [ return r2ishiguro.ECVRF_verify(publicKey, proof, message) } -func (base vrfEd25519r2ishiguro) ProofLength() int { - return R2ishiguroProofLength -} - func (base vrfEd25519r2ishiguro) ProofToHash(proof Proof) (Output, error) { // validate proof with ECVRF_decode_proof _, _, _, err := r2ishiguro.ECVRF_decode_proof(proof) diff --git a/evidence/verify_test.go b/evidence/verify_test.go index 44d81a3e2..573b3b637 100644 --- a/evidence/verify_test.go +++ b/evidence/verify_test.go @@ -734,7 +734,7 @@ func makeHeaderRandom(height int64) *types.Header { LastResultsHash: crypto.CRandBytes(tmhash.Size), EvidenceHash: crypto.CRandBytes(tmhash.Size), ProposerAddress: crypto.CRandBytes(crypto.AddressSize), - Proof: crypto.CRandBytes(vrf.ProofLength()), + Proof: crypto.CRandBytes(vrf.ProofSize), } } diff --git a/types/block_test.go b/types/block_test.go index b392439a2..b743fde26 100644 --- a/types/block_test.go +++ b/types/block_test.go @@ -644,7 +644,7 @@ func TestHeaderValidateBasic(t *testing.T) { header.Round = -1 }, true}, {"Invalid Proof", func(header *Header) { - header.Proof = make([]byte, vrf.ProofLength()-1) + header.Proof = make([]byte, vrf.ProofSize-1) }, true}, {"Invalid Validators Hash", func(header *Header) { header.ValidatorsHash = []byte(strings.Repeat("h", invalidHashLength)) @@ -671,7 +671,7 @@ func TestHeaderValidateBasic(t *testing.T) { EvidenceHash: tmhash.Sum([]byte("evidence_hash")), ProposerAddress: crypto.AddressHash([]byte("proposer_address")), Round: 1, - Proof: make([]byte, vrf.ProofLength()), + Proof: make([]byte, vrf.ProofSize), } tc.malleateHeader(header) err := header.ValidateBasic() @@ -1279,7 +1279,7 @@ func makeRandHeader() Header { height := tmrand.Int63() randBytes := tmrand.Bytes(tmhash.Size) randAddress := tmrand.Bytes(crypto.AddressSize) - randProof := tmrand.Bytes(vrf.ProofLength()) + randProof := tmrand.Bytes(vrf.ProofSize) h := Header{ Version: tmversion.Consensus{Block: version.BlockProtocol, App: 1}, ChainID: chainID, diff --git a/types/validation.go b/types/validation.go index 36477400a..4eae08e37 100644 --- a/types/validation.go +++ b/types/validation.go @@ -41,11 +41,11 @@ func ValidateHash(h []byte) error { } // ValidateProof returns an error if the proof is not empty, but its -// size != vrf.ProofLength(). +// size != vrf.ProofSize. func ValidateProof(h []byte) error { - if len(h) > 0 && len(h) != vrf.ProofLength() { + if len(h) > 0 && len(h) != vrf.ProofSize { return fmt.Errorf("expected size to be %d bytes, got %d bytes", - vrf.ProofLength(), + vrf.ProofSize, len(h), ) } From 7730830ae838a9cb8a07ea9e188adc071b30ef7c Mon Sep 17 00:00:00 2001 From: Mdaiki0730 Date: Mon, 28 Nov 2022 12:15:39 +0900 Subject: [PATCH 09/15] Separate the diff with tendermint down into the code --- types/block.go | 23 ++++++++++++----------- types/block_test.go | 10 ++++++---- 2 files changed, 18 insertions(+), 15 deletions(-) diff --git a/types/block.go b/types/block.go index d881bd491..6eff57dd7 100644 --- a/types/block.go +++ b/types/block.go @@ -423,9 +423,6 @@ func (h Header) ValidateBasic() error { if len(h.ChainID) > MaxChainIDLen { return fmt.Errorf("chainID is too long; got: %d, max: %d", len(h.ChainID), MaxChainIDLen) } - if h.Round < 0 { - return errors.New("negative Round") - } if h.Height < 0 { return errors.New("negative Height") @@ -456,19 +453,11 @@ func (h Header) ValidateBasic() error { ) } - if err := ValidateProof(h.Proof); err != nil { - return fmt.Errorf("wrong Proof: %v", err) - } - // Basic validation of hashes related to application data. // Will validate fully against state in state#ValidateBlock. if err := ValidateHash(h.ValidatorsHash); err != nil { return fmt.Errorf("wrong ValidatorsHash: %v", err) } - if err := ValidateHash(h.VotersHash); err != nil { - return fmt.Errorf("wrong VotersHash: %v", err) - } - // TODO When we add `Header.ValidatorsHash` in a future commit, we have to add a similar check here. if err := ValidateHash(h.NextValidatorsHash); err != nil { return fmt.Errorf("wrong NextValidatorsHash: %v", err) } @@ -480,6 +469,18 @@ func (h Header) ValidateBasic() error { return fmt.Errorf("wrong LastResultsHash: %v", err) } + if err := ValidateHash(h.VotersHash); err != nil { + return fmt.Errorf("wrong VotersHash: %v", err) + } + + if h.Round < 0 { + return errors.New("negative Round") + } + + if err := ValidateProof(h.Proof); err != nil { + return fmt.Errorf("wrong Proof: %v", err) + } + return nil } diff --git a/types/block_test.go b/types/block_test.go index b743fde26..41e2305fe 100644 --- a/types/block_test.go +++ b/types/block_test.go @@ -495,8 +495,6 @@ func TestCommitHash(t *testing.T) { }) } -// The Proof defined here does not depend on the vrf ProofLength, -// but it is a fixed value for the purpose of calculating the Hash value. func TestHeaderHash(t *testing.T) { testCases := []struct { desc string @@ -520,7 +518,9 @@ func TestHeaderHash(t *testing.T) { EvidenceHash: tmhash.Sum([]byte("evidence_hash")), ProposerAddress: crypto.AddressHash([]byte("proposer_address")), Round: 1, - Proof: tmhash.Sum([]byte("proof")), + // The Proof defined here does not depend on the vrf ProofLength, + // but it is a fixed value for the purpose of calculating the Hash value. + Proof: tmhash.Sum([]byte("proof")), }, hexBytesFromString("0368E6F15B6B7BC9DC5B10F36F37D6F867E132A22333F083A11290324274E183")}, {"nil header yields nil", nil, nil}, {"nil VotersHash yields nil", &Header{ @@ -539,7 +539,9 @@ func TestHeaderHash(t *testing.T) { EvidenceHash: tmhash.Sum([]byte("evidence_hash")), ProposerAddress: crypto.AddressHash([]byte("proposer_address")), Round: 1, - Proof: tmhash.Sum([]byte("proof")), + // The Proof defined here does not depend on the vrf ProofLength, + // but it is a fixed value for the purpose of calculating the Hash value. + Proof: tmhash.Sum([]byte("proof")), }, nil}, } for _, tc := range testCases { From d9dc011554ed996f22a707e7893506b236e85024 Mon Sep 17 00:00:00 2001 From: Mdaiki0730 Date: Mon, 28 Nov 2022 14:58:12 +0900 Subject: [PATCH 10/15] Change e2e test build from libsodium to r2ishiguro --- test/e2e/Makefile | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test/e2e/Makefile b/test/e2e/Makefile index aafb91839..16994bf16 100644 --- a/test/e2e/Makefile +++ b/test/e2e/Makefile @@ -7,17 +7,17 @@ docker: # order to build a binary with an Ostracon node in it (for built-in # ABCI testing). node: - go build -o build/node -tags libsodium,badgerdb,boltdb,cleveldb,rocksdb ./node + go build -o build/node -tags badgerdb,boltdb,cleveldb,rocksdb ./node # To be used primarily by the e2e docker instance. If you want to produce this binary # elsewhere, then run go build in the maverick directory. maverick: - go build -o build/maverick -tags libsodium,badgerdb,boltdb,cleveldb,rocksdb ../maverick + go build -o build/maverick -tags badgerdb,boltdb,cleveldb,rocksdb ../maverick generator: - go build -o build/generator -tags libsodium ./generator + go build -o build/generator ./generator runner: - go build -o build/runner -tags libsodium ./runner + go build -o build/runner ./runner .PHONY: all node docker generator maverick runner From 9ea2532dee9722b5a47b885078edba1603d2e123 Mon Sep 17 00:00:00 2001 From: Mdaiki0730 Date: Tue, 29 Nov 2022 11:47:11 +0900 Subject: [PATCH 11/15] Revert e2e test build and Change runner build from default to libsodium --- .github/workflows/e2e.yml | 7 +++++++ test/e2e/Makefile | 8 ++++---- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/.github/workflows/e2e.yml b/.github/workflows/e2e.yml index 4fc7b5bcf..cd06ae218 100644 --- a/.github/workflows/e2e.yml +++ b/.github/workflows/e2e.yml @@ -122,6 +122,13 @@ jobs: docker load -i ${{ needs.e2e-build.outputs.CACHE_FILE }} if: "env.GIT_DIFF != ''" + - name: Build libsodium + - run: make libsodium + - uses: actions/upload-artifact@v3 + with: + name: libsodium + path: crypto/vrf/internal/vrf/sodium + - name: Build e2e runner working-directory: test/e2e run: make runner diff --git a/test/e2e/Makefile b/test/e2e/Makefile index 16994bf16..aafb91839 100644 --- a/test/e2e/Makefile +++ b/test/e2e/Makefile @@ -7,17 +7,17 @@ docker: # order to build a binary with an Ostracon node in it (for built-in # ABCI testing). node: - go build -o build/node -tags badgerdb,boltdb,cleveldb,rocksdb ./node + go build -o build/node -tags libsodium,badgerdb,boltdb,cleveldb,rocksdb ./node # To be used primarily by the e2e docker instance. If you want to produce this binary # elsewhere, then run go build in the maverick directory. maverick: - go build -o build/maverick -tags badgerdb,boltdb,cleveldb,rocksdb ../maverick + go build -o build/maverick -tags libsodium,badgerdb,boltdb,cleveldb,rocksdb ../maverick generator: - go build -o build/generator ./generator + go build -o build/generator -tags libsodium ./generator runner: - go build -o build/runner ./runner + go build -o build/runner -tags libsodium ./runner .PHONY: all node docker generator maverick runner From 2aaf21b293b0ae9f418eb9f87848908b6d3a4af1 Mon Sep 17 00:00:00 2001 From: Mdaiki0730 Date: Tue, 29 Nov 2022 11:53:07 +0900 Subject: [PATCH 12/15] Fix e2e test actions bug --- .github/workflows/e2e.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.github/workflows/e2e.yml b/.github/workflows/e2e.yml index cd06ae218..d10e04f4a 100644 --- a/.github/workflows/e2e.yml +++ b/.github/workflows/e2e.yml @@ -123,7 +123,8 @@ jobs: if: "env.GIT_DIFF != ''" - name: Build libsodium - - run: make libsodium + run: make libsodium + - uses: actions/upload-artifact@v3 with: name: libsodium From 54ad8238d8978ef391b8bfb55221e659d98126ba Mon Sep 17 00:00:00 2001 From: Mdaiki0730 Date: Tue, 29 Nov 2022 12:18:53 +0900 Subject: [PATCH 13/15] Add build tag option to test proccess --- test/e2e/runner/test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/e2e/runner/test.go b/test/e2e/runner/test.go index 0efd80d64..2b8de16af 100644 --- a/test/e2e/runner/test.go +++ b/test/e2e/runner/test.go @@ -15,5 +15,5 @@ func Test(testnet *e2e.Testnet) error { return err } - return execVerbose("go", "test", "-count", "1", "./tests/...") + return execVerbose("go", "test", "-count", "1", "-tags", "libsodium", "./tests/...") } From 42accef2c0d937cd321c3407b3e7f5eca5018a92 Mon Sep 17 00:00:00 2001 From: Mdaiki0730 Date: Tue, 29 Nov 2022 14:13:35 +0900 Subject: [PATCH 14/15] Use ProofSize from coniks --- crypto/vrf/vrf_coniks.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crypto/vrf/vrf_coniks.go b/crypto/vrf/vrf_coniks.go index bbc7b33d5..1d29a8015 100644 --- a/crypto/vrf/vrf_coniks.go +++ b/crypto/vrf/vrf_coniks.go @@ -19,7 +19,7 @@ func init() { defaultVrf = newVrfEd25519coniks() } -const ProofSize = 96 +const ProofSize = coniks.ProofSize func newVrfEd25519coniks() *vrfEd25519coniks { return &vrfEd25519coniks{nil, nil} From 14d66a5e76bf776a0813d2bc44249a13aa96594a Mon Sep 17 00:00:00 2001 From: Mdaiki0730 Date: Tue, 29 Nov 2022 19:03:17 +0900 Subject: [PATCH 15/15] Remove unnecessary upload --- .github/workflows/e2e.yml | 5 ----- 1 file changed, 5 deletions(-) diff --git a/.github/workflows/e2e.yml b/.github/workflows/e2e.yml index d10e04f4a..47b323963 100644 --- a/.github/workflows/e2e.yml +++ b/.github/workflows/e2e.yml @@ -125,11 +125,6 @@ jobs: - name: Build libsodium run: make libsodium - - uses: actions/upload-artifact@v3 - with: - name: libsodium - path: crypto/vrf/internal/vrf/sodium - - name: Build e2e runner working-directory: test/e2e run: make runner