Secp256k1 curve package¶
Pure python implementation for scp256k1 curve algebra and associated
ECDSA - SCHNORR signatures.
>>> from dposlib.ark import secp256k1
>>> G = secp256k1.Point(0x79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798)
>>> G.y
32670510020758816978083085130507043184471273380659243275938904335757337482424
>>> G
<secp256k1 point:
x:79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798
y:483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8
>
>>> G+G == 2*G
True
>>> secp256k1.PublicKey.from_int(secp256k1.int_from_bytes(secp256k1.hash_sha256("secret")))
<secp256k1 public key:
x:a02b9d5fdd1307c2ee4652ba54d492d1fd11a7d1bb3f3a44c4a05e79f19de933
y:924aa2580069952b0140d88de21c367ee4af7c4a906e1498f20ab8f62e4c2921
>
>>> secp256k1.PublicKey.from_seed(secp256k1.hash_sha256("secret"))
<secp256k1 public key:
x:a02b9d5fdd1307c2ee4652ba54d492d1fd11a7d1bb3f3a44c4a05e79f19de933
y:924aa2580069952b0140d88de21c367ee4af7c4a906e1498f20ab8f62e4c2921
>
>>> secp256k1.PublicKey.from_secret("secret")
<secp256k1 public key:
x:a02b9d5fdd1307c2ee4652ba54d492d1fd11a7d1bb3f3a44c4a05e79f19de933
y:924aa2580069952b0140d88de21c367ee4af7c4a906e1498f20ab8f62e4c2921
>
- Sources:
- Variables:
secret(str): passphrasesecret0(bytes): private keyP(list): public key assecp256k1curve pointpubkey(bytes): compressed - encoded public keypubkeyB(bytes): compressed - encoded public key according to bip schnorr specmsg(bytes): sha256 hash of message to sign- Uppercase variables refer to points on the curve with equation
y²=x³+7over the integers modulo p
-
class
dposlib.ark.secp256k1.Point(*xy)[source]¶ secp256k1point . Initialization can be done with solexvalue.Pointoverrides*and+operators which acceptslistas argument and returnsPoint.-
static
decode(pubkey)[source]¶ See
point_from_encoded().
-
encode()[source]¶ See
encoded_from_point().
-
x¶ Return list item #0
-
y¶ Return list item #1
-
static
-
class
dposlib.ark.secp256k1.PublicKey(*xy)[source]¶ Pointextension providing specific initialization methods.-
static
from_int(value)[source]¶ Compute a public key from
intvalue.Parameters: value ( int) – scalar to useReturns: the public key Return type: PublicKey
-
static
-
dposlib.ark.secp256k1.der_from_sig(r, s)[source]¶ Encode a signature according
DERspec.Parameters: - r (
int) – signature part #1 - s (
int) – signature part #2
Returns: encoded signature
Return type: bytes- r (
-
dposlib.ark.secp256k1.encoded_from_point(P)[source]¶ - Encode and compress a
secp256k1point: bytes(2) || bytes(x)if y is evenbytes(3) || bytes(x)if y is odd
Parameters: P ( list) –secp256k1pointReturns: compressed and encoded point Return type: bytes- Encode and compress a
-
dposlib.ark.secp256k1.hash_sha256(b)[source]¶ Parameters: b ( bytesorstr) – sequence to be hashedReturns: sha256 hash Return type: bytes
-
dposlib.ark.secp256k1.point_add(P1, P2)[source]¶ Add
secp256k1points.Parameters: - P1 (
list) – firstsecp256k1point - P2 (
list) – secondsecp256k1point
Returns: secp256k1pointReturn type: list- P1 (
-
dposlib.ark.secp256k1.point_from_encoded(pubkey)[source]¶ Decode and decompress a
secp256k1point.Parameters: pubkey ( bytes) – compressed and encoded pointReturns: secp256k1pointReturn type: list
-
dposlib.ark.secp256k1.point_mul(P, n)[source]¶ Multiply
secp256k1point with scalar.Parameters: - P (
list) –secp256k1point - n (
int) – scalar
Returns: secp256k1pointReturn type: list- P (
-
dposlib.ark.secp256k1.rfc6979_k(msg, secret0, V=None)[source]¶ Generate a deterministic nonce according to rfc6979 spec.
Parameters: - msg (
bytes) – 32-bytes sequence - secret0 (
bytes) – private key - V (
bytes) –
Returns: deterministic nonce
Return type: int- msg (
-
dposlib.ark.secp256k1.sig_from_der(der)[source]¶ Decode a
DERsignature.Parameters: der ( bytes) – encoded signatureReturns: signature (r, s) Return type: ( int,int)
-
dposlib.ark.secp256k1.tagged_hash(tag, msg)[source]¶ Return
sha256(sha256(tag) || sha256(tag) || msg). Tagged hash are registered to speed up code execution.Parameters: - tag (
str) – tag to use - msg (
bytes) – sha256 hash of message to sign
Returns: tagged hash
Return type: bytes- tag (
-
dposlib.ark.secp256k1.x(P)[source]¶ Return
P.xorP[0].Parameters: P ( list) –secp256k1pointReturns: x Return type: int
-
dposlib.ark.secp256k1.y(P)[source]¶ Return
P.yorP[1].Parameters: P ( list) –secp256k1pointReturns: y Return type: int
ECDSA signatures¶
-
dposlib.ark.secp256k1.ecdsa.rfc6979_sign(msg, secret0, canonical=True)[source]¶ Generate signature according to
ECDSAscheme using a RFC-6979 nonceParameters: - msg (
bytes) – sha256 message-hash - secret0 (
bytes) – private key - canonical (
bool) – canonalize signature
Returns: DER signature
Return type: bytes- msg (
-
dposlib.ark.secp256k1.ecdsa.sign(msg, secret0, k=None, canonical=True)[source]¶ Generate signature according to
ECDSAscheme.Parameters: - msg (
bytes) – sha256 message-hash - secret0 (
bytes) – private key - k (
int) – nonce (random nonce used if k=None) - canonical (
bool) – canonalize signature
Returns: DER signature
Return type: bytes- msg (
Schnorr signatures¶
-
dposlib.ark.secp256k1.schnorr.bcrypto410_sign(msg, seckey0)[source]¶ Generate message signature according to Bcrypto 4.10 schnorr spec.
Parameters: - msg (
bytes) – sha256 message-hash - secret0 (
bytes) – private key
Returns: RAW signature
Return type: bytes- msg (
-
dposlib.ark.secp256k1.schnorr.bcrypto410_verify(msg, pubkey, sig)[source]¶ Check if public key match message signature according to Bcrypto 4.10 schnorr spec.
Parameters: - msg (
bytes) – sha256 message-hash - pubkey (
bytes) – encoded public key - sig (
bytes) – signature
Returns: True if match
Return type: bool- msg (
-
dposlib.ark.secp256k1.schnorr.bytes_from_point(P)[source]¶ Encode a public key as defined in BIP schnorr spec.
Parameters: P ( Point) – secp256k1 curve pointReturns: encoded public key Return type: bytes
-
dposlib.ark.secp256k1.schnorr.point_from_bytes(pubkeyB)[source]¶ Decode a public key as defined in BIP schnorr spec.
Parameters: pubkeyB ( bytes) – encoded public keyReturns: secp256k1 curve point Return type: Point
-
dposlib.ark.secp256k1.schnorr.sign(msg, seckey0)[source]¶ Generate message signature according to BIP schnorr spec.
Parameters: - msg (
bytes) – sha256 message-hash - seckey0 (
bytes) – private key
Returns: RAW signature
Return type: bytes- msg (
-
dposlib.ark.secp256k1.schnorr.verify(msg, pubkey, sig)[source]¶ Check if public key match message signature according to BIP schnorr spec.
Parameters: - msg (
bytes) – sha256 message-hash - pubkey (
bytes) – encoded public key - sig (
bytes) – signature
Returns: True if match
Return type: bool- msg (