diff options
author | bojie <bojie@dexon.org> | 2019-03-13 15:38:33 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-03-13 15:38:33 +0800 |
commit | 8786160e28cf17c1125e26939b81ac59df5c260a (patch) | |
tree | 509444fbca35c37b1d073510e439a7b1f2f50768 /core/crypto | |
parent | 92d64e7e743f7afce6ab811bce4d57fc67297567 (diff) | |
download | dexon-consensus-8786160e28cf17c1125e26939b81ac59df5c260a.tar dexon-consensus-8786160e28cf17c1125e26939b81ac59df5c260a.tar.gz dexon-consensus-8786160e28cf17c1125e26939b81ac59df5c260a.tar.bz2 dexon-consensus-8786160e28cf17c1125e26939b81ac59df5c260a.tar.lz dexon-consensus-8786160e28cf17c1125e26939b81ac59df5c260a.tar.xz dexon-consensus-8786160e28cf17c1125e26939b81ac59df5c260a.tar.zst dexon-consensus-8786160e28cf17c1125e26939b81ac59df5c260a.zip |
core: recover DKG master private shares (#487)
Diffstat (limited to 'core/crypto')
-rw-r--r-- | core/crypto/dkg/dkg.go | 27 | ||||
-rw-r--r-- | core/crypto/dkg/dkg_test.go | 16 |
2 files changed, 43 insertions, 0 deletions
diff --git a/core/crypto/dkg/dkg.go b/core/crypto/dkg/dkg.go index f6b3e0e..425d96b 100644 --- a/core/crypto/dkg/dkg.go +++ b/core/crypto/dkg/dkg.go @@ -131,6 +131,33 @@ func (prvs *PrivateKeyShares) Equal(other *PrivateKeyShares) bool { return true } +// EncodeRLP implements rlp.Encoder +func (prvs *PrivateKeyShares) EncodeRLP(w io.Writer) error { + mpks := make([][]byte, len(prvs.masterPrivateKey)) + for i, m := range prvs.masterPrivateKey { + mpks[i] = m.GetLittleEndian() + } + return rlp.Encode(w, mpks) +} + +// DecodeRLP implements rlp.Decoder +func (prvs *PrivateKeyShares) DecodeRLP(s *rlp.Stream) error { + var dec [][]byte + if err := s.Decode(&dec); err != nil { + return err + } + + for _, k := range dec { + var key bls.SecretKey + if err := key.SetLittleEndian(k); err != nil { + return err + } + prvs.masterPrivateKey = append(prvs.masterPrivateKey, key) + } + + return nil +} + // PublicKeyShares represents a public key shares for DKG protocol. type PublicKeyShares struct { shareCaches []PublicKey diff --git a/core/crypto/dkg/dkg_test.go b/core/crypto/dkg/dkg_test.go index e097bdb..20dd908 100644 --- a/core/crypto/dkg/dkg_test.go +++ b/core/crypto/dkg/dkg_test.go @@ -328,6 +328,22 @@ func (s *DKGTestSuite) TestPublicKeySharesRLPEncodeDecode() { s.Require().True(reflect.DeepEqual(b, bb)) } +func (s *DKGTestSuite) TestPrivateKeySharesRLPEncodeDecode() { + privShares, _ := NewPrivateKeyShares(10) + + b, err := rlp.EncodeToBytes(privShares) + s.Require().NoError(err) + + var newPrivShares PrivateKeyShares + err = rlp.DecodeBytes(b, &newPrivShares) + s.Require().NoError(err) + + bb, err := rlp.EncodeToBytes(&newPrivShares) + s.Require().NoError(err) + + s.Require().True(reflect.DeepEqual(b, bb)) +} + func (s *DKGTestSuite) TestPublicKeySharesEquality() { var req = s.Require() IDs := s.genID(2) |