From ea63ea58201c0b5adfdbabf41f3d090f9b1867bd Mon Sep 17 00:00:00 2001 From: Julien Robert Date: Tue, 8 Feb 2022 15:44:31 +0100 Subject: [PATCH] fix(crypto): Multisig verification doesn't work for multisig sizes of multiples of 8 --- 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 433776a6e4f4..1fc16893eb90 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 1bbd5ec07db4..741509f7753a 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 11984729ac28..1a76d4531352 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}}, }