Short answer: X25519 is Diffie-Hellman on Curve25519. The scalar is clamped before the ladder runs: the low three bits of the first byte are cleared, the high bit of the last byte is cleared, and bit 254 is set. The Montgomery ladder multiplies the u-coordinate by that scalar. The public value is a u-coordinate, not an (x, y) pair. The fragment uses the RFC 7748 scalar and the base point 9.
What the problem actually asks
Curve25519 is the Montgomery curve y^2 = x^3 + 486662 x^2 + x over the field 2^255 - 19. The ladder updates two points from the u-coordinate alone, using the constant a24 = 121665, which is (486662 - 2) / 4. Clamping clears the bits that would make small-order inputs and non-canonical scalars more useful, and it forces the scalar into the intended range. The output is the u-coordinate of the shared point, encoded as 32 little-endian bytes.
What the algorithm guarantees
The same clamped scalar and the same u-coordinate reproduce the same 32 bytes. Multiplying the base point 9 by the RFC scalar produces the published shared secret checked here. A second party multiplies that public u-coordinate by a second scalar. The raw 32 bytes still go through a key derivation function before they become an AES or ChaCha20 key. The ladder does not authenticate the peer’s public key.
The working steps
- Clamp the 32-byte scalar: clear bits 0, 1, and 2, clear bit 255, and set bit 254.
- Run the Montgomery ladder for bits 254 down to 0, conditionally swapping the two working points.
- Recover the u-coordinate as
x2 * z2^(-1)modulo2^255 - 19. - Encode it as 32 little-endian bytes.
Approaches that look simpler and fail
| Approach | Why it fails when the encoding or the clamp is skipped |
|---|---|
| Skip the clamp | The published function clears and sets those bits before the ladder. An unclamped scalar is a different multiplier. |
| Encode the coordinate big-endian | The 32-byte public value is little-endian. |
| Use the 32 bytes directly as a cipher key | The output is a shared field element. A key derivation function labels and extracts the key. |
| Accept any peer public key with no separate authentication | The ladder agrees on a point. It does not say which peer sent the public key. |
The toy ECDH example is the same commutation on a curve small enough to add by hand. X25519 is the Curve25519 ladder.
A working example
This Python fragment clamps the RFC 7748 scalar and multiplies the base point 9. It prints 1c9fd88f45606d932a80c71824ae151d15d73e77de38e8e000852e614fae7019.
def x25519(scalar: bytes, u: int) -> int:
clamped = bytearray(scalar)
clamped[0] &= 248
clamped[31] &= 127
clamped[31] |= 64
k = int.from_bytes(clamped, "little")
p = 2**255 - 19
a24 = 121665
x1 = u
x2, z2, x3, z3 = 1, 0, u, 1
swap = 0
for t in range(254, -1, -1):
kt = (k >> t) & 1
swap ^= kt
if swap:
x2, x3 = x3, x2
z2, z3 = z3, z2
swap = kt
a = (x2 + z2) % p
aa = (a * a) % p
b = (x2 - z2) % p
bb = (b * b) % p
e = (aa - bb) % p
c = (x3 + z3) % p
d = (x3 - z3) % p
da = (d * a) % p
cb = (c * b) % p
x3 = pow(da + cb, 2, p)
z3 = (x1 * pow(da - cb, 2, p)) % p
x2 = (aa * bb) % p
z2 = (e * (aa + a24 * e)) % p
return (x2 * pow(z2, -1, p)) % p
scalar = bytes.fromhex("a546e36bf0527c9d3b16154b82465edd62144c0ac1fc5a18506a2244ba449ac4")
shared = x25519(scalar, 9).to_bytes(32, "little")
assert shared.hex() == "1c9fd88f45606d932a80c71824ae151d15d73e77de38e8e000852e614fae7019"
print(shared.hex())
Where it is used
TLS 1.3, WireGuard, Signal, and OpenSSH agree on a key with X25519. Each side clamps a scalar and multiplies a Curve25519 u-coordinate. The 32 shared bytes then go through HKDF before they become a traffic key.
When you can drop this algorithm
Drop an unclamped or big-endian variant of this ladder. Keep X25519 where each side clamps its scalar, the peer’s public key is authenticated by a signature or a stored key, and the shared bytes pass through HKDF.
Why this stays published
We publish this so a search for X25519 returns the clamp, the Montgomery ladder, and the RFC 7748 shared secret. X25519 clamps a 32-byte scalar and multiplies a Curve25519 u-coordinate with the Montgomery ladder.
本文由 HoHo 與 AI 協作整理,最後更新於 2026 年 11 月 13 日。
