A compressed public key is 264 bits long which is 66 hex characters. You drop the 256 bit y coordinate, and change the prefix byte to either 02 or 03 depending on whether the y coordinate is even or odd.
Why do I drop the 256 bit y coordinate? Let's take it from start, I have this public key:
03d322a42421f709d1b6253ecd0c442059534644544276ac3f964154e4b1ca56f4Right now its length is 66, which means 264 bits. I want 256 to get the 24 words, so I'll have to get rid of 2 characters. Should I remove the first 2 which define if it's odd or even? Will I, then, be able to return from mnemonic to public key?
On my code I've removed the last 2 chars:
03d322a42421f709d1b6253ecd0c442059534644544276ac3f964154e4b1ca56For some weird reason when I convert this to binary I get 258 bits. Shouldn't I get 256? (264 - 4*2 = 256)
A simple solution is to pad the input with 0s. For BIP-39, you need a multiple of 32 bits, so you add 24 extra 0 bits to the end. I like to include a count in the padding as an additional check.
1. Public key:
2. Pad:
3. Encode: