I am attempting to grasp the logic of producing a sound BIP-39 seed phrase. I’ve learn the BIP-39 and attempting to implement the part Producing the mnemonic step-by-step. I’ve the next Crystal code that works very effectively for a 256-bit seed I discovered within the reply to: How one can generate a sound hash for a bip39 seed phrase?
# First, an preliminary entropy of ENT bits is generated.
entropy = "87c1b129fbadd7b6e9abc0a9ef7695436d767aece042bec198a97e949fcbe14c"
# => "87c1b129fbadd7b6e9abc0a9ef7695436d767aece042bec198a97e949fcbe14c"
# A checksum is generated by taking the primary ENT / 32 bits of its SHA256 hash.
sha2sum = OpenSSL::Digest.new("SHA256").replace(entropy.hexbytes).ultimate.hexstring
# => "0dc811788c7e02c32b9c4b3586baf58ca27f74330c92d661042b19faa6c7e9f2"
# the checksum size (CS): 2 (hex) := 8 (bits)
checksum_length_hex = 2
# => 2
checksum = sha2sum[0, checksum_length_hex]
# => "0d"
# This checksum is appended to the tip of the preliminary entropy.
entropy_checksummed = entropy + checksum
# => "87c1b129fbadd7b6e9abc0a9ef7695436d767aece042bec198a97e949fcbe14c0d"
This works, as I stated and my code is ready to generate the right indices for the phrases required, and the ensuing phrase matches with the one supplied within the different thread.
My drawback is, nonetheless, how can we handle the checksum_length_hex
for entropies of 160, 192, and 224 bits? As I stated, my code works for 256 bit with checksum_length_hex
of 2
and it additionally works for 128 bit with a size of 1
.
However how do I take care of the instances in between? I can not create a prefix of 1.2
. I used to be considering of taking the checksum prefix in pure bits (e.g., 5 bits for 160 entropy) and ultimately acquired solely gibberish outcomes. The next desk is taken instantly from BIP-39:
# | ENT | CS | ENT+CS | MS |
# +-------+----+--------+------+
# | 128 | 4 | 132 | 12 |
# | 160 | 5 | 165 | 15 |
# | 192 | 6 | 198 | 18 |
# | 224 | 7 | 231 | 21 |
# | 256 | 8 | 264 | 24 |
How do I correctly compute BIP-39 checksum bytes of sizes 5, 6, and seven for entropies of 160, 192, and 224?