<jupyter_start><jupyter_text>Secp256k1Constants from https://en.bitcoin.it/wiki/Secp256k1<jupyter_code>p = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F
F = FiniteField(p)
a = 0
b = 7
E = EllipticCurve(F, [a, b])
G_x = 0x79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798
h = 1 # cofactor
G = B = E.lift_x(G_x)
assert(G.order() == 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141)
assert(E.order() / G.order() == h)<jupyter_output><empty_output><jupyter_text>Key generation<jupyter_code>N = FiniteField(G.order())
def keypair (x = None):
    if x is None:
        x = int(N.random_element(1, N.order() - 1))
    P = x * G
    return (x, P)<jupyter_output><empty_output><jupyter_text>ECDSA<jupyter_code>import hashlib
def sign (m, d):
    e = int(hashlib.sha1(m).hexdigest(), 16) % N.order()
    k = int(N.random_element(1, N.order() - 1))
    R = k * G # (k, r) is like a one-time key pair
    r = int(R.xy()[0]) % N.order()
    assert(r % N.order() != 0)
    s = (e + d*r)/k % N.order()
    return [r,s]
    
def verify (sig, m, P):
    r, s = sig
    assert(0 < r < N.order())
    assert(0 < s < N.order())
    e = int(hashlib.sha1(m).hexdigest(), 16) % N.order()
    u1 = e / s % N.order()
    u2 = r / s % N.order()
    R = u1 * G + u2 * P
    v = int(R.xy()[0]) % N.order()
    return v == r<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.16896301667875271, 'fraction_numerical': 0.0630891950688905, 'mean_word_length': 3.6, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 13, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '264353', 'license': '0BSD', 'n_tokens_mistral': 659, 'n_tokens_neox': 600, 'n_words': 120, 'path': 'elliptic-curves/secp256k1-ecdsa.ipynb', 'repo_name': 'emilbayes/abstract-algebra-fun', 'size': '1379'}