Saturday, July 6, 2024

mnemonic seed – Tips on how to correctly compute the BIP39 checksum bytes?

I am attempting to grasp the logic of producing a legitimate 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 properly for a 256-bit seed I discovered within the reply to: Tips on how to generate a legitimate 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).remaining.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 top of the preliminary entropy.
entropy_checksummed = entropy + checksum
# => "87c1b129fbadd7b6e9abc0a9ef7695436d767aece042bec198a97e949fcbe14c0d"

This works, as I mentioned and my code is ready to generate the right indices for the phrases required, and the ensuing phrase matches with the one offered within the different thread.

My downside is, nevertheless, how will we handle the checksum_length_hex for entropies of 160, 192, and 224 bits? As I mentioned, 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’t 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 straight 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?

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles