diff --git a/consensus/consensus.go b/consensus/consensus.go index f689cce37..b9f8f8658 100644 --- a/consensus/consensus.go +++ b/consensus/consensus.go @@ -5,8 +5,10 @@ import ( "sync" "time" + "github.com/harmony-one/harmony/crypto/bls" + "github.com/harmony-one/abool" - "github.com/harmony-one/bls/ffi/go/bls" + bls_core "github.com/harmony-one/bls/ffi/go/bls" "github.com/harmony-one/harmony/consensus/quorum" "github.com/harmony-one/harmony/core" "github.com/harmony-one/harmony/core/types" @@ -14,7 +16,6 @@ import ( "github.com/harmony-one/harmony/internal/utils" "github.com/harmony-one/harmony/multibls" "github.com/harmony-one/harmony/p2p" - "github.com/harmony-one/harmony/shard" "github.com/harmony-one/harmony/staking/slash" "github.com/pkg/errors" ) @@ -47,8 +48,8 @@ type Consensus struct { // 2 types of timeouts: normal and viewchange consensusTimeout map[TimeoutType]*utils.Timeout // Commits collected from validators. - aggregatedPrepareSig *bls.Sign - aggregatedCommitSig *bls.Sign + aggregatedPrepareSig *bls_core.Sign + aggregatedCommitSig *bls_core.Sign prepareBitmap *bls_cosi.Mask commitBitmap *bls_cosi.Mask // Commits collected from view change @@ -57,11 +58,11 @@ type Consensus struct { // after one of viewID has enough votes, we can reset and clean the map // honest nodes will never double votes on different viewID // bhpSigs: blockHashPreparedSigs is the signature on m1 type message - bhpSigs map[uint64]map[string]*bls.Sign + bhpSigs map[uint64]map[string]*bls_core.Sign // nilSigs: there is no prepared message when view change, // it's signature on m2 type (i.e. nil) messages - nilSigs map[uint64]map[string]*bls.Sign - viewIDSigs map[uint64]map[string]*bls.Sign + nilSigs map[uint64]map[string]*bls_core.Sign + viewIDSigs map[uint64]map[string]*bls_core.Sign bhpBitmap map[uint64]*bls_cosi.Mask nilBitmap map[uint64]*bls_cosi.Mask viewIDBitmap map[uint64]*bls_cosi.Mask @@ -78,7 +79,7 @@ type Consensus struct { // private/public keys of current node priKey multibls.PrivateKeys // the publickey of leader - LeaderPubKey *shard.BLSPublicKeyWrapper + LeaderPubKey *bls.PublicKeyWrapper viewID uint64 // Blockhash - 32 byte blockHash [32]byte @@ -157,7 +158,7 @@ func (consensus *Consensus) GetPublicKeys() multibls.PublicKeys { } // GetLeaderPrivateKey returns leader private key if node is the leader -func (consensus *Consensus) GetLeaderPrivateKey(leaderKey *bls.PublicKey) (*shard.BLSPrivateKeyWrapper, error) { +func (consensus *Consensus) GetLeaderPrivateKey(leaderKey *bls_core.PublicKey) (*bls.PrivateKeyWrapper, error) { for i, key := range consensus.priKey { if key.Pub.Object.IsEqual(leaderKey) { return &consensus.priKey[i], nil @@ -167,7 +168,7 @@ func (consensus *Consensus) GetLeaderPrivateKey(leaderKey *bls.PublicKey) (*shar } // GetConsensusLeaderPrivateKey returns consensus leader private key if node is the leader -func (consensus *Consensus) GetConsensusLeaderPrivateKey() (*shard.BLSPrivateKeyWrapper, error) { +func (consensus *Consensus) GetConsensusLeaderPrivateKey() (*bls.PrivateKeyWrapper, error) { return consensus.GetLeaderPrivateKey(consensus.LeaderPubKey.Object) } diff --git a/consensus/consensus_service.go b/consensus/consensus_service.go index 35dbbb48b..27eba0a2e 100644 --- a/consensus/consensus_service.go +++ b/consensus/consensus_service.go @@ -4,9 +4,11 @@ import ( "math/big" "sync/atomic" + "github.com/harmony-one/harmony/crypto/bls" + "github.com/ethereum/go-ethereum/common" protobuf "github.com/golang/protobuf/proto" - "github.com/harmony-one/bls/ffi/go/bls" + bls_core "github.com/harmony-one/bls/ffi/go/bls" msg_pb "github.com/harmony-one/harmony/api/proto/message" "github.com/harmony-one/harmony/block" consensus_engine "github.com/harmony-one/harmony/consensus/engine" @@ -56,7 +58,7 @@ var ( // Signs the consensus message and returns the marshaled message. func (consensus *Consensus) signAndMarshalConsensusMessage(message *msg_pb.Message, - priKey *bls.SecretKey) ([]byte, error) { + priKey *bls_core.SecretKey) ([]byte, error) { if err := consensus.signConsensusMessage(message, priKey); err != nil { return empty, err } @@ -74,7 +76,7 @@ func (consensus *Consensus) GetViewID() uint64 { // UpdatePublicKeys updates the PublicKeys for // quorum on current subcommittee, protected by a mutex -func (consensus *Consensus) UpdatePublicKeys(pubKeys []*bls.PublicKey) int64 { +func (consensus *Consensus) UpdatePublicKeys(pubKeys []*bls_core.PublicKey) int64 { consensus.pubKeyLock.Lock() consensus.Decider.UpdateParticipants(pubKeys) utils.Logger().Info().Msg("My Committee updated") @@ -105,7 +107,7 @@ func NewFaker() *Consensus { } // Sign on the hash of the message -func (consensus *Consensus) signMessage(message []byte, priKey *bls.SecretKey) []byte { +func (consensus *Consensus) signMessage(message []byte, priKey *bls_core.SecretKey) []byte { hash := hash.Keccak256(message) signature := priKey.SignHash(hash[:]) return signature.Serialize() @@ -113,7 +115,7 @@ func (consensus *Consensus) signMessage(message []byte, priKey *bls.SecretKey) [ // Sign on the consensus message signature field. func (consensus *Consensus) signConsensusMessage(message *msg_pb.Message, - priKey *bls.SecretKey) error { + priKey *bls_core.SecretKey) error { message.Signature = nil // TODO: use custom serialization method rather than protobuf marshaledMessage, err := protobuf.Marshal(message) @@ -127,8 +129,8 @@ func (consensus *Consensus) signConsensusMessage(message *msg_pb.Message, } // GetViewIDSigsArray returns the signatures for viewID in viewchange -func (consensus *Consensus) GetViewIDSigsArray(viewID uint64) []*bls.Sign { - sigs := []*bls.Sign{} +func (consensus *Consensus) GetViewIDSigsArray(viewID uint64) []*bls_core.Sign { + sigs := []*bls_core.Sign{} for _, sig := range consensus.viewIDSigs[viewID] { sigs = append(sigs, sig) } @@ -136,8 +138,8 @@ func (consensus *Consensus) GetViewIDSigsArray(viewID uint64) []*bls.Sign { } // GetNilSigsArray returns the signatures for nil prepared message in viewchange -func (consensus *Consensus) GetNilSigsArray(viewID uint64) []*bls.Sign { - sigs := []*bls.Sign{} +func (consensus *Consensus) GetNilSigsArray(viewID uint64) []*bls_core.Sign { + sigs := []*bls_core.Sign{} for _, sig := range consensus.nilSigs[viewID] { sigs = append(sigs, sig) } @@ -150,7 +152,7 @@ func (consensus *Consensus) UpdateBitmaps() { Str("Phase", consensus.phase.String()). Msg("[UpdateBitmaps] Updating consensus bitmaps") members := consensus.Decider.Participants() - publicKeys := []*bls.PublicKey{} + publicKeys := []*bls_core.PublicKey{} for _, key := range members { publicKeys = append(publicKeys, key.Object) } @@ -186,17 +188,17 @@ func (consensus *Consensus) ToggleConsensusCheck() { } // IsValidatorInCommittee returns whether the given validator BLS address is part of my committee -func (consensus *Consensus) IsValidatorInCommittee(pubKey shard.BLSPublicKey) bool { +func (consensus *Consensus) IsValidatorInCommittee(pubKey bls.SerializedPublicKey) bool { return consensus.Decider.IndexOf(pubKey) != -1 } // IsValidatorInCommitteeBytes returns whether the given validator BLS address is part of my committee -func (consensus *Consensus) IsValidatorInCommitteeBytes(pubKey shard.BLSPublicKey) bool { +func (consensus *Consensus) IsValidatorInCommitteeBytes(pubKey bls.SerializedPublicKey) bool { return consensus.Decider.IndexOf(pubKey) != -1 } // Verify the signature of the message are valid from the signer's public key. -func verifyMessageSig(signerPubKey *bls.PublicKey, message *msg_pb.Message) error { +func verifyMessageSig(signerPubKey *bls_core.PublicKey, message *msg_pb.Message) error { signature := message.Signature message.Signature = nil messageBytes, err := protobuf.Marshal(message) @@ -204,7 +206,7 @@ func verifyMessageSig(signerPubKey *bls.PublicKey, message *msg_pb.Message) erro return err } - msgSig := bls.Sign{} + msgSig := bls_core.Sign{} err = msgSig.Deserialize(signature) if err != nil { return err @@ -219,7 +221,7 @@ func verifyMessageSig(signerPubKey *bls.PublicKey, message *msg_pb.Message) erro // verifySenderKey verifys the message senderKey is properly signed and senderAddr is valid func (consensus *Consensus) verifySenderKey(msg *msg_pb.Message) error { - senderKey := shard.BLSPublicKey{} + senderKey := bls.SerializedPublicKey{} copy(senderKey[:], msg.GetConsensus().SenderPubkey[:]) if !consensus.IsValidatorInCommitteeBytes(senderKey) { @@ -230,7 +232,7 @@ func (consensus *Consensus) verifySenderKey(msg *msg_pb.Message) error { func (consensus *Consensus) verifyViewChangeSenderKey(msg *msg_pb.Message) error { vcMsg := msg.GetViewchange() - senderKey := shard.BLSPublicKey{} + senderKey := bls.SerializedPublicKey{} copy(senderKey[:], vcMsg.SenderPubkey) if !consensus.IsValidatorInCommittee(senderKey) { @@ -302,15 +304,15 @@ func (consensus *Consensus) SetBlockNum(blockNum uint64) { // ReadSignatureBitmapPayload read the payload for signature and bitmap; offset is the beginning position of reading func (consensus *Consensus) ReadSignatureBitmapPayload( recvPayload []byte, offset int, -) (*bls.Sign, *bls_cosi.Mask, error) { - if offset+shard.BLSSignatureSizeInBytes > len(recvPayload) { +) (*bls_core.Sign, *bls_cosi.Mask, error) { + if offset+bls.BLSSignatureSizeInBytes > len(recvPayload) { return nil, nil, errors.New("payload not have enough length") } sigAndBitmapPayload := recvPayload[offset:] // TODO(audit): keep a Mask in the Decider so it won't be reconstructed on the fly. members := consensus.Decider.Participants() - publicKeys := []*bls.PublicKey{} + publicKeys := []*bls_core.PublicKey{} for _, key := range members { publicKeys = append(publicKeys, key.Object) } @@ -333,7 +335,7 @@ func (consensus *Consensus) getLogger() *zerolog.Logger { // retrieve corresponding blsPublicKey from Coinbase Address func (consensus *Consensus) getLeaderPubKeyFromCoinbase( header *block.Header, -) (*shard.BLSPublicKeyWrapper, error) { +) (*bls.PublicKeyWrapper, error) { shardState, err := consensus.ChainReader.ReadShardState(header.Epoch()) if err != nil { return nil, errors.Wrapf(err, "cannot read shard state %v %s", @@ -347,7 +349,7 @@ func (consensus *Consensus) getLeaderPubKeyFromCoinbase( return nil, err } - committerKey := new(bls.PublicKey) + committerKey := new(bls_core.PublicKey) isStaking := consensus.ChainReader.Config().IsStaking(header.Epoch()) for _, member := range committee.Slots { if isStaking { @@ -356,14 +358,14 @@ func (consensus *Consensus) getLeaderPubKeyFromCoinbase( if err := member.BLSPublicKey.ToLibBLSPublicKey(committerKey); err != nil { return nil, err } - return &shard.BLSPublicKeyWrapper{Object: committerKey, Bytes: member.BLSPublicKey}, nil + return &bls.PublicKeyWrapper{Object: committerKey, Bytes: member.BLSPublicKey}, nil } } else { if member.EcdsaAddress == header.Coinbase() { if err := member.BLSPublicKey.ToLibBLSPublicKey(committerKey); err != nil { return nil, err } - return &shard.BLSPublicKeyWrapper{Object: committerKey, Bytes: member.BLSPublicKey}, nil + return &bls.PublicKeyWrapper{Object: committerKey, Bytes: member.BLSPublicKey}, nil } } } @@ -565,18 +567,18 @@ func (consensus *Consensus) NeedsRandomNumberGeneration(epoch *big.Int) bool { func (consensus *Consensus) addViewIDKeyIfNotExist(viewID uint64) { members := consensus.Decider.Participants() - publicKeys := []*bls.PublicKey{} + publicKeys := []*bls_core.PublicKey{} for _, key := range members { publicKeys = append(publicKeys, key.Object) } if _, ok := consensus.bhpSigs[viewID]; !ok { - consensus.bhpSigs[viewID] = map[string]*bls.Sign{} + consensus.bhpSigs[viewID] = map[string]*bls_core.Sign{} } if _, ok := consensus.nilSigs[viewID]; !ok { - consensus.nilSigs[viewID] = map[string]*bls.Sign{} + consensus.nilSigs[viewID] = map[string]*bls_core.Sign{} } if _, ok := consensus.viewIDSigs[viewID]; !ok { - consensus.viewIDSigs[viewID] = map[string]*bls.Sign{} + consensus.viewIDSigs[viewID] = map[string]*bls_core.Sign{} } if _, ok := consensus.bhpBitmap[viewID]; !ok { bhpBitmap, _ := bls_cosi.NewMask(publicKeys, nil) diff --git a/consensus/consensus_service_test.go b/consensus/consensus_service_test.go index 0dbaabedd..6f127051e 100644 --- a/consensus/consensus_service_test.go +++ b/consensus/consensus_service_test.go @@ -4,9 +4,10 @@ import ( "bytes" "testing" + "github.com/harmony-one/harmony/crypto/bls" + msg_pb "github.com/harmony-one/harmony/api/proto/message" "github.com/harmony-one/harmony/consensus/quorum" - "github.com/harmony-one/harmony/crypto/bls" "github.com/harmony-one/harmony/internal/utils" "github.com/harmony-one/harmony/multibls" "github.com/harmony-one/harmony/p2p" @@ -40,7 +41,7 @@ func TestPopulateMessageFields(t *testing.T) { }, } - keyBytes := shard.BLSPublicKey{} + keyBytes := bls.SerializedPublicKey{} keyBytes.FromLibBLSPublicKey(blsPriKey.GetPublicKey()) consensusMsg := consensus.populateMessageFields(msg.GetConsensus(), consensus.blockHash[:], keyBytes) diff --git a/consensus/consensus_v2.go b/consensus/consensus_v2.go index 7a25a7b86..20d3c5842 100644 --- a/consensus/consensus_v2.go +++ b/consensus/consensus_v2.go @@ -6,6 +6,8 @@ import ( "sync/atomic" "time" + "github.com/harmony-one/harmony/crypto/bls" + protobuf "github.com/golang/protobuf/proto" msg_pb "github.com/harmony-one/harmony/api/proto/message" "github.com/harmony-one/harmony/block" @@ -195,7 +197,7 @@ func (consensus *Consensus) BlockCommitSig(blockNum uint64) ([]byte, []byte, err } lastCommits, err := consensus.ChainReader.ReadCommitSig(blockNum) if err != nil || - len(lastCommits) < shard.BLSSignatureSizeInBytes { + len(lastCommits) < bls.BLSSignatureSizeInBytes { msgs := consensus.FBFTLog.GetMessagesByTypeSeq( msg_pb.MessageType_COMMITTED, blockNum, ) @@ -210,11 +212,11 @@ func (consensus *Consensus) BlockCommitSig(blockNum uint64) ([]byte, []byte, err lastCommits = msgs[0].Payload } //#### Read payload data from committed msg - aggSig := make([]byte, shard.BLSSignatureSizeInBytes) - bitmap := make([]byte, len(lastCommits)-shard.BLSSignatureSizeInBytes) + aggSig := make([]byte, bls.BLSSignatureSizeInBytes) + bitmap := make([]byte, len(lastCommits)-bls.BLSSignatureSizeInBytes) offset := 0 - copy(aggSig[:], lastCommits[offset:offset+shard.BLSSignatureSizeInBytes]) - offset += shard.BLSSignatureSizeInBytes + copy(aggSig[:], lastCommits[offset:offset+bls.BLSSignatureSizeInBytes]) + offset += bls.BLSSignatureSizeInBytes copy(bitmap[:], lastCommits[offset:]) //#### END Read payload data from committed msg return aggSig, bitmap, nil diff --git a/consensus/consensus_viewchange_msg.go b/consensus/consensus_viewchange_msg.go index bed7cb959..fd2496128 100644 --- a/consensus/consensus_viewchange_msg.go +++ b/consensus/consensus_viewchange_msg.go @@ -3,7 +3,7 @@ package consensus import ( "encoding/binary" - "github.com/harmony-one/harmony/shard" + "github.com/harmony-one/harmony/crypto/bls" "github.com/ethereum/go-ethereum/rlp" @@ -14,7 +14,7 @@ import ( ) // construct the view change message -func (consensus *Consensus) constructViewChangeMessage(priKey *shard.BLSPrivateKeyWrapper) []byte { +func (consensus *Consensus) constructViewChangeMessage(priKey *bls.PrivateKeyWrapper) []byte { message := &msg_pb.Message{ ServiceType: msg_pb.ServiceType_CONSENSUS, Type: msg_pb.MessageType_VIEWCHANGE, @@ -99,7 +99,7 @@ func (consensus *Consensus) constructViewChangeMessage(priKey *shard.BLSPrivateK } // new leader construct newview message -func (consensus *Consensus) constructNewViewMessage(viewID uint64, priKey *shard.BLSPrivateKeyWrapper) []byte { +func (consensus *Consensus) constructNewViewMessage(viewID uint64, priKey *bls.PrivateKeyWrapper) []byte { message := &msg_pb.Message{ ServiceType: msg_pb.ServiceType_CONSENSUS, Type: msg_pb.MessageType_NEWVIEW, diff --git a/consensus/construct.go b/consensus/construct.go index 98d39ddd3..e4d1b880d 100644 --- a/consensus/construct.go +++ b/consensus/construct.go @@ -3,9 +3,9 @@ package consensus import ( "bytes" - "github.com/harmony-one/harmony/shard" + "github.com/harmony-one/harmony/crypto/bls" - "github.com/harmony-one/bls/ffi/go/bls" + bls_core "github.com/harmony-one/bls/ffi/go/bls" "github.com/harmony-one/harmony/api/proto" msg_pb "github.com/harmony-one/harmony/api/proto/message" "github.com/harmony-one/harmony/consensus/quorum" @@ -19,12 +19,12 @@ type NetworkMessage struct { Phase msg_pb.MessageType Bytes []byte FBFTMsg *FBFTMessage - OptionalAggregateSignature *bls.Sign + OptionalAggregateSignature *bls_core.Sign } // Populates the common basic fields for all consensus message. func (consensus *Consensus) populateMessageFields( - request *msg_pb.ConsensusRequest, blockHash []byte, pubKey shard.BLSPublicKey, + request *msg_pb.ConsensusRequest, blockHash []byte, pubKey bls.SerializedPublicKey, ) *msg_pb.ConsensusRequest { request.ViewId = consensus.viewID request.BlockNum = consensus.blockNum @@ -38,7 +38,7 @@ func (consensus *Consensus) populateMessageFields( // construct is the single creation point of messages intended for the wire. func (consensus *Consensus) construct( - p msg_pb.MessageType, payloadForSign []byte, priKey *shard.BLSPrivateKeyWrapper, + p msg_pb.MessageType, payloadForSign []byte, priKey *bls.PrivateKeyWrapper, ) (*NetworkMessage, error) { message := &msg_pb.Message{ ServiceType: msg_pb.ServiceType_CONSENSUS, @@ -49,7 +49,7 @@ func (consensus *Consensus) construct( } var ( consensusMsg *msg_pb.ConsensusRequest - aggSig *bls.Sign + aggSig *bls_core.Sign ) consensusMsg = consensus.populateMessageFields( diff --git a/consensus/construct_test.go b/consensus/construct_test.go index 1fb7e8deb..edd91ad34 100644 --- a/consensus/construct_test.go +++ b/consensus/construct_test.go @@ -32,9 +32,9 @@ func TestConstructAnnounceMessage(test *testing.T) { test.Fatalf("Cannot create consensus: %v", err) } consensus.blockHash = [32]byte{} - pubKeyWrapper := shard.BLSPublicKeyWrapper{Object: blsPriKey.GetPublicKey()} + pubKeyWrapper := bls.PublicKeyWrapper{Object: blsPriKey.GetPublicKey()} pubKeyWrapper.Bytes.FromLibBLSPublicKey(pubKeyWrapper.Object) - priKeyWrapper := shard.BLSPrivateKeyWrapper{blsPriKey, &pubKeyWrapper} + priKeyWrapper := bls.PrivateKeyWrapper{blsPriKey, &pubKeyWrapper} if _, err = consensus.construct(msg_pb.MessageType_ANNOUNCE, nil, &priKeyWrapper); err != nil { test.Fatalf("could not construct announce: %v", err) } @@ -67,9 +67,9 @@ func TestConstructPreparedMessage(test *testing.T) { consensus.blockHash = [32]byte{} message := "test string" - leaderKey := shard.BLSPublicKey{} + leaderKey := bls.SerializedPublicKey{} leaderKey.FromLibBLSPublicKey(leaderPubKey) - validatorKey := shard.BLSPublicKey{} + validatorKey := bls.SerializedPublicKey{} validatorKey.FromLibBLSPublicKey(validatorPubKey) consensus.Decider.SubmitVote( quorum.Prepare, @@ -98,9 +98,9 @@ func TestConstructPreparedMessage(test *testing.T) { test.Log(errors.New("prepareBitmap.SetKey")) } - pubKeyWrapper := shard.BLSPublicKeyWrapper{Object: blsPriKey.GetPublicKey()} + pubKeyWrapper := bls.PublicKeyWrapper{Object: blsPriKey.GetPublicKey()} pubKeyWrapper.Bytes.FromLibBLSPublicKey(pubKeyWrapper.Object) - priKeyWrapper := shard.BLSPrivateKeyWrapper{blsPriKey, &pubKeyWrapper} + priKeyWrapper := bls.PrivateKeyWrapper{blsPriKey, &pubKeyWrapper} network, err := consensus.construct(msg_pb.MessageType_PREPARED, nil, &priKeyWrapper) if err != nil { test.Errorf("Error when creating prepared message") diff --git a/consensus/fbft_log.go b/consensus/fbft_log.go index 6c7d0fad6..e55ec04f3 100644 --- a/consensus/fbft_log.go +++ b/consensus/fbft_log.go @@ -3,11 +3,11 @@ package consensus import ( "fmt" - "github.com/harmony-one/harmony/shard" + "github.com/harmony-one/harmony/crypto/bls" mapset "github.com/deckarep/golang-set" "github.com/ethereum/go-ethereum/common" - "github.com/harmony-one/bls/ffi/go/bls" + bls_core "github.com/harmony-one/bls/ffi/go/bls" msg_pb "github.com/harmony-one/harmony/api/proto/message" "github.com/harmony-one/harmony/core/types" bls_cosi "github.com/harmony-one/harmony/crypto/bls" @@ -28,14 +28,14 @@ type FBFTMessage struct { BlockNum uint64 BlockHash common.Hash Block []byte - SenderPubkey *shard.BLSPublicKeyWrapper - LeaderPubkey *shard.BLSPublicKeyWrapper + SenderPubkey *bls.PublicKeyWrapper + LeaderPubkey *bls.PublicKeyWrapper Payload []byte - ViewchangeSig *bls.Sign - ViewidSig *bls.Sign - M2AggSig *bls.Sign + ViewchangeSig *bls_core.Sign + ViewidSig *bls_core.Sign + M2AggSig *bls_core.Sign M2Bitmap *bls_cosi.Mask - M3AggSig *bls.Sign + M3AggSig *bls_core.Sign M3Bitmap *bls_cosi.Mask } @@ -249,7 +249,7 @@ func ParseFBFTMessage(msg *msg_pb.Message) (*FBFTMessage, error) { if err != nil { return nil, err } - pbftMsg.SenderPubkey = &shard.BLSPublicKeyWrapper{Object: pubKey} + pbftMsg.SenderPubkey = &bls.PublicKeyWrapper{Object: pubKey} copy(pbftMsg.SenderPubkey.Bytes[:], consensusMsg.SenderPubkey[:]) return &pbftMsg, nil @@ -282,23 +282,23 @@ func ParseViewChangeMessage(msg *msg_pb.Message) (*FBFTMessage, error) { return nil, err } - vcSig := bls.Sign{} + vcSig := bls_core.Sign{} err = vcSig.Deserialize(vcMsg.ViewchangeSig) if err != nil { utils.Logger().Warn().Err(err).Msg("ParseViewChangeMessage failed to deserialize the viewchange signature") return nil, err } - vcSig1 := bls.Sign{} + vcSig1 := bls_core.Sign{} err = vcSig1.Deserialize(vcMsg.ViewidSig) if err != nil { utils.Logger().Warn().Err(err).Msg("ParseViewChangeMessage failed to deserialize the viewid signature") return nil, err } - pbftMsg.SenderPubkey = &shard.BLSPublicKeyWrapper{Object: pubKey} + pbftMsg.SenderPubkey = &bls.PublicKeyWrapper{Object: pubKey} copy(pbftMsg.SenderPubkey.Bytes[:], vcMsg.SenderPubkey[:]) - pbftMsg.LeaderPubkey = &shard.BLSPublicKeyWrapper{Object: leaderKey} + pbftMsg.LeaderPubkey = &bls.PublicKeyWrapper{Object: leaderKey} copy(pbftMsg.LeaderPubkey.Bytes[:], vcMsg.LeaderPubkey[:]) pbftMsg.ViewchangeSig = &vcSig pbftMsg.ViewidSig = &vcSig1 @@ -328,16 +328,16 @@ func (consensus *Consensus) ParseNewViewMessage(msg *msg_pb.Message) (*FBFTMessa return nil, err } - FBFTMsg.SenderPubkey = &shard.BLSPublicKeyWrapper{Object: pubKey} + FBFTMsg.SenderPubkey = &bls.PublicKeyWrapper{Object: pubKey} copy(FBFTMsg.SenderPubkey.Bytes[:], vcMsg.SenderPubkey[:]) members := consensus.Decider.Participants() - publicKeys := []*bls.PublicKey{} + publicKeys := []*bls_core.PublicKey{} for _, key := range members { publicKeys = append(publicKeys, key.Object) } if len(vcMsg.M3Aggsigs) > 0 { - m3Sig := bls.Sign{} + m3Sig := bls_core.Sign{} err = m3Sig.Deserialize(vcMsg.M3Aggsigs) if err != nil { utils.Logger().Warn().Err(err).Msg("ParseViewChangeMessage failed to deserialize the multi signature for M3 viewID signature") @@ -354,7 +354,7 @@ func (consensus *Consensus) ParseNewViewMessage(msg *msg_pb.Message) (*FBFTMessa } if len(vcMsg.M2Aggsigs) > 0 { - m2Sig := bls.Sign{} + m2Sig := bls_core.Sign{} err = m2Sig.Deserialize(vcMsg.M2Aggsigs) if err != nil { utils.Logger().Warn().Err(err).Msg("ParseViewChangeMessage failed to deserialize the multi signature for M2 aggregated signature") diff --git a/consensus/quorum/one-node-one-vote.go b/consensus/quorum/one-node-one-vote.go index 7468e7aa0..7477ed976 100644 --- a/consensus/quorum/one-node-one-vote.go +++ b/consensus/quorum/one-node-one-vote.go @@ -4,8 +4,10 @@ import ( "encoding/json" "math/big" + bls_core "github.com/harmony-one/bls/ffi/go/bls" + "github.com/harmony-one/harmony/crypto/bls" + "github.com/ethereum/go-ethereum/common" - "github.com/harmony-one/bls/ffi/go/bls" "github.com/harmony-one/harmony/consensus/votepower" bls_cosi "github.com/harmony-one/harmony/crypto/bls" @@ -27,8 +29,8 @@ func (v *uniformVoteWeight) Policy() Policy { // AddNewVote .. func (v *uniformVoteWeight) AddNewVote( - p Phase, pubKeyBytes shard.BLSPublicKey, - sig *bls.Sign, headerHash common.Hash, + p Phase, pubKeyBytes bls.SerializedPublicKey, + sig *bls_core.Sign, headerHash common.Hash, height, viewID uint64) (*votepower.Ballot, error) { return v.SubmitVote(p, pubKeyBytes, sig, headerHash, height, viewID) diff --git a/consensus/quorum/one-node-staked-vote.go b/consensus/quorum/one-node-staked-vote.go index e8d6101dc..3e064827d 100644 --- a/consensus/quorum/one-node-staked-vote.go +++ b/consensus/quorum/one-node-staked-vote.go @@ -4,10 +4,12 @@ import ( "encoding/json" "math/big" + "github.com/harmony-one/harmony/crypto/bls" + "github.com/harmony-one/harmony/internal/utils" "github.com/ethereum/go-ethereum/common" - "github.com/harmony-one/bls/ffi/go/bls" + bls_core "github.com/harmony-one/bls/ffi/go/bls" "github.com/pkg/errors" "github.com/harmony-one/harmony/consensus/votepower" @@ -55,8 +57,8 @@ func (v *stakedVoteWeight) Policy() Policy { // AddNewVote .. func (v *stakedVoteWeight) AddNewVote( - p Phase, pubKeyBytes shard.BLSPublicKey, - sig *bls.Sign, headerHash common.Hash, + p Phase, pubKeyBytes bls.SerializedPublicKey, + sig *bls_core.Sign, headerHash common.Hash, height, viewID uint64) (*votepower.Ballot, error) { ballet, err := v.SubmitVote(p, pubKeyBytes, sig, headerHash, height, viewID) @@ -142,7 +144,7 @@ func (v *stakedVoteWeight) currentTotalPower(p Phase) (*numeric.Dec, error) { // ComputeTotalPowerByMask computes the total power indicated by bitmap mask func (v *stakedVoteWeight) computeTotalPowerByMask(mask *bls_cosi.Mask) *numeric.Dec { pubKeys := mask.Publics - w := shard.BLSPublicKey{} + w := bls.SerializedPublicKey{} currentTotal := numeric.ZeroDec() for i := range pubKeys { @@ -192,7 +194,7 @@ func (v *stakedVoteWeight) String() string { } // HACK later remove - unify votepower in UI (aka MarshalJSON) -func (v *stakedVoteWeight) SetRawStake(key shard.BLSPublicKey, d numeric.Dec) { +func (v *stakedVoteWeight) SetRawStake(key bls.SerializedPublicKey, d numeric.Dec) { if voter, ok := v.roster.Voters[key]; ok { voter.RawStake = d } diff --git a/consensus/quorum/one-node-staked-vote_test.go b/consensus/quorum/one-node-staked-vote_test.go index a6ec07a57..f51c9ab2a 100644 --- a/consensus/quorum/one-node-staked-vote_test.go +++ b/consensus/quorum/one-node-staked-vote_test.go @@ -6,10 +6,12 @@ import ( "strconv" "testing" + "github.com/harmony-one/harmony/crypto/bls" + shardingconfig "github.com/harmony-one/harmony/internal/configs/sharding" "github.com/ethereum/go-ethereum/common" - "github.com/harmony-one/bls/ffi/go/bls" + bls_core "github.com/harmony-one/bls/ffi/go/bls" "github.com/harmony-one/harmony/numeric" "github.com/harmony-one/harmony/shard" ) @@ -28,19 +30,19 @@ var ( stakeGen = rand.New(rand.NewSource(541)) ) -type secretKeyMap map[shard.BLSPublicKey]bls.SecretKey +type secretKeyMap map[bls.SerializedPublicKey]bls_core.SecretKey func init() { basicDecider = NewDecider(SuperMajorityStake, shard.BeaconChainShardID) shard.Schedule = shardingconfig.LocalnetSchedule } -func generateRandomSlot() (shard.Slot, bls.SecretKey) { +func generateRandomSlot() (shard.Slot, bls_core.SecretKey) { addr := common.Address{} addr.SetBytes(big.NewInt(int64(accountGen.Int63n(maxAccountGen))).Bytes()) - secretKey := bls.SecretKey{} + secretKey := bls_core.SecretKey{} secretKey.Deserialize(big.NewInt(int64(keyGen.Int63n(maxKeyGen))).Bytes()) - key := shard.BLSPublicKey{} + key := bls.SerializedPublicKey{} key.FromLibBLSPublicKey(secretKey.GetPublicKey()) stake := numeric.NewDecFromBigInt(big.NewInt(int64(stakeGen.Int63n(maxStakeGen)))) return shard.Slot{addr, key, &stake}, secretKey @@ -52,7 +54,7 @@ func setupBaseCase() (Decider, *TallyResult, shard.SlotList, map[string]secretKe sKeys := map[string]secretKeyMap{} sKeys[hmy] = secretKeyMap{} sKeys[reg] = secretKeyMap{} - pubKeys := []*bls.PublicKey{} + pubKeys := []*bls_core.PublicKey{} for i := 0; i < quorumNodes; i++ { newSlot, sKey := generateRandomSlot() @@ -81,7 +83,7 @@ func setupBaseCase() (Decider, *TallyResult, shard.SlotList, map[string]secretKe func setupEdgeCase() (Decider, *TallyResult, shard.SlotList, secretKeyMap) { slotList := shard.SlotList{} sKeys := secretKeyMap{} - pubKeys := []*bls.PublicKey{} + pubKeys := []*bls_core.PublicKey{} for i := 0; i < quorumNodes; i++ { newSlot, sKey := generateRandomSlot() diff --git a/consensus/quorum/quorum.go b/consensus/quorum/quorum.go index 202a29cd2..ddf34b7b5 100644 --- a/consensus/quorum/quorum.go +++ b/consensus/quorum/quorum.go @@ -4,8 +4,10 @@ import ( "fmt" "math/big" + "github.com/harmony-one/harmony/crypto/bls" + "github.com/ethereum/go-ethereum/common" - "github.com/harmony-one/bls/ffi/go/bls" + bls_core "github.com/harmony-one/bls/ffi/go/bls" "github.com/harmony-one/harmony/consensus/votepower" bls_cosi "github.com/harmony-one/harmony/crypto/bls" "github.com/harmony-one/harmony/multibls" @@ -68,18 +70,18 @@ func (p Policy) String() string { // ParticipantTracker .. type ParticipantTracker interface { Participants() multibls.PublicKeys - IndexOf(shard.BLSPublicKey) int + IndexOf(bls.SerializedPublicKey) int ParticipantsCount() int64 - NextAfter(*shard.BLSPublicKeyWrapper) (bool, *shard.BLSPublicKeyWrapper) - UpdateParticipants(pubKeys []*bls.PublicKey) + NextAfter(*bls.PublicKeyWrapper) (bool, *bls.PublicKeyWrapper) + UpdateParticipants(pubKeys []*bls_core.PublicKey) } // SignatoryTracker .. type SignatoryTracker interface { ParticipantTracker SubmitVote( - p Phase, pubkey shard.BLSPublicKey, - sig *bls.Sign, headerHash common.Hash, + p Phase, pubkey bls.SerializedPublicKey, + sig *bls_core.Sign, headerHash common.Hash, height, viewID uint64, ) (*votepower.Ballot, error) // Caller assumes concurrency protection @@ -91,10 +93,10 @@ type SignatoryTracker interface { type SignatureReader interface { SignatoryTracker ReadAllBallots(Phase) []*votepower.Ballot - ReadBallot(p Phase, pubkey shard.BLSPublicKey) *votepower.Ballot + ReadBallot(p Phase, pubkey bls.SerializedPublicKey) *votepower.Ballot TwoThirdsSignersCount() int64 // 96 bytes aggregated signature - AggregateVotes(p Phase) *bls.Sign + AggregateVotes(p Phase) *bls_core.Sign } // DependencyInjectionWriter .. @@ -115,8 +117,8 @@ type Decider interface { SetVoters(subCommittee *shard.Committee, epoch *big.Int) (*TallyResult, error) Policy() Policy AddNewVote( - p Phase, pubkey shard.BLSPublicKey, - sig *bls.Sign, headerHash common.Hash, + p Phase, pubkey bls.SerializedPublicKey, + sig *bls_core.Sign, headerHash common.Hash, height, viewID uint64, ) (*votepower.Ballot, error) IsQuorumAchieved(Phase) bool @@ -150,8 +152,8 @@ type Transition struct { // and values are BLS private key signed signatures type cIdentities struct { // Public keys of the committee including leader and validators - publicKeys []shard.BLSPublicKeyWrapper - keyIndexMap map[shard.BLSPublicKey]int + publicKeys []bls.PublicKeyWrapper + keyIndexMap map[bls.SerializedPublicKey]int prepare *votepower.Round commit *votepower.Round // viewIDSigs: every validator @@ -163,11 +165,11 @@ type depInject struct { publicKeyProvider func() (multibls.PublicKeys, error) } -func (s *cIdentities) AggregateVotes(p Phase) *bls.Sign { +func (s *cIdentities) AggregateVotes(p Phase) *bls_core.Sign { ballots := s.ReadAllBallots(p) - sigs := make([]*bls.Sign, 0, len(ballots)) + sigs := make([]*bls_core.Sign, 0, len(ballots)) for _, ballot := range ballots { - sig := &bls.Sign{} + sig := &bls_core.Sign{} // NOTE invariant that shouldn't happen by now // but pointers are pointers if ballot != nil { @@ -178,14 +180,14 @@ func (s *cIdentities) AggregateVotes(p Phase) *bls.Sign { return bls_cosi.AggregateSig(sigs) } -func (s *cIdentities) IndexOf(pubKey shard.BLSPublicKey) int { +func (s *cIdentities) IndexOf(pubKey bls.SerializedPublicKey) int { if index, ok := s.keyIndexMap[pubKey]; ok { return index } return -1 } -func (s *cIdentities) NextAfter(pubKey *shard.BLSPublicKeyWrapper) (bool, *shard.BLSPublicKeyWrapper) { +func (s *cIdentities) NextAfter(pubKey *bls.PublicKeyWrapper) (bool, *bls.PublicKeyWrapper) { found := false idx := s.IndexOf(pubKey.Bytes) @@ -200,14 +202,14 @@ func (s *cIdentities) Participants() multibls.PublicKeys { return s.publicKeys } -func (s *cIdentities) UpdateParticipants(pubKeys []*bls.PublicKey) { - keys := make([]shard.BLSPublicKeyWrapper, len(pubKeys)) - keyIndexMap := map[shard.BLSPublicKey]int{} +func (s *cIdentities) UpdateParticipants(pubKeys []*bls_core.PublicKey) { + keys := make([]bls.PublicKeyWrapper, len(pubKeys)) + keyIndexMap := map[bls.SerializedPublicKey]int{} for i := range pubKeys { - kBytes := shard.BLSPublicKey{} + kBytes := bls.SerializedPublicKey{} kBytes.FromLibBLSPublicKey(pubKeys[i]) - keys[i] = shard.BLSPublicKeyWrapper{Object: pubKeys[i], Bytes: kBytes} + keys[i] = bls.PublicKeyWrapper{Object: pubKeys[i], Bytes: kBytes} keyIndexMap[kBytes] = i } s.publicKeys = keys @@ -233,8 +235,8 @@ func (s *cIdentities) SignersCount(p Phase) int64 { } func (s *cIdentities) SubmitVote( - p Phase, pubkey shard.BLSPublicKey, - sig *bls.Sign, headerHash common.Hash, + p Phase, pubkey bls.SerializedPublicKey, + sig *bls_core.Sign, headerHash common.Hash, height, viewID uint64, ) (*votepower.Ballot, error) { if ballet := s.ReadBallot(p, pubkey); ballet != nil { @@ -278,8 +280,8 @@ func (s *cIdentities) TwoThirdsSignersCount() int64 { return s.ParticipantsCount()*2/3 + 1 } -func (s *cIdentities) ReadBallot(p Phase, pubkey shard.BLSPublicKey) *votepower.Ballot { - ballotBox := map[shard.BLSPublicKey]*votepower.Ballot{} +func (s *cIdentities) ReadBallot(p Phase, pubkey bls.SerializedPublicKey) *votepower.Ballot { + ballotBox := map[bls.SerializedPublicKey]*votepower.Ballot{} switch p { case Prepare: @@ -298,7 +300,7 @@ func (s *cIdentities) ReadBallot(p Phase, pubkey shard.BLSPublicKey) *votepower. } func (s *cIdentities) ReadAllBallots(p Phase) []*votepower.Ballot { - m := map[shard.BLSPublicKey]*votepower.Ballot{} + m := map[bls.SerializedPublicKey]*votepower.Ballot{} switch p { case Prepare: m = s.prepare.BallotBox @@ -316,8 +318,8 @@ func (s *cIdentities) ReadAllBallots(p Phase) []*votepower.Ballot { func newBallotsBackedSignatureReader() *cIdentities { return &cIdentities{ - publicKeys: []shard.BLSPublicKeyWrapper{}, - keyIndexMap: map[shard.BLSPublicKey]int{}, + publicKeys: []bls.PublicKeyWrapper{}, + keyIndexMap: map[bls.SerializedPublicKey]int{}, prepare: votepower.NewRound(), commit: votepower.NewRound(), viewChange: votepower.NewRound(), diff --git a/consensus/reward/rewarder.go b/consensus/reward/rewarder.go index d70f263e8..7acad0d89 100644 --- a/consensus/reward/rewarder.go +++ b/consensus/reward/rewarder.go @@ -3,6 +3,8 @@ package reward import ( "math/big" + "github.com/harmony-one/harmony/crypto/bls" + "github.com/ethereum/go-ethereum/common" "github.com/harmony-one/harmony/shard" ) @@ -12,7 +14,7 @@ type Payout struct { ShardID uint32 Addr common.Address NewlyEarned *big.Int - EarningKey shard.BLSPublicKey + EarningKey bls.SerializedPublicKey } // CompletedRound .. diff --git a/consensus/view_change.go b/consensus/view_change.go index 125308e53..dba2ed494 100644 --- a/consensus/view_change.go +++ b/consensus/view_change.go @@ -7,13 +7,13 @@ import ( "sync" "time" - "github.com/harmony-one/harmony/shard" + "github.com/harmony-one/harmony/crypto/bls" "github.com/ethereum/go-ethereum/rlp" "github.com/harmony-one/harmony/core/types" "github.com/ethereum/go-ethereum/common" - "github.com/harmony-one/bls/ffi/go/bls" + bls_core "github.com/harmony-one/bls/ffi/go/bls" msg_pb "github.com/harmony-one/harmony/api/proto/message" "github.com/harmony-one/harmony/consensus/quorum" "github.com/harmony-one/harmony/consensus/signature" @@ -84,7 +84,7 @@ func (consensus *Consensus) switchPhase(desired FBFTPhase, override bool) { } // GetNextLeaderKey uniquely determine who is the leader for given viewID -func (consensus *Consensus) GetNextLeaderKey() *shard.BLSPublicKeyWrapper { +func (consensus *Consensus) GetNextLeaderKey() *bls.PublicKeyWrapper { wasFound, next := consensus.Decider.NextAfter(consensus.LeaderPubKey) if !wasFound { consensus.getLogger().Warn(). @@ -101,9 +101,9 @@ func (consensus *Consensus) ResetViewChangeState() { Msg("[ResetViewChangeState] Resetting view change state") consensus.current.SetMode(Normal) consensus.m1Payload = []byte{} - consensus.bhpSigs = map[uint64]map[string]*bls.Sign{} - consensus.nilSigs = map[uint64]map[string]*bls.Sign{} - consensus.viewIDSigs = map[uint64]map[string]*bls.Sign{} + consensus.bhpSigs = map[uint64]map[string]*bls_core.Sign{} + consensus.nilSigs = map[uint64]map[string]*bls_core.Sign{} + consensus.viewIDSigs = map[uint64]map[string]*bls_core.Sign{} consensus.bhpBitmap = map[uint64]*bls_cosi.Mask{} consensus.nilBitmap = map[uint64]*bls_cosi.Mask{} consensus.viewIDBitmap = map[uint64]*bls_cosi.Mask{} diff --git a/consensus/view_change_test.go b/consensus/view_change_test.go index 4b905e754..10a30d150 100644 --- a/consensus/view_change_test.go +++ b/consensus/view_change_test.go @@ -3,9 +3,10 @@ package consensus import ( "testing" - "github.com/harmony-one/bls/ffi/go/bls" + "github.com/harmony-one/harmony/crypto/bls" + + bls_core "github.com/harmony-one/bls/ffi/go/bls" harmony_bls "github.com/harmony-one/harmony/crypto/bls" - "github.com/harmony-one/harmony/shard" "github.com/stretchr/testify/assert" ) @@ -123,16 +124,16 @@ func TestGetNextLeaderKeyShouldSucceed(t *testing.T) { assert.Equal(t, int64(0), consensus.Decider.ParticipantsCount()) - blsKeys := []*bls.PublicKey{} - wrappedBLSKeys := []*shard.BLSPublicKeyWrapper{} + blsKeys := []*bls_core.PublicKey{} + wrappedBLSKeys := []*bls.PublicKeyWrapper{} keyCount := int64(5) for i := int64(0); i < keyCount; i++ { blsKey := harmony_bls.RandPrivateKey() blsPubKey := blsKey.GetPublicKey() - bytes := shard.BLSPublicKey{} + bytes := bls.SerializedPublicKey{} bytes.FromLibBLSPublicKey(blsPubKey) - wrapped := &shard.BLSPublicKeyWrapper{Object: blsPubKey, Bytes: bytes} + wrapped := &bls.PublicKeyWrapper{Object: blsPubKey, Bytes: bytes} blsKeys = append(blsKeys, blsPubKey) wrappedBLSKeys = append(wrappedBLSKeys, wrapped) diff --git a/consensus/votepower/roster.go b/consensus/votepower/roster.go index 897542818..1dd43174f 100644 --- a/consensus/votepower/roster.go +++ b/consensus/votepower/roster.go @@ -6,12 +6,14 @@ import ( "math/big" "sort" + "github.com/harmony-one/harmony/shard" + "github.com/ethereum/go-ethereum/common" - "github.com/harmony-one/bls/ffi/go/bls" + bls_core "github.com/harmony-one/bls/ffi/go/bls" + "github.com/harmony-one/harmony/crypto/bls" common2 "github.com/harmony-one/harmony/internal/common" "github.com/harmony-one/harmony/internal/utils" "github.com/harmony-one/harmony/numeric" - "github.com/harmony-one/harmony/shard" "github.com/pkg/errors" ) @@ -22,11 +24,11 @@ var ( // Ballot is a vote cast by a validator type Ballot struct { - SignerPubKey shard.BLSPublicKey `json:"bls-public-key"` - BlockHeaderHash common.Hash `json:"block-header-hash"` - Signature []byte `json:"bls-signature"` - Height uint64 `json:"block-height"` - ViewID uint64 `json:"view-id"` + SignerPubKey bls.SerializedPublicKey `json:"bls-public-key"` + BlockHeaderHash common.Hash `json:"block-header-hash"` + Signature []byte `json:"bls-signature"` + Height uint64 `json:"block-height"` + ViewID uint64 `json:"view-id"` } // MarshalJSON .. @@ -48,8 +50,8 @@ func (b Ballot) MarshalJSON() ([]byte, error) { // Round is a round of voting in any FBFT phase type Round struct { - AggregatedVote *bls.Sign - BallotBox map[shard.BLSPublicKey]*Ballot + AggregatedVote *bls_core.Sign + BallotBox map[bls.SerializedPublicKey]*Ballot } func (b Ballot) String() string { @@ -60,18 +62,18 @@ func (b Ballot) String() string { // NewRound .. func NewRound() *Round { return &Round{ - AggregatedVote: &bls.Sign{}, - BallotBox: map[shard.BLSPublicKey]*Ballot{}, + AggregatedVote: &bls_core.Sign{}, + BallotBox: map[bls.SerializedPublicKey]*Ballot{}, } } // PureStakedVote .. type PureStakedVote struct { - EarningAccount common.Address `json:"earning-account"` - Identity shard.BLSPublicKey `json:"bls-public-key"` - GroupPercent numeric.Dec `json:"group-percent"` - EffectiveStake numeric.Dec `json:"effective-stake"` - RawStake numeric.Dec `json:"raw-stake"` + EarningAccount common.Address `json:"earning-account"` + Identity bls.SerializedPublicKey `json:"bls-public-key"` + GroupPercent numeric.Dec `json:"group-percent"` + EffectiveStake numeric.Dec `json:"effective-stake"` + RawStake numeric.Dec `json:"raw-stake"` } // AccommodateHarmonyVote .. @@ -96,7 +98,7 @@ type topLevelRegistry struct { // Roster .. type Roster struct { - Voters map[shard.BLSPublicKey]*AccommodateHarmonyVote + Voters map[bls.SerializedPublicKey]*AccommodateHarmonyVote topLevelRegistry ShardID uint32 } @@ -232,7 +234,7 @@ func Compute(subComm *shard.Committee, epoch *big.Int) (*Roster, error) { // NewRoster .. func NewRoster(shardID uint32) *Roster { - m := map[shard.BLSPublicKey]*AccommodateHarmonyVote{} + m := map[bls.SerializedPublicKey]*AccommodateHarmonyVote{} return &Roster{ Voters: m, topLevelRegistry: topLevelRegistry{ diff --git a/consensus/votepower/roster_test.go b/consensus/votepower/roster_test.go index 4459fa01c..26d13564b 100644 --- a/consensus/votepower/roster_test.go +++ b/consensus/votepower/roster_test.go @@ -5,10 +5,12 @@ import ( "math/rand" "testing" + "github.com/harmony-one/harmony/crypto/bls" + shardingconfig "github.com/harmony-one/harmony/internal/configs/sharding" "github.com/ethereum/go-ethereum/common" - "github.com/harmony-one/bls/ffi/go/bls" + bls_core "github.com/harmony-one/bls/ffi/go/bls" "github.com/harmony-one/harmony/numeric" "github.com/harmony-one/harmony/shard" ) @@ -45,9 +47,9 @@ func init() { func generateRandomSlot() shard.Slot { addr := common.Address{} addr.SetBytes(big.NewInt(int64(accountGen.Int63n(maxAccountGen))).Bytes()) - secretKey := bls.SecretKey{} + secretKey := bls_core.SecretKey{} secretKey.Deserialize(big.NewInt(int64(keyGen.Int63n(maxKeyGen))).Bytes()) - key := shard.BLSPublicKey{} + key := bls.SerializedPublicKey{} key.FromLibBLSPublicKey(secretKey.GetPublicKey()) stake := numeric.NewDecFromBigInt(big.NewInt(int64(stakeGen.Int63n(maxStakeGen)))) return shard.Slot{addr, key, &stake} diff --git a/core/rawdb/accessors_indexes_test.go b/core/rawdb/accessors_indexes_test.go index 777edf314..889c4d38d 100644 --- a/core/rawdb/accessors_indexes_test.go +++ b/core/rawdb/accessors_indexes_test.go @@ -20,16 +20,17 @@ import ( "math/big" "testing" + "github.com/harmony-one/harmony/crypto/bls" + "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/ethdb" - "github.com/harmony-one/bls/ffi/go/bls" + bls_core "github.com/harmony-one/bls/ffi/go/bls" blockfactory "github.com/harmony-one/harmony/block/factory" "github.com/harmony-one/harmony/core/types" "github.com/harmony-one/harmony/crypto/hash" "github.com/harmony-one/harmony/numeric" - "github.com/harmony-one/harmony/shard" staking "github.com/harmony-one/harmony/staking/types" ) @@ -138,16 +139,16 @@ func TestMixedLookupStorage(t *testing.T) { func sampleCreateValidatorStakingTxn() *staking.StakingTransaction { key, _ := crypto.GenerateKey() stakePayloadMaker := func() (staking.Directive, interface{}) { - p := &bls.PublicKey{} + p := &bls_core.PublicKey{} p.DeserializeHexStr(testBLSPubKey) - pub := shard.BLSPublicKey{} + pub := bls.SerializedPublicKey{} pub.FromLibBLSPublicKey(p) messageBytes := []byte(staking.BLSVerificationStr) - privateKey := &bls.SecretKey{} + privateKey := &bls_core.SecretKey{} privateKey.DeserializeHexStr(testBLSPrvKey) msgHash := hash.Keccak256(messageBytes) signature := privateKey.SignHash(msgHash[:]) - var sig shard.BLSSignature + var sig bls.SerializedSignature copy(sig[:], signature.Serialize()) ra, _ := numeric.NewDecFromStr("0.7") @@ -169,8 +170,8 @@ func sampleCreateValidatorStakingTxn() *staking.StakingTransaction { MinSelfDelegation: big.NewInt(1e18), MaxTotalDelegation: big.NewInt(3e18), ValidatorAddress: crypto.PubkeyToAddress(key.PublicKey), - SlotPubKeys: []shard.BLSPublicKey{pub}, - SlotKeySigs: []shard.BLSSignature{sig}, + SlotPubKeys: []bls.SerializedPublicKey{pub}, + SlotKeySigs: []bls.SerializedSignature{sig}, Amount: big.NewInt(1e18), } } diff --git a/core/staking_verifier.go b/core/staking_verifier.go index 2ed005e4c..945c83704 100644 --- a/core/staking_verifier.go +++ b/core/staking_verifier.go @@ -4,12 +4,13 @@ import ( "bytes" "math/big" + "github.com/harmony-one/harmony/crypto/bls" + "github.com/ethereum/go-ethereum/common" "github.com/harmony-one/harmony/common/denominations" "github.com/harmony-one/harmony/core/vm" common2 "github.com/harmony-one/harmony/internal/common" "github.com/harmony-one/harmony/internal/utils" - "github.com/harmony-one/harmony/shard" "github.com/harmony-one/harmony/staking/effective" staking "github.com/harmony-one/harmony/staking/types" "github.com/pkg/errors" @@ -24,7 +25,7 @@ var ( func checkDuplicateFields( bc ChainContext, state vm.StateDB, - validator common.Address, identity string, blsKeys []shard.BLSPublicKey, + validator common.Address, identity string, blsKeys []bls.SerializedPublicKey, ) error { addrs, err := bc.ReadValidatorList() if err != nil { @@ -34,7 +35,7 @@ func checkDuplicateFields( checkIdentity := identity != "" checkBlsKeys := len(blsKeys) != 0 - blsKeyMap := map[shard.BLSPublicKey]struct{}{} + blsKeyMap := map[bls.SerializedPublicKey]struct{}{} for _, key := range blsKeys { blsKeyMap[key] = struct{}{} } @@ -140,7 +141,7 @@ func VerifyAndEditValidatorFromMsg( if !stateDB.IsValidator(msg.ValidatorAddress) { return nil, errValidatorNotExist } - newBlsKeys := []shard.BLSPublicKey{} + newBlsKeys := []bls.SerializedPublicKey{} if msg.SlotKeyToAdd != nil { newBlsKeys = append(newBlsKeys, *msg.SlotKeyToAdd) } diff --git a/core/staking_verifier_test.go b/core/staking_verifier_test.go index 908f4cac0..be4a4876d 100644 --- a/core/staking_verifier_test.go +++ b/core/staking_verifier_test.go @@ -7,16 +7,16 @@ import ( "strings" "testing" + "github.com/harmony-one/harmony/crypto/bls" + "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/ethdb" "github.com/harmony-one/harmony/block" consensus_engine "github.com/harmony-one/harmony/consensus/engine" "github.com/harmony-one/harmony/core/state" "github.com/harmony-one/harmony/core/vm" - "github.com/harmony-one/harmony/crypto/bls" "github.com/harmony-one/harmony/crypto/hash" "github.com/harmony-one/harmony/numeric" - "github.com/harmony-one/harmony/shard" "github.com/harmony-one/harmony/staking/effective" staking "github.com/harmony-one/harmony/staking/types" staketest "github.com/harmony-one/harmony/staking/types/test" @@ -90,7 +90,7 @@ func TestCheckDuplicateFields(t *testing.T) { sdb *state.DB validator common.Address identity string - pubs []shard.BLSPublicKey + pubs []bls.SerializedPublicKey expErr error }{ @@ -100,7 +100,7 @@ func TestCheckDuplicateFields(t *testing.T) { sdb: makeStateDBForStake(t), validator: createValidatorAddr, identity: makeIdentityStr("new validator"), - pubs: []shard.BLSPublicKey{blsKeys[11].pub}, + pubs: []bls.SerializedPublicKey{blsKeys[11].pub}, expErr: nil, }, @@ -110,7 +110,7 @@ func TestCheckDuplicateFields(t *testing.T) { sdb: makeStateDBForStake(t), validator: makeTestAddr(0), identity: makeIdentityStr(0), - pubs: []shard.BLSPublicKey{blsKeys[0].pub, blsKeys[1].pub}, + pubs: []bls.SerializedPublicKey{blsKeys[0].pub, blsKeys[1].pub}, expErr: nil, }, @@ -120,7 +120,7 @@ func TestCheckDuplicateFields(t *testing.T) { sdb: makeStateDBForStake(t), validator: createValidatorAddr, identity: makeIdentityStr("new validator"), - pubs: []shard.BLSPublicKey{}, + pubs: []bls.SerializedPublicKey{}, expErr: nil, }, @@ -143,7 +143,7 @@ func TestCheckDuplicateFields(t *testing.T) { }(t), validator: createValidatorAddr, identity: makeIdentityStr("new validator"), - pubs: []shard.BLSPublicKey{blsKeys[11].pub}, + pubs: []bls.SerializedPublicKey{blsKeys[11].pub}, expErr: nil, }, @@ -153,7 +153,7 @@ func TestCheckDuplicateFields(t *testing.T) { sdb: makeStateDBForStake(t), validator: createValidatorAddr, identity: makeIdentityStr("new validator"), - pubs: []shard.BLSPublicKey{blsKeys[11].pub}, + pubs: []bls.SerializedPublicKey{blsKeys[11].pub}, expErr: errors.New("error intended"), }, @@ -169,7 +169,7 @@ func TestCheckDuplicateFields(t *testing.T) { sdb: makeStateDBForStake(t), validator: createValidatorAddr, identity: makeIdentityStr("new validator"), - pubs: []shard.BLSPublicKey{blsKeys[11].pub}, + pubs: []bls.SerializedPublicKey{blsKeys[11].pub}, expErr: errors.New("address not present in state"), }, @@ -179,7 +179,7 @@ func TestCheckDuplicateFields(t *testing.T) { sdb: makeStateDBForStake(t), validator: createValidatorAddr, identity: makeIdentityStr(0), - pubs: []shard.BLSPublicKey{blsKeys[11].pub}, + pubs: []bls.SerializedPublicKey{blsKeys[11].pub}, expErr: errDupIdentity, }, @@ -189,7 +189,7 @@ func TestCheckDuplicateFields(t *testing.T) { sdb: makeStateDBForStake(t), validator: createValidatorAddr, identity: makeIdentityStr("new validator"), - pubs: []shard.BLSPublicKey{blsKeys[0].pub}, + pubs: []bls.SerializedPublicKey{blsKeys[0].pub}, expErr: errDupBlsKey, }, @@ -299,7 +299,7 @@ func TestVerifyAndCreateValidatorFromMsg(t *testing.T) { blockNum: big.NewInt(defaultBlockNumber), msg: func() staking.CreateValidator { m := defaultMsgCreateValidator() - m.SlotPubKeys = []shard.BLSPublicKey{blsKeys[0].pub} + m.SlotPubKeys = []bls.SerializedPublicKey{blsKeys[0].pub} return m }(), @@ -328,7 +328,7 @@ func TestVerifyAndCreateValidatorFromMsg(t *testing.T) { blockNum: big.NewInt(defaultBlockNumber), msg: func() staking.CreateValidator { m := defaultMsgCreateValidator() - m.SlotKeySigs = []shard.BLSSignature{blsKeys[12].sig} + m.SlotKeySigs = []bls.SerializedSignature{blsKeys[12].sig} return m }(), @@ -392,8 +392,8 @@ func defaultMsgCreateValidator() staking.CreateValidator { CommissionRates: defaultCommissionRates, MinSelfDelegation: staketest.DefaultMinSelfDel, MaxTotalDelegation: staketest.DefaultMaxTotalDel, - SlotPubKeys: []shard.BLSPublicKey{pub}, - SlotKeySigs: []shard.BLSSignature{sig}, + SlotPubKeys: []bls.SerializedPublicKey{pub}, + SlotKeySigs: []bls.SerializedSignature{sig}, Amount: staketest.DefaultDelAmount, } return cv @@ -403,7 +403,7 @@ func defaultExpWrapperCreateValidator() staking.ValidatorWrapper { pub := blsKeys[11].pub v := staking.Validator{ Address: createValidatorAddr, - SlotPubKeys: []shard.BLSPublicKey{pub}, + SlotPubKeys: []bls.SerializedPublicKey{pub}, LastEpochInCommittee: new(big.Int), MinSelfDelegation: staketest.DefaultMinSelfDel, MaxTotalDelegation: staketest.DefaultMaxTotalDel, @@ -690,9 +690,9 @@ var ( func defaultMsgEditValidator() staking.EditValidator { var ( - pub0Copy shard.BLSPublicKey - pub12Copy shard.BLSPublicKey - sig12Copy shard.BLSSignature + pub0Copy bls.SerializedPublicKey + pub12Copy bls.SerializedPublicKey + sig12Copy bls.SerializedSignature ) copy(pub0Copy[:], blsKeys[0].pub[:]) copy(pub12Copy[:], blsKeys[12].pub[:]) @@ -1353,7 +1353,7 @@ func makeVWrappersForStake(num, numPubsPerVal int) []*staking.ValidatorWrapper { func makeStateVWrapperFromGetter(index int, numPubs int, pubGetter *BLSPubGetter) staking.ValidatorWrapper { addr := makeTestAddr(index) - pubs := make([]shard.BLSPublicKey, 0, numPubs) + pubs := make([]bls.SerializedPublicKey, 0, numPubs) for i := 0; i != numPubs; i++ { pubs = append(pubs, pubGetter.getPub()) } @@ -1375,7 +1375,7 @@ func newBLSPubGetter(keys []blsPubSigPair) *BLSPubGetter { } } -func (g *BLSPubGetter) getPub() shard.BLSPublicKey { +func (g *BLSPubGetter) getPub() bls.SerializedPublicKey { key := g.keys[g.index] g.index++ return key.pub @@ -1469,8 +1469,8 @@ func makeKeyPairs(size int) []blsPubSigPair { } type blsPubSigPair struct { - pub shard.BLSPublicKey - sig shard.BLSSignature + pub bls.SerializedPublicKey + sig bls.SerializedSignature } func makeBLSKeyPair() blsPubSigPair { @@ -1479,10 +1479,10 @@ func makeBLSKeyPair() blsPubSigPair { msgHash := hash.Keccak256([]byte(staking.BLSVerificationStr)) sig := blsPriv.SignHash(msgHash) - var shardPub shard.BLSPublicKey + var shardPub bls.SerializedPublicKey copy(shardPub[:], blsPub.Serialize()) - var shardSig shard.BLSSignature + var shardSig bls.SerializedSignature copy(shardSig[:], sig.Serialize()) return blsPubSigPair{shardPub, shardSig} diff --git a/core/tx_pool_test.go b/core/tx_pool_test.go index d41b546f5..51a22f2cd 100644 --- a/core/tx_pool_test.go +++ b/core/tx_pool_test.go @@ -26,11 +26,13 @@ import ( "testing" "time" + "github.com/harmony-one/harmony/crypto/bls" + "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/ethdb" "github.com/ethereum/go-ethereum/event" - "github.com/harmony-one/bls/ffi/go/bls" + bls_core "github.com/harmony-one/bls/ffi/go/bls" blockfactory "github.com/harmony-one/harmony/block/factory" "github.com/harmony-one/harmony/common/denominations" "github.com/harmony-one/harmony/core/state" @@ -40,7 +42,6 @@ import ( chain2 "github.com/harmony-one/harmony/internal/chain" "github.com/harmony-one/harmony/internal/params" "github.com/harmony-one/harmony/numeric" - "github.com/harmony-one/harmony/shard" staking "github.com/harmony-one/harmony/staking/types" ) @@ -88,16 +89,16 @@ func (bc *testBlockChain) SubscribeChainHeadEvent(ch chan<- ChainHeadEvent) even // TODO: more staking tests in tx pool & testing lib func stakingCreateValidatorTransaction(key *ecdsa.PrivateKey) (*staking.StakingTransaction, error) { stakePayloadMaker := func() (staking.Directive, interface{}) { - p := &bls.PublicKey{} + p := &bls_core.PublicKey{} p.DeserializeHexStr(testBLSPubKey) - pub := shard.BLSPublicKey{} + pub := bls.SerializedPublicKey{} pub.FromLibBLSPublicKey(p) messageBytes := []byte(staking.BLSVerificationStr) - privateKey := &bls.SecretKey{} + privateKey := &bls_core.SecretKey{} privateKey.DeserializeHexStr(testBLSPrvKey) msgHash := hash.Keccak256(messageBytes) signature := privateKey.SignHash(msgHash[:]) - var sig shard.BLSSignature + var sig bls.SerializedSignature copy(sig[:], signature.Serialize()) ra, _ := numeric.NewDecFromStr("0.7") @@ -119,8 +120,8 @@ func stakingCreateValidatorTransaction(key *ecdsa.PrivateKey) (*staking.StakingT MinSelfDelegation: tenKOnes, MaxTotalDelegation: twelveKOnes, ValidatorAddress: crypto.PubkeyToAddress(key.PublicKey), - SlotPubKeys: []shard.BLSPublicKey{pub}, - SlotKeySigs: []shard.BLSSignature{sig}, + SlotPubKeys: []bls.SerializedPublicKey{pub}, + SlotKeySigs: []bls.SerializedSignature{sig}, Amount: tenKOnes, } } diff --git a/crypto/bls/bls.go b/crypto/bls/bls.go index a57fcff6d..1239ae02a 100644 --- a/crypto/bls/bls.go +++ b/crypto/bls/bls.go @@ -1,283 +1,89 @@ package bls import ( + "bytes" + "encoding/hex" + "math/big" + "github.com/harmony-one/bls/ffi/go/bls" - lru "github.com/hashicorp/golang-lru" "github.com/pkg/errors" ) -const ( - blsPubKeyCacheSize = 1024 +var ( + emptyBLSPubKey = SerializedPublicKey{} ) -var ( - // BLSPubKeyCache is the Cache of the Deserialized BLS PubKey - BLSPubKeyCache, _ = lru.New(blsPubKeyCacheSize) +// PublicKeySizeInBytes .. +const ( + PublicKeySizeInBytes = 48 + BLSSignatureSizeInBytes = 96 ) -func init() { - bls.Init(bls.BLS12_381) +// PrivateKeyWrapper combines the bls private key and the corresponding public key +type PrivateKeyWrapper struct { + Pri *bls.SecretKey + Pub *PublicKeyWrapper } -// RandPrivateKey returns a random private key. -func RandPrivateKey() *bls.SecretKey { - sec := bls.SecretKey{} - sec.SetByCSPRNG() - return &sec +// PublicKeyWrapper defines the bls public key in both serialized and +// deserialized form. +type PublicKeyWrapper struct { + Bytes SerializedPublicKey + Object *bls.PublicKey } -var ( - errEmptyInput = errors.New("BytesToBLSPublicKey: empty input") - errPubKeyCast = errors.New("BytesToBLSPublicKey: cast error") -) +// SerializedPublicKey defines the serialized bls public key +type SerializedPublicKey [PublicKeySizeInBytes]byte -// BytesToBLSPublicKey converts bytes into bls.PublicKey pointer. -func BytesToBLSPublicKey(bytes []byte) (*bls.PublicKey, error) { - if len(bytes) == 0 { - return nil, errEmptyInput - } - kkey := string(bytes) - if k, ok := BLSPubKeyCache.Get(kkey); ok { - if pk, ok := k.(bls.PublicKey); ok { - return &pk, nil - } - return nil, errPubKeyCast - } - pubKey := &bls.PublicKey{} - err := pubKey.Deserialize(bytes) - - if err == nil { - BLSPubKeyCache.Add(kkey, *pubKey) - return pubKey, nil - } +// SerializedSignature defines the bls signature +type SerializedSignature [BLSSignatureSizeInBytes]byte - return nil, err +// Big .. +func (pk SerializedPublicKey) Big() *big.Int { + return new(big.Int).SetBytes(pk[:]) } -// AggregateSig aggregates all the BLS signature into a single multi-signature. -func AggregateSig(sigs []*bls.Sign) *bls.Sign { - var aggregatedSig bls.Sign - for _, sig := range sigs { - aggregatedSig.Add(sig) - } - return &aggregatedSig +// IsEmpty returns whether the bls public key is empty 0 bytes +func (pk SerializedPublicKey) IsEmpty() bool { + return bytes.Equal(pk[:], emptyBLSPubKey[:]) } -// Mask represents a cosigning participation bitmask. -type Mask struct { - Bitmap []byte - Publics []*bls.PublicKey - AggregatePublic *bls.PublicKey +// Hex returns the hex string of bls public key +func (pk SerializedPublicKey) Hex() string { + return hex.EncodeToString(pk[:]) } -// NewMask returns a new participation bitmask for cosigning where all -// cosigners are disabled by default. If a public key is given it verifies that -// it is present in the list of keys and sets the corresponding index in the -// bitmask to 1 (enabled). -func NewMask(publics []*bls.PublicKey, myKey *bls.PublicKey) (*Mask, error) { - m := &Mask{ - Publics: publics, - } - m.Bitmap = make([]byte, m.Len()) - m.AggregatePublic = &bls.PublicKey{} - if myKey != nil { - found := false - for i, key := range publics { - if key.IsEqual(myKey) { - m.SetBit(i, true) - found = true - break - } - } - if !found { - return nil, errors.New("key not found") - } - } - return m, nil +// MarshalText so that we can use this as JSON printable when used as +// key in a map +func (pk SerializedPublicKey) MarshalText() (text []byte, err error) { + text = make([]byte, BLSSignatureSizeInBytes) + hex.Encode(text, pk[:]) + return text, nil } -// Clear clears the existing bits and aggregate public keys. -func (m *Mask) Clear() { - m.Bitmap = make([]byte, m.Len()) - m.AggregatePublic = &bls.PublicKey{} -} - -// Mask returns a copy of the participation bitmask. -func (m *Mask) Mask() []byte { - clone := make([]byte, len(m.Bitmap)) - copy(clone[:], m.Bitmap) - return clone -} - -// Len returns the Bitmap length in bytes. -func (m *Mask) Len() int { - return (len(m.Publics) + 7) >> 3 +// FromLibBLSPublicKeyUnsafe could give back nil, use only in cases when +// have invariant that return value won't be nil +func FromLibBLSPublicKeyUnsafe(key *bls.PublicKey) *SerializedPublicKey { + result := &SerializedPublicKey{} + if err := result.FromLibBLSPublicKey(key); err != nil { + return nil + } + return result } -// SetMask sets the participation bitmask according to the given byte slice -// interpreted in little-endian order, i.e., bits 0-7 of byte 0 correspond to -// cosigners 0-7, bits 0-7 of byte 1 correspond to cosigners 8-15, etc. -func (m *Mask) SetMask(mask []byte) error { - if m.Len() != len(mask) { +// FromLibBLSPublicKey replaces the key contents with the given key, +func (pk *SerializedPublicKey) FromLibBLSPublicKey(key *bls.PublicKey) error { + bytes := key.Serialize() + if len(bytes) != len(pk) { return errors.Errorf( - "mismatching bitmap lengths expectedBitmapLength %d providedBitmapLength %d", - m.Len(), - len(mask), + "key size (BLS) size mismatch, expected %d have %d", len(pk), len(bytes), ) } - for i := range m.Publics { - byt := i >> 3 - msk := byte(1) << uint(i&7) - if ((m.Bitmap[byt] & msk) == 0) && ((mask[byt] & msk) != 0) { - m.Bitmap[byt] ^= msk // flip bit in Bitmap from 0 to 1 - m.AggregatePublic.Add(m.Publics[i]) - } - if ((m.Bitmap[byt] & msk) != 0) && ((mask[byt] & msk) == 0) { - m.Bitmap[byt] ^= msk // flip bit in Bitmap from 1 to 0 - m.AggregatePublic.Sub(m.Publics[i]) - } - } - return nil -} - -// SetBit enables (enable: true) or disables (enable: false) the bit -// in the participation Bitmap of the given cosigner. -func (m *Mask) SetBit(i int, enable bool) error { - if i >= len(m.Publics) { - return errors.New("index out of range") - } - byt := i >> 3 - msk := byte(1) << uint(i&7) - if ((m.Bitmap[byt] & msk) == 0) && enable { - m.Bitmap[byt] ^= msk // flip bit in Bitmap from 0 to 1 - m.AggregatePublic.Add(m.Publics[i]) - } - if ((m.Bitmap[byt] & msk) != 0) && !enable { - m.Bitmap[byt] ^= msk // flip bit in Bitmap from 1 to 0 - m.AggregatePublic.Sub(m.Publics[i]) - } + copy(pk[:], bytes) return nil } -// GetPubKeyFromMask will return pubkeys which masked either zero or one depending on the flag -// it is used to show which signers are signed or not in the cosign message -func (m *Mask) GetPubKeyFromMask(flag bool) []*bls.PublicKey { - pubKeys := []*bls.PublicKey{} - for i := range m.Publics { - byt := i >> 3 - msk := byte(1) << uint(i&7) - if flag { - if (m.Bitmap[byt] & msk) != 0 { - pubKeys = append(pubKeys, m.Publics[i]) - } - } else { - if (m.Bitmap[byt] & msk) == 0 { - pubKeys = append(pubKeys, m.Publics[i]) - } - } - } - return pubKeys -} - -// IndexEnabled checks whether the given index is enabled in the Bitmap or not. -func (m *Mask) IndexEnabled(i int) (bool, error) { - if i >= len(m.Publics) { - return false, errors.New("index out of range") - } - byt := i >> 3 - msk := byte(1) << uint(i&7) - return ((m.Bitmap[byt] & msk) != 0), nil -} - -// KeyEnabled checks whether the index, corresponding to the given key, is -// enabled in the Bitmap or not. -func (m *Mask) KeyEnabled(public *bls.PublicKey) (bool, error) { - for i, key := range m.Publics { - if key.IsEqual(public) { - return m.IndexEnabled(i) - } - } - return false, errors.New("key not found") -} - -// SetKey set the bit in the Bitmap for the given cosigner -func (m *Mask) SetKey(public *bls.PublicKey, enable bool) error { - for i, key := range m.Publics { - if key.IsEqual(public) { - return m.SetBit(i, enable) - } - } - return errors.New("key not found") -} - -// CountEnabled returns the number of enabled nodes in the CoSi participation -// Bitmap. -func (m *Mask) CountEnabled() int { - // hw is hamming weight - hw := 0 - for i := range m.Publics { - byt := i >> 3 - msk := byte(1) << uint(i&7) - if (m.Bitmap[byt] & msk) != 0 { - hw++ - } - } - return hw -} - -// CountTotal returns the total number of nodes this CoSi instance knows. -func (m *Mask) CountTotal() int { - return len(m.Publics) -} - -// AggregateMasks computes the bitwise OR of the two given participation masks. -func AggregateMasks(a, b []byte) ([]byte, error) { - if len(a) != len(b) { - return nil, errors.New("mismatching Bitmap lengths") - } - m := make([]byte, len(a)) - for i := range m { - m[i] = a[i] | b[i] - } - return m, nil -} - -// Policy represents a fully customizable cosigning policy deciding what -// cosigner sets are and aren't sufficient for a collective signature to be -// considered acceptable to a verifier. The Check method may inspect the set of -// participants that cosigned by invoking cosi.Mask and/or cosi.MaskBit, and may -// use any other relevant contextual information (e.g., how security-critical -// the operation relying on the collective signature is) in determining whether -// the collective signature was produced by an acceptable set of cosigners. -type Policy interface { - Check(m *Mask) bool -} - -// CompletePolicy is the default policy requiring that all participants have -// cosigned to make a collective signature valid. -type CompletePolicy struct { -} - -// Check verifies that all participants have contributed to a collective -// signature. -func (p CompletePolicy) Check(m *Mask) bool { - return m.CountEnabled() == m.CountTotal() -} - -// ThresholdPolicy allows to specify a simple t-of-n policy requring that at -// least the given threshold number of participants t have cosigned to make a -// collective signature valid. -type ThresholdPolicy struct { - thold int -} - -// NewThresholdPolicy returns a new ThresholdPolicy with the given threshold. -func NewThresholdPolicy(thold int) *ThresholdPolicy { - return &ThresholdPolicy{thold: thold} -} - -// Check verifies that at least a threshold number of participants have -// contributed to a collective signature. -func (p ThresholdPolicy) Check(m *Mask) bool { - return m.CountEnabled() >= p.thold +// ToLibBLSPublicKey copies the key contents into the given key. +func (pk *SerializedPublicKey) ToLibBLSPublicKey(key *bls.PublicKey) error { + return key.Deserialize(pk[:]) } diff --git a/crypto/bls/mask.go b/crypto/bls/mask.go new file mode 100644 index 000000000..a57fcff6d --- /dev/null +++ b/crypto/bls/mask.go @@ -0,0 +1,283 @@ +package bls + +import ( + "github.com/harmony-one/bls/ffi/go/bls" + lru "github.com/hashicorp/golang-lru" + "github.com/pkg/errors" +) + +const ( + blsPubKeyCacheSize = 1024 +) + +var ( + // BLSPubKeyCache is the Cache of the Deserialized BLS PubKey + BLSPubKeyCache, _ = lru.New(blsPubKeyCacheSize) +) + +func init() { + bls.Init(bls.BLS12_381) +} + +// RandPrivateKey returns a random private key. +func RandPrivateKey() *bls.SecretKey { + sec := bls.SecretKey{} + sec.SetByCSPRNG() + return &sec +} + +var ( + errEmptyInput = errors.New("BytesToBLSPublicKey: empty input") + errPubKeyCast = errors.New("BytesToBLSPublicKey: cast error") +) + +// BytesToBLSPublicKey converts bytes into bls.PublicKey pointer. +func BytesToBLSPublicKey(bytes []byte) (*bls.PublicKey, error) { + if len(bytes) == 0 { + return nil, errEmptyInput + } + kkey := string(bytes) + if k, ok := BLSPubKeyCache.Get(kkey); ok { + if pk, ok := k.(bls.PublicKey); ok { + return &pk, nil + } + return nil, errPubKeyCast + } + pubKey := &bls.PublicKey{} + err := pubKey.Deserialize(bytes) + + if err == nil { + BLSPubKeyCache.Add(kkey, *pubKey) + return pubKey, nil + } + + return nil, err +} + +// AggregateSig aggregates all the BLS signature into a single multi-signature. +func AggregateSig(sigs []*bls.Sign) *bls.Sign { + var aggregatedSig bls.Sign + for _, sig := range sigs { + aggregatedSig.Add(sig) + } + return &aggregatedSig +} + +// Mask represents a cosigning participation bitmask. +type Mask struct { + Bitmap []byte + Publics []*bls.PublicKey + AggregatePublic *bls.PublicKey +} + +// NewMask returns a new participation bitmask for cosigning where all +// cosigners are disabled by default. If a public key is given it verifies that +// it is present in the list of keys and sets the corresponding index in the +// bitmask to 1 (enabled). +func NewMask(publics []*bls.PublicKey, myKey *bls.PublicKey) (*Mask, error) { + m := &Mask{ + Publics: publics, + } + m.Bitmap = make([]byte, m.Len()) + m.AggregatePublic = &bls.PublicKey{} + if myKey != nil { + found := false + for i, key := range publics { + if key.IsEqual(myKey) { + m.SetBit(i, true) + found = true + break + } + } + if !found { + return nil, errors.New("key not found") + } + } + return m, nil +} + +// Clear clears the existing bits and aggregate public keys. +func (m *Mask) Clear() { + m.Bitmap = make([]byte, m.Len()) + m.AggregatePublic = &bls.PublicKey{} +} + +// Mask returns a copy of the participation bitmask. +func (m *Mask) Mask() []byte { + clone := make([]byte, len(m.Bitmap)) + copy(clone[:], m.Bitmap) + return clone +} + +// Len returns the Bitmap length in bytes. +func (m *Mask) Len() int { + return (len(m.Publics) + 7) >> 3 +} + +// SetMask sets the participation bitmask according to the given byte slice +// interpreted in little-endian order, i.e., bits 0-7 of byte 0 correspond to +// cosigners 0-7, bits 0-7 of byte 1 correspond to cosigners 8-15, etc. +func (m *Mask) SetMask(mask []byte) error { + if m.Len() != len(mask) { + return errors.Errorf( + "mismatching bitmap lengths expectedBitmapLength %d providedBitmapLength %d", + m.Len(), + len(mask), + ) + } + for i := range m.Publics { + byt := i >> 3 + msk := byte(1) << uint(i&7) + if ((m.Bitmap[byt] & msk) == 0) && ((mask[byt] & msk) != 0) { + m.Bitmap[byt] ^= msk // flip bit in Bitmap from 0 to 1 + m.AggregatePublic.Add(m.Publics[i]) + } + if ((m.Bitmap[byt] & msk) != 0) && ((mask[byt] & msk) == 0) { + m.Bitmap[byt] ^= msk // flip bit in Bitmap from 1 to 0 + m.AggregatePublic.Sub(m.Publics[i]) + } + } + return nil +} + +// SetBit enables (enable: true) or disables (enable: false) the bit +// in the participation Bitmap of the given cosigner. +func (m *Mask) SetBit(i int, enable bool) error { + if i >= len(m.Publics) { + return errors.New("index out of range") + } + byt := i >> 3 + msk := byte(1) << uint(i&7) + if ((m.Bitmap[byt] & msk) == 0) && enable { + m.Bitmap[byt] ^= msk // flip bit in Bitmap from 0 to 1 + m.AggregatePublic.Add(m.Publics[i]) + } + if ((m.Bitmap[byt] & msk) != 0) && !enable { + m.Bitmap[byt] ^= msk // flip bit in Bitmap from 1 to 0 + m.AggregatePublic.Sub(m.Publics[i]) + } + return nil +} + +// GetPubKeyFromMask will return pubkeys which masked either zero or one depending on the flag +// it is used to show which signers are signed or not in the cosign message +func (m *Mask) GetPubKeyFromMask(flag bool) []*bls.PublicKey { + pubKeys := []*bls.PublicKey{} + for i := range m.Publics { + byt := i >> 3 + msk := byte(1) << uint(i&7) + if flag { + if (m.Bitmap[byt] & msk) != 0 { + pubKeys = append(pubKeys, m.Publics[i]) + } + } else { + if (m.Bitmap[byt] & msk) == 0 { + pubKeys = append(pubKeys, m.Publics[i]) + } + } + } + return pubKeys +} + +// IndexEnabled checks whether the given index is enabled in the Bitmap or not. +func (m *Mask) IndexEnabled(i int) (bool, error) { + if i >= len(m.Publics) { + return false, errors.New("index out of range") + } + byt := i >> 3 + msk := byte(1) << uint(i&7) + return ((m.Bitmap[byt] & msk) != 0), nil +} + +// KeyEnabled checks whether the index, corresponding to the given key, is +// enabled in the Bitmap or not. +func (m *Mask) KeyEnabled(public *bls.PublicKey) (bool, error) { + for i, key := range m.Publics { + if key.IsEqual(public) { + return m.IndexEnabled(i) + } + } + return false, errors.New("key not found") +} + +// SetKey set the bit in the Bitmap for the given cosigner +func (m *Mask) SetKey(public *bls.PublicKey, enable bool) error { + for i, key := range m.Publics { + if key.IsEqual(public) { + return m.SetBit(i, enable) + } + } + return errors.New("key not found") +} + +// CountEnabled returns the number of enabled nodes in the CoSi participation +// Bitmap. +func (m *Mask) CountEnabled() int { + // hw is hamming weight + hw := 0 + for i := range m.Publics { + byt := i >> 3 + msk := byte(1) << uint(i&7) + if (m.Bitmap[byt] & msk) != 0 { + hw++ + } + } + return hw +} + +// CountTotal returns the total number of nodes this CoSi instance knows. +func (m *Mask) CountTotal() int { + return len(m.Publics) +} + +// AggregateMasks computes the bitwise OR of the two given participation masks. +func AggregateMasks(a, b []byte) ([]byte, error) { + if len(a) != len(b) { + return nil, errors.New("mismatching Bitmap lengths") + } + m := make([]byte, len(a)) + for i := range m { + m[i] = a[i] | b[i] + } + return m, nil +} + +// Policy represents a fully customizable cosigning policy deciding what +// cosigner sets are and aren't sufficient for a collective signature to be +// considered acceptable to a verifier. The Check method may inspect the set of +// participants that cosigned by invoking cosi.Mask and/or cosi.MaskBit, and may +// use any other relevant contextual information (e.g., how security-critical +// the operation relying on the collective signature is) in determining whether +// the collective signature was produced by an acceptable set of cosigners. +type Policy interface { + Check(m *Mask) bool +} + +// CompletePolicy is the default policy requiring that all participants have +// cosigned to make a collective signature valid. +type CompletePolicy struct { +} + +// Check verifies that all participants have contributed to a collective +// signature. +func (p CompletePolicy) Check(m *Mask) bool { + return m.CountEnabled() == m.CountTotal() +} + +// ThresholdPolicy allows to specify a simple t-of-n policy requring that at +// least the given threshold number of participants t have cosigned to make a +// collective signature valid. +type ThresholdPolicy struct { + thold int +} + +// NewThresholdPolicy returns a new ThresholdPolicy with the given threshold. +func NewThresholdPolicy(thold int) *ThresholdPolicy { + return &ThresholdPolicy{thold: thold} +} + +// Check verifies that at least a threshold number of participants have +// contributed to a collective signature. +func (p ThresholdPolicy) Check(m *Mask) bool { + return m.CountEnabled() >= p.thold +} diff --git a/crypto/bls/bls_test.go b/crypto/bls/mask_test.go similarity index 100% rename from crypto/bls/bls_test.go rename to crypto/bls/mask_test.go diff --git a/internal/chain/sig.go b/internal/chain/sig.go index 11c354f96..aa4f84be6 100644 --- a/internal/chain/sig.go +++ b/internal/chain/sig.go @@ -3,14 +3,14 @@ package chain import ( "errors" - "github.com/harmony-one/bls/ffi/go/bls" + bls_core "github.com/harmony-one/bls/ffi/go/bls" - bls2 "github.com/harmony-one/harmony/crypto/bls" + "github.com/harmony-one/harmony/crypto/bls" "github.com/harmony-one/harmony/internal/utils" ) // ReadSignatureBitmapByPublicKeys read the payload of signature and bitmap based on public keys -func ReadSignatureBitmapByPublicKeys(recvPayload []byte, publicKeys []*bls.PublicKey) (*bls.Sign, *bls2.Mask, error) { +func ReadSignatureBitmapByPublicKeys(recvPayload []byte, publicKeys []*bls_core.PublicKey) (*bls_core.Sign, *bls.Mask, error) { if len(recvPayload) < 96 { return nil, nil, errors.New("payload not have enough length") } @@ -24,12 +24,12 @@ func ReadSignatureBitmapByPublicKeys(recvPayload []byte, publicKeys []*bls.Publi bitmap := payload[offset:] //#### END Read payload data - aggSig := bls.Sign{} + aggSig := bls_core.Sign{} err := aggSig.Deserialize(multiSig) if err != nil { return nil, nil, errors.New("unable to deserialize multi-signature from payload") } - mask, err := bls2.NewMask(publicKeys, nil) + mask, err := bls.NewMask(publicKeys, nil) if err != nil { utils.Logger().Warn().Err(err).Msg("onNewView unable to setup mask for prepared message") return nil, nil, errors.New("unable to setup mask from payload") diff --git a/internal/configs/node/config.go b/internal/configs/node/config.go index 9b9a2e14f..df7203cc2 100644 --- a/internal/configs/node/config.go +++ b/internal/configs/node/config.go @@ -9,11 +9,12 @@ import ( "strings" "sync" - "github.com/harmony-one/bls/ffi/go/bls" + "github.com/harmony-one/harmony/crypto/bls" + + bls_core "github.com/harmony-one/bls/ffi/go/bls" shardingconfig "github.com/harmony-one/harmony/internal/configs/sharding" "github.com/harmony-one/harmony/internal/params" "github.com/harmony-one/harmony/multibls" - "github.com/harmony-one/harmony/shard" "github.com/harmony-one/harmony/webhooks" p2p_crypto "github.com/libp2p/go-libp2p-core/crypto" "github.com/pkg/errors" @@ -251,8 +252,8 @@ func SetShardingSchedule(schedule shardingconfig.Schedule) { } // ShardIDFromKey returns the shard ID statically determined from the input key -func (conf *ConfigType) ShardIDFromKey(key *bls.PublicKey) (uint32, error) { - var pubKey shard.BLSPublicKey +func (conf *ConfigType) ShardIDFromKey(key *bls_core.PublicKey) (uint32, error) { + var pubKey bls.SerializedPublicKey if err := pubKey.FromLibBLSPublicKey(key); err != nil { return 0, errors.Wrapf(err, "cannot convert libbls public key %s to internal form", diff --git a/internal/configs/node/config_test.go b/internal/configs/node/config_test.go index 537e7d8f9..f7fc6bef4 100644 --- a/internal/configs/node/config_test.go +++ b/internal/configs/node/config_test.go @@ -3,11 +3,11 @@ package nodeconfig import ( "testing" - "github.com/harmony-one/harmony/multibls" - "github.com/harmony-one/harmony/shard" + "github.com/harmony-one/harmony/crypto/bls" "github.com/harmony-one/harmony/internal/blsgen" shardingconfig "github.com/harmony-one/harmony/internal/configs/sharding" + "github.com/harmony-one/harmony/multibls" "github.com/pkg/errors" ) @@ -72,12 +72,12 @@ func TestValidateConsensusKeysForSameShard(t *testing.T) { t.Error(err) } keys := multibls.PublicKeys{} - dummyKey := shard.BLSPublicKey{} + dummyKey := bls.SerializedPublicKey{} dummyKey.FromLibBLSPublicKey(pubKey1) - keys = append(keys, shard.BLSPublicKeyWrapper{Object: pubKey1, Bytes: dummyKey}) - dummyKey = shard.BLSPublicKey{} + keys = append(keys, bls.PublicKeyWrapper{Object: pubKey1, Bytes: dummyKey}) + dummyKey = bls.SerializedPublicKey{} dummyKey.FromLibBLSPublicKey(pubKey2) - keys = append(keys, shard.BLSPublicKeyWrapper{Object: pubKey2, Bytes: dummyKey}) + keys = append(keys, bls.PublicKeyWrapper{Object: pubKey2, Bytes: dummyKey}) if err := GetDefaultConfig().ValidateConsensusKeysForSameShard(keys, 0); err != nil { t.Error("expected", nil, "got", err) } @@ -88,9 +88,9 @@ func TestValidateConsensusKeysForSameShard(t *testing.T) { if err != nil { t.Error(err) } - dummyKey = shard.BLSPublicKey{} + dummyKey = bls.SerializedPublicKey{} dummyKey.FromLibBLSPublicKey(pubKey3) - keys = append(keys, shard.BLSPublicKeyWrapper{Object: pubKey3, Bytes: dummyKey}) + keys = append(keys, bls.PublicKeyWrapper{Object: pubKey3, Bytes: dummyKey}) if err := GetDefaultConfig().ValidateConsensusKeysForSameShard(keys, 0); err == nil { e := errors.New("bls keys do not belong to the same shard") t.Error("expected", e, "got", nil) diff --git a/internal/hmyapi/common/hacks.go b/internal/hmyapi/common/hacks.go index 7de12f82c..6eeebd756 100644 --- a/internal/hmyapi/common/hacks.go +++ b/internal/hmyapi/common/hacks.go @@ -2,16 +2,16 @@ package common import ( "github.com/harmony-one/harmony/consensus/quorum" + "github.com/harmony-one/harmony/crypto/bls" "github.com/harmony-one/harmony/numeric" - "github.com/harmony-one/harmony/shard" ) type setRawStakeHack interface { - SetRawStake(key shard.BLSPublicKey, d numeric.Dec) + SetRawStake(key bls.SerializedPublicKey, d numeric.Dec) } // SetRawStake is a hack, return value is if was successful or not at setting -func SetRawStake(q quorum.Decider, key shard.BLSPublicKey, d numeric.Dec) bool { +func SetRawStake(q quorum.Decider, key bls.SerializedPublicKey, d numeric.Dec) bool { if setter, ok := q.(setRawStakeHack); ok { setter.SetRawStake(key, d) return true diff --git a/internal/utils/utils.go b/internal/utils/utils.go index 51e5bc05a..a5113a420 100644 --- a/internal/utils/utils.go +++ b/internal/utils/utils.go @@ -13,8 +13,8 @@ import ( "sync" "github.com/ethereum/go-ethereum/common" - "github.com/harmony-one/bls/ffi/go/bls" - bls2 "github.com/harmony-one/harmony/crypto/bls" + bls_core "github.com/harmony-one/bls/ffi/go/bls" + "github.com/harmony-one/harmony/crypto/bls" p2p_crypto "github.com/libp2p/go-libp2p-core/crypto" "github.com/pkg/errors" ) @@ -28,7 +28,7 @@ type PrivKeyStore struct { } func init() { - bls.Init(bls.BLS12_381) + bls_core.Init(bls_core.BLS12_381) for _, cidr := range []string{ "127.0.0.0/8", // IPv4 loopback @@ -68,7 +68,7 @@ func GetUniqueIDFromIPPort(ip, port string) uint32 { } // GetAddressFromBLSPubKey return the address object from bls pub key. -func GetAddressFromBLSPubKey(pubKey *bls.PublicKey) common.Address { +func GetAddressFromBLSPubKey(pubKey *bls_core.PublicKey) common.Address { addr := common.Address{} addrBytes := pubKey.GetAddress() addr.SetBytes(addrBytes[:]) @@ -77,7 +77,7 @@ func GetAddressFromBLSPubKey(pubKey *bls.PublicKey) common.Address { // GetAddressFromBLSPubKeyBytes return the address object from bls pub key. func GetAddressFromBLSPubKeyBytes(pubKeyBytes []byte) common.Address { - pubKey, err := bls2.BytesToBLSPublicKey(pubKeyBytes[:]) + pubKey, err := bls.BytesToBLSPublicKey(pubKeyBytes[:]) addr := common.Address{} if err == nil { addrBytes := pubKey.GetAddress() diff --git a/multibls/multibls.go b/multibls/multibls.go index 4faf31f43..f1e9ed356 100644 --- a/multibls/multibls.go +++ b/multibls/multibls.go @@ -3,16 +3,16 @@ package multibls import ( "strings" - "github.com/harmony-one/harmony/shard" + "github.com/harmony-one/harmony/crypto/bls" - "github.com/harmony-one/bls/ffi/go/bls" + bls_core "github.com/harmony-one/bls/ffi/go/bls" ) // PrivateKeys stores the bls secret keys that belongs to the node -type PrivateKeys []shard.BLSPrivateKeyWrapper +type PrivateKeys []bls.PrivateKeyWrapper // PublicKeys stores the bls public keys that belongs to the node -type PublicKeys []shard.BLSPublicKeyWrapper +type PublicKeys []bls.PublicKeyWrapper // SerializeToHexStr wrapper func (multiKey PublicKeys) SerializeToHexStr() string { @@ -27,7 +27,7 @@ func (multiKey PublicKeys) SerializeToHexStr() string { } // Contains wrapper -func (multiKey PublicKeys) Contains(pubKey *bls.PublicKey) bool { +func (multiKey PublicKeys) Contains(pubKey *bls_core.PublicKey) bool { for _, key := range multiKey { if key.Object.IsEqual(pubKey) { return true @@ -38,7 +38,7 @@ func (multiKey PublicKeys) Contains(pubKey *bls.PublicKey) bool { // GetPublicKeys wrapper func (multiKey PrivateKeys) GetPublicKeys() PublicKeys { - pubKeys := make([]shard.BLSPublicKeyWrapper, len(multiKey)) + pubKeys := make([]bls.PublicKeyWrapper, len(multiKey)) for i, key := range multiKey { pubKeys[i] = *key.Pub } @@ -47,9 +47,9 @@ func (multiKey PrivateKeys) GetPublicKeys() PublicKeys { } // GetPrivateKeys creates a multibls PrivateKeys using bls.SecretKey -func GetPrivateKeys(key *bls.SecretKey) PrivateKeys { +func GetPrivateKeys(key *bls_core.SecretKey) PrivateKeys { pub := key.GetPublicKey() - pubWrapper := shard.BLSPublicKeyWrapper{Object: pub} + pubWrapper := bls.PublicKeyWrapper{Object: pub} pubWrapper.Bytes.FromLibBLSPublicKey(pub) - return PrivateKeys{shard.BLSPrivateKeyWrapper{Pri: key, Pub: &pubWrapper}} + return PrivateKeys{bls.PrivateKeyWrapper{Pri: key, Pub: &pubWrapper}} } diff --git a/node/node.go b/node/node.go index 084b94f40..19bfcaef1 100644 --- a/node/node.go +++ b/node/node.go @@ -10,9 +10,11 @@ import ( "sync" "time" + "github.com/harmony-one/harmony/crypto/bls" + "github.com/ethereum/go-ethereum/common" "github.com/harmony-one/abool" - "github.com/harmony-one/bls/ffi/go/bls" + bls_core "github.com/harmony-one/bls/ffi/go/bls" "github.com/harmony-one/harmony/api/client" msg_pb "github.com/harmony-one/harmony/api/proto/message" proto_node "github.com/harmony-one/harmony/api/proto/node" @@ -685,7 +687,7 @@ func (node *Node) populateSelfAddresses(epoch *big.Int) { for _, blskey := range node.Consensus.GetPublicKeys() { blsStr := blskey.Bytes.Hex() - shardkey := shard.FromLibBLSPublicKeyUnsafe(blskey.Object) + shardkey := bls.FromLibBLSPublicKeyUnsafe(blskey.Object) if shardkey == nil { utils.Logger().Error(). Int64("epoch", epoch.Int64()). @@ -714,7 +716,7 @@ func (node *Node) populateSelfAddresses(epoch *big.Int) { } // GetAddressForBLSKey retrieves the ECDSA address associated with bls key for epoch -func (node *Node) GetAddressForBLSKey(blskey *bls.PublicKey, epoch *big.Int) common.Address { +func (node *Node) GetAddressForBLSKey(blskey *bls_core.PublicKey, epoch *big.Int) common.Address { // populate if first time setting or new epoch node.keysToAddrsMutex.Lock() defer node.keysToAddrsMutex.Unlock() diff --git a/node/node_test.go b/node/node_test.go index 70160eaca..956cd5679 100644 --- a/node/node_test.go +++ b/node/node_test.go @@ -7,7 +7,7 @@ import ( "github.com/harmony-one/harmony/consensus" "github.com/harmony-one/harmony/consensus/quorum" - bls2 "github.com/harmony-one/harmony/crypto/bls" + "github.com/harmony-one/harmony/crypto/bls" "github.com/harmony-one/harmony/internal/shardchain" "github.com/harmony-one/harmony/internal/utils" "github.com/harmony-one/harmony/multibls" @@ -19,7 +19,7 @@ import ( var testDBFactory = &shardchain.MemDBFactory{} func TestNewNode(t *testing.T) { - blsKey := bls2.RandPrivateKey() + blsKey := bls.RandPrivateKey() pubKey := blsKey.GetPublicKey() leader := p2p.Peer{IP: "127.0.0.1", Port: "8882", ConsensusPubKey: pubKey} priKey, _, _ := utils.GenKeyP2P("127.0.0.1", "9902") @@ -172,8 +172,8 @@ func makeLocalSyncingPeerProvider() *LocalSyncingPeerProvider { } func TestAddBeaconPeer(t *testing.T) { - pubKey1 := bls2.RandPrivateKey().GetPublicKey() - pubKey2 := bls2.RandPrivateKey().GetPublicKey() + pubKey1 := bls.RandPrivateKey().GetPublicKey() + pubKey2 := bls.RandPrivateKey().GetPublicKey() peers1 := []*p2p.Peer{ { @@ -189,7 +189,7 @@ func TestAddBeaconPeer(t *testing.T) { PeerID: "4567", }, } - blsKey := bls2.RandPrivateKey() + blsKey := bls.RandPrivateKey() pubKey := blsKey.GetPublicKey() leader := p2p.Peer{IP: "127.0.0.1", Port: "8982", ConsensusPubKey: pubKey} priKey, _, _ := utils.GenKeyP2P("127.0.0.1", "9902") diff --git a/shard/committee/assignment.go b/shard/committee/assignment.go index 01f8ca4c1..32c1f9fa7 100644 --- a/shard/committee/assignment.go +++ b/shard/committee/assignment.go @@ -4,8 +4,10 @@ import ( "encoding/json" "math/big" + "github.com/harmony-one/harmony/crypto/bls" + "github.com/ethereum/go-ethereum/common" - "github.com/harmony-one/bls/ffi/go/bls" + bls_core "github.com/harmony-one/bls/ffi/go/bls" "github.com/harmony-one/harmony/block" "github.com/harmony-one/harmony/core/types" common2 "github.com/harmony-one/harmony/internal/common" @@ -125,18 +127,18 @@ func prepareOrders( stakedReader StakingCandidatesReader, ) (map[common.Address]*effective.SlotOrder, error) { candidates := stakedReader.ValidatorCandidates() - blsKeys := map[shard.BLSPublicKey]struct{}{} + blsKeys := map[bls.SerializedPublicKey]struct{}{} essentials := map[common.Address]*effective.SlotOrder{} totalStaked, tempZero := big.NewInt(0), numeric.ZeroDec() // Avoid duplicate BLS keys as harmony nodes instance := shard.Schedule.InstanceForEpoch(stakedReader.CurrentBlock().Epoch()) for _, account := range instance.HmyAccounts() { - pub := &bls.PublicKey{} + pub := &bls_core.PublicKey{} if err := pub.DeserializeHexStr(account.BLSPublicKey); err != nil { continue } - pubKey := shard.BLSPublicKey{} + pubKey := bls.SerializedPublicKey{} if err := pubKey.FromLibBLSPublicKey(pub); err != nil { continue } @@ -267,9 +269,9 @@ func preStakingEnabledCommittee(s shardingconfig.Instance) *shard.State { com := shard.Committee{ShardID: uint32(i)} for j := 0; j < shardHarmonyNodes; j++ { index := i + j*shardNum // The initial account to use for genesis nodes - pub := &bls.PublicKey{} + pub := &bls_core.PublicKey{} pub.DeserializeHexStr(hmyAccounts[index].BLSPublicKey) - pubKey := shard.BLSPublicKey{} + pubKey := bls.SerializedPublicKey{} pubKey.FromLibBLSPublicKey(pub) // TODO: directly read address for bls too curNodeID := shard.Slot{ @@ -282,9 +284,9 @@ func preStakingEnabledCommittee(s shardingconfig.Instance) *shard.State { // add FN runner's key for j := shardHarmonyNodes; j < shardSize; j++ { index := i + (j-shardHarmonyNodes)*shardNum - pub := &bls.PublicKey{} + pub := &bls_core.PublicKey{} pub.DeserializeHexStr(fnAccounts[index].BLSPublicKey) - pubKey := shard.BLSPublicKey{} + pubKey := bls.SerializedPublicKey{} pubKey.FromLibBLSPublicKey(pub) // TODO: directly read address for bls too curNodeID := shard.Slot{ @@ -312,11 +314,11 @@ func eposStakedCommittee( shardState.Shards[i] = shard.Committee{uint32(i), shard.SlotList{}} for j := 0; j < shardHarmonyNodes; j++ { index := i + j*shardCount - pub := &bls.PublicKey{} + pub := &bls_core.PublicKey{} if err := pub.DeserializeHexStr(hAccounts[index].BLSPublicKey); err != nil { return nil, err } - pubKey := shard.BLSPublicKey{} + pubKey := bls.SerializedPublicKey{} if err := pubKey.FromLibBLSPublicKey(pub); err != nil { return nil, err } diff --git a/shard/shard_state.go b/shard/shard_state.go index 97033f2d9..49fa565af 100644 --- a/shard/shard_state.go +++ b/shard/shard_state.go @@ -8,9 +8,11 @@ import ( "sort" "time" + bls_core "github.com/harmony-one/bls/ffi/go/bls" + "github.com/harmony-one/harmony/crypto/bls" + "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/rlp" - "github.com/harmony-one/bls/ffi/go/bls" "github.com/harmony-one/harmony/crypto/hash" common2 "github.com/harmony-one/harmony/internal/common" "github.com/harmony-one/harmony/numeric" @@ -20,46 +22,21 @@ import ( ) var ( - emptyBLSPubKey = BLSPublicKey{} + emptyBLSPubKey = bls.SerializedPublicKey{} // ErrShardIDNotInSuperCommittee .. ErrShardIDNotInSuperCommittee = errors.New("shardID not in super committee") ) -// PublicKeySizeInBytes .. -const ( - PublicKeySizeInBytes = 48 - BLSSignatureSizeInBytes = 96 -) - // State is the collection of all committees type State struct { Epoch *big.Int `json:"epoch"` Shards []Committee `json:"shards"` } -// BLSPrivateKeyWrapper combines the bls private key and the corresponding public key -type BLSPrivateKeyWrapper struct { - Pri *bls.SecretKey - Pub *BLSPublicKeyWrapper -} - -// BLSPublicKeyWrapper defines the bls public key in both serialized and -// deserialized form. -type BLSPublicKeyWrapper struct { - Bytes BLSPublicKey - Object *bls.PublicKey -} - -// BLSPublicKey defines the serialized bls public key -type BLSPublicKey [PublicKeySizeInBytes]byte - -// BLSSignature defines the bls signature -type BLSSignature [BLSSignatureSizeInBytes]byte - // Slot represents node id (BLS address) type Slot struct { - EcdsaAddress common.Address `json:"ecdsa-address"` - BLSPublicKey BLSPublicKey `json:"bls-pubkey"` + EcdsaAddress common.Address `json:"ecdsa-address"` + BLSPublicKey bls.SerializedPublicKey `json:"bls-pubkey"` // nil means our node, 0 means not active, > 0 means staked node EffectiveStake *numeric.Dec `json:"effective-stake" rlp:"nil"` } @@ -92,8 +69,8 @@ type StateLegacy []CommitteeLegacy // SlotLegacy represents node id (BLS address) type SlotLegacy struct { - EcdsaAddress common.Address `json:"ecdsa-address"` - BLSPublicKey BLSPublicKey `json:"bls-pubkey"` + EcdsaAddress common.Address `json:"ecdsa-address"` + BLSPublicKey bls.SerializedPublicKey `json:"bls-pubkey"` } // SlotListLegacy is a list of SlotList. @@ -307,58 +284,8 @@ func (ss *State) DeepCopy() *State { return &r } -// Big .. -func (pk BLSPublicKey) Big() *big.Int { - return new(big.Int).SetBytes(pk[:]) -} - -// IsEmpty returns whether the bls public key is empty 0 bytes -func (pk BLSPublicKey) IsEmpty() bool { - return bytes.Equal(pk[:], emptyBLSPubKey[:]) -} - -// Hex returns the hex string of bls public key -func (pk BLSPublicKey) Hex() string { - return hex.EncodeToString(pk[:]) -} - -// MarshalText so that we can use this as JSON printable when used as -// key in a map -func (pk BLSPublicKey) MarshalText() (text []byte, err error) { - text = make([]byte, BLSSignatureSizeInBytes) - hex.Encode(text, pk[:]) - return text, nil -} - -// FromLibBLSPublicKeyUnsafe could give back nil, use only in cases when -// have invariant that return value won't be nil -func FromLibBLSPublicKeyUnsafe(key *bls.PublicKey) *BLSPublicKey { - result := &BLSPublicKey{} - if err := result.FromLibBLSPublicKey(key); err != nil { - return nil - } - return result -} - -// FromLibBLSPublicKey replaces the key contents with the given key, -func (pk *BLSPublicKey) FromLibBLSPublicKey(key *bls.PublicKey) error { - bytes := key.Serialize() - if len(bytes) != len(pk) { - return errors.Errorf( - "key size (BLS) size mismatch, expected %d have %d", len(pk), len(bytes), - ) - } - copy(pk[:], bytes) - return nil -} - -// ToLibBLSPublicKey copies the key contents into the given key. -func (pk *BLSPublicKey) ToLibBLSPublicKey(key *bls.PublicKey) error { - return key.Deserialize(pk[:]) -} - -// CompareBLSPublicKey compares two BLSPublicKey, lexicographically. -func CompareBLSPublicKey(k1, k2 BLSPublicKey) int { +// CompareBLSPublicKey compares two SerializedPublicKey, lexicographically. +func CompareBLSPublicKey(k1, k2 bls.SerializedPublicKey) int { return bytes.Compare(k1[:], k2[:]) } @@ -417,13 +344,13 @@ var ( func lookupBLSPublicKeys( c *Committee, -) ([]*bls.PublicKey, error) { +) ([]*bls_core.PublicKey, error) { key := c.Hash().Hex() results, err, _ := blsKeyCache.Do( key, func() (interface{}, error) { - slice := make([]*bls.PublicKey, len(c.Slots)) + slice := make([]*bls_core.PublicKey, len(c.Slots)) for j := range c.Slots { - committerKey := &bls.PublicKey{} + committerKey := &bls_core.PublicKey{} if err := c.Slots[j].BLSPublicKey.ToLibBLSPublicKey( committerKey, ); err != nil { @@ -443,11 +370,11 @@ func lookupBLSPublicKeys( return nil, err } - return results.([]*bls.PublicKey), nil + return results.([]*bls_core.PublicKey), nil } // BLSPublicKeys .. -func (c *Committee) BLSPublicKeys() ([]*bls.PublicKey, error) { +func (c *Committee) BLSPublicKeys() ([]*bls_core.PublicKey, error) { if c == nil { return nil, ErrSubCommitteeNil } @@ -464,7 +391,7 @@ var ( ) // AddressForBLSKey .. -func (c *Committee) AddressForBLSKey(key BLSPublicKey) (*common.Address, error) { +func (c *Committee) AddressForBLSKey(key bls.SerializedPublicKey) (*common.Address, error) { if c == nil { return nil, ErrSubCommitteeNil } diff --git a/staking/availability/measure.go b/staking/availability/measure.go index 1643254cd..2ff8112c5 100644 --- a/staking/availability/measure.go +++ b/staking/availability/measure.go @@ -4,7 +4,7 @@ import ( "math/big" "github.com/ethereum/go-ethereum/common" - bls2 "github.com/harmony-one/harmony/crypto/bls" + "github.com/harmony-one/harmony/crypto/bls" "github.com/harmony-one/harmony/internal/utils" "github.com/harmony-one/harmony/numeric" "github.com/harmony-one/harmony/shard" @@ -27,7 +27,7 @@ func BlockSigners( if err != nil { return nil, nil, err } - mask, err := bls2.NewMask(committerKeys, nil) + mask, err := bls.NewMask(committerKeys, nil) if err != nil { return nil, nil, err } diff --git a/staking/availability/measure_test.go b/staking/availability/measure_test.go index 104056e20..7c0edaae9 100644 --- a/staking/availability/measure_test.go +++ b/staking/availability/measure_test.go @@ -7,9 +7,10 @@ import ( "reflect" "testing" + "github.com/harmony-one/harmony/crypto/bls" + "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/rlp" - "github.com/harmony-one/harmony/crypto/bls" "github.com/harmony-one/harmony/numeric" "github.com/harmony-one/harmony/shard" "github.com/harmony-one/harmony/staking/effective" @@ -577,7 +578,7 @@ func newTestStateDBFromCommittee(cmt *shard.Committee) testStateDB { } var wrapper staking.ValidatorWrapper wrapper.Address = slot.EcdsaAddress - wrapper.SlotPubKeys = []shard.BLSPublicKey{slot.BLSPublicKey} + wrapper.SlotPubKeys = []bls.SerializedPublicKey{slot.BLSPublicKey} wrapper.Counters.NumBlocksSigned = new(big.Int).SetInt64(1) wrapper.Counters.NumBlocksToSign = new(big.Int).SetInt64(1) @@ -593,7 +594,7 @@ func (state testStateDB) snapshot() testStateDB { wrapperCpy := staking.ValidatorWrapper{ Validator: staking.Validator{ Address: addr, - SlotPubKeys: make([]shard.BLSPublicKey, 1), + SlotPubKeys: make([]bls.SerializedPublicKey, 1), }, } copy(wrapperCpy.SlotPubKeys, wrapper.SlotPubKeys) @@ -674,7 +675,7 @@ func makeTestCommittee(n int, shardID uint32) *shard.Committee { func makeHmySlot(seed int, shardID uint32) shard.Slot { addr := common.BigToAddress(new(big.Int).SetInt64(int64(seed) + int64(shardID*1000000))) - var blsKey shard.BLSPublicKey + var blsKey bls.SerializedPublicKey copy(blsKey[:], bls.RandPrivateKey().GetPublicKey().Serialize()) return shard.Slot{ diff --git a/staking/effective/calculate.go b/staking/effective/calculate.go index 07f66bb85..f49544b25 100644 --- a/staking/effective/calculate.go +++ b/staking/effective/calculate.go @@ -6,10 +6,11 @@ import ( "math/big" "sort" + "github.com/harmony-one/harmony/crypto/bls" + "github.com/ethereum/go-ethereum/common" common2 "github.com/harmony-one/harmony/internal/common" "github.com/harmony-one/harmony/numeric" - "github.com/harmony-one/harmony/shard" ) // medium.com/harmony-one/introducing-harmonys-effective-proof-of-stake-epos-2d39b4b8d58 @@ -29,7 +30,7 @@ func effectiveStake(median, actual numeric.Dec) numeric.Dec { // SlotPurchase .. type SlotPurchase struct { Addr common.Address - Key shard.BLSPublicKey + Key bls.SerializedPublicKey RawStake numeric.Dec EPoSStake numeric.Dec } @@ -51,9 +52,9 @@ func (p SlotPurchase) MarshalJSON() ([]byte, error) { // SlotOrder .. type SlotOrder struct { - Stake *big.Int `json:"stake"` - SpreadAmong []shard.BLSPublicKey `json:"keys-at-auction"` - Percentage numeric.Dec `json:"percentage-of-total-auction-stake"` + Stake *big.Int `json:"stake"` + SpreadAmong []bls.SerializedPublicKey `json:"keys-at-auction"` + Percentage numeric.Dec `json:"percentage-of-total-auction-stake"` } // Median .. diff --git a/staking/effective/calculate_test.go b/staking/effective/calculate_test.go index e8fefc370..01c394c58 100644 --- a/staking/effective/calculate_test.go +++ b/staking/effective/calculate_test.go @@ -9,10 +9,11 @@ import ( "sort" "testing" + bls_core "github.com/harmony-one/bls/ffi/go/bls" + "github.com/harmony-one/harmony/crypto/bls" + "github.com/ethereum/go-ethereum/common" - "github.com/harmony-one/bls/ffi/go/bls" "github.com/harmony-one/harmony/numeric" - "github.com/harmony-one/harmony/shard" ) const eposTestingFile = "epos.json" @@ -57,9 +58,9 @@ func generateRandomSlots(num int) []SlotPurchase { for i := 0; i < num; i++ { addr := common.Address{} addr.SetBytes(big.NewInt(int64(accountGen.Int63n(maxAccountGen))).Bytes()) - secretKey := bls.SecretKey{} + secretKey := bls_core.SecretKey{} secretKey.Deserialize(big.NewInt(int64(keyGen.Int63n(maxKeyGen))).Bytes()) - key := shard.BLSPublicKey{} + key := bls.SerializedPublicKey{} key.FromLibBLSPublicKey(secretKey.GetPublicKey()) stake := numeric.NewDecFromBigInt(big.NewInt(int64(stakeGen.Int63n(maxStakeGen)))) randomSlots = append(randomSlots, SlotPurchase{addr, key, stake, stake}) @@ -100,7 +101,3 @@ func TestEffectiveStake(t *testing.T) { } } } - -func TestApply(t *testing.T) { - // -} diff --git a/staking/slash/double-sign.go b/staking/slash/double-sign.go index 9eaa1b0dd..9691d8516 100644 --- a/staking/slash/double-sign.go +++ b/staking/slash/double-sign.go @@ -5,9 +5,12 @@ import ( "encoding/json" "math/big" + "github.com/harmony-one/harmony/crypto/bls" + "github.com/harmony-one/harmony/shard" + "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/rlp" - "github.com/harmony-one/bls/ffi/go/bls" + bls_core "github.com/harmony-one/bls/ffi/go/bls" consensus_sig "github.com/harmony-one/harmony/consensus/signature" "github.com/harmony-one/harmony/consensus/votepower" "github.com/harmony-one/harmony/core/state" @@ -15,7 +18,6 @@ import ( common2 "github.com/harmony-one/harmony/internal/common" "github.com/harmony-one/harmony/internal/utils" "github.com/harmony-one/harmony/numeric" - "github.com/harmony-one/harmony/shard" "github.com/harmony-one/harmony/staking/effective" staking "github.com/harmony-one/harmony/staking/types" "github.com/pkg/errors" @@ -71,9 +73,9 @@ type ConflictingVotes struct { // Vote is the vote of the double signer type Vote struct { - SignerPubKey shard.BLSPublicKey `json:"bls-public-key"` - BlockHeaderHash common.Hash `json:"block-header-hash"` - Signature []byte `json:"bls-signature"` + SignerPubKey bls.SerializedPublicKey `json:"bls-public-key"` + BlockHeaderHash common.Hash `json:"block-header-hash"` + Signature []byte `json:"bls-signature"` } // Record is an proof of a slashing made by a witness of a double-signing event @@ -169,8 +171,8 @@ func Verify( candidate.Evidence.FirstVote, candidate.Evidence.SecondVote k1, k2 := len(first.SignerPubKey), len(second.SignerPubKey) - if k1 != shard.PublicKeySizeInBytes || - k2 != shard.PublicKeySizeInBytes { + if k1 != bls.PublicKeySizeInBytes || + k2 != bls.PublicKeySizeInBytes { return errors.Wrapf( errSignerKeyNotRightSize, "cast key %d double-signed key %d", k1, k2, ) @@ -237,8 +239,8 @@ func Verify( candidate.Evidence.SecondVote, } { // now the only real assurance, cryptography - signature := &bls.Sign{} - publicKey := &bls.PublicKey{} + signature := &bls_core.Sign{} + publicKey := &bls_core.PublicKey{} if err := signature.Deserialize(ballot.Signature); err != nil { return err diff --git a/staking/slash/double-sign_test.go b/staking/slash/double-sign_test.go index 3883234a4..5a4edf338 100644 --- a/staking/slash/double-sign_test.go +++ b/staking/slash/double-sign_test.go @@ -8,15 +8,16 @@ import ( "strings" "testing" + "github.com/harmony-one/harmony/crypto/bls" + "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/ethdb" - "github.com/harmony-one/bls/ffi/go/bls" + bls_core "github.com/harmony-one/bls/ffi/go/bls" blockfactory "github.com/harmony-one/harmony/block/factory" consensus_sig "github.com/harmony-one/harmony/consensus/signature" "github.com/harmony-one/harmony/consensus/votepower" "github.com/harmony-one/harmony/core/state" "github.com/harmony-one/harmony/core/types" - bls2 "github.com/harmony-one/harmony/crypto/bls" "github.com/harmony-one/harmony/internal/params" "github.com/harmony-one/harmony/numeric" "github.com/harmony-one/harmony/shard" @@ -685,7 +686,7 @@ func TestRate(t *testing.T) { expRate numeric.Dec }{ { - votingPower: makeVotingPower(map[shard.BLSPublicKey]numeric.Dec{ + votingPower: makeVotingPower(map[bls.SerializedPublicKey]numeric.Dec{ keyPairs[0].Pub(): numeric.NewDecWithPrec(1, 2), keyPairs[1].Pub(): numeric.NewDecWithPrec(2, 2), keyPairs[2].Pub(): numeric.NewDecWithPrec(3, 2), @@ -698,7 +699,7 @@ func TestRate(t *testing.T) { expRate: numeric.NewDecWithPrec(6, 2), }, { - votingPower: makeVotingPower(map[shard.BLSPublicKey]numeric.Dec{ + votingPower: makeVotingPower(map[bls.SerializedPublicKey]numeric.Dec{ keyPairs[0].Pub(): numeric.NewDecWithPrec(1, 2), }), records: Records{ @@ -707,12 +708,12 @@ func TestRate(t *testing.T) { expRate: oneDoubleSignerRate, }, { - votingPower: makeVotingPower(map[shard.BLSPublicKey]numeric.Dec{}), + votingPower: makeVotingPower(map[bls.SerializedPublicKey]numeric.Dec{}), records: Records{}, expRate: oneDoubleSignerRate, }, { - votingPower: makeVotingPower(map[shard.BLSPublicKey]numeric.Dec{ + votingPower: makeVotingPower(map[bls.SerializedPublicKey]numeric.Dec{ keyPairs[0].Pub(): numeric.NewDecWithPrec(1, 2), keyPairs[1].Pub(): numeric.NewDecWithPrec(2, 2), keyPairs[3].Pub(): numeric.NewDecWithPrec(3, 2), @@ -734,15 +735,15 @@ func TestRate(t *testing.T) { } -func makeEmptyRecordWithSecondSignerKey(pub shard.BLSPublicKey) Record { +func makeEmptyRecordWithSecondSignerKey(pub bls.SerializedPublicKey) Record { var r Record r.Evidence.SecondVote.SignerPubKey = pub return r } -func makeVotingPower(m map[shard.BLSPublicKey]numeric.Dec) *votepower.Roster { +func makeVotingPower(m map[bls.SerializedPublicKey]numeric.Dec) *votepower.Roster { r := &votepower.Roster{ - Voters: make(map[shard.BLSPublicKey]*votepower.AccommodateHarmonyVote), + Voters: make(map[bls.SerializedPublicKey]*votepower.AccommodateHarmonyVote), } for pub, pct := range m { r.Voters[pub] = &votepower.AccommodateHarmonyVote{ @@ -796,7 +797,7 @@ func makeBlockForTest(epoch int64, index int) *types.Block { } func defaultValidatorWrapper() *staking.ValidatorWrapper { - pubKeys := []shard.BLSPublicKey{offPub} + pubKeys := []bls.SerializedPublicKey{offPub} v := defaultTestValidator(pubKeys) ds := defaultTestDelegations() @@ -811,7 +812,7 @@ func defaultSnapValidatorWrapper() *staking.ValidatorWrapper { } func defaultCurrentValidatorWrapper() *staking.ValidatorWrapper { - pubKeys := []shard.BLSPublicKey{offPub} + pubKeys := []bls.SerializedPublicKey{offPub} v := defaultTestValidator(pubKeys) ds := defaultDelegationsWithUndelegates() @@ -822,7 +823,7 @@ func defaultCurrentValidatorWrapper() *staking.ValidatorWrapper { } // defaultTestValidator makes a valid Validator kps structure -func defaultTestValidator(pubKeys []shard.BLSPublicKey) staking.Validator { +func defaultTestValidator(pubKeys []bls.SerializedPublicKey) staking.Validator { comm := staking.Commission{ CommissionRates: staking.CommissionRates{ Rate: numeric.MustNewDecFromStr("0.167983520183826780"), @@ -942,8 +943,8 @@ func (maker *shardSlotMaker) makeSlot() shard.Slot { } type blsKeyPair struct { - pri *bls.SecretKey - pub *bls.PublicKey + pri *bls_core.SecretKey + pub *bls_core.PublicKey } func genKeyPairs(size int) []blsKeyPair { @@ -955,7 +956,7 @@ func genKeyPairs(size int) []blsKeyPair { } func genKeyPair() blsKeyPair { - pri := bls2.RandPrivateKey() + pri := bls.RandPrivateKey() pub := pri.GetPublicKey() return blsKeyPair{ pri: pri, @@ -963,8 +964,8 @@ func genKeyPair() blsKeyPair { } } -func (kp blsKeyPair) Pub() shard.BLSPublicKey { - var pub shard.BLSPublicKey +func (kp blsKeyPair) Pub() bls.SerializedPublicKey { + var pub bls.SerializedPublicKey copy(pub[:], kp.pub.Serialize()) return pub } diff --git a/staking/slash/test/copy_test.go b/staking/slash/test/copy_test.go index 4b5dd06a5..9f6e23600 100644 --- a/staking/slash/test/copy_test.go +++ b/staking/slash/test/copy_test.go @@ -5,8 +5,9 @@ import ( "reflect" "testing" + "github.com/harmony-one/harmony/crypto/bls" + "github.com/ethereum/go-ethereum/common" - "github.com/harmony-one/harmony/shard" "github.com/harmony-one/harmony/staking/slash" ) @@ -88,13 +89,13 @@ var ( } nonZeroVote1 = slash.Vote{ - SignerPubKey: shard.BLSPublicKey{1}, + SignerPubKey: bls.SerializedPublicKey{1}, BlockHeaderHash: common.Hash{2}, Signature: []byte{1, 2, 3}, } nonZeroVote2 = slash.Vote{ - SignerPubKey: shard.BLSPublicKey{3}, + SignerPubKey: bls.SerializedPublicKey{3}, BlockHeaderHash: common.Hash{4}, Signature: []byte{4, 5, 6}, } diff --git a/staking/types/messages.go b/staking/types/messages.go index 78de59ab3..7dcdaf998 100644 --- a/staking/types/messages.go +++ b/staking/types/messages.go @@ -4,9 +4,10 @@ import ( "fmt" "math/big" + "github.com/harmony-one/harmony/crypto/bls" + "github.com/ethereum/go-ethereum/common" "github.com/harmony-one/harmony/numeric" - "github.com/harmony-one/harmony/shard" "github.com/harmony-one/harmony/staking/effective" "github.com/pkg/errors" ) @@ -57,11 +58,11 @@ type CreateValidator struct { ValidatorAddress common.Address `json:"validator-address"` Description `json:"description"` CommissionRates `json:"commission"` - MinSelfDelegation *big.Int `json:"min-self-delegation"` - MaxTotalDelegation *big.Int `json:"max-total-delegation"` - SlotPubKeys []shard.BLSPublicKey `json:"slot-pub-keys"` - SlotKeySigs []shard.BLSSignature `json:"slot-key-sigs"` - Amount *big.Int `json:"amount"` + MinSelfDelegation *big.Int `json:"min-self-delegation"` + MaxTotalDelegation *big.Int `json:"max-total-delegation"` + SlotPubKeys []bls.SerializedPublicKey `json:"slot-pub-keys"` + SlotKeySigs []bls.SerializedSignature `json:"slot-key-sigs"` + Amount *big.Int `json:"amount"` } // Type of CreateValidator @@ -78,11 +79,11 @@ func (v CreateValidator) Copy() StakeMsg { } if v.SlotPubKeys != nil { - cp.SlotPubKeys = make([]shard.BLSPublicKey, len(v.SlotPubKeys)) + cp.SlotPubKeys = make([]bls.SerializedPublicKey, len(v.SlotPubKeys)) copy(cp.SlotPubKeys, v.SlotPubKeys) } if v.SlotKeySigs != nil { - cp.SlotKeySigs = make([]shard.BLSSignature, len(v.SlotKeySigs)) + cp.SlotKeySigs = make([]bls.SerializedSignature, len(v.SlotKeySigs)) copy(cp.SlotKeySigs, v.SlotKeySigs) } if v.MinSelfDelegation != nil { @@ -101,13 +102,13 @@ func (v CreateValidator) Copy() StakeMsg { type EditValidator struct { ValidatorAddress common.Address `json:"validator-address"` Description `json:"description"` - CommissionRate *numeric.Dec `json:"commission-rate" rlp:"nil"` - MinSelfDelegation *big.Int `json:"min-self-delegation" rlp:"nil"` - MaxTotalDelegation *big.Int `json:"max-total-delegation" rlp:"nil"` - SlotKeyToRemove *shard.BLSPublicKey `json:"slot-key-to_remove" rlp:"nil"` - SlotKeyToAdd *shard.BLSPublicKey `json:"slot-key-to_add" rlp:"nil"` - SlotKeyToAddSig *shard.BLSSignature `json:"slot-key-to-add-sig" rlp:"nil"` - EPOSStatus effective.Eligibility `json:"epos-eligibility-status" rlp:"nil"` + CommissionRate *numeric.Dec `json:"commission-rate" rlp:"nil"` + MinSelfDelegation *big.Int `json:"min-self-delegation" rlp:"nil"` + MaxTotalDelegation *big.Int `json:"max-total-delegation" rlp:"nil"` + SlotKeyToRemove *bls.SerializedPublicKey `json:"slot-key-to_remove" rlp:"nil"` + SlotKeyToAdd *bls.SerializedPublicKey `json:"slot-key-to_add" rlp:"nil"` + SlotKeyToAddSig *bls.SerializedSignature `json:"slot-key-to-add-sig" rlp:"nil"` + EPOSStatus effective.Eligibility `json:"epos-eligibility-status" rlp:"nil"` } // Type of EditValidator diff --git a/staking/types/messages_test.go b/staking/types/messages_test.go index 25401da18..f640716f7 100644 --- a/staking/types/messages_test.go +++ b/staking/types/messages_test.go @@ -7,8 +7,9 @@ import ( "reflect" "testing" + "github.com/harmony-one/harmony/crypto/bls" + "github.com/ethereum/go-ethereum/common" - "github.com/harmony-one/harmony/shard" "github.com/harmony-one/harmony/staking/effective" ) @@ -260,7 +261,7 @@ func assertBigIntCopy(i1, i2 *big.Int) error { return nil } -func assertPubsCopy(s1, s2 []shard.BLSPublicKey) error { +func assertPubsCopy(s1, s2 []bls.SerializedPublicKey) error { if len(s1) != len(s2) { return fmt.Errorf("size not equal") } @@ -272,7 +273,7 @@ func assertPubsCopy(s1, s2 []shard.BLSPublicKey) error { return nil } -func assertSigsCopy(s1, s2 []shard.BLSSignature) error { +func assertSigsCopy(s1, s2 []bls.SerializedSignature) error { if len(s1) != len(s2) { return fmt.Errorf("size not equal") } @@ -302,8 +303,8 @@ func cpTestDataSetup() { MaxRate: zeroDec, MaxChangeRate: zeroDec, } - var zeroBLSPub shard.BLSPublicKey - var zeroBLSSig shard.BLSSignature + var zeroBLSPub bls.SerializedPublicKey + var zeroBLSSig bls.SerializedSignature testCreateValidator = CreateValidator{ ValidatorAddress: validatorAddr, @@ -311,16 +312,16 @@ func cpTestDataSetup() { CommissionRates: cr, MinSelfDelegation: tenK, MaxTotalDelegation: twelveK, - SlotPubKeys: []shard.BLSPublicKey{blsPubSigPairs[0].pub}, - SlotKeySigs: []shard.BLSSignature{blsPubSigPairs[0].sig}, + SlotPubKeys: []bls.SerializedPublicKey{blsPubSigPairs[0].pub}, + SlotKeySigs: []bls.SerializedSignature{blsPubSigPairs[0].sig}, Amount: twelveK, } zeroCreateValidator = CreateValidator{ CommissionRates: zeroCr, MinSelfDelegation: common.Big0, MaxTotalDelegation: common.Big0, - SlotPubKeys: make([]shard.BLSPublicKey, 0), - SlotKeySigs: make([]shard.BLSSignature, 0), + SlotPubKeys: make([]bls.SerializedPublicKey, 0), + SlotKeySigs: make([]bls.SerializedSignature, 0), Amount: common.Big0, } @@ -380,7 +381,7 @@ func cpTestDataSetup() { // maxRate := NewDecWithPrec(2, 2) // 20% // maxChangeRate := NewDecWithPrec(1, 3) // 1% -// blsPublickey := shard.BLSPublicKey{} +// blsPublickey := shard.SerializedPublicKey{} // blsPublickey.FromLibBLSPublicKey(blsPubKey) // msgCreateValidator := NewMsgCreateValidator(Description{ @@ -443,7 +444,7 @@ func cpTestDataSetup() { // func TestMsgEditValidatorRLP(t *testing.T) { // commissionRate := NewDecWithPrec(1, 2) // 10% -// blsPublickey := shard.BLSPublicKey{} +// blsPublickey := shard.SerializedPublicKey{} // blsPublickey.FromLibBLSPublicKey(blsPubKey) // msgEditValidator := NewMsgEditValidator(Description{ diff --git a/staking/types/test/copy.go b/staking/types/test/copy.go index fcb827d9a..f9295ca14 100644 --- a/staking/types/test/copy.go +++ b/staking/types/test/copy.go @@ -3,7 +3,8 @@ package staketest import ( "math/big" - "github.com/harmony-one/harmony/shard" + "github.com/harmony-one/harmony/crypto/bls" + staking "github.com/harmony-one/harmony/staking/types" ) @@ -34,7 +35,7 @@ func CopyValidator(v staking.Validator) staking.Validator { Description: v.Description, } if v.SlotPubKeys != nil { - cp.SlotPubKeys = make([]shard.BLSPublicKey, len(v.SlotPubKeys)) + cp.SlotPubKeys = make([]bls.SerializedPublicKey, len(v.SlotPubKeys)) copy(cp.SlotPubKeys, v.SlotPubKeys) } if v.LastEpochInCommittee != nil { diff --git a/staking/types/test/copy_test.go b/staking/types/test/copy_test.go index fc200e961..6dc3799b9 100644 --- a/staking/types/test/copy_test.go +++ b/staking/types/test/copy_test.go @@ -7,15 +7,16 @@ import ( "reflect" "testing" + "github.com/harmony-one/harmony/crypto/bls" + "github.com/ethereum/go-ethereum/common" "github.com/harmony-one/harmony/numeric" - "github.com/harmony-one/harmony/shard" "github.com/harmony-one/harmony/staking/effective" staking "github.com/harmony-one/harmony/staking/types" ) var ( - testPub = shard.BLSPublicKey{1} + testPub = bls.SerializedPublicKey{1} ) func TestCopyValidatorWrapper(t *testing.T) { @@ -82,7 +83,7 @@ func makeNonZeroValidator() staking.Validator { } v := staking.Validator{ Address: common.BigToAddress(common.Big0), - SlotPubKeys: []shard.BLSPublicKey{testPub}, + SlotPubKeys: []bls.SerializedPublicKey{testPub}, LastEpochInCommittee: big.NewInt(20), MinSelfDelegation: common.Big1, MaxTotalDelegation: common.Big1, @@ -96,7 +97,7 @@ func makeNonZeroValidator() staking.Validator { func makeZeroValidator() staking.Validator { v := staking.Validator{ - SlotPubKeys: make([]shard.BLSPublicKey, 0), + SlotPubKeys: make([]bls.SerializedPublicKey, 0), LastEpochInCommittee: common.Big0, MinSelfDelegation: common.Big0, MaxTotalDelegation: common.Big0, diff --git a/staking/types/test/equal.go b/staking/types/test/equal.go index a9eab1ff0..0cc359573 100644 --- a/staking/types/test/equal.go +++ b/staking/types/test/equal.go @@ -4,8 +4,9 @@ import ( "fmt" "math/big" + "github.com/harmony-one/harmony/crypto/bls" + "github.com/harmony-one/harmony/numeric" - "github.com/harmony-one/harmony/shard" staking "github.com/harmony-one/harmony/staking/types" ) @@ -128,7 +129,7 @@ func checkUndelegationEqual(ud1, ud2 staking.Undelegation) error { return nil } -func checkPubKeysEqual(pubs1, pubs2 []shard.BLSPublicKey) error { +func checkPubKeysEqual(pubs1, pubs2 []bls.SerializedPublicKey) error { if len(pubs1) != len(pubs2) { return fmt.Errorf(".len not equal: %v / %v", len(pubs1), len(pubs2)) } diff --git a/staking/types/test/prototype.go b/staking/types/test/prototype.go index 3b0ab46c5..eff31ceda 100644 --- a/staking/types/test/prototype.go +++ b/staking/types/test/prototype.go @@ -3,9 +3,10 @@ package staketest import ( "math/big" + "github.com/harmony-one/harmony/crypto/bls" + "github.com/ethereum/go-ethereum/common" "github.com/harmony-one/harmony/numeric" - "github.com/harmony-one/harmony/shard" "github.com/harmony-one/harmony/staking/effective" staking "github.com/harmony-one/harmony/staking/types" ) @@ -47,7 +48,7 @@ var ( validatorPrototype = staking.Validator{ Address: common.Address{}, - SlotPubKeys: []shard.BLSPublicKey{shard.BLSPublicKey{}}, + SlotPubKeys: []bls.SerializedPublicKey{bls.SerializedPublicKey{}}, LastEpochInCommittee: common.Big0, MinSelfDelegation: DefaultMinSelfDel, MaxTotalDelegation: DefaultMaxTotalDel, @@ -84,11 +85,11 @@ func GetDefaultValidator() staking.Validator { // GetDefaultValidatorWithAddr return the default staking.Validator with the // given validator address and bls keys -func GetDefaultValidatorWithAddr(addr common.Address, pubs []shard.BLSPublicKey) staking.Validator { +func GetDefaultValidatorWithAddr(addr common.Address, pubs []bls.SerializedPublicKey) staking.Validator { v := CopyValidator(validatorPrototype) v.Address = addr if pubs != nil { - v.SlotPubKeys = make([]shard.BLSPublicKey, len(pubs)) + v.SlotPubKeys = make([]bls.SerializedPublicKey, len(pubs)) copy(v.SlotPubKeys, pubs) } else { v.SlotPubKeys = nil @@ -103,11 +104,11 @@ func GetDefaultValidatorWrapper() staking.ValidatorWrapper { // GetDefaultValidatorWrapperWithAddr return the default staking.ValidatorWrapper // with the given validator address and bls keys. -func GetDefaultValidatorWrapperWithAddr(addr common.Address, pubs []shard.BLSPublicKey) staking.ValidatorWrapper { +func GetDefaultValidatorWrapperWithAddr(addr common.Address, pubs []bls.SerializedPublicKey) staking.ValidatorWrapper { w := CopyValidatorWrapper(vWrapperPrototype) w.Address = addr if pubs != nil { - w.SlotPubKeys = make([]shard.BLSPublicKey, len(pubs)) + w.SlotPubKeys = make([]bls.SerializedPublicKey, len(pubs)) copy(w.SlotPubKeys, pubs) } else { w.SlotPubKeys = nil diff --git a/staking/types/test/prototype_test.go b/staking/types/test/prototype_test.go index 3cc4d7661..f854b65b3 100644 --- a/staking/types/test/prototype_test.go +++ b/staking/types/test/prototype_test.go @@ -3,8 +3,9 @@ package staketest import ( "testing" + "github.com/harmony-one/harmony/crypto/bls" + "github.com/ethereum/go-ethereum/common" - "github.com/harmony-one/harmony/shard" ) func TestGetDefaultValidator(t *testing.T) { @@ -24,15 +25,15 @@ func TestGetDefaultValidatorWrapper(t *testing.T) { func TestGetDefaultValidatorWithAddr(t *testing.T) { tests := []struct { addr common.Address - keys []shard.BLSPublicKey + keys []bls.SerializedPublicKey }{ { addr: common.BigToAddress(common.Big1), - keys: []shard.BLSPublicKey{{1}, {}}, + keys: []bls.SerializedPublicKey{{1}, {}}, }, { addr: common.Address{}, - keys: make([]shard.BLSPublicKey, 0), + keys: make([]bls.SerializedPublicKey, 0), }, {}, } @@ -52,15 +53,15 @@ func TestGetDefaultValidatorWithAddr(t *testing.T) { func TestGetDefaultValidatorWrapperWithAddr(t *testing.T) { tests := []struct { addr common.Address - keys []shard.BLSPublicKey + keys []bls.SerializedPublicKey }{ { addr: common.BigToAddress(common.Big1), - keys: []shard.BLSPublicKey{{1}, {}}, + keys: []bls.SerializedPublicKey{{1}, {}}, }, { addr: common.Address{}, - keys: make([]shard.BLSPublicKey, 0), + keys: make([]bls.SerializedPublicKey, 0), }, {}, } diff --git a/staking/types/transaction_test.go b/staking/types/transaction_test.go index 0f7817476..5548beef4 100644 --- a/staking/types/transaction_test.go +++ b/staking/types/transaction_test.go @@ -6,11 +6,12 @@ import ( "math/big" "testing" + "github.com/harmony-one/harmony/crypto/bls" + "github.com/ethereum/go-ethereum/common" - "github.com/harmony-one/bls/ffi/go/bls" + bls_core "github.com/harmony-one/bls/ffi/go/bls" common2 "github.com/harmony-one/harmony/internal/common" numeric "github.com/harmony-one/harmony/numeric" - "github.com/harmony-one/harmony/shard" ) // for testing purpose @@ -38,9 +39,9 @@ func CreateTestNewTransaction() (*StakingTransaction, error) { dAddr, _ := common2.Bech32ToAddress(testAccount) stakePayloadMaker := func() (Directive, interface{}) { - p := &bls.PublicKey{} + p := &bls_core.PublicKey{} p.DeserializeHexStr(testBLSPubKey) - pub := shard.BLSPublicKey{} + pub := bls.SerializedPublicKey{} pub.FromLibBLSPublicKey(p) ra, _ := numeric.NewDecFromStr("0.7") @@ -62,7 +63,7 @@ func CreateTestNewTransaction() (*StakingTransaction, error) { MinSelfDelegation: big.NewInt(10), MaxTotalDelegation: big.NewInt(3000), ValidatorAddress: common.Address(dAddr), - SlotPubKeys: []shard.BLSPublicKey{pub}, + SlotPubKeys: []bls.SerializedPublicKey{pub}, Amount: big.NewInt(100), } } @@ -86,9 +87,9 @@ func TestTransactionCopy(t *testing.T) { newRate, _ := numeric.NewDecFromStr("0.5") cv1.CommissionRates.Rate = newRate - p := &bls.PublicKey{} + p := &bls_core.PublicKey{} p.DeserializeHexStr(testBLSPubKey2) - pub := shard.BLSPublicKey{} + pub := bls.SerializedPublicKey{} pub.FromLibBLSPublicKey(p) cv1.SlotPubKeys = append(cv1.SlotPubKeys, pub) diff --git a/staking/types/validator.go b/staking/types/validator.go index 118a9e66e..ddad643b9 100644 --- a/staking/types/validator.go +++ b/staking/types/validator.go @@ -4,16 +4,18 @@ import ( "encoding/json" "math/big" + "github.com/harmony-one/harmony/crypto/bls" + "github.com/harmony-one/harmony/shard" + "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/rlp" - "github.com/harmony-one/bls/ffi/go/bls" + bls_core "github.com/harmony-one/bls/ffi/go/bls" "github.com/harmony-one/harmony/common/denominations" "github.com/harmony-one/harmony/consensus/votepower" "github.com/harmony-one/harmony/crypto/hash" common2 "github.com/harmony-one/harmony/internal/common" "github.com/harmony-one/harmony/internal/genesis" "github.com/harmony-one/harmony/numeric" - "github.com/harmony-one/harmony/shard" "github.com/harmony-one/harmony/staking/effective" "github.com/pkg/errors" ) @@ -212,7 +214,7 @@ type Validator struct { // ECDSA address of the validator Address common.Address `json:"address"` // The BLS public key of the validator for consensus - SlotPubKeys []shard.BLSPublicKey `json:"bls-public-keys"` + SlotPubKeys []bls.SerializedPublicKey `json:"bls-public-keys"` // The number of the last epoch this validator is // selected in committee (0 means never selected) LastEpochInCommittee *big.Int `json:"last-epoch-in-committee"` @@ -314,7 +316,7 @@ func (v *Validator) SanityCheck() error { ) } - allKeys := map[shard.BLSPublicKey]struct{}{} + allKeys := map[bls.SerializedPublicKey]struct{}{} for i := range v.SlotPubKeys { if _, ok := allKeys[v.SlotPubKeys[i]]; !ok { allKeys[v.SlotPubKeys[i]] = struct{}{} @@ -452,7 +454,7 @@ func (d Description) EnsureLength() (Description, error) { // VerifyBLSKeys checks if the public BLS key at index i of pubKeys matches the // BLS key signature at index i of pubKeysSigs. -func VerifyBLSKeys(pubKeys []shard.BLSPublicKey, pubKeySigs []shard.BLSSignature) error { +func VerifyBLSKeys(pubKeys []bls.SerializedPublicKey, pubKeySigs []bls.SerializedSignature) error { if len(pubKeys) != len(pubKeySigs) { return errBLSKeysNotMatchSigs } @@ -467,17 +469,17 @@ func VerifyBLSKeys(pubKeys []shard.BLSPublicKey, pubKeySigs []shard.BLSSignature } // VerifyBLSKey checks if the public BLS key matches the BLS signature -func VerifyBLSKey(pubKey *shard.BLSPublicKey, pubKeySig *shard.BLSSignature) error { +func VerifyBLSKey(pubKey *bls.SerializedPublicKey, pubKeySig *bls.SerializedSignature) error { if len(pubKeySig) == 0 { return errBLSKeysNotMatchSigs } - blsPubKey := new(bls.PublicKey) + blsPubKey := new(bls_core.PublicKey) if err := pubKey.ToLibBLSPublicKey(blsPubKey); err != nil { return errBLSKeysNotMatchSigs } - msgSig := bls.Sign{} + msgSig := bls_core.Sign{} if err := msgSig.Deserialize(pubKeySig[:]); err != nil { return err } @@ -492,7 +494,7 @@ func VerifyBLSKey(pubKey *shard.BLSPublicKey, pubKeySig *shard.BLSSignature) err } func containsHarmonyBLSKeys( - blsKeys []shard.BLSPublicKey, + blsKeys []bls.SerializedPublicKey, hmyAccounts []genesis.DeployAccount, epoch *big.Int, ) error { @@ -507,7 +509,7 @@ func containsHarmonyBLSKeys( } func matchesHarmonyBLSKey( - blsKey *shard.BLSPublicKey, + blsKey *bls.SerializedPublicKey, hmyAccounts []genesis.DeployAccount, epoch *big.Int, ) error { diff --git a/staking/types/validator_test.go b/staking/types/validator_test.go index 9ee34352f..1f0caa07c 100644 --- a/staking/types/validator_test.go +++ b/staking/types/validator_test.go @@ -6,20 +6,20 @@ import ( "strings" "testing" - common "github.com/ethereum/go-ethereum/common" "github.com/harmony-one/harmony/crypto/bls" + + "github.com/ethereum/go-ethereum/common" "github.com/harmony-one/harmony/crypto/hash" common2 "github.com/harmony-one/harmony/internal/common" "github.com/harmony-one/harmony/internal/genesis" "github.com/harmony-one/harmony/numeric" - "github.com/harmony-one/harmony/shard" "github.com/harmony-one/harmony/staking/effective" "github.com/pkg/errors" ) var ( blsPubSigPairs = makeBLSPubSigPairs(5) - hmyBLSPub shard.BLSPublicKey + hmyBLSPub bls.SerializedPublicKey hmyBLSPubStr = "c2962419d9999a87daa134f6d177f9ccabfe168a470587b13dd02ce91d1690a92170e5949d3dbdfc1b13fd7327dbef8c" validatorAddr, _ = common2.Bech32ToAddress("one1pdv9lrdwl0rg5vglh4xtyrv3wjk3wsqket7zxy") @@ -172,7 +172,9 @@ func TestValidator_SanityCheck(t *testing.T) { errCommissionRateTooLarge, }, { - func(v *Validator) { v.SlotPubKeys = []shard.BLSPublicKey{blsPubSigPairs[0].pub, blsPubSigPairs[0].pub} }, + func(v *Validator) { + v.SlotPubKeys = []bls.SerializedPublicKey{blsPubSigPairs[0].pub, blsPubSigPairs[0].pub} + }, errDuplicateSlotKeys, }, } @@ -479,7 +481,7 @@ func TestContainsHarmonyBLSKeys(t *testing.T) { } } -func makeDeployAccountsFromBLSPubs(pubs []shard.BLSPublicKey) []genesis.DeployAccount { +func makeDeployAccountsFromBLSPubs(pubs []bls.SerializedPublicKey) []genesis.DeployAccount { das := make([]genesis.DeployAccount, 0, len(pubs)) for i, pub := range pubs { das = append(das, genesis.DeployAccount{ @@ -689,8 +691,8 @@ func TestUpdateValidatorFromEditMsg(t *testing.T) { } type blsPubSigPair struct { - pub shard.BLSPublicKey - sig shard.BLSSignature + pub bls.SerializedPublicKey + sig bls.SerializedSignature } func makeBLSPubSigPairs(size int) []blsPubSigPair { @@ -707,25 +709,25 @@ func makeBLSPubSigPair() blsPubSigPair { msgHash := hash.Keccak256([]byte(BLSVerificationStr)) sig := blsPriv.SignHash(msgHash) - var shardPub shard.BLSPublicKey + var shardPub bls.SerializedPublicKey copy(shardPub[:], blsPub.Serialize()) - var shardSig shard.BLSSignature + var shardSig bls.SerializedSignature copy(shardSig[:], sig.Serialize()) return blsPubSigPair{shardPub, shardSig} } -func getPubsFromPairs(pairs []blsPubSigPair, indexes []int) []shard.BLSPublicKey { - pubs := make([]shard.BLSPublicKey, 0, len(indexes)) +func getPubsFromPairs(pairs []blsPubSigPair, indexes []int) []bls.SerializedPublicKey { + pubs := make([]bls.SerializedPublicKey, 0, len(indexes)) for _, index := range indexes { pubs = append(pubs, pairs[index].pub) } return pubs } -func getSigsFromPairs(pairs []blsPubSigPair, indexes []int) []shard.BLSSignature { - sigs := make([]shard.BLSSignature, 0, len(indexes)) +func getSigsFromPairs(pairs []blsPubSigPair, indexes []int) []bls.SerializedSignature { + sigs := make([]bls.SerializedSignature, 0, len(indexes)) for _, index := range indexes { sigs = append(sigs, pairs[index].sig) } @@ -744,7 +746,7 @@ func makeValidValidator() Validator { } v := Validator{ Address: validatorAddr, - SlotPubKeys: []shard.BLSPublicKey{blsPubSigPairs[0].pub}, + SlotPubKeys: []bls.SerializedPublicKey{blsPubSigPairs[0].pub}, LastEpochInCommittee: big.NewInt(20), MinSelfDelegation: tenK, MaxTotalDelegation: twelveK, diff --git a/test/chain/reward/main.go b/test/chain/reward/main.go index c5006f44e..c8ec2f167 100644 --- a/test/chain/reward/main.go +++ b/test/chain/reward/main.go @@ -6,6 +6,8 @@ import ( "math/rand" "time" + "github.com/harmony-one/harmony/crypto/bls" + blockfactory "github.com/harmony-one/harmony/block/factory" "github.com/harmony-one/harmony/internal/params" "github.com/harmony-one/harmony/internal/utils" @@ -13,7 +15,7 @@ import ( common2 "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/ethdb" - "github.com/harmony-one/bls/ffi/go/bls" + bls_core "github.com/harmony-one/bls/ffi/go/bls" "github.com/harmony-one/harmony/core" "github.com/harmony-one/harmony/core/state" "github.com/harmony-one/harmony/core/vm" @@ -21,7 +23,6 @@ import ( "github.com/harmony-one/harmony/internal/chain" "github.com/harmony-one/harmony/internal/common" "github.com/harmony-one/harmony/numeric" - "github.com/harmony-one/harmony/shard" staking "github.com/harmony-one/harmony/staking/types" ) @@ -34,20 +35,20 @@ var ( ) func init() { - bls.Init(bls.BLS12_381) + bls_core.Init(bls_core.BLS12_381) } -func generateBLSKeySigPair() (shard.BLSPublicKey, shard.BLSSignature) { - p := &bls.PublicKey{} +func generateBLSKeySigPair() (bls.SerializedPublicKey, bls.SerializedSignature) { + p := &bls_core.PublicKey{} p.DeserializeHexStr(testBLSPubKey) - pub := shard.BLSPublicKey{} + pub := bls.SerializedPublicKey{} pub.FromLibBLSPublicKey(p) messageBytes := []byte(staking.BLSVerificationStr) - privateKey := &bls.SecretKey{} + privateKey := &bls_core.SecretKey{} privateKey.DeserializeHexStr(testBLSPrvKey) msgHash := hash.Keccak256(messageBytes) signature := privateKey.SignHash(msgHash[:]) - var sig shard.BLSSignature + var sig bls.SerializedSignature copy(sig[:], signature.Serialize()) return pub, sig } @@ -71,8 +72,8 @@ func createValidator() *staking.CreateValidator { minSelfDel := new(big.Int).Mul(big.NewInt(5e18), big.NewInt(2000)) maxTotalDel := new(big.Int).Mul(big.NewInt(5e18), big.NewInt(100000)) pubKey, pubSig := generateBLSKeySigPair() - slotPubKeys := []shard.BLSPublicKey{pubKey} - slotKeySigs := []shard.BLSSignature{pubSig} + slotPubKeys := []bls.SerializedPublicKey{pubKey} + slotKeySigs := []bls.SerializedSignature{pubSig} amount := new(big.Int).Mul(big.NewInt(5e18), big.NewInt(2000)) v := staking.CreateValidator{ ValidatorAddress: validatorAddress,