--- /dev/null
+/* -*-c-*-
+ *
+ * The X25519 key-agreement algorithm
+ *
+ * (c) 2017 Straylight/Edgeware
+ */
+
+/*----- Licensing notice --------------------------------------------------*
+ *
+ * This file is part of secnet.
+ * See README for full list of copyright holders.
+ *
+ * secnet is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version d of the License, or
+ * (at your option) any later version.
+ *
+ * secnet is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * version 3 along with secnet; if not, see
+ * https://www.gnu.org/licenses/gpl.html.
+ *
+ * This file was originally part of Catacomb, but has been automatically
+ * modified for incorporation into secnet: see `import-catacomb-crypto'
+ * for details.
+ *
+ * Catacomb is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Library General Public License as
+ * published by the Free Software Foundation; either version 2 of the
+ * License, or (at your option) any later version.
+ *
+ * Catacomb is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with Catacomb; if not, write to the Free
+ * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
+ * MA 02111-1307, USA.
+ */
+
+/*----- Header files ------------------------------------------------------*/
+
+#include "fake-mLib-bits.h"
+
+#include "montladder.h"
+#include "f25519.h"
+#include "x25519.h"
+
+/*----- Important constants -----------------------------------------------*/
+
+const octet x25519_base[32] = { 9, 0, /* ... */ };
+
+#define A0 121665
+
+/*----- Main code ---------------------------------------------------------*/
+
+/* --- @x25519@ --- *
+ *
+ * Arguments: @octet zz[X25519_OUTSZ]@ = where to put the result
+ * @const octet k[X25519_KEYSZ]@ = pointer to private key
+ * @const octet qx[X25519_PUBSZ]@ = pointer to public value
+ *
+ * Returns: ---
+ *
+ * Use: Calculates X25519 of @k@ and @qx@.
+ *
+ * Note that there is disagreement over whether the most
+ * significant bit of @qx@ (i.e., the value @qx[31]&0x80@)
+ * should be ignored or counted towards the represented value.
+ * Historically implementations respected the bit; later
+ * convention seems to be to ignore it. This implementation
+ * honours the bit: a caller who wants to ignore the bit can
+ * easily clear it, while caller who wants to respect it has a
+ * difficult job if this function ignores it.
+ */
+
+void x25519(octet zz[X25519_OUTSZ],
+ const octet k[X25519_KEYSZ],
+ const octet qx[X25519_PUBSZ])
+{
+ uint32 kw[8];
+ f25519 x1;
+
+ /* Load and clamp the key. The low bits are cleared to kill the small
+ * subgroups on the curve and its twist, and a high bit is set to guard
+ * against careless implementations, though this isn't one of those.
+ */
+ kw[0] = LOAD32_L(k + 0); kw[1] = LOAD32_L(k + 4);
+ kw[2] = LOAD32_L(k + 8); kw[3] = LOAD32_L(k + 12);
+ kw[4] = LOAD32_L(k + 16); kw[5] = LOAD32_L(k + 20);
+ kw[6] = LOAD32_L(k + 24); kw[7] = LOAD32_L(k + 28);
+ kw[0] &= 0xfffffff8; kw[7] = (kw[7]&0x3fffffff) | 0x40000000;
+
+ /* And run the ladder. */
+ f25519_load(&x1, qx);
+#define MULA0(z, x) do { f25519_mulconst((z), (x), A0); } while (0)
+ MONT_LADDER(f25519, MULA0, kw, 8, 32, &x1, &x1);
+#undef MULA0
+ f25519_store(zz, &x1);
+}
+
+/*----- That's all, folks -------------------------------------------------*/