MCPcopy
hub / github.com/petertodd/python-bitcoinlib / signature_to_low_s

Method signature_to_low_s

bitcoin/core/key.py:399–426  ·  view source on GitHub ↗
(self, sig)

Source from the content-addressed store, hash-verified

397 raise ValueError
398
399 def signature_to_low_s(self, sig):
400 der_sig = ECDSA_SIG_st()
401 _ssl.d2i_ECDSA_SIG(ctypes.byref(ctypes.pointer(der_sig)), ctypes.byref(ctypes.c_char_p(sig)), len(sig))
402 group = _ssl.EC_KEY_get0_group(self.k)
403 order = _ssl.BN_new()
404 halforder = _ssl.BN_new()
405 ctx = _ssl.BN_CTX_new()
406 _ssl.EC_GROUP_get_order(group, order, ctx)
407 _ssl.BN_rshift1(halforder, order)
408
409 # Verify that s is over half the order of the curve before we actually subtract anything from it
410 if _ssl.BN_cmp(der_sig.s, halforder) > 0:
411 _ssl.BN_sub(der_sig.s, order, der_sig.s)
412
413 _ssl.BN_free(halforder)
414 _ssl.BN_free(order)
415 _ssl.BN_CTX_free(ctx)
416
417 derlen = _ssl.i2d_ECDSA_SIG(ctypes.pointer(der_sig), 0)
418 if derlen == 0:
419 _ssl.ECDSA_SIG_free(der_sig)
420 return None
421 new_sig = ctypes.create_string_buffer(derlen)
422 _ssl.i2d_ECDSA_SIG(ctypes.pointer(der_sig), ctypes.byref(ctypes.pointer(new_sig)))
423 _ssl.BN_free(der_sig.r)
424 _ssl.BN_free(der_sig.s)
425
426 return new_sig.raw
427
428 def verify(self, hash, sig): # pylint: disable=redefined-builtin
429 """Verify a DER signature"""

Callers 2

signMethod · 0.95
sign_compactMethod · 0.95

Calls 1

ECDSA_SIG_stClass · 0.85

Tested by

no test coverage detected