General robustification.
[u/mdw/catacomb] / mp.h
diff --git a/mp.h b/mp.h
index 5bc465d..bfed14a 100644 (file)
--- a/mp.h
+++ b/mp.h
@@ -1,6 +1,6 @@
 /* -*-c-*-
  *
- * $Id: mp.h,v 1.9 2000/07/29 17:03:31 mdw Exp $
+ * $Id: mp.h,v 1.18 2004/04/03 03:32:05 mdw Exp $
  *
  * Simple multiprecision arithmetic
  *
 /*----- Revision history --------------------------------------------------* 
  *
  * $Log: mp.h,v $
+ * Revision 1.18  2004/04/03 03:32:05  mdw
+ * General robustification.
+ *
+ * Revision 1.17  2003/05/16 09:09:24  mdw
+ * Fix @mp_lsl2c@.  Turns out to be surprisingly tricky.
+ *
+ * Revision 1.16  2002/10/15 22:57:22  mdw
+ * Handy new comparison macros.
+ *
+ * Revision 1.15  2002/10/15 19:18:31  mdw
+ * New operation to negate numbers.
+ *
+ * Revision 1.14  2002/10/15 00:19:40  mdw
+ * Bit setting and clearing functions.
+ *
+ * Revision 1.13  2002/10/06 22:52:50  mdw
+ * Pile of changes for supporting two's complement properly.
+ *
+ * Revision 1.12  2001/06/16 12:57:43  mdw
+ * Move the @mpmont_factor@ structure and rename it now that it's used for
+ * Barrett simultaneous exponentiation too.
+ *
+ * Revision 1.11  2001/04/03 19:36:05  mdw
+ * Add some simple bitwise operations so that Perl can use them.
+ *
+ * Revision 1.10  2000/10/08 12:03:16  mdw
+ * Provide @mp_eq@ and @MP_EQ@ for rapidly testing equality of two
+ * integers.
+ *
  * Revision 1.9  2000/07/29 17:03:31  mdw
  * Add support for left-to-right bitscanning, for use in modular
  * exponentiation.
 
 /*----- Data structures ---------------------------------------------------*/
 
+/* --- A multiprecision integer --- */
+
 typedef struct mp {
-  mpw *v, *vl;
-  size_t sz;
-  mparena *a;
-  unsigned f;
-  unsigned ref;
+  mpw *v, *vl;                         /* Vector of digits, current limit */
+  size_t sz;                           /* Size of digit buffer in words */
+  mparena *a;                          /* Arena for buffer allocation */
+  unsigned f;                          /* Flags (see below) */
+  unsigned ref;                                /* Reference counter */
 } mp;
 
-#define MP_NEG 1u
-#define MP_BURN 2u
-#define MP_CONST 4u
-#define MP_UNDEF 8u
-#define MP_DESTROYED 16u
+#define MP_NEG 1u                      /* Negative (signed magnitude) */
+#define MP_BURN 2u                     /* Secret (viral flag) */
+#define MP_CONST 4u                    /* Uses strange memory allocation */
+#define MP_UNDEF 8u                    /* Contains nothing interesting */
+#define MP_DESTROYED 16u               /* Has been destroyed */
+
+/* --- A factor for simultaneous exponentation --- *
+ *
+ * Used by the Montgomery and Barrett exponentiators.
+ */
+
+typedef struct mp_expfactor {
+  mp *base;
+  mp *exp;
+} mp_expfactor;
 
 /*----- Useful constants --------------------------------------------------*/
 
@@ -469,6 +510,18 @@ extern void mp_rscan(mpscan */*sc*/, const mp */*m*/);
 
 extern size_t mp_octets(const mp */*m*/);
 
