I whipped up a quick script for generating BIP39 mnemonics. Just a fun project, not gonna use it for real money.
Here’s the code:
To run it:
1. Create a file (let's say mnemonic_gen.py).
2. Copy the code from above.
3. Make another file called bip39_wordlist.txt and paste the wordlist there.
4. Keep both files in the same folder.
5. Run it using python mnemonic_gen.py.
Sample output below.
Might want to ditch the zfill. Doesn't really help when you've already got SHA256 hashing afterward. Should just regenerate entropy if it's less than what's needed instead. Also, a quick check on entropy before hashing could make it more secure.
You’ve actually made it a bit biased now...
This is a common pitfall. By looping until it’s exactly 128 bits, you remove the chance of generating a true 128-bit value with a leading bit of 0. In hex, that means your entropy will always start from 8 upwards, which isn’t ideal.
Generating extra rounds of entropy averages it out. Like, you’re just gonna get the mean of all the rounds.
If you're trying to strengthen the RNG, it might not be the best move. OpenSSL's RNG outperforms it.
Yeah, slicing a 128-bit string from a longer random string can help fix that bias. But it seems like you might not have grasped the bias issue fully.
Let me explain: pretend you’re running a test...
I took a deep get into entropy, made cool tweaks like adding a random secp256k1 point and different entropy salt methods.
Also, using random hashes to avoid predictability. If entropy is close to 50%, that’s better in theory. What do you think?
Interesting approach. But are you sure you have a better source of entropy? Randomly picking from three sources doesn’t guarantee that.
Also, what’s the deal with not storing the function result?
For those 24 words, it’s the checksum issue. It should follow BIP39 rules where the checksum is the first 8 bits of the SHA256 hash from the entropy.
Here’s how to fix it:
Also, the 12 words are using incorrect 256-bit entropy instead of the expected 128 bits.
Time flies, and I forgot some key replies. Thanks for the detailed feedback.
I reviewed the issues you pointed out and made corrections. Can you check them out before I update my main post?
One trick I’ve picked up: run hash-based cumulative testing. Just loop your code with predictable random bits, then hash the generated mnemonics. I used this with a million inputs:
Here’s what I got:
After testing my own BIP39 code this way, I got the same result, so I feel confident about the correctness of mnemonics.