diff options
author | Jimmy Hu <jimmy.hu@dexon.org> | 2019-04-11 16:45:38 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-04-11 16:45:38 +0800 |
commit | 5b0aad05d7ccc1dabedfd1f3bfc0d584db849e63 (patch) | |
tree | bdbecd8695ed5225aa24f56b6a3c0acc7244f68f /core/utils | |
parent | 269fed574986331e07bf931b2c9b1a495c40f8ac (diff) | |
download | tangerine-consensus-5b0aad05d7ccc1dabedfd1f3bfc0d584db849e63.tar tangerine-consensus-5b0aad05d7ccc1dabedfd1f3bfc0d584db849e63.tar.gz tangerine-consensus-5b0aad05d7ccc1dabedfd1f3bfc0d584db849e63.tar.bz2 tangerine-consensus-5b0aad05d7ccc1dabedfd1f3bfc0d584db849e63.tar.lz tangerine-consensus-5b0aad05d7ccc1dabedfd1f3bfc0d584db849e63.tar.xz tangerine-consensus-5b0aad05d7ccc1dabedfd1f3bfc0d584db849e63.tar.zst tangerine-consensus-5b0aad05d7ccc1dabedfd1f3bfc0d584db849e63.zip |
core: change CRSSignature with bls (#563)
Diffstat (limited to 'core/utils')
-rw-r--r-- | core/utils/crypto.go | 23 | ||||
-rw-r--r-- | core/utils/crypto_test.go | 11 | ||||
-rw-r--r-- | core/utils/signer.go | 23 | ||||
-rw-r--r-- | core/utils/signer_test.go | 6 |
4 files changed, 45 insertions, 18 deletions
diff --git a/core/utils/crypto.go b/core/utils/crypto.go index 7fd3a77..496944d 100644 --- a/core/utils/crypto.go +++ b/core/utils/crypto.go @@ -18,6 +18,7 @@ package utils import ( + "bytes" "encoding/binary" "github.com/dexon-foundation/dexon-consensus/common" @@ -122,21 +123,27 @@ func VerifyVoteSignature(vote *types.Vote) (bool, error) { func hashCRS(block *types.Block, crs common.Hash) common.Hash { hashPos := HashPosition(block.Position) + if block.Position.Round < dkgDelayRound { + return crypto.Keccak256Hash(crs[:], hashPos[:], block.ProposerID.Hash[:]) + } return crypto.Keccak256Hash(crs[:], hashPos[:]) } // VerifyCRSSignature verifies the CRS signature of types.Block. -func VerifyCRSSignature(block *types.Block, crs common.Hash) ( - bool, error) { +func VerifyCRSSignature( + block *types.Block, crs common.Hash, npks *typesDKG.NodePublicKeys) bool { hash := hashCRS(block, crs) - pubKey, err := crypto.SigToPub(hash, block.CRSSignature) - if err != nil { - return false, err + if block.Position.Round < dkgDelayRound { + return bytes.Compare(block.CRSSignature.Signature[:], hash[:]) == 0 } - if block.ProposerID != types.NewNodeID(pubKey) { - return false, nil + if npks == nil { + return false } - return true, nil + pubKey, exist := npks.PublicKeys[block.ProposerID] + if !exist { + return false + } + return pubKey.VerifySignature(hash, block.CRSSignature) } // HashPosition generates hash of a types.Position. diff --git a/core/utils/crypto_test.go b/core/utils/crypto_test.go index 5dfd82b..24ea68e 100644 --- a/core/utils/crypto_test.go +++ b/core/utils/crypto_test.go @@ -127,6 +127,7 @@ func (s *CryptoTestSuite) TestVoteSignature() { } func (s *CryptoTestSuite) TestCRSSignature() { + dkgDelayRound = 1 crs := common.NewRandomHash() prv, err := ecdsa.NewPrivateKey() s.Require().NoError(err) @@ -135,14 +136,12 @@ func (s *CryptoTestSuite) TestCRSSignature() { block := &types.Block{ ProposerID: nID, } - block.CRSSignature, err = prv.Sign(hashCRS(block, crs)) - s.Require().NoError(err) - ok, err := VerifyCRSSignature(block, crs) - s.Require().NoError(err) + hash := hashCRS(block, crs) + block.CRSSignature.Signature = hash[:] + ok := VerifyCRSSignature(block, crs, nil) s.True(ok) block.Position.Height++ - ok, err = VerifyCRSSignature(block, crs) - s.Require().NoError(err) + ok = VerifyCRSSignature(block, crs, nil) s.False(ok) } diff --git a/core/utils/signer.go b/core/utils/signer.go index 7694dab..9904410 100644 --- a/core/utils/signer.go +++ b/core/utils/signer.go @@ -31,13 +31,17 @@ var ( ErrInvalidProposerID = errors.New("invalid proposer id") ErrIncorrectHash = errors.New("hash of block is incorrect") ErrIncorrectSignature = errors.New("signature of block is incorrect") + ErrNoBLSSigner = errors.New("bls signer not set") ) +type blsSigner func(round uint64, hash common.Hash) (crypto.Signature, error) + // Signer signs a segment of data. type Signer struct { prvKey crypto.PrivateKey pubKey crypto.PublicKey proposerID types.NodeID + blsSign blsSigner } // NewSigner constructs an Signer instance. @@ -50,6 +54,11 @@ func NewSigner(prvKey crypto.PrivateKey) (s *Signer) { return } +// SetBLSSigner for signing CRSSignature +func (s *Signer) SetBLSSigner(signer blsSigner) { + s.blsSign = signer +} + // SignBlock signs a types.Block. func (s *Signer) SignBlock(b *types.Block) (err error) { b.ProposerID = s.proposerID @@ -76,7 +85,19 @@ func (s *Signer) SignCRS(b *types.Block, crs common.Hash) (err error) { err = ErrInvalidProposerID return } - b.CRSSignature, err = s.prvKey.Sign(hashCRS(b, crs)) + if b.Position.Round < dkgDelayRound { + hash := hashCRS(b, crs) + b.CRSSignature = crypto.Signature{ + Type: "bls", + Signature: hash[:], + } + return + } + if s.blsSign == nil { + err = ErrNoBLSSigner + return + } + b.CRSSignature, err = s.blsSign(b.Position.Round, hashCRS(b, crs)) return } diff --git a/core/utils/signer_test.go b/core/utils/signer_test.go index 3905352..0ee1c30 100644 --- a/core/utils/signer_test.go +++ b/core/utils/signer_test.go @@ -66,11 +66,12 @@ func (s *SignerTestSuite) TestVote() { } func (s *SignerTestSuite) TestCRS() { + dkgDelayRound = 1 k := s.setupSigner() b := &types.Block{ ParentHash: common.NewRandomHash(), Position: types.Position{ - Round: 8, + Round: 0, Height: 9, }, Timestamp: time.Now().UTC(), @@ -80,9 +81,8 @@ func (s *SignerTestSuite) TestCRS() { // Hash block before hash CRS. s.NoError(k.SignBlock(b)) s.NoError(k.SignCRS(b, crs)) - ok, err := VerifyCRSSignature(b, crs) + ok := VerifyCRSSignature(b, crs, nil) s.True(ok) - s.NoError(err) } func TestSigner(t *testing.T) { |