Getting public key from private key via libnacl\libsodium (Python)

Hi all!

I wonder how to get public key from private key via libnacl\libsodium (Python).
There is already amazing implementation by Gimre by using pure Python.
I just want to try develop this part by myself so I have few questions.

This is how I try to to this by my own:

Convert private key from hex into byte string.
Reverse this string.
Make Keccak (512 bits) hash from this byte string.
Perform call of crypto_sign_ed25519_sk_to_pk function from nacl\libsodium with this binary hash as parameter.

And here is the main problem - hash is different as expected.

I also tried to call crypto_scalarmult_base with hash as parameter.
Shifting bytes (eg. myhash[0] &= 0xF8) did not help too.
I suppose to think, there is difference in scalar_multiplication function in pure python and nacl\sodium.

Can you point me what I am missing?

Thank you a lot!

you should not use crypto_sign_ed25519_sk_to_pk it probably uses different hash inside…

maybe this will help you

more specifically:

P.S. you can find vectors in https://github.com/NemProject/nem-test-vectors

Thank you, Gimer! I appreciate your answer. This is the part I am afraid of: why the heck it uses it is own hash part. I suppose to thing that there is already some SHA hashing inside.
I will look at code, thanks again.

actually, looking at the docs, you’re probably misusing crypto_sign_ed25519_sk_to_pk, you probably should call in order:

  • crypto_sign_ed25519_sk_to_seed
  • crypto_sign_seed_keypair
  • crypto_sign_ed25519_sk_to_pk

BUT, as I’ve mentioned, crypto_sign_seed_keypair inside uses sha512, take a look:

If you look at actual ed25519 paper it does not define hashing function (section 2: The signature system, EdDSA parameters), but existing implementations ususally use sha512, cause it’s easier, than passing hash into many of the functions used.

P.S. note that (besides hash and some memoves) crypto_sign_ed25519_seed_keypair is almost exactly == KeyGenerator::derivePublicKey from vanitygen

1 Like

There is no way to use some patched library? So the only way is too use pure-python?