How ECDH multiplies a shared elliptic-curve point

Short answer: ECDH agrees on a shared point. Each side multiplies the public base point by its own scalar and publishes the result. Each side then multiplies the other side’s point by its own scalar. The two products are the same point, because scalar multiplication commutes. The fragment uses the curve y^2 = x^3 + 2x + 2 over the field of 17 elements, the base point (5, 1), and the scalars 3 and 5. The shared x-coordinate is 3.


What the problem actually asks

Two parties who have never shared a secret need a value that an observer of their published points cannot compute. Each keeps a scalar and publishes the matching point. The shared point is the base point multiplied by both scalars. The raw x-coordinate is not yet a symmetric key. A deployment feeds it through a key derivation function. The 17-element field is a demonstration of the commutation. X25519 is this agreement on Curve25519, with a clamped scalar and a Montgomery ladder.


What the algorithm guarantees

With these scalars, both orders of multiplication produce the point whose x-coordinate is 3. The identity used here is (a b) G = a (b G) = b (a G). An observer who sees only the public points and can solve the discrete logarithm can compute the same point. In this group that logarithm is a short search. The fragment checks the equality, not the secrecy of a 17-element field.


The working steps

  1. Agree on a curve and a base point.
  2. Each side draws a scalar and publishes the base point times that scalar.
  3. Each side multiplies the received point by its own scalar.
  4. Use a key derivation function on the shared x-coordinate before using the result as a symmetric key.

Approaches that look simpler and fail

Approach Why it fails when the raw point is used as a key
Use the x-coordinate as an AES key The coordinate is a group element encoding. HKDF, or another labeled derivation, turns it into key bytes.
Deploy the field of 17 elements Both scalars are recoverable by trying a handful of multiples.
Skip the second multiplication and hash the published points The shared secret is the product of both scalars. The published points are public.
Reuse one scalar for every peer and never rotate it A long-term scalar is a long-term key. The fragment only shows one agreement.

X25519 is the Curve25519 form of this agreement. The toy curve is the commutation check.


A working example

This Python fragment agrees on a point of the same toy curve used for the ECDSA example. It prints 3.

def _curve_demo() -> tuple[int, int, int, int, tuple[int, int], int]:
    # y^2 = x^3 + 2x + 2 over F_17. Generator (5, 1) has order 19.
    return 17, 2, 2, 19, (5, 1), 7

def _ec_add(p, a, P, Q):
    if P is None:
        return Q
    if Q is None:
        return P
    x1, y1 = P
    x2, y2 = Q
    if x1 == x2 and (y1 + y2) % p == 0:
        return None
    if P == Q:
        lam = (3 * x1 * x1 + a) * pow(2 * y1, -1, p) % p
    else:
        lam = (y2 - y1) * pow(x2 - x1, -1, p) % p
    x3 = (lam * lam - x1 - x2) % p
    y3 = (lam * (x1 - x3) - y1) % p
    return x3, y3

def _ec_mul(p, a, k, point):
    result = None
    addend = point
    while k:
        if k & 1:
            result = _ec_add(p, a, result, addend)
        addend = _ec_add(p, a, addend, addend)
        k >>= 1
    return result

def ecdh_demo() -> str:
    p, a, _b, _n, g, _d = _curve_demo()
    da, db = 3, 5
    shared_a = _ec_mul(p, a, da, _ec_mul(p, a, db, g))
    shared_b = _ec_mul(p, a, db, _ec_mul(p, a, da, g))
    assert shared_a == shared_b
    return str(shared_a[0])

assert ecdh_demo() == "3"
print(ecdh_demo())

Where it is used

TLS key agreement on NIST curve P-256 is elliptic-curve Diffie-Hellman. The client and the server each send a point and derive the shared point. X25519 is the Curve25519 form of the same agreement and is the usual choice in new handshakes.


When you can drop this algorithm

Drop this curve for any shared secret. Keep ECDH where the curve is large, each side has a scalar the other does not see, and the shared coordinate passes through a key derivation function.


Why this stays published

We publish this so a search for ECDH returns the two scalar multiplications and the shared point. ECDH publishes aG and bG and both sides compute abG, then derive symmetric keys from that point.

本文由 HoHo 與 AI 協作整理,最後更新於 2026 年 11 月 13 日。

Leave a Reply

Your email address will not be published. Required fields are marked *