I generate the private and the public keys in Shell like that:

openssl ecparam -name secp256k1 -rand /dev/random -genkey -noout -out private-key.pem
openssl ec -in private-key.pem -outform DER | tail -c +8 | head -c 32 | xxd -p -c 32 > bitcoin_private_key.pem
openssl ec -in private-key.pem -pubout -outform DER | tail -c 65 | xxd -p -c 65 > bitcoin_public_key.pem

Then I can take the public key X with:

head -c 66 bitcoin_public_key.pem | tail -c 64

And the last byte of Y with:

tail -c 3 bitcoin_public_key.pem

The compressed public key must be 0x02 + X for an even last byte of Y and 0x03 + X for an odd one.

How to check if the last Y's byte is odd or even?

2 Answers 2


Instead of compressing 'by hand' you can have OpenSSL do it:

openssl ec -in privatekey.pem -pubout -conv_form compressed -outform der | tail -c 33 | xxd -p -c 33 >compressedpub.hex

And .pem is usually used to mean PEM format, while the files you are creating are totally not at all or in any way PEM format, so that's very misleading and confusing. I used .hex for my example, but there are other reasonable possibilities.


You just check if Y is divisible by 2 (i.e Y % 2 == 0). It's the default parity concept…

This site is temporarily in read-only mode and not accepting new answers.

Not the answer you're looking for? Browse other questions tagged .