+/* --- @mp_octets2c@ --- *
+ *
+ * Arguments:  @const mp *m@ = a multiprecision integer
+ *
+ * Returns:    The number of octets required to represent @m@.
+ *
+ * Use:                Calculates the external storage required for a multiprecision
+ *             integer represented as two's complement.
+ */
+
+extern size_t mp_octets2c(const mp */*m*/);
+
 /* --- @mp_bits@ --- *
  *
  * Arguments:  @const mp *m@ = a multiprecision integer
@@ -551,50 +604,243 @@ extern mp *mp_loadb(mp */*d*/, const void */*pv*/, size_t /*sz*/);
 
 extern void mp_storeb(const mp */*m*/, void */*pv*/, size_t /*sz*/);
 
-/*----- Simple arithmetic -------------------------------------------------*/
+/* --- @mp_loadl2c@ --- *
+ *
+ * Arguments:  @mp *d@ = destination
+ *             @const void *pv@ = pointer to source data
+ *             @size_t sz@ = size of the source data
+ *
+ * Returns:    Resulting multiprecision number.
+ *
+ * Use:                Loads a multiprecision number from an array of octets as
+ *             two's complement.  The first byte in the array is the least
+ *             significant.
+ */
+
+extern mp *mp_loadl2c(mp */*d*/, const void */*pv*/, size_t /*sz*/);
 
-/* --- @mp_2c@ --- *
+/* --- @mp_storel2c@ --- *
+ *
+ * Arguments:  @const mp *m@ = source
+ *             @void *pv@ = pointer to output array
+ *             @size_t sz@ = size of the output array
+ *
+ * Returns:    ---
+ *
+ * Use:                Stores a multiprecision number in an array of octets as two's
+ *             complement.  The first byte in the array is the least
+ *             significant.  If the array is too small to represent the
+ *             number, high-order bits are truncated; if the array is too
+ *             large, high order bytes are sign-extended.
+ */
+
+extern void mp_storel2c(const mp */*m*/, void */*pv*/, size_t /*sz*/);
+
+/* --- @mp_loadb2c@ --- *
  *
  * Arguments:  @mp *d@ = destination
- *             @mp *a@ = source
+ *             @const void *pv@ = pointer to source data
+ *             @size_t sz@ = size of the source data
+ *
+ * Returns:    Resulting multiprecision number.
+ *
+ * Use:                Loads a multiprecision number from an array of octets as
+ *             two's complement.  The last byte in the array is the least
+ *             significant.
+ */
+
+extern mp *mp_loadb2c(mp */*d*/, const void */*pv*/, size_t /*sz*/);
+
+/* --- @mp_storeb2c@ --- *
+ *
+ * Arguments:  @const mp *m@ = source
+ *             @void *pv@ = pointer to output array
+ *             @size_t sz@ = size of the output array
+ *
+ * Returns:    ---
  *
- * Returns:    Result, @a@ converted to two's complement notation.
+ * Use:                Stores a multiprecision number in an array of octets, as
+ *             two's complement.  The last byte in the array is the least
+ *             significant.  If the array is too small to represent the
+ *             number, high-order bits are truncated; if the array is too
+ *             large, high order bytes are sign-extended.
  */
 
-extern mp *mp_2c(mp */*d*/, mp */*a*/);
+extern void mp_storeb2c(const mp */*m*/, void */*pv*/, size_t /*sz*/);
 
-/* --- @mp_sm@ --- *
+/*----- Bit operations ----------------------------------------------------*/
+
+/* --- @mp_not@ --- *
  *
  * Arguments:  @mp *d@ = destination
  *             @mp *a@ = source
  *
- * Returns:    Result, @a@ converted to the native signed-magnitude
- *             notation.
+ * Returns:    The bitwise complement of the source.
+ */ 
+
+extern mp *mp_not(mp */*d*/, mp */*a*/);
+
+/* --- @mp_bitop@ --- *
+ *
+ * Arguments:  @mp *d@ = destination
+ *             @mp *a, *b@ = sources
+ *
+ * Returns:    The result of the given bitwise operation.  These functions
+ *             don't handle negative numbers at all sensibly.  For that, use
+ *             the @...2c@ variants.  The functions are named after the
+ *             truth tables they generate:
+ *
+ *                     a:      0011
+ *                     b:      0101
+ *                     @mpx_bitXXXX@
  */
 
