X-Git-Url: https://git.distorted.org.uk/u/mdw/catacomb/blobdiff_plain/5fbe3846b6a2a0eea61ed4ba0ca0c522005d6489..3884054be9261b4607f5f3a2d0b79695f65b851f:/mp.h diff --git a/mp.h b/mp.h index 5bc465d..71092fd 100644 --- 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.12 2001/06/16 12:57:43 mdw Exp $ * * Simple multiprecision arithmetic * @@ -30,6 +30,17 @@ /*----- Revision history --------------------------------------------------* * * $Log: mp.h,v $ + * 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. @@ -91,19 +102,31 @@ /*----- 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 --------------------------------------------------*/ @@ -596,6 +619,19 @@ extern mp *mp_lsl(mp */*d*/, mp */*a*/, size_t /*n*/); extern mp *mp_lsr(mp */*d*/, mp */*a*/, size_t /*n*/); +/* --- @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@ --- * * * Arguments: @const mp *a, *b@ = two numbers @@ -608,6 +644,19 @@ extern int mp_cmp(const mp */*a*/, const mp */*b*/); #define MP_CMP(a, op, b) (mp_cmp((a), (b)) op 0) +/* --- @mpx_and@, @mpx_or@, @mpx_xor@, @mpx_not@ --- * + * + * Arguments: @mp *d@ = destination + * @mp *a, *b@ = sources + * + * Returns: The result of the obvious bitwise operation. + */ + +extern mp *mp_and(mp */*d*/, mp */*a*/, mp */*b*/); +extern mp *mp_or(mp */*d*/, mp */*a*/, mp */*b*/); +extern mp *mp_xor(mp */*d*/, mp */*a*/, mp */*b*/); +extern mp *mp_not(mp */*d*/, mp */*a*/); + /* --- @mp_add@ --- * * * Arguments: @mp *d@ = destination