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)
This commit is contained in:
Julien Robert 2022-02-25 11:37:53 +01:00 committed by GitHub
parent 4de7d40010
commit c686634819
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 5 additions and 2 deletions

View File

@ -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 }

View File

@ -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.

View File

@ -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}},
}