-extern mp *mp_sm(mp */*d*/, mp */*a*/);
+#define MP_BITDECL(string)                                             \
+  extern mp *mp_bit##string(mp */*d*/, mp */*a*/, mp */*b*/);
+MPX_DOBIN(MP_BITDECL)
 
-/* --- @mp_lsl@ --- *
+/* --- @mp_[n]and@, @mp_[n]or@, @mp_[n]xor@, @mp_not@ --- *
+ *
+ * Synonyms for the commonly-used functions.
+ */
+
+#define mp_and  mp_bit0001
+#define mp_or   mp_bit0111
+#define mp_nand mp_bit1110
+#define mp_nor  mp_bit1000
+#define mp_xor  mp_bit0110
+
+/* --- @mp_testbit@ --- *
+ *
+ * Arguments:  @mp *x@ = a large integer
+ *             @unsigned long n@ = which bit to test
+ *
+ * Returns:    Nonzero if the bit is set, zero if not.
+ */
+
+extern int mp_testbit(mp */*x*/, unsigned long /*n*/);
+
+/* --- @mp_setbit@, @mp_clearbit@ --- *
+ *
+ * Arguments:  @mp *d@ = a destination
+ *             @mp *x@ = a large integer
+ *             @unsigned long n@ = which bit to modify
+ *
+ * Returns:    The argument @x@, with the appropriate bit set or cleared.
+ */
+
+extern mp *mp_setbit(mp */*d*/, mp */*x*/, unsigned long /*n*/);
+extern mp *mp_clearbit(mp */*d*/, mp */*x*/, unsigned long /*n*/);
+
+/* --- @mp_lsl@, @mp_lslc@, @mp_lsr@ --- *
  *
  * Arguments:  @mp *d@ = destination
  *             @mp *a@ = source
  *             @size_t n@ = number of bits to move
  *
- * Returns:    Result, @a@ shifted left by @n@.
+ * Returns:    Result, @a@ shifted left or right by @n@.
+ *
+ * Use:                Bitwise shift operators.  @mp_lslc@ fills the bits introduced
+ *             on the right with ones instead of zeroes: it's used
+ *             internally by @mp_lsl2c@, though it may be useful on its
+ *             own.
  */
 
 extern mp *mp_lsl(mp */*d*/, mp */*a*/, size_t /*n*/);
+extern mp *mp_lslc(mp */*d*/, mp */*a*/, size_t /*n*/);
+extern mp *mp_lsr(mp */*d*/, mp */*a*/, size_t /*n*/);
+
+/* --- @mp_not2c@ --- *
+ *
+ * Arguments:  @mp *d@ = destination
+ *             @mp *a@ = source
+ *
+ * Returns:    The sign-extended complement of the argument.
+ */
+
+extern mp *mp_not2c(mp */*d*/, mp */*a*/);
+
+/* --- @mp_bitop2c@ --- *
+ *
+ * Arguments:  @mp *d@ = destination
+ *             @mp *a, *b@ = sources
+ *
+ * Returns:    The result of the given bitwise operation.  Negative numbers
+ *             are treated as two's complement, sign-extended infinitely to
+ *             the left.  The functions are named after the truth tables
+ *             they generate:
+ *
+ *                     a:      0011
+ *                     b:      0101
+ *                     @mpx_bitXXXX@
+ */
+
+#define MP_BIT2CDECL(string)                                           \
+  extern mp *mp_bit##string##2c(mp */*d*/, mp */*a*/, mp */*b*/);
+MPX_DOBIN(MP_BIT2CDECL)
+
+/* --- @mp_[n]and@, @mp_[n]or@, @mp_[n]xor@, @mp_not@ --- *
+ *
+ * Synonyms for the commonly-used functions.
+ */
 
