{"id":509,"date":"2026-11-13T13:40:03","date_gmt":"2026-11-13T05:40:03","guid":{"rendered":"https:\/\/hoho.live\/luda\/index.php\/2026\/11\/13\/how-x25519-multiplies-a-point-on-curve25519\/"},"modified":"2026-11-13T21:59:27","modified_gmt":"2026-11-13T13:59:27","slug":"how-x25519-multiplies-a-point-on-curve25519","status":"publish","type":"post","link":"https:\/\/hoho.live\/luda\/index.php\/2026\/11\/13\/how-x25519-multiplies-a-point-on-curve25519\/","title":{"rendered":"How X25519 multiplies a point on Curve25519"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><strong>Short answer:<\/strong> <strong>X25519<\/strong> 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 <code>(x, y)<\/code> pair. The fragment uses the RFC 7748 scalar and the base point 9.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>What the problem actually asks<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">Curve25519 is the Montgomery curve <code>y^2 = x^3 + 486662 x^2 + x<\/code> over the field <code>2^255 - 19<\/code>. The ladder updates two points from the u-coordinate alone, using the constant <code>a24 = 121665<\/code>, which is <code>(486662 - 2) \/ 4<\/code>. 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.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>What the algorithm guarantees<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">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&#8217;s public key.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>The working steps<\/strong><\/h4>\n\n<ol class=\"wp-block-list\">\n<li>Clamp the 32-byte scalar: clear bits 0, 1, and 2, clear bit 255, and set bit 254.<\/li>\n<li>Run the Montgomery ladder for bits 254 down to 0, conditionally swapping the two working points.<\/li>\n<li>Recover the u-coordinate as <code>x2 * z2^(-1)<\/code> modulo <code>2^255 - 19<\/code>.<\/li>\n<li>Encode it as 32 little-endian bytes.<\/li>\n<\/ol>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>Approaches that look simpler and fail<\/strong><\/h4>\n\n<figure class=\"wp-block-table\">\n<table>\n<thead>\n<tr>\n<th>Approach<\/th>\n<th>Why it fails when the encoding or the clamp is skipped<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Skip the clamp<\/td>\n<td>The published function clears and sets those bits before the ladder. An unclamped scalar is a different multiplier.<\/td>\n<\/tr>\n<tr>\n<td>Encode the coordinate big-endian<\/td>\n<td>The 32-byte public value is little-endian.<\/td>\n<\/tr>\n<tr>\n<td>Use the 32 bytes directly as a cipher key<\/td>\n<td>The output is a shared field element. A key derivation function labels and extracts the key.<\/td>\n<\/tr>\n<tr>\n<td>Accept any peer public key with no separate authentication<\/td>\n<td>The ladder agrees on a point. It does not say which peer sent the public key.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n\n\n<p class=\"wp-block-paragraph\">The toy ECDH example is the same commutation on a curve small enough to add by hand. X25519 is the Curve25519 ladder.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>A working example<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">This Python fragment clamps the RFC 7748 scalar and multiplies the base point 9. It prints <code>1c9fd88f45606d932a80c71824ae151d15d73e77de38e8e000852e614fae7019<\/code>.<\/p>\n\n<pre><code class=\"language-python\">def x25519(scalar: bytes, u: int) -&gt; int:\n    clamped = bytearray(scalar)\n    clamped[0] &amp;= 248\n    clamped[31] &amp;= 127\n    clamped[31] |= 64\n    k = int.from_bytes(clamped, &quot;little&quot;)\n    p = 2**255 - 19\n    a24 = 121665\n    x1 = u\n    x2, z2, x3, z3 = 1, 0, u, 1\n    swap = 0\n    for t in range(254, -1, -1):\n        kt = (k &gt;&gt; t) &amp; 1\n        swap ^= kt\n        if swap:\n            x2, x3 = x3, x2\n            z2, z3 = z3, z2\n        swap = kt\n        a = (x2 + z2) % p\n        aa = (a * a) % p\n        b = (x2 - z2) % p\n        bb = (b * b) % p\n        e = (aa - bb) % p\n        c = (x3 + z3) % p\n        d = (x3 - z3) % p\n        da = (d * a) % p\n        cb = (c * b) % p\n        x3 = pow(da + cb, 2, p)\n        z3 = (x1 * pow(da - cb, 2, p)) % p\n        x2 = (aa * bb) % p\n        z2 = (e * (aa + a24 * e)) % p\n    return (x2 * pow(z2, -1, p)) % p\n\nscalar = bytes.fromhex(&quot;a546e36bf0527c9d3b16154b82465edd62144c0ac1fc5a18506a2244ba449ac4&quot;)\nshared = x25519(scalar, 9).to_bytes(32, &quot;little&quot;)\nassert shared.hex() == &quot;1c9fd88f45606d932a80c71824ae151d15d73e77de38e8e000852e614fae7019&quot;\nprint(shared.hex())\n<\/code><\/pre>\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>Where it is used<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\"><strong>TLS 1.3<\/strong>, <strong>WireGuard<\/strong>, <strong>Signal<\/strong>, 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.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>When you can drop this algorithm<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">Drop an unclamped or big-endian variant of this ladder. Keep X25519 where each side clamps its scalar, the peer&#8217;s public key is authenticated by a signature or a stored key, and the shared bytes pass through HKDF.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>Why this stays published<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">We publish this so a search for X25519 returns the clamp, the Montgomery ladder, and the RFC 7748 shared secret. <strong>X25519 clamps a 32-byte scalar and multiplies a Curve25519 u-coordinate with the Montgomery ladder.<\/strong><\/p>\n\n\n<p class=\"wp-block-paragraph\">\u672c\u6587\u7531 HoHo \u8207 AI \u5354\u4f5c\u6574\u7406\uff0c\u6700\u5f8c\u66f4\u65b0\u65bc 2026 \u5e74 11 \u6708 13 \u65e5\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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&hellip;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[30,49],"tags":[],"class_list":["post-509","post","type-post","status-publish","format-standard","hentry","category-algorithms","category-cryptography"],"_links":{"self":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/509","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/comments?post=509"}],"version-history":[{"count":1,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/509\/revisions"}],"predecessor-version":[{"id":721,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/509\/revisions\/721"}],"wp:attachment":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/media?parent=509"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/categories?post=509"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/tags?post=509"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}