{"id":1819,"date":"2022-02-12T23:18:55","date_gmt":"2022-02-12T23:18:55","guid":{"rendered":"http:\/\/www.ishygddt.xyz\/~blog\/?p=1819"},"modified":"2023-06-11T01:04:33","modified_gmt":"2023-06-11T06:04:33","slug":"x25519-original-specification","status":"publish","type":"post","link":"http:\/\/www.ishygddt.xyz\/~blog\/2022\/02\/x25519-original-specification","title":{"rendered":"X25519 original specification"},"content":{"rendered":"<p>Adapted from the <a href=\"https:\/\/cr.yp.to\/ecdh\/curve25519-20060209.pdf\">original specification<\/a>:<\/p>\n<blockquote><p>This section defines what is <a href=\"https:\/\/web.archive.org\/web\/20141210051053\/www.ietf.org\/mail-archive\/web\/cfrg\/current\/msg04996.html\">now known as<\/a> the X25519 function.<\/p>\n<p><strong>Theorem 2.1<\/strong>: Let <span lang=\"x-mathjax\">$p$<\/span> be a prime number with <span lang=\"x-mathjax\">$p\\geq 5$<\/span>. Let <span lang=\"x-mathjax\">$A$<\/span> be an integer such that <span lang=\"x-mathjax\">$A^2 &#8211; 4$<\/span> is not a square modulo <span lang=\"x-mathjax\">$p$<\/span>. Define <span lang=\"x-mathjax\">$E$<\/span> as the elliptic curve <span lang=\"x-mathjax\">$y^2=x^3+Ax^2+x$<\/span> over the field <span lang=\"x-mathjax\">$\\mathbb{F}_p$<\/span>. Define <span lang=\"x-mathjax\">$X_0:E\\left(\\mathbb{F}_{p^2}\\right)\\to\\mathbb{F}_{p^2}$<\/span> as follows: <span lang=\"x-mathjax\">$X_0\\left(\\infty\\right)=0;X_0\\left(x,y\\right)=x$<\/span>. Let <span lang=\"x-mathjax\">$n$<\/span> be an integer. Let <span lang=\"x-mathjax\">$q$<\/span> be an element of <span lang=\"x-mathjax\">$\\mathbb{F}_p$<\/span>. Then there exists a unique <span lang=\"x-mathjax\">$s\\in\\mathbb{F}_p$<\/span> such that <span lang=\"x-mathjax\">$X_0\\left(nQ\\right)=s$<\/span> for all <span lang=\"x-mathjax\">$Q\\in E\\left(\\mathbb{F}_{p^2}\\right)$<\/span> such that <span lang=\"x-mathjax\">$X_0\\left(Q\\right)=q$<\/span>.<\/p>\n<p>In particular, define <span lang=\"x-mathjax\">$p$<\/span> as the prime <span lang=\"x-mathjax\">$2^{255}-19$<\/span>. Define <span lang=\"x-mathjax\">$\\mathbb{F}_p$<\/span> as the prime field <span lang=\"x-mathjax\">$\\mathbb{Z}\/p=\\mathbb{Z}\/{(2^{255}-19})$<\/span>. Note that <span lang=\"x-mathjax\">$2$<\/span> is not a square in <span lang=\"x-mathjax\">$\\mathbb{F}_p$<\/span>; define <span lang=\"x-mathjax\">$\\mathbb{F}_{p^2}$<\/span> as the field <span lang=\"x-mathjax\">$\\left(\\mathbb{Z}\/{(2^{255}-19)}\\right){[\\sqrt{2}]}$<\/span>. Define <span lang=\"x-mathjax\">$A=486662$<\/span>. Note that <span lang=\"x-mathjax\">$486662^2-4$<\/span> is not a square in <span lang=\"x-mathjax\">$\\mathbb{F}_p$<\/span>. Define <span lang=\"x-mathjax\">$E$<\/span> as the elliptic curve <span lang=\"x-mathjax\">$y^2=x^3+Ax^2+x$<\/span> over <span lang=\"x-mathjax\">$\\mathbb{F}_p$<\/span>. Define a function <span lang=\"x-mathjax\">$X_0: E\\left(\\mathbb{F}_{p^2}\\right)\\to\\mathbb{F}_{p^2}$<\/span> as follows: <span lang=\"x-mathjax\">$X_0\\left(\\infty\\right)=0;X_0\\left(x,y\\right)=x$<\/span>. Define <span lang=\"x-mathjax\">$E$<\/span> as the elliptic curve <span lang=\"x-mathjax\">$y^2=x^3+Ax^2+x$<\/span> over <span lang=\"x-mathjax\">$\\mathbb{F}_p$<\/span>. Define a function <span lang=\"x-mathjax\">$X: E\\left(\\mathbb{F}_{p^2}\\right)\\to\\left\\{\\infty\\right\\}\\cup\\mathbb{F}_{p^2}$<\/span> as follows: <span lang=\"x-mathjax\">$X\\left(\\infty\\right)=\\infty;X\\left(x,y\\right)=x$<\/span>.<\/p>\n<p>Given <span lang=\"x-mathjax\">$n\\in 2^{254}+8\\left\\{0,1,2,3,\\ldots,2^{251}-1\\right\\}$<\/span> and <span lang=\"x-mathjax\">$q\\in\\mathbb{F}_p$<\/span>, the <span lang=\"x-mathjax\">$\\text{X25519}$<\/span> function produces <span lang=\"x-mathjax\">$s$<\/span> in Theorem 2.1.<\/p>\n<p>Now <span lang=\"x-mathjax\">$\\text{X25519}:\\left\\{\\text{X25519 secret keys}\\right\\}\\times\\left\\{\\text{X25519 public keys}\\right\\}\\to{\\{0,1,\\ldots 2^{8}-1\\}}^{32}$<\/span> is defined as follows. Fix <span lang=\"x-mathjax\">$q\\in\\left\\{0,1,\\ldots,2^{256}-1\\right\\}$<\/span> and <span lang=\"x-mathjax\">$n\\in 2^{254}+\\left\\{0,1,2,3,\\ldots,2^{251}-1\\right\\}$<\/span>. By Theorem 2.1, there is a unique integer <span lang=\"x-mathjax\">$s\\in\\left\\{0,1,2,\\ldots,2^{255}-20\\right\\}$<\/span> with the following property: <span lang=\"x-mathjax\">$s=X_0\\left(nQ\\right)$<\/span> for all <span lang=\"x-mathjax\">$Q\\in E\\left(\\mathbb{F}_{p^2}\\right)$<\/span> such that <span lang=\"x-mathjax\">$X_0\\left(Q\\right)=q\\text{ mod }2^{255}-19$<\/span>. Finally, <span lang=\"x-mathjax\">$\\text{X25519}{(\\underline{n},\\underline{q})}$<\/span> is defined as <span lang=\"x-mathjax\">$\\underline{s}$<\/span>. Note that <span lang=\"x-mathjax\">$\\text{X25519}$<\/span> is not surjective: in particular, its final output bit is <span lang=\"x-mathjax\">$0$<\/span> and need not be transmitted.<\/p><\/blockquote>\n<p>The author then proceeds to give some highly applied implementation details to do with bit twiddling algebraic polynomials, reductions around the CPU register size for constant-time arithmetic and whatnot\u2026 but, if my understanding is correct, the above quote contains the full specification of the <strong>function itself<\/strong>, if you have enough mathematical knowledge to wring it out of there (left, canonically, as an exercise for the reader; the paper <a href=\"https:\/\/miniwa.moe\/notice\/AGmc2kqavBFB3uyFcG\">excuses its terseness<\/a> by recommending some math textbooks).<\/p>\n<p>There have been some fantastic attempts [<a href=\"https:\/\/martin.kleppmann.com\/papers\/curve25519.pdf\">Martin Kleppman<\/a>; <a href=\"https:\/\/medium.com\/asecuritysite-when-bob-met-alice\/authenticated-ecdh-in-python-using-x25519-7fcf66cc455c\">Bill Buchanan<\/a>] to bridge this gap and explain the <em>core function<\/em> at a humanly-comprehensible level, but they, like the original paper, all jump straight from the mathematically pure high-level descriptions of \"point operations on generating curves\" to the extremely technical low-level \"scalar multiplications with montgomery ladders\", without ever taking a segue through \"just normal numbers\". If I cannot find such an explanation myself, I may have to learn the relevant mathematics and <em>write<\/em> one.<\/p>\n<hr \/>\n<p>Though, perhaps crafting such a document wouldn't be worth the investment: X25519 isn't quantum-safe, and <a href=\"https:\/\/classic.mceliece.org\/comparison.html\">what may be the best upcoming quantum-safe algorithm<\/a> (produced by\u2014<a href=\"https:\/\/classic.mceliece.org\/people.html\">among others<\/a>\u2014the very same gentleman that made X25519) has already got a <a href=\"http:\/\/webpages.uah.edu\/~je0029\/archive\/surface.syr.edu\/honors_capstone\/845.pdf\"><!--integrity=\"ipfs-mAVUSIJyxnrlw3SWYGmusKQDksv\/NTWeJ4JDY7m9IovbXstuX\"-->fantastic explainer by Ashley Valentijn of Syracuse University<\/a>, published as her honors capstone project. Better, perhaps, to study up about that, instead.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Adapted from the original specification<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6,101],"tags":[102,103],"class_list":["post-1819","post","type-post","status-publish","format-standard","hentry","category-drafts","category-writeups","tag-cryptography","tag-elliptic-curves"],"_links":{"self":[{"href":"http:\/\/www.ishygddt.xyz\/~blog\/wp-json\/wp\/v2\/posts\/1819","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.ishygddt.xyz\/~blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.ishygddt.xyz\/~blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.ishygddt.xyz\/~blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/www.ishygddt.xyz\/~blog\/wp-json\/wp\/v2\/comments?post=1819"}],"version-history":[{"count":51,"href":"http:\/\/www.ishygddt.xyz\/~blog\/wp-json\/wp\/v2\/posts\/1819\/revisions"}],"predecessor-version":[{"id":2854,"href":"http:\/\/www.ishygddt.xyz\/~blog\/wp-json\/wp\/v2\/posts\/1819\/revisions\/2854"}],"wp:attachment":[{"href":"http:\/\/www.ishygddt.xyz\/~blog\/wp-json\/wp\/v2\/media?parent=1819"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.ishygddt.xyz\/~blog\/wp-json\/wp\/v2\/categories?post=1819"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.ishygddt.xyz\/~blog\/wp-json\/wp\/v2\/tags?post=1819"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}