Extra byte at the start of a public key

ludovic@demblans.com ludovic@REDACTED
Fri Jul 10 16:38:14 CEST 2020


I am trying some crypto code, mostly for fun. I want to generate an Ethereum public key from a know private key.

This is a sample private key : <<22,99,59,58,153,154,229,99,217,142,170,105,82,105,204,11,55,249,101,152,109,41,109,195,18,134,228,189,83,139,28,92>>

I run this code:


    run() -> 
        PrivateKey = <<22,99,59,58,153,154,229,99,217,142,170,105,82,105,204,11,55,249,101,152,109,41,109,195,18,134,228,189,83,139,28,92>>,
        {PublicKey, NewPrivateKey} = crypto:generate_key(ecdh, secp256k1, PrivateKey),
        dump_hex("Private key", PrivateKey),
        dump_hex("Public key", PublicKey).

    dump_hex(Label, Bin) -> 
        io:format("~s: ~s~n", [Label, [io_lib:format("~2.16.0B",[X]) || <<X:8>> <= Bin]]).

And I get this result:

    Private key: 16633B3A999AE563D98EAA695269CC0B37F965986D296DC31286E4BD538B1C5C
    Public key: 047D5ED8E5B1843FF0EF0F443419E3DC589D900F3C683D2D7268AC1D9FE47F7E7350A1...(cut)
But I am expected to find this: 7D5ED8E5B1843FF0EF0F443419E3DC589D900F3C683D2D7268AC1D9FE47F7E7350A...(cut)

At first I was quite discouraged because crypto is hard to learn, but after giving a closer look I found that I have the correct key actually. I just have an extra leading byte. The expected length is 512 bits and I have 520.

Expected   7D5ED8E5B1843FF0EF0F443419E3DC589D900F3C683D...(cut)
Mine     047D5ED8E5B1843FF0EF0F443419E3DC589D900F3C683D...(cut)

Do you know why the crypto library behaves like this ? Can I just cut the extra leading byte ? 

Thank you

- lud

More information about the erlang-questions mailing list