From c686634819861fdc07947171c5db1200a0134a10 Mon Sep 17 00:00:00 2001 From: Julien Robert Date: Fri, 25 Feb 2022 11:37:53 +0100 Subject: [PATCH] fix(crypto): Multisig verification doesn't work for multisig sizes of multiples of 8 (#11142) ## Description Closes: #9914 `NumTrueBitsBefore` was getting out of bound and panicking. --- ### Author Checklist *All items are required. Please add a note to the item if the item is not applicable and please add links to any relevant follow up issues.* I have... - [x] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] added `!` to the type prefix if API or client breaking change - [x] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#pr-targeting)) - [x] provided a link to the relevant issue or specification - [ ] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/master/docs/building-modules) - [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#testing) - [ ] added a changelog entry to `CHANGELOG.md` - [ ] included comments for [documenting Go code](https://blog.golang.org/godoc) - [ ] updated the relevant documentation or specification - [x] reviewed "Files changed" and left comments if necessary - [ ] confirmed all CI checks have passed ### Reviewers Checklist *All items are required. Please add a note if the item is not applicable and please add your handle next to the items reviewed if you only reviewed selected items.* I have... - [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] confirmed `!` in the type prefix if API or client breaking change - [ ] confirmed all author checklist items have been addressed - [ ] reviewed state machine logic - [ ] reviewed API design and naming - [ ] reviewed documentation is accurate - [ ] reviewed tests and test coverage - [ ] manually tested (if applicable) --- crypto/keys/multisig/multisig_test.go | 2 +- crypto/types/compact_bit_array.go | 4 +++- crypto/types/compact_bit_array_test.go | 1 + 3 files changed, 5 insertions(+), 2 deletions(-) diff --git a/crypto/keys/multisig/multisig_test.go b/crypto/keys/multisig/multisig_test.go index 433776a6e4..1fc16893eb 100644 --- a/crypto/keys/multisig/multisig_test.go +++ b/crypto/keys/multisig/multisig_test.go @@ -124,7 +124,7 @@ func TestVerifyMultisignature(t *testing.T) { func(require *require.Assertions) { k := 2 signingIndices := []int{0, 3, 1} - pubKeys, sigs := generatePubKeysAndSignatures(5, msg) + pubKeys, sigs := generatePubKeysAndSignatures(8, msg) pk = kmultisig.NewLegacyAminoPubKey(k, pubKeys) sig = multisig.NewMultisig(len(pubKeys)) signBytesFn := func(mode signing.SignMode) ([]byte, error) { return msg, nil } diff --git a/crypto/types/compact_bit_array.go b/crypto/types/compact_bit_array.go index 1bbd5ec07d..741509f775 100644 --- a/crypto/types/compact_bit_array.go +++ b/crypto/types/compact_bit_array.go @@ -92,13 +92,15 @@ func (bA *CompactBitArray) NumTrueBitsBefore(index int) int { index = max } // below we iterate over the bytes then over bits (in low endian) and count bits set to 1 - for elem := 0; ; elem++ { + for elem := 0; elem < len(bA.Elems); elem++ { if elem*8+7 >= index { onesCount += bits.OnesCount8(bA.Elems[elem] >> (7 - (index % 8) + 1)) return onesCount } onesCount += bits.OnesCount8(bA.Elems[elem]) } + + return onesCount } // Copy returns a copy of the provided bit array. diff --git a/crypto/types/compact_bit_array_test.go b/crypto/types/compact_bit_array_test.go index 11984729ac..1a76d45313 100644 --- a/crypto/types/compact_bit_array_test.go +++ b/crypto/types/compact_bit_array_test.go @@ -205,6 +205,7 @@ func TestCompactBitArrayNumOfTrueBitsBefore(t *testing.T) { {`"x"`, []int{0}, []int{0}}, {`"_x"`, []int{1}, []int{0}}, {`"x___xxxx"`, []int{0, 4, 5, 6, 7}, []int{0, 1, 2, 3, 4}}, + {`"x___xxxx"`, []int{0, 4, 5, 6, 7, 8}, []int{0, 1, 2, 3, 4, 5}}, {`"__x_xx_x__x_x___"`, []int{2, 4, 5, 7, 10, 12}, []int{0, 1, 2, 3, 4, 5}}, {`"______________xx"`, []int{14, 15}, []int{0, 1}}, }