https://bitcointalksearch.org/topic/bad-signatures-leading-to-5582152538-btc-theft-so-far-271486
here is some references you can read:
RFC doc: https://tools.ietf.org/html/rfc6979
electrum source code: https://github.com/spesmilo/electrum/blob/428b63822b359d56d6ececabf406a43589545d24/electrum/ecc.py#L417
the library it calls: https://github.com/warner/python-ecdsa/blob/aea736c610752bf1478febfd15b11c711debcf61/src/ecdsa/keys.py#L1022-L1055
there is also libsec256k1 which electrum uses but i couldn't find the code there. you can look into that too.
https://tools.ietf.org/html/rfc6979