Now it is rumored that the Hangzhou police have summoned practitioners in the cryptocurrency industry. I want to ask, does this matter have anything to do with you? You haven't made any money and have lost everything. I was just thinking about asking them if they can recover the money. I'm done with this.
So let's continue studying, haha.
1. The function of 'prefix'
Decimal is the most familiar to everyone, so it will be easier to understand as an example.
Step 1: Assume a four-digit decimal number starting with 3. The minimum is 3000 and the maximum is 3999. In other words, as long as this number n satisfies 3000 ≤ n ≤ 3999, the first digit of this number must be 3.
Step 2: OK, now we just need to know what the binary representation of 3000 and 3999 is.
3000 -> 101110111000 (minimum value, 12 bits)
3999 -> 111110011111 (maximum value, 12 bits)
Step 3: Now we encounter an 8-bit binary number xxxxxxxx (each x can be 0 or 1). We need to add a few bits in front and convert it to decimal, ensuring that the first digit of the decimal number is always 3. How to do it?
Step 4: It's simple. Just add a four-digit number yyyy 'prefix' in front of this 8-bit binary number, and ensure that the 12-bit binary number formed by yyyyxxxxxxxx is within the range mentioned above. This four-digit number can be 1110 (1110xxxxxxxx), which means that regardless of whether the 'low bits' x are 1 or 0, the size of this number is determined by the 'high bits'.
Homework: 1. What do you think this four-digit number could be?
2. Is it possible to add prefixes with other digit lengths, like 8 bits?
If the conversion between the above binary and decimal is understood, then the conversion between binary and base58 follows the same principle.
The essence of Base58 encoding is that it is a base58 number. Therefore, by adding a suitable 'prefix' in front of the binary number, we can ensure that the corresponding base58 number starts with a fixed character.
2. The prefix of the private key
Friends who have read the previous articles know that a private key is a 256-bit binary number. To let the 'wallet' know that this binary number is a Bitcoin private key, a label 0x80 (hexadecimal, corresponding binary is 10000000) is added to the private key. Then, this 264-bit binary is encoded into a string using Base58Check. Therefore, when the wallet imports this encoded string, it knows this is a Bitcoin private key, so the encoded Bitcoin private key format is also called 'Wallet Import Format | WIF.'
Because 0x80 is in the high position, the encoded string starts with '5'.
The reason why the encoded 'compressed wallet import format | WIF-compressed' starts with K or L is also the same principle. What are the encoding rules of Base58Check? We'll discuss that in the next article.