From b9a9190f12f35aa0185355bd2369e658dd1c7bc9 Mon Sep 17 00:00:00 2001 From: Marco Argentieri <3596602+tiero@users.noreply.github.com> Date: Fri, 5 Feb 2021 23:08:32 +0100 Subject: [PATCH 1/2] constants overflofws untyped int --- blech32/blech32.go | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/blech32/blech32.go b/blech32/blech32.go index a3d8ddd..000f3e4 100644 --- a/blech32/blech32.go +++ b/blech32/blech32.go @@ -8,7 +8,22 @@ import ( const charset = "qpzry9x8gf2tvdw0s3jn54khce6mua7l" //new generators, 7 bytes compared to bech32 -var gen = []int{0x7d52fba40bd886, 0x5e8dbf1a03950c, 0x1c3a3c74072a18, 0x385d72fa0e5139, 0x7093e5a608865b} +var gen = []int64{0x7d52fba40bd886, 0x5e8dbf1a03950c, 0x1c3a3c74072a18, 0x385d72fa0e5139, 0x7093e5a608865b} + +// For more details on the polymod calculation, please refer to BIP 173. +func blech32Polymod(values []int) int64 { + chk := int64(1) + for _, v := range values { + b := chk >> 55 //25->55 compared to bech32 + chk = (chk&int64(0x7fffffffffffff))<0x7fffffffffffff compared to bech32 + for i := 0; i < 5; i++ { + if (b>>uint(i))&1 == 1 { + chk ^= gen[i] + } + } + } + return chk +} // Decode decodes a blech32 encoded string, returning the human-readable // part and the data part excluding the checksum. @@ -208,21 +223,6 @@ func blech32Checksum(hrp string, data []byte) []byte { return res } -// For more details on the polymod calculation, please refer to BIP 173. -func blech32Polymod(values []int) int { - chk := 1 - for _, v := range values { - b := chk >> 55 //25->55 compared to bech32 - chk = (chk&0x7fffffffffffff)<<5 ^ v //0x1ffffff->0x7fffffffffffff compared to bech32 - for i := 0; i < 5; i++ { - if (b>>uint(i))&1 == 1 { - chk ^= gen[i] - } - } - } - return chk -} - // For more details on HRP expansion, please refer to BIP 173. func blech32HrpExpand(hrp string) []int { v := make([]int, 0, len(hrp)*2+1) From 9fcd635c72611dd73203adbca3c8103c61194a26 Mon Sep 17 00:00:00 2001 From: Marco Argentieri <3596602+tiero@users.noreply.github.com> Date: Fri, 5 Feb 2021 23:14:01 +0100 Subject: [PATCH 2/2] revert position of the method --- blech32/blech32.go | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/blech32/blech32.go b/blech32/blech32.go index 000f3e4..6dc1e6c 100644 --- a/blech32/blech32.go +++ b/blech32/blech32.go @@ -10,21 +10,6 @@ const charset = "qpzry9x8gf2tvdw0s3jn54khce6mua7l" //new generators, 7 bytes compared to bech32 var gen = []int64{0x7d52fba40bd886, 0x5e8dbf1a03950c, 0x1c3a3c74072a18, 0x385d72fa0e5139, 0x7093e5a608865b} -// For more details on the polymod calculation, please refer to BIP 173. -func blech32Polymod(values []int) int64 { - chk := int64(1) - for _, v := range values { - b := chk >> 55 //25->55 compared to bech32 - chk = (chk&int64(0x7fffffffffffff))<0x7fffffffffffff compared to bech32 - for i := 0; i < 5; i++ { - if (b>>uint(i))&1 == 1 { - chk ^= gen[i] - } - } - } - return chk -} - // Decode decodes a blech32 encoded string, returning the human-readable // part and the data part excluding the checksum. func Decode(blech string) (string, []byte, error) { @@ -223,6 +208,21 @@ func blech32Checksum(hrp string, data []byte) []byte { return res } +// For more details on the polymod calculation, please refer to BIP 173. +func blech32Polymod(values []int) int64 { + chk := int64(1) + for _, v := range values { + b := chk >> 55 //25->55 compared to bech32 + chk = (chk&int64(0x7fffffffffffff))<0x7fffffffffffff compared to bech32 + for i := 0; i < 5; i++ { + if (b>>uint(i))&1 == 1 { + chk ^= gen[i] + } + } + } + return chk +} + // For more details on HRP expansion, please refer to BIP 173. func blech32HrpExpand(hrp string) []int { v := make([]int, 0, len(hrp)*2+1)