Merge branch 'master' of git+ssh://metalzone.distorted.org.uk/~mdw/public-git/catacomb/
[u/mdw/catacomb] / mp.h
diff --git a/mp.h b/mp.h
index bf0303a..74b6473 100644 (file)
--- a/mp.h
+++ b/mp.h
@@ -1,6 +1,6 @@
 /* -*-c-*-
  *
- * $Id: mp.h,v 1.16 2002/10/15 22:57:22 mdw Exp $
+ * $Id$
  *
  * Simple multiprecision arithmetic
  *
  * MA 02111-1307, USA.
  */
 
-/*----- Revision history --------------------------------------------------* 
- *
- * $Log: mp.h,v $
- * 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.
- *
- * Revision 1.8  2000/06/22 19:02:01  mdw
- * Add new functions.
- *
- * Revision 1.7  2000/06/17 11:45:09  mdw
- * Major memory management overhaul.  Added arena support.  Use the secure
- * arena for secret integers.  Replace and improve the MP management macros
- * (e.g., replace MP_MODIFY by MP_DEST).
- *
- * Revision 1.6  1999/12/10 23:19:46  mdw
- * Minor bugfixes.  New interface for suggested destinations.
- *
- * Revision 1.5  1999/11/22 20:50:37  mdw
- * Add support for computing Jacobi symbols.
- *
- * Revision 1.4  1999/11/21 22:13:02  mdw
- * Add mp version of MPX_BITS.
- *
- * Revision 1.3  1999/11/19 13:19:14  mdw
- * Fix const annotation.
- *
- * Revision 1.2  1999/11/17 18:02:16  mdw
- * New multiprecision integer arithmetic suite.
- *
- */
-
 #ifndef CATACOMB_MP_H
 #define CATACOMB_MP_H
 
@@ -418,7 +363,7 @@ extern void mp_shrink(mp */*m*/);
 #define MP_SHRINK(m) do {                                              \
   mp *_mm = (m);                                                       \
   MPX_SHRINK(_mm->v, _mm->vl);                                         \
-  if (!MP_LEN(_mm))                                                    \
+  if (MP_ZEROP(_mm))                                                   \
     _mm->f &= ~MP_NEG;                                                 \
 } while (0)
 
@@ -726,16 +671,22 @@ extern int mp_testbit(mp */*x*/, unsigned long /*n*/);
 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_lsr@ --- *
+/* --- @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 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@ --- *
@@ -844,9 +795,11 @@ extern int mp_cmp(const mp */*a*/, const mp */*b*/);
 
 /* --- Other handy macros --- */
 
-#define MP_ISNEG(x) ((x)->f & MP_NEG)
-#define MP_ISZERO(x) MP_EQ((x), MP_ZERO)
-#define MP_ISPOS(x) (!MP_ISNEG(x) && !MP_ISZERO(x))
+#define MP_NEGP(x) ((x)->f & MP_NEG)
+#define MP_ZEROP(x) (!MP_LEN(x))
+#define MP_POSP(x) (!MP_NEGP(x) && !MP_ZEROP(x))
+#define MP_ODDP(x) (!MP_ZEROP(x) && ((x)->v[0] & 1u))
+#define MP_EVENP(x) (!MP_ODDP(x))
 
 /*----- Arithmetic operations ---------------------------------------------*/
 
@@ -913,6 +866,17 @@ extern mp *mp_sqr(mp */*d*/, mp */*a*/);
 
 extern void mp_div(mp **/*qq*/, mp **/*rr*/, mp */*a*/, mp */*b*/);
 
+/* --- @mp_exp@ --- *
+ *
+ * Arguments:  @mp *d@ = fake destination
+ *             @mp *a@ = base
+ *             @mp *e@ = exponent
+ *
+ * Returns:    Result, %$a^e$%.
+ */
+
+extern mp *mp_exp(mp */*d*/, mp */*a*/, mp */*e*/);
+
 /* --- @mp_odd@ --- *
  *
  * Arguments:  @mp *d@ = pointer to destination integer
@@ -961,6 +925,20 @@ extern mp *mp_sqrt(mp */*d*/, mp */*a*/);
 extern void mp_gcd(mp **/*gcd*/, mp **/*xx*/, mp **/*yy*/,
                   mp */*a*/, mp */*b*/);
 
+/* -- @mp_modinv@ --- *
+ *
+ * Arguments:  @mp *d@ = destination
+ *             @mp *x@ = argument
+ *             @mp *p@ = modulus
+ *
+ * Returns:    The inverse %$x^{-1} \bmod p$%.
+ *
+ * Use:                Computes a modular inverse.    An assertion fails if %$p$%
+ *             has no inverse.
+ */
+
+extern mp *mp_modinv(mp */*d*/, mp */*x*/, mp */*p*/);
+
 /* --- @mp_jacobi@ --- *
  *
  * Arguments:  @mp *a@ = an integer less than @n@
@@ -990,6 +968,9 @@ extern int mp_jacobi(mp */*a*/, mp */*n*/);
  *             work if %$p$% is composite: you must factor the modulus, take
  *             a square root mod each factor, and recombine the results
  *             using the Chinese Remainder Theorem.
+ *
+ *             We guarantee that the square root returned is the smallest
+ *             one (i.e., the `positive' square root).
  */
 
 extern mp *mp_modsqrt(mp */*d*/, mp */*a*/, mp */*p*/);