<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by 0xMouiz on Medium]]></title>
        <description><![CDATA[Stories by 0xMouiz on Medium]]></description>
        <link>https://medium.com/@mouiz?source=rss-30a9a7d123e1------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*Am990Hmm31Bs1uNqfQW1gQ.jpeg</url>
            <title>Stories by 0xMouiz on Medium</title>
            <link>https://medium.com/@mouiz?source=rss-30a9a7d123e1------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 06 Dec 2026 00:35:11 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@mouiz/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Building Bitcoin’s secp256k1 Curve and ECDSA from Scratch in Python]]></title>
            <link>https://medium.com/@mouiz/building-bitcoins-secp256k1-curve-and-ecdsa-from-scratch-in-python-4f921a61caeb?source=rss-30a9a7d123e1------2</link>
            <guid isPermaLink="false">https://medium.com/p/4f921a61caeb</guid>
            <category><![CDATA[ethereum]]></category>
            <category><![CDATA[algorithms]]></category>
            <category><![CDATA[blockchain]]></category>
            <category><![CDATA[bitcoin]]></category>
            <category><![CDATA[python]]></category>
            <dc:creator><![CDATA[0xMouiz]]></dc:creator>
            <pubDate>Tue, 12 Aug 2025 01:15:15 GMT</pubDate>
            <atom:updated>2025-08-12T01:28:15.182Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/800/0*eHtBdZz4I9xVcOxL" /><figcaption>Bitcoin’s secp256k1 Curve and ECDSA from Scratch in Python</figcaption></figure><h3>Intro</h3><p>Elliptic curve cryptography (ECC) powers the security of Bitcoin, Ethereum, and countless secure systems.<br> One of the most famous curves, <strong>secp256k1</strong>, is the backbone of Bitcoin addresses and Ethereum keys.</p><p>While libraries like secp256k1 and ecdsa abstract away the math, building it yourself is a great way to understand how elliptic curves and ECDSA signatures work.</p><p>In this post, we’ll walk through my <strong>minimal Python implementation</strong> of secp256k1 — covering:</p><ul><li>The math behind the curve</li><li>Point addition, doubling, and scalar multiplication</li><li>Private/public key generation</li><li>ECDSA signing and verification with Keccak-256</li></ul><h3>1. What is secp256k1?</h3><p>secp256k1 is a Koblitz curve defined over a finite field. Its equation is:</p><p><strong><em>y² ≡ x³ + 7 (mod p)</em></strong></p><p>Where:</p><ul><li><strong><em>p</em></strong> is a large prime</li><li><strong><em>a</em></strong>=0, <strong><em>b</em></strong>=7</li><li><strong><em>G</em></strong> is the generator point</li><li><strong><em>n</em></strong> is the order of the curve</li></ul><p>It’s used in Bitcoin, Ethereum, and other blockchain systems because it offers strong security and fast computation.</p><h3>2. Implementing the Curve</h3><p>We start with a Point class for elliptic curve points, supporting:</p><ul><li><strong>Addition</strong> <strong><em>(P+Q)</em></strong></li><li><strong>Doubling</strong> <strong><em>(2P)</em></strong></li><li><strong>Scalar multiplication</strong> <strong><em>(kP)</em></strong></li></ul><pre>class Point:<br>    def __add__(self, other):<br>        if self.is_at_infinity():<br>            return other<br>        if other.is_at_infinity():<br>            return self<br>        if self == other:<br>            return self.double()<br>        if self.x == other.x and (self.y + other.y) % self.curve.p == 0:<br>            return Point(None, None, self.curve)  # point at infinity<br>        s = ((other.y - self.y) * pow(other.x - self.x, -1, self.curve.p)) % self.curve.p<br>        x_r = (s**2 - self.x - other.x) % self.curve.p<br>        y_r = (s * (self.x - x_r) - self.y) % self.curve.p<br>        return Point(x_r, y_r, self.curve)</pre><p>The Curve class enforces the curve equation and provides helper methods like tangent_slope for doubling.</p><h3>3. secp256k1 Parameters</h3><p>We define the constants exactly as per the official specification:</p><pre>p  = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F<br>a  = 0<br>b  = 7<br>Gx = 0x79BE66...<br>Gy = 0x483ADA...<br>n  = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141<br>h  = 1<br><br>curve = Curve(a, b, p, n, h, Gx, Gy, &quot;secp256k1&quot;)</pre><h3>4. Key Generation</h3><p><strong>Private</strong> <strong>key</strong>: random integer in <strong><em>[1,n−1]</em></strong><br><strong>Public</strong> <strong>key</strong>: <strong><em>priv × G</em></strong></p><pre>def generate_private_key():<br>    return secrets.randbelow(curve.n)<br><br>def derive_public_key(private_key):<br>    return private_key * curve.G</pre><h3>5. ECDSA Signing</h3><p>We use <strong>Keccak-256</strong> for hashing (Ethereum’s choice) and the standard ECDSA formula:</p><pre>def sign(msg, private_key):<br>    z = int.from_bytes(keccak256(msg), &quot;big&quot;)<br>    while True:<br>        k = secrets.randbelow(curve.n)<br>        R = k * curve.G<br>        r = R.x % curve.n<br>        s = (modinv(k, curve.n) * (z + r * private_key)) % curve.n<br>        if r != 0 and s != 0:<br>            return (r, s)</pre><h3>6. ECDSA Verification</h3><p>The verifier computes:<br><strong><em>u1 = z * w</em></strong><br><strong><em>u2 = r * w<br>P = u1 * G + u2 * Q</em></strong><br>Valid if <strong><em>Px mod n = r</em></strong></p><pre>def verify(msg, signature, public_key):<br>    r, s = signature<br>    z = int.from_bytes(keccak256(msg), &quot;big&quot;)<br>    w = modinv(s, curve.n)<br>    u1 = (z * w) % curve.n<br>    u2 = (r * w) % curve.n<br>    point = u1 * curve.G + u2 * public_key<br>    return (point.x % curve.n) == r</pre><h3>7. Demo Run</h3><pre>python3 main.py</pre><p>Example output:</p><pre>[+] Private key: 0x1393b6573adf24c61b73561768d9ea4ba1670dcc77554f25938cbca621ed7645<br>[+] Public key: 0475ca8eaf8393...<br>[+] Signature: (2313066..., 8090080...)<br>[+] Signature valid? True</pre><h3>8. Why This Matters</h3><p>This code is <strong>not for production</strong> — it’s educational.<br> It shows exactly how:</p><ul><li>Bitcoin addresses are derived</li><li>Ethereum signs transactions</li><li>ECDSA math works without relying on “magic” library calls</li></ul><h3>9. Full Source Code</h3><p><a href="https://github.com/0xMouiz/python-secp256k1">https://github.com/0xMouiz/python-secp256k1</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4f921a61caeb" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>