pub/, progs/: Implement Bernstein's Ed25519 signature scheme.
[catacomb] / math / scaf.h
diff --git a/math/scaf.h b/math/scaf.h
new file mode 100644 (file)
index 0000000..2535a9c
--- /dev/null
@@ -0,0 +1,163 @@
+/* -*-c-*-
+ *
+ * Simple scalar fields
+ *
+ * (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_SCAF_H
+#define CATACOMB_SCAF_H
+
+#ifdef __cplusplus
+  extern "C" {
+#endif
+
+/*----- Header files ------------------------------------------------------*/
+
+#include <mLib/bits.h>
+
+/*----- Type selection ----------------------------------------------------*/
+
+#ifndef SCAF_IMPL
+#  ifdef HAVE_UINT64
+#    define SCAF_IMPL 32
+#  else
+#    define SCAF_IMPL 16
+#  endif
+#endif
+
+#if SCAF_IMPL == 32
+  typedef uint32 scaf_piece;
+  typedef uint64 scaf_dblpiece;
+#endif
+#if SCAF_IMPL == 16
+  typedef uint16 scaf_piece;
+  typedef uint32 scaf_dblpiece;
+#endif
+
+#define SCAF_NPIECE(bits, piecewd) (((bits) + piecewd - 1)/piecewd)
+
+/*----- Functions provided -----------------------------------------------*/
+
+/* --- @scaf_load@ --- *
+ *
+ * Arguments:  @scaf_piece *z@ = where to write the result
+ *             @const octet *b@ = source buffer to read
+ *             @size_t sz@ = size of the source buffer
+ *             @size_t npiece@ = number of pieces to read
+ *             @unsigned piecewd@ = nominal width of pieces in bits
+ *
+ * Returns:    ---
+ *
+ * Use:                Loads a little-endian encoded scalar into a vector @z@ of
+ *             single-precision pieces.
+ */
+
+extern void scaf_load(scaf_piece */*z*/, const octet */*b*/, size_t /*sz*/,
+                     size_t /*npiece*/, unsigned /*piecewd*/);
+
+/* --- @scaf_loaddbl@ --- *
+ *
+ * Arguments:  @scaf_dblpiece *z@ = where to write the result
+ *             @const octet *b@ = source buffer to read
+ *             @size_t sz@ = size of the source buffer
+ *             @size_t npiece@ = number of pieces to read
+ *             @unsigned piecewd@ = nominal width of pieces in bits
+ *
+ * Returns:    ---
+ *
+ * Use:                Loads a little-endian encoded scalar into a vector @z@ of
+ *             double-precision pieces.
+ */
+
+extern void scaf_loaddbl(scaf_dblpiece */*z*/, const octet */*b*/,
+                        size_t /*sz*/, size_t /*npiece*/,
+                        unsigned /*piecewd*/);
+
+/* --- @scaf_store@ --- *
+ *
+ * Arguments:  @octet *b@ = buffer to fill in
+ *             @size_t sz@ = size of the buffer
+ *             @const scaf_piece *x@ = scalar to store
+ *             @size_t npiece@ = number of pieces in @x@
+ *             @unsigned piecewd@ = nominal width of pieces in bits
+ *
+ * Returns:    ---
+ *
+ * Use:                Stores a scalar in a vector of single-precison pieces as a
+ *             little-endian vector of bytes.
+ */
+
+extern void scaf_store(octet */*b*/, size_t /*sz*/, const scaf_piece */*x*/,
+                      size_t /*npiece*/, unsigned /*piecewd*/);
+
+/* --- @scaf_mul@ --- *
+ *
+ * Arguments:  @scaf_dblpiece *z@ = where to put the answer
+ *             @const scaf_piece *x, *y@ = the operands
+ *             @size_t npiece@ = the length of the operands
+ *
+ * Returns:    ---
+ *
+ * Use:                Multiply two scalars.  The destination must have space for
+ *             @2*npiece@ pieces (though the last one will always be zero).
+ *             The result is not reduced.
+ */
+
+extern void scaf_mul(scaf_dblpiece */*z*/, const scaf_piece */*x*/,
+                    const scaf_piece */*y*/, size_t /*npiece*/);
+
+/* --- @scaf_reduce@ --- *
+ *
+ * Arguments:  @scaf_piece *z@ = where to write the result
+ *             @const scaf_dblpiece *x@ = the operand to reduce
+ *             @const scaf_piece *l@ = the modulus, in internal format
+ *             @const scaf_piece *mu@ = scaled approximation to @1/l@
+ *             @size_t npiece@ = number of pieces in @l@
+ *             @unsigned piecewd@ = nominal width of a piece in bits
+ *             @scaf_piece *scratch@ = @3*npiece + 1@ scratch pieces
+ *
+ * Returns:    ---
+ *
+ * Use:                Reduce @x@ (a vector of @2*npiece@ double-precision pieces)
+ *             modulo @l@ (a vector of @npiece@ single-precision pieces),
+ *             writing the result to @z@.
+ *
+ *             Write @n = npiece@, @w = piecewd@, and %$B = 2^w$%.  The
+ *             operand @mu@ must contain %$\lfloor B^{2n}/l \rfloor$%, in
+ *             @npiece + 1@ pieces.  Furthermore, we must have
+ *             %$3 l < B^n$%.  (Fiddle with %$w$% if necessary.)
+ */
+
+extern void scaf_reduce(scaf_piece */*z*/, const scaf_dblpiece */*x*/,
+                       const scaf_piece */*l*/, const scaf_piece */*mu*/,
+                       size_t /*npiece*/, unsigned /*piecewd*/,
+                       scaf_piece */*scratch*/);
+
+/*----- That's all, folks -------------------------------------------------*/
+
+#ifdef __cplusplus
+  }
+#endif
+
+#endif