Add multikey dedup and test cases

pull/3374/head
Rongjian Lan 4 years ago
parent b13c96bc1e
commit 0141604162
  1. 16
      cmd/harmony/bls.go
  2. 53
      consensus/quorum/quorom_test.go

@ -5,6 +5,8 @@ import (
"os"
"sync"
"github.com/harmony-one/harmony/crypto/bls"
"github.com/harmony-one/harmony/internal/blsgen"
nodeconfig "github.com/harmony-one/harmony/internal/configs/node"
"github.com/harmony-one/harmony/multibls"
@ -25,7 +27,19 @@ func setupConsensusKeys(hc harmonyConfig, config *nodeconfig.ConfigType) multibl
os.Exit(100)
}
})
config.ConsensusPriKey = multiBLSPriKey
// Dedup private keys
uniqueKeys := map[bls.SerializedPublicKey]struct{}{}
deduped := multibls.PrivateKeys{}
for _, priKey := range multiBLSPriKey {
if _, ok := uniqueKeys[priKey.Pub.Bytes]; ok {
continue
}
uniqueKeys[priKey.Pub.Bytes] = struct{}{}
deduped = append(deduped, priKey)
}
config.ConsensusPriKey = deduped
return multiBLSPriKey.GetPublicKeys()
}

@ -7,7 +7,7 @@ import (
bls_core "github.com/harmony-one/bls/ffi/go/bls"
harmony_bls "github.com/harmony-one/harmony/crypto/bls"
"github.com/harmony-one/harmony/internal/configs/sharding"
shardingconfig "github.com/harmony-one/harmony/internal/configs/sharding"
"github.com/harmony-one/harmony/shard"
"github.com/stretchr/testify/assert"
@ -479,3 +479,54 @@ func TestAddNewVoteInvalidAggregateSig(test *testing.T) {
test.Errorf("signers are incorrect for harmony nodes signing with aggregate sig: have %d, expect %d", decider.SignersCount(Prepare), 4)
}
}
func TestInvalidAggregateSig(test *testing.T) {
shard.Schedule = shardingconfig.LocalnetSchedule
blockHash := [32]byte{}
copy(blockHash[:], []byte("random"))
slotList := shard.SlotList{}
sKeys := []bls_core.SecretKey{}
pubKeys := []bls.PublicKeyWrapper{}
quorumNodes := 8
for i := 0; i < quorumNodes; i++ {
newSlot, sKey := generateRandomSlot()
if i < 3 {
newSlot.EffectiveStake = nil
}
sKeys = append(sKeys, sKey)
slotList = append(slotList, newSlot)
wrapper := bls.PublicKeyWrapper{Object: sKey.GetPublicKey()}
wrapper.Bytes.FromLibBLSPublicKey(wrapper.Object)
pubKeys = append(pubKeys, wrapper)
}
aggSig := &bls_core.Sign{}
for _, priKey := range []*bls_core.SecretKey{&sKeys[0], &sKeys[1], &sKeys[2], &sKeys[2]} {
if s := priKey.SignHash(blockHash[:]); s != nil {
aggSig.Add(s)
}
}
aggPubKey := &bls_core.PublicKey{}
for _, priKey := range []*bls_core.PublicKey{pubKeys[0].Object, pubKeys[1].Object, pubKeys[2].Object} {
aggPubKey.Add(priKey)
}
if aggSig.VerifyHash(aggPubKey, blockHash[:]) {
test.Error("Expect aggregate signature verification to fail due to duplicate signing from one key")
}
aggSig = &bls_core.Sign{}
for _, priKey := range []*bls_core.SecretKey{&sKeys[0], &sKeys[1], &sKeys[2]} {
if s := priKey.SignHash(blockHash[:]); s != nil {
aggSig.Add(s)
}
}
if !aggSig.VerifyHash(aggPubKey, blockHash[:]) {
test.Error("Expect aggregate signature verification to succeed with correctly matched keys and sigs")
}
}

Loading…
Cancel
Save