Construct a DER-encoded ECDSA signature with this key. See https://en.wikipedia.org/wiki/Elliptic_Curve_Digital_Signature_Algorithm for the ECDSA signer algorithm.
(self, msg, low_s=True, rfc6979=False)
| 380 | return ret |
| 381 | |
| 382 | def sign_ecdsa(self, msg, low_s=True, rfc6979=False): |
| 383 | """Construct a DER-encoded ECDSA signature with this key. |
| 384 | |
| 385 | See https://en.wikipedia.org/wiki/Elliptic_Curve_Digital_Signature_Algorithm for the |
| 386 | ECDSA signer algorithm.""" |
| 387 | assert(self.valid) |
| 388 | z = int.from_bytes(msg, 'big') |
| 389 | # Note: no RFC6979 by default, but a simple random nonce (some tests rely on distinct transactions for the same operation) |
| 390 | if rfc6979: |
| 391 | k = int.from_bytes(rfc6979_nonce(self.secret.to_bytes(32, 'big') + msg), 'big') |
| 392 | else: |
| 393 | k = random.randrange(1, SECP256K1_ORDER) |
| 394 | R = SECP256K1.affine(SECP256K1.mul([(SECP256K1_G, k)])) |
| 395 | r = R[0] % SECP256K1_ORDER |
| 396 | s = (modinv(k, SECP256K1_ORDER) * (z + self.secret * r)) % SECP256K1_ORDER |
| 397 | if low_s and s > SECP256K1_ORDER_HALF: |
| 398 | s = SECP256K1_ORDER - s |
| 399 | # Represent in DER format. The byte representations of r and s have |
| 400 | # length rounded up (255 bits becomes 32 bytes and 256 bits becomes 33 |
| 401 | # bytes). |
| 402 | rb = r.to_bytes((r.bit_length() + 8) // 8, 'big') |
| 403 | sb = s.to_bytes((s.bit_length() + 8) // 8, 'big') |
| 404 | return b'\x30' + bytes([4 + len(rb) + len(sb), 2, len(rb)]) + rb + bytes([2, len(sb)]) + sb |
| 405 | |
| 406 | def compute_xonly_pubkey(key): |
| 407 | """Compute an x-only (32 byte) public key from a (32 byte) private key. |