Import implementations of X25519 and X448 from Catacomb.
[secnet] / fgoldi.h
diff --git a/fgoldi.h b/fgoldi.h
new file mode 100644 (file)
index 0000000..b05fd77
--- /dev/null
+++ b/fgoldi.h
@@ -0,0 +1,202 @@
+/* -*-c-*-
+ *
+ * Arithmetic in the Goldilocks field GF(2^448 - 2^224 - 1)
+ *
+ * (c) 2017 Straylight/Edgeware
+ */
+
+/*----- Licensing notice --------------------------------------------------*
+ *
+ * This file is part of Catacomb.
+ *
+ * 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.
+ */
+
+#ifndef CATACOMB_FGOLDI_H
+#define CATACOMB_FGOLDI_H
+
+#ifdef __cplusplus
+  extern "C" {
+#endif
+
+/*----- Header files ------------------------------------------------------*/
+
+#include <mLib/bits.h>
+
+#ifndef CATACOMB_QFARITH_H
+#  include "qfarith.h"
+#endif
+
+/*----- Data structures ---------------------------------------------------*/
+
+typedef union {
+  int32 p28[16];
+  int16 p12[40];
+} fgoldi;
+
+#if !defined(FGOLDI_IMPL) && defined(HAVE_INT64)
+#  define FGOLDI_IMPL 28
+#endif
+
+#ifndef FGOLDI_IMPL
+#  define FGOLDI_IMPL 12
+#endif
+
+/*----- Functions provided ------------------------------------------------*/
+
+/* --- @fgoldi_load@ --- *
+ *
+ * Arguments:  @fgoldi *z@ = where to store the result
+ *             @const octet xv[56]@ = source to read
+ *
+ * Returns:    ---
+ *
+ * Use:                Reads an element of %$\gf{2^{448} - 2^{224} - 19}$% in
+ *             external representation from @xv@ and stores it in @z@.
+ *
+ *             External representation is little-endian base-256.  Some
+ *             elements have multiple encodings, which are not produced by
+ *             correct software; use of noncanonical encodings is not an
+ *             error, and toleration of them is considered a performance
+ *             feature.
+ */
+
+extern void fgoldi_load(fgoldi */*z*/, const octet /*xv*/[56]);
+
+/* --- @fgoldi_store@ --- *
+ *
+ * Arguments:  @octet zv[56]@ = where to write the result
+ *             @const fgoldi *x@ = the field element to write
+ *
+ * Returns:    ---
+ *
+ * Use:                Stores a field element in the given octet vector in external
+ *             representation.  A canonical encoding is always stored.
+ */
+
+extern void fgoldi_store(octet /*zv*/[56], const fgoldi */*x*/);
+
+/* --- @fgoldi_set@ --- *
+ *
+ * Arguments:  @fgoldi *z@ = where to write the result
+ *             @int a@ = a small-ish constant
+ *
+ * Returns:    ---
+ *
+ * Use:                Sets @z@ to equal @a@.
+ */
+
+extern void fgoldi_set(fgoldi */*x*/, int /*a*/);
+
+/* --- @fgoldi_add@ --- *
+ *
+ * Arguments:  @fgoldi *z@ = where to put the result (may alias @x@ or @y@)
+ *             @const fgoldi *x, *y@ = two operands
+ *
+ * Returns:    ---
+ *
+ * Use:                Set @z@ to the sum %$x + y$%.
+ */
+
+extern void fgoldi_add(fgoldi */*z*/,
+                      const fgoldi */*x*/, const fgoldi */*y*/);
+
+/* --- @fgoldi_sub@ --- *
+ *
+ * Arguments:  @fgoldi *z@ = where to put the result (may alias @x@ or @y@)
+ *             @const fgoldi *x, *y@ = two operands
+ *
+ * Returns:    ---
+ *
+ * Use:                Set @z@ to the difference %$x - y$%.
+ */
+
+extern void fgoldi_sub(fgoldi */*z*/,
+                      const fgoldi */*x*/, const fgoldi */*y*/);
+
+/* --- @fgoldi_condswap@ --- *
+ *
+ * Arguments:  @fgoldi *x, *y@ = two operands
+ *             @uint32 m@ = a mask
+ *
+ * Returns:    ---
+ *
+ * Use:                If @m@ is zero, do nothing; if @m@ is all-bits-set, then
+ *             exchange @x@ and @y@.  If @m@ has some other value, then
+ *             scramble @x@ and @y@ in an unhelpful way.
+ */
+
+extern void fgoldi_condswap(fgoldi */*x*/, fgoldi */*y*/, uint32 /*m*/);
+
+/* --- @fgoldi_mulconst@ --- *
+ *
+ * Arguments:  @fgoldi *z@ = where to put the result (may alias @x@)
+ *             @const fgoldi *x@ = an operand
+ *             @long a@ = a small-ish constant; %$|a| < 2^{20}$%.
+ *
+ * Returns:    ---
+ *
+ * Use:                Set @z@ to the product %$a x$%.
+ */
+
+extern void fgoldi_mulconst(fgoldi */*z*/, const fgoldi */*x*/, long /*a*/);
+
+/* --- @fgoldi_mul@ --- *
+ *
+ * Arguments:  @fgoldi *z@ = where to put the result (may alias @x@ or @y@)
+ *             @const fgoldi *x, *y@ = two operands
+ *
+ * Returns:    ---
+ *
+ * Use:                Set @z@ to the product %$x y$%.
+ */
+
+extern void fgoldi_mul(fgoldi */*z*/,
+                      const fgoldi */*x*/, const fgoldi */*y*/);
+
+/* --- @fgoldi_sqr@ --- *
+ *
+ * Arguments:  @fgoldi *z@ = where to put the result (may alias @x@ or @y@)
+ *             @const fgoldi *x@ = an operand
+ *
+ * Returns:    ---
+ *
+ * Use:                Set @z@ to the square %$x^2$%.
+ */
+
+extern void fgoldi_sqr(fgoldi */*z*/, const fgoldi */*x*/);
+
+/* --- @fgoldi_inv@ --- *
+ *
+ * Arguments:  @fgoldi *z@ = where to put the result (may alias @x@)
+ *             @const fgoldi *x@ = an operand
+ *
+ * Returns:    ---
+ *
+ * Use:                Stores in @z@ the multiplicative inverse %$x^{-1}$%.  If
+ *             %$x = 0$% then @z@ is set to zero.  This is considered a
+ *             feature.
+ */
+
+extern void fgoldi_inv(fgoldi */*z*/, const fgoldi */*x*/);
+
+/*----- That's all, folks -------------------------------------------------*/
+
+#ifdef __cplusplus
+  }
+#endif
+
+#endif