embit/docs/api/ec
2021-08-20 01:16:14 +02:00
..
_sidebar.md finish ec docs 2021-08-16 13:28:11 +02:00
private_key.md add bip32 docs 2021-08-20 01:16:14 +02:00
public_key.md finish ec docs 2021-08-16 13:28:11 +02:00
README.md finish ec docs 2021-08-16 13:28:11 +02:00
schnorr_sig.md finish ec docs 2021-08-16 13:28:11 +02:00
signature.md finish ec docs 2021-08-16 13:28:11 +02:00

Ellictic curve keys and signatures

This module includes classes for signatures and individual private and public keys.

Defined classes:

!> Schnorr and taproot support is experimental and API is not stable yet!

Example

from embit import ec
from embit.networks import NETWORKS

# pass 32-byte big-endian secret key to the constructor
pk = ec.PrivateKey(b"1"*32)
# string representation is WIF
print(pk)
# >>> KxsLKrFM2X4k...ni3DeKDcDFeS3DU

# pass network to get WIF for it
print(pk.to_string(NETWORKS['test']))
# >>> cPEKnmFCTam1...ypDAYikyDrxLXSx4Z

# load from WIF
pk = PrivateKey.from_string("KxsLKrFM2X4kK4zkxGtmTaWv2tvyNLdZmuMWhni3DeKDcDFeS3DU")

# get corresponding public key
pub = pk.get_public_key()
# serialize to SEC format (33-byte repr)
pub.sec()
# serialize as x-only public key (taproot)
pub.xonly()
# string representation is hex of SEC
print(pub)
# >>> 036930f46dd0b1...1cafceb82

# sign a message
msg = b"5"*32 # should be a 32-byte hash of the message

# ECDSA siganture:
sig = pk.sign(msg)
# serialization - DER encoding
# string repr - hex of DER serialization
print(sig)
# >>> 304402200f735678a171...5a30e4f2f5bfd

# verify the signature
pub.verify(sig, msg)
# >>> True

# tweak private key (taproot)
# argument is the hash of tapscripts
# or empty bytes if you don't need tapscripts.
tweak = b""
tweaked_pk = pk.taproot_tweak(tweak)
# sign a message with Schnorr
schnorrsig = tweaked_pk.schnorr_sign(msg)
# serialization - 64 byte encoded sig
# string repr - hex of serialization
print(schnorrsig)
# >>> 15492285664fc22...f05fd38abdda9dd95

# verify schnorr signature
tweaked_pub = pub.taproot_tweak(tweak)
tweaked_pub.schnorr_verify(schnorrsig, msg)
# >>> True