MCPcopy Create free account
hub / github.com/ElementsProject/elements / sign_ecdsa

Method sign_ecdsa

test/functional/test_framework/key.py:382–404  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

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
406def compute_xonly_pubkey(key):
407 """Compute an x-only (32 byte) public key from a (32 byte) private key.

Callers 8

sign_p2pk_witness_inputFunction · 0.80
run_testMethod · 0.80
sign_txMethod · 0.80
default_signatureFunction · 0.80
sign_txMethod · 0.80

Calls 5

rfc6979_nonceFunction · 0.85
modinvFunction · 0.85
randrangeMethod · 0.80
affineMethod · 0.80
mulMethod · 0.45

Tested by 2