jdk/src/share/native/sun/security/ec/mpi.c
changeset 3492 e549cea58864
equal deleted inserted replaced
3480:c197e38bf15a 3492:e549cea58864
       
     1 /* *********************************************************************
       
     2  *
       
     3  * Sun elects to have this file available under and governed by the
       
     4  * Mozilla Public License Version 1.1 ("MPL") (see
       
     5  * http://www.mozilla.org/MPL/ for full license text). For the avoidance
       
     6  * of doubt and subject to the following, Sun also elects to allow
       
     7  * licensees to use this file under the MPL, the GNU General Public
       
     8  * License version 2 only or the Lesser General Public License version
       
     9  * 2.1 only. Any references to the "GNU General Public License version 2
       
    10  * or later" or "GPL" in the following shall be construed to mean the
       
    11  * GNU General Public License version 2 only. Any references to the "GNU
       
    12  * Lesser General Public License version 2.1 or later" or "LGPL" in the
       
    13  * following shall be construed to mean the GNU Lesser General Public
       
    14  * License version 2.1 only. However, the following notice accompanied
       
    15  * the original version of this file:
       
    16  *
       
    17  *
       
    18  *  Arbitrary precision integer arithmetic library
       
    19  *
       
    20  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
       
    21  *
       
    22  * The contents of this file are subject to the Mozilla Public License Version
       
    23  * 1.1 (the "License"); you may not use this file except in compliance with
       
    24  * the License. You may obtain a copy of the License at
       
    25  * http://www.mozilla.org/MPL/
       
    26  *
       
    27  * Software distributed under the License is distributed on an "AS IS" basis,
       
    28  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
       
    29  * for the specific language governing rights and limitations under the
       
    30  * License.
       
    31  *
       
    32  * The Original Code is the MPI Arbitrary Precision Integer Arithmetic library.
       
    33  *
       
    34  * The Initial Developer of the Original Code is
       
    35  * Michael J. Fromberger.
       
    36  * Portions created by the Initial Developer are Copyright (C) 1998
       
    37  * the Initial Developer. All Rights Reserved.
       
    38  *
       
    39  * Contributor(s):
       
    40  *   Netscape Communications Corporation
       
    41  *   Douglas Stebila <douglas@stebila.ca> of Sun Laboratories.
       
    42  *
       
    43  * Alternatively, the contents of this file may be used under the terms of
       
    44  * either the GNU General Public License Version 2 or later (the "GPL"), or
       
    45  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
       
    46  * in which case the provisions of the GPL or the LGPL are applicable instead
       
    47  * of those above. If you wish to allow use of your version of this file only
       
    48  * under the terms of either the GPL or the LGPL, and not to allow others to
       
    49  * use your version of this file under the terms of the MPL, indicate your
       
    50  * decision by deleting the provisions above and replace them with the notice
       
    51  * and other provisions required by the GPL or the LGPL. If you do not delete
       
    52  * the provisions above, a recipient may use your version of this file under
       
    53  * the terms of any one of the MPL, the GPL or the LGPL.
       
    54  *
       
    55  *********************************************************************** */
       
    56 /*
       
    57  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
       
    58  * Use is subject to license terms.
       
    59  */
       
    60 
       
    61 #pragma ident   "%Z%%M% %I%     %E% SMI"
       
    62 
       
    63 /* $Id: mpi.c,v 1.45 2006/09/29 20:12:21 alexei.volkov.bugs%sun.com Exp $ */
       
    64 
       
    65 #include "mpi-priv.h"
       
    66 #if defined(OSF1)
       
    67 #include <c_asm.h>
       
    68 #endif
       
    69 
       
    70 #if MP_LOGTAB
       
    71 /*
       
    72   A table of the logs of 2 for various bases (the 0 and 1 entries of
       
    73   this table are meaningless and should not be referenced).
       
    74 
       
    75   This table is used to compute output lengths for the mp_toradix()
       
    76   function.  Since a number n in radix r takes up about log_r(n)
       
    77   digits, we estimate the output size by taking the least integer
       
    78   greater than log_r(n), where:
       
    79 
       
    80   log_r(n) = log_2(n) * log_r(2)
       
    81 
       
    82   This table, therefore, is a table of log_r(2) for 2 <= r <= 36,
       
    83   which are the output bases supported.
       
    84  */
       
    85 #include "logtab.h"
       
    86 #endif
       
    87 
       
    88 /* {{{ Constant strings */
       
    89 
       
    90 /* Constant strings returned by mp_strerror() */
       
    91 static const char *mp_err_string[] = {
       
    92   "unknown result code",     /* say what?            */
       
    93   "boolean true",            /* MP_OKAY, MP_YES      */
       
    94   "boolean false",           /* MP_NO                */
       
    95   "out of memory",           /* MP_MEM               */
       
    96   "argument out of range",   /* MP_RANGE             */
       
    97   "invalid input parameter", /* MP_BADARG            */
       
    98   "result is undefined"      /* MP_UNDEF             */
       
    99 };
       
   100 
       
   101 /* Value to digit maps for radix conversion   */
       
   102 
       
   103 /* s_dmap_1 - standard digits and letters */
       
   104 static const char *s_dmap_1 =
       
   105   "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz+/";
       
   106 
       
   107 /* }}} */
       
   108 
       
   109 unsigned long mp_allocs;
       
   110 unsigned long mp_frees;
       
   111 unsigned long mp_copies;
       
   112 
       
   113 /* {{{ Default precision manipulation */
       
   114 
       
   115 /* Default precision for newly created mp_int's      */
       
   116 static mp_size s_mp_defprec = MP_DEFPREC;
       
   117 
       
   118 mp_size mp_get_prec(void)
       
   119 {
       
   120   return s_mp_defprec;
       
   121 
       
   122 } /* end mp_get_prec() */
       
   123 
       
   124 void         mp_set_prec(mp_size prec)
       
   125 {
       
   126   if(prec == 0)
       
   127     s_mp_defprec = MP_DEFPREC;
       
   128   else
       
   129     s_mp_defprec = prec;
       
   130 
       
   131 } /* end mp_set_prec() */
       
   132 
       
   133 /* }}} */
       
   134 
       
   135 /*------------------------------------------------------------------------*/
       
   136 /* {{{ mp_init(mp, kmflag) */
       
   137 
       
   138 /*
       
   139   mp_init(mp, kmflag)
       
   140 
       
   141   Initialize a new zero-valued mp_int.  Returns MP_OKAY if successful,
       
   142   MP_MEM if memory could not be allocated for the structure.
       
   143  */
       
   144 
       
   145 mp_err mp_init(mp_int *mp, int kmflag)
       
   146 {
       
   147   return mp_init_size(mp, s_mp_defprec, kmflag);
       
   148 
       
   149 } /* end mp_init() */
       
   150 
       
   151 /* }}} */
       
   152 
       
   153 /* {{{ mp_init_size(mp, prec, kmflag) */
       
   154 
       
   155 /*
       
   156   mp_init_size(mp, prec, kmflag)
       
   157 
       
   158   Initialize a new zero-valued mp_int with at least the given
       
   159   precision; returns MP_OKAY if successful, or MP_MEM if memory could
       
   160   not be allocated for the structure.
       
   161  */
       
   162 
       
   163 mp_err mp_init_size(mp_int *mp, mp_size prec, int kmflag)
       
   164 {
       
   165   ARGCHK(mp != NULL && prec > 0, MP_BADARG);
       
   166 
       
   167   prec = MP_ROUNDUP(prec, s_mp_defprec);
       
   168   if((DIGITS(mp) = s_mp_alloc(prec, sizeof(mp_digit), kmflag)) == NULL)
       
   169     return MP_MEM;
       
   170 
       
   171   SIGN(mp) = ZPOS;
       
   172   USED(mp) = 1;
       
   173   ALLOC(mp) = prec;
       
   174 
       
   175   return MP_OKAY;
       
   176 
       
   177 } /* end mp_init_size() */
       
   178 
       
   179 /* }}} */
       
   180 
       
   181 /* {{{ mp_init_copy(mp, from) */
       
   182 
       
   183 /*
       
   184   mp_init_copy(mp, from)
       
   185 
       
   186   Initialize mp as an exact copy of from.  Returns MP_OKAY if
       
   187   successful, MP_MEM if memory could not be allocated for the new
       
   188   structure.
       
   189  */
       
   190 
       
   191 mp_err mp_init_copy(mp_int *mp, const mp_int *from)
       
   192 {
       
   193   ARGCHK(mp != NULL && from != NULL, MP_BADARG);
       
   194 
       
   195   if(mp == from)
       
   196     return MP_OKAY;
       
   197 
       
   198   if((DIGITS(mp) = s_mp_alloc(ALLOC(from), sizeof(mp_digit), FLAG(from))) == NULL)
       
   199     return MP_MEM;
       
   200 
       
   201   s_mp_copy(DIGITS(from), DIGITS(mp), USED(from));
       
   202   USED(mp) = USED(from);
       
   203   ALLOC(mp) = ALLOC(from);
       
   204   SIGN(mp) = SIGN(from);
       
   205 
       
   206 #ifndef _WIN32
       
   207   FLAG(mp) = FLAG(from);
       
   208 #endif /* _WIN32 */
       
   209 
       
   210   return MP_OKAY;
       
   211 
       
   212 } /* end mp_init_copy() */
       
   213 
       
   214 /* }}} */
       
   215 
       
   216 /* {{{ mp_copy(from, to) */
       
   217 
       
   218 /*
       
   219   mp_copy(from, to)
       
   220 
       
   221   Copies the mp_int 'from' to the mp_int 'to'.  It is presumed that
       
   222   'to' has already been initialized (if not, use mp_init_copy()
       
   223   instead). If 'from' and 'to' are identical, nothing happens.
       
   224  */
       
   225 
       
   226 mp_err mp_copy(const mp_int *from, mp_int *to)
       
   227 {
       
   228   ARGCHK(from != NULL && to != NULL, MP_BADARG);
       
   229 
       
   230   if(from == to)
       
   231     return MP_OKAY;
       
   232 
       
   233   ++mp_copies;
       
   234   { /* copy */
       
   235     mp_digit   *tmp;
       
   236 
       
   237     /*
       
   238       If the allocated buffer in 'to' already has enough space to hold
       
   239       all the used digits of 'from', we'll re-use it to avoid hitting
       
   240       the memory allocater more than necessary; otherwise, we'd have
       
   241       to grow anyway, so we just allocate a hunk and make the copy as
       
   242       usual
       
   243      */
       
   244     if(ALLOC(to) >= USED(from)) {
       
   245       s_mp_setz(DIGITS(to) + USED(from), ALLOC(to) - USED(from));
       
   246       s_mp_copy(DIGITS(from), DIGITS(to), USED(from));
       
   247 
       
   248     } else {
       
   249       if((tmp = s_mp_alloc(ALLOC(from), sizeof(mp_digit), FLAG(from))) == NULL)
       
   250         return MP_MEM;
       
   251 
       
   252       s_mp_copy(DIGITS(from), tmp, USED(from));
       
   253 
       
   254       if(DIGITS(to) != NULL) {
       
   255 #if MP_CRYPTO
       
   256         s_mp_setz(DIGITS(to), ALLOC(to));
       
   257 #endif
       
   258         s_mp_free(DIGITS(to), ALLOC(to));
       
   259       }
       
   260 
       
   261       DIGITS(to) = tmp;
       
   262       ALLOC(to) = ALLOC(from);
       
   263     }
       
   264 
       
   265     /* Copy the precision and sign from the original */
       
   266     USED(to) = USED(from);
       
   267     SIGN(to) = SIGN(from);
       
   268   } /* end copy */
       
   269 
       
   270   return MP_OKAY;
       
   271 
       
   272 } /* end mp_copy() */
       
   273 
       
   274 /* }}} */
       
   275 
       
   276 /* {{{ mp_exch(mp1, mp2) */
       
   277 
       
   278 /*
       
   279   mp_exch(mp1, mp2)
       
   280 
       
   281   Exchange mp1 and mp2 without allocating any intermediate memory
       
   282   (well, unless you count the stack space needed for this call and the
       
   283   locals it creates...).  This cannot fail.
       
   284  */
       
   285 
       
   286 void mp_exch(mp_int *mp1, mp_int *mp2)
       
   287 {
       
   288 #if MP_ARGCHK == 2
       
   289   assert(mp1 != NULL && mp2 != NULL);
       
   290 #else
       
   291   if(mp1 == NULL || mp2 == NULL)
       
   292     return;
       
   293 #endif
       
   294 
       
   295   s_mp_exch(mp1, mp2);
       
   296 
       
   297 } /* end mp_exch() */
       
   298 
       
   299 /* }}} */
       
   300 
       
   301 /* {{{ mp_clear(mp) */
       
   302 
       
   303 /*
       
   304   mp_clear(mp)
       
   305 
       
   306   Release the storage used by an mp_int, and void its fields so that
       
   307   if someone calls mp_clear() again for the same int later, we won't
       
   308   get tollchocked.
       
   309  */
       
   310 
       
   311 void   mp_clear(mp_int *mp)
       
   312 {
       
   313   if(mp == NULL)
       
   314     return;
       
   315 
       
   316   if(DIGITS(mp) != NULL) {
       
   317 #if MP_CRYPTO
       
   318     s_mp_setz(DIGITS(mp), ALLOC(mp));
       
   319 #endif
       
   320     s_mp_free(DIGITS(mp), ALLOC(mp));
       
   321     DIGITS(mp) = NULL;
       
   322   }
       
   323 
       
   324   USED(mp) = 0;
       
   325   ALLOC(mp) = 0;
       
   326 
       
   327 } /* end mp_clear() */
       
   328 
       
   329 /* }}} */
       
   330 
       
   331 /* {{{ mp_zero(mp) */
       
   332 
       
   333 /*
       
   334   mp_zero(mp)
       
   335 
       
   336   Set mp to zero.  Does not change the allocated size of the structure,
       
   337   and therefore cannot fail (except on a bad argument, which we ignore)
       
   338  */
       
   339 void   mp_zero(mp_int *mp)
       
   340 {
       
   341   if(mp == NULL)
       
   342     return;
       
   343 
       
   344   s_mp_setz(DIGITS(mp), ALLOC(mp));
       
   345   USED(mp) = 1;
       
   346   SIGN(mp) = ZPOS;
       
   347 
       
   348 } /* end mp_zero() */
       
   349 
       
   350 /* }}} */
       
   351 
       
   352 /* {{{ mp_set(mp, d) */
       
   353 
       
   354 void   mp_set(mp_int *mp, mp_digit d)
       
   355 {
       
   356   if(mp == NULL)
       
   357     return;
       
   358 
       
   359   mp_zero(mp);
       
   360   DIGIT(mp, 0) = d;
       
   361 
       
   362 } /* end mp_set() */
       
   363 
       
   364 /* }}} */
       
   365 
       
   366 /* {{{ mp_set_int(mp, z) */
       
   367 
       
   368 mp_err mp_set_int(mp_int *mp, long z)
       
   369 {
       
   370   int            ix;
       
   371   unsigned long  v = labs(z);
       
   372   mp_err         res;
       
   373 
       
   374   ARGCHK(mp != NULL, MP_BADARG);
       
   375 
       
   376   mp_zero(mp);
       
   377   if(z == 0)
       
   378     return MP_OKAY;  /* shortcut for zero */
       
   379 
       
   380   if (sizeof v <= sizeof(mp_digit)) {
       
   381     DIGIT(mp,0) = v;
       
   382   } else {
       
   383     for (ix = sizeof(long) - 1; ix >= 0; ix--) {
       
   384       if ((res = s_mp_mul_d(mp, (UCHAR_MAX + 1))) != MP_OKAY)
       
   385         return res;
       
   386 
       
   387       res = s_mp_add_d(mp, (mp_digit)((v >> (ix * CHAR_BIT)) & UCHAR_MAX));
       
   388       if (res != MP_OKAY)
       
   389         return res;
       
   390     }
       
   391   }
       
   392   if(z < 0)
       
   393     SIGN(mp) = NEG;
       
   394 
       
   395   return MP_OKAY;
       
   396 
       
   397 } /* end mp_set_int() */
       
   398 
       
   399 /* }}} */
       
   400 
       
   401 /* {{{ mp_set_ulong(mp, z) */
       
   402 
       
   403 mp_err mp_set_ulong(mp_int *mp, unsigned long z)
       
   404 {
       
   405   int            ix;
       
   406   mp_err         res;
       
   407 
       
   408   ARGCHK(mp != NULL, MP_BADARG);
       
   409 
       
   410   mp_zero(mp);
       
   411   if(z == 0)
       
   412     return MP_OKAY;  /* shortcut for zero */
       
   413 
       
   414   if (sizeof z <= sizeof(mp_digit)) {
       
   415     DIGIT(mp,0) = z;
       
   416   } else {
       
   417     for (ix = sizeof(long) - 1; ix >= 0; ix--) {
       
   418       if ((res = s_mp_mul_d(mp, (UCHAR_MAX + 1))) != MP_OKAY)
       
   419         return res;
       
   420 
       
   421       res = s_mp_add_d(mp, (mp_digit)((z >> (ix * CHAR_BIT)) & UCHAR_MAX));
       
   422       if (res != MP_OKAY)
       
   423         return res;
       
   424     }
       
   425   }
       
   426   return MP_OKAY;
       
   427 } /* end mp_set_ulong() */
       
   428 
       
   429 /* }}} */
       
   430 
       
   431 /*------------------------------------------------------------------------*/
       
   432 /* {{{ Digit arithmetic */
       
   433 
       
   434 /* {{{ mp_add_d(a, d, b) */
       
   435 
       
   436 /*
       
   437   mp_add_d(a, d, b)
       
   438 
       
   439   Compute the sum b = a + d, for a single digit d.  Respects the sign of
       
   440   its primary addend (single digits are unsigned anyway).
       
   441  */
       
   442 
       
   443 mp_err mp_add_d(const mp_int *a, mp_digit d, mp_int *b)
       
   444 {
       
   445   mp_int   tmp;
       
   446   mp_err   res;
       
   447 
       
   448   ARGCHK(a != NULL && b != NULL, MP_BADARG);
       
   449 
       
   450   if((res = mp_init_copy(&tmp, a)) != MP_OKAY)
       
   451     return res;
       
   452 
       
   453   if(SIGN(&tmp) == ZPOS) {
       
   454     if((res = s_mp_add_d(&tmp, d)) != MP_OKAY)
       
   455       goto CLEANUP;
       
   456   } else if(s_mp_cmp_d(&tmp, d) >= 0) {
       
   457     if((res = s_mp_sub_d(&tmp, d)) != MP_OKAY)
       
   458       goto CLEANUP;
       
   459   } else {
       
   460     mp_neg(&tmp, &tmp);
       
   461 
       
   462     DIGIT(&tmp, 0) = d - DIGIT(&tmp, 0);
       
   463   }
       
   464 
       
   465   if(s_mp_cmp_d(&tmp, 0) == 0)
       
   466     SIGN(&tmp) = ZPOS;
       
   467 
       
   468   s_mp_exch(&tmp, b);
       
   469 
       
   470 CLEANUP:
       
   471   mp_clear(&tmp);
       
   472   return res;
       
   473 
       
   474 } /* end mp_add_d() */
       
   475 
       
   476 /* }}} */
       
   477 
       
   478 /* {{{ mp_sub_d(a, d, b) */
       
   479 
       
   480 /*
       
   481   mp_sub_d(a, d, b)
       
   482 
       
   483   Compute the difference b = a - d, for a single digit d.  Respects the
       
   484   sign of its subtrahend (single digits are unsigned anyway).
       
   485  */
       
   486 
       
   487 mp_err mp_sub_d(const mp_int *a, mp_digit d, mp_int *b)
       
   488 {
       
   489   mp_int   tmp;
       
   490   mp_err   res;
       
   491 
       
   492   ARGCHK(a != NULL && b != NULL, MP_BADARG);
       
   493 
       
   494   if((res = mp_init_copy(&tmp, a)) != MP_OKAY)
       
   495     return res;
       
   496 
       
   497   if(SIGN(&tmp) == NEG) {
       
   498     if((res = s_mp_add_d(&tmp, d)) != MP_OKAY)
       
   499       goto CLEANUP;
       
   500   } else if(s_mp_cmp_d(&tmp, d) >= 0) {
       
   501     if((res = s_mp_sub_d(&tmp, d)) != MP_OKAY)
       
   502       goto CLEANUP;
       
   503   } else {
       
   504     mp_neg(&tmp, &tmp);
       
   505 
       
   506     DIGIT(&tmp, 0) = d - DIGIT(&tmp, 0);
       
   507     SIGN(&tmp) = NEG;
       
   508   }
       
   509 
       
   510   if(s_mp_cmp_d(&tmp, 0) == 0)
       
   511     SIGN(&tmp) = ZPOS;
       
   512 
       
   513   s_mp_exch(&tmp, b);
       
   514 
       
   515 CLEANUP:
       
   516   mp_clear(&tmp);
       
   517   return res;
       
   518 
       
   519 } /* end mp_sub_d() */
       
   520 
       
   521 /* }}} */
       
   522 
       
   523 /* {{{ mp_mul_d(a, d, b) */
       
   524 
       
   525 /*
       
   526   mp_mul_d(a, d, b)
       
   527 
       
   528   Compute the product b = a * d, for a single digit d.  Respects the sign
       
   529   of its multiplicand (single digits are unsigned anyway)
       
   530  */
       
   531 
       
   532 mp_err mp_mul_d(const mp_int *a, mp_digit d, mp_int *b)
       
   533 {
       
   534   mp_err  res;
       
   535 
       
   536   ARGCHK(a != NULL && b != NULL, MP_BADARG);
       
   537 
       
   538   if(d == 0) {
       
   539     mp_zero(b);
       
   540     return MP_OKAY;
       
   541   }
       
   542 
       
   543   if((res = mp_copy(a, b)) != MP_OKAY)
       
   544     return res;
       
   545 
       
   546   res = s_mp_mul_d(b, d);
       
   547 
       
   548   return res;
       
   549 
       
   550 } /* end mp_mul_d() */
       
   551 
       
   552 /* }}} */
       
   553 
       
   554 /* {{{ mp_mul_2(a, c) */
       
   555 
       
   556 mp_err mp_mul_2(const mp_int *a, mp_int *c)
       
   557 {
       
   558   mp_err  res;
       
   559 
       
   560   ARGCHK(a != NULL && c != NULL, MP_BADARG);
       
   561 
       
   562   if((res = mp_copy(a, c)) != MP_OKAY)
       
   563     return res;
       
   564 
       
   565   return s_mp_mul_2(c);
       
   566 
       
   567 } /* end mp_mul_2() */
       
   568 
       
   569 /* }}} */
       
   570 
       
   571 /* {{{ mp_div_d(a, d, q, r) */
       
   572 
       
   573 /*
       
   574   mp_div_d(a, d, q, r)
       
   575 
       
   576   Compute the quotient q = a / d and remainder r = a mod d, for a
       
   577   single digit d.  Respects the sign of its divisor (single digits are
       
   578   unsigned anyway).
       
   579  */
       
   580 
       
   581 mp_err mp_div_d(const mp_int *a, mp_digit d, mp_int *q, mp_digit *r)
       
   582 {
       
   583   mp_err   res;
       
   584   mp_int   qp;
       
   585   mp_digit rem;
       
   586   int      pow;
       
   587 
       
   588   ARGCHK(a != NULL, MP_BADARG);
       
   589 
       
   590   if(d == 0)
       
   591     return MP_RANGE;
       
   592 
       
   593   /* Shortcut for powers of two ... */
       
   594   if((pow = s_mp_ispow2d(d)) >= 0) {
       
   595     mp_digit  mask;
       
   596 
       
   597     mask = ((mp_digit)1 << pow) - 1;
       
   598     rem = DIGIT(a, 0) & mask;
       
   599 
       
   600     if(q) {
       
   601       mp_copy(a, q);
       
   602       s_mp_div_2d(q, pow);
       
   603     }
       
   604 
       
   605     if(r)
       
   606       *r = rem;
       
   607 
       
   608     return MP_OKAY;
       
   609   }
       
   610 
       
   611   if((res = mp_init_copy(&qp, a)) != MP_OKAY)
       
   612     return res;
       
   613 
       
   614   res = s_mp_div_d(&qp, d, &rem);
       
   615 
       
   616   if(s_mp_cmp_d(&qp, 0) == 0)
       
   617     SIGN(q) = ZPOS;
       
   618 
       
   619   if(r)
       
   620     *r = rem;
       
   621 
       
   622   if(q)
       
   623     s_mp_exch(&qp, q);
       
   624 
       
   625   mp_clear(&qp);
       
   626   return res;
       
   627 
       
   628 } /* end mp_div_d() */
       
   629 
       
   630 /* }}} */
       
   631 
       
   632 /* {{{ mp_div_2(a, c) */
       
   633 
       
   634 /*
       
   635   mp_div_2(a, c)
       
   636 
       
   637   Compute c = a / 2, disregarding the remainder.
       
   638  */
       
   639 
       
   640 mp_err mp_div_2(const mp_int *a, mp_int *c)
       
   641 {
       
   642   mp_err  res;
       
   643 
       
   644   ARGCHK(a != NULL && c != NULL, MP_BADARG);
       
   645 
       
   646   if((res = mp_copy(a, c)) != MP_OKAY)
       
   647     return res;
       
   648 
       
   649   s_mp_div_2(c);
       
   650 
       
   651   return MP_OKAY;
       
   652 
       
   653 } /* end mp_div_2() */
       
   654 
       
   655 /* }}} */
       
   656 
       
   657 /* {{{ mp_expt_d(a, d, b) */
       
   658 
       
   659 mp_err mp_expt_d(const mp_int *a, mp_digit d, mp_int *c)
       
   660 {
       
   661   mp_int   s, x;
       
   662   mp_err   res;
       
   663 
       
   664   ARGCHK(a != NULL && c != NULL, MP_BADARG);
       
   665 
       
   666   if((res = mp_init(&s, FLAG(a))) != MP_OKAY)
       
   667     return res;
       
   668   if((res = mp_init_copy(&x, a)) != MP_OKAY)
       
   669     goto X;
       
   670 
       
   671   DIGIT(&s, 0) = 1;
       
   672 
       
   673   while(d != 0) {
       
   674     if(d & 1) {
       
   675       if((res = s_mp_mul(&s, &x)) != MP_OKAY)
       
   676         goto CLEANUP;
       
   677     }
       
   678 
       
   679     d /= 2;
       
   680 
       
   681     if((res = s_mp_sqr(&x)) != MP_OKAY)
       
   682       goto CLEANUP;
       
   683   }
       
   684 
       
   685   s_mp_exch(&s, c);
       
   686 
       
   687 CLEANUP:
       
   688   mp_clear(&x);
       
   689 X:
       
   690   mp_clear(&s);
       
   691 
       
   692   return res;
       
   693 
       
   694 } /* end mp_expt_d() */
       
   695 
       
   696 /* }}} */
       
   697 
       
   698 /* }}} */
       
   699 
       
   700 /*------------------------------------------------------------------------*/
       
   701 /* {{{ Full arithmetic */
       
   702 
       
   703 /* {{{ mp_abs(a, b) */
       
   704 
       
   705 /*
       
   706   mp_abs(a, b)
       
   707 
       
   708   Compute b = |a|.  'a' and 'b' may be identical.
       
   709  */
       
   710 
       
   711 mp_err mp_abs(const mp_int *a, mp_int *b)
       
   712 {
       
   713   mp_err   res;
       
   714 
       
   715   ARGCHK(a != NULL && b != NULL, MP_BADARG);
       
   716 
       
   717   if((res = mp_copy(a, b)) != MP_OKAY)
       
   718     return res;
       
   719 
       
   720   SIGN(b) = ZPOS;
       
   721 
       
   722   return MP_OKAY;
       
   723 
       
   724 } /* end mp_abs() */
       
   725 
       
   726 /* }}} */
       
   727 
       
   728 /* {{{ mp_neg(a, b) */
       
   729 
       
   730 /*
       
   731   mp_neg(a, b)
       
   732 
       
   733   Compute b = -a.  'a' and 'b' may be identical.
       
   734  */
       
   735 
       
   736 mp_err mp_neg(const mp_int *a, mp_int *b)
       
   737 {
       
   738   mp_err   res;
       
   739 
       
   740   ARGCHK(a != NULL && b != NULL, MP_BADARG);
       
   741 
       
   742   if((res = mp_copy(a, b)) != MP_OKAY)
       
   743     return res;
       
   744 
       
   745   if(s_mp_cmp_d(b, 0) == MP_EQ)
       
   746     SIGN(b) = ZPOS;
       
   747   else
       
   748     SIGN(b) = (SIGN(b) == NEG) ? ZPOS : NEG;
       
   749 
       
   750   return MP_OKAY;
       
   751 
       
   752 } /* end mp_neg() */
       
   753 
       
   754 /* }}} */
       
   755 
       
   756 /* {{{ mp_add(a, b, c) */
       
   757 
       
   758 /*
       
   759   mp_add(a, b, c)
       
   760 
       
   761   Compute c = a + b.  All parameters may be identical.
       
   762  */
       
   763 
       
   764 mp_err mp_add(const mp_int *a, const mp_int *b, mp_int *c)
       
   765 {
       
   766   mp_err  res;
       
   767 
       
   768   ARGCHK(a != NULL && b != NULL && c != NULL, MP_BADARG);
       
   769 
       
   770   if(SIGN(a) == SIGN(b)) { /* same sign:  add values, keep sign */
       
   771     MP_CHECKOK( s_mp_add_3arg(a, b, c) );
       
   772   } else if(s_mp_cmp(a, b) >= 0) {  /* different sign: |a| >= |b|   */
       
   773     MP_CHECKOK( s_mp_sub_3arg(a, b, c) );
       
   774   } else {                          /* different sign: |a|  < |b|   */
       
   775     MP_CHECKOK( s_mp_sub_3arg(b, a, c) );
       
   776   }
       
   777 
       
   778   if (s_mp_cmp_d(c, 0) == MP_EQ)
       
   779     SIGN(c) = ZPOS;
       
   780 
       
   781 CLEANUP:
       
   782   return res;
       
   783 
       
   784 } /* end mp_add() */
       
   785 
       
   786 /* }}} */
       
   787 
       
   788 /* {{{ mp_sub(a, b, c) */
       
   789 
       
   790 /*
       
   791   mp_sub(a, b, c)
       
   792 
       
   793   Compute c = a - b.  All parameters may be identical.
       
   794  */
       
   795 
       
   796 mp_err mp_sub(const mp_int *a, const mp_int *b, mp_int *c)
       
   797 {
       
   798   mp_err  res;
       
   799   int     magDiff;
       
   800 
       
   801   ARGCHK(a != NULL && b != NULL && c != NULL, MP_BADARG);
       
   802 
       
   803   if (a == b) {
       
   804     mp_zero(c);
       
   805     return MP_OKAY;
       
   806   }
       
   807 
       
   808   if (MP_SIGN(a) != MP_SIGN(b)) {
       
   809     MP_CHECKOK( s_mp_add_3arg(a, b, c) );
       
   810   } else if (!(magDiff = s_mp_cmp(a, b))) {
       
   811     mp_zero(c);
       
   812     res = MP_OKAY;
       
   813   } else if (magDiff > 0) {
       
   814     MP_CHECKOK( s_mp_sub_3arg(a, b, c) );
       
   815   } else {
       
   816     MP_CHECKOK( s_mp_sub_3arg(b, a, c) );
       
   817     MP_SIGN(c) = !MP_SIGN(a);
       
   818   }
       
   819 
       
   820   if (s_mp_cmp_d(c, 0) == MP_EQ)
       
   821     MP_SIGN(c) = MP_ZPOS;
       
   822 
       
   823 CLEANUP:
       
   824   return res;
       
   825 
       
   826 } /* end mp_sub() */
       
   827 
       
   828 /* }}} */
       
   829 
       
   830 /* {{{ mp_mul(a, b, c) */
       
   831 
       
   832 /*
       
   833   mp_mul(a, b, c)
       
   834 
       
   835   Compute c = a * b.  All parameters may be identical.
       
   836  */
       
   837 mp_err   mp_mul(const mp_int *a, const mp_int *b, mp_int * c)
       
   838 {
       
   839   mp_digit *pb;
       
   840   mp_int   tmp;
       
   841   mp_err   res;
       
   842   mp_size  ib;
       
   843   mp_size  useda, usedb;
       
   844 
       
   845   ARGCHK(a != NULL && b != NULL && c != NULL, MP_BADARG);
       
   846 
       
   847   if (a == c) {
       
   848     if ((res = mp_init_copy(&tmp, a)) != MP_OKAY)
       
   849       return res;
       
   850     if (a == b)
       
   851       b = &tmp;
       
   852     a = &tmp;
       
   853   } else if (b == c) {
       
   854     if ((res = mp_init_copy(&tmp, b)) != MP_OKAY)
       
   855       return res;
       
   856     b = &tmp;
       
   857   } else {
       
   858     MP_DIGITS(&tmp) = 0;
       
   859   }
       
   860 
       
   861   if (MP_USED(a) < MP_USED(b)) {
       
   862     const mp_int *xch = b;      /* switch a and b, to do fewer outer loops */
       
   863     b = a;
       
   864     a = xch;
       
   865   }
       
   866 
       
   867   MP_USED(c) = 1; MP_DIGIT(c, 0) = 0;
       
   868   if((res = s_mp_pad(c, USED(a) + USED(b))) != MP_OKAY)
       
   869     goto CLEANUP;
       
   870 
       
   871 #ifdef NSS_USE_COMBA
       
   872   if ((MP_USED(a) == MP_USED(b)) && IS_POWER_OF_2(MP_USED(b))) {
       
   873       if (MP_USED(a) == 4) {
       
   874           s_mp_mul_comba_4(a, b, c);
       
   875           goto CLEANUP;
       
   876       }
       
   877       if (MP_USED(a) == 8) {
       
   878           s_mp_mul_comba_8(a, b, c);
       
   879           goto CLEANUP;
       
   880       }
       
   881       if (MP_USED(a) == 16) {
       
   882           s_mp_mul_comba_16(a, b, c);
       
   883           goto CLEANUP;
       
   884       }
       
   885       if (MP_USED(a) == 32) {
       
   886           s_mp_mul_comba_32(a, b, c);
       
   887           goto CLEANUP;
       
   888       }
       
   889   }
       
   890 #endif
       
   891 
       
   892   pb = MP_DIGITS(b);
       
   893   s_mpv_mul_d(MP_DIGITS(a), MP_USED(a), *pb++, MP_DIGITS(c));
       
   894 
       
   895   /* Outer loop:  Digits of b */
       
   896   useda = MP_USED(a);
       
   897   usedb = MP_USED(b);
       
   898   for (ib = 1; ib < usedb; ib++) {
       
   899     mp_digit b_i    = *pb++;
       
   900 
       
   901     /* Inner product:  Digits of a */
       
   902     if (b_i)
       
   903       s_mpv_mul_d_add(MP_DIGITS(a), useda, b_i, MP_DIGITS(c) + ib);
       
   904     else
       
   905       MP_DIGIT(c, ib + useda) = b_i;
       
   906   }
       
   907 
       
   908   s_mp_clamp(c);
       
   909 
       
   910   if(SIGN(a) == SIGN(b) || s_mp_cmp_d(c, 0) == MP_EQ)
       
   911     SIGN(c) = ZPOS;
       
   912   else
       
   913     SIGN(c) = NEG;
       
   914 
       
   915 CLEANUP:
       
   916   mp_clear(&tmp);
       
   917   return res;
       
   918 } /* end mp_mul() */
       
   919 
       
   920 /* }}} */
       
   921 
       
   922 /* {{{ mp_sqr(a, sqr) */
       
   923 
       
   924 #if MP_SQUARE
       
   925 /*
       
   926   Computes the square of a.  This can be done more
       
   927   efficiently than a general multiplication, because many of the
       
   928   computation steps are redundant when squaring.  The inner product
       
   929   step is a bit more complicated, but we save a fair number of
       
   930   iterations of the multiplication loop.
       
   931  */
       
   932 
       
   933 /* sqr = a^2;   Caller provides both a and tmp; */
       
   934 mp_err   mp_sqr(const mp_int *a, mp_int *sqr)
       
   935 {
       
   936   mp_digit *pa;
       
   937   mp_digit d;
       
   938   mp_err   res;
       
   939   mp_size  ix;
       
   940   mp_int   tmp;
       
   941   int      count;
       
   942 
       
   943   ARGCHK(a != NULL && sqr != NULL, MP_BADARG);
       
   944 
       
   945   if (a == sqr) {
       
   946     if((res = mp_init_copy(&tmp, a)) != MP_OKAY)
       
   947       return res;
       
   948     a = &tmp;
       
   949   } else {
       
   950     DIGITS(&tmp) = 0;
       
   951     res = MP_OKAY;
       
   952   }
       
   953 
       
   954   ix = 2 * MP_USED(a);
       
   955   if (ix > MP_ALLOC(sqr)) {
       
   956     MP_USED(sqr) = 1;
       
   957     MP_CHECKOK( s_mp_grow(sqr, ix) );
       
   958   }
       
   959   MP_USED(sqr) = ix;
       
   960   MP_DIGIT(sqr, 0) = 0;
       
   961 
       
   962 #ifdef NSS_USE_COMBA
       
   963   if (IS_POWER_OF_2(MP_USED(a))) {
       
   964       if (MP_USED(a) == 4) {
       
   965           s_mp_sqr_comba_4(a, sqr);
       
   966           goto CLEANUP;
       
   967       }
       
   968       if (MP_USED(a) == 8) {
       
   969           s_mp_sqr_comba_8(a, sqr);
       
   970           goto CLEANUP;
       
   971       }
       
   972       if (MP_USED(a) == 16) {
       
   973           s_mp_sqr_comba_16(a, sqr);
       
   974           goto CLEANUP;
       
   975       }
       
   976       if (MP_USED(a) == 32) {
       
   977           s_mp_sqr_comba_32(a, sqr);
       
   978           goto CLEANUP;
       
   979       }
       
   980   }
       
   981 #endif
       
   982 
       
   983   pa = MP_DIGITS(a);
       
   984   count = MP_USED(a) - 1;
       
   985   if (count > 0) {
       
   986     d = *pa++;
       
   987     s_mpv_mul_d(pa, count, d, MP_DIGITS(sqr) + 1);
       
   988     for (ix = 3; --count > 0; ix += 2) {
       
   989       d = *pa++;
       
   990       s_mpv_mul_d_add(pa, count, d, MP_DIGITS(sqr) + ix);
       
   991     } /* for(ix ...) */
       
   992     MP_DIGIT(sqr, MP_USED(sqr)-1) = 0; /* above loop stopped short of this. */
       
   993 
       
   994     /* now sqr *= 2 */
       
   995     s_mp_mul_2(sqr);
       
   996   } else {
       
   997     MP_DIGIT(sqr, 1) = 0;
       
   998   }
       
   999 
       
  1000   /* now add the squares of the digits of a to sqr. */
       
  1001   s_mpv_sqr_add_prop(MP_DIGITS(a), MP_USED(a), MP_DIGITS(sqr));
       
  1002 
       
  1003   SIGN(sqr) = ZPOS;
       
  1004   s_mp_clamp(sqr);
       
  1005 
       
  1006 CLEANUP:
       
  1007   mp_clear(&tmp);
       
  1008   return res;
       
  1009 
       
  1010 } /* end mp_sqr() */
       
  1011 #endif
       
  1012 
       
  1013 /* }}} */
       
  1014 
       
  1015 /* {{{ mp_div(a, b, q, r) */
       
  1016 
       
  1017 /*
       
  1018   mp_div(a, b, q, r)
       
  1019 
       
  1020   Compute q = a / b and r = a mod b.  Input parameters may be re-used
       
  1021   as output parameters.  If q or r is NULL, that portion of the
       
  1022   computation will be discarded (although it will still be computed)
       
  1023  */
       
  1024 mp_err mp_div(const mp_int *a, const mp_int *b, mp_int *q, mp_int *r)
       
  1025 {
       
  1026   mp_err   res;
       
  1027   mp_int   *pQ, *pR;
       
  1028   mp_int   qtmp, rtmp, btmp;
       
  1029   int      cmp;
       
  1030   mp_sign  signA;
       
  1031   mp_sign  signB;
       
  1032 
       
  1033   ARGCHK(a != NULL && b != NULL, MP_BADARG);
       
  1034 
       
  1035   signA = MP_SIGN(a);
       
  1036   signB = MP_SIGN(b);
       
  1037 
       
  1038   if(mp_cmp_z(b) == MP_EQ)
       
  1039     return MP_RANGE;
       
  1040 
       
  1041   DIGITS(&qtmp) = 0;
       
  1042   DIGITS(&rtmp) = 0;
       
  1043   DIGITS(&btmp) = 0;
       
  1044 
       
  1045   /* Set up some temporaries... */
       
  1046   if (!r || r == a || r == b) {
       
  1047     MP_CHECKOK( mp_init_copy(&rtmp, a) );
       
  1048     pR = &rtmp;
       
  1049   } else {
       
  1050     MP_CHECKOK( mp_copy(a, r) );
       
  1051     pR = r;
       
  1052   }
       
  1053 
       
  1054   if (!q || q == a || q == b) {
       
  1055     MP_CHECKOK( mp_init_size(&qtmp, MP_USED(a), FLAG(a)) );
       
  1056     pQ = &qtmp;
       
  1057   } else {
       
  1058     MP_CHECKOK( s_mp_pad(q, MP_USED(a)) );
       
  1059     pQ = q;
       
  1060     mp_zero(pQ);
       
  1061   }
       
  1062 
       
  1063   /*
       
  1064     If |a| <= |b|, we can compute the solution without division;
       
  1065     otherwise, we actually do the work required.
       
  1066    */
       
  1067   if ((cmp = s_mp_cmp(a, b)) <= 0) {
       
  1068     if (cmp) {
       
  1069       /* r was set to a above. */
       
  1070       mp_zero(pQ);
       
  1071     } else {
       
  1072       mp_set(pQ, 1);
       
  1073       mp_zero(pR);
       
  1074     }
       
  1075   } else {
       
  1076     MP_CHECKOK( mp_init_copy(&btmp, b) );
       
  1077     MP_CHECKOK( s_mp_div(pR, &btmp, pQ) );
       
  1078   }
       
  1079 
       
  1080   /* Compute the signs for the output  */
       
  1081   MP_SIGN(pR) = signA;   /* Sr = Sa              */
       
  1082   /* Sq = ZPOS if Sa == Sb */ /* Sq = NEG if Sa != Sb */
       
  1083   MP_SIGN(pQ) = (signA == signB) ? ZPOS : NEG;
       
  1084 
       
  1085   if(s_mp_cmp_d(pQ, 0) == MP_EQ)
       
  1086     SIGN(pQ) = ZPOS;
       
  1087   if(s_mp_cmp_d(pR, 0) == MP_EQ)
       
  1088     SIGN(pR) = ZPOS;
       
  1089 
       
  1090   /* Copy output, if it is needed      */
       
  1091   if(q && q != pQ)
       
  1092     s_mp_exch(pQ, q);
       
  1093 
       
  1094   if(r && r != pR)
       
  1095     s_mp_exch(pR, r);
       
  1096 
       
  1097 CLEANUP:
       
  1098   mp_clear(&btmp);
       
  1099   mp_clear(&rtmp);
       
  1100   mp_clear(&qtmp);
       
  1101 
       
  1102   return res;
       
  1103 
       
  1104 } /* end mp_div() */
       
  1105 
       
  1106 /* }}} */
       
  1107 
       
  1108 /* {{{ mp_div_2d(a, d, q, r) */
       
  1109 
       
  1110 mp_err mp_div_2d(const mp_int *a, mp_digit d, mp_int *q, mp_int *r)
       
  1111 {
       
  1112   mp_err  res;
       
  1113 
       
  1114   ARGCHK(a != NULL, MP_BADARG);
       
  1115 
       
  1116   if(q) {
       
  1117     if((res = mp_copy(a, q)) != MP_OKAY)
       
  1118       return res;
       
  1119   }
       
  1120   if(r) {
       
  1121     if((res = mp_copy(a, r)) != MP_OKAY)
       
  1122       return res;
       
  1123   }
       
  1124   if(q) {
       
  1125     s_mp_div_2d(q, d);
       
  1126   }
       
  1127   if(r) {
       
  1128     s_mp_mod_2d(r, d);
       
  1129   }
       
  1130 
       
  1131   return MP_OKAY;
       
  1132 
       
  1133 } /* end mp_div_2d() */
       
  1134 
       
  1135 /* }}} */
       
  1136 
       
  1137 /* {{{ mp_expt(a, b, c) */
       
  1138 
       
  1139 /*
       
  1140   mp_expt(a, b, c)
       
  1141 
       
  1142   Compute c = a ** b, that is, raise a to the b power.  Uses a
       
  1143   standard iterative square-and-multiply technique.
       
  1144  */
       
  1145 
       
  1146 mp_err mp_expt(mp_int *a, mp_int *b, mp_int *c)
       
  1147 {
       
  1148   mp_int   s, x;
       
  1149   mp_err   res;
       
  1150   mp_digit d;
       
  1151   int      dig, bit;
       
  1152 
       
  1153   ARGCHK(a != NULL && b != NULL && c != NULL, MP_BADARG);
       
  1154 
       
  1155   if(mp_cmp_z(b) < 0)
       
  1156     return MP_RANGE;
       
  1157 
       
  1158   if((res = mp_init(&s, FLAG(a))) != MP_OKAY)
       
  1159     return res;
       
  1160 
       
  1161   mp_set(&s, 1);
       
  1162 
       
  1163   if((res = mp_init_copy(&x, a)) != MP_OKAY)
       
  1164     goto X;
       
  1165 
       
  1166   /* Loop over low-order digits in ascending order */
       
  1167   for(dig = 0; dig < (USED(b) - 1); dig++) {
       
  1168     d = DIGIT(b, dig);
       
  1169 
       
  1170     /* Loop over bits of each non-maximal digit */
       
  1171     for(bit = 0; bit < DIGIT_BIT; bit++) {
       
  1172       if(d & 1) {
       
  1173         if((res = s_mp_mul(&s, &x)) != MP_OKAY)
       
  1174           goto CLEANUP;
       
  1175       }
       
  1176 
       
  1177       d >>= 1;
       
  1178 
       
  1179       if((res = s_mp_sqr(&x)) != MP_OKAY)
       
  1180         goto CLEANUP;
       
  1181     }
       
  1182   }
       
  1183 
       
  1184   /* Consider now the last digit... */
       
  1185   d = DIGIT(b, dig);
       
  1186 
       
  1187   while(d) {
       
  1188     if(d & 1) {
       
  1189       if((res = s_mp_mul(&s, &x)) != MP_OKAY)
       
  1190         goto CLEANUP;
       
  1191     }
       
  1192 
       
  1193     d >>= 1;
       
  1194 
       
  1195     if((res = s_mp_sqr(&x)) != MP_OKAY)
       
  1196       goto CLEANUP;
       
  1197   }
       
  1198 
       
  1199   if(mp_iseven(b))
       
  1200     SIGN(&s) = SIGN(a);
       
  1201 
       
  1202   res = mp_copy(&s, c);
       
  1203 
       
  1204 CLEANUP:
       
  1205   mp_clear(&x);
       
  1206 X:
       
  1207   mp_clear(&s);
       
  1208 
       
  1209   return res;
       
  1210 
       
  1211 } /* end mp_expt() */
       
  1212 
       
  1213 /* }}} */
       
  1214 
       
  1215 /* {{{ mp_2expt(a, k) */
       
  1216 
       
  1217 /* Compute a = 2^k */
       
  1218 
       
  1219 mp_err mp_2expt(mp_int *a, mp_digit k)
       
  1220 {
       
  1221   ARGCHK(a != NULL, MP_BADARG);
       
  1222 
       
  1223   return s_mp_2expt(a, k);
       
  1224 
       
  1225 } /* end mp_2expt() */
       
  1226 
       
  1227 /* }}} */
       
  1228 
       
  1229 /* {{{ mp_mod(a, m, c) */
       
  1230 
       
  1231 /*
       
  1232   mp_mod(a, m, c)
       
  1233 
       
  1234   Compute c = a (mod m).  Result will always be 0 <= c < m.
       
  1235  */
       
  1236 
       
  1237 mp_err mp_mod(const mp_int *a, const mp_int *m, mp_int *c)
       
  1238 {
       
  1239   mp_err  res;
       
  1240   int     mag;
       
  1241 
       
  1242   ARGCHK(a != NULL && m != NULL && c != NULL, MP_BADARG);
       
  1243 
       
  1244   if(SIGN(m) == NEG)
       
  1245     return MP_RANGE;
       
  1246 
       
  1247   /*
       
  1248      If |a| > m, we need to divide to get the remainder and take the
       
  1249      absolute value.
       
  1250 
       
  1251      If |a| < m, we don't need to do any division, just copy and adjust
       
  1252      the sign (if a is negative).
       
  1253 
       
  1254      If |a| == m, we can simply set the result to zero.
       
  1255 
       
  1256      This order is intended to minimize the average path length of the
       
  1257      comparison chain on common workloads -- the most frequent cases are
       
  1258      that |a| != m, so we do those first.
       
  1259    */
       
  1260   if((mag = s_mp_cmp(a, m)) > 0) {
       
  1261     if((res = mp_div(a, m, NULL, c)) != MP_OKAY)
       
  1262       return res;
       
  1263 
       
  1264     if(SIGN(c) == NEG) {
       
  1265       if((res = mp_add(c, m, c)) != MP_OKAY)
       
  1266         return res;
       
  1267     }
       
  1268 
       
  1269   } else if(mag < 0) {
       
  1270     if((res = mp_copy(a, c)) != MP_OKAY)
       
  1271       return res;
       
  1272 
       
  1273     if(mp_cmp_z(a) < 0) {
       
  1274       if((res = mp_add(c, m, c)) != MP_OKAY)
       
  1275         return res;
       
  1276 
       
  1277     }
       
  1278 
       
  1279   } else {
       
  1280     mp_zero(c);
       
  1281 
       
  1282   }
       
  1283 
       
  1284   return MP_OKAY;
       
  1285 
       
  1286 } /* end mp_mod() */
       
  1287 
       
  1288 /* }}} */
       
  1289 
       
  1290 /* {{{ mp_mod_d(a, d, c) */
       
  1291 
       
  1292 /*
       
  1293   mp_mod_d(a, d, c)
       
  1294 
       
  1295   Compute c = a (mod d).  Result will always be 0 <= c < d
       
  1296  */
       
  1297 mp_err mp_mod_d(const mp_int *a, mp_digit d, mp_digit *c)
       
  1298 {
       
  1299   mp_err   res;
       
  1300   mp_digit rem;
       
  1301 
       
  1302   ARGCHK(a != NULL && c != NULL, MP_BADARG);
       
  1303 
       
  1304   if(s_mp_cmp_d(a, d) > 0) {
       
  1305     if((res = mp_div_d(a, d, NULL, &rem)) != MP_OKAY)
       
  1306       return res;
       
  1307 
       
  1308   } else {
       
  1309     if(SIGN(a) == NEG)
       
  1310       rem = d - DIGIT(a, 0);
       
  1311     else
       
  1312       rem = DIGIT(a, 0);
       
  1313   }
       
  1314 
       
  1315   if(c)
       
  1316     *c = rem;
       
  1317 
       
  1318   return MP_OKAY;
       
  1319 
       
  1320 } /* end mp_mod_d() */
       
  1321 
       
  1322 /* }}} */
       
  1323 
       
  1324 /* {{{ mp_sqrt(a, b) */
       
  1325 
       
  1326 /*
       
  1327   mp_sqrt(a, b)
       
  1328 
       
  1329   Compute the integer square root of a, and store the result in b.
       
  1330   Uses an integer-arithmetic version of Newton's iterative linear
       
  1331   approximation technique to determine this value; the result has the
       
  1332   following two properties:
       
  1333 
       
  1334      b^2 <= a
       
  1335      (b+1)^2 >= a
       
  1336 
       
  1337   It is a range error to pass a negative value.
       
  1338  */
       
  1339 mp_err mp_sqrt(const mp_int *a, mp_int *b)
       
  1340 {
       
  1341   mp_int   x, t;
       
  1342   mp_err   res;
       
  1343   mp_size  used;
       
  1344 
       
  1345   ARGCHK(a != NULL && b != NULL, MP_BADARG);
       
  1346 
       
  1347   /* Cannot take square root of a negative value */
       
  1348   if(SIGN(a) == NEG)
       
  1349     return MP_RANGE;
       
  1350 
       
  1351   /* Special cases for zero and one, trivial     */
       
  1352   if(mp_cmp_d(a, 1) <= 0)
       
  1353     return mp_copy(a, b);
       
  1354 
       
  1355   /* Initialize the temporaries we'll use below  */
       
  1356   if((res = mp_init_size(&t, USED(a), FLAG(a))) != MP_OKAY)
       
  1357     return res;
       
  1358 
       
  1359   /* Compute an initial guess for the iteration as a itself */
       
  1360   if((res = mp_init_copy(&x, a)) != MP_OKAY)
       
  1361     goto X;
       
  1362 
       
  1363   used = MP_USED(&x);
       
  1364   if (used > 1) {
       
  1365     s_mp_rshd(&x, used / 2);
       
  1366   }
       
  1367 
       
  1368   for(;;) {
       
  1369     /* t = (x * x) - a */
       
  1370     mp_copy(&x, &t);      /* can't fail, t is big enough for original x */
       
  1371     if((res = mp_sqr(&t, &t)) != MP_OKAY ||
       
  1372        (res = mp_sub(&t, a, &t)) != MP_OKAY)
       
  1373       goto CLEANUP;
       
  1374 
       
  1375     /* t = t / 2x       */
       
  1376     s_mp_mul_2(&x);
       
  1377     if((res = mp_div(&t, &x, &t, NULL)) != MP_OKAY)
       
  1378       goto CLEANUP;
       
  1379     s_mp_div_2(&x);
       
  1380 
       
  1381     /* Terminate the loop, if the quotient is zero */
       
  1382     if(mp_cmp_z(&t) == MP_EQ)
       
  1383       break;
       
  1384 
       
  1385     /* x = x - t       */
       
  1386     if((res = mp_sub(&x, &t, &x)) != MP_OKAY)
       
  1387       goto CLEANUP;
       
  1388 
       
  1389   }
       
  1390 
       
  1391   /* Copy result to output parameter */
       
  1392   mp_sub_d(&x, 1, &x);
       
  1393   s_mp_exch(&x, b);
       
  1394 
       
  1395  CLEANUP:
       
  1396   mp_clear(&x);
       
  1397  X:
       
  1398   mp_clear(&t);
       
  1399 
       
  1400   return res;
       
  1401 
       
  1402 } /* end mp_sqrt() */
       
  1403 
       
  1404 /* }}} */
       
  1405 
       
  1406 /* }}} */
       
  1407 
       
  1408 /*------------------------------------------------------------------------*/
       
  1409 /* {{{ Modular arithmetic */
       
  1410 
       
  1411 #if MP_MODARITH
       
  1412 /* {{{ mp_addmod(a, b, m, c) */
       
  1413 
       
  1414 /*
       
  1415   mp_addmod(a, b, m, c)
       
  1416 
       
  1417   Compute c = (a + b) mod m
       
  1418  */
       
  1419 
       
  1420 mp_err mp_addmod(const mp_int *a, const mp_int *b, const mp_int *m, mp_int *c)
       
  1421 {
       
  1422   mp_err  res;
       
  1423 
       
  1424   ARGCHK(a != NULL && b != NULL && m != NULL && c != NULL, MP_BADARG);
       
  1425 
       
  1426   if((res = mp_add(a, b, c)) != MP_OKAY)
       
  1427     return res;
       
  1428   if((res = mp_mod(c, m, c)) != MP_OKAY)
       
  1429     return res;
       
  1430 
       
  1431   return MP_OKAY;
       
  1432 
       
  1433 }
       
  1434 
       
  1435 /* }}} */
       
  1436 
       
  1437 /* {{{ mp_submod(a, b, m, c) */
       
  1438 
       
  1439 /*
       
  1440   mp_submod(a, b, m, c)
       
  1441 
       
  1442   Compute c = (a - b) mod m
       
  1443  */
       
  1444 
       
  1445 mp_err mp_submod(const mp_int *a, const mp_int *b, const mp_int *m, mp_int *c)
       
  1446 {
       
  1447   mp_err  res;
       
  1448 
       
  1449   ARGCHK(a != NULL && b != NULL && m != NULL && c != NULL, MP_BADARG);
       
  1450 
       
  1451   if((res = mp_sub(a, b, c)) != MP_OKAY)
       
  1452     return res;
       
  1453   if((res = mp_mod(c, m, c)) != MP_OKAY)
       
  1454     return res;
       
  1455 
       
  1456   return MP_OKAY;
       
  1457 
       
  1458 }
       
  1459 
       
  1460 /* }}} */
       
  1461 
       
  1462 /* {{{ mp_mulmod(a, b, m, c) */
       
  1463 
       
  1464 /*
       
  1465   mp_mulmod(a, b, m, c)
       
  1466 
       
  1467   Compute c = (a * b) mod m
       
  1468  */
       
  1469 
       
  1470 mp_err mp_mulmod(const mp_int *a, const mp_int *b, const mp_int *m, mp_int *c)
       
  1471 {
       
  1472   mp_err  res;
       
  1473 
       
  1474   ARGCHK(a != NULL && b != NULL && m != NULL && c != NULL, MP_BADARG);
       
  1475 
       
  1476   if((res = mp_mul(a, b, c)) != MP_OKAY)
       
  1477     return res;
       
  1478   if((res = mp_mod(c, m, c)) != MP_OKAY)
       
  1479     return res;
       
  1480 
       
  1481   return MP_OKAY;
       
  1482 
       
  1483 }
       
  1484 
       
  1485 /* }}} */
       
  1486 
       
  1487 /* {{{ mp_sqrmod(a, m, c) */
       
  1488 
       
  1489 #if MP_SQUARE
       
  1490 mp_err mp_sqrmod(const mp_int *a, const mp_int *m, mp_int *c)
       
  1491 {
       
  1492   mp_err  res;
       
  1493 
       
  1494   ARGCHK(a != NULL && m != NULL && c != NULL, MP_BADARG);
       
  1495 
       
  1496   if((res = mp_sqr(a, c)) != MP_OKAY)
       
  1497     return res;
       
  1498   if((res = mp_mod(c, m, c)) != MP_OKAY)
       
  1499     return res;
       
  1500 
       
  1501   return MP_OKAY;
       
  1502 
       
  1503 } /* end mp_sqrmod() */
       
  1504 #endif
       
  1505 
       
  1506 /* }}} */
       
  1507 
       
  1508 /* {{{ s_mp_exptmod(a, b, m, c) */
       
  1509 
       
  1510 /*
       
  1511   s_mp_exptmod(a, b, m, c)
       
  1512 
       
  1513   Compute c = (a ** b) mod m.  Uses a standard square-and-multiply
       
  1514   method with modular reductions at each step. (This is basically the
       
  1515   same code as mp_expt(), except for the addition of the reductions)
       
  1516 
       
  1517   The modular reductions are done using Barrett's algorithm (see
       
  1518   s_mp_reduce() below for details)
       
  1519  */
       
  1520 
       
  1521 mp_err s_mp_exptmod(const mp_int *a, const mp_int *b, const mp_int *m, mp_int *c)
       
  1522 {
       
  1523   mp_int   s, x, mu;
       
  1524   mp_err   res;
       
  1525   mp_digit d;
       
  1526   int      dig, bit;
       
  1527 
       
  1528   ARGCHK(a != NULL && b != NULL && c != NULL, MP_BADARG);
       
  1529 
       
  1530   if(mp_cmp_z(b) < 0 || mp_cmp_z(m) <= 0)
       
  1531     return MP_RANGE;
       
  1532 
       
  1533   if((res = mp_init(&s, FLAG(a))) != MP_OKAY)
       
  1534     return res;
       
  1535   if((res = mp_init_copy(&x, a)) != MP_OKAY ||
       
  1536      (res = mp_mod(&x, m, &x)) != MP_OKAY)
       
  1537     goto X;
       
  1538   if((res = mp_init(&mu, FLAG(a))) != MP_OKAY)
       
  1539     goto MU;
       
  1540 
       
  1541   mp_set(&s, 1);
       
  1542 
       
  1543   /* mu = b^2k / m */
       
  1544   s_mp_add_d(&mu, 1);
       
  1545   s_mp_lshd(&mu, 2 * USED(m));
       
  1546   if((res = mp_div(&mu, m, &mu, NULL)) != MP_OKAY)
       
  1547     goto CLEANUP;
       
  1548 
       
  1549   /* Loop over digits of b in ascending order, except highest order */
       
  1550   for(dig = 0; dig < (USED(b) - 1); dig++) {
       
  1551     d = DIGIT(b, dig);
       
  1552 
       
  1553     /* Loop over the bits of the lower-order digits */
       
  1554     for(bit = 0; bit < DIGIT_BIT; bit++) {
       
  1555       if(d & 1) {
       
  1556         if((res = s_mp_mul(&s, &x)) != MP_OKAY)
       
  1557           goto CLEANUP;
       
  1558         if((res = s_mp_reduce(&s, m, &mu)) != MP_OKAY)
       
  1559           goto CLEANUP;
       
  1560       }
       
  1561 
       
  1562       d >>= 1;
       
  1563 
       
  1564       if((res = s_mp_sqr(&x)) != MP_OKAY)
       
  1565         goto CLEANUP;
       
  1566       if((res = s_mp_reduce(&x, m, &mu)) != MP_OKAY)
       
  1567         goto CLEANUP;
       
  1568     }
       
  1569   }
       
  1570 
       
  1571   /* Now do the last digit... */
       
  1572   d = DIGIT(b, dig);
       
  1573 
       
  1574   while(d) {
       
  1575     if(d & 1) {
       
  1576       if((res = s_mp_mul(&s, &x)) != MP_OKAY)
       
  1577         goto CLEANUP;
       
  1578       if((res = s_mp_reduce(&s, m, &mu)) != MP_OKAY)
       
  1579         goto CLEANUP;
       
  1580     }
       
  1581 
       
  1582     d >>= 1;
       
  1583 
       
  1584     if((res = s_mp_sqr(&x)) != MP_OKAY)
       
  1585       goto CLEANUP;
       
  1586     if((res = s_mp_reduce(&x, m, &mu)) != MP_OKAY)
       
  1587       goto CLEANUP;
       
  1588   }
       
  1589 
       
  1590   s_mp_exch(&s, c);
       
  1591 
       
  1592  CLEANUP:
       
  1593   mp_clear(&mu);
       
  1594  MU:
       
  1595   mp_clear(&x);
       
  1596  X:
       
  1597   mp_clear(&s);
       
  1598 
       
  1599   return res;
       
  1600 
       
  1601 } /* end s_mp_exptmod() */
       
  1602 
       
  1603 /* }}} */
       
  1604 
       
  1605 /* {{{ mp_exptmod_d(a, d, m, c) */
       
  1606 
       
  1607 mp_err mp_exptmod_d(const mp_int *a, mp_digit d, const mp_int *m, mp_int *c)
       
  1608 {
       
  1609   mp_int   s, x;
       
  1610   mp_err   res;
       
  1611 
       
  1612   ARGCHK(a != NULL && c != NULL, MP_BADARG);
       
  1613 
       
  1614   if((res = mp_init(&s, FLAG(a))) != MP_OKAY)
       
  1615     return res;
       
  1616   if((res = mp_init_copy(&x, a)) != MP_OKAY)
       
  1617     goto X;
       
  1618 
       
  1619   mp_set(&s, 1);
       
  1620 
       
  1621   while(d != 0) {
       
  1622     if(d & 1) {
       
  1623       if((res = s_mp_mul(&s, &x)) != MP_OKAY ||
       
  1624          (res = mp_mod(&s, m, &s)) != MP_OKAY)
       
  1625         goto CLEANUP;
       
  1626     }
       
  1627 
       
  1628     d /= 2;
       
  1629 
       
  1630     if((res = s_mp_sqr(&x)) != MP_OKAY ||
       
  1631        (res = mp_mod(&x, m, &x)) != MP_OKAY)
       
  1632       goto CLEANUP;
       
  1633   }
       
  1634 
       
  1635   s_mp_exch(&s, c);
       
  1636 
       
  1637 CLEANUP:
       
  1638   mp_clear(&x);
       
  1639 X:
       
  1640   mp_clear(&s);
       
  1641 
       
  1642   return res;
       
  1643 
       
  1644 } /* end mp_exptmod_d() */
       
  1645 
       
  1646 /* }}} */
       
  1647 #endif /* if MP_MODARITH */
       
  1648 
       
  1649 /* }}} */
       
  1650 
       
  1651 /*------------------------------------------------------------------------*/
       
  1652 /* {{{ Comparison functions */
       
  1653 
       
  1654 /* {{{ mp_cmp_z(a) */
       
  1655 
       
  1656 /*
       
  1657   mp_cmp_z(a)
       
  1658 
       
  1659   Compare a <=> 0.  Returns <0 if a<0, 0 if a=0, >0 if a>0.
       
  1660  */
       
  1661 
       
  1662 int    mp_cmp_z(const mp_int *a)
       
  1663 {
       
  1664   if(SIGN(a) == NEG)
       
  1665     return MP_LT;
       
  1666   else if(USED(a) == 1 && DIGIT(a, 0) == 0)
       
  1667     return MP_EQ;
       
  1668   else
       
  1669     return MP_GT;
       
  1670 
       
  1671 } /* end mp_cmp_z() */
       
  1672 
       
  1673 /* }}} */
       
  1674 
       
  1675 /* {{{ mp_cmp_d(a, d) */
       
  1676 
       
  1677 /*
       
  1678   mp_cmp_d(a, d)
       
  1679 
       
  1680   Compare a <=> d.  Returns <0 if a<d, 0 if a=d, >0 if a>d
       
  1681  */
       
  1682 
       
  1683 int    mp_cmp_d(const mp_int *a, mp_digit d)
       
  1684 {
       
  1685   ARGCHK(a != NULL, MP_EQ);
       
  1686 
       
  1687   if(SIGN(a) == NEG)
       
  1688     return MP_LT;
       
  1689 
       
  1690   return s_mp_cmp_d(a, d);
       
  1691 
       
  1692 } /* end mp_cmp_d() */
       
  1693 
       
  1694 /* }}} */
       
  1695 
       
  1696 /* {{{ mp_cmp(a, b) */
       
  1697 
       
  1698 int    mp_cmp(const mp_int *a, const mp_int *b)
       
  1699 {
       
  1700   ARGCHK(a != NULL && b != NULL, MP_EQ);
       
  1701 
       
  1702   if(SIGN(a) == SIGN(b)) {
       
  1703     int  mag;
       
  1704 
       
  1705     if((mag = s_mp_cmp(a, b)) == MP_EQ)
       
  1706       return MP_EQ;
       
  1707 
       
  1708     if(SIGN(a) == ZPOS)
       
  1709       return mag;
       
  1710     else
       
  1711       return -mag;
       
  1712 
       
  1713   } else if(SIGN(a) == ZPOS) {
       
  1714     return MP_GT;
       
  1715   } else {
       
  1716     return MP_LT;
       
  1717   }
       
  1718 
       
  1719 } /* end mp_cmp() */
       
  1720 
       
  1721 /* }}} */
       
  1722 
       
  1723 /* {{{ mp_cmp_mag(a, b) */
       
  1724 
       
  1725 /*
       
  1726   mp_cmp_mag(a, b)
       
  1727 
       
  1728   Compares |a| <=> |b|, and returns an appropriate comparison result
       
  1729  */
       
  1730 
       
  1731 int    mp_cmp_mag(mp_int *a, mp_int *b)
       
  1732 {
       
  1733   ARGCHK(a != NULL && b != NULL, MP_EQ);
       
  1734 
       
  1735   return s_mp_cmp(a, b);
       
  1736 
       
  1737 } /* end mp_cmp_mag() */
       
  1738 
       
  1739 /* }}} */
       
  1740 
       
  1741 /* {{{ mp_cmp_int(a, z, kmflag) */
       
  1742 
       
  1743 /*
       
  1744   This just converts z to an mp_int, and uses the existing comparison
       
  1745   routines.  This is sort of inefficient, but it's not clear to me how
       
  1746   frequently this wil get used anyway.  For small positive constants,
       
  1747   you can always use mp_cmp_d(), and for zero, there is mp_cmp_z().
       
  1748  */
       
  1749 int    mp_cmp_int(const mp_int *a, long z, int kmflag)
       
  1750 {
       
  1751   mp_int  tmp;
       
  1752   int     out;
       
  1753 
       
  1754   ARGCHK(a != NULL, MP_EQ);
       
  1755 
       
  1756   mp_init(&tmp, kmflag); mp_set_int(&tmp, z);
       
  1757   out = mp_cmp(a, &tmp);
       
  1758   mp_clear(&tmp);
       
  1759 
       
  1760   return out;
       
  1761 
       
  1762 } /* end mp_cmp_int() */
       
  1763 
       
  1764 /* }}} */
       
  1765 
       
  1766 /* {{{ mp_isodd(a) */
       
  1767 
       
  1768 /*
       
  1769   mp_isodd(a)
       
  1770 
       
  1771   Returns a true (non-zero) value if a is odd, false (zero) otherwise.
       
  1772  */
       
  1773 int    mp_isodd(const mp_int *a)
       
  1774 {
       
  1775   ARGCHK(a != NULL, 0);
       
  1776 
       
  1777   return (int)(DIGIT(a, 0) & 1);
       
  1778 
       
  1779 } /* end mp_isodd() */
       
  1780 
       
  1781 /* }}} */
       
  1782 
       
  1783 /* {{{ mp_iseven(a) */
       
  1784 
       
  1785 int    mp_iseven(const mp_int *a)
       
  1786 {
       
  1787   return !mp_isodd(a);
       
  1788 
       
  1789 } /* end mp_iseven() */
       
  1790 
       
  1791 /* }}} */
       
  1792 
       
  1793 /* }}} */
       
  1794 
       
  1795 /*------------------------------------------------------------------------*/
       
  1796 /* {{{ Number theoretic functions */
       
  1797 
       
  1798 #if MP_NUMTH
       
  1799 /* {{{ mp_gcd(a, b, c) */
       
  1800 
       
  1801 /*
       
  1802   Like the old mp_gcd() function, except computes the GCD using the
       
  1803   binary algorithm due to Josef Stein in 1961 (via Knuth).
       
  1804  */
       
  1805 mp_err mp_gcd(mp_int *a, mp_int *b, mp_int *c)
       
  1806 {
       
  1807   mp_err   res;
       
  1808   mp_int   u, v, t;
       
  1809   mp_size  k = 0;
       
  1810 
       
  1811   ARGCHK(a != NULL && b != NULL && c != NULL, MP_BADARG);
       
  1812 
       
  1813   if(mp_cmp_z(a) == MP_EQ && mp_cmp_z(b) == MP_EQ)
       
  1814       return MP_RANGE;
       
  1815   if(mp_cmp_z(a) == MP_EQ) {
       
  1816     return mp_copy(b, c);
       
  1817   } else if(mp_cmp_z(b) == MP_EQ) {
       
  1818     return mp_copy(a, c);
       
  1819   }
       
  1820 
       
  1821   if((res = mp_init(&t, FLAG(a))) != MP_OKAY)
       
  1822     return res;
       
  1823   if((res = mp_init_copy(&u, a)) != MP_OKAY)
       
  1824     goto U;
       
  1825   if((res = mp_init_copy(&v, b)) != MP_OKAY)
       
  1826     goto V;
       
  1827 
       
  1828   SIGN(&u) = ZPOS;
       
  1829   SIGN(&v) = ZPOS;
       
  1830 
       
  1831   /* Divide out common factors of 2 until at least 1 of a, b is even */
       
  1832   while(mp_iseven(&u) && mp_iseven(&v)) {
       
  1833     s_mp_div_2(&u);
       
  1834     s_mp_div_2(&v);
       
  1835     ++k;
       
  1836   }
       
  1837 
       
  1838   /* Initialize t */
       
  1839   if(mp_isodd(&u)) {
       
  1840     if((res = mp_copy(&v, &t)) != MP_OKAY)
       
  1841       goto CLEANUP;
       
  1842 
       
  1843     /* t = -v */
       
  1844     if(SIGN(&v) == ZPOS)
       
  1845       SIGN(&t) = NEG;
       
  1846     else
       
  1847       SIGN(&t) = ZPOS;
       
  1848 
       
  1849   } else {
       
  1850     if((res = mp_copy(&u, &t)) != MP_OKAY)
       
  1851       goto CLEANUP;
       
  1852 
       
  1853   }
       
  1854 
       
  1855   for(;;) {
       
  1856     while(mp_iseven(&t)) {
       
  1857       s_mp_div_2(&t);
       
  1858     }
       
  1859 
       
  1860     if(mp_cmp_z(&t) == MP_GT) {
       
  1861       if((res = mp_copy(&t, &u)) != MP_OKAY)
       
  1862         goto CLEANUP;
       
  1863 
       
  1864     } else {
       
  1865       if((res = mp_copy(&t, &v)) != MP_OKAY)
       
  1866         goto CLEANUP;
       
  1867 
       
  1868       /* v = -t */
       
  1869       if(SIGN(&t) == ZPOS)
       
  1870         SIGN(&v) = NEG;
       
  1871       else
       
  1872         SIGN(&v) = ZPOS;
       
  1873     }
       
  1874 
       
  1875     if((res = mp_sub(&u, &v, &t)) != MP_OKAY)
       
  1876       goto CLEANUP;
       
  1877 
       
  1878     if(s_mp_cmp_d(&t, 0) == MP_EQ)
       
  1879       break;
       
  1880   }
       
  1881 
       
  1882   s_mp_2expt(&v, k);       /* v = 2^k   */
       
  1883   res = mp_mul(&u, &v, c); /* c = u * v */
       
  1884 
       
  1885  CLEANUP:
       
  1886   mp_clear(&v);
       
  1887  V:
       
  1888   mp_clear(&u);
       
  1889  U:
       
  1890   mp_clear(&t);
       
  1891 
       
  1892   return res;
       
  1893 
       
  1894 } /* end mp_gcd() */
       
  1895 
       
  1896 /* }}} */
       
  1897 
       
  1898 /* {{{ mp_lcm(a, b, c) */
       
  1899 
       
  1900 /* We compute the least common multiple using the rule:
       
  1901 
       
  1902    ab = [a, b](a, b)
       
  1903 
       
  1904    ... by computing the product, and dividing out the gcd.
       
  1905  */
       
  1906 
       
  1907 mp_err mp_lcm(mp_int *a, mp_int *b, mp_int *c)
       
  1908 {
       
  1909   mp_int  gcd, prod;
       
  1910   mp_err  res;
       
  1911 
       
  1912   ARGCHK(a != NULL && b != NULL && c != NULL, MP_BADARG);
       
  1913 
       
  1914   /* Set up temporaries */
       
  1915   if((res = mp_init(&gcd, FLAG(a))) != MP_OKAY)
       
  1916     return res;
       
  1917   if((res = mp_init(&prod, FLAG(a))) != MP_OKAY)
       
  1918     goto GCD;
       
  1919 
       
  1920   if((res = mp_mul(a, b, &prod)) != MP_OKAY)
       
  1921     goto CLEANUP;
       
  1922   if((res = mp_gcd(a, b, &gcd)) != MP_OKAY)
       
  1923     goto CLEANUP;
       
  1924 
       
  1925   res = mp_div(&prod, &gcd, c, NULL);
       
  1926 
       
  1927  CLEANUP:
       
  1928   mp_clear(&prod);
       
  1929  GCD:
       
  1930   mp_clear(&gcd);
       
  1931 
       
  1932   return res;
       
  1933 
       
  1934 } /* end mp_lcm() */
       
  1935 
       
  1936 /* }}} */
       
  1937 
       
  1938 /* {{{ mp_xgcd(a, b, g, x, y) */
       
  1939 
       
  1940 /*
       
  1941   mp_xgcd(a, b, g, x, y)
       
  1942 
       
  1943   Compute g = (a, b) and values x and y satisfying Bezout's identity
       
  1944   (that is, ax + by = g).  This uses the binary extended GCD algorithm
       
  1945   based on the Stein algorithm used for mp_gcd()
       
  1946   See algorithm 14.61 in Handbook of Applied Cryptogrpahy.
       
  1947  */
       
  1948 
       
  1949 mp_err mp_xgcd(const mp_int *a, const mp_int *b, mp_int *g, mp_int *x, mp_int *y)
       
  1950 {
       
  1951   mp_int   gx, xc, yc, u, v, A, B, C, D;
       
  1952   mp_int  *clean[9];
       
  1953   mp_err   res;
       
  1954   int      last = -1;
       
  1955 
       
  1956   if(mp_cmp_z(b) == 0)
       
  1957     return MP_RANGE;
       
  1958 
       
  1959   /* Initialize all these variables we need */
       
  1960   MP_CHECKOK( mp_init(&u, FLAG(a)) );
       
  1961   clean[++last] = &u;
       
  1962   MP_CHECKOK( mp_init(&v, FLAG(a)) );
       
  1963   clean[++last] = &v;
       
  1964   MP_CHECKOK( mp_init(&gx, FLAG(a)) );
       
  1965   clean[++last] = &gx;
       
  1966   MP_CHECKOK( mp_init(&A, FLAG(a)) );
       
  1967   clean[++last] = &A;
       
  1968   MP_CHECKOK( mp_init(&B, FLAG(a)) );
       
  1969   clean[++last] = &B;
       
  1970   MP_CHECKOK( mp_init(&C, FLAG(a)) );
       
  1971   clean[++last] = &C;
       
  1972   MP_CHECKOK( mp_init(&D, FLAG(a)) );
       
  1973   clean[++last] = &D;
       
  1974   MP_CHECKOK( mp_init_copy(&xc, a) );
       
  1975   clean[++last] = &xc;
       
  1976   mp_abs(&xc, &xc);
       
  1977   MP_CHECKOK( mp_init_copy(&yc, b) );
       
  1978   clean[++last] = &yc;
       
  1979   mp_abs(&yc, &yc);
       
  1980 
       
  1981   mp_set(&gx, 1);
       
  1982 
       
  1983   /* Divide by two until at least one of them is odd */
       
  1984   while(mp_iseven(&xc) && mp_iseven(&yc)) {
       
  1985     mp_size nx = mp_trailing_zeros(&xc);
       
  1986     mp_size ny = mp_trailing_zeros(&yc);
       
  1987     mp_size n  = MP_MIN(nx, ny);
       
  1988     s_mp_div_2d(&xc,n);
       
  1989     s_mp_div_2d(&yc,n);
       
  1990     MP_CHECKOK( s_mp_mul_2d(&gx,n) );
       
  1991   }
       
  1992 
       
  1993   mp_copy(&xc, &u);
       
  1994   mp_copy(&yc, &v);
       
  1995   mp_set(&A, 1); mp_set(&D, 1);
       
  1996 
       
  1997   /* Loop through binary GCD algorithm */
       
  1998   do {
       
  1999     while(mp_iseven(&u)) {
       
  2000       s_mp_div_2(&u);
       
  2001 
       
  2002       if(mp_iseven(&A) && mp_iseven(&B)) {
       
  2003         s_mp_div_2(&A); s_mp_div_2(&B);
       
  2004       } else {
       
  2005         MP_CHECKOK( mp_add(&A, &yc, &A) );
       
  2006         s_mp_div_2(&A);
       
  2007         MP_CHECKOK( mp_sub(&B, &xc, &B) );
       
  2008         s_mp_div_2(&B);
       
  2009       }
       
  2010     }
       
  2011 
       
  2012     while(mp_iseven(&v)) {
       
  2013       s_mp_div_2(&v);
       
  2014 
       
  2015       if(mp_iseven(&C) && mp_iseven(&D)) {
       
  2016         s_mp_div_2(&C); s_mp_div_2(&D);
       
  2017       } else {
       
  2018         MP_CHECKOK( mp_add(&C, &yc, &C) );
       
  2019         s_mp_div_2(&C);
       
  2020         MP_CHECKOK( mp_sub(&D, &xc, &D) );
       
  2021         s_mp_div_2(&D);
       
  2022       }
       
  2023     }
       
  2024 
       
  2025     if(mp_cmp(&u, &v) >= 0) {
       
  2026       MP_CHECKOK( mp_sub(&u, &v, &u) );
       
  2027       MP_CHECKOK( mp_sub(&A, &C, &A) );
       
  2028       MP_CHECKOK( mp_sub(&B, &D, &B) );
       
  2029     } else {
       
  2030       MP_CHECKOK( mp_sub(&v, &u, &v) );
       
  2031       MP_CHECKOK( mp_sub(&C, &A, &C) );
       
  2032       MP_CHECKOK( mp_sub(&D, &B, &D) );
       
  2033     }
       
  2034   } while (mp_cmp_z(&u) != 0);
       
  2035 
       
  2036   /* copy results to output */
       
  2037   if(x)
       
  2038     MP_CHECKOK( mp_copy(&C, x) );
       
  2039 
       
  2040   if(y)
       
  2041     MP_CHECKOK( mp_copy(&D, y) );
       
  2042 
       
  2043   if(g)
       
  2044     MP_CHECKOK( mp_mul(&gx, &v, g) );
       
  2045 
       
  2046  CLEANUP:
       
  2047   while(last >= 0)
       
  2048     mp_clear(clean[last--]);
       
  2049 
       
  2050   return res;
       
  2051 
       
  2052 } /* end mp_xgcd() */
       
  2053 
       
  2054 /* }}} */
       
  2055 
       
  2056 mp_size mp_trailing_zeros(const mp_int *mp)
       
  2057 {
       
  2058   mp_digit d;
       
  2059   mp_size  n = 0;
       
  2060   int      ix;
       
  2061 
       
  2062   if (!mp || !MP_DIGITS(mp) || !mp_cmp_z(mp))
       
  2063     return n;
       
  2064 
       
  2065   for (ix = 0; !(d = MP_DIGIT(mp,ix)) && (ix < MP_USED(mp)); ++ix)
       
  2066     n += MP_DIGIT_BIT;
       
  2067   if (!d)
       
  2068     return 0;   /* shouldn't happen, but ... */
       
  2069 #if !defined(MP_USE_UINT_DIGIT)
       
  2070   if (!(d & 0xffffffffU)) {
       
  2071     d >>= 32;
       
  2072     n  += 32;
       
  2073   }
       
  2074 #endif
       
  2075   if (!(d & 0xffffU)) {
       
  2076     d >>= 16;
       
  2077     n  += 16;
       
  2078   }
       
  2079   if (!(d & 0xffU)) {
       
  2080     d >>= 8;
       
  2081     n  += 8;
       
  2082   }
       
  2083   if (!(d & 0xfU)) {
       
  2084     d >>= 4;
       
  2085     n  += 4;
       
  2086   }
       
  2087   if (!(d & 0x3U)) {
       
  2088     d >>= 2;
       
  2089     n  += 2;
       
  2090   }
       
  2091   if (!(d & 0x1U)) {
       
  2092     d >>= 1;
       
  2093     n  += 1;
       
  2094   }
       
  2095 #if MP_ARGCHK == 2
       
  2096   assert(0 != (d & 1));
       
  2097 #endif
       
  2098   return n;
       
  2099 }
       
  2100 
       
  2101 /* Given a and prime p, computes c and k such that a*c == 2**k (mod p).
       
  2102 ** Returns k (positive) or error (negative).
       
  2103 ** This technique from the paper "Fast Modular Reciprocals" (unpublished)
       
  2104 ** by Richard Schroeppel (a.k.a. Captain Nemo).
       
  2105 */
       
  2106 mp_err s_mp_almost_inverse(const mp_int *a, const mp_int *p, mp_int *c)
       
  2107 {
       
  2108   mp_err res;
       
  2109   mp_err k    = 0;
       
  2110   mp_int d, f, g;
       
  2111 
       
  2112   ARGCHK(a && p && c, MP_BADARG);
       
  2113 
       
  2114   MP_DIGITS(&d) = 0;
       
  2115   MP_DIGITS(&f) = 0;
       
  2116   MP_DIGITS(&g) = 0;
       
  2117   MP_CHECKOK( mp_init(&d, FLAG(a)) );
       
  2118   MP_CHECKOK( mp_init_copy(&f, a) );    /* f = a */
       
  2119   MP_CHECKOK( mp_init_copy(&g, p) );    /* g = p */
       
  2120 
       
  2121   mp_set(c, 1);
       
  2122   mp_zero(&d);
       
  2123 
       
  2124   if (mp_cmp_z(&f) == 0) {
       
  2125     res = MP_UNDEF;
       
  2126   } else
       
  2127   for (;;) {
       
  2128     int diff_sign;
       
  2129     while (mp_iseven(&f)) {
       
  2130       mp_size n = mp_trailing_zeros(&f);
       
  2131       if (!n) {
       
  2132         res = MP_UNDEF;
       
  2133         goto CLEANUP;
       
  2134       }
       
  2135       s_mp_div_2d(&f, n);
       
  2136       MP_CHECKOK( s_mp_mul_2d(&d, n) );
       
  2137       k += n;
       
  2138     }
       
  2139     if (mp_cmp_d(&f, 1) == MP_EQ) {     /* f == 1 */
       
  2140       res = k;
       
  2141       break;
       
  2142     }
       
  2143     diff_sign = mp_cmp(&f, &g);
       
  2144     if (diff_sign < 0) {                /* f < g */
       
  2145       s_mp_exch(&f, &g);
       
  2146       s_mp_exch(c, &d);
       
  2147     } else if (diff_sign == 0) {                /* f == g */
       
  2148       res = MP_UNDEF;           /* a and p are not relatively prime */
       
  2149       break;
       
  2150     }
       
  2151     if ((MP_DIGIT(&f,0) % 4) == (MP_DIGIT(&g,0) % 4)) {
       
  2152       MP_CHECKOK( mp_sub(&f, &g, &f) ); /* f = f - g */
       
  2153       MP_CHECKOK( mp_sub(c,  &d,  c) ); /* c = c - d */
       
  2154     } else {
       
  2155       MP_CHECKOK( mp_add(&f, &g, &f) ); /* f = f + g */
       
  2156       MP_CHECKOK( mp_add(c,  &d,  c) ); /* c = c + d */
       
  2157     }
       
  2158   }
       
  2159   if (res >= 0) {
       
  2160     while (MP_SIGN(c) != MP_ZPOS) {
       
  2161       MP_CHECKOK( mp_add(c, p, c) );
       
  2162     }
       
  2163     res = k;
       
  2164   }
       
  2165 
       
  2166 CLEANUP:
       
  2167   mp_clear(&d);
       
  2168   mp_clear(&f);
       
  2169   mp_clear(&g);
       
  2170   return res;
       
  2171 }
       
  2172 
       
  2173 /* Compute T = (P ** -1) mod MP_RADIX.  Also works for 16-bit mp_digits.
       
  2174 ** This technique from the paper "Fast Modular Reciprocals" (unpublished)
       
  2175 ** by Richard Schroeppel (a.k.a. Captain Nemo).
       
  2176 */
       
  2177 mp_digit  s_mp_invmod_radix(mp_digit P)
       
  2178 {
       
  2179   mp_digit T = P;
       
  2180   T *= 2 - (P * T);
       
  2181   T *= 2 - (P * T);
       
  2182   T *= 2 - (P * T);
       
  2183   T *= 2 - (P * T);
       
  2184 #if !defined(MP_USE_UINT_DIGIT)
       
  2185   T *= 2 - (P * T);
       
  2186   T *= 2 - (P * T);
       
  2187 #endif
       
  2188   return T;
       
  2189 }
       
  2190 
       
  2191 /* Given c, k, and prime p, where a*c == 2**k (mod p),
       
  2192 ** Compute x = (a ** -1) mod p.  This is similar to Montgomery reduction.
       
  2193 ** This technique from the paper "Fast Modular Reciprocals" (unpublished)
       
  2194 ** by Richard Schroeppel (a.k.a. Captain Nemo).
       
  2195 */
       
  2196 mp_err  s_mp_fixup_reciprocal(const mp_int *c, const mp_int *p, int k, mp_int *x)
       
  2197 {
       
  2198   int      k_orig = k;
       
  2199   mp_digit r;
       
  2200   mp_size  ix;
       
  2201   mp_err   res;
       
  2202 
       
  2203   if (mp_cmp_z(c) < 0) {                /* c < 0 */
       
  2204     MP_CHECKOK( mp_add(c, p, x) );      /* x = c + p */
       
  2205   } else {
       
  2206     MP_CHECKOK( mp_copy(c, x) );        /* x = c */
       
  2207   }
       
  2208 
       
  2209   /* make sure x is large enough */
       
  2210   ix = MP_HOWMANY(k, MP_DIGIT_BIT) + MP_USED(p) + 1;
       
  2211   ix = MP_MAX(ix, MP_USED(x));
       
  2212   MP_CHECKOK( s_mp_pad(x, ix) );
       
  2213 
       
  2214   r = 0 - s_mp_invmod_radix(MP_DIGIT(p,0));
       
  2215 
       
  2216   for (ix = 0; k > 0; ix++) {
       
  2217     int      j = MP_MIN(k, MP_DIGIT_BIT);
       
  2218     mp_digit v = r * MP_DIGIT(x, ix);
       
  2219     if (j < MP_DIGIT_BIT) {
       
  2220       v &= ((mp_digit)1 << j) - 1;      /* v = v mod (2 ** j) */
       
  2221     }
       
  2222     s_mp_mul_d_add_offset(p, v, x, ix); /* x += p * v * (RADIX ** ix) */
       
  2223     k -= j;
       
  2224   }
       
  2225   s_mp_clamp(x);
       
  2226   s_mp_div_2d(x, k_orig);
       
  2227   res = MP_OKAY;
       
  2228 
       
  2229 CLEANUP:
       
  2230   return res;
       
  2231 }
       
  2232 
       
  2233 /* compute mod inverse using Schroeppel's method, only if m is odd */
       
  2234 mp_err s_mp_invmod_odd_m(const mp_int *a, const mp_int *m, mp_int *c)
       
  2235 {
       
  2236   int k;
       
  2237   mp_err  res;
       
  2238   mp_int  x;
       
  2239 
       
  2240   ARGCHK(a && m && c, MP_BADARG);
       
  2241 
       
  2242   if(mp_cmp_z(a) == 0 || mp_cmp_z(m) == 0)
       
  2243     return MP_RANGE;
       
  2244   if (mp_iseven(m))
       
  2245     return MP_UNDEF;
       
  2246 
       
  2247   MP_DIGITS(&x) = 0;
       
  2248 
       
  2249   if (a == c) {
       
  2250     if ((res = mp_init_copy(&x, a)) != MP_OKAY)
       
  2251       return res;
       
  2252     if (a == m)
       
  2253       m = &x;
       
  2254     a = &x;
       
  2255   } else if (m == c) {
       
  2256     if ((res = mp_init_copy(&x, m)) != MP_OKAY)
       
  2257       return res;
       
  2258     m = &x;
       
  2259   } else {
       
  2260     MP_DIGITS(&x) = 0;
       
  2261   }
       
  2262 
       
  2263   MP_CHECKOK( s_mp_almost_inverse(a, m, c) );
       
  2264   k = res;
       
  2265   MP_CHECKOK( s_mp_fixup_reciprocal(c, m, k, c) );
       
  2266 CLEANUP:
       
  2267   mp_clear(&x);
       
  2268   return res;
       
  2269 }
       
  2270 
       
  2271 /* Known good algorithm for computing modular inverse.  But slow. */
       
  2272 mp_err mp_invmod_xgcd(const mp_int *a, const mp_int *m, mp_int *c)
       
  2273 {
       
  2274   mp_int  g, x;
       
  2275   mp_err  res;
       
  2276 
       
  2277   ARGCHK(a && m && c, MP_BADARG);
       
  2278 
       
  2279   if(mp_cmp_z(a) == 0 || mp_cmp_z(m) == 0)
       
  2280     return MP_RANGE;
       
  2281 
       
  2282   MP_DIGITS(&g) = 0;
       
  2283   MP_DIGITS(&x) = 0;
       
  2284   MP_CHECKOK( mp_init(&x, FLAG(a)) );
       
  2285   MP_CHECKOK( mp_init(&g, FLAG(a)) );
       
  2286 
       
  2287   MP_CHECKOK( mp_xgcd(a, m, &g, &x, NULL) );
       
  2288 
       
  2289   if (mp_cmp_d(&g, 1) != MP_EQ) {
       
  2290     res = MP_UNDEF;
       
  2291     goto CLEANUP;
       
  2292   }
       
  2293 
       
  2294   res = mp_mod(&x, m, c);
       
  2295   SIGN(c) = SIGN(a);
       
  2296 
       
  2297 CLEANUP:
       
  2298   mp_clear(&x);
       
  2299   mp_clear(&g);
       
  2300 
       
  2301   return res;
       
  2302 }
       
  2303 
       
  2304 /* modular inverse where modulus is 2**k. */
       
  2305 /* c = a**-1 mod 2**k */
       
  2306 mp_err s_mp_invmod_2d(const mp_int *a, mp_size k, mp_int *c)
       
  2307 {
       
  2308   mp_err res;
       
  2309   mp_size ix = k + 4;
       
  2310   mp_int t0, t1, val, tmp, two2k;
       
  2311 
       
  2312   static const mp_digit d2 = 2;
       
  2313   static const mp_int two = { 0, MP_ZPOS, 1, 1, (mp_digit *)&d2 };
       
  2314 
       
  2315   if (mp_iseven(a))
       
  2316     return MP_UNDEF;
       
  2317   if (k <= MP_DIGIT_BIT) {
       
  2318     mp_digit i = s_mp_invmod_radix(MP_DIGIT(a,0));
       
  2319     if (k < MP_DIGIT_BIT)
       
  2320       i &= ((mp_digit)1 << k) - (mp_digit)1;
       
  2321     mp_set(c, i);
       
  2322     return MP_OKAY;
       
  2323   }
       
  2324   MP_DIGITS(&t0) = 0;
       
  2325   MP_DIGITS(&t1) = 0;
       
  2326   MP_DIGITS(&val) = 0;
       
  2327   MP_DIGITS(&tmp) = 0;
       
  2328   MP_DIGITS(&two2k) = 0;
       
  2329   MP_CHECKOK( mp_init_copy(&val, a) );
       
  2330   s_mp_mod_2d(&val, k);
       
  2331   MP_CHECKOK( mp_init_copy(&t0, &val) );
       
  2332   MP_CHECKOK( mp_init_copy(&t1, &t0)  );
       
  2333   MP_CHECKOK( mp_init(&tmp, FLAG(a)) );
       
  2334   MP_CHECKOK( mp_init(&two2k, FLAG(a)) );
       
  2335   MP_CHECKOK( s_mp_2expt(&two2k, k) );
       
  2336   do {
       
  2337     MP_CHECKOK( mp_mul(&val, &t1, &tmp)  );
       
  2338     MP_CHECKOK( mp_sub(&two, &tmp, &tmp) );
       
  2339     MP_CHECKOK( mp_mul(&t1, &tmp, &t1)   );
       
  2340     s_mp_mod_2d(&t1, k);
       
  2341     while (MP_SIGN(&t1) != MP_ZPOS) {
       
  2342       MP_CHECKOK( mp_add(&t1, &two2k, &t1) );
       
  2343     }
       
  2344     if (mp_cmp(&t1, &t0) == MP_EQ)
       
  2345       break;
       
  2346     MP_CHECKOK( mp_copy(&t1, &t0) );
       
  2347   } while (--ix > 0);
       
  2348   if (!ix) {
       
  2349     res = MP_UNDEF;
       
  2350   } else {
       
  2351     mp_exch(c, &t1);
       
  2352   }
       
  2353 
       
  2354 CLEANUP:
       
  2355   mp_clear(&t0);
       
  2356   mp_clear(&t1);
       
  2357   mp_clear(&val);
       
  2358   mp_clear(&tmp);
       
  2359   mp_clear(&two2k);
       
  2360   return res;
       
  2361 }
       
  2362 
       
  2363 mp_err s_mp_invmod_even_m(const mp_int *a, const mp_int *m, mp_int *c)
       
  2364 {
       
  2365   mp_err res;
       
  2366   mp_size k;
       
  2367   mp_int oddFactor, evenFactor; /* factors of the modulus */
       
  2368   mp_int oddPart, evenPart;     /* parts to combine via CRT. */
       
  2369   mp_int C2, tmp1, tmp2;
       
  2370 
       
  2371   /*static const mp_digit d1 = 1; */
       
  2372   /*static const mp_int one = { MP_ZPOS, 1, 1, (mp_digit *)&d1 }; */
       
  2373 
       
  2374   if ((res = s_mp_ispow2(m)) >= 0) {
       
  2375     k = res;
       
  2376     return s_mp_invmod_2d(a, k, c);
       
  2377   }
       
  2378   MP_DIGITS(&oddFactor) = 0;
       
  2379   MP_DIGITS(&evenFactor) = 0;
       
  2380   MP_DIGITS(&oddPart) = 0;
       
  2381   MP_DIGITS(&evenPart) = 0;
       
  2382   MP_DIGITS(&C2)     = 0;
       
  2383   MP_DIGITS(&tmp1)   = 0;
       
  2384   MP_DIGITS(&tmp2)   = 0;
       
  2385 
       
  2386   MP_CHECKOK( mp_init_copy(&oddFactor, m) );    /* oddFactor = m */
       
  2387   MP_CHECKOK( mp_init(&evenFactor, FLAG(m)) );
       
  2388   MP_CHECKOK( mp_init(&oddPart, FLAG(m)) );
       
  2389   MP_CHECKOK( mp_init(&evenPart, FLAG(m)) );
       
  2390   MP_CHECKOK( mp_init(&C2, FLAG(m))     );
       
  2391   MP_CHECKOK( mp_init(&tmp1, FLAG(m))   );
       
  2392   MP_CHECKOK( mp_init(&tmp2, FLAG(m))   );
       
  2393 
       
  2394   k = mp_trailing_zeros(m);
       
  2395   s_mp_div_2d(&oddFactor, k);
       
  2396   MP_CHECKOK( s_mp_2expt(&evenFactor, k) );
       
  2397 
       
  2398   /* compute a**-1 mod oddFactor. */
       
  2399   MP_CHECKOK( s_mp_invmod_odd_m(a, &oddFactor, &oddPart) );
       
  2400   /* compute a**-1 mod evenFactor, where evenFactor == 2**k. */
       
  2401   MP_CHECKOK( s_mp_invmod_2d(   a,       k,    &evenPart) );
       
  2402 
       
  2403   /* Use Chinese Remainer theorem to compute a**-1 mod m. */
       
  2404   /* let m1 = oddFactor,  v1 = oddPart,
       
  2405    * let m2 = evenFactor, v2 = evenPart.
       
  2406    */
       
  2407 
       
  2408   /* Compute C2 = m1**-1 mod m2. */
       
  2409   MP_CHECKOK( s_mp_invmod_2d(&oddFactor, k,    &C2) );
       
  2410 
       
  2411   /* compute u = (v2 - v1)*C2 mod m2 */
       
  2412   MP_CHECKOK( mp_sub(&evenPart, &oddPart,   &tmp1) );
       
  2413   MP_CHECKOK( mp_mul(&tmp1,     &C2,        &tmp2) );
       
  2414   s_mp_mod_2d(&tmp2, k);
       
  2415   while (MP_SIGN(&tmp2) != MP_ZPOS) {
       
  2416     MP_CHECKOK( mp_add(&tmp2, &evenFactor, &tmp2) );
       
  2417   }
       
  2418 
       
  2419   /* compute answer = v1 + u*m1 */
       
  2420   MP_CHECKOK( mp_mul(&tmp2,     &oddFactor, c) );
       
  2421   MP_CHECKOK( mp_add(&oddPart,  c,          c) );
       
  2422   /* not sure this is necessary, but it's low cost if not. */
       
  2423   MP_CHECKOK( mp_mod(c,         m,          c) );
       
  2424 
       
  2425 CLEANUP:
       
  2426   mp_clear(&oddFactor);
       
  2427   mp_clear(&evenFactor);
       
  2428   mp_clear(&oddPart);
       
  2429   mp_clear(&evenPart);
       
  2430   mp_clear(&C2);
       
  2431   mp_clear(&tmp1);
       
  2432   mp_clear(&tmp2);
       
  2433   return res;
       
  2434 }
       
  2435 
       
  2436 
       
  2437 /* {{{ mp_invmod(a, m, c) */
       
  2438 
       
  2439 /*
       
  2440   mp_invmod(a, m, c)
       
  2441 
       
  2442   Compute c = a^-1 (mod m), if there is an inverse for a (mod m).
       
  2443   This is equivalent to the question of whether (a, m) = 1.  If not,
       
  2444   MP_UNDEF is returned, and there is no inverse.
       
  2445  */
       
  2446 
       
  2447 mp_err mp_invmod(const mp_int *a, const mp_int *m, mp_int *c)
       
  2448 {
       
  2449 
       
  2450   ARGCHK(a && m && c, MP_BADARG);
       
  2451 
       
  2452   if(mp_cmp_z(a) == 0 || mp_cmp_z(m) == 0)
       
  2453     return MP_RANGE;
       
  2454 
       
  2455   if (mp_isodd(m)) {
       
  2456     return s_mp_invmod_odd_m(a, m, c);
       
  2457   }
       
  2458   if (mp_iseven(a))
       
  2459     return MP_UNDEF;    /* not invertable */
       
  2460 
       
  2461   return s_mp_invmod_even_m(a, m, c);
       
  2462 
       
  2463 } /* end mp_invmod() */
       
  2464 
       
  2465 /* }}} */
       
  2466 #endif /* if MP_NUMTH */
       
  2467 
       
  2468 /* }}} */
       
  2469 
       
  2470 /*------------------------------------------------------------------------*/
       
  2471 /* {{{ mp_print(mp, ofp) */
       
  2472 
       
  2473 #if MP_IOFUNC
       
  2474 /*
       
  2475   mp_print(mp, ofp)
       
  2476 
       
  2477   Print a textual representation of the given mp_int on the output
       
  2478   stream 'ofp'.  Output is generated using the internal radix.
       
  2479  */
       
  2480 
       
  2481 void   mp_print(mp_int *mp, FILE *ofp)
       
  2482 {
       
  2483   int   ix;
       
  2484 
       
  2485   if(mp == NULL || ofp == NULL)
       
  2486     return;
       
  2487 
       
  2488   fputc((SIGN(mp) == NEG) ? '-' : '+', ofp);
       
  2489 
       
  2490   for(ix = USED(mp) - 1; ix >= 0; ix--) {
       
  2491     fprintf(ofp, DIGIT_FMT, DIGIT(mp, ix));
       
  2492   }
       
  2493 
       
  2494 } /* end mp_print() */
       
  2495 
       
  2496 #endif /* if MP_IOFUNC */
       
  2497 
       
  2498 /* }}} */
       
  2499 
       
  2500 /*------------------------------------------------------------------------*/
       
  2501 /* {{{ More I/O Functions */
       
  2502 
       
  2503 /* {{{ mp_read_raw(mp, str, len) */
       
  2504 
       
  2505 /*
       
  2506    mp_read_raw(mp, str, len)
       
  2507 
       
  2508    Read in a raw value (base 256) into the given mp_int
       
  2509  */
       
  2510 
       
  2511 mp_err  mp_read_raw(mp_int *mp, char *str, int len)
       
  2512 {
       
  2513   int            ix;
       
  2514   mp_err         res;
       
  2515   unsigned char *ustr = (unsigned char *)str;
       
  2516 
       
  2517   ARGCHK(mp != NULL && str != NULL && len > 0, MP_BADARG);
       
  2518 
       
  2519   mp_zero(mp);
       
  2520 
       
  2521   /* Get sign from first byte */
       
  2522   if(ustr[0])
       
  2523     SIGN(mp) = NEG;
       
  2524   else
       
  2525     SIGN(mp) = ZPOS;
       
  2526 
       
  2527   /* Read the rest of the digits */
       
  2528   for(ix = 1; ix < len; ix++) {
       
  2529     if((res = mp_mul_d(mp, 256, mp)) != MP_OKAY)
       
  2530       return res;
       
  2531     if((res = mp_add_d(mp, ustr[ix], mp)) != MP_OKAY)
       
  2532       return res;
       
  2533   }
       
  2534 
       
  2535   return MP_OKAY;
       
  2536 
       
  2537 } /* end mp_read_raw() */
       
  2538 
       
  2539 /* }}} */
       
  2540 
       
  2541 /* {{{ mp_raw_size(mp) */
       
  2542 
       
  2543 int    mp_raw_size(mp_int *mp)
       
  2544 {
       
  2545   ARGCHK(mp != NULL, 0);
       
  2546 
       
  2547   return (USED(mp) * sizeof(mp_digit)) + 1;
       
  2548 
       
  2549 } /* end mp_raw_size() */
       
  2550 
       
  2551 /* }}} */
       
  2552 
       
  2553 /* {{{ mp_toraw(mp, str) */
       
  2554 
       
  2555 mp_err mp_toraw(mp_int *mp, char *str)
       
  2556 {
       
  2557   int  ix, jx, pos = 1;
       
  2558 
       
  2559   ARGCHK(mp != NULL && str != NULL, MP_BADARG);
       
  2560 
       
  2561   str[0] = (char)SIGN(mp);
       
  2562 
       
  2563   /* Iterate over each digit... */
       
  2564   for(ix = USED(mp) - 1; ix >= 0; ix--) {
       
  2565     mp_digit  d = DIGIT(mp, ix);
       
  2566 
       
  2567     /* Unpack digit bytes, high order first */
       
  2568     for(jx = sizeof(mp_digit) - 1; jx >= 0; jx--) {
       
  2569       str[pos++] = (char)(d >> (jx * CHAR_BIT));
       
  2570     }
       
  2571   }
       
  2572 
       
  2573   return MP_OKAY;
       
  2574 
       
  2575 } /* end mp_toraw() */
       
  2576 
       
  2577 /* }}} */
       
  2578 
       
  2579 /* {{{ mp_read_radix(mp, str, radix) */
       
  2580 
       
  2581 /*
       
  2582   mp_read_radix(mp, str, radix)
       
  2583 
       
  2584   Read an integer from the given string, and set mp to the resulting
       
  2585   value.  The input is presumed to be in base 10.  Leading non-digit
       
  2586   characters are ignored, and the function reads until a non-digit
       
  2587   character or the end of the string.
       
  2588  */
       
  2589 
       
  2590 mp_err  mp_read_radix(mp_int *mp, const char *str, int radix)
       
  2591 {
       
  2592   int     ix = 0, val = 0;
       
  2593   mp_err  res;
       
  2594   mp_sign sig = ZPOS;
       
  2595 
       
  2596   ARGCHK(mp != NULL && str != NULL && radix >= 2 && radix <= MAX_RADIX,
       
  2597          MP_BADARG);
       
  2598 
       
  2599   mp_zero(mp);
       
  2600 
       
  2601   /* Skip leading non-digit characters until a digit or '-' or '+' */
       
  2602   while(str[ix] &&
       
  2603         (s_mp_tovalue(str[ix], radix) < 0) &&
       
  2604         str[ix] != '-' &&
       
  2605         str[ix] != '+') {
       
  2606     ++ix;
       
  2607   }
       
  2608 
       
  2609   if(str[ix] == '-') {
       
  2610     sig = NEG;
       
  2611     ++ix;
       
  2612   } else if(str[ix] == '+') {
       
  2613     sig = ZPOS; /* this is the default anyway... */
       
  2614     ++ix;
       
  2615   }
       
  2616 
       
  2617   while((val = s_mp_tovalue(str[ix], radix)) >= 0) {
       
  2618     if((res = s_mp_mul_d(mp, radix)) != MP_OKAY)
       
  2619       return res;
       
  2620     if((res = s_mp_add_d(mp, val)) != MP_OKAY)
       
  2621       return res;
       
  2622     ++ix;
       
  2623   }
       
  2624 
       
  2625   if(s_mp_cmp_d(mp, 0) == MP_EQ)
       
  2626     SIGN(mp) = ZPOS;
       
  2627   else
       
  2628     SIGN(mp) = sig;
       
  2629 
       
  2630   return MP_OKAY;
       
  2631 
       
  2632 } /* end mp_read_radix() */
       
  2633 
       
  2634 mp_err mp_read_variable_radix(mp_int *a, const char * str, int default_radix)
       
  2635 {
       
  2636   int     radix = default_radix;
       
  2637   int     cx;
       
  2638   mp_sign sig   = ZPOS;
       
  2639   mp_err  res;
       
  2640 
       
  2641   /* Skip leading non-digit characters until a digit or '-' or '+' */
       
  2642   while ((cx = *str) != 0 &&
       
  2643         (s_mp_tovalue(cx, radix) < 0) &&
       
  2644         cx != '-' &&
       
  2645         cx != '+') {
       
  2646     ++str;
       
  2647   }
       
  2648 
       
  2649   if (cx == '-') {
       
  2650     sig = NEG;
       
  2651     ++str;
       
  2652   } else if (cx == '+') {
       
  2653     sig = ZPOS; /* this is the default anyway... */
       
  2654     ++str;
       
  2655   }
       
  2656 
       
  2657   if (str[0] == '0') {
       
  2658     if ((str[1] | 0x20) == 'x') {
       
  2659       radix = 16;
       
  2660       str += 2;
       
  2661     } else {
       
  2662       radix = 8;
       
  2663       str++;
       
  2664     }
       
  2665   }
       
  2666   res = mp_read_radix(a, str, radix);
       
  2667   if (res == MP_OKAY) {
       
  2668     MP_SIGN(a) = (s_mp_cmp_d(a, 0) == MP_EQ) ? ZPOS : sig;
       
  2669   }
       
  2670   return res;
       
  2671 }
       
  2672 
       
  2673 /* }}} */
       
  2674 
       
  2675 /* {{{ mp_radix_size(mp, radix) */
       
  2676 
       
  2677 int    mp_radix_size(mp_int *mp, int radix)
       
  2678 {
       
  2679   int  bits;
       
  2680 
       
  2681   if(!mp || radix < 2 || radix > MAX_RADIX)
       
  2682     return 0;
       
  2683 
       
  2684   bits = USED(mp) * DIGIT_BIT - 1;
       
  2685 
       
  2686   return s_mp_outlen(bits, radix);
       
  2687 
       
  2688 } /* end mp_radix_size() */
       
  2689 
       
  2690 /* }}} */
       
  2691 
       
  2692 /* {{{ mp_toradix(mp, str, radix) */
       
  2693 
       
  2694 mp_err mp_toradix(mp_int *mp, char *str, int radix)
       
  2695 {
       
  2696   int  ix, pos = 0;
       
  2697 
       
  2698   ARGCHK(mp != NULL && str != NULL, MP_BADARG);
       
  2699   ARGCHK(radix > 1 && radix <= MAX_RADIX, MP_RANGE);
       
  2700 
       
  2701   if(mp_cmp_z(mp) == MP_EQ) {
       
  2702     str[0] = '0';
       
  2703     str[1] = '\0';
       
  2704   } else {
       
  2705     mp_err   res;
       
  2706     mp_int   tmp;
       
  2707     mp_sign  sgn;
       
  2708     mp_digit rem, rdx = (mp_digit)radix;
       
  2709     char     ch;
       
  2710 
       
  2711     if((res = mp_init_copy(&tmp, mp)) != MP_OKAY)
       
  2712       return res;
       
  2713 
       
  2714     /* Save sign for later, and take absolute value */
       
  2715     sgn = SIGN(&tmp); SIGN(&tmp) = ZPOS;
       
  2716 
       
  2717     /* Generate output digits in reverse order      */
       
  2718     while(mp_cmp_z(&tmp) != 0) {
       
  2719       if((res = mp_div_d(&tmp, rdx, &tmp, &rem)) != MP_OKAY) {
       
  2720         mp_clear(&tmp);
       
  2721         return res;
       
  2722       }
       
  2723 
       
  2724       /* Generate digits, use capital letters */
       
  2725       ch = s_mp_todigit(rem, radix, 0);
       
  2726 
       
  2727       str[pos++] = ch;
       
  2728     }
       
  2729 
       
  2730     /* Add - sign if original value was negative */
       
  2731     if(sgn == NEG)
       
  2732       str[pos++] = '-';
       
  2733 
       
  2734     /* Add trailing NUL to end the string        */
       
  2735     str[pos--] = '\0';
       
  2736 
       
  2737     /* Reverse the digits and sign indicator     */
       
  2738     ix = 0;
       
  2739     while(ix < pos) {
       
  2740       char tmp = str[ix];
       
  2741 
       
  2742       str[ix] = str[pos];
       
  2743       str[pos] = tmp;
       
  2744       ++ix;
       
  2745       --pos;
       
  2746     }
       
  2747 
       
  2748     mp_clear(&tmp);
       
  2749   }
       
  2750 
       
  2751   return MP_OKAY;
       
  2752 
       
  2753 } /* end mp_toradix() */
       
  2754 
       
  2755 /* }}} */
       
  2756 
       
  2757 /* {{{ mp_tovalue(ch, r) */
       
  2758 
       
  2759 int    mp_tovalue(char ch, int r)
       
  2760 {
       
  2761   return s_mp_tovalue(ch, r);
       
  2762 
       
  2763 } /* end mp_tovalue() */
       
  2764 
       
  2765 /* }}} */
       
  2766 
       
  2767 /* }}} */
       
  2768 
       
  2769 /* {{{ mp_strerror(ec) */
       
  2770 
       
  2771 /*
       
  2772   mp_strerror(ec)
       
  2773 
       
  2774   Return a string describing the meaning of error code 'ec'.  The
       
  2775   string returned is allocated in static memory, so the caller should
       
  2776   not attempt to modify or free the memory associated with this
       
  2777   string.
       
  2778  */
       
  2779 const char  *mp_strerror(mp_err ec)
       
  2780 {
       
  2781   int   aec = (ec < 0) ? -ec : ec;
       
  2782 
       
  2783   /* Code values are negative, so the senses of these comparisons
       
  2784      are accurate */
       
  2785   if(ec < MP_LAST_CODE || ec > MP_OKAY) {
       
  2786     return mp_err_string[0];  /* unknown error code */
       
  2787   } else {
       
  2788     return mp_err_string[aec + 1];
       
  2789   }
       
  2790 
       
  2791 } /* end mp_strerror() */
       
  2792 
       
  2793 /* }}} */
       
  2794 
       
  2795 /*========================================================================*/
       
  2796 /*------------------------------------------------------------------------*/
       
  2797 /* Static function definitions (internal use only)                        */
       
  2798 
       
  2799 /* {{{ Memory management */
       
  2800 
       
  2801 /* {{{ s_mp_grow(mp, min) */
       
  2802 
       
  2803 /* Make sure there are at least 'min' digits allocated to mp              */
       
  2804 mp_err   s_mp_grow(mp_int *mp, mp_size min)
       
  2805 {
       
  2806   if(min > ALLOC(mp)) {
       
  2807     mp_digit   *tmp;
       
  2808 
       
  2809     /* Set min to next nearest default precision block size */
       
  2810     min = MP_ROUNDUP(min, s_mp_defprec);
       
  2811 
       
  2812     if((tmp = s_mp_alloc(min, sizeof(mp_digit), FLAG(mp))) == NULL)
       
  2813       return MP_MEM;
       
  2814 
       
  2815     s_mp_copy(DIGITS(mp), tmp, USED(mp));
       
  2816 
       
  2817 #if MP_CRYPTO
       
  2818     s_mp_setz(DIGITS(mp), ALLOC(mp));
       
  2819 #endif
       
  2820     s_mp_free(DIGITS(mp), ALLOC(mp));
       
  2821     DIGITS(mp) = tmp;
       
  2822     ALLOC(mp) = min;
       
  2823   }
       
  2824 
       
  2825   return MP_OKAY;
       
  2826 
       
  2827 } /* end s_mp_grow() */
       
  2828 
       
  2829 /* }}} */
       
  2830 
       
  2831 /* {{{ s_mp_pad(mp, min) */
       
  2832 
       
  2833 /* Make sure the used size of mp is at least 'min', growing if needed     */
       
  2834 mp_err   s_mp_pad(mp_int *mp, mp_size min)
       
  2835 {
       
  2836   if(min > USED(mp)) {
       
  2837     mp_err  res;
       
  2838 
       
  2839     /* Make sure there is room to increase precision  */
       
  2840     if (min > ALLOC(mp)) {
       
  2841       if ((res = s_mp_grow(mp, min)) != MP_OKAY)
       
  2842         return res;
       
  2843     } else {
       
  2844       s_mp_setz(DIGITS(mp) + USED(mp), min - USED(mp));
       
  2845     }
       
  2846 
       
  2847     /* Increase precision; should already be 0-filled */
       
  2848     USED(mp) = min;
       
  2849   }
       
  2850 
       
  2851   return MP_OKAY;
       
  2852 
       
  2853 } /* end s_mp_pad() */
       
  2854 
       
  2855 /* }}} */
       
  2856 
       
  2857 /* {{{ s_mp_setz(dp, count) */
       
  2858 
       
  2859 #if MP_MACRO == 0
       
  2860 /* Set 'count' digits pointed to by dp to be zeroes                       */
       
  2861 void s_mp_setz(mp_digit *dp, mp_size count)
       
  2862 {
       
  2863 #if MP_MEMSET == 0
       
  2864   int  ix;
       
  2865 
       
  2866   for(ix = 0; ix < count; ix++)
       
  2867     dp[ix] = 0;
       
  2868 #else
       
  2869   memset(dp, 0, count * sizeof(mp_digit));
       
  2870 #endif
       
  2871 
       
  2872 } /* end s_mp_setz() */
       
  2873 #endif
       
  2874 
       
  2875 /* }}} */
       
  2876 
       
  2877 /* {{{ s_mp_copy(sp, dp, count) */
       
  2878 
       
  2879 #if MP_MACRO == 0
       
  2880 /* Copy 'count' digits from sp to dp                                      */
       
  2881 void s_mp_copy(const mp_digit *sp, mp_digit *dp, mp_size count)
       
  2882 {
       
  2883 #if MP_MEMCPY == 0
       
  2884   int  ix;
       
  2885 
       
  2886   for(ix = 0; ix < count; ix++)
       
  2887     dp[ix] = sp[ix];
       
  2888 #else
       
  2889   memcpy(dp, sp, count * sizeof(mp_digit));
       
  2890 #endif
       
  2891 
       
  2892 } /* end s_mp_copy() */
       
  2893 #endif
       
  2894 
       
  2895 /* }}} */
       
  2896 
       
  2897 /* {{{ s_mp_alloc(nb, ni, kmflag) */
       
  2898 
       
  2899 #if MP_MACRO == 0
       
  2900 /* Allocate ni records of nb bytes each, and return a pointer to that     */
       
  2901 void    *s_mp_alloc(size_t nb, size_t ni, int kmflag)
       
  2902 {
       
  2903   mp_int *mp;
       
  2904   ++mp_allocs;
       
  2905 #ifdef _KERNEL
       
  2906   mp = kmem_zalloc(nb * ni, kmflag);
       
  2907   if (mp != NULL)
       
  2908     FLAG(mp) = kmflag;
       
  2909   return (mp);
       
  2910 #else
       
  2911   return calloc(nb, ni);
       
  2912 #endif
       
  2913 
       
  2914 } /* end s_mp_alloc() */
       
  2915 #endif
       
  2916 
       
  2917 /* }}} */
       
  2918 
       
  2919 /* {{{ s_mp_free(ptr) */
       
  2920 
       
  2921 #if MP_MACRO == 0
       
  2922 /* Free the memory pointed to by ptr                                      */
       
  2923 void     s_mp_free(void *ptr, mp_size alloc)
       
  2924 {
       
  2925   if(ptr) {
       
  2926     ++mp_frees;
       
  2927 #ifdef _KERNEL
       
  2928     kmem_free(ptr, alloc * sizeof (mp_digit));
       
  2929 #else
       
  2930     free(ptr);
       
  2931 #endif
       
  2932   }
       
  2933 } /* end s_mp_free() */
       
  2934 #endif
       
  2935 
       
  2936 /* }}} */
       
  2937 
       
  2938 /* {{{ s_mp_clamp(mp) */
       
  2939 
       
  2940 #if MP_MACRO == 0
       
  2941 /* Remove leading zeroes from the given value                             */
       
  2942 void     s_mp_clamp(mp_int *mp)
       
  2943 {
       
  2944   mp_size used = MP_USED(mp);
       
  2945   while (used > 1 && DIGIT(mp, used - 1) == 0)
       
  2946     --used;
       
  2947   MP_USED(mp) = used;
       
  2948 } /* end s_mp_clamp() */
       
  2949 #endif
       
  2950 
       
  2951 /* }}} */
       
  2952 
       
  2953 /* {{{ s_mp_exch(a, b) */
       
  2954 
       
  2955 /* Exchange the data for a and b; (b, a) = (a, b)                         */
       
  2956 void     s_mp_exch(mp_int *a, mp_int *b)
       
  2957 {
       
  2958   mp_int   tmp;
       
  2959 
       
  2960   tmp = *a;
       
  2961   *a = *b;
       
  2962   *b = tmp;
       
  2963 
       
  2964 } /* end s_mp_exch() */
       
  2965 
       
  2966 /* }}} */
       
  2967 
       
  2968 /* }}} */
       
  2969 
       
  2970 /* {{{ Arithmetic helpers */
       
  2971 
       
  2972 /* {{{ s_mp_lshd(mp, p) */
       
  2973 
       
  2974 /*
       
  2975    Shift mp leftward by p digits, growing if needed, and zero-filling
       
  2976    the in-shifted digits at the right end.  This is a convenient
       
  2977    alternative to multiplication by powers of the radix
       
  2978    The value of USED(mp) must already have been set to the value for
       
  2979    the shifted result.
       
  2980  */
       
  2981 
       
  2982 mp_err   s_mp_lshd(mp_int *mp, mp_size p)
       
  2983 {
       
  2984   mp_err  res;
       
  2985   mp_size pos;
       
  2986   int     ix;
       
  2987 
       
  2988   if(p == 0)
       
  2989     return MP_OKAY;
       
  2990 
       
  2991   if (MP_USED(mp) == 1 && MP_DIGIT(mp, 0) == 0)
       
  2992     return MP_OKAY;
       
  2993 
       
  2994   if((res = s_mp_pad(mp, USED(mp) + p)) != MP_OKAY)
       
  2995     return res;
       
  2996 
       
  2997   pos = USED(mp) - 1;
       
  2998 
       
  2999   /* Shift all the significant figures over as needed */
       
  3000   for(ix = pos - p; ix >= 0; ix--)
       
  3001     DIGIT(mp, ix + p) = DIGIT(mp, ix);
       
  3002 
       
  3003   /* Fill the bottom digits with zeroes */
       
  3004   for(ix = 0; ix < p; ix++)
       
  3005     DIGIT(mp, ix) = 0;
       
  3006 
       
  3007   return MP_OKAY;
       
  3008 
       
  3009 } /* end s_mp_lshd() */
       
  3010 
       
  3011 /* }}} */
       
  3012 
       
  3013 /* {{{ s_mp_mul_2d(mp, d) */
       
  3014 
       
  3015 /*
       
  3016   Multiply the integer by 2^d, where d is a number of bits.  This
       
  3017   amounts to a bitwise shift of the value.
       
  3018  */
       
  3019 mp_err   s_mp_mul_2d(mp_int *mp, mp_digit d)
       
  3020 {
       
  3021   mp_err   res;
       
  3022   mp_digit dshift, bshift;
       
  3023   mp_digit mask;
       
  3024 
       
  3025   ARGCHK(mp != NULL,  MP_BADARG);
       
  3026 
       
  3027   dshift = d / MP_DIGIT_BIT;
       
  3028   bshift = d % MP_DIGIT_BIT;
       
  3029   /* bits to be shifted out of the top word */
       
  3030   mask   = ((mp_digit)~0 << (MP_DIGIT_BIT - bshift));
       
  3031   mask  &= MP_DIGIT(mp, MP_USED(mp) - 1);
       
  3032 
       
  3033   if (MP_OKAY != (res = s_mp_pad(mp, MP_USED(mp) + dshift + (mask != 0) )))
       
  3034     return res;
       
  3035 
       
  3036   if (dshift && MP_OKAY != (res = s_mp_lshd(mp, dshift)))
       
  3037     return res;
       
  3038 
       
  3039   if (bshift) {
       
  3040     mp_digit *pa = MP_DIGITS(mp);
       
  3041     mp_digit *alim = pa + MP_USED(mp);
       
  3042     mp_digit  prev = 0;
       
  3043 
       
  3044     for (pa += dshift; pa < alim; ) {
       
  3045       mp_digit x = *pa;
       
  3046       *pa++ = (x << bshift) | prev;
       
  3047       prev = x >> (DIGIT_BIT - bshift);
       
  3048     }
       
  3049   }
       
  3050 
       
  3051   s_mp_clamp(mp);
       
  3052   return MP_OKAY;
       
  3053 } /* end s_mp_mul_2d() */
       
  3054 
       
  3055 /* {{{ s_mp_rshd(mp, p) */
       
  3056 
       
  3057 /*
       
  3058    Shift mp rightward by p digits.  Maintains the invariant that
       
  3059    digits above the precision are all zero.  Digits shifted off the
       
  3060    end are lost.  Cannot fail.
       
  3061  */
       
  3062 
       
  3063 void     s_mp_rshd(mp_int *mp, mp_size p)
       
  3064 {
       
  3065   mp_size  ix;
       
  3066   mp_digit *src, *dst;
       
  3067 
       
  3068   if(p == 0)
       
  3069     return;
       
  3070 
       
  3071   /* Shortcut when all digits are to be shifted off */
       
  3072   if(p >= USED(mp)) {
       
  3073     s_mp_setz(DIGITS(mp), ALLOC(mp));
       
  3074     USED(mp) = 1;
       
  3075     SIGN(mp) = ZPOS;
       
  3076     return;
       
  3077   }
       
  3078 
       
  3079   /* Shift all the significant figures over as needed */
       
  3080   dst = MP_DIGITS(mp);
       
  3081   src = dst + p;
       
  3082   for (ix = USED(mp) - p; ix > 0; ix--)
       
  3083     *dst++ = *src++;
       
  3084 
       
  3085   MP_USED(mp) -= p;
       
  3086   /* Fill the top digits with zeroes */
       
  3087   while (p-- > 0)
       
  3088     *dst++ = 0;
       
  3089 
       
  3090 #if 0
       
  3091   /* Strip off any leading zeroes    */
       
  3092   s_mp_clamp(mp);
       
  3093 #endif
       
  3094 
       
  3095 } /* end s_mp_rshd() */
       
  3096 
       
  3097 /* }}} */
       
  3098 
       
  3099 /* {{{ s_mp_div_2(mp) */
       
  3100 
       
  3101 /* Divide by two -- take advantage of radix properties to do it fast      */
       
  3102 void     s_mp_div_2(mp_int *mp)
       
  3103 {
       
  3104   s_mp_div_2d(mp, 1);
       
  3105 
       
  3106 } /* end s_mp_div_2() */
       
  3107 
       
  3108 /* }}} */
       
  3109 
       
  3110 /* {{{ s_mp_mul_2(mp) */
       
  3111 
       
  3112 mp_err s_mp_mul_2(mp_int *mp)
       
  3113 {
       
  3114   mp_digit *pd;
       
  3115   int      ix, used;
       
  3116   mp_digit kin = 0;
       
  3117 
       
  3118   /* Shift digits leftward by 1 bit */
       
  3119   used = MP_USED(mp);
       
  3120   pd = MP_DIGITS(mp);
       
  3121   for (ix = 0; ix < used; ix++) {
       
  3122     mp_digit d = *pd;
       
  3123     *pd++ = (d << 1) | kin;
       
  3124     kin = (d >> (DIGIT_BIT - 1));
       
  3125   }
       
  3126 
       
  3127   /* Deal with rollover from last digit */
       
  3128   if (kin) {
       
  3129     if (ix >= ALLOC(mp)) {
       
  3130       mp_err res;
       
  3131       if((res = s_mp_grow(mp, ALLOC(mp) + 1)) != MP_OKAY)
       
  3132         return res;
       
  3133     }
       
  3134 
       
  3135     DIGIT(mp, ix) = kin;
       
  3136     USED(mp) += 1;
       
  3137   }
       
  3138 
       
  3139   return MP_OKAY;
       
  3140 
       
  3141 } /* end s_mp_mul_2() */
       
  3142 
       
  3143 /* }}} */
       
  3144 
       
  3145 /* {{{ s_mp_mod_2d(mp, d) */
       
  3146 
       
  3147 /*
       
  3148   Remainder the integer by 2^d, where d is a number of bits.  This
       
  3149   amounts to a bitwise AND of the value, and does not require the full
       
  3150   division code
       
  3151  */
       
  3152 void     s_mp_mod_2d(mp_int *mp, mp_digit d)
       
  3153 {
       
  3154   mp_size  ndig = (d / DIGIT_BIT), nbit = (d % DIGIT_BIT);
       
  3155   mp_size  ix;
       
  3156   mp_digit dmask;
       
  3157 
       
  3158   if(ndig >= USED(mp))
       
  3159     return;
       
  3160 
       
  3161   /* Flush all the bits above 2^d in its digit */
       
  3162   dmask = ((mp_digit)1 << nbit) - 1;
       
  3163   DIGIT(mp, ndig) &= dmask;
       
  3164 
       
  3165   /* Flush all digits above the one with 2^d in it */
       
  3166   for(ix = ndig + 1; ix < USED(mp); ix++)
       
  3167     DIGIT(mp, ix) = 0;
       
  3168 
       
  3169   s_mp_clamp(mp);
       
  3170 
       
  3171 } /* end s_mp_mod_2d() */
       
  3172 
       
  3173 /* }}} */
       
  3174 
       
  3175 /* {{{ s_mp_div_2d(mp, d) */
       
  3176 
       
  3177 /*
       
  3178   Divide the integer by 2^d, where d is a number of bits.  This
       
  3179   amounts to a bitwise shift of the value, and does not require the
       
  3180   full division code (used in Barrett reduction, see below)
       
  3181  */
       
  3182 void     s_mp_div_2d(mp_int *mp, mp_digit d)
       
  3183 {
       
  3184   int       ix;
       
  3185   mp_digit  save, next, mask;
       
  3186 
       
  3187   s_mp_rshd(mp, d / DIGIT_BIT);
       
  3188   d %= DIGIT_BIT;
       
  3189   if (d) {
       
  3190     mask = ((mp_digit)1 << d) - 1;
       
  3191     save = 0;
       
  3192     for(ix = USED(mp) - 1; ix >= 0; ix--) {
       
  3193       next = DIGIT(mp, ix) & mask;
       
  3194       DIGIT(mp, ix) = (DIGIT(mp, ix) >> d) | (save << (DIGIT_BIT - d));
       
  3195       save = next;
       
  3196     }
       
  3197   }
       
  3198   s_mp_clamp(mp);
       
  3199 
       
  3200 } /* end s_mp_div_2d() */
       
  3201 
       
  3202 /* }}} */
       
  3203 
       
  3204 /* {{{ s_mp_norm(a, b, *d) */
       
  3205 
       
  3206 /*
       
  3207   s_mp_norm(a, b, *d)
       
  3208 
       
  3209   Normalize a and b for division, where b is the divisor.  In order
       
  3210   that we might make good guesses for quotient digits, we want the
       
  3211   leading digit of b to be at least half the radix, which we
       
  3212   accomplish by multiplying a and b by a power of 2.  The exponent
       
  3213   (shift count) is placed in *pd, so that the remainder can be shifted
       
  3214   back at the end of the division process.
       
  3215  */
       
  3216 
       
  3217 mp_err   s_mp_norm(mp_int *a, mp_int *b, mp_digit *pd)
       
  3218 {
       
  3219   mp_digit  d;
       
  3220   mp_digit  mask;
       
  3221   mp_digit  b_msd;
       
  3222   mp_err    res    = MP_OKAY;
       
  3223 
       
  3224   d = 0;
       
  3225   mask  = DIGIT_MAX & ~(DIGIT_MAX >> 1);        /* mask is msb of digit */
       
  3226   b_msd = DIGIT(b, USED(b) - 1);
       
  3227   while (!(b_msd & mask)) {
       
  3228     b_msd <<= 1;
       
  3229     ++d;
       
  3230   }
       
  3231 
       
  3232   if (d) {
       
  3233     MP_CHECKOK( s_mp_mul_2d(a, d) );
       
  3234     MP_CHECKOK( s_mp_mul_2d(b, d) );
       
  3235   }
       
  3236 
       
  3237   *pd = d;
       
  3238 CLEANUP:
       
  3239   return res;
       
  3240 
       
  3241 } /* end s_mp_norm() */
       
  3242 
       
  3243 /* }}} */
       
  3244 
       
  3245 /* }}} */
       
  3246 
       
  3247 /* {{{ Primitive digit arithmetic */
       
  3248 
       
  3249 /* {{{ s_mp_add_d(mp, d) */
       
  3250 
       
  3251 /* Add d to |mp| in place                                                 */
       
  3252 mp_err   s_mp_add_d(mp_int *mp, mp_digit d)    /* unsigned digit addition */
       
  3253 {
       
  3254 #if !defined(MP_NO_MP_WORD) && !defined(MP_NO_ADD_WORD)
       
  3255   mp_word   w, k = 0;
       
  3256   mp_size   ix = 1;
       
  3257 
       
  3258   w = (mp_word)DIGIT(mp, 0) + d;
       
  3259   DIGIT(mp, 0) = ACCUM(w);
       
  3260   k = CARRYOUT(w);
       
  3261 
       
  3262   while(ix < USED(mp) && k) {
       
  3263     w = (mp_word)DIGIT(mp, ix) + k;
       
  3264     DIGIT(mp, ix) = ACCUM(w);
       
  3265     k = CARRYOUT(w);
       
  3266     ++ix;
       
  3267   }
       
  3268 
       
  3269   if(k != 0) {
       
  3270     mp_err  res;
       
  3271 
       
  3272     if((res = s_mp_pad(mp, USED(mp) + 1)) != MP_OKAY)
       
  3273       return res;
       
  3274 
       
  3275     DIGIT(mp, ix) = (mp_digit)k;
       
  3276   }
       
  3277 
       
  3278   return MP_OKAY;
       
  3279 #else
       
  3280   mp_digit * pmp = MP_DIGITS(mp);
       
  3281   mp_digit sum, mp_i, carry = 0;
       
  3282   mp_err   res = MP_OKAY;
       
  3283   int used = (int)MP_USED(mp);
       
  3284 
       
  3285   mp_i = *pmp;
       
  3286   *pmp++ = sum = d + mp_i;
       
  3287   carry = (sum < d);
       
  3288   while (carry && --used > 0) {
       
  3289     mp_i = *pmp;
       
  3290     *pmp++ = sum = carry + mp_i;
       
  3291     carry = !sum;
       
  3292   }
       
  3293   if (carry && !used) {
       
  3294     /* mp is growing */
       
  3295     used = MP_USED(mp);
       
  3296     MP_CHECKOK( s_mp_pad(mp, used + 1) );
       
  3297     MP_DIGIT(mp, used) = carry;
       
  3298   }
       
  3299 CLEANUP:
       
  3300   return res;
       
  3301 #endif
       
  3302 } /* end s_mp_add_d() */
       
  3303 
       
  3304 /* }}} */
       
  3305 
       
  3306 /* {{{ s_mp_sub_d(mp, d) */
       
  3307 
       
  3308 /* Subtract d from |mp| in place, assumes |mp| > d                        */
       
  3309 mp_err   s_mp_sub_d(mp_int *mp, mp_digit d)    /* unsigned digit subtract */
       
  3310 {
       
  3311 #if !defined(MP_NO_MP_WORD) && !defined(MP_NO_SUB_WORD)
       
  3312   mp_word   w, b = 0;
       
  3313   mp_size   ix = 1;
       
  3314 
       
  3315   /* Compute initial subtraction    */
       
  3316   w = (RADIX + (mp_word)DIGIT(mp, 0)) - d;
       
  3317   b = CARRYOUT(w) ? 0 : 1;
       
  3318   DIGIT(mp, 0) = ACCUM(w);
       
  3319 
       
  3320   /* Propagate borrows leftward     */
       
  3321   while(b && ix < USED(mp)) {
       
  3322     w = (RADIX + (mp_word)DIGIT(mp, ix)) - b;
       
  3323     b = CARRYOUT(w) ? 0 : 1;
       
  3324     DIGIT(mp, ix) = ACCUM(w);
       
  3325     ++ix;
       
  3326   }
       
  3327 
       
  3328   /* Remove leading zeroes          */
       
  3329   s_mp_clamp(mp);
       
  3330 
       
  3331   /* If we have a borrow out, it's a violation of the input invariant */
       
  3332   if(b)
       
  3333     return MP_RANGE;
       
  3334   else
       
  3335     return MP_OKAY;
       
  3336 #else
       
  3337   mp_digit *pmp = MP_DIGITS(mp);
       
  3338   mp_digit mp_i, diff, borrow;
       
  3339   mp_size  used = MP_USED(mp);
       
  3340 
       
  3341   mp_i = *pmp;
       
  3342   *pmp++ = diff = mp_i - d;
       
  3343   borrow = (diff > mp_i);
       
  3344   while (borrow && --used) {
       
  3345     mp_i = *pmp;
       
  3346     *pmp++ = diff = mp_i - borrow;
       
  3347     borrow = (diff > mp_i);
       
  3348   }
       
  3349   s_mp_clamp(mp);
       
  3350   return (borrow && !used) ? MP_RANGE : MP_OKAY;
       
  3351 #endif
       
  3352 } /* end s_mp_sub_d() */
       
  3353 
       
  3354 /* }}} */
       
  3355 
       
  3356 /* {{{ s_mp_mul_d(a, d) */
       
  3357 
       
  3358 /* Compute a = a * d, single digit multiplication                         */
       
  3359 mp_err   s_mp_mul_d(mp_int *a, mp_digit d)
       
  3360 {
       
  3361   mp_err  res;
       
  3362   mp_size used;
       
  3363   int     pow;
       
  3364 
       
  3365   if (!d) {
       
  3366     mp_zero(a);
       
  3367     return MP_OKAY;
       
  3368   }
       
  3369   if (d == 1)
       
  3370     return MP_OKAY;
       
  3371   if (0 <= (pow = s_mp_ispow2d(d))) {
       
  3372     return s_mp_mul_2d(a, (mp_digit)pow);
       
  3373   }
       
  3374 
       
  3375   used = MP_USED(a);
       
  3376   MP_CHECKOK( s_mp_pad(a, used + 1) );
       
  3377 
       
  3378   s_mpv_mul_d(MP_DIGITS(a), used, d, MP_DIGITS(a));
       
  3379 
       
  3380   s_mp_clamp(a);
       
  3381 
       
  3382 CLEANUP:
       
  3383   return res;
       
  3384 
       
  3385 } /* end s_mp_mul_d() */
       
  3386 
       
  3387 /* }}} */
       
  3388 
       
  3389 /* {{{ s_mp_div_d(mp, d, r) */
       
  3390 
       
  3391 /*
       
  3392   s_mp_div_d(mp, d, r)
       
  3393 
       
  3394   Compute the quotient mp = mp / d and remainder r = mp mod d, for a
       
  3395   single digit d.  If r is null, the remainder will be discarded.
       
  3396  */
       
  3397 
       
  3398 mp_err   s_mp_div_d(mp_int *mp, mp_digit d, mp_digit *r)
       
  3399 {
       
  3400 #if !defined(MP_NO_MP_WORD) && !defined(MP_NO_DIV_WORD)
       
  3401   mp_word   w = 0, q;
       
  3402 #else
       
  3403   mp_digit  w, q;
       
  3404 #endif
       
  3405   int       ix;
       
  3406   mp_err    res;
       
  3407   mp_int    quot;
       
  3408   mp_int    rem;
       
  3409 
       
  3410   if(d == 0)
       
  3411     return MP_RANGE;
       
  3412   if (d == 1) {
       
  3413     if (r)
       
  3414       *r = 0;
       
  3415     return MP_OKAY;
       
  3416   }
       
  3417   /* could check for power of 2 here, but mp_div_d does that. */
       
  3418   if (MP_USED(mp) == 1) {
       
  3419     mp_digit n   = MP_DIGIT(mp,0);
       
  3420     mp_digit rem;
       
  3421 
       
  3422     q   = n / d;
       
  3423     rem = n % d;
       
  3424     MP_DIGIT(mp,0) = q;
       
  3425     if (r)
       
  3426       *r = rem;
       
  3427     return MP_OKAY;
       
  3428   }
       
  3429 
       
  3430   MP_DIGITS(&rem)  = 0;
       
  3431   MP_DIGITS(&quot) = 0;
       
  3432   /* Make room for the quotient */
       
  3433   MP_CHECKOK( mp_init_size(&quot, USED(mp), FLAG(mp)) );
       
  3434 
       
  3435 #if !defined(MP_NO_MP_WORD) && !defined(MP_NO_DIV_WORD)
       
  3436   for(ix = USED(mp) - 1; ix >= 0; ix--) {
       
  3437     w = (w << DIGIT_BIT) | DIGIT(mp, ix);
       
  3438 
       
  3439     if(w >= d) {
       
  3440       q = w / d;
       
  3441       w = w % d;
       
  3442     } else {
       
  3443       q = 0;
       
  3444     }
       
  3445 
       
  3446     s_mp_lshd(&quot, 1);
       
  3447     DIGIT(&quot, 0) = (mp_digit)q;
       
  3448   }
       
  3449 #else
       
  3450   {
       
  3451     mp_digit p;
       
  3452 #if !defined(MP_ASSEMBLY_DIV_2DX1D)
       
  3453     mp_digit norm;
       
  3454 #endif
       
  3455 
       
  3456     MP_CHECKOK( mp_init_copy(&rem, mp) );
       
  3457 
       
  3458 #if !defined(MP_ASSEMBLY_DIV_2DX1D)
       
  3459     MP_DIGIT(&quot, 0) = d;
       
  3460     MP_CHECKOK( s_mp_norm(&rem, &quot, &norm) );
       
  3461     if (norm)
       
  3462       d <<= norm;
       
  3463     MP_DIGIT(&quot, 0) = 0;
       
  3464 #endif
       
  3465 
       
  3466     p = 0;
       
  3467     for (ix = USED(&rem) - 1; ix >= 0; ix--) {
       
  3468       w = DIGIT(&rem, ix);
       
  3469 
       
  3470       if (p) {
       
  3471         MP_CHECKOK( s_mpv_div_2dx1d(p, w, d, &q, &w) );
       
  3472       } else if (w >= d) {
       
  3473         q = w / d;
       
  3474         w = w % d;
       
  3475       } else {
       
  3476         q = 0;
       
  3477       }
       
  3478 
       
  3479       MP_CHECKOK( s_mp_lshd(&quot, 1) );
       
  3480       DIGIT(&quot, 0) = q;
       
  3481       p = w;
       
  3482     }
       
  3483 #if !defined(MP_ASSEMBLY_DIV_2DX1D)
       
  3484     if (norm)
       
  3485       w >>= norm;
       
  3486 #endif
       
  3487   }
       
  3488 #endif
       
  3489 
       
  3490   /* Deliver the remainder, if desired */
       
  3491   if(r)
       
  3492     *r = (mp_digit)w;
       
  3493 
       
  3494   s_mp_clamp(&quot);
       
  3495   mp_exch(&quot, mp);
       
  3496 CLEANUP:
       
  3497   mp_clear(&quot);
       
  3498   mp_clear(&rem);
       
  3499 
       
  3500   return res;
       
  3501 } /* end s_mp_div_d() */
       
  3502 
       
  3503 /* }}} */
       
  3504 
       
  3505 
       
  3506 /* }}} */
       
  3507 
       
  3508 /* {{{ Primitive full arithmetic */
       
  3509 
       
  3510 /* {{{ s_mp_add(a, b) */
       
  3511 
       
  3512 /* Compute a = |a| + |b|                                                  */
       
  3513 mp_err   s_mp_add(mp_int *a, const mp_int *b)  /* magnitude addition      */
       
  3514 {
       
  3515 #if !defined(MP_NO_MP_WORD) && !defined(MP_NO_ADD_WORD)
       
  3516   mp_word   w = 0;
       
  3517 #else
       
  3518   mp_digit  d, sum, carry = 0;
       
  3519 #endif
       
  3520   mp_digit *pa, *pb;
       
  3521   mp_size   ix;
       
  3522   mp_size   used;
       
  3523   mp_err    res;
       
  3524 
       
  3525   /* Make sure a has enough precision for the output value */
       
  3526   if((USED(b) > USED(a)) && (res = s_mp_pad(a, USED(b))) != MP_OKAY)
       
  3527     return res;
       
  3528 
       
  3529   /*
       
  3530     Add up all digits up to the precision of b.  If b had initially
       
  3531     the same precision as a, or greater, we took care of it by the
       
  3532     padding step above, so there is no problem.  If b had initially
       
  3533     less precision, we'll have to make sure the carry out is duly
       
  3534     propagated upward among the higher-order digits of the sum.
       
  3535    */
       
  3536   pa = MP_DIGITS(a);
       
  3537   pb = MP_DIGITS(b);
       
  3538   used = MP_USED(b);
       
  3539   for(ix = 0; ix < used; ix++) {
       
  3540 #if !defined(MP_NO_MP_WORD) && !defined(MP_NO_ADD_WORD)
       
  3541     w = w + *pa + *pb++;
       
  3542     *pa++ = ACCUM(w);
       
  3543     w = CARRYOUT(w);
       
  3544 #else
       
  3545     d = *pa;
       
  3546     sum = d + *pb++;
       
  3547     d = (sum < d);                      /* detect overflow */
       
  3548     *pa++ = sum += carry;
       
  3549     carry = d + (sum < carry);          /* detect overflow */
       
  3550 #endif
       
  3551   }
       
  3552 
       
  3553   /* If we run out of 'b' digits before we're actually done, make
       
  3554      sure the carries get propagated upward...
       
  3555    */
       
  3556   used = MP_USED(a);
       
  3557 #if !defined(MP_NO_MP_WORD) && !defined(MP_NO_ADD_WORD)
       
  3558   while (w && ix < used) {
       
  3559     w = w + *pa;
       
  3560     *pa++ = ACCUM(w);
       
  3561     w = CARRYOUT(w);
       
  3562     ++ix;
       
  3563   }
       
  3564 #else
       
  3565   while (carry && ix < used) {
       
  3566     sum = carry + *pa;
       
  3567     *pa++ = sum;
       
  3568     carry = !sum;
       
  3569     ++ix;
       
  3570   }
       
  3571 #endif
       
  3572 
       
  3573   /* If there's an overall carry out, increase precision and include
       
  3574      it.  We could have done this initially, but why touch the memory
       
  3575      allocator unless we're sure we have to?
       
  3576    */
       
  3577 #if !defined(MP_NO_MP_WORD) && !defined(MP_NO_ADD_WORD)
       
  3578   if (w) {
       
  3579     if((res = s_mp_pad(a, used + 1)) != MP_OKAY)
       
  3580       return res;
       
  3581 
       
  3582     DIGIT(a, ix) = (mp_digit)w;
       
  3583   }
       
  3584 #else
       
  3585   if (carry) {
       
  3586     if((res = s_mp_pad(a, used + 1)) != MP_OKAY)
       
  3587       return res;
       
  3588 
       
  3589     DIGIT(a, used) = carry;
       
  3590   }
       
  3591 #endif
       
  3592 
       
  3593   return MP_OKAY;
       
  3594 } /* end s_mp_add() */
       
  3595 
       
  3596 /* }}} */
       
  3597 
       
  3598 /* Compute c = |a| + |b|         */ /* magnitude addition      */
       
  3599 mp_err   s_mp_add_3arg(const mp_int *a, const mp_int *b, mp_int *c)
       
  3600 {
       
  3601   mp_digit *pa, *pb, *pc;
       
  3602 #if !defined(MP_NO_MP_WORD) && !defined(MP_NO_ADD_WORD)
       
  3603   mp_word   w = 0;
       
  3604 #else
       
  3605   mp_digit  sum, carry = 0, d;
       
  3606 #endif
       
  3607   mp_size   ix;
       
  3608   mp_size   used;
       
  3609   mp_err    res;
       
  3610 
       
  3611   MP_SIGN(c) = MP_SIGN(a);
       
  3612   if (MP_USED(a) < MP_USED(b)) {
       
  3613     const mp_int *xch = a;
       
  3614     a = b;
       
  3615     b = xch;
       
  3616   }
       
  3617 
       
  3618   /* Make sure a has enough precision for the output value */
       
  3619   if (MP_OKAY != (res = s_mp_pad(c, MP_USED(a))))
       
  3620     return res;
       
  3621 
       
  3622   /*
       
  3623     Add up all digits up to the precision of b.  If b had initially
       
  3624     the same precision as a, or greater, we took care of it by the
       
  3625     exchange step above, so there is no problem.  If b had initially
       
  3626     less precision, we'll have to make sure the carry out is duly
       
  3627     propagated upward among the higher-order digits of the sum.
       
  3628    */
       
  3629   pa = MP_DIGITS(a);
       
  3630   pb = MP_DIGITS(b);
       
  3631   pc = MP_DIGITS(c);
       
  3632   used = MP_USED(b);
       
  3633   for (ix = 0; ix < used; ix++) {
       
  3634 #if !defined(MP_NO_MP_WORD) && !defined(MP_NO_ADD_WORD)
       
  3635     w = w + *pa++ + *pb++;
       
  3636     *pc++ = ACCUM(w);
       
  3637     w = CARRYOUT(w);
       
  3638 #else
       
  3639     d = *pa++;
       
  3640     sum = d + *pb++;
       
  3641     d = (sum < d);                      /* detect overflow */
       
  3642     *pc++ = sum += carry;
       
  3643     carry = d + (sum < carry);          /* detect overflow */
       
  3644 #endif
       
  3645   }
       
  3646 
       
  3647   /* If we run out of 'b' digits before we're actually done, make
       
  3648      sure the carries get propagated upward...
       
  3649    */
       
  3650   for (used = MP_USED(a); ix < used; ++ix) {
       
  3651 #if !defined(MP_NO_MP_WORD) && !defined(MP_NO_ADD_WORD)
       
  3652     w = w + *pa++;
       
  3653     *pc++ = ACCUM(w);
       
  3654     w = CARRYOUT(w);
       
  3655 #else
       
  3656     *pc++ = sum = carry + *pa++;
       
  3657     carry = (sum < carry);
       
  3658 #endif
       
  3659   }
       
  3660 
       
  3661   /* If there's an overall carry out, increase precision and include
       
  3662      it.  We could have done this initially, but why touch the memory
       
  3663      allocator unless we're sure we have to?
       
  3664    */
       
  3665 #if !defined(MP_NO_MP_WORD) && !defined(MP_NO_ADD_WORD)
       
  3666   if (w) {
       
  3667     if((res = s_mp_pad(c, used + 1)) != MP_OKAY)
       
  3668       return res;
       
  3669 
       
  3670     DIGIT(c, used) = (mp_digit)w;
       
  3671     ++used;
       
  3672   }
       
  3673 #else
       
  3674   if (carry) {
       
  3675     if((res = s_mp_pad(c, used + 1)) != MP_OKAY)
       
  3676       return res;
       
  3677 
       
  3678     DIGIT(c, used) = carry;
       
  3679     ++used;
       
  3680   }
       
  3681 #endif
       
  3682   MP_USED(c) = used;
       
  3683   return MP_OKAY;
       
  3684 }
       
  3685 /* {{{ s_mp_add_offset(a, b, offset) */
       
  3686 
       
  3687 /* Compute a = |a| + ( |b| * (RADIX ** offset) )             */
       
  3688 mp_err   s_mp_add_offset(mp_int *a, mp_int *b, mp_size offset)
       
  3689 {
       
  3690 #if !defined(MP_NO_MP_WORD) && !defined(MP_NO_ADD_WORD)
       
  3691   mp_word   w, k = 0;
       
  3692 #else
       
  3693   mp_digit  d, sum, carry = 0;
       
  3694 #endif
       
  3695   mp_size   ib;
       
  3696   mp_size   ia;
       
  3697   mp_size   lim;
       
  3698   mp_err    res;
       
  3699 
       
  3700   /* Make sure a has enough precision for the output value */
       
  3701   lim = MP_USED(b) + offset;
       
  3702   if((lim > USED(a)) && (res = s_mp_pad(a, lim)) != MP_OKAY)
       
  3703     return res;
       
  3704 
       
  3705   /*
       
  3706     Add up all digits up to the precision of b.  If b had initially
       
  3707     the same precision as a, or greater, we took care of it by the
       
  3708     padding step above, so there is no problem.  If b had initially
       
  3709     less precision, we'll have to make sure the carry out is duly
       
  3710     propagated upward among the higher-order digits of the sum.
       
  3711    */
       
  3712   lim = USED(b);
       
  3713   for(ib = 0, ia = offset; ib < lim; ib++, ia++) {
       
  3714 #if !defined(MP_NO_MP_WORD) && !defined(MP_NO_ADD_WORD)
       
  3715     w = (mp_word)DIGIT(a, ia) + DIGIT(b, ib) + k;
       
  3716     DIGIT(a, ia) = ACCUM(w);
       
  3717     k = CARRYOUT(w);
       
  3718 #else
       
  3719     d = MP_DIGIT(a, ia);
       
  3720     sum = d + MP_DIGIT(b, ib);
       
  3721     d = (sum < d);
       
  3722     MP_DIGIT(a,ia) = sum += carry;
       
  3723     carry = d + (sum < carry);
       
  3724 #endif
       
  3725   }
       
  3726 
       
  3727   /* If we run out of 'b' digits before we're actually done, make
       
  3728      sure the carries get propagated upward...
       
  3729    */
       
  3730 #if !defined(MP_NO_MP_WORD) && !defined(MP_NO_ADD_WORD)
       
  3731   for (lim = MP_USED(a); k && (ia < lim); ++ia) {
       
  3732     w = (mp_word)DIGIT(a, ia) + k;
       
  3733     DIGIT(a, ia) = ACCUM(w);
       
  3734     k = CARRYOUT(w);
       
  3735   }
       
  3736 #else
       
  3737   for (lim = MP_USED(a); carry && (ia < lim); ++ia) {
       
  3738     d = MP_DIGIT(a, ia);
       
  3739     MP_DIGIT(a,ia) = sum = d + carry;
       
  3740     carry = (sum < d);
       
  3741   }
       
  3742 #endif
       
  3743 
       
  3744   /* If there's an overall carry out, increase precision and include
       
  3745      it.  We could have done this initially, but why touch the memory
       
  3746      allocator unless we're sure we have to?
       
  3747    */
       
  3748 #if !defined(MP_NO_MP_WORD) && !defined(MP_NO_ADD_WORD)
       
  3749   if(k) {
       
  3750     if((res = s_mp_pad(a, USED(a) + 1)) != MP_OKAY)
       
  3751       return res;
       
  3752 
       
  3753     DIGIT(a, ia) = (mp_digit)k;
       
  3754   }
       
  3755 #else
       
  3756   if (carry) {
       
  3757     if((res = s_mp_pad(a, lim + 1)) != MP_OKAY)
       
  3758       return res;
       
  3759 
       
  3760     DIGIT(a, lim) = carry;
       
  3761   }
       
  3762 #endif
       
  3763   s_mp_clamp(a);
       
  3764 
       
  3765   return MP_OKAY;
       
  3766 
       
  3767 } /* end s_mp_add_offset() */
       
  3768 
       
  3769 /* }}} */
       
  3770 
       
  3771 /* {{{ s_mp_sub(a, b) */
       
  3772 
       
  3773 /* Compute a = |a| - |b|, assumes |a| >= |b|                              */
       
  3774 mp_err   s_mp_sub(mp_int *a, const mp_int *b)  /* magnitude subtract      */
       
  3775 {
       
  3776   mp_digit *pa, *pb, *limit;
       
  3777 #if !defined(MP_NO_MP_WORD) && !defined(MP_NO_SUB_WORD)
       
  3778   mp_sword  w = 0;
       
  3779 #else
       
  3780   mp_digit  d, diff, borrow = 0;
       
  3781 #endif
       
  3782 
       
  3783   /*
       
  3784     Subtract and propagate borrow.  Up to the precision of b, this
       
  3785     accounts for the digits of b; after that, we just make sure the
       
  3786     carries get to the right place.  This saves having to pad b out to
       
  3787     the precision of a just to make the loops work right...
       
  3788    */
       
  3789   pa = MP_DIGITS(a);
       
  3790   pb = MP_DIGITS(b);
       
  3791   limit = pb + MP_USED(b);
       
  3792   while (pb < limit) {
       
  3793 #if !defined(MP_NO_MP_WORD) && !defined(MP_NO_SUB_WORD)
       
  3794     w = w + *pa - *pb++;
       
  3795     *pa++ = ACCUM(w);
       
  3796     w >>= MP_DIGIT_BIT;
       
  3797 #else
       
  3798     d = *pa;
       
  3799     diff = d - *pb++;
       
  3800     d = (diff > d);                             /* detect borrow */
       
  3801     if (borrow && --diff == MP_DIGIT_MAX)
       
  3802       ++d;
       
  3803     *pa++ = diff;
       
  3804     borrow = d;
       
  3805 #endif
       
  3806   }
       
  3807   limit = MP_DIGITS(a) + MP_USED(a);
       
  3808 #if !defined(MP_NO_MP_WORD) && !defined(MP_NO_SUB_WORD)
       
  3809   while (w && pa < limit) {
       
  3810     w = w + *pa;
       
  3811     *pa++ = ACCUM(w);
       
  3812     w >>= MP_DIGIT_BIT;
       
  3813   }
       
  3814 #else
       
  3815   while (borrow && pa < limit) {
       
  3816     d = *pa;
       
  3817     *pa++ = diff = d - borrow;
       
  3818     borrow = (diff > d);
       
  3819   }
       
  3820 #endif
       
  3821 
       
  3822   /* Clobber any leading zeroes we created    */
       
  3823   s_mp_clamp(a);
       
  3824 
       
  3825   /*
       
  3826      If there was a borrow out, then |b| > |a| in violation
       
  3827      of our input invariant.  We've already done the work,
       
  3828      but we'll at least complain about it...
       
  3829    */
       
  3830 #if !defined(MP_NO_MP_WORD) && !defined(MP_NO_SUB_WORD)
       
  3831   return w ? MP_RANGE : MP_OKAY;
       
  3832 #else
       
  3833   return borrow ? MP_RANGE : MP_OKAY;
       
  3834 #endif
       
  3835 } /* end s_mp_sub() */
       
  3836 
       
  3837 /* }}} */
       
  3838 
       
  3839 /* Compute c = |a| - |b|, assumes |a| >= |b| */ /* magnitude subtract      */
       
  3840 mp_err   s_mp_sub_3arg(const mp_int *a, const mp_int *b, mp_int *c)
       
  3841 {
       
  3842   mp_digit *pa, *pb, *pc;
       
  3843 #if !defined(MP_NO_MP_WORD) && !defined(MP_NO_SUB_WORD)
       
  3844   mp_sword  w = 0;
       
  3845 #else
       
  3846   mp_digit  d, diff, borrow = 0;
       
  3847 #endif
       
  3848   int       ix, limit;
       
  3849   mp_err    res;
       
  3850 
       
  3851   MP_SIGN(c) = MP_SIGN(a);
       
  3852 
       
  3853   /* Make sure a has enough precision for the output value */
       
  3854   if (MP_OKAY != (res = s_mp_pad(c, MP_USED(a))))
       
  3855     return res;
       
  3856 
       
  3857   /*
       
  3858     Subtract and propagate borrow.  Up to the precision of b, this
       
  3859     accounts for the digits of b; after that, we just make sure the
       
  3860     carries get to the right place.  This saves having to pad b out to
       
  3861     the precision of a just to make the loops work right...
       
  3862    */
       
  3863   pa = MP_DIGITS(a);
       
  3864   pb = MP_DIGITS(b);
       
  3865   pc = MP_DIGITS(c);
       
  3866   limit = MP_USED(b);
       
  3867   for (ix = 0; ix < limit; ++ix) {
       
  3868 #if !defined(MP_NO_MP_WORD) && !defined(MP_NO_SUB_WORD)
       
  3869     w = w + *pa++ - *pb++;
       
  3870     *pc++ = ACCUM(w);
       
  3871     w >>= MP_DIGIT_BIT;
       
  3872 #else
       
  3873     d = *pa++;
       
  3874     diff = d - *pb++;
       
  3875     d = (diff > d);
       
  3876     if (borrow && --diff == MP_DIGIT_MAX)
       
  3877       ++d;
       
  3878     *pc++ = diff;
       
  3879     borrow = d;
       
  3880 #endif
       
  3881   }
       
  3882   for (limit = MP_USED(a); ix < limit; ++ix) {
       
  3883 #if !defined(MP_NO_MP_WORD) && !defined(MP_NO_SUB_WORD)
       
  3884     w = w + *pa++;
       
  3885     *pc++ = ACCUM(w);
       
  3886     w >>= MP_DIGIT_BIT;
       
  3887 #else
       
  3888     d = *pa++;
       
  3889     *pc++ = diff = d - borrow;
       
  3890     borrow = (diff > d);
       
  3891 #endif
       
  3892   }
       
  3893 
       
  3894   /* Clobber any leading zeroes we created    */
       
  3895   MP_USED(c) = ix;
       
  3896   s_mp_clamp(c);
       
  3897 
       
  3898   /*
       
  3899      If there was a borrow out, then |b| > |a| in violation
       
  3900      of our input invariant.  We've already done the work,
       
  3901      but we'll at least complain about it...
       
  3902    */
       
  3903 #if !defined(MP_NO_MP_WORD) && !defined(MP_NO_SUB_WORD)
       
  3904   return w ? MP_RANGE : MP_OKAY;
       
  3905 #else
       
  3906   return borrow ? MP_RANGE : MP_OKAY;
       
  3907 #endif
       
  3908 }
       
  3909 /* {{{ s_mp_mul(a, b) */
       
  3910 
       
  3911 /* Compute a = |a| * |b|                                                  */
       
  3912 mp_err   s_mp_mul(mp_int *a, const mp_int *b)
       
  3913 {
       
  3914   return mp_mul(a, b, a);
       
  3915 } /* end s_mp_mul() */
       
  3916 
       
  3917 /* }}} */
       
  3918 
       
  3919 #if defined(MP_USE_UINT_DIGIT) && defined(MP_USE_LONG_LONG_MULTIPLY)
       
  3920 /* This trick works on Sparc V8 CPUs with the Workshop compilers. */
       
  3921 #define MP_MUL_DxD(a, b, Phi, Plo) \
       
  3922   { unsigned long long product = (unsigned long long)a * b; \
       
  3923     Plo = (mp_digit)product; \
       
  3924     Phi = (mp_digit)(product >> MP_DIGIT_BIT); }
       
  3925 #elif defined(OSF1)
       
  3926 #define MP_MUL_DxD(a, b, Phi, Plo) \
       
  3927   { Plo = asm ("mulq %a0, %a1, %v0", a, b);\
       
  3928     Phi = asm ("umulh %a0, %a1, %v0", a, b); }
       
  3929 #else
       
  3930 #define MP_MUL_DxD(a, b, Phi, Plo) \
       
  3931   { mp_digit a0b1, a1b0; \
       
  3932     Plo = (a & MP_HALF_DIGIT_MAX) * (b & MP_HALF_DIGIT_MAX); \
       
  3933     Phi = (a >> MP_HALF_DIGIT_BIT) * (b >> MP_HALF_DIGIT_BIT); \
       
  3934     a0b1 = (a & MP_HALF_DIGIT_MAX) * (b >> MP_HALF_DIGIT_BIT); \
       
  3935     a1b0 = (a >> MP_HALF_DIGIT_BIT) * (b & MP_HALF_DIGIT_MAX); \
       
  3936     a1b0 += a0b1; \
       
  3937     Phi += a1b0 >> MP_HALF_DIGIT_BIT; \
       
  3938     if (a1b0 < a0b1)  \
       
  3939       Phi += MP_HALF_RADIX; \
       
  3940     a1b0 <<= MP_HALF_DIGIT_BIT; \
       
  3941     Plo += a1b0; \
       
  3942     if (Plo < a1b0) \
       
  3943       ++Phi; \
       
  3944   }
       
  3945 #endif
       
  3946 
       
  3947 #if !defined(MP_ASSEMBLY_MULTIPLY)
       
  3948 /* c = a * b */
       
  3949 void s_mpv_mul_d(const mp_digit *a, mp_size a_len, mp_digit b, mp_digit *c)
       
  3950 {
       
  3951 #if !defined(MP_NO_MP_WORD) && !defined(MP_NO_MUL_WORD)
       
  3952   mp_digit   d = 0;
       
  3953 
       
  3954   /* Inner product:  Digits of a */
       
  3955   while (a_len--) {
       
  3956     mp_word w = ((mp_word)b * *a++) + d;
       
  3957     *c++ = ACCUM(w);
       
  3958     d = CARRYOUT(w);
       
  3959   }
       
  3960   *c = d;
       
  3961 #else
       
  3962   mp_digit carry = 0;
       
  3963   while (a_len--) {
       
  3964     mp_digit a_i = *a++;
       
  3965     mp_digit a0b0, a1b1;
       
  3966 
       
  3967     MP_MUL_DxD(a_i, b, a1b1, a0b0);
       
  3968 
       
  3969     a0b0 += carry;
       
  3970     if (a0b0 < carry)
       
  3971       ++a1b1;
       
  3972     *c++ = a0b0;
       
  3973     carry = a1b1;
       
  3974   }
       
  3975   *c = carry;
       
  3976 #endif
       
  3977 }
       
  3978 
       
  3979 /* c += a * b */
       
  3980 void s_mpv_mul_d_add(const mp_digit *a, mp_size a_len, mp_digit b,
       
  3981                               mp_digit *c)
       
  3982 {
       
  3983 #if !defined(MP_NO_MP_WORD) && !defined(MP_NO_MUL_WORD)
       
  3984   mp_digit   d = 0;
       
  3985 
       
  3986   /* Inner product:  Digits of a */
       
  3987   while (a_len--) {
       
  3988     mp_word w = ((mp_word)b * *a++) + *c + d;
       
  3989     *c++ = ACCUM(w);
       
  3990     d = CARRYOUT(w);
       
  3991   }
       
  3992   *c = d;
       
  3993 #else
       
  3994   mp_digit carry = 0;
       
  3995   while (a_len--) {
       
  3996     mp_digit a_i = *a++;
       
  3997     mp_digit a0b0, a1b1;
       
  3998 
       
  3999     MP_MUL_DxD(a_i, b, a1b1, a0b0);
       
  4000 
       
  4001     a0b0 += carry;
       
  4002     if (a0b0 < carry)
       
  4003       ++a1b1;
       
  4004     a0b0 += a_i = *c;
       
  4005     if (a0b0 < a_i)
       
  4006       ++a1b1;
       
  4007     *c++ = a0b0;
       
  4008     carry = a1b1;
       
  4009   }
       
  4010   *c = carry;
       
  4011 #endif
       
  4012 }
       
  4013 
       
  4014 /* Presently, this is only used by the Montgomery arithmetic code. */
       
  4015 /* c += a * b */
       
  4016 void s_mpv_mul_d_add_prop(const mp_digit *a, mp_size a_len, mp_digit b, mp_digit *c)
       
  4017 {
       
  4018 #if !defined(MP_NO_MP_WORD) && !defined(MP_NO_MUL_WORD)
       
  4019   mp_digit   d = 0;
       
  4020 
       
  4021   /* Inner product:  Digits of a */
       
  4022   while (a_len--) {
       
  4023     mp_word w = ((mp_word)b * *a++) + *c + d;
       
  4024     *c++ = ACCUM(w);
       
  4025     d = CARRYOUT(w);
       
  4026   }
       
  4027 
       
  4028   while (d) {
       
  4029     mp_word w = (mp_word)*c + d;
       
  4030     *c++ = ACCUM(w);
       
  4031     d = CARRYOUT(w);
       
  4032   }
       
  4033 #else
       
  4034   mp_digit carry = 0;
       
  4035   while (a_len--) {
       
  4036     mp_digit a_i = *a++;
       
  4037     mp_digit a0b0, a1b1;
       
  4038 
       
  4039     MP_MUL_DxD(a_i, b, a1b1, a0b0);
       
  4040 
       
  4041     a0b0 += carry;
       
  4042     if (a0b0 < carry)
       
  4043       ++a1b1;
       
  4044 
       
  4045     a0b0 += a_i = *c;
       
  4046     if (a0b0 < a_i)
       
  4047       ++a1b1;
       
  4048 
       
  4049     *c++ = a0b0;
       
  4050     carry = a1b1;
       
  4051   }
       
  4052   while (carry) {
       
  4053     mp_digit c_i = *c;
       
  4054     carry += c_i;
       
  4055     *c++ = carry;
       
  4056     carry = carry < c_i;
       
  4057   }
       
  4058 #endif
       
  4059 }
       
  4060 #endif
       
  4061 
       
  4062 #if defined(MP_USE_UINT_DIGIT) && defined(MP_USE_LONG_LONG_MULTIPLY)
       
  4063 /* This trick works on Sparc V8 CPUs with the Workshop compilers. */
       
  4064 #define MP_SQR_D(a, Phi, Plo) \
       
  4065   { unsigned long long square = (unsigned long long)a * a; \
       
  4066     Plo = (mp_digit)square; \
       
  4067     Phi = (mp_digit)(square >> MP_DIGIT_BIT); }
       
  4068 #elif defined(OSF1)
       
  4069 #define MP_SQR_D(a, Phi, Plo) \
       
  4070   { Plo = asm ("mulq  %a0, %a0, %v0", a);\
       
  4071     Phi = asm ("umulh %a0, %a0, %v0", a); }
       
  4072 #else
       
  4073 #define MP_SQR_D(a, Phi, Plo) \
       
  4074   { mp_digit Pmid; \
       
  4075     Plo  = (a  & MP_HALF_DIGIT_MAX) * (a  & MP_HALF_DIGIT_MAX); \
       
  4076     Phi  = (a >> MP_HALF_DIGIT_BIT) * (a >> MP_HALF_DIGIT_BIT); \
       
  4077     Pmid = (a  & MP_HALF_DIGIT_MAX) * (a >> MP_HALF_DIGIT_BIT); \
       
  4078     Phi += Pmid >> (MP_HALF_DIGIT_BIT - 1);  \
       
  4079     Pmid <<= (MP_HALF_DIGIT_BIT + 1);  \
       
  4080     Plo += Pmid;  \
       
  4081     if (Plo < Pmid)  \
       
  4082       ++Phi;  \
       
  4083   }
       
  4084 #endif
       
  4085 
       
  4086 #if !defined(MP_ASSEMBLY_SQUARE)
       
  4087 /* Add the squares of the digits of a to the digits of b. */
       
  4088 void s_mpv_sqr_add_prop(const mp_digit *pa, mp_size a_len, mp_digit *ps)
       
  4089 {
       
  4090 #if !defined(MP_NO_MP_WORD) && !defined(MP_NO_MUL_WORD)
       
  4091   mp_word  w;
       
  4092   mp_digit d;
       
  4093   mp_size  ix;
       
  4094 
       
  4095   w  = 0;
       
  4096 #define ADD_SQUARE(n) \
       
  4097     d = pa[n]; \
       
  4098     w += (d * (mp_word)d) + ps[2*n]; \
       
  4099     ps[2*n] = ACCUM(w); \
       
  4100     w = (w >> DIGIT_BIT) + ps[2*n+1]; \
       
  4101     ps[2*n+1] = ACCUM(w); \
       
  4102     w = (w >> DIGIT_BIT)
       
  4103 
       
  4104   for (ix = a_len; ix >= 4; ix -= 4) {
       
  4105     ADD_SQUARE(0);
       
  4106     ADD_SQUARE(1);
       
  4107     ADD_SQUARE(2);
       
  4108     ADD_SQUARE(3);
       
  4109     pa += 4;
       
  4110     ps += 8;
       
  4111   }
       
  4112   if (ix) {
       
  4113     ps += 2*ix;
       
  4114     pa += ix;
       
  4115     switch (ix) {
       
  4116     case 3: ADD_SQUARE(-3); /* FALLTHRU */
       
  4117     case 2: ADD_SQUARE(-2); /* FALLTHRU */
       
  4118     case 1: ADD_SQUARE(-1); /* FALLTHRU */
       
  4119     case 0: break;
       
  4120     }
       
  4121   }
       
  4122   while (w) {
       
  4123     w += *ps;
       
  4124     *ps++ = ACCUM(w);
       
  4125     w = (w >> DIGIT_BIT);
       
  4126   }
       
  4127 #else
       
  4128   mp_digit carry = 0;
       
  4129   while (a_len--) {
       
  4130     mp_digit a_i = *pa++;
       
  4131     mp_digit a0a0, a1a1;
       
  4132 
       
  4133     MP_SQR_D(a_i, a1a1, a0a0);
       
  4134 
       
  4135     /* here a1a1 and a0a0 constitute a_i ** 2 */
       
  4136     a0a0 += carry;
       
  4137     if (a0a0 < carry)
       
  4138       ++a1a1;
       
  4139 
       
  4140     /* now add to ps */
       
  4141     a0a0 += a_i = *ps;
       
  4142     if (a0a0 < a_i)
       
  4143       ++a1a1;
       
  4144     *ps++ = a0a0;
       
  4145     a1a1 += a_i = *ps;
       
  4146     carry = (a1a1 < a_i);
       
  4147     *ps++ = a1a1;
       
  4148   }
       
  4149   while (carry) {
       
  4150     mp_digit s_i = *ps;
       
  4151     carry += s_i;
       
  4152     *ps++ = carry;
       
  4153     carry = carry < s_i;
       
  4154   }
       
  4155 #endif
       
  4156 }
       
  4157 #endif
       
  4158 
       
  4159 #if (defined(MP_NO_MP_WORD) || defined(MP_NO_DIV_WORD)) \
       
  4160 && !defined(MP_ASSEMBLY_DIV_2DX1D)
       
  4161 /*
       
  4162 ** Divide 64-bit (Nhi,Nlo) by 32-bit divisor, which must be normalized
       
  4163 ** so its high bit is 1.   This code is from NSPR.
       
  4164 */
       
  4165 mp_err s_mpv_div_2dx1d(mp_digit Nhi, mp_digit Nlo, mp_digit divisor,
       
  4166                        mp_digit *qp, mp_digit *rp)
       
  4167 {
       
  4168     mp_digit d1, d0, q1, q0;
       
  4169     mp_digit r1, r0, m;
       
  4170 
       
  4171     d1 = divisor >> MP_HALF_DIGIT_BIT;
       
  4172     d0 = divisor & MP_HALF_DIGIT_MAX;
       
  4173     r1 = Nhi % d1;
       
  4174     q1 = Nhi / d1;
       
  4175     m = q1 * d0;
       
  4176     r1 = (r1 << MP_HALF_DIGIT_BIT) | (Nlo >> MP_HALF_DIGIT_BIT);
       
  4177     if (r1 < m) {
       
  4178         q1--, r1 += divisor;
       
  4179         if (r1 >= divisor && r1 < m) {
       
  4180             q1--, r1 += divisor;
       
  4181         }
       
  4182     }
       
  4183     r1 -= m;
       
  4184     r0 = r1 % d1;
       
  4185     q0 = r1 / d1;
       
  4186     m = q0 * d0;
       
  4187     r0 = (r0 << MP_HALF_DIGIT_BIT) | (Nlo & MP_HALF_DIGIT_MAX);
       
  4188     if (r0 < m) {
       
  4189         q0--, r0 += divisor;
       
  4190         if (r0 >= divisor && r0 < m) {
       
  4191             q0--, r0 += divisor;
       
  4192         }
       
  4193     }
       
  4194     if (qp)
       
  4195         *qp = (q1 << MP_HALF_DIGIT_BIT) | q0;
       
  4196     if (rp)
       
  4197         *rp = r0 - m;
       
  4198     return MP_OKAY;
       
  4199 }
       
  4200 #endif
       
  4201 
       
  4202 #if MP_SQUARE
       
  4203 /* {{{ s_mp_sqr(a) */
       
  4204 
       
  4205 mp_err   s_mp_sqr(mp_int *a)
       
  4206 {
       
  4207   mp_err   res;
       
  4208   mp_int   tmp;
       
  4209 
       
  4210   if((res = mp_init_size(&tmp, 2 * USED(a), FLAG(a))) != MP_OKAY)
       
  4211     return res;
       
  4212   res = mp_sqr(a, &tmp);
       
  4213   if (res == MP_OKAY) {
       
  4214     s_mp_exch(&tmp, a);
       
  4215   }
       
  4216   mp_clear(&tmp);
       
  4217   return res;
       
  4218 }
       
  4219 
       
  4220 /* }}} */
       
  4221 #endif
       
  4222 
       
  4223 /* {{{ s_mp_div(a, b) */
       
  4224 
       
  4225 /*
       
  4226   s_mp_div(a, b)
       
  4227 
       
  4228   Compute a = a / b and b = a mod b.  Assumes b > a.
       
  4229  */
       
  4230 
       
  4231 mp_err   s_mp_div(mp_int *rem,  /* i: dividend, o: remainder */
       
  4232                   mp_int *div,  /* i: divisor                */
       
  4233                   mp_int *quot) /* i: 0;        o: quotient  */
       
  4234 {
       
  4235   mp_int   part, t;
       
  4236 #if !defined(MP_NO_MP_WORD) && !defined(MP_NO_DIV_WORD)
       
  4237   mp_word  q_msd;
       
  4238 #else
       
  4239   mp_digit q_msd;
       
  4240 #endif
       
  4241   mp_err   res;
       
  4242   mp_digit d;
       
  4243   mp_digit div_msd;
       
  4244   int      ix;
       
  4245 
       
  4246   if(mp_cmp_z(div) == 0)
       
  4247     return MP_RANGE;
       
  4248 
       
  4249   /* Shortcut if divisor is power of two */
       
  4250   if((ix = s_mp_ispow2(div)) >= 0) {
       
  4251     MP_CHECKOK( mp_copy(rem, quot) );
       
  4252     s_mp_div_2d(quot, (mp_digit)ix);
       
  4253     s_mp_mod_2d(rem,  (mp_digit)ix);
       
  4254 
       
  4255     return MP_OKAY;
       
  4256   }
       
  4257 
       
  4258   DIGITS(&t) = 0;
       
  4259   MP_SIGN(rem) = ZPOS;
       
  4260   MP_SIGN(div) = ZPOS;
       
  4261 
       
  4262   /* A working temporary for division     */
       
  4263   MP_CHECKOK( mp_init_size(&t, MP_ALLOC(rem), FLAG(rem)));
       
  4264 
       
  4265   /* Normalize to optimize guessing       */
       
  4266   MP_CHECKOK( s_mp_norm(rem, div, &d) );
       
  4267 
       
  4268   part = *rem;
       
  4269 
       
  4270   /* Perform the division itself...woo!   */
       
  4271   MP_USED(quot) = MP_ALLOC(quot);
       
  4272 
       
  4273   /* Find a partial substring of rem which is at least div */
       
  4274   /* If we didn't find one, we're finished dividing    */
       
  4275   while (MP_USED(rem) > MP_USED(div) || s_mp_cmp(rem, div) >= 0) {
       
  4276     int i;
       
  4277     int unusedRem;
       
  4278 
       
  4279     unusedRem = MP_USED(rem) - MP_USED(div);
       
  4280     MP_DIGITS(&part) = MP_DIGITS(rem) + unusedRem;
       
  4281     MP_ALLOC(&part)  = MP_ALLOC(rem)  - unusedRem;
       
  4282     MP_USED(&part)   = MP_USED(div);
       
  4283     if (s_mp_cmp(&part, div) < 0) {
       
  4284       -- unusedRem;
       
  4285 #if MP_ARGCHK == 2
       
  4286       assert(unusedRem >= 0);
       
  4287 #endif
       
  4288       -- MP_DIGITS(&part);
       
  4289       ++ MP_USED(&part);
       
  4290       ++ MP_ALLOC(&part);
       
  4291     }
       
  4292 
       
  4293     /* Compute a guess for the next quotient digit       */
       
  4294     q_msd = MP_DIGIT(&part, MP_USED(&part) - 1);
       
  4295     div_msd = MP_DIGIT(div, MP_USED(div) - 1);
       
  4296     if (q_msd >= div_msd) {
       
  4297       q_msd = 1;
       
  4298     } else if (MP_USED(&part) > 1) {
       
  4299 #if !defined(MP_NO_MP_WORD) && !defined(MP_NO_DIV_WORD)
       
  4300       q_msd = (q_msd << MP_DIGIT_BIT) | MP_DIGIT(&part, MP_USED(&part) - 2);
       
  4301       q_msd /= div_msd;
       
  4302       if (q_msd == RADIX)
       
  4303         --q_msd;
       
  4304 #else
       
  4305       mp_digit r;
       
  4306       MP_CHECKOK( s_mpv_div_2dx1d(q_msd, MP_DIGIT(&part, MP_USED(&part) - 2),
       
  4307                                   div_msd, &q_msd, &r) );
       
  4308 #endif
       
  4309     } else {
       
  4310       q_msd = 0;
       
  4311     }
       
  4312 #if MP_ARGCHK == 2
       
  4313     assert(q_msd > 0); /* This case should never occur any more. */
       
  4314 #endif
       
  4315     if (q_msd <= 0)
       
  4316       break;
       
  4317 
       
  4318     /* See what that multiplies out to                   */
       
  4319     mp_copy(div, &t);
       
  4320     MP_CHECKOK( s_mp_mul_d(&t, (mp_digit)q_msd) );
       
  4321 
       
  4322     /*
       
  4323        If it's too big, back it off.  We should not have to do this
       
  4324        more than once, or, in rare cases, twice.  Knuth describes a
       
  4325        method by which this could be reduced to a maximum of once, but
       
  4326        I didn't implement that here.
       
  4327      * When using s_mpv_div_2dx1d, we may have to do this 3 times.
       
  4328      */
       
  4329     for (i = 4; s_mp_cmp(&t, &part) > 0 && i > 0; --i) {
       
  4330       --q_msd;
       
  4331       s_mp_sub(&t, div);        /* t -= div */
       
  4332     }
       
  4333     if (i < 0) {
       
  4334       res = MP_RANGE;
       
  4335       goto CLEANUP;
       
  4336     }
       
  4337 
       
  4338     /* At this point, q_msd should be the right next digit   */
       
  4339     MP_CHECKOK( s_mp_sub(&part, &t) );  /* part -= t */
       
  4340     s_mp_clamp(rem);
       
  4341 
       
  4342     /*
       
  4343       Include the digit in the quotient.  We allocated enough memory
       
  4344       for any quotient we could ever possibly get, so we should not
       
  4345       have to check for failures here
       
  4346      */
       
  4347     MP_DIGIT(quot, unusedRem) = (mp_digit)q_msd;
       
  4348   }
       
  4349 
       
  4350   /* Denormalize remainder                */
       
  4351   if (d) {
       
  4352     s_mp_div_2d(rem, d);
       
  4353   }
       
  4354 
       
  4355   s_mp_clamp(quot);
       
  4356 
       
  4357 CLEANUP:
       
  4358   mp_clear(&t);
       
  4359 
       
  4360   return res;
       
  4361 
       
  4362 } /* end s_mp_div() */
       
  4363 
       
  4364 
       
  4365 /* }}} */
       
  4366 
       
  4367 /* {{{ s_mp_2expt(a, k) */
       
  4368 
       
  4369 mp_err   s_mp_2expt(mp_int *a, mp_digit k)
       
  4370 {
       
  4371   mp_err    res;
       
  4372   mp_size   dig, bit;
       
  4373 
       
  4374   dig = k / DIGIT_BIT;
       
  4375   bit = k % DIGIT_BIT;
       
  4376 
       
  4377   mp_zero(a);
       
  4378   if((res = s_mp_pad(a, dig + 1)) != MP_OKAY)
       
  4379     return res;
       
  4380 
       
  4381   DIGIT(a, dig) |= ((mp_digit)1 << bit);
       
  4382 
       
  4383   return MP_OKAY;
       
  4384 
       
  4385 } /* end s_mp_2expt() */
       
  4386 
       
  4387 /* }}} */
       
  4388 
       
  4389 /* {{{ s_mp_reduce(x, m, mu) */
       
  4390 
       
  4391 /*
       
  4392   Compute Barrett reduction, x (mod m), given a precomputed value for
       
  4393   mu = b^2k / m, where b = RADIX and k = #digits(m).  This should be
       
  4394   faster than straight division, when many reductions by the same
       
  4395   value of m are required (such as in modular exponentiation).  This
       
  4396   can nearly halve the time required to do modular exponentiation,
       
  4397   as compared to using the full integer divide to reduce.
       
  4398 
       
  4399   This algorithm was derived from the _Handbook of Applied
       
  4400   Cryptography_ by Menezes, Oorschot and VanStone, Ch. 14,
       
  4401   pp. 603-604.
       
  4402  */
       
  4403 
       
  4404 mp_err   s_mp_reduce(mp_int *x, const mp_int *m, const mp_int *mu)
       
  4405 {
       
  4406   mp_int   q;
       
  4407   mp_err   res;
       
  4408 
       
  4409   if((res = mp_init_copy(&q, x)) != MP_OKAY)
       
  4410     return res;
       
  4411 
       
  4412   s_mp_rshd(&q, USED(m) - 1);  /* q1 = x / b^(k-1)  */
       
  4413   s_mp_mul(&q, mu);            /* q2 = q1 * mu      */
       
  4414   s_mp_rshd(&q, USED(m) + 1);  /* q3 = q2 / b^(k+1) */
       
  4415 
       
  4416   /* x = x mod b^(k+1), quick (no division) */
       
  4417   s_mp_mod_2d(x, DIGIT_BIT * (USED(m) + 1));
       
  4418 
       
  4419   /* q = q * m mod b^(k+1), quick (no division) */
       
  4420   s_mp_mul(&q, m);
       
  4421   s_mp_mod_2d(&q, DIGIT_BIT * (USED(m) + 1));
       
  4422 
       
  4423   /* x = x - q */
       
  4424   if((res = mp_sub(x, &q, x)) != MP_OKAY)
       
  4425     goto CLEANUP;
       
  4426 
       
  4427   /* If x < 0, add b^(k+1) to it */
       
  4428   if(mp_cmp_z(x) < 0) {
       
  4429     mp_set(&q, 1);
       
  4430     if((res = s_mp_lshd(&q, USED(m) + 1)) != MP_OKAY)
       
  4431       goto CLEANUP;
       
  4432     if((res = mp_add(x, &q, x)) != MP_OKAY)
       
  4433       goto CLEANUP;
       
  4434   }
       
  4435 
       
  4436   /* Back off if it's too big */
       
  4437   while(mp_cmp(x, m) >= 0) {
       
  4438     if((res = s_mp_sub(x, m)) != MP_OKAY)
       
  4439       break;
       
  4440   }
       
  4441 
       
  4442  CLEANUP:
       
  4443   mp_clear(&q);
       
  4444 
       
  4445   return res;
       
  4446 
       
  4447 } /* end s_mp_reduce() */
       
  4448 
       
  4449 /* }}} */
       
  4450 
       
  4451 /* }}} */
       
  4452 
       
  4453 /* {{{ Primitive comparisons */
       
  4454 
       
  4455 /* {{{ s_mp_cmp(a, b) */
       
  4456 
       
  4457 /* Compare |a| <=> |b|, return 0 if equal, <0 if a<b, >0 if a>b           */
       
  4458 int      s_mp_cmp(const mp_int *a, const mp_int *b)
       
  4459 {
       
  4460   mp_size used_a = MP_USED(a);
       
  4461   {
       
  4462     mp_size used_b = MP_USED(b);
       
  4463 
       
  4464     if (used_a > used_b)
       
  4465       goto IS_GT;
       
  4466     if (used_a < used_b)
       
  4467       goto IS_LT;
       
  4468   }
       
  4469   {
       
  4470     mp_digit *pa, *pb;
       
  4471     mp_digit da = 0, db = 0;
       
  4472 
       
  4473 #define CMP_AB(n) if ((da = pa[n]) != (db = pb[n])) goto done
       
  4474 
       
  4475     pa = MP_DIGITS(a) + used_a;
       
  4476     pb = MP_DIGITS(b) + used_a;
       
  4477     while (used_a >= 4) {
       
  4478       pa     -= 4;
       
  4479       pb     -= 4;
       
  4480       used_a -= 4;
       
  4481       CMP_AB(3);
       
  4482       CMP_AB(2);
       
  4483       CMP_AB(1);
       
  4484       CMP_AB(0);
       
  4485     }
       
  4486     while (used_a-- > 0 && ((da = *--pa) == (db = *--pb)))
       
  4487       /* do nothing */;
       
  4488 done:
       
  4489     if (da > db)
       
  4490       goto IS_GT;
       
  4491     if (da < db)
       
  4492       goto IS_LT;
       
  4493   }
       
  4494   return MP_EQ;
       
  4495 IS_LT:
       
  4496   return MP_LT;
       
  4497 IS_GT:
       
  4498   return MP_GT;
       
  4499 } /* end s_mp_cmp() */
       
  4500 
       
  4501 /* }}} */
       
  4502 
       
  4503 /* {{{ s_mp_cmp_d(a, d) */
       
  4504 
       
  4505 /* Compare |a| <=> d, return 0 if equal, <0 if a<d, >0 if a>d             */
       
  4506 int      s_mp_cmp_d(const mp_int *a, mp_digit d)
       
  4507 {
       
  4508   if(USED(a) > 1)
       
  4509     return MP_GT;
       
  4510 
       
  4511   if(DIGIT(a, 0) < d)
       
  4512     return MP_LT;
       
  4513   else if(DIGIT(a, 0) > d)
       
  4514     return MP_GT;
       
  4515   else
       
  4516     return MP_EQ;
       
  4517 
       
  4518 } /* end s_mp_cmp_d() */
       
  4519 
       
  4520 /* }}} */
       
  4521 
       
  4522 /* {{{ s_mp_ispow2(v) */
       
  4523 
       
  4524 /*
       
  4525   Returns -1 if the value is not a power of two; otherwise, it returns
       
  4526   k such that v = 2^k, i.e. lg(v).
       
  4527  */
       
  4528 int      s_mp_ispow2(const mp_int *v)
       
  4529 {
       
  4530   mp_digit d;
       
  4531   int      extra = 0, ix;
       
  4532 
       
  4533   ix = MP_USED(v) - 1;
       
  4534   d = MP_DIGIT(v, ix); /* most significant digit of v */
       
  4535 
       
  4536   extra = s_mp_ispow2d(d);
       
  4537   if (extra < 0 || ix == 0)
       
  4538     return extra;
       
  4539 
       
  4540   while (--ix >= 0) {
       
  4541     if (DIGIT(v, ix) != 0)
       
  4542       return -1; /* not a power of two */
       
  4543     extra += MP_DIGIT_BIT;
       
  4544   }
       
  4545 
       
  4546   return extra;
       
  4547 
       
  4548 } /* end s_mp_ispow2() */
       
  4549 
       
  4550 /* }}} */
       
  4551 
       
  4552 /* {{{ s_mp_ispow2d(d) */
       
  4553 
       
  4554 int      s_mp_ispow2d(mp_digit d)
       
  4555 {
       
  4556   if ((d != 0) && ((d & (d-1)) == 0)) { /* d is a power of 2 */
       
  4557     int pow = 0;
       
  4558 #if defined (MP_USE_UINT_DIGIT)
       
  4559     if (d & 0xffff0000U)
       
  4560       pow += 16;
       
  4561     if (d & 0xff00ff00U)
       
  4562       pow += 8;
       
  4563     if (d & 0xf0f0f0f0U)
       
  4564       pow += 4;
       
  4565     if (d & 0xccccccccU)
       
  4566       pow += 2;
       
  4567     if (d & 0xaaaaaaaaU)
       
  4568       pow += 1;
       
  4569 #elif defined(MP_USE_LONG_LONG_DIGIT)
       
  4570     if (d & 0xffffffff00000000ULL)
       
  4571       pow += 32;
       
  4572     if (d & 0xffff0000ffff0000ULL)
       
  4573       pow += 16;
       
  4574     if (d & 0xff00ff00ff00ff00ULL)
       
  4575       pow += 8;
       
  4576     if (d & 0xf0f0f0f0f0f0f0f0ULL)
       
  4577       pow += 4;
       
  4578     if (d & 0xccccccccccccccccULL)
       
  4579       pow += 2;
       
  4580     if (d & 0xaaaaaaaaaaaaaaaaULL)
       
  4581       pow += 1;
       
  4582 #elif defined(MP_USE_LONG_DIGIT)
       
  4583     if (d & 0xffffffff00000000UL)
       
  4584       pow += 32;
       
  4585     if (d & 0xffff0000ffff0000UL)
       
  4586       pow += 16;
       
  4587     if (d & 0xff00ff00ff00ff00UL)
       
  4588       pow += 8;
       
  4589     if (d & 0xf0f0f0f0f0f0f0f0UL)
       
  4590       pow += 4;
       
  4591     if (d & 0xccccccccccccccccUL)
       
  4592       pow += 2;
       
  4593     if (d & 0xaaaaaaaaaaaaaaaaUL)
       
  4594       pow += 1;
       
  4595 #else
       
  4596 #error "unknown type for mp_digit"
       
  4597 #endif
       
  4598     return pow;
       
  4599   }
       
  4600   return -1;
       
  4601 
       
  4602 } /* end s_mp_ispow2d() */
       
  4603 
       
  4604 /* }}} */
       
  4605 
       
  4606 /* }}} */
       
  4607 
       
  4608 /* {{{ Primitive I/O helpers */
       
  4609 
       
  4610 /* {{{ s_mp_tovalue(ch, r) */
       
  4611 
       
  4612 /*
       
  4613   Convert the given character to its digit value, in the given radix.
       
  4614   If the given character is not understood in the given radix, -1 is
       
  4615   returned.  Otherwise the digit's numeric value is returned.
       
  4616 
       
  4617   The results will be odd if you use a radix < 2 or > 62, you are
       
  4618   expected to know what you're up to.
       
  4619  */
       
  4620 int      s_mp_tovalue(char ch, int r)
       
  4621 {
       
  4622   int    val, xch;
       
  4623 
       
  4624   if(r > 36)
       
  4625     xch = ch;
       
  4626   else
       
  4627     xch = toupper(ch);
       
  4628 
       
  4629   if(isdigit(xch))
       
  4630     val = xch - '0';
       
  4631   else if(isupper(xch))
       
  4632     val = xch - 'A' + 10;
       
  4633   else if(islower(xch))
       
  4634     val = xch - 'a' + 36;
       
  4635   else if(xch == '+')
       
  4636     val = 62;
       
  4637   else if(xch == '/')
       
  4638     val = 63;
       
  4639   else
       
  4640     return -1;
       
  4641 
       
  4642   if(val < 0 || val >= r)
       
  4643     return -1;
       
  4644 
       
  4645   return val;
       
  4646 
       
  4647 } /* end s_mp_tovalue() */
       
  4648 
       
  4649 /* }}} */
       
  4650 
       
  4651 /* {{{ s_mp_todigit(val, r, low) */
       
  4652 
       
  4653 /*
       
  4654   Convert val to a radix-r digit, if possible.  If val is out of range
       
  4655   for r, returns zero.  Otherwise, returns an ASCII character denoting
       
  4656   the value in the given radix.
       
  4657 
       
  4658   The results may be odd if you use a radix < 2 or > 64, you are
       
  4659   expected to know what you're doing.
       
  4660  */
       
  4661 
       
  4662 char     s_mp_todigit(mp_digit val, int r, int low)
       
  4663 {
       
  4664   char   ch;
       
  4665 
       
  4666   if(val >= r)
       
  4667     return 0;
       
  4668 
       
  4669   ch = s_dmap_1[val];
       
  4670 
       
  4671   if(r <= 36 && low)
       
  4672     ch = tolower(ch);
       
  4673 
       
  4674   return ch;
       
  4675 
       
  4676 } /* end s_mp_todigit() */
       
  4677 
       
  4678 /* }}} */
       
  4679 
       
  4680 /* {{{ s_mp_outlen(bits, radix) */
       
  4681 
       
  4682 /*
       
  4683    Return an estimate for how long a string is needed to hold a radix
       
  4684    r representation of a number with 'bits' significant bits, plus an
       
  4685    extra for a zero terminator (assuming C style strings here)
       
  4686  */
       
  4687 int      s_mp_outlen(int bits, int r)
       
  4688 {
       
  4689   return (int)((double)bits * LOG_V_2(r) + 1.5) + 1;
       
  4690 
       
  4691 } /* end s_mp_outlen() */
       
  4692 
       
  4693 /* }}} */
       
  4694 
       
  4695 /* }}} */
       
  4696 
       
  4697 /* {{{ mp_read_unsigned_octets(mp, str, len) */
       
  4698 /* mp_read_unsigned_octets(mp, str, len)
       
  4699    Read in a raw value (base 256) into the given mp_int
       
  4700    No sign bit, number is positive.  Leading zeros ignored.
       
  4701  */
       
  4702 
       
  4703 mp_err
       
  4704 mp_read_unsigned_octets(mp_int *mp, const unsigned char *str, mp_size len)
       
  4705 {
       
  4706   int            count;
       
  4707   mp_err         res;
       
  4708   mp_digit       d;
       
  4709 
       
  4710   ARGCHK(mp != NULL && str != NULL && len > 0, MP_BADARG);
       
  4711 
       
  4712   mp_zero(mp);
       
  4713 
       
  4714   count = len % sizeof(mp_digit);
       
  4715   if (count) {
       
  4716     for (d = 0; count-- > 0; --len) {
       
  4717       d = (d << 8) | *str++;
       
  4718     }
       
  4719     MP_DIGIT(mp, 0) = d;
       
  4720   }
       
  4721 
       
  4722   /* Read the rest of the digits */
       
  4723   for(; len > 0; len -= sizeof(mp_digit)) {
       
  4724     for (d = 0, count = sizeof(mp_digit); count > 0; --count) {
       
  4725       d = (d << 8) | *str++;
       
  4726     }
       
  4727     if (MP_EQ == mp_cmp_z(mp)) {
       
  4728       if (!d)
       
  4729         continue;
       
  4730     } else {
       
  4731       if((res = s_mp_lshd(mp, 1)) != MP_OKAY)
       
  4732         return res;
       
  4733     }
       
  4734     MP_DIGIT(mp, 0) = d;
       
  4735   }
       
  4736   return MP_OKAY;
       
  4737 } /* end mp_read_unsigned_octets() */
       
  4738 /* }}} */
       
  4739 
       
  4740 /* {{{ mp_unsigned_octet_size(mp) */
       
  4741 int
       
  4742 mp_unsigned_octet_size(const mp_int *mp)
       
  4743 {
       
  4744   int  bytes;
       
  4745   int  ix;
       
  4746   mp_digit  d = 0;
       
  4747 
       
  4748   ARGCHK(mp != NULL, MP_BADARG);
       
  4749   ARGCHK(MP_ZPOS == SIGN(mp), MP_BADARG);
       
  4750 
       
  4751   bytes = (USED(mp) * sizeof(mp_digit));
       
  4752 
       
  4753   /* subtract leading zeros. */
       
  4754   /* Iterate over each digit... */
       
  4755   for(ix = USED(mp) - 1; ix >= 0; ix--) {
       
  4756     d = DIGIT(mp, ix);
       
  4757     if (d)
       
  4758         break;
       
  4759     bytes -= sizeof(d);
       
  4760   }
       
  4761   if (!bytes)
       
  4762     return 1;
       
  4763 
       
  4764   /* Have MSD, check digit bytes, high order first */
       
  4765   for(ix = sizeof(mp_digit) - 1; ix >= 0; ix--) {
       
  4766     unsigned char x = (unsigned char)(d >> (ix * CHAR_BIT));
       
  4767     if (x)
       
  4768         break;
       
  4769     --bytes;
       
  4770   }
       
  4771   return bytes;
       
  4772 } /* end mp_unsigned_octet_size() */
       
  4773 /* }}} */
       
  4774 
       
  4775 /* {{{ mp_to_unsigned_octets(mp, str) */
       
  4776 /* output a buffer of big endian octets no longer than specified. */
       
  4777 mp_err
       
  4778 mp_to_unsigned_octets(const mp_int *mp, unsigned char *str, mp_size maxlen)
       
  4779 {
       
  4780   int  ix, pos = 0;
       
  4781   int  bytes;
       
  4782 
       
  4783   ARGCHK(mp != NULL && str != NULL && !SIGN(mp), MP_BADARG);
       
  4784 
       
  4785   bytes = mp_unsigned_octet_size(mp);
       
  4786   ARGCHK(bytes <= maxlen, MP_BADARG);
       
  4787 
       
  4788   /* Iterate over each digit... */
       
  4789   for(ix = USED(mp) - 1; ix >= 0; ix--) {
       
  4790     mp_digit  d = DIGIT(mp, ix);
       
  4791     int       jx;
       
  4792 
       
  4793     /* Unpack digit bytes, high order first */
       
  4794     for(jx = sizeof(mp_digit) - 1; jx >= 0; jx--) {
       
  4795       unsigned char x = (unsigned char)(d >> (jx * CHAR_BIT));
       
  4796       if (!pos && !x)   /* suppress leading zeros */
       
  4797         continue;
       
  4798       str[pos++] = x;
       
  4799     }
       
  4800   }
       
  4801   if (!pos)
       
  4802     str[pos++] = 0;
       
  4803   return pos;
       
  4804 } /* end mp_to_unsigned_octets() */
       
  4805 /* }}} */
       
  4806 
       
  4807 /* {{{ mp_to_signed_octets(mp, str) */
       
  4808 /* output a buffer of big endian octets no longer than specified. */
       
  4809 mp_err
       
  4810 mp_to_signed_octets(const mp_int *mp, unsigned char *str, mp_size maxlen)
       
  4811 {
       
  4812   int  ix, pos = 0;
       
  4813   int  bytes;
       
  4814 
       
  4815   ARGCHK(mp != NULL && str != NULL && !SIGN(mp), MP_BADARG);
       
  4816 
       
  4817   bytes = mp_unsigned_octet_size(mp);
       
  4818   ARGCHK(bytes <= maxlen, MP_BADARG);
       
  4819 
       
  4820   /* Iterate over each digit... */
       
  4821   for(ix = USED(mp) - 1; ix >= 0; ix--) {
       
  4822     mp_digit  d = DIGIT(mp, ix);
       
  4823     int       jx;
       
  4824 
       
  4825     /* Unpack digit bytes, high order first */
       
  4826     for(jx = sizeof(mp_digit) - 1; jx >= 0; jx--) {
       
  4827       unsigned char x = (unsigned char)(d >> (jx * CHAR_BIT));
       
  4828       if (!pos) {
       
  4829         if (!x)         /* suppress leading zeros */
       
  4830           continue;
       
  4831         if (x & 0x80) { /* add one leading zero to make output positive.  */
       
  4832           ARGCHK(bytes + 1 <= maxlen, MP_BADARG);
       
  4833           if (bytes + 1 > maxlen)
       
  4834             return MP_BADARG;
       
  4835           str[pos++] = 0;
       
  4836         }
       
  4837       }
       
  4838       str[pos++] = x;
       
  4839     }
       
  4840   }
       
  4841   if (!pos)
       
  4842     str[pos++] = 0;
       
  4843   return pos;
       
  4844 } /* end mp_to_signed_octets() */
       
  4845 /* }}} */
       
  4846 
       
  4847 /* {{{ mp_to_fixlen_octets(mp, str) */
       
  4848 /* output a buffer of big endian octets exactly as long as requested. */
       
  4849 mp_err
       
  4850 mp_to_fixlen_octets(const mp_int *mp, unsigned char *str, mp_size length)
       
  4851 {
       
  4852   int  ix, pos = 0;
       
  4853   int  bytes;
       
  4854 
       
  4855   ARGCHK(mp != NULL && str != NULL && !SIGN(mp), MP_BADARG);
       
  4856 
       
  4857   bytes = mp_unsigned_octet_size(mp);
       
  4858   ARGCHK(bytes <= length, MP_BADARG);
       
  4859 
       
  4860   /* place any needed leading zeros */
       
  4861   for (;length > bytes; --length) {
       
  4862         *str++ = 0;
       
  4863   }
       
  4864 
       
  4865   /* Iterate over each digit... */
       
  4866   for(ix = USED(mp) - 1; ix >= 0; ix--) {
       
  4867     mp_digit  d = DIGIT(mp, ix);
       
  4868     int       jx;
       
  4869 
       
  4870     /* Unpack digit bytes, high order first */
       
  4871     for(jx = sizeof(mp_digit) - 1; jx >= 0; jx--) {
       
  4872       unsigned char x = (unsigned char)(d >> (jx * CHAR_BIT));
       
  4873       if (!pos && !x)   /* suppress leading zeros */
       
  4874         continue;
       
  4875       str[pos++] = x;
       
  4876     }
       
  4877   }
       
  4878   if (!pos)
       
  4879     str[pos++] = 0;
       
  4880   return MP_OKAY;
       
  4881 } /* end mp_to_fixlen_octets() */
       
  4882 /* }}} */
       
  4883 
       
  4884 
       
  4885 /*------------------------------------------------------------------------*/
       
  4886 /* HERE THERE BE DRAGONS                                                  */