catcrypt.c, catsign.c: Shorten chunk sizes.
[u/mdw/catacomb] / mp.h
diff --git a/mp.h b/mp.h
index 8319173..13f97d1 100644 (file)
--- a/mp.h
+++ b/mp.h
@@ -1,13 +1,13 @@
 /* -*-c-*-
  *
- * $Id: mp.h,v 1.19 2004/04/08 01:36:15 mdw Exp $
+ * $Id$
  *
  * Simple multiprecision arithmetic
  *
  * (c) 1999 Straylight/Edgeware
  */
 
-/*----- Licensing notice --------------------------------------------------* 
+/*----- Licensing notice --------------------------------------------------*
  *
  * This file is part of Catacomb.
  *
  * 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,
@@ -90,14 +90,14 @@ typedef struct mp_expfactor {
 extern mp mp_const[];
 
 #define MP_ZERO         (&mp_const[0])
-#define MP_ONE   (&mp_const[1])
-#define MP_TWO   (&mp_const[2])
+#define MP_ONE  (&mp_const[1])
+#define MP_TWO  (&mp_const[2])
 #define MP_THREE (&mp_const[3])
-#define MP_FOUR  (&mp_const[4])
-#define MP_FIVE  (&mp_const[5])
-#define MP_TEN   (&mp_const[6])
+#define MP_FOUR         (&mp_const[4])
+#define MP_FIVE         (&mp_const[5])
+#define MP_TEN  (&mp_const[6])
 #define MP_256  (&mp_const[7])
-#define MP_MONE  (&mp_const[8])
+#define MP_MONE         (&mp_const[8])
 
 #define MP_NEW ((mp *)0)
 #define MP_NEWSEC (&mp_const[9])
@@ -218,7 +218,7 @@ extern void mp_drop(mp */*m*/);
   if (_mm->ref == 0 && !(_mm->f & MP_CONST))                           \
     mp_destroy(_mm);                                                   \
 } while (0)
-                  
+
 /* --- @mp_split@ --- *
  *
  * Arguments:  @mp *m@ = pointer to a multiprecision integer
@@ -363,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)
 
@@ -615,7 +615,7 @@ extern void mp_storeb2c(const mp */*m*/, void */*pv*/, size_t /*sz*/);
  *             @mp *a@ = source
  *
  * Returns:    The bitwise complement of the source.
- */ 
+ */
 
 extern mp *mp_not(mp */*d*/, mp */*a*/);
 
@@ -643,11 +643,11 @@ MPX_DOBIN(MP_BITDECL)
  * Synonyms for the commonly-used functions.
  */
 
-#define mp_and  mp_bit0001
-#define mp_or   mp_bit0111
+#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
+#define mp_nor mp_bit1000
+#define mp_xor mp_bit0110
 
 /* --- @mp_testbit@ --- *
  *
@@ -724,7 +724,7 @@ MPX_DOBIN(MP_BIT2CDECL)
  */
 
 #define mp_and2c  mp_bit00012c
-#define mp_or2c   mp_bit01112c
+#define mp_or2c          mp_bit01112c
 #define mp_nand2c mp_bit11102c
 #define mp_nor2c  mp_bit10002c
 #define mp_xor2c  mp_bit01102c
@@ -795,11 +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_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))
+#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 ---------------------------------------------*/
 
@@ -866,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
@@ -930,15 +941,35 @@ extern mp *mp_modinv(mp */*d*/, mp */*x*/, mp */*p*/);
 
 /* --- @mp_jacobi@ --- *
  *
- * Arguments:  @mp *a@ = an integer less than @n@
- *             @mp *n@ = an odd integer
+ * Arguments:  @mp *a@ = an integer
+ *             @mp *n@ = another integer
  *
  * Returns:    @-1@, @0@ or @1@ -- the Jacobi symbol %$J(a, n)$%.
  *
- * Use:                Computes the Jacobi symbol.  If @n@ is prime, this is the
- *             Legendre symbol and is equal to 1 if and only if @a@ is a
- *             quadratic residue mod @n@.  The result is zero if and only if
- *             @a@ and @n@ have a common factor greater than one.
+ * Use:                Computes the Kronecker symbol %$\jacobi{a}{n}$%.  If @n@ is
+ *             prime, this is the Legendre symbol and is equal to 1 if and
+ *             only if @a@ is a quadratic residue mod @n@.  The result is
+ *             zero if and only if @a@ and @n@ have a common factor greater
+ *             than one.
+ *
+ *             If @n@ is composite, then this computes the Kronecker symbol
+ *
+ *               %$\jacobi{a}{n}=\jacobi{a}{u}\prod_i\jacobi{a}{p_i}^{e_i}$%
+ *
+ *             where %$n = u p_0^{e_0} \ldots p_{n-1}^{e_{n-1}}$% is the
+ *             prime factorization of %$n$%.  The missing bits are:
+ *
+ *               * %$\jacobi{a}{1} = 1$%;
+ *               * %$\jacobi{a}{-1} = 1$% if @a@ is negative, or 1 if
+ *                 positive;
+ *               * %$\jacobi{a}{0} = 0$%;
+ *               * %$\jacobi{a}{2}$ is 0 if @a@ is even, 1 if @a@ is
+ *                 congruent to 1 or 7 (mod 8), or %$-1$% otherwise.
+ *
+ *             If %$n$% is positive and odd, then this is the Jacobi
+ *             symbol.  (The Kronecker symbol is a consistant domain
+ *             extension; the Jacobi symbol was implemented first, and the
+ *             name stuck.)
  */
 
 extern int mp_jacobi(mp */*a*/, mp */*n*/);
@@ -957,10 +988,25 @@ 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*/);
 
+/* --- @mp_modexp@ --- *
+ *
+ * Arguments:  @mp *d@ = fake destination
+ *             @mp *x@ = base of exponentiation
+ *             @mp *e@ = exponent
+ *             @mp *n@ = modulus (must be positive)
+ *
+ * Returns:    The value %$x^e \bmod n$%.
+ */
+
+extern mp *mp_modexp(mp */*d*/, mp */*x*/, mp */*e*/, mp */*n*/);
+
 /*----- Test harness support ----------------------------------------------*/
 
 #include <mLib/testrig.h>