How do I compute the checksum? The bip 173 page has the code in python which I do not understand. Can you explain the process so I could code in Java/Kotlin?
I haven't seen a Java/Kotlic implementation but apart from the C implementation there is
JavaScript and my own
C# implementations that are similar languages.
The process is:
"Expand HRP" by converting its N base-256 (8-bit) representation to 2N+1 base-32 (5-bit).
For example bc which is 0x6263 or 0b01100010_01100011 becomes 0x0303000203
In this process for each octet the highest 3 bits are placed in first half of the result and the remaining (low) 5 bits in second half. In the example above:
b=0x62=0b
01100010 -> 0x
030300
0203 and
c=0x63=0b
01100011 -> 0x03
030002
03The two halves are separated with a zero (the value at the middle is always 0) -> 0x0303
000203
Now we can compute checksum of
[expanded HRP] | [base32 data] | [6x zeros] (note that "|" is concatenation).
The process is best explained by code
private static uint Polymod(byte[] data)
{
uint chk = 1;
foreach (byte b in data)
{
uint temp = chk >> 25;
chk = ((chk & 0x1ffffff) << 5) ^ b;
for (int i = 0; i < 5; i++)
{
if (((temp >> i) & 1) == 1)
{
chk ^= generator[i];
}
}
}
return chk;
}
The actual checksum used in encoding is the result of "polymod" XORed with a constant. This constant is 0x01 for Bech32 (BIP-173) encoding just flipping the least significant bit and is 0x2bc830a3 for Bech32m (BIP-350) encoding. The former is used for witness version 0 addresses and the later for 1+. This is basically the only difference that was introduced in
BIP-350.