Skip to content

Instantly share code, notes, and snippets.

@JBaczuk
Created October 16, 2018 14:38
Show Gist options
  • Save JBaczuk/d8d082c060d19045449aa4ef34a55874 to your computer and use it in GitHub Desktop.
Save JBaczuk/d8d082c060d19045449aa4ef34a55874 to your computer and use it in GitHub Desktop.
secp256k1
#!/bin/bash
if [[ $# -lt 1 ]]; then
echo "Usage: $ secp256k1 <private-key-hex>"
exit 1
fi
priv=$1
## Calculate Public Keys
public_key=$(openssl ec -inform DER -text -noout -in <(cat <(echo -n "302e0201010420") <(echo -n $priv) <(echo -n "a00706052b8104000a") | xxd -r -p) 2>/dev/null | tail -6 | head -5 | sed 's/[ :]//g' | tr -d '\n' && echo)
echo "Public Key: "$public_key
x_coord=$(printf $public_key | cut -c -66 | cut -c 3-)
last_byte=$(printf $public_key | cut -c 129-)
last_int=$(printf "%d" 0x$last_byte)
is_odd=$(expr $last_int % 2)
if [ "$is_odd" == 1 ]; then
compressed_public_key=03$x_coord
else
compressed_public_key=02$x_coord
fi
echo "Compressed Public Key: "$compressed_public_key
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment