Post
Topic
Board Development & Technical Discussion
Re: public key to mnemonic
by
odolvlobo
on 01/12/2020, 19:31:39 UTC
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:
03d322a42421f709d1b6253ecd0c442059534644544276ac3f964154e4b1ca56f4

Right 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:
03d322a42421f709d1b6253ecd0c442059534644544276ac3f964154e4b1ca56

For 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.

Encoding:

1. Public key: 03d322a42421f709d1b6253ecd0c442059534644544276ac3f964154e4b1ca56f4
2. Pad:      03d322a42421f709d1b6253ecd0c442059534644544276ac3f964154e4b1ca56f4000021
3. Encode: adult october poverty embark buyer lumber egg seven disagree half baby doll news permit bacon marriage issue giggle tortoise live exact flight citizen teach abandon absurd issue