-/* --- @mp_lsr@ --- *
+#define mp_and2c  mp_bit00012c
+#define mp_or2c   mp_bit01112c
+#define mp_nand2c mp_bit11102c
+#define mp_nor2c  mp_bit10002c
+#define mp_xor2c  mp_bit01102c
+
+/* --- @mp_lsl2c@, @mp_lsr2c@ --- *
  *
  * Arguments:  @mp *d@ = destination
  *             @mp *a@ = source
  *             @size_t n@ = number of bits to move
  *
- * Returns:    Result, @a@ shifted left by @n@.
+ * Returns:    Result, @a@ shifted left or right by @n@.  Handles the
+ *             pretence of sign-extension for negative numbers.
  */
 
-extern mp *mp_lsr(mp */*d*/, mp */*a*/, size_t /*n*/);
+extern mp *mp_lsl2c(mp */*d*/, mp */*a*/, size_t /*n*/);
+extern mp *mp_lsr2c(mp */*d*/, mp */*a*/, size_t /*n*/);
+
+/* --- @mp_testbit2c@ --- *
+ *
+ * Arguments:  @mp *x@ = a large integer
+ *             @unsigned long n@ = which bit to test
+ *
+ * Returns:    Nonzero if the bit is set, zero if not.  Fakes up two's
+ *             complement representation.
+ */
+
+extern int mp_testbit2c(mp */*x*/, unsigned long /*n*/);
+
+/* --- @mp_setbit2c@, @mp_clearbit2c@ --- *
+ *
+ * Arguments:  @mp *d@ = a destination
+ *             @mp *x@ = a large integer
+ *             @unsigned long n@ = which bit to modify
+ *
+ * Returns:    The argument @x@, with the appropriate bit set or cleared.
+ *             Fakes up two's complement representation.
+ */
+
+extern mp *mp_setbit2c(mp */*d*/, mp */*x*/, unsigned long /*n*/);
+extern mp *mp_clearbit2c(mp */*d*/, mp */*x*/, unsigned long /*n*/);
+
+/*----- Comparisons -------------------------------------------------------*/
+
+/* --- @mp_eq@ --- *
+ *
+ * Arguments:  @const mp *a, *b@ = two numbers
+ *
+ * Returns:    Nonzero if the numbers are equal.
+ */
+
+extern int mp_eq(const mp */*a*/, const mp */*b*/);
+
+#define MP_EQ(a, b)                                                    \
+  ((((a)->f ^ (b)->f) & MP_NEG) == 0 &&                                        \
+   mpx_ueq((a)->v, (a)->vl, (b)->v, (b)->vl))
 
 /* --- @mp_cmp@ --- *
  *
@@ -608,6 +854,28 @@ extern int mp_cmp(const mp */*a*/, const mp */*b*/);
 
 #define MP_CMP(a, op, b) (mp_cmp((a), (b)) op 0)
 
+/* --- Other handy macros --- */
+
+#define MP_ISNEG(x) ((x)->f & MP_NEG)
+#define MP_ISZERO(x) (!MP_LEN(x))
+#define MP_ISPOS(x) (!MP_ISNEG(x) && !MP_ISZERO(x))
+#define MP_ISODD(x) (!MP_ISZERO(x) && ((x)->v[0] & 1u))
+#define MP_ISEVEN(x) (!MP_ISODD(x))
+
+/*----- Arithmetic operations ---------------------------------------------*/
+
+/* --- @mp_neg@ --- *
+ *
+ * Arguments:  @mp *d@ = destination
+ *             @mp *a@ = argument
+ *
+ * Returns:    The negation of the argument.
+ *
+ * Use:                Negates its argument.
+ */
+
+extern mp *mp_neg(mp */*d*/, mp */*a*/);
+
 /* --- @mp_add@ --- *
  *
  * Arguments:  @mp *d@ = destination