4272N/A/*
4272N/A * Copyright (c) 2007, 2011, Oracle and/or its affiliates. All rights reserved.
4272N/A * Use is subject to license terms.
4272N/A *
4272N/A * This library is free software; you can redistribute it and/or
4272N/A * modify it under the terms of the GNU Lesser General Public
4272N/A * License as published by the Free Software Foundation; either
4272N/A * version 2.1 of the License, or (at your option) any later version.
1674N/A *
4272N/A * This library is distributed in the hope that it will be useful,
4272N/A * but WITHOUT ANY WARRANTY; without even the implied warranty of
4272N/A * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
4272N/A * Lesser General Public License for more details.
1674N/A *
4272N/A * You should have received a copy of the GNU Lesser General Public License
4272N/A * along with this library; if not, write to the Free Software Foundation,
4272N/A * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
1674N/A *
4272N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
4272N/A * or visit www.oracle.com if you need additional information or have any
4272N/A * questions.
4272N/A */
4272N/A
4272N/A/* *********************************************************************
1674N/A *
1674N/A * The Original Code is the MPI Arbitrary Precision Integer Arithmetic library.
1674N/A *
1674N/A * The Initial Developer of the Original Code is
1674N/A * Michael J. Fromberger.
1674N/A * Portions created by the Initial Developer are Copyright (C) 1998
1674N/A * the Initial Developer. All Rights Reserved.
1674N/A *
1674N/A * Contributor(s):
1674N/A * Netscape Communications Corporation
1674N/A * Douglas Stebila <douglas@stebila.ca> of Sun Laboratories.
1674N/A *
1674N/A *********************************************************************** */
4272N/A
4272N/A/* Arbitrary precision integer arithmetic library */
1674N/A
1674N/A#include "mpi-priv.h"
1674N/A#if defined(OSF1)
1674N/A#include <c_asm.h>
1674N/A#endif
1674N/A
1674N/A#if MP_LOGTAB
1674N/A/*
1674N/A A table of the logs of 2 for various bases (the 0 and 1 entries of
1674N/A this table are meaningless and should not be referenced).
1674N/A
1674N/A This table is used to compute output lengths for the mp_toradix()
1674N/A function. Since a number n in radix r takes up about log_r(n)
1674N/A digits, we estimate the output size by taking the least integer
1674N/A greater than log_r(n), where:
1674N/A
1674N/A log_r(n) = log_2(n) * log_r(2)
1674N/A
1674N/A This table, therefore, is a table of log_r(2) for 2 <= r <= 36,
1674N/A which are the output bases supported.
1674N/A */
1674N/A#include "logtab.h"
1674N/A#endif
1674N/A
1674N/A/* {{{ Constant strings */
1674N/A
1674N/A/* Constant strings returned by mp_strerror() */
1674N/Astatic const char *mp_err_string[] = {
1674N/A "unknown result code", /* say what? */
1674N/A "boolean true", /* MP_OKAY, MP_YES */
1674N/A "boolean false", /* MP_NO */
1674N/A "out of memory", /* MP_MEM */
1674N/A "argument out of range", /* MP_RANGE */
1674N/A "invalid input parameter", /* MP_BADARG */
1674N/A "result is undefined" /* MP_UNDEF */
1674N/A};
1674N/A
1674N/A/* Value to digit maps for radix conversion */
1674N/A
1674N/A/* s_dmap_1 - standard digits and letters */
1674N/Astatic const char *s_dmap_1 =
1674N/A "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz+/";
1674N/A
1674N/A/* }}} */
1674N/A
1674N/Aunsigned long mp_allocs;
1674N/Aunsigned long mp_frees;
1674N/Aunsigned long mp_copies;
1674N/A
1674N/A/* {{{ Default precision manipulation */
1674N/A
1674N/A/* Default precision for newly created mp_int's */
1674N/Astatic mp_size s_mp_defprec = MP_DEFPREC;
1674N/A
1674N/Amp_size mp_get_prec(void)
1674N/A{
1674N/A return s_mp_defprec;
1674N/A
1674N/A} /* end mp_get_prec() */
1674N/A
1674N/Avoid mp_set_prec(mp_size prec)
1674N/A{
1674N/A if(prec == 0)
1674N/A s_mp_defprec = MP_DEFPREC;
1674N/A else
1674N/A s_mp_defprec = prec;
1674N/A
1674N/A} /* end mp_set_prec() */
1674N/A
1674N/A/* }}} */
1674N/A
1674N/A/*------------------------------------------------------------------------*/
1674N/A/* {{{ mp_init(mp, kmflag) */
1674N/A
1674N/A/*
1674N/A mp_init(mp, kmflag)
1674N/A
1674N/A Initialize a new zero-valued mp_int. Returns MP_OKAY if successful,
1674N/A MP_MEM if memory could not be allocated for the structure.
1674N/A */
1674N/A
1674N/Amp_err mp_init(mp_int *mp, int kmflag)
1674N/A{
1674N/A return mp_init_size(mp, s_mp_defprec, kmflag);
1674N/A
1674N/A} /* end mp_init() */
1674N/A
1674N/A/* }}} */
1674N/A
1674N/A/* {{{ mp_init_size(mp, prec, kmflag) */
1674N/A
1674N/A/*
1674N/A mp_init_size(mp, prec, kmflag)
1674N/A
1674N/A Initialize a new zero-valued mp_int with at least the given
1674N/A precision; returns MP_OKAY if successful, or MP_MEM if memory could
1674N/A not be allocated for the structure.
1674N/A */
1674N/A
1674N/Amp_err mp_init_size(mp_int *mp, mp_size prec, int kmflag)
1674N/A{
1674N/A ARGCHK(mp != NULL && prec > 0, MP_BADARG);
1674N/A
1674N/A prec = MP_ROUNDUP(prec, s_mp_defprec);
1674N/A if((DIGITS(mp) = s_mp_alloc(prec, sizeof(mp_digit), kmflag)) == NULL)
1674N/A return MP_MEM;
1674N/A
1674N/A SIGN(mp) = ZPOS;
1674N/A USED(mp) = 1;
1674N/A ALLOC(mp) = prec;
1674N/A
1674N/A return MP_OKAY;
1674N/A
1674N/A} /* end mp_init_size() */
1674N/A
1674N/A/* }}} */
1674N/A
1674N/A/* {{{ mp_init_copy(mp, from) */
1674N/A
1674N/A/*
1674N/A mp_init_copy(mp, from)
1674N/A
1674N/A Initialize mp as an exact copy of from. Returns MP_OKAY if
1674N/A successful, MP_MEM if memory could not be allocated for the new
1674N/A structure.
1674N/A */
1674N/A
1674N/Amp_err mp_init_copy(mp_int *mp, const mp_int *from)
1674N/A{
1674N/A ARGCHK(mp != NULL && from != NULL, MP_BADARG);
1674N/A
1674N/A if(mp == from)
1674N/A return MP_OKAY;
1674N/A
1674N/A if((DIGITS(mp) = s_mp_alloc(ALLOC(from), sizeof(mp_digit), FLAG(from))) == NULL)
1674N/A return MP_MEM;
1674N/A
1674N/A s_mp_copy(DIGITS(from), DIGITS(mp), USED(from));
1674N/A USED(mp) = USED(from);
1674N/A ALLOC(mp) = ALLOC(from);
1674N/A SIGN(mp) = SIGN(from);
1674N/A
1674N/A#ifndef _WIN32
1674N/A FLAG(mp) = FLAG(from);
1674N/A#endif /* _WIN32 */
1674N/A
1674N/A return MP_OKAY;
1674N/A
1674N/A} /* end mp_init_copy() */
1674N/A
1674N/A/* }}} */
1674N/A
1674N/A/* {{{ mp_copy(from, to) */
1674N/A
1674N/A/*
1674N/A mp_copy(from, to)
1674N/A
1674N/A Copies the mp_int 'from' to the mp_int 'to'. It is presumed that
1674N/A 'to' has already been initialized (if not, use mp_init_copy()
1674N/A instead). If 'from' and 'to' are identical, nothing happens.
1674N/A */
1674N/A
1674N/Amp_err mp_copy(const mp_int *from, mp_int *to)
1674N/A{
1674N/A ARGCHK(from != NULL && to != NULL, MP_BADARG);
1674N/A
1674N/A if(from == to)
1674N/A return MP_OKAY;
1674N/A
1674N/A ++mp_copies;
1674N/A { /* copy */
1674N/A mp_digit *tmp;
1674N/A
1674N/A /*
1674N/A If the allocated buffer in 'to' already has enough space to hold
1674N/A all the used digits of 'from', we'll re-use it to avoid hitting
1674N/A the memory allocater more than necessary; otherwise, we'd have
1674N/A to grow anyway, so we just allocate a hunk and make the copy as
1674N/A usual
1674N/A */
1674N/A if(ALLOC(to) >= USED(from)) {
1674N/A s_mp_setz(DIGITS(to) + USED(from), ALLOC(to) - USED(from));
1674N/A s_mp_copy(DIGITS(from), DIGITS(to), USED(from));
1674N/A
1674N/A } else {
1674N/A if((tmp = s_mp_alloc(ALLOC(from), sizeof(mp_digit), FLAG(from))) == NULL)
1674N/A return MP_MEM;
1674N/A
1674N/A s_mp_copy(DIGITS(from), tmp, USED(from));
1674N/A
1674N/A if(DIGITS(to) != NULL) {
1674N/A#if MP_CRYPTO
1674N/A s_mp_setz(DIGITS(to), ALLOC(to));
1674N/A#endif
1674N/A s_mp_free(DIGITS(to), ALLOC(to));
1674N/A }
1674N/A
1674N/A DIGITS(to) = tmp;
1674N/A ALLOC(to) = ALLOC(from);
1674N/A }
1674N/A
1674N/A /* Copy the precision and sign from the original */
1674N/A USED(to) = USED(from);
1674N/A SIGN(to) = SIGN(from);
1674N/A } /* end copy */
1674N/A
1674N/A return MP_OKAY;
1674N/A
1674N/A} /* end mp_copy() */
1674N/A
1674N/A/* }}} */
1674N/A
1674N/A/* {{{ mp_exch(mp1, mp2) */
1674N/A
1674N/A/*
1674N/A mp_exch(mp1, mp2)
1674N/A
1674N/A Exchange mp1 and mp2 without allocating any intermediate memory
1674N/A (well, unless you count the stack space needed for this call and the
1674N/A locals it creates...). This cannot fail.
1674N/A */
1674N/A
1674N/Avoid mp_exch(mp_int *mp1, mp_int *mp2)
1674N/A{
1674N/A#if MP_ARGCHK == 2
1674N/A assert(mp1 != NULL && mp2 != NULL);
1674N/A#else
1674N/A if(mp1 == NULL || mp2 == NULL)
1674N/A return;
1674N/A#endif
1674N/A
1674N/A s_mp_exch(mp1, mp2);
1674N/A
1674N/A} /* end mp_exch() */
1674N/A
1674N/A/* }}} */
1674N/A
1674N/A/* {{{ mp_clear(mp) */
1674N/A
1674N/A/*
1674N/A mp_clear(mp)
1674N/A
1674N/A Release the storage used by an mp_int, and void its fields so that
1674N/A if someone calls mp_clear() again for the same int later, we won't
1674N/A get tollchocked.
1674N/A */
1674N/A
1674N/Avoid mp_clear(mp_int *mp)
1674N/A{
1674N/A if(mp == NULL)
1674N/A return;
1674N/A
1674N/A if(DIGITS(mp) != NULL) {
1674N/A#if MP_CRYPTO
1674N/A s_mp_setz(DIGITS(mp), ALLOC(mp));
1674N/A#endif
1674N/A s_mp_free(DIGITS(mp), ALLOC(mp));
1674N/A DIGITS(mp) = NULL;
1674N/A }
1674N/A
1674N/A USED(mp) = 0;
1674N/A ALLOC(mp) = 0;
1674N/A
1674N/A} /* end mp_clear() */
1674N/A
1674N/A/* }}} */
1674N/A
1674N/A/* {{{ mp_zero(mp) */
1674N/A
1674N/A/*
1674N/A mp_zero(mp)
1674N/A
1674N/A Set mp to zero. Does not change the allocated size of the structure,
1674N/A and therefore cannot fail (except on a bad argument, which we ignore)
1674N/A */
1674N/Avoid mp_zero(mp_int *mp)
1674N/A{
1674N/A if(mp == NULL)
1674N/A return;
1674N/A
1674N/A s_mp_setz(DIGITS(mp), ALLOC(mp));
1674N/A USED(mp) = 1;
1674N/A SIGN(mp) = ZPOS;
1674N/A
1674N/A} /* end mp_zero() */
1674N/A
1674N/A/* }}} */
1674N/A
1674N/A/* {{{ mp_set(mp, d) */
1674N/A
1674N/Avoid mp_set(mp_int *mp, mp_digit d)
1674N/A{
1674N/A if(mp == NULL)
1674N/A return;
1674N/A
1674N/A mp_zero(mp);
1674N/A DIGIT(mp, 0) = d;
1674N/A
1674N/A} /* end mp_set() */
1674N/A
1674N/A/* }}} */
1674N/A
1674N/A/* {{{ mp_set_int(mp, z) */
1674N/A
1674N/Amp_err mp_set_int(mp_int *mp, long z)
1674N/A{
1674N/A int ix;
1674N/A unsigned long v = labs(z);
1674N/A mp_err res;
1674N/A
1674N/A ARGCHK(mp != NULL, MP_BADARG);
1674N/A
1674N/A mp_zero(mp);
1674N/A if(z == 0)
1674N/A return MP_OKAY; /* shortcut for zero */
1674N/A
1674N/A if (sizeof v <= sizeof(mp_digit)) {
1674N/A DIGIT(mp,0) = v;
1674N/A } else {
1674N/A for (ix = sizeof(long) - 1; ix >= 0; ix--) {
1674N/A if ((res = s_mp_mul_d(mp, (UCHAR_MAX + 1))) != MP_OKAY)
1674N/A return res;
1674N/A
1674N/A res = s_mp_add_d(mp, (mp_digit)((v >> (ix * CHAR_BIT)) & UCHAR_MAX));
1674N/A if (res != MP_OKAY)
1674N/A return res;
1674N/A }
1674N/A }
1674N/A if(z < 0)
1674N/A SIGN(mp) = NEG;
1674N/A
1674N/A return MP_OKAY;
1674N/A
1674N/A} /* end mp_set_int() */
1674N/A
1674N/A/* }}} */
1674N/A
1674N/A/* {{{ mp_set_ulong(mp, z) */
1674N/A
1674N/Amp_err mp_set_ulong(mp_int *mp, unsigned long z)
1674N/A{
1674N/A int ix;
1674N/A mp_err res;
1674N/A
1674N/A ARGCHK(mp != NULL, MP_BADARG);
1674N/A
1674N/A mp_zero(mp);
1674N/A if(z == 0)
1674N/A return MP_OKAY; /* shortcut for zero */
1674N/A
1674N/A if (sizeof z <= sizeof(mp_digit)) {
1674N/A DIGIT(mp,0) = z;
1674N/A } else {
1674N/A for (ix = sizeof(long) - 1; ix >= 0; ix--) {
1674N/A if ((res = s_mp_mul_d(mp, (UCHAR_MAX + 1))) != MP_OKAY)
1674N/A return res;
1674N/A
1674N/A res = s_mp_add_d(mp, (mp_digit)((z >> (ix * CHAR_BIT)) & UCHAR_MAX));
1674N/A if (res != MP_OKAY)
1674N/A return res;
1674N/A }
1674N/A }
1674N/A return MP_OKAY;
1674N/A} /* end mp_set_ulong() */
1674N/A
1674N/A/* }}} */
1674N/A
1674N/A/*------------------------------------------------------------------------*/
1674N/A/* {{{ Digit arithmetic */
1674N/A
1674N/A/* {{{ mp_add_d(a, d, b) */
1674N/A
1674N/A/*
1674N/A mp_add_d(a, d, b)
1674N/A
1674N/A Compute the sum b = a + d, for a single digit d. Respects the sign of
1674N/A its primary addend (single digits are unsigned anyway).
1674N/A */
1674N/A
1674N/Amp_err mp_add_d(const mp_int *a, mp_digit d, mp_int *b)
1674N/A{
1674N/A mp_int tmp;
1674N/A mp_err res;
1674N/A
1674N/A ARGCHK(a != NULL && b != NULL, MP_BADARG);
1674N/A
1674N/A if((res = mp_init_copy(&tmp, a)) != MP_OKAY)
1674N/A return res;
1674N/A
1674N/A if(SIGN(&tmp) == ZPOS) {
1674N/A if((res = s_mp_add_d(&tmp, d)) != MP_OKAY)
1674N/A goto CLEANUP;
1674N/A } else if(s_mp_cmp_d(&tmp, d) >= 0) {
1674N/A if((res = s_mp_sub_d(&tmp, d)) != MP_OKAY)
1674N/A goto CLEANUP;
1674N/A } else {
1674N/A mp_neg(&tmp, &tmp);
1674N/A
1674N/A DIGIT(&tmp, 0) = d - DIGIT(&tmp, 0);
1674N/A }
1674N/A
1674N/A if(s_mp_cmp_d(&tmp, 0) == 0)
1674N/A SIGN(&tmp) = ZPOS;
1674N/A
1674N/A s_mp_exch(&tmp, b);
1674N/A
1674N/ACLEANUP:
1674N/A mp_clear(&tmp);
1674N/A return res;
1674N/A
1674N/A} /* end mp_add_d() */
1674N/A
1674N/A/* }}} */
1674N/A
1674N/A/* {{{ mp_sub_d(a, d, b) */
1674N/A
1674N/A/*
1674N/A mp_sub_d(a, d, b)
1674N/A
1674N/A Compute the difference b = a - d, for a single digit d. Respects the
1674N/A sign of its subtrahend (single digits are unsigned anyway).
1674N/A */
1674N/A
1674N/Amp_err mp_sub_d(const mp_int *a, mp_digit d, mp_int *b)
1674N/A{
1674N/A mp_int tmp;
1674N/A mp_err res;
1674N/A
1674N/A ARGCHK(a != NULL && b != NULL, MP_BADARG);
1674N/A
1674N/A if((res = mp_init_copy(&tmp, a)) != MP_OKAY)
1674N/A return res;
1674N/A
1674N/A if(SIGN(&tmp) == NEG) {
1674N/A if((res = s_mp_add_d(&tmp, d)) != MP_OKAY)
1674N/A goto CLEANUP;
1674N/A } else if(s_mp_cmp_d(&tmp, d) >= 0) {
1674N/A if((res = s_mp_sub_d(&tmp, d)) != MP_OKAY)
1674N/A goto CLEANUP;
1674N/A } else {
1674N/A mp_neg(&tmp, &tmp);
1674N/A
1674N/A DIGIT(&tmp, 0) = d - DIGIT(&tmp, 0);
1674N/A SIGN(&tmp) = NEG;
1674N/A }
1674N/A
1674N/A if(s_mp_cmp_d(&tmp, 0) == 0)
1674N/A SIGN(&tmp) = ZPOS;
1674N/A
1674N/A s_mp_exch(&tmp, b);
1674N/A
1674N/ACLEANUP:
1674N/A mp_clear(&tmp);
1674N/A return res;
1674N/A
1674N/A} /* end mp_sub_d() */
1674N/A
1674N/A/* }}} */
1674N/A
1674N/A/* {{{ mp_mul_d(a, d, b) */
1674N/A
1674N/A/*
1674N/A mp_mul_d(a, d, b)
1674N/A
1674N/A Compute the product b = a * d, for a single digit d. Respects the sign
1674N/A of its multiplicand (single digits are unsigned anyway)
1674N/A */
1674N/A
1674N/Amp_err mp_mul_d(const mp_int *a, mp_digit d, mp_int *b)
1674N/A{
1674N/A mp_err res;
1674N/A
1674N/A ARGCHK(a != NULL && b != NULL, MP_BADARG);
1674N/A
1674N/A if(d == 0) {
1674N/A mp_zero(b);
1674N/A return MP_OKAY;
1674N/A }
1674N/A
1674N/A if((res = mp_copy(a, b)) != MP_OKAY)
1674N/A return res;
1674N/A
1674N/A res = s_mp_mul_d(b, d);
1674N/A
1674N/A return res;
1674N/A
1674N/A} /* end mp_mul_d() */
1674N/A
1674N/A/* }}} */
1674N/A
1674N/A/* {{{ mp_mul_2(a, c) */
1674N/A
1674N/Amp_err mp_mul_2(const mp_int *a, mp_int *c)
1674N/A{
1674N/A mp_err res;
1674N/A
1674N/A ARGCHK(a != NULL && c != NULL, MP_BADARG);
1674N/A
1674N/A if((res = mp_copy(a, c)) != MP_OKAY)
1674N/A return res;
1674N/A
1674N/A return s_mp_mul_2(c);
1674N/A
1674N/A} /* end mp_mul_2() */
1674N/A
1674N/A/* }}} */
1674N/A
1674N/A/* {{{ mp_div_d(a, d, q, r) */
1674N/A
1674N/A/*
1674N/A mp_div_d(a, d, q, r)
1674N/A
1674N/A Compute the quotient q = a / d and remainder r = a mod d, for a
1674N/A single digit d. Respects the sign of its divisor (single digits are
1674N/A unsigned anyway).
1674N/A */
1674N/A
1674N/Amp_err mp_div_d(const mp_int *a, mp_digit d, mp_int *q, mp_digit *r)
1674N/A{
1674N/A mp_err res;
1674N/A mp_int qp;
1674N/A mp_digit rem;
1674N/A int pow;
1674N/A
1674N/A ARGCHK(a != NULL, MP_BADARG);
1674N/A
1674N/A if(d == 0)
1674N/A return MP_RANGE;
1674N/A
1674N/A /* Shortcut for powers of two ... */
1674N/A if((pow = s_mp_ispow2d(d)) >= 0) {
1674N/A mp_digit mask;
1674N/A
1674N/A mask = ((mp_digit)1 << pow) - 1;
1674N/A rem = DIGIT(a, 0) & mask;
1674N/A
1674N/A if(q) {
1674N/A mp_copy(a, q);
1674N/A s_mp_div_2d(q, pow);
1674N/A }
1674N/A
1674N/A if(r)
1674N/A *r = rem;
1674N/A
1674N/A return MP_OKAY;
1674N/A }
1674N/A
1674N/A if((res = mp_init_copy(&qp, a)) != MP_OKAY)
1674N/A return res;
1674N/A
1674N/A res = s_mp_div_d(&qp, d, &rem);
1674N/A
1674N/A if(s_mp_cmp_d(&qp, 0) == 0)
1674N/A SIGN(q) = ZPOS;
1674N/A
1674N/A if(r)
1674N/A *r = rem;
1674N/A
1674N/A if(q)
1674N/A s_mp_exch(&qp, q);
1674N/A
1674N/A mp_clear(&qp);
1674N/A return res;
1674N/A
1674N/A} /* end mp_div_d() */
1674N/A
1674N/A/* }}} */
1674N/A
1674N/A/* {{{ mp_div_2(a, c) */
1674N/A
1674N/A/*
1674N/A mp_div_2(a, c)
1674N/A
1674N/A Compute c = a / 2, disregarding the remainder.
1674N/A */
1674N/A
1674N/Amp_err mp_div_2(const mp_int *a, mp_int *c)
1674N/A{
1674N/A mp_err res;
1674N/A
1674N/A ARGCHK(a != NULL && c != NULL, MP_BADARG);
1674N/A
1674N/A if((res = mp_copy(a, c)) != MP_OKAY)
1674N/A return res;
1674N/A
1674N/A s_mp_div_2(c);
1674N/A
1674N/A return MP_OKAY;
1674N/A
1674N/A} /* end mp_div_2() */
1674N/A
1674N/A/* }}} */
1674N/A
1674N/A/* {{{ mp_expt_d(a, d, b) */
1674N/A
1674N/Amp_err mp_expt_d(const mp_int *a, mp_digit d, mp_int *c)
1674N/A{
1674N/A mp_int s, x;
1674N/A mp_err res;
1674N/A
1674N/A ARGCHK(a != NULL && c != NULL, MP_BADARG);
1674N/A
1674N/A if((res = mp_init(&s, FLAG(a))) != MP_OKAY)
1674N/A return res;
1674N/A if((res = mp_init_copy(&x, a)) != MP_OKAY)
1674N/A goto X;
1674N/A
1674N/A DIGIT(&s, 0) = 1;
1674N/A
1674N/A while(d != 0) {
1674N/A if(d & 1) {
1674N/A if((res = s_mp_mul(&s, &x)) != MP_OKAY)
1674N/A goto CLEANUP;
1674N/A }
1674N/A
1674N/A d /= 2;
1674N/A
1674N/A if((res = s_mp_sqr(&x)) != MP_OKAY)
1674N/A goto CLEANUP;
1674N/A }
1674N/A
1674N/A s_mp_exch(&s, c);
1674N/A
1674N/ACLEANUP:
1674N/A mp_clear(&x);
1674N/AX:
1674N/A mp_clear(&s);
1674N/A
1674N/A return res;
1674N/A
1674N/A} /* end mp_expt_d() */
1674N/A
1674N/A/* }}} */
1674N/A
1674N/A/* }}} */
1674N/A
1674N/A/*------------------------------------------------------------------------*/
1674N/A/* {{{ Full arithmetic */
1674N/A
1674N/A/* {{{ mp_abs(a, b) */
1674N/A
1674N/A/*
1674N/A mp_abs(a, b)
1674N/A
1674N/A Compute b = |a|. 'a' and 'b' may be identical.
1674N/A */
1674N/A
1674N/Amp_err mp_abs(const mp_int *a, mp_int *b)
1674N/A{
1674N/A mp_err res;
1674N/A
1674N/A ARGCHK(a != NULL && b != NULL, MP_BADARG);
1674N/A
1674N/A if((res = mp_copy(a, b)) != MP_OKAY)
1674N/A return res;
1674N/A
1674N/A SIGN(b) = ZPOS;
1674N/A
1674N/A return MP_OKAY;
1674N/A
1674N/A} /* end mp_abs() */
1674N/A
1674N/A/* }}} */
1674N/A
1674N/A/* {{{ mp_neg(a, b) */
1674N/A
1674N/A/*
1674N/A mp_neg(a, b)
1674N/A
1674N/A Compute b = -a. 'a' and 'b' may be identical.
1674N/A */
1674N/A
1674N/Amp_err mp_neg(const mp_int *a, mp_int *b)
1674N/A{
1674N/A mp_err res;
1674N/A
1674N/A ARGCHK(a != NULL && b != NULL, MP_BADARG);
1674N/A
1674N/A if((res = mp_copy(a, b)) != MP_OKAY)
1674N/A return res;
1674N/A
1674N/A if(s_mp_cmp_d(b, 0) == MP_EQ)
1674N/A SIGN(b) = ZPOS;
1674N/A else
1674N/A SIGN(b) = (SIGN(b) == NEG) ? ZPOS : NEG;
1674N/A
1674N/A return MP_OKAY;
1674N/A
1674N/A} /* end mp_neg() */
1674N/A
1674N/A/* }}} */
1674N/A
1674N/A/* {{{ mp_add(a, b, c) */
1674N/A
1674N/A/*
1674N/A mp_add(a, b, c)
1674N/A
1674N/A Compute c = a + b. All parameters may be identical.
1674N/A */
1674N/A
1674N/Amp_err mp_add(const mp_int *a, const mp_int *b, mp_int *c)
1674N/A{
1674N/A mp_err res;
1674N/A
1674N/A ARGCHK(a != NULL && b != NULL && c != NULL, MP_BADARG);
1674N/A
1674N/A if(SIGN(a) == SIGN(b)) { /* same sign: add values, keep sign */
1674N/A MP_CHECKOK( s_mp_add_3arg(a, b, c) );
1674N/A } else if(s_mp_cmp(a, b) >= 0) { /* different sign: |a| >= |b| */
1674N/A MP_CHECKOK( s_mp_sub_3arg(a, b, c) );
1674N/A } else { /* different sign: |a| < |b| */
1674N/A MP_CHECKOK( s_mp_sub_3arg(b, a, c) );
1674N/A }
1674N/A
1674N/A if (s_mp_cmp_d(c, 0) == MP_EQ)
1674N/A SIGN(c) = ZPOS;
1674N/A
1674N/ACLEANUP:
1674N/A return res;
1674N/A
1674N/A} /* end mp_add() */
1674N/A
1674N/A/* }}} */
1674N/A
1674N/A/* {{{ mp_sub(a, b, c) */
1674N/A
1674N/A/*
1674N/A mp_sub(a, b, c)
1674N/A
1674N/A Compute c = a - b. All parameters may be identical.
1674N/A */
1674N/A
1674N/Amp_err mp_sub(const mp_int *a, const mp_int *b, mp_int *c)
1674N/A{
1674N/A mp_err res;
1674N/A int magDiff;
1674N/A
1674N/A ARGCHK(a != NULL && b != NULL && c != NULL, MP_BADARG);
1674N/A
1674N/A if (a == b) {
1674N/A mp_zero(c);
1674N/A return MP_OKAY;
1674N/A }
1674N/A
1674N/A if (MP_SIGN(a) != MP_SIGN(b)) {
1674N/A MP_CHECKOK( s_mp_add_3arg(a, b, c) );
1674N/A } else if (!(magDiff = s_mp_cmp(a, b))) {
1674N/A mp_zero(c);
1674N/A res = MP_OKAY;
1674N/A } else if (magDiff > 0) {
1674N/A MP_CHECKOK( s_mp_sub_3arg(a, b, c) );
1674N/A } else {
1674N/A MP_CHECKOK( s_mp_sub_3arg(b, a, c) );
1674N/A MP_SIGN(c) = !MP_SIGN(a);
1674N/A }
1674N/A
1674N/A if (s_mp_cmp_d(c, 0) == MP_EQ)
1674N/A MP_SIGN(c) = MP_ZPOS;
1674N/A
1674N/ACLEANUP:
1674N/A return res;
1674N/A
1674N/A} /* end mp_sub() */
1674N/A
1674N/A/* }}} */
1674N/A
1674N/A/* {{{ mp_mul(a, b, c) */
1674N/A
1674N/A/*
1674N/A mp_mul(a, b, c)
1674N/A
1674N/A Compute c = a * b. All parameters may be identical.
1674N/A */
1674N/Amp_err mp_mul(const mp_int *a, const mp_int *b, mp_int * c)
1674N/A{
1674N/A mp_digit *pb;
1674N/A mp_int tmp;
1674N/A mp_err res;
1674N/A mp_size ib;
1674N/A mp_size useda, usedb;
1674N/A
1674N/A ARGCHK(a != NULL && b != NULL && c != NULL, MP_BADARG);
1674N/A
1674N/A if (a == c) {
1674N/A if ((res = mp_init_copy(&tmp, a)) != MP_OKAY)
1674N/A return res;
1674N/A if (a == b)
1674N/A b = &tmp;
1674N/A a = &tmp;
1674N/A } else if (b == c) {
1674N/A if ((res = mp_init_copy(&tmp, b)) != MP_OKAY)
1674N/A return res;
1674N/A b = &tmp;
1674N/A } else {
1674N/A MP_DIGITS(&tmp) = 0;
1674N/A }
1674N/A
1674N/A if (MP_USED(a) < MP_USED(b)) {
1674N/A const mp_int *xch = b; /* switch a and b, to do fewer outer loops */
1674N/A b = a;
1674N/A a = xch;
1674N/A }
1674N/A
1674N/A MP_USED(c) = 1; MP_DIGIT(c, 0) = 0;
1674N/A if((res = s_mp_pad(c, USED(a) + USED(b))) != MP_OKAY)
1674N/A goto CLEANUP;
1674N/A
1674N/A#ifdef NSS_USE_COMBA
1674N/A if ((MP_USED(a) == MP_USED(b)) && IS_POWER_OF_2(MP_USED(b))) {
1674N/A if (MP_USED(a) == 4) {
1674N/A s_mp_mul_comba_4(a, b, c);
1674N/A goto CLEANUP;
1674N/A }
1674N/A if (MP_USED(a) == 8) {
1674N/A s_mp_mul_comba_8(a, b, c);
1674N/A goto CLEANUP;
1674N/A }
1674N/A if (MP_USED(a) == 16) {
1674N/A s_mp_mul_comba_16(a, b, c);
1674N/A goto CLEANUP;
1674N/A }
1674N/A if (MP_USED(a) == 32) {
1674N/A s_mp_mul_comba_32(a, b, c);
1674N/A goto CLEANUP;
1674N/A }
1674N/A }
1674N/A#endif
1674N/A
1674N/A pb = MP_DIGITS(b);
1674N/A s_mpv_mul_d(MP_DIGITS(a), MP_USED(a), *pb++, MP_DIGITS(c));
1674N/A
1674N/A /* Outer loop: Digits of b */
1674N/A useda = MP_USED(a);
1674N/A usedb = MP_USED(b);
1674N/A for (ib = 1; ib < usedb; ib++) {
1674N/A mp_digit b_i = *pb++;
1674N/A
1674N/A /* Inner product: Digits of a */
1674N/A if (b_i)
1674N/A s_mpv_mul_d_add(MP_DIGITS(a), useda, b_i, MP_DIGITS(c) + ib);
1674N/A else
1674N/A MP_DIGIT(c, ib + useda) = b_i;
1674N/A }
1674N/A
1674N/A s_mp_clamp(c);
1674N/A
1674N/A if(SIGN(a) == SIGN(b) || s_mp_cmp_d(c, 0) == MP_EQ)
1674N/A SIGN(c) = ZPOS;
1674N/A else
1674N/A SIGN(c) = NEG;
1674N/A
1674N/ACLEANUP:
1674N/A mp_clear(&tmp);
1674N/A return res;
1674N/A} /* end mp_mul() */
1674N/A
1674N/A/* }}} */
1674N/A
1674N/A/* {{{ mp_sqr(a, sqr) */
1674N/A
1674N/A#if MP_SQUARE
1674N/A/*
1674N/A Computes the square of a. This can be done more
1674N/A efficiently than a general multiplication, because many of the
1674N/A computation steps are redundant when squaring. The inner product
1674N/A step is a bit more complicated, but we save a fair number of
1674N/A iterations of the multiplication loop.
1674N/A */
1674N/A
1674N/A/* sqr = a^2; Caller provides both a and tmp; */
1674N/Amp_err mp_sqr(const mp_int *a, mp_int *sqr)
1674N/A{
1674N/A mp_digit *pa;
1674N/A mp_digit d;
1674N/A mp_err res;
1674N/A mp_size ix;
1674N/A mp_int tmp;
1674N/A int count;
1674N/A
1674N/A ARGCHK(a != NULL && sqr != NULL, MP_BADARG);
1674N/A
1674N/A if (a == sqr) {
1674N/A if((res = mp_init_copy(&tmp, a)) != MP_OKAY)
1674N/A return res;
1674N/A a = &tmp;
1674N/A } else {
1674N/A DIGITS(&tmp) = 0;
1674N/A res = MP_OKAY;
1674N/A }
1674N/A
1674N/A ix = 2 * MP_USED(a);
1674N/A if (ix > MP_ALLOC(sqr)) {
1674N/A MP_USED(sqr) = 1;
1674N/A MP_CHECKOK( s_mp_grow(sqr, ix) );
1674N/A }
1674N/A MP_USED(sqr) = ix;
1674N/A MP_DIGIT(sqr, 0) = 0;
1674N/A
1674N/A#ifdef NSS_USE_COMBA
1674N/A if (IS_POWER_OF_2(MP_USED(a))) {
1674N/A if (MP_USED(a) == 4) {
1674N/A s_mp_sqr_comba_4(a, sqr);
1674N/A goto CLEANUP;
1674N/A }
1674N/A if (MP_USED(a) == 8) {
1674N/A s_mp_sqr_comba_8(a, sqr);
1674N/A goto CLEANUP;
1674N/A }
1674N/A if (MP_USED(a) == 16) {
1674N/A s_mp_sqr_comba_16(a, sqr);
1674N/A goto CLEANUP;
1674N/A }
1674N/A if (MP_USED(a) == 32) {
1674N/A s_mp_sqr_comba_32(a, sqr);
1674N/A goto CLEANUP;
1674N/A }
1674N/A }
1674N/A#endif
1674N/A
1674N/A pa = MP_DIGITS(a);
1674N/A count = MP_USED(a) - 1;
1674N/A if (count > 0) {
1674N/A d = *pa++;
1674N/A s_mpv_mul_d(pa, count, d, MP_DIGITS(sqr) + 1);
1674N/A for (ix = 3; --count > 0; ix += 2) {
1674N/A d = *pa++;
1674N/A s_mpv_mul_d_add(pa, count, d, MP_DIGITS(sqr) + ix);
1674N/A } /* for(ix ...) */
1674N/A MP_DIGIT(sqr, MP_USED(sqr)-1) = 0; /* above loop stopped short of this. */
1674N/A
1674N/A /* now sqr *= 2 */
1674N/A s_mp_mul_2(sqr);
1674N/A } else {
1674N/A MP_DIGIT(sqr, 1) = 0;
1674N/A }
1674N/A
1674N/A /* now add the squares of the digits of a to sqr. */
1674N/A s_mpv_sqr_add_prop(MP_DIGITS(a), MP_USED(a), MP_DIGITS(sqr));
1674N/A
1674N/A SIGN(sqr) = ZPOS;
1674N/A s_mp_clamp(sqr);
1674N/A
1674N/ACLEANUP:
1674N/A mp_clear(&tmp);
1674N/A return res;
1674N/A
1674N/A} /* end mp_sqr() */
1674N/A#endif
1674N/A
1674N/A/* }}} */
1674N/A
1674N/A/* {{{ mp_div(a, b, q, r) */
1674N/A
1674N/A/*
1674N/A mp_div(a, b, q, r)
1674N/A
1674N/A Compute q = a / b and r = a mod b. Input parameters may be re-used
1674N/A as output parameters. If q or r is NULL, that portion of the
1674N/A computation will be discarded (although it will still be computed)
1674N/A */
1674N/Amp_err mp_div(const mp_int *a, const mp_int *b, mp_int *q, mp_int *r)
1674N/A{
1674N/A mp_err res;
1674N/A mp_int *pQ, *pR;
1674N/A mp_int qtmp, rtmp, btmp;
1674N/A int cmp;
1674N/A mp_sign signA;
1674N/A mp_sign signB;
1674N/A
1674N/A ARGCHK(a != NULL && b != NULL, MP_BADARG);
1674N/A
1674N/A signA = MP_SIGN(a);
1674N/A signB = MP_SIGN(b);
1674N/A
1674N/A if(mp_cmp_z(b) == MP_EQ)
1674N/A return MP_RANGE;
1674N/A
1674N/A DIGITS(&qtmp) = 0;
1674N/A DIGITS(&rtmp) = 0;
1674N/A DIGITS(&btmp) = 0;
1674N/A
1674N/A /* Set up some temporaries... */
1674N/A if (!r || r == a || r == b) {
1674N/A MP_CHECKOK( mp_init_copy(&rtmp, a) );
1674N/A pR = &rtmp;
1674N/A } else {
1674N/A MP_CHECKOK( mp_copy(a, r) );
1674N/A pR = r;
1674N/A }
1674N/A
1674N/A if (!q || q == a || q == b) {
1674N/A MP_CHECKOK( mp_init_size(&qtmp, MP_USED(a), FLAG(a)) );
1674N/A pQ = &qtmp;
1674N/A } else {
1674N/A MP_CHECKOK( s_mp_pad(q, MP_USED(a)) );
1674N/A pQ = q;
1674N/A mp_zero(pQ);
1674N/A }
1674N/A
1674N/A /*
1674N/A If |a| <= |b|, we can compute the solution without division;
1674N/A otherwise, we actually do the work required.
1674N/A */
1674N/A if ((cmp = s_mp_cmp(a, b)) <= 0) {
1674N/A if (cmp) {
1674N/A /* r was set to a above. */
1674N/A mp_zero(pQ);
1674N/A } else {
1674N/A mp_set(pQ, 1);
1674N/A mp_zero(pR);
1674N/A }
1674N/A } else {
1674N/A MP_CHECKOK( mp_init_copy(&btmp, b) );
1674N/A MP_CHECKOK( s_mp_div(pR, &btmp, pQ) );
1674N/A }
1674N/A
1674N/A /* Compute the signs for the output */
1674N/A MP_SIGN(pR) = signA; /* Sr = Sa */
1674N/A /* Sq = ZPOS if Sa == Sb */ /* Sq = NEG if Sa != Sb */
1674N/A MP_SIGN(pQ) = (signA == signB) ? ZPOS : NEG;
1674N/A
1674N/A if(s_mp_cmp_d(pQ, 0) == MP_EQ)
1674N/A SIGN(pQ) = ZPOS;
1674N/A if(s_mp_cmp_d(pR, 0) == MP_EQ)
1674N/A SIGN(pR) = ZPOS;
1674N/A
1674N/A /* Copy output, if it is needed */
1674N/A if(q && q != pQ)
1674N/A s_mp_exch(pQ, q);
1674N/A
1674N/A if(r && r != pR)
1674N/A s_mp_exch(pR, r);
1674N/A
1674N/ACLEANUP:
1674N/A mp_clear(&btmp);
1674N/A mp_clear(&rtmp);
1674N/A mp_clear(&qtmp);
1674N/A
1674N/A return res;
1674N/A
1674N/A} /* end mp_div() */
1674N/A
1674N/A/* }}} */
1674N/A
1674N/A/* {{{ mp_div_2d(a, d, q, r) */
1674N/A
1674N/Amp_err mp_div_2d(const mp_int *a, mp_digit d, mp_int *q, mp_int *r)
1674N/A{
1674N/A mp_err res;
1674N/A
1674N/A ARGCHK(a != NULL, MP_BADARG);
1674N/A
1674N/A if(q) {
1674N/A if((res = mp_copy(a, q)) != MP_OKAY)
1674N/A return res;
1674N/A }
1674N/A if(r) {
1674N/A if((res = mp_copy(a, r)) != MP_OKAY)
1674N/A return res;
1674N/A }
1674N/A if(q) {
1674N/A s_mp_div_2d(q, d);
1674N/A }
1674N/A if(r) {
1674N/A s_mp_mod_2d(r, d);
1674N/A }
1674N/A
1674N/A return MP_OKAY;
1674N/A
1674N/A} /* end mp_div_2d() */
1674N/A
1674N/A/* }}} */
1674N/A
1674N/A/* {{{ mp_expt(a, b, c) */
1674N/A
1674N/A/*
1674N/A mp_expt(a, b, c)
1674N/A
1674N/A Compute c = a ** b, that is, raise a to the b power. Uses a
1674N/A standard iterative square-and-multiply technique.
1674N/A */
1674N/A
1674N/Amp_err mp_expt(mp_int *a, mp_int *b, mp_int *c)
1674N/A{
1674N/A mp_int s, x;
1674N/A mp_err res;
1674N/A mp_digit d;
3488N/A unsigned int dig, bit;
1674N/A
1674N/A ARGCHK(a != NULL && b != NULL && c != NULL, MP_BADARG);
1674N/A
1674N/A if(mp_cmp_z(b) < 0)
1674N/A return MP_RANGE;
1674N/A
1674N/A if((res = mp_init(&s, FLAG(a))) != MP_OKAY)
1674N/A return res;
1674N/A
1674N/A mp_set(&s, 1);
1674N/A
1674N/A if((res = mp_init_copy(&x, a)) != MP_OKAY)
1674N/A goto X;
1674N/A
1674N/A /* Loop over low-order digits in ascending order */
1674N/A for(dig = 0; dig < (USED(b) - 1); dig++) {
1674N/A d = DIGIT(b, dig);
1674N/A
1674N/A /* Loop over bits of each non-maximal digit */
1674N/A for(bit = 0; bit < DIGIT_BIT; bit++) {
1674N/A if(d & 1) {
1674N/A if((res = s_mp_mul(&s, &x)) != MP_OKAY)
1674N/A goto CLEANUP;
1674N/A }
1674N/A
1674N/A d >>= 1;
1674N/A
1674N/A if((res = s_mp_sqr(&x)) != MP_OKAY)
1674N/A goto CLEANUP;
1674N/A }
1674N/A }
1674N/A
1674N/A /* Consider now the last digit... */
1674N/A d = DIGIT(b, dig);
1674N/A
1674N/A while(d) {
1674N/A if(d & 1) {
1674N/A if((res = s_mp_mul(&s, &x)) != MP_OKAY)
1674N/A goto CLEANUP;
1674N/A }
1674N/A
1674N/A d >>= 1;
1674N/A
1674N/A if((res = s_mp_sqr(&x)) != MP_OKAY)
1674N/A goto CLEANUP;
1674N/A }
1674N/A
1674N/A if(mp_iseven(b))
1674N/A SIGN(&s) = SIGN(a);
1674N/A
1674N/A res = mp_copy(&s, c);
1674N/A
1674N/ACLEANUP:
1674N/A mp_clear(&x);
1674N/AX:
1674N/A mp_clear(&s);
1674N/A
1674N/A return res;
1674N/A
1674N/A} /* end mp_expt() */
1674N/A
1674N/A/* }}} */
1674N/A
1674N/A/* {{{ mp_2expt(a, k) */
1674N/A
1674N/A/* Compute a = 2^k */
1674N/A
1674N/Amp_err mp_2expt(mp_int *a, mp_digit k)
1674N/A{
1674N/A ARGCHK(a != NULL, MP_BADARG);
1674N/A
1674N/A return s_mp_2expt(a, k);
1674N/A
1674N/A} /* end mp_2expt() */
1674N/A
1674N/A/* }}} */
1674N/A
1674N/A/* {{{ mp_mod(a, m, c) */
1674N/A
1674N/A/*
1674N/A mp_mod(a, m, c)
1674N/A
1674N/A Compute c = a (mod m). Result will always be 0 <= c < m.
1674N/A */
1674N/A
1674N/Amp_err mp_mod(const mp_int *a, const mp_int *m, mp_int *c)
1674N/A{
1674N/A mp_err res;
1674N/A int mag;
1674N/A
1674N/A ARGCHK(a != NULL && m != NULL && c != NULL, MP_BADARG);
1674N/A
1674N/A if(SIGN(m) == NEG)
1674N/A return MP_RANGE;
1674N/A
1674N/A /*
1674N/A If |a| > m, we need to divide to get the remainder and take the
1674N/A absolute value.
1674N/A
1674N/A If |a| < m, we don't need to do any division, just copy and adjust
1674N/A the sign (if a is negative).
1674N/A
1674N/A If |a| == m, we can simply set the result to zero.
1674N/A
1674N/A This order is intended to minimize the average path length of the
1674N/A comparison chain on common workloads -- the most frequent cases are
1674N/A that |a| != m, so we do those first.
1674N/A */
1674N/A if((mag = s_mp_cmp(a, m)) > 0) {
1674N/A if((res = mp_div(a, m, NULL, c)) != MP_OKAY)
1674N/A return res;
1674N/A
1674N/A if(SIGN(c) == NEG) {
1674N/A if((res = mp_add(c, m, c)) != MP_OKAY)
1674N/A return res;
1674N/A }
1674N/A
1674N/A } else if(mag < 0) {
1674N/A if((res = mp_copy(a, c)) != MP_OKAY)
1674N/A return res;
1674N/A
1674N/A if(mp_cmp_z(a) < 0) {
1674N/A if((res = mp_add(c, m, c)) != MP_OKAY)
1674N/A return res;
1674N/A
1674N/A }
1674N/A
1674N/A } else {
1674N/A mp_zero(c);
1674N/A
1674N/A }
1674N/A
1674N/A return MP_OKAY;
1674N/A
1674N/A} /* end mp_mod() */
1674N/A
1674N/A/* }}} */
1674N/A
1674N/A/* {{{ mp_mod_d(a, d, c) */
1674N/A
1674N/A/*
1674N/A mp_mod_d(a, d, c)
1674N/A
1674N/A Compute c = a (mod d). Result will always be 0 <= c < d
1674N/A */
1674N/Amp_err mp_mod_d(const mp_int *a, mp_digit d, mp_digit *c)
1674N/A{
1674N/A mp_err res;
1674N/A mp_digit rem;
1674N/A
1674N/A ARGCHK(a != NULL && c != NULL, MP_BADARG);
1674N/A
1674N/A if(s_mp_cmp_d(a, d) > 0) {
1674N/A if((res = mp_div_d(a, d, NULL, &rem)) != MP_OKAY)
1674N/A return res;
1674N/A
1674N/A } else {
1674N/A if(SIGN(a) == NEG)
1674N/A rem = d - DIGIT(a, 0);
1674N/A else
1674N/A rem = DIGIT(a, 0);
1674N/A }
1674N/A
1674N/A if(c)
1674N/A *c = rem;
1674N/A
1674N/A return MP_OKAY;
1674N/A
1674N/A} /* end mp_mod_d() */
1674N/A
1674N/A/* }}} */
1674N/A
1674N/A/* {{{ mp_sqrt(a, b) */
1674N/A
1674N/A/*
1674N/A mp_sqrt(a, b)
1674N/A
1674N/A Compute the integer square root of a, and store the result in b.
1674N/A Uses an integer-arithmetic version of Newton's iterative linear
1674N/A approximation technique to determine this value; the result has the
1674N/A following two properties:
1674N/A
1674N/A b^2 <= a
1674N/A (b+1)^2 >= a
1674N/A
1674N/A It is a range error to pass a negative value.
1674N/A */
1674N/Amp_err mp_sqrt(const mp_int *a, mp_int *b)
1674N/A{
1674N/A mp_int x, t;
1674N/A mp_err res;
1674N/A mp_size used;
1674N/A
1674N/A ARGCHK(a != NULL && b != NULL, MP_BADARG);
1674N/A
1674N/A /* Cannot take square root of a negative value */
1674N/A if(SIGN(a) == NEG)
1674N/A return MP_RANGE;
1674N/A
1674N/A /* Special cases for zero and one, trivial */
1674N/A if(mp_cmp_d(a, 1) <= 0)
1674N/A return mp_copy(a, b);
1674N/A
1674N/A /* Initialize the temporaries we'll use below */
1674N/A if((res = mp_init_size(&t, USED(a), FLAG(a))) != MP_OKAY)
1674N/A return res;
1674N/A
1674N/A /* Compute an initial guess for the iteration as a itself */
1674N/A if((res = mp_init_copy(&x, a)) != MP_OKAY)
1674N/A goto X;
1674N/A
1674N/A used = MP_USED(&x);
1674N/A if (used > 1) {
1674N/A s_mp_rshd(&x, used / 2);
1674N/A }
1674N/A
1674N/A for(;;) {
1674N/A /* t = (x * x) - a */
1674N/A mp_copy(&x, &t); /* can't fail, t is big enough for original x */
1674N/A if((res = mp_sqr(&t, &t)) != MP_OKAY ||
1674N/A (res = mp_sub(&t, a, &t)) != MP_OKAY)
1674N/A goto CLEANUP;
1674N/A
1674N/A /* t = t / 2x */
1674N/A s_mp_mul_2(&x);
1674N/A if((res = mp_div(&t, &x, &t, NULL)) != MP_OKAY)
1674N/A goto CLEANUP;
1674N/A s_mp_div_2(&x);
1674N/A
1674N/A /* Terminate the loop, if the quotient is zero */
1674N/A if(mp_cmp_z(&t) == MP_EQ)
1674N/A break;
1674N/A
1674N/A /* x = x - t */
1674N/A if((res = mp_sub(&x, &t, &x)) != MP_OKAY)
1674N/A goto CLEANUP;
1674N/A
1674N/A }
1674N/A
1674N/A /* Copy result to output parameter */
1674N/A mp_sub_d(&x, 1, &x);
1674N/A s_mp_exch(&x, b);
1674N/A
1674N/A CLEANUP:
1674N/A mp_clear(&x);
1674N/A X:
1674N/A mp_clear(&t);
1674N/A
1674N/A return res;
1674N/A
1674N/A} /* end mp_sqrt() */
1674N/A
1674N/A/* }}} */
1674N/A
1674N/A/* }}} */
1674N/A
1674N/A/*------------------------------------------------------------------------*/
1674N/A/* {{{ Modular arithmetic */
1674N/A
1674N/A#if MP_MODARITH
1674N/A/* {{{ mp_addmod(a, b, m, c) */
1674N/A
1674N/A/*
1674N/A mp_addmod(a, b, m, c)
1674N/A
1674N/A Compute c = (a + b) mod m
1674N/A */
1674N/A
1674N/Amp_err mp_addmod(const mp_int *a, const mp_int *b, const mp_int *m, mp_int *c)
1674N/A{
1674N/A mp_err res;
1674N/A
1674N/A ARGCHK(a != NULL && b != NULL && m != NULL && c != NULL, MP_BADARG);
1674N/A
1674N/A if((res = mp_add(a, b, c)) != MP_OKAY)
1674N/A return res;
1674N/A if((res = mp_mod(c, m, c)) != MP_OKAY)
1674N/A return res;
1674N/A
1674N/A return MP_OKAY;
1674N/A
1674N/A}
1674N/A
1674N/A/* }}} */
1674N/A
1674N/A/* {{{ mp_submod(a, b, m, c) */
1674N/A
1674N/A/*
1674N/A mp_submod(a, b, m, c)
1674N/A
1674N/A Compute c = (a - b) mod m
1674N/A */
1674N/A
1674N/Amp_err mp_submod(const mp_int *a, const mp_int *b, const mp_int *m, mp_int *c)
1674N/A{
1674N/A mp_err res;
1674N/A
1674N/A ARGCHK(a != NULL && b != NULL && m != NULL && c != NULL, MP_BADARG);
1674N/A
1674N/A if((res = mp_sub(a, b, c)) != MP_OKAY)
1674N/A return res;
1674N/A if((res = mp_mod(c, m, c)) != MP_OKAY)
1674N/A return res;
1674N/A
1674N/A return MP_OKAY;
1674N/A
1674N/A}
1674N/A
1674N/A/* }}} */
1674N/A
1674N/A/* {{{ mp_mulmod(a, b, m, c) */
1674N/A
1674N/A/*
1674N/A mp_mulmod(a, b, m, c)
1674N/A
1674N/A Compute c = (a * b) mod m
1674N/A */
1674N/A
1674N/Amp_err mp_mulmod(const mp_int *a, const mp_int *b, const mp_int *m, mp_int *c)
1674N/A{
1674N/A mp_err res;
1674N/A
1674N/A ARGCHK(a != NULL && b != NULL && m != NULL && c != NULL, MP_BADARG);
1674N/A
1674N/A if((res = mp_mul(a, b, c)) != MP_OKAY)
1674N/A return res;
1674N/A if((res = mp_mod(c, m, c)) != MP_OKAY)
1674N/A return res;
1674N/A
1674N/A return MP_OKAY;
1674N/A
1674N/A}
1674N/A
1674N/A/* }}} */
1674N/A
1674N/A/* {{{ mp_sqrmod(a, m, c) */
1674N/A
1674N/A#if MP_SQUARE
1674N/Amp_err mp_sqrmod(const mp_int *a, const mp_int *m, mp_int *c)
1674N/A{
1674N/A mp_err res;
1674N/A
1674N/A ARGCHK(a != NULL && m != NULL && c != NULL, MP_BADARG);
1674N/A
1674N/A if((res = mp_sqr(a, c)) != MP_OKAY)
1674N/A return res;
1674N/A if((res = mp_mod(c, m, c)) != MP_OKAY)
1674N/A return res;
1674N/A
1674N/A return MP_OKAY;
1674N/A
1674N/A} /* end mp_sqrmod() */
1674N/A#endif
1674N/A
1674N/A/* }}} */
1674N/A
1674N/A/* {{{ s_mp_exptmod(a, b, m, c) */
1674N/A
1674N/A/*
1674N/A s_mp_exptmod(a, b, m, c)
1674N/A
1674N/A Compute c = (a ** b) mod m. Uses a standard square-and-multiply
1674N/A method with modular reductions at each step. (This is basically the
1674N/A same code as mp_expt(), except for the addition of the reductions)
1674N/A
1674N/A The modular reductions are done using Barrett's algorithm (see
1674N/A s_mp_reduce() below for details)
1674N/A */
1674N/A
1674N/Amp_err s_mp_exptmod(const mp_int *a, const mp_int *b, const mp_int *m, mp_int *c)
1674N/A{
1674N/A mp_int s, x, mu;
1674N/A mp_err res;
1674N/A mp_digit d;
3488N/A unsigned int dig, bit;
1674N/A
1674N/A ARGCHK(a != NULL && b != NULL && c != NULL, MP_BADARG);
1674N/A
1674N/A if(mp_cmp_z(b) < 0 || mp_cmp_z(m) <= 0)
1674N/A return MP_RANGE;
1674N/A
1674N/A if((res = mp_init(&s, FLAG(a))) != MP_OKAY)
1674N/A return res;
1674N/A if((res = mp_init_copy(&x, a)) != MP_OKAY ||
1674N/A (res = mp_mod(&x, m, &x)) != MP_OKAY)
1674N/A goto X;
1674N/A if((res = mp_init(&mu, FLAG(a))) != MP_OKAY)
1674N/A goto MU;
1674N/A
1674N/A mp_set(&s, 1);
1674N/A
1674N/A /* mu = b^2k / m */
1674N/A s_mp_add_d(&mu, 1);
1674N/A s_mp_lshd(&mu, 2 * USED(m));
1674N/A if((res = mp_div(&mu, m, &mu, NULL)) != MP_OKAY)
1674N/A goto CLEANUP;
1674N/A
1674N/A /* Loop over digits of b in ascending order, except highest order */
1674N/A for(dig = 0; dig < (USED(b) - 1); dig++) {
1674N/A d = DIGIT(b, dig);
1674N/A
1674N/A /* Loop over the bits of the lower-order digits */
1674N/A for(bit = 0; bit < DIGIT_BIT; bit++) {
1674N/A if(d & 1) {
1674N/A if((res = s_mp_mul(&s, &x)) != MP_OKAY)
1674N/A goto CLEANUP;
1674N/A if((res = s_mp_reduce(&s, m, &mu)) != MP_OKAY)
1674N/A goto CLEANUP;
1674N/A }
1674N/A
1674N/A d >>= 1;
1674N/A
1674N/A if((res = s_mp_sqr(&x)) != MP_OKAY)
1674N/A goto CLEANUP;
1674N/A if((res = s_mp_reduce(&x, m, &mu)) != MP_OKAY)
1674N/A goto CLEANUP;
1674N/A }
1674N/A }
1674N/A
1674N/A /* Now do the last digit... */
1674N/A d = DIGIT(b, dig);
1674N/A
1674N/A while(d) {
1674N/A if(d & 1) {
1674N/A if((res = s_mp_mul(&s, &x)) != MP_OKAY)
1674N/A goto CLEANUP;
1674N/A if((res = s_mp_reduce(&s, m, &mu)) != MP_OKAY)
1674N/A goto CLEANUP;
1674N/A }
1674N/A
1674N/A d >>= 1;
1674N/A
1674N/A if((res = s_mp_sqr(&x)) != MP_OKAY)
1674N/A goto CLEANUP;
1674N/A if((res = s_mp_reduce(&x, m, &mu)) != MP_OKAY)
1674N/A goto CLEANUP;
1674N/A }
1674N/A
1674N/A s_mp_exch(&s, c);
1674N/A
1674N/A CLEANUP:
1674N/A mp_clear(&mu);
1674N/A MU:
1674N/A mp_clear(&x);
1674N/A X:
1674N/A mp_clear(&s);
1674N/A
1674N/A return res;
1674N/A
1674N/A} /* end s_mp_exptmod() */
1674N/A
1674N/A/* }}} */
1674N/A
1674N/A/* {{{ mp_exptmod_d(a, d, m, c) */
1674N/A
1674N/Amp_err mp_exptmod_d(const mp_int *a, mp_digit d, const mp_int *m, mp_int *c)
1674N/A{
1674N/A mp_int s, x;
1674N/A mp_err res;
1674N/A
1674N/A ARGCHK(a != NULL && c != NULL, MP_BADARG);
1674N/A
1674N/A if((res = mp_init(&s, FLAG(a))) != MP_OKAY)
1674N/A return res;
1674N/A if((res = mp_init_copy(&x, a)) != MP_OKAY)
1674N/A goto X;
1674N/A
1674N/A mp_set(&s, 1);
1674N/A
1674N/A while(d != 0) {
1674N/A if(d & 1) {
1674N/A if((res = s_mp_mul(&s, &x)) != MP_OKAY ||
1674N/A (res = mp_mod(&s, m, &s)) != MP_OKAY)
1674N/A goto CLEANUP;
1674N/A }
1674N/A
1674N/A d /= 2;
1674N/A
1674N/A if((res = s_mp_sqr(&x)) != MP_OKAY ||
1674N/A (res = mp_mod(&x, m, &x)) != MP_OKAY)
1674N/A goto CLEANUP;
1674N/A }
1674N/A
1674N/A s_mp_exch(&s, c);
1674N/A
1674N/ACLEANUP:
1674N/A mp_clear(&x);
1674N/AX:
1674N/A mp_clear(&s);
1674N/A
1674N/A return res;
1674N/A
1674N/A} /* end mp_exptmod_d() */
1674N/A
1674N/A/* }}} */
1674N/A#endif /* if MP_MODARITH */
1674N/A
1674N/A/* }}} */
1674N/A
1674N/A/*------------------------------------------------------------------------*/
1674N/A/* {{{ Comparison functions */
1674N/A
1674N/A/* {{{ mp_cmp_z(a) */
1674N/A
1674N/A/*
1674N/A mp_cmp_z(a)
1674N/A
1674N/A Compare a <=> 0. Returns <0 if a<0, 0 if a=0, >0 if a>0.
1674N/A */
1674N/A
1674N/Aint mp_cmp_z(const mp_int *a)
1674N/A{
1674N/A if(SIGN(a) == NEG)
1674N/A return MP_LT;
1674N/A else if(USED(a) == 1 && DIGIT(a, 0) == 0)
1674N/A return MP_EQ;
1674N/A else
1674N/A return MP_GT;
1674N/A
1674N/A} /* end mp_cmp_z() */
1674N/A
1674N/A/* }}} */
1674N/A
1674N/A/* {{{ mp_cmp_d(a, d) */
1674N/A
1674N/A/*
1674N/A mp_cmp_d(a, d)
1674N/A
1674N/A Compare a <=> d. Returns <0 if a<d, 0 if a=d, >0 if a>d
1674N/A */
1674N/A
1674N/Aint mp_cmp_d(const mp_int *a, mp_digit d)
1674N/A{
1674N/A ARGCHK(a != NULL, MP_EQ);
1674N/A
1674N/A if(SIGN(a) == NEG)
1674N/A return MP_LT;
1674N/A
1674N/A return s_mp_cmp_d(a, d);
1674N/A
1674N/A} /* end mp_cmp_d() */
1674N/A
1674N/A/* }}} */
1674N/A
1674N/A/* {{{ mp_cmp(a, b) */
1674N/A
1674N/Aint mp_cmp(const mp_int *a, const mp_int *b)
1674N/A{
1674N/A ARGCHK(a != NULL && b != NULL, MP_EQ);
1674N/A
1674N/A if(SIGN(a) == SIGN(b)) {
1674N/A int mag;
1674N/A
1674N/A if((mag = s_mp_cmp(a, b)) == MP_EQ)
1674N/A return MP_EQ;
1674N/A
1674N/A if(SIGN(a) == ZPOS)
1674N/A return mag;
1674N/A else
1674N/A return -mag;
1674N/A
1674N/A } else if(SIGN(a) == ZPOS) {
1674N/A return MP_GT;
1674N/A } else {
1674N/A return MP_LT;
1674N/A }
1674N/A
1674N/A} /* end mp_cmp() */
1674N/A
1674N/A/* }}} */
1674N/A
1674N/A/* {{{ mp_cmp_mag(a, b) */
1674N/A
1674N/A/*
1674N/A mp_cmp_mag(a, b)
1674N/A
1674N/A Compares |a| <=> |b|, and returns an appropriate comparison result
1674N/A */
1674N/A
1674N/Aint mp_cmp_mag(mp_int *a, mp_int *b)
1674N/A{
1674N/A ARGCHK(a != NULL && b != NULL, MP_EQ);
1674N/A
1674N/A return s_mp_cmp(a, b);
1674N/A
1674N/A} /* end mp_cmp_mag() */
1674N/A
1674N/A/* }}} */
1674N/A
1674N/A/* {{{ mp_cmp_int(a, z, kmflag) */
1674N/A
1674N/A/*
1674N/A This just converts z to an mp_int, and uses the existing comparison
1674N/A routines. This is sort of inefficient, but it's not clear to me how
1674N/A frequently this wil get used anyway. For small positive constants,
1674N/A you can always use mp_cmp_d(), and for zero, there is mp_cmp_z().
1674N/A */
1674N/Aint mp_cmp_int(const mp_int *a, long z, int kmflag)
1674N/A{
1674N/A mp_int tmp;
1674N/A int out;
1674N/A
1674N/A ARGCHK(a != NULL, MP_EQ);
1674N/A
1674N/A mp_init(&tmp, kmflag); mp_set_int(&tmp, z);
1674N/A out = mp_cmp(a, &tmp);
1674N/A mp_clear(&tmp);
1674N/A
1674N/A return out;
1674N/A
1674N/A} /* end mp_cmp_int() */
1674N/A
1674N/A/* }}} */
1674N/A
1674N/A/* {{{ mp_isodd(a) */
1674N/A
1674N/A/*
1674N/A mp_isodd(a)
1674N/A
1674N/A Returns a true (non-zero) value if a is odd, false (zero) otherwise.
1674N/A */
1674N/Aint mp_isodd(const mp_int *a)
1674N/A{
1674N/A ARGCHK(a != NULL, 0);
1674N/A
1674N/A return (int)(DIGIT(a, 0) & 1);
1674N/A
1674N/A} /* end mp_isodd() */
1674N/A
1674N/A/* }}} */
1674N/A
1674N/A/* {{{ mp_iseven(a) */
1674N/A
1674N/Aint mp_iseven(const mp_int *a)
1674N/A{
1674N/A return !mp_isodd(a);
1674N/A
1674N/A} /* end mp_iseven() */
1674N/A
1674N/A/* }}} */
1674N/A
1674N/A/* }}} */
1674N/A
1674N/A/*------------------------------------------------------------------------*/
1674N/A/* {{{ Number theoretic functions */
1674N/A
1674N/A#if MP_NUMTH
1674N/A/* {{{ mp_gcd(a, b, c) */
1674N/A
1674N/A/*
1674N/A Like the old mp_gcd() function, except computes the GCD using the
1674N/A binary algorithm due to Josef Stein in 1961 (via Knuth).
1674N/A */
1674N/Amp_err mp_gcd(mp_int *a, mp_int *b, mp_int *c)
1674N/A{
1674N/A mp_err res;
1674N/A mp_int u, v, t;
1674N/A mp_size k = 0;
1674N/A
1674N/A ARGCHK(a != NULL && b != NULL && c != NULL, MP_BADARG);
1674N/A
1674N/A if(mp_cmp_z(a) == MP_EQ && mp_cmp_z(b) == MP_EQ)
1674N/A return MP_RANGE;
1674N/A if(mp_cmp_z(a) == MP_EQ) {
1674N/A return mp_copy(b, c);
1674N/A } else if(mp_cmp_z(b) == MP_EQ) {
1674N/A return mp_copy(a, c);
1674N/A }
1674N/A
1674N/A if((res = mp_init(&t, FLAG(a))) != MP_OKAY)
1674N/A return res;
1674N/A if((res = mp_init_copy(&u, a)) != MP_OKAY)
1674N/A goto U;
1674N/A if((res = mp_init_copy(&v, b)) != MP_OKAY)
1674N/A goto V;
1674N/A
1674N/A SIGN(&u) = ZPOS;
1674N/A SIGN(&v) = ZPOS;
1674N/A
1674N/A /* Divide out common factors of 2 until at least 1 of a, b is even */
1674N/A while(mp_iseven(&u) && mp_iseven(&v)) {
1674N/A s_mp_div_2(&u);
1674N/A s_mp_div_2(&v);
1674N/A ++k;
1674N/A }
1674N/A
1674N/A /* Initialize t */
1674N/A if(mp_isodd(&u)) {
1674N/A if((res = mp_copy(&v, &t)) != MP_OKAY)
1674N/A goto CLEANUP;
1674N/A
1674N/A /* t = -v */
1674N/A if(SIGN(&v) == ZPOS)
1674N/A SIGN(&t) = NEG;
1674N/A else
1674N/A SIGN(&t) = ZPOS;
1674N/A
1674N/A } else {
1674N/A if((res = mp_copy(&u, &t)) != MP_OKAY)
1674N/A goto CLEANUP;
1674N/A
1674N/A }
1674N/A
1674N/A for(;;) {
1674N/A while(mp_iseven(&t)) {
1674N/A s_mp_div_2(&t);
1674N/A }
1674N/A
1674N/A if(mp_cmp_z(&t) == MP_GT) {
1674N/A if((res = mp_copy(&t, &u)) != MP_OKAY)
1674N/A goto CLEANUP;
1674N/A
1674N/A } else {
1674N/A if((res = mp_copy(&t, &v)) != MP_OKAY)
1674N/A goto CLEANUP;
1674N/A
1674N/A /* v = -t */
1674N/A if(SIGN(&t) == ZPOS)
1674N/A SIGN(&v) = NEG;
1674N/A else
1674N/A SIGN(&v) = ZPOS;
1674N/A }
1674N/A
1674N/A if((res = mp_sub(&u, &v, &t)) != MP_OKAY)
1674N/A goto CLEANUP;
1674N/A
1674N/A if(s_mp_cmp_d(&t, 0) == MP_EQ)
1674N/A break;
1674N/A }
1674N/A
1674N/A s_mp_2expt(&v, k); /* v = 2^k */
1674N/A res = mp_mul(&u, &v, c); /* c = u * v */
1674N/A
1674N/A CLEANUP:
1674N/A mp_clear(&v);
1674N/A V:
1674N/A mp_clear(&u);
1674N/A U:
1674N/A mp_clear(&t);
1674N/A
1674N/A return res;
1674N/A
1674N/A} /* end mp_gcd() */
1674N/A
1674N/A/* }}} */
1674N/A
1674N/A/* {{{ mp_lcm(a, b, c) */
1674N/A
1674N/A/* We compute the least common multiple using the rule:
1674N/A
1674N/A ab = [a, b](a, b)
1674N/A
1674N/A ... by computing the product, and dividing out the gcd.
1674N/A */
1674N/A
1674N/Amp_err mp_lcm(mp_int *a, mp_int *b, mp_int *c)
1674N/A{
1674N/A mp_int gcd, prod;
1674N/A mp_err res;
1674N/A
1674N/A ARGCHK(a != NULL && b != NULL && c != NULL, MP_BADARG);
1674N/A
1674N/A /* Set up temporaries */
1674N/A if((res = mp_init(&gcd, FLAG(a))) != MP_OKAY)
1674N/A return res;
1674N/A if((res = mp_init(&prod, FLAG(a))) != MP_OKAY)
1674N/A goto GCD;
1674N/A
1674N/A if((res = mp_mul(a, b, &prod)) != MP_OKAY)
1674N/A goto CLEANUP;
1674N/A if((res = mp_gcd(a, b, &gcd)) != MP_OKAY)
1674N/A goto CLEANUP;
1674N/A
1674N/A res = mp_div(&prod, &gcd, c, NULL);
1674N/A
1674N/A CLEANUP:
1674N/A mp_clear(&prod);
1674N/A GCD:
1674N/A mp_clear(&gcd);
1674N/A
1674N/A return res;
1674N/A
1674N/A} /* end mp_lcm() */
1674N/A
1674N/A/* }}} */
1674N/A
1674N/A/* {{{ mp_xgcd(a, b, g, x, y) */
1674N/A
1674N/A/*
1674N/A mp_xgcd(a, b, g, x, y)
1674N/A
1674N/A Compute g = (a, b) and values x and y satisfying Bezout's identity
1674N/A (that is, ax + by = g). This uses the binary extended GCD algorithm
1674N/A based on the Stein algorithm used for mp_gcd()
1674N/A See algorithm 14.61 in Handbook of Applied Cryptogrpahy.
1674N/A */
1674N/A
1674N/Amp_err mp_xgcd(const mp_int *a, const mp_int *b, mp_int *g, mp_int *x, mp_int *y)
1674N/A{
1674N/A mp_int gx, xc, yc, u, v, A, B, C, D;
1674N/A mp_int *clean[9];
1674N/A mp_err res;
1674N/A int last = -1;
1674N/A
1674N/A if(mp_cmp_z(b) == 0)
1674N/A return MP_RANGE;
1674N/A
1674N/A /* Initialize all these variables we need */
1674N/A MP_CHECKOK( mp_init(&u, FLAG(a)) );
1674N/A clean[++last] = &u;
1674N/A MP_CHECKOK( mp_init(&v, FLAG(a)) );
1674N/A clean[++last] = &v;
1674N/A MP_CHECKOK( mp_init(&gx, FLAG(a)) );
1674N/A clean[++last] = &gx;
1674N/A MP_CHECKOK( mp_init(&A, FLAG(a)) );
1674N/A clean[++last] = &A;
1674N/A MP_CHECKOK( mp_init(&B, FLAG(a)) );
1674N/A clean[++last] = &B;
1674N/A MP_CHECKOK( mp_init(&C, FLAG(a)) );
1674N/A clean[++last] = &C;
1674N/A MP_CHECKOK( mp_init(&D, FLAG(a)) );
1674N/A clean[++last] = &D;
1674N/A MP_CHECKOK( mp_init_copy(&xc, a) );
1674N/A clean[++last] = &xc;
1674N/A mp_abs(&xc, &xc);
1674N/A MP_CHECKOK( mp_init_copy(&yc, b) );
1674N/A clean[++last] = &yc;
1674N/A mp_abs(&yc, &yc);
1674N/A
1674N/A mp_set(&gx, 1);
1674N/A
1674N/A /* Divide by two until at least one of them is odd */
1674N/A while(mp_iseven(&xc) && mp_iseven(&yc)) {
1674N/A mp_size nx = mp_trailing_zeros(&xc);
1674N/A mp_size ny = mp_trailing_zeros(&yc);
1674N/A mp_size n = MP_MIN(nx, ny);
1674N/A s_mp_div_2d(&xc,n);
1674N/A s_mp_div_2d(&yc,n);
1674N/A MP_CHECKOK( s_mp_mul_2d(&gx,n) );
1674N/A }
1674N/A
1674N/A mp_copy(&xc, &u);
1674N/A mp_copy(&yc, &v);
1674N/A mp_set(&A, 1); mp_set(&D, 1);
1674N/A
1674N/A /* Loop through binary GCD algorithm */
1674N/A do {
1674N/A while(mp_iseven(&u)) {
1674N/A s_mp_div_2(&u);
1674N/A
1674N/A if(mp_iseven(&A) && mp_iseven(&B)) {
1674N/A s_mp_div_2(&A); s_mp_div_2(&B);
1674N/A } else {
1674N/A MP_CHECKOK( mp_add(&A, &yc, &A) );
1674N/A s_mp_div_2(&A);
1674N/A MP_CHECKOK( mp_sub(&B, &xc, &B) );
1674N/A s_mp_div_2(&B);
1674N/A }
1674N/A }
1674N/A
1674N/A while(mp_iseven(&v)) {
1674N/A s_mp_div_2(&v);
1674N/A
1674N/A if(mp_iseven(&C) && mp_iseven(&D)) {
1674N/A s_mp_div_2(&C); s_mp_div_2(&D);
1674N/A } else {
1674N/A MP_CHECKOK( mp_add(&C, &yc, &C) );
1674N/A s_mp_div_2(&C);
1674N/A MP_CHECKOK( mp_sub(&D, &xc, &D) );
1674N/A s_mp_div_2(&D);
1674N/A }
1674N/A }
1674N/A
1674N/A if(mp_cmp(&u, &v) >= 0) {
1674N/A MP_CHECKOK( mp_sub(&u, &v, &u) );
1674N/A MP_CHECKOK( mp_sub(&A, &C, &A) );
1674N/A MP_CHECKOK( mp_sub(&B, &D, &B) );
1674N/A } else {
1674N/A MP_CHECKOK( mp_sub(&v, &u, &v) );
1674N/A MP_CHECKOK( mp_sub(&C, &A, &C) );
1674N/A MP_CHECKOK( mp_sub(&D, &B, &D) );
1674N/A }
1674N/A } while (mp_cmp_z(&u) != 0);
1674N/A
1674N/A /* copy results to output */
1674N/A if(x)
1674N/A MP_CHECKOK( mp_copy(&C, x) );
1674N/A
1674N/A if(y)
1674N/A MP_CHECKOK( mp_copy(&D, y) );
1674N/A
1674N/A if(g)
1674N/A MP_CHECKOK( mp_mul(&gx, &v, g) );
1674N/A
1674N/A CLEANUP:
1674N/A while(last >= 0)
1674N/A mp_clear(clean[last--]);
1674N/A
1674N/A return res;
1674N/A
1674N/A} /* end mp_xgcd() */
1674N/A
1674N/A/* }}} */
1674N/A
1674N/Amp_size mp_trailing_zeros(const mp_int *mp)
1674N/A{
1674N/A mp_digit d;
1674N/A mp_size n = 0;
3488N/A unsigned int ix;
1674N/A
1674N/A if (!mp || !MP_DIGITS(mp) || !mp_cmp_z(mp))
1674N/A return n;
1674N/A
1674N/A for (ix = 0; !(d = MP_DIGIT(mp,ix)) && (ix < MP_USED(mp)); ++ix)
1674N/A n += MP_DIGIT_BIT;
1674N/A if (!d)
1674N/A return 0; /* shouldn't happen, but ... */
1674N/A#if !defined(MP_USE_UINT_DIGIT)
1674N/A if (!(d & 0xffffffffU)) {
1674N/A d >>= 32;
1674N/A n += 32;
1674N/A }
1674N/A#endif
1674N/A if (!(d & 0xffffU)) {
1674N/A d >>= 16;
1674N/A n += 16;
1674N/A }
1674N/A if (!(d & 0xffU)) {
1674N/A d >>= 8;
1674N/A n += 8;
1674N/A }
1674N/A if (!(d & 0xfU)) {
1674N/A d >>= 4;
1674N/A n += 4;
1674N/A }
1674N/A if (!(d & 0x3U)) {
1674N/A d >>= 2;
1674N/A n += 2;
1674N/A }
1674N/A if (!(d & 0x1U)) {
1674N/A d >>= 1;
1674N/A n += 1;
1674N/A }
1674N/A#if MP_ARGCHK == 2
1674N/A assert(0 != (d & 1));
1674N/A#endif
1674N/A return n;
1674N/A}
1674N/A
1674N/A/* Given a and prime p, computes c and k such that a*c == 2**k (mod p).
1674N/A** Returns k (positive) or error (negative).
1674N/A** This technique from the paper "Fast Modular Reciprocals" (unpublished)
1674N/A** by Richard Schroeppel (a.k.a. Captain Nemo).
1674N/A*/
1674N/Amp_err s_mp_almost_inverse(const mp_int *a, const mp_int *p, mp_int *c)
1674N/A{
1674N/A mp_err res;
1674N/A mp_err k = 0;
1674N/A mp_int d, f, g;
1674N/A
1674N/A ARGCHK(a && p && c, MP_BADARG);
1674N/A
1674N/A MP_DIGITS(&d) = 0;
1674N/A MP_DIGITS(&f) = 0;
1674N/A MP_DIGITS(&g) = 0;
1674N/A MP_CHECKOK( mp_init(&d, FLAG(a)) );
1674N/A MP_CHECKOK( mp_init_copy(&f, a) ); /* f = a */
1674N/A MP_CHECKOK( mp_init_copy(&g, p) ); /* g = p */
1674N/A
1674N/A mp_set(c, 1);
1674N/A mp_zero(&d);
1674N/A
1674N/A if (mp_cmp_z(&f) == 0) {
1674N/A res = MP_UNDEF;
1674N/A } else
1674N/A for (;;) {
1674N/A int diff_sign;
1674N/A while (mp_iseven(&f)) {
1674N/A mp_size n = mp_trailing_zeros(&f);
1674N/A if (!n) {
1674N/A res = MP_UNDEF;
1674N/A goto CLEANUP;
1674N/A }
1674N/A s_mp_div_2d(&f, n);
1674N/A MP_CHECKOK( s_mp_mul_2d(&d, n) );
1674N/A k += n;
1674N/A }
1674N/A if (mp_cmp_d(&f, 1) == MP_EQ) { /* f == 1 */
1674N/A res = k;
1674N/A break;
1674N/A }
1674N/A diff_sign = mp_cmp(&f, &g);
1674N/A if (diff_sign < 0) { /* f < g */
1674N/A s_mp_exch(&f, &g);
1674N/A s_mp_exch(c, &d);
1674N/A } else if (diff_sign == 0) { /* f == g */
1674N/A res = MP_UNDEF; /* a and p are not relatively prime */
1674N/A break;
1674N/A }
1674N/A if ((MP_DIGIT(&f,0) % 4) == (MP_DIGIT(&g,0) % 4)) {
1674N/A MP_CHECKOK( mp_sub(&f, &g, &f) ); /* f = f - g */
1674N/A MP_CHECKOK( mp_sub(c, &d, c) ); /* c = c - d */
1674N/A } else {
1674N/A MP_CHECKOK( mp_add(&f, &g, &f) ); /* f = f + g */
1674N/A MP_CHECKOK( mp_add(c, &d, c) ); /* c = c + d */
1674N/A }
1674N/A }
1674N/A if (res >= 0) {
1674N/A while (MP_SIGN(c) != MP_ZPOS) {
1674N/A MP_CHECKOK( mp_add(c, p, c) );
1674N/A }
1674N/A res = k;
1674N/A }
1674N/A
1674N/ACLEANUP:
1674N/A mp_clear(&d);
1674N/A mp_clear(&f);
1674N/A mp_clear(&g);
1674N/A return res;
1674N/A}
1674N/A
1674N/A/* Compute T = (P ** -1) mod MP_RADIX. Also works for 16-bit mp_digits.
1674N/A** This technique from the paper "Fast Modular Reciprocals" (unpublished)
1674N/A** by Richard Schroeppel (a.k.a. Captain Nemo).
1674N/A*/
1674N/Amp_digit s_mp_invmod_radix(mp_digit P)
1674N/A{
1674N/A mp_digit T = P;
1674N/A T *= 2 - (P * T);
1674N/A T *= 2 - (P * T);
1674N/A T *= 2 - (P * T);
1674N/A T *= 2 - (P * T);
1674N/A#if !defined(MP_USE_UINT_DIGIT)
1674N/A T *= 2 - (P * T);
1674N/A T *= 2 - (P * T);
1674N/A#endif
1674N/A return T;
1674N/A}
1674N/A
1674N/A/* Given c, k, and prime p, where a*c == 2**k (mod p),
1674N/A** Compute x = (a ** -1) mod p. This is similar to Montgomery reduction.
1674N/A** This technique from the paper "Fast Modular Reciprocals" (unpublished)
1674N/A** by Richard Schroeppel (a.k.a. Captain Nemo).
1674N/A*/
1674N/Amp_err s_mp_fixup_reciprocal(const mp_int *c, const mp_int *p, int k, mp_int *x)
1674N/A{
1674N/A int k_orig = k;
1674N/A mp_digit r;
1674N/A mp_size ix;
1674N/A mp_err res;
1674N/A
1674N/A if (mp_cmp_z(c) < 0) { /* c < 0 */
1674N/A MP_CHECKOK( mp_add(c, p, x) ); /* x = c + p */
1674N/A } else {
1674N/A MP_CHECKOK( mp_copy(c, x) ); /* x = c */
1674N/A }
1674N/A
1674N/A /* make sure x is large enough */
1674N/A ix = MP_HOWMANY(k, MP_DIGIT_BIT) + MP_USED(p) + 1;
1674N/A ix = MP_MAX(ix, MP_USED(x));
1674N/A MP_CHECKOK( s_mp_pad(x, ix) );
1674N/A
1674N/A r = 0 - s_mp_invmod_radix(MP_DIGIT(p,0));
1674N/A
1674N/A for (ix = 0; k > 0; ix++) {
1674N/A int j = MP_MIN(k, MP_DIGIT_BIT);
1674N/A mp_digit v = r * MP_DIGIT(x, ix);
1674N/A if (j < MP_DIGIT_BIT) {
1674N/A v &= ((mp_digit)1 << j) - 1; /* v = v mod (2 ** j) */
1674N/A }
1674N/A s_mp_mul_d_add_offset(p, v, x, ix); /* x += p * v * (RADIX ** ix) */
1674N/A k -= j;
1674N/A }
1674N/A s_mp_clamp(x);
1674N/A s_mp_div_2d(x, k_orig);
1674N/A res = MP_OKAY;
1674N/A
1674N/ACLEANUP:
1674N/A return res;
1674N/A}
1674N/A
1674N/A/* compute mod inverse using Schroeppel's method, only if m is odd */
1674N/Amp_err s_mp_invmod_odd_m(const mp_int *a, const mp_int *m, mp_int *c)
1674N/A{
1674N/A int k;
1674N/A mp_err res;
1674N/A mp_int x;
1674N/A
1674N/A ARGCHK(a && m && c, MP_BADARG);
1674N/A
1674N/A if(mp_cmp_z(a) == 0 || mp_cmp_z(m) == 0)
1674N/A return MP_RANGE;
1674N/A if (mp_iseven(m))
1674N/A return MP_UNDEF;
1674N/A
1674N/A MP_DIGITS(&x) = 0;
1674N/A
1674N/A if (a == c) {
1674N/A if ((res = mp_init_copy(&x, a)) != MP_OKAY)
1674N/A return res;
1674N/A if (a == m)
1674N/A m = &x;
1674N/A a = &x;
1674N/A } else if (m == c) {
1674N/A if ((res = mp_init_copy(&x, m)) != MP_OKAY)
1674N/A return res;
1674N/A m = &x;
1674N/A } else {
1674N/A MP_DIGITS(&x) = 0;
1674N/A }
1674N/A
1674N/A MP_CHECKOK( s_mp_almost_inverse(a, m, c) );
1674N/A k = res;
1674N/A MP_CHECKOK( s_mp_fixup_reciprocal(c, m, k, c) );
1674N/ACLEANUP:
1674N/A mp_clear(&x);
1674N/A return res;
1674N/A}
1674N/A
1674N/A/* Known good algorithm for computing modular inverse. But slow. */
1674N/Amp_err mp_invmod_xgcd(const mp_int *a, const mp_int *m, mp_int *c)
1674N/A{
1674N/A mp_int g, x;
1674N/A mp_err res;
1674N/A
1674N/A ARGCHK(a && m && c, MP_BADARG);
1674N/A
1674N/A if(mp_cmp_z(a) == 0 || mp_cmp_z(m) == 0)
1674N/A return MP_RANGE;
1674N/A
1674N/A MP_DIGITS(&g) = 0;
1674N/A MP_DIGITS(&x) = 0;
1674N/A MP_CHECKOK( mp_init(&x, FLAG(a)) );
1674N/A MP_CHECKOK( mp_init(&g, FLAG(a)) );
1674N/A
1674N/A MP_CHECKOK( mp_xgcd(a, m, &g, &x, NULL) );
1674N/A
1674N/A if (mp_cmp_d(&g, 1) != MP_EQ) {
1674N/A res = MP_UNDEF;
1674N/A goto CLEANUP;
1674N/A }
1674N/A
1674N/A res = mp_mod(&x, m, c);
1674N/A SIGN(c) = SIGN(a);
1674N/A
1674N/ACLEANUP:
1674N/A mp_clear(&x);
1674N/A mp_clear(&g);
1674N/A
1674N/A return res;
1674N/A}
1674N/A
1674N/A/* modular inverse where modulus is 2**k. */
1674N/A/* c = a**-1 mod 2**k */
1674N/Amp_err s_mp_invmod_2d(const mp_int *a, mp_size k, mp_int *c)
1674N/A{
1674N/A mp_err res;
1674N/A mp_size ix = k + 4;
1674N/A mp_int t0, t1, val, tmp, two2k;
1674N/A
1674N/A static const mp_digit d2 = 2;
1674N/A static const mp_int two = { 0, MP_ZPOS, 1, 1, (mp_digit *)&d2 };
1674N/A
1674N/A if (mp_iseven(a))
1674N/A return MP_UNDEF;
1674N/A if (k <= MP_DIGIT_BIT) {
1674N/A mp_digit i = s_mp_invmod_radix(MP_DIGIT(a,0));
1674N/A if (k < MP_DIGIT_BIT)
1674N/A i &= ((mp_digit)1 << k) - (mp_digit)1;
1674N/A mp_set(c, i);
1674N/A return MP_OKAY;
1674N/A }
1674N/A MP_DIGITS(&t0) = 0;
1674N/A MP_DIGITS(&t1) = 0;
1674N/A MP_DIGITS(&val) = 0;
1674N/A MP_DIGITS(&tmp) = 0;
1674N/A MP_DIGITS(&two2k) = 0;
1674N/A MP_CHECKOK( mp_init_copy(&val, a) );
1674N/A s_mp_mod_2d(&val, k);
1674N/A MP_CHECKOK( mp_init_copy(&t0, &val) );
1674N/A MP_CHECKOK( mp_init_copy(&t1, &t0) );
1674N/A MP_CHECKOK( mp_init(&tmp, FLAG(a)) );
1674N/A MP_CHECKOK( mp_init(&two2k, FLAG(a)) );
1674N/A MP_CHECKOK( s_mp_2expt(&two2k, k) );
1674N/A do {
1674N/A MP_CHECKOK( mp_mul(&val, &t1, &tmp) );
1674N/A MP_CHECKOK( mp_sub(&two, &tmp, &tmp) );
1674N/A MP_CHECKOK( mp_mul(&t1, &tmp, &t1) );
1674N/A s_mp_mod_2d(&t1, k);
1674N/A while (MP_SIGN(&t1) != MP_ZPOS) {
1674N/A MP_CHECKOK( mp_add(&t1, &two2k, &t1) );
1674N/A }
1674N/A if (mp_cmp(&t1, &t0) == MP_EQ)
1674N/A break;
1674N/A MP_CHECKOK( mp_copy(&t1, &t0) );
1674N/A } while (--ix > 0);
1674N/A if (!ix) {
1674N/A res = MP_UNDEF;
1674N/A } else {
1674N/A mp_exch(c, &t1);
1674N/A }
1674N/A
1674N/ACLEANUP:
1674N/A mp_clear(&t0);
1674N/A mp_clear(&t1);
1674N/A mp_clear(&val);
1674N/A mp_clear(&tmp);
1674N/A mp_clear(&two2k);
1674N/A return res;
1674N/A}
1674N/A
1674N/Amp_err s_mp_invmod_even_m(const mp_int *a, const mp_int *m, mp_int *c)
1674N/A{
1674N/A mp_err res;
1674N/A mp_size k;
1674N/A mp_int oddFactor, evenFactor; /* factors of the modulus */
1674N/A mp_int oddPart, evenPart; /* parts to combine via CRT. */
1674N/A mp_int C2, tmp1, tmp2;
1674N/A
1674N/A /*static const mp_digit d1 = 1; */
1674N/A /*static const mp_int one = { MP_ZPOS, 1, 1, (mp_digit *)&d1 }; */
1674N/A
1674N/A if ((res = s_mp_ispow2(m)) >= 0) {
1674N/A k = res;
1674N/A return s_mp_invmod_2d(a, k, c);
1674N/A }
1674N/A MP_DIGITS(&oddFactor) = 0;
1674N/A MP_DIGITS(&evenFactor) = 0;
1674N/A MP_DIGITS(&oddPart) = 0;
1674N/A MP_DIGITS(&evenPart) = 0;
1674N/A MP_DIGITS(&C2) = 0;
1674N/A MP_DIGITS(&tmp1) = 0;
1674N/A MP_DIGITS(&tmp2) = 0;
1674N/A
1674N/A MP_CHECKOK( mp_init_copy(&oddFactor, m) ); /* oddFactor = m */
1674N/A MP_CHECKOK( mp_init(&evenFactor, FLAG(m)) );
1674N/A MP_CHECKOK( mp_init(&oddPart, FLAG(m)) );
1674N/A MP_CHECKOK( mp_init(&evenPart, FLAG(m)) );
1674N/A MP_CHECKOK( mp_init(&C2, FLAG(m)) );
1674N/A MP_CHECKOK( mp_init(&tmp1, FLAG(m)) );
1674N/A MP_CHECKOK( mp_init(&tmp2, FLAG(m)) );
1674N/A
1674N/A k = mp_trailing_zeros(m);
1674N/A s_mp_div_2d(&oddFactor, k);
1674N/A MP_CHECKOK( s_mp_2expt(&evenFactor, k) );
1674N/A
1674N/A /* compute a**-1 mod oddFactor. */
1674N/A MP_CHECKOK( s_mp_invmod_odd_m(a, &oddFactor, &oddPart) );
1674N/A /* compute a**-1 mod evenFactor, where evenFactor == 2**k. */
1674N/A MP_CHECKOK( s_mp_invmod_2d( a, k, &evenPart) );
1674N/A
1674N/A /* Use Chinese Remainer theorem to compute a**-1 mod m. */
1674N/A /* let m1 = oddFactor, v1 = oddPart,
1674N/A * let m2 = evenFactor, v2 = evenPart.
1674N/A */
1674N/A
1674N/A /* Compute C2 = m1**-1 mod m2. */
1674N/A MP_CHECKOK( s_mp_invmod_2d(&oddFactor, k, &C2) );
1674N/A
1674N/A /* compute u = (v2 - v1)*C2 mod m2 */
1674N/A MP_CHECKOK( mp_sub(&evenPart, &oddPart, &tmp1) );
1674N/A MP_CHECKOK( mp_mul(&tmp1, &C2, &tmp2) );
1674N/A s_mp_mod_2d(&tmp2, k);
1674N/A while (MP_SIGN(&tmp2) != MP_ZPOS) {
1674N/A MP_CHECKOK( mp_add(&tmp2, &evenFactor, &tmp2) );
1674N/A }
1674N/A
1674N/A /* compute answer = v1 + u*m1 */
1674N/A MP_CHECKOK( mp_mul(&tmp2, &oddFactor, c) );
1674N/A MP_CHECKOK( mp_add(&oddPart, c, c) );
1674N/A /* not sure this is necessary, but it's low cost if not. */
1674N/A MP_CHECKOK( mp_mod(c, m, c) );
1674N/A
1674N/ACLEANUP:
1674N/A mp_clear(&oddFactor);
1674N/A mp_clear(&evenFactor);
1674N/A mp_clear(&oddPart);
1674N/A mp_clear(&evenPart);
1674N/A mp_clear(&C2);
1674N/A mp_clear(&tmp1);
1674N/A mp_clear(&tmp2);
1674N/A return res;
1674N/A}
1674N/A
1674N/A
1674N/A/* {{{ mp_invmod(a, m, c) */
1674N/A
1674N/A/*
1674N/A mp_invmod(a, m, c)
1674N/A
1674N/A Compute c = a^-1 (mod m), if there is an inverse for a (mod m).
1674N/A This is equivalent to the question of whether (a, m) = 1. If not,
1674N/A MP_UNDEF is returned, and there is no inverse.
1674N/A */
1674N/A
1674N/Amp_err mp_invmod(const mp_int *a, const mp_int *m, mp_int *c)
1674N/A{
1674N/A
1674N/A ARGCHK(a && m && c, MP_BADARG);
1674N/A
1674N/A if(mp_cmp_z(a) == 0 || mp_cmp_z(m) == 0)
1674N/A return MP_RANGE;
1674N/A
1674N/A if (mp_isodd(m)) {
1674N/A return s_mp_invmod_odd_m(a, m, c);
1674N/A }
1674N/A if (mp_iseven(a))
1674N/A return MP_UNDEF; /* not invertable */
1674N/A
1674N/A return s_mp_invmod_even_m(a, m, c);
1674N/A
1674N/A} /* end mp_invmod() */
1674N/A
1674N/A/* }}} */
1674N/A#endif /* if MP_NUMTH */
1674N/A
1674N/A/* }}} */
1674N/A
1674N/A/*------------------------------------------------------------------------*/
1674N/A/* {{{ mp_print(mp, ofp) */
1674N/A
1674N/A#if MP_IOFUNC
1674N/A/*
1674N/A mp_print(mp, ofp)
1674N/A
1674N/A Print a textual representation of the given mp_int on the output
1674N/A stream 'ofp'. Output is generated using the internal radix.
1674N/A */
1674N/A
1674N/Avoid mp_print(mp_int *mp, FILE *ofp)
1674N/A{
1674N/A int ix;
1674N/A
1674N/A if(mp == NULL || ofp == NULL)
1674N/A return;
1674N/A
1674N/A fputc((SIGN(mp) == NEG) ? '-' : '+', ofp);
1674N/A
1674N/A for(ix = USED(mp) - 1; ix >= 0; ix--) {
1674N/A fprintf(ofp, DIGIT_FMT, DIGIT(mp, ix));
1674N/A }
1674N/A
1674N/A} /* end mp_print() */
1674N/A
1674N/A#endif /* if MP_IOFUNC */
1674N/A
1674N/A/* }}} */
1674N/A
1674N/A/*------------------------------------------------------------------------*/
1674N/A/* {{{ More I/O Functions */
1674N/A
1674N/A/* {{{ mp_read_raw(mp, str, len) */
1674N/A
1674N/A/*
1674N/A mp_read_raw(mp, str, len)
1674N/A
1674N/A Read in a raw value (base 256) into the given mp_int
1674N/A */
1674N/A
1674N/Amp_err mp_read_raw(mp_int *mp, char *str, int len)
1674N/A{
1674N/A int ix;
1674N/A mp_err res;
1674N/A unsigned char *ustr = (unsigned char *)str;
1674N/A
1674N/A ARGCHK(mp != NULL && str != NULL && len > 0, MP_BADARG);
1674N/A
1674N/A mp_zero(mp);
1674N/A
1674N/A /* Get sign from first byte */
1674N/A if(ustr[0])
1674N/A SIGN(mp) = NEG;
1674N/A else
1674N/A SIGN(mp) = ZPOS;
1674N/A
1674N/A /* Read the rest of the digits */
1674N/A for(ix = 1; ix < len; ix++) {
1674N/A if((res = mp_mul_d(mp, 256, mp)) != MP_OKAY)
1674N/A return res;
1674N/A if((res = mp_add_d(mp, ustr[ix], mp)) != MP_OKAY)
1674N/A return res;
1674N/A }
1674N/A
1674N/A return MP_OKAY;
1674N/A
1674N/A} /* end mp_read_raw() */
1674N/A
1674N/A/* }}} */
1674N/A
1674N/A/* {{{ mp_raw_size(mp) */
1674N/A
1674N/Aint mp_raw_size(mp_int *mp)
1674N/A{
1674N/A ARGCHK(mp != NULL, 0);
1674N/A
1674N/A return (USED(mp) * sizeof(mp_digit)) + 1;
1674N/A
1674N/A} /* end mp_raw_size() */
1674N/A
1674N/A/* }}} */
1674N/A
1674N/A/* {{{ mp_toraw(mp, str) */
1674N/A
1674N/Amp_err mp_toraw(mp_int *mp, char *str)
1674N/A{
1674N/A int ix, jx, pos = 1;
1674N/A
1674N/A ARGCHK(mp != NULL && str != NULL, MP_BADARG);
1674N/A
1674N/A str[0] = (char)SIGN(mp);
1674N/A
1674N/A /* Iterate over each digit... */
1674N/A for(ix = USED(mp) - 1; ix >= 0; ix--) {
1674N/A mp_digit d = DIGIT(mp, ix);
1674N/A
1674N/A /* Unpack digit bytes, high order first */
1674N/A for(jx = sizeof(mp_digit) - 1; jx >= 0; jx--) {
1674N/A str[pos++] = (char)(d >> (jx * CHAR_BIT));
1674N/A }
1674N/A }
1674N/A
1674N/A return MP_OKAY;
1674N/A
1674N/A} /* end mp_toraw() */
1674N/A
1674N/A/* }}} */
1674N/A
1674N/A/* {{{ mp_read_radix(mp, str, radix) */
1674N/A
1674N/A/*
1674N/A mp_read_radix(mp, str, radix)
1674N/A
1674N/A Read an integer from the given string, and set mp to the resulting
1674N/A value. The input is presumed to be in base 10. Leading non-digit
1674N/A characters are ignored, and the function reads until a non-digit
1674N/A character or the end of the string.
1674N/A */
1674N/A
1674N/Amp_err mp_read_radix(mp_int *mp, const char *str, int radix)
1674N/A{
1674N/A int ix = 0, val = 0;
1674N/A mp_err res;
1674N/A mp_sign sig = ZPOS;
1674N/A
1674N/A ARGCHK(mp != NULL && str != NULL && radix >= 2 && radix <= MAX_RADIX,
1674N/A MP_BADARG);
1674N/A
1674N/A mp_zero(mp);
1674N/A
1674N/A /* Skip leading non-digit characters until a digit or '-' or '+' */
1674N/A while(str[ix] &&
1674N/A (s_mp_tovalue(str[ix], radix) < 0) &&
1674N/A str[ix] != '-' &&
1674N/A str[ix] != '+') {
1674N/A ++ix;
1674N/A }
1674N/A
1674N/A if(str[ix] == '-') {
1674N/A sig = NEG;
1674N/A ++ix;
1674N/A } else if(str[ix] == '+') {
1674N/A sig = ZPOS; /* this is the default anyway... */
1674N/A ++ix;
1674N/A }
1674N/A
1674N/A while((val = s_mp_tovalue(str[ix], radix)) >= 0) {
1674N/A if((res = s_mp_mul_d(mp, radix)) != MP_OKAY)
1674N/A return res;
1674N/A if((res = s_mp_add_d(mp, val)) != MP_OKAY)
1674N/A return res;
1674N/A ++ix;
1674N/A }
1674N/A
1674N/A if(s_mp_cmp_d(mp, 0) == MP_EQ)
1674N/A SIGN(mp) = ZPOS;
1674N/A else
1674N/A SIGN(mp) = sig;
1674N/A
1674N/A return MP_OKAY;
1674N/A
1674N/A} /* end mp_read_radix() */
1674N/A
1674N/Amp_err mp_read_variable_radix(mp_int *a, const char * str, int default_radix)
1674N/A{
1674N/A int radix = default_radix;
1674N/A int cx;
1674N/A mp_sign sig = ZPOS;
1674N/A mp_err res;
1674N/A
1674N/A /* Skip leading non-digit characters until a digit or '-' or '+' */
1674N/A while ((cx = *str) != 0 &&
1674N/A (s_mp_tovalue(cx, radix) < 0) &&
1674N/A cx != '-' &&
1674N/A cx != '+') {
1674N/A ++str;
1674N/A }
1674N/A
1674N/A if (cx == '-') {
1674N/A sig = NEG;
1674N/A ++str;
1674N/A } else if (cx == '+') {
1674N/A sig = ZPOS; /* this is the default anyway... */
1674N/A ++str;
1674N/A }
1674N/A
1674N/A if (str[0] == '0') {
1674N/A if ((str[1] | 0x20) == 'x') {
1674N/A radix = 16;
1674N/A str += 2;
1674N/A } else {
1674N/A radix = 8;
1674N/A str++;
1674N/A }
1674N/A }
1674N/A res = mp_read_radix(a, str, radix);
1674N/A if (res == MP_OKAY) {
1674N/A MP_SIGN(a) = (s_mp_cmp_d(a, 0) == MP_EQ) ? ZPOS : sig;
1674N/A }
1674N/A return res;
1674N/A}
1674N/A
1674N/A/* }}} */
1674N/A
1674N/A/* {{{ mp_radix_size(mp, radix) */
1674N/A
1674N/Aint mp_radix_size(mp_int *mp, int radix)
1674N/A{
1674N/A int bits;
1674N/A
1674N/A if(!mp || radix < 2 || radix > MAX_RADIX)
1674N/A return 0;
1674N/A
1674N/A bits = USED(mp) * DIGIT_BIT - 1;
1674N/A
1674N/A return s_mp_outlen(bits, radix);
1674N/A
1674N/A} /* end mp_radix_size() */
1674N/A
1674N/A/* }}} */
1674N/A
1674N/A/* {{{ mp_toradix(mp, str, radix) */
1674N/A
1674N/Amp_err mp_toradix(mp_int *mp, char *str, int radix)
1674N/A{
1674N/A int ix, pos = 0;
1674N/A
1674N/A ARGCHK(mp != NULL && str != NULL, MP_BADARG);
1674N/A ARGCHK(radix > 1 && radix <= MAX_RADIX, MP_RANGE);
1674N/A
1674N/A if(mp_cmp_z(mp) == MP_EQ) {
1674N/A str[0] = '0';
1674N/A str[1] = '\0';
1674N/A } else {
1674N/A mp_err res;
1674N/A mp_int tmp;
1674N/A mp_sign sgn;
1674N/A mp_digit rem, rdx = (mp_digit)radix;
1674N/A char ch;
1674N/A
1674N/A if((res = mp_init_copy(&tmp, mp)) != MP_OKAY)
1674N/A return res;
1674N/A
1674N/A /* Save sign for later, and take absolute value */
1674N/A sgn = SIGN(&tmp); SIGN(&tmp) = ZPOS;
1674N/A
1674N/A /* Generate output digits in reverse order */
1674N/A while(mp_cmp_z(&tmp) != 0) {
1674N/A if((res = mp_div_d(&tmp, rdx, &tmp, &rem)) != MP_OKAY) {
1674N/A mp_clear(&tmp);
1674N/A return res;
1674N/A }
1674N/A
1674N/A /* Generate digits, use capital letters */
1674N/A ch = s_mp_todigit(rem, radix, 0);
1674N/A
1674N/A str[pos++] = ch;
1674N/A }
1674N/A
1674N/A /* Add - sign if original value was negative */
1674N/A if(sgn == NEG)
1674N/A str[pos++] = '-';
1674N/A
1674N/A /* Add trailing NUL to end the string */
1674N/A str[pos--] = '\0';
1674N/A
1674N/A /* Reverse the digits and sign indicator */
1674N/A ix = 0;
1674N/A while(ix < pos) {
1674N/A char tmp = str[ix];
1674N/A
1674N/A str[ix] = str[pos];
1674N/A str[pos] = tmp;
1674N/A ++ix;
1674N/A --pos;
1674N/A }
1674N/A
1674N/A mp_clear(&tmp);
1674N/A }
1674N/A
1674N/A return MP_OKAY;
1674N/A
1674N/A} /* end mp_toradix() */
1674N/A
1674N/A/* }}} */
1674N/A
1674N/A/* {{{ mp_tovalue(ch, r) */
1674N/A
1674N/Aint mp_tovalue(char ch, int r)
1674N/A{
1674N/A return s_mp_tovalue(ch, r);
1674N/A
1674N/A} /* end mp_tovalue() */
1674N/A
1674N/A/* }}} */
1674N/A
1674N/A/* }}} */
1674N/A
1674N/A/* {{{ mp_strerror(ec) */
1674N/A
1674N/A/*
1674N/A mp_strerror(ec)
1674N/A
1674N/A Return a string describing the meaning of error code 'ec'. The
1674N/A string returned is allocated in static memory, so the caller should
1674N/A not attempt to modify or free the memory associated with this
1674N/A string.
1674N/A */
1674N/Aconst char *mp_strerror(mp_err ec)
1674N/A{
1674N/A int aec = (ec < 0) ? -ec : ec;
1674N/A
1674N/A /* Code values are negative, so the senses of these comparisons
1674N/A are accurate */
1674N/A if(ec < MP_LAST_CODE || ec > MP_OKAY) {
1674N/A return mp_err_string[0]; /* unknown error code */
1674N/A } else {
1674N/A return mp_err_string[aec + 1];
1674N/A }
1674N/A
1674N/A} /* end mp_strerror() */
1674N/A
1674N/A/* }}} */
1674N/A
1674N/A/*========================================================================*/
1674N/A/*------------------------------------------------------------------------*/
1674N/A/* Static function definitions (internal use only) */
1674N/A
1674N/A/* {{{ Memory management */
1674N/A
1674N/A/* {{{ s_mp_grow(mp, min) */
1674N/A
1674N/A/* Make sure there are at least 'min' digits allocated to mp */
1674N/Amp_err s_mp_grow(mp_int *mp, mp_size min)
1674N/A{
1674N/A if(min > ALLOC(mp)) {
1674N/A mp_digit *tmp;
1674N/A
1674N/A /* Set min to next nearest default precision block size */
1674N/A min = MP_ROUNDUP(min, s_mp_defprec);
1674N/A
1674N/A if((tmp = s_mp_alloc(min, sizeof(mp_digit), FLAG(mp))) == NULL)
1674N/A return MP_MEM;
1674N/A
1674N/A s_mp_copy(DIGITS(mp), tmp, USED(mp));
1674N/A
1674N/A#if MP_CRYPTO
1674N/A s_mp_setz(DIGITS(mp), ALLOC(mp));
1674N/A#endif
1674N/A s_mp_free(DIGITS(mp), ALLOC(mp));
1674N/A DIGITS(mp) = tmp;
1674N/A ALLOC(mp) = min;
1674N/A }
1674N/A
1674N/A return MP_OKAY;
1674N/A
1674N/A} /* end s_mp_grow() */
1674N/A
1674N/A/* }}} */
1674N/A
1674N/A/* {{{ s_mp_pad(mp, min) */
1674N/A
1674N/A/* Make sure the used size of mp is at least 'min', growing if needed */
1674N/Amp_err s_mp_pad(mp_int *mp, mp_size min)
1674N/A{
1674N/A if(min > USED(mp)) {
1674N/A mp_err res;
1674N/A
1674N/A /* Make sure there is room to increase precision */
1674N/A if (min > ALLOC(mp)) {
1674N/A if ((res = s_mp_grow(mp, min)) != MP_OKAY)
1674N/A return res;
1674N/A } else {
1674N/A s_mp_setz(DIGITS(mp) + USED(mp), min - USED(mp));
1674N/A }
1674N/A
1674N/A /* Increase precision; should already be 0-filled */
1674N/A USED(mp) = min;
1674N/A }
1674N/A
1674N/A return MP_OKAY;
1674N/A
1674N/A} /* end s_mp_pad() */
1674N/A
1674N/A/* }}} */
1674N/A
1674N/A/* {{{ s_mp_setz(dp, count) */
1674N/A
1674N/A#if MP_MACRO == 0
1674N/A/* Set 'count' digits pointed to by dp to be zeroes */
1674N/Avoid s_mp_setz(mp_digit *dp, mp_size count)
1674N/A{
1674N/A#if MP_MEMSET == 0
1674N/A int ix;
1674N/A
1674N/A for(ix = 0; ix < count; ix++)
1674N/A dp[ix] = 0;
1674N/A#else
1674N/A memset(dp, 0, count * sizeof(mp_digit));
1674N/A#endif
1674N/A
1674N/A} /* end s_mp_setz() */
1674N/A#endif
1674N/A
1674N/A/* }}} */
1674N/A
1674N/A/* {{{ s_mp_copy(sp, dp, count) */
1674N/A
1674N/A#if MP_MACRO == 0
1674N/A/* Copy 'count' digits from sp to dp */
1674N/Avoid s_mp_copy(const mp_digit *sp, mp_digit *dp, mp_size count)
1674N/A{
1674N/A#if MP_MEMCPY == 0
1674N/A int ix;
1674N/A
1674N/A for(ix = 0; ix < count; ix++)
1674N/A dp[ix] = sp[ix];
1674N/A#else
1674N/A memcpy(dp, sp, count * sizeof(mp_digit));
1674N/A#endif
1674N/A
1674N/A} /* end s_mp_copy() */
1674N/A#endif
1674N/A
1674N/A/* }}} */
1674N/A
1674N/A/* {{{ s_mp_alloc(nb, ni, kmflag) */
1674N/A
1674N/A#if MP_MACRO == 0
1674N/A/* Allocate ni records of nb bytes each, and return a pointer to that */
1674N/Avoid *s_mp_alloc(size_t nb, size_t ni, int kmflag)
1674N/A{
1674N/A ++mp_allocs;
1674N/A#ifdef _KERNEL
3488N/A mp_int *mp;
1674N/A mp = kmem_zalloc(nb * ni, kmflag);
1674N/A if (mp != NULL)
1674N/A FLAG(mp) = kmflag;
1674N/A return (mp);
1674N/A#else
1674N/A return calloc(nb, ni);
1674N/A#endif
1674N/A
1674N/A} /* end s_mp_alloc() */
1674N/A#endif
1674N/A
1674N/A/* }}} */
1674N/A
1674N/A/* {{{ s_mp_free(ptr) */
1674N/A
1674N/A#if MP_MACRO == 0
1674N/A/* Free the memory pointed to by ptr */
1674N/Avoid s_mp_free(void *ptr, mp_size alloc)
1674N/A{
1674N/A if(ptr) {
1674N/A ++mp_frees;
1674N/A#ifdef _KERNEL
1674N/A kmem_free(ptr, alloc * sizeof (mp_digit));
1674N/A#else
1674N/A free(ptr);
1674N/A#endif
1674N/A }
1674N/A} /* end s_mp_free() */
1674N/A#endif
1674N/A
1674N/A/* }}} */
1674N/A
1674N/A/* {{{ s_mp_clamp(mp) */
1674N/A
1674N/A#if MP_MACRO == 0
1674N/A/* Remove leading zeroes from the given value */
1674N/Avoid s_mp_clamp(mp_int *mp)
1674N/A{
1674N/A mp_size used = MP_USED(mp);
1674N/A while (used > 1 && DIGIT(mp, used - 1) == 0)
1674N/A --used;
1674N/A MP_USED(mp) = used;
1674N/A} /* end s_mp_clamp() */
1674N/A#endif
1674N/A
1674N/A/* }}} */
1674N/A
1674N/A/* {{{ s_mp_exch(a, b) */
1674N/A
1674N/A/* Exchange the data for a and b; (b, a) = (a, b) */
1674N/Avoid s_mp_exch(mp_int *a, mp_int *b)
1674N/A{
1674N/A mp_int tmp;
1674N/A
1674N/A tmp = *a;
1674N/A *a = *b;
1674N/A *b = tmp;
1674N/A
1674N/A} /* end s_mp_exch() */
1674N/A
1674N/A/* }}} */
1674N/A
1674N/A/* }}} */
1674N/A
1674N/A/* {{{ Arithmetic helpers */
1674N/A
1674N/A/* {{{ s_mp_lshd(mp, p) */
1674N/A
1674N/A/*
1674N/A Shift mp leftward by p digits, growing if needed, and zero-filling
1674N/A the in-shifted digits at the right end. This is a convenient
1674N/A alternative to multiplication by powers of the radix
1674N/A The value of USED(mp) must already have been set to the value for
1674N/A the shifted result.
1674N/A */
1674N/A
1674N/Amp_err s_mp_lshd(mp_int *mp, mp_size p)
1674N/A{
1674N/A mp_err res;
1674N/A mp_size pos;
1674N/A int ix;
1674N/A
1674N/A if(p == 0)
1674N/A return MP_OKAY;
1674N/A
1674N/A if (MP_USED(mp) == 1 && MP_DIGIT(mp, 0) == 0)
1674N/A return MP_OKAY;
1674N/A
1674N/A if((res = s_mp_pad(mp, USED(mp) + p)) != MP_OKAY)
1674N/A return res;
1674N/A
1674N/A pos = USED(mp) - 1;
1674N/A
1674N/A /* Shift all the significant figures over as needed */
1674N/A for(ix = pos - p; ix >= 0; ix--)
1674N/A DIGIT(mp, ix + p) = DIGIT(mp, ix);
1674N/A
1674N/A /* Fill the bottom digits with zeroes */
1674N/A for(ix = 0; ix < p; ix++)
1674N/A DIGIT(mp, ix) = 0;
1674N/A
1674N/A return MP_OKAY;
1674N/A
1674N/A} /* end s_mp_lshd() */
1674N/A
1674N/A/* }}} */
1674N/A
1674N/A/* {{{ s_mp_mul_2d(mp, d) */
1674N/A
1674N/A/*
1674N/A Multiply the integer by 2^d, where d is a number of bits. This
1674N/A amounts to a bitwise shift of the value.
1674N/A */
1674N/Amp_err s_mp_mul_2d(mp_int *mp, mp_digit d)
1674N/A{
1674N/A mp_err res;
1674N/A mp_digit dshift, bshift;
1674N/A mp_digit mask;
1674N/A
1674N/A ARGCHK(mp != NULL, MP_BADARG);
1674N/A
1674N/A dshift = d / MP_DIGIT_BIT;
1674N/A bshift = d % MP_DIGIT_BIT;
1674N/A /* bits to be shifted out of the top word */
1674N/A mask = ((mp_digit)~0 << (MP_DIGIT_BIT - bshift));
1674N/A mask &= MP_DIGIT(mp, MP_USED(mp) - 1);
1674N/A
1674N/A if (MP_OKAY != (res = s_mp_pad(mp, MP_USED(mp) + dshift + (mask != 0) )))
1674N/A return res;
1674N/A
1674N/A if (dshift && MP_OKAY != (res = s_mp_lshd(mp, dshift)))
1674N/A return res;
1674N/A
1674N/A if (bshift) {
1674N/A mp_digit *pa = MP_DIGITS(mp);
1674N/A mp_digit *alim = pa + MP_USED(mp);
1674N/A mp_digit prev = 0;
1674N/A
1674N/A for (pa += dshift; pa < alim; ) {
1674N/A mp_digit x = *pa;
1674N/A *pa++ = (x << bshift) | prev;
1674N/A prev = x >> (DIGIT_BIT - bshift);
1674N/A }
1674N/A }
1674N/A
1674N/A s_mp_clamp(mp);
1674N/A return MP_OKAY;
1674N/A} /* end s_mp_mul_2d() */
1674N/A
1674N/A/* {{{ s_mp_rshd(mp, p) */
1674N/A
1674N/A/*
1674N/A Shift mp rightward by p digits. Maintains the invariant that
1674N/A digits above the precision are all zero. Digits shifted off the
1674N/A end are lost. Cannot fail.
1674N/A */
1674N/A
1674N/Avoid s_mp_rshd(mp_int *mp, mp_size p)
1674N/A{
1674N/A mp_size ix;
1674N/A mp_digit *src, *dst;
1674N/A
1674N/A if(p == 0)
1674N/A return;
1674N/A
1674N/A /* Shortcut when all digits are to be shifted off */
1674N/A if(p >= USED(mp)) {
1674N/A s_mp_setz(DIGITS(mp), ALLOC(mp));
1674N/A USED(mp) = 1;
1674N/A SIGN(mp) = ZPOS;
1674N/A return;
1674N/A }
1674N/A
1674N/A /* Shift all the significant figures over as needed */
1674N/A dst = MP_DIGITS(mp);
1674N/A src = dst + p;
1674N/A for (ix = USED(mp) - p; ix > 0; ix--)
1674N/A *dst++ = *src++;
1674N/A
1674N/A MP_USED(mp) -= p;
1674N/A /* Fill the top digits with zeroes */
1674N/A while (p-- > 0)
1674N/A *dst++ = 0;
1674N/A
1674N/A#if 0
1674N/A /* Strip off any leading zeroes */
1674N/A s_mp_clamp(mp);
1674N/A#endif
1674N/A
1674N/A} /* end s_mp_rshd() */
1674N/A
1674N/A/* }}} */
1674N/A
1674N/A/* {{{ s_mp_div_2(mp) */
1674N/A
1674N/A/* Divide by two -- take advantage of radix properties to do it fast */
1674N/Avoid s_mp_div_2(mp_int *mp)
1674N/A{
1674N/A s_mp_div_2d(mp, 1);
1674N/A
1674N/A} /* end s_mp_div_2() */
1674N/A
1674N/A/* }}} */
1674N/A
1674N/A/* {{{ s_mp_mul_2(mp) */
1674N/A
1674N/Amp_err s_mp_mul_2(mp_int *mp)
1674N/A{
1674N/A mp_digit *pd;
3488N/A unsigned int ix, used;
1674N/A mp_digit kin = 0;
1674N/A
1674N/A /* Shift digits leftward by 1 bit */
1674N/A used = MP_USED(mp);
1674N/A pd = MP_DIGITS(mp);
1674N/A for (ix = 0; ix < used; ix++) {
1674N/A mp_digit d = *pd;
1674N/A *pd++ = (d << 1) | kin;
1674N/A kin = (d >> (DIGIT_BIT - 1));
1674N/A }
1674N/A
1674N/A /* Deal with rollover from last digit */
1674N/A if (kin) {
1674N/A if (ix >= ALLOC(mp)) {
1674N/A mp_err res;
1674N/A if((res = s_mp_grow(mp, ALLOC(mp) + 1)) != MP_OKAY)
1674N/A return res;
1674N/A }
1674N/A
1674N/A DIGIT(mp, ix) = kin;
1674N/A USED(mp) += 1;
1674N/A }
1674N/A
1674N/A return MP_OKAY;
1674N/A
1674N/A} /* end s_mp_mul_2() */
1674N/A
1674N/A/* }}} */
1674N/A
1674N/A/* {{{ s_mp_mod_2d(mp, d) */
1674N/A
1674N/A/*
1674N/A Remainder the integer by 2^d, where d is a number of bits. This
1674N/A amounts to a bitwise AND of the value, and does not require the full
1674N/A division code
1674N/A */
1674N/Avoid s_mp_mod_2d(mp_int *mp, mp_digit d)
1674N/A{
1674N/A mp_size ndig = (d / DIGIT_BIT), nbit = (d % DIGIT_BIT);
1674N/A mp_size ix;
1674N/A mp_digit dmask;
1674N/A
1674N/A if(ndig >= USED(mp))
1674N/A return;
1674N/A
1674N/A /* Flush all the bits above 2^d in its digit */
1674N/A dmask = ((mp_digit)1 << nbit) - 1;
1674N/A DIGIT(mp, ndig) &= dmask;
1674N/A
1674N/A /* Flush all digits above the one with 2^d in it */
1674N/A for(ix = ndig + 1; ix < USED(mp); ix++)
1674N/A DIGIT(mp, ix) = 0;
1674N/A
1674N/A s_mp_clamp(mp);
1674N/A
1674N/A} /* end s_mp_mod_2d() */
1674N/A
1674N/A/* }}} */
1674N/A
1674N/A/* {{{ s_mp_div_2d(mp, d) */
1674N/A
1674N/A/*
1674N/A Divide the integer by 2^d, where d is a number of bits. This
1674N/A amounts to a bitwise shift of the value, and does not require the
1674N/A full division code (used in Barrett reduction, see below)
1674N/A */
1674N/Avoid s_mp_div_2d(mp_int *mp, mp_digit d)
1674N/A{
1674N/A int ix;
1674N/A mp_digit save, next, mask;
1674N/A
1674N/A s_mp_rshd(mp, d / DIGIT_BIT);
1674N/A d %= DIGIT_BIT;
1674N/A if (d) {
1674N/A mask = ((mp_digit)1 << d) - 1;
1674N/A save = 0;
1674N/A for(ix = USED(mp) - 1; ix >= 0; ix--) {
1674N/A next = DIGIT(mp, ix) & mask;
1674N/A DIGIT(mp, ix) = (DIGIT(mp, ix) >> d) | (save << (DIGIT_BIT - d));
1674N/A save = next;
1674N/A }
1674N/A }
1674N/A s_mp_clamp(mp);
1674N/A
1674N/A} /* end s_mp_div_2d() */
1674N/A
1674N/A/* }}} */
1674N/A
1674N/A/* {{{ s_mp_norm(a, b, *d) */
1674N/A
1674N/A/*
1674N/A s_mp_norm(a, b, *d)
1674N/A
1674N/A Normalize a and b for division, where b is the divisor. In order
1674N/A that we might make good guesses for quotient digits, we want the
1674N/A leading digit of b to be at least half the radix, which we
1674N/A accomplish by multiplying a and b by a power of 2. The exponent
1674N/A (shift count) is placed in *pd, so that the remainder can be shifted
1674N/A back at the end of the division process.
1674N/A */
1674N/A
1674N/Amp_err s_mp_norm(mp_int *a, mp_int *b, mp_digit *pd)
1674N/A{
1674N/A mp_digit d;
1674N/A mp_digit mask;
1674N/A mp_digit b_msd;
1674N/A mp_err res = MP_OKAY;
1674N/A
1674N/A d = 0;
1674N/A mask = DIGIT_MAX & ~(DIGIT_MAX >> 1); /* mask is msb of digit */
1674N/A b_msd = DIGIT(b, USED(b) - 1);
1674N/A while (!(b_msd & mask)) {
1674N/A b_msd <<= 1;
1674N/A ++d;
1674N/A }
1674N/A
1674N/A if (d) {
1674N/A MP_CHECKOK( s_mp_mul_2d(a, d) );
1674N/A MP_CHECKOK( s_mp_mul_2d(b, d) );
1674N/A }
1674N/A
1674N/A *pd = d;
1674N/ACLEANUP:
1674N/A return res;
1674N/A
1674N/A} /* end s_mp_norm() */
1674N/A
1674N/A/* }}} */
1674N/A
1674N/A/* }}} */
1674N/A
1674N/A/* {{{ Primitive digit arithmetic */
1674N/A
1674N/A/* {{{ s_mp_add_d(mp, d) */
1674N/A
1674N/A/* Add d to |mp| in place */
1674N/Amp_err s_mp_add_d(mp_int *mp, mp_digit d) /* unsigned digit addition */
1674N/A{
1674N/A#if !defined(MP_NO_MP_WORD) && !defined(MP_NO_ADD_WORD)
1674N/A mp_word w, k = 0;
1674N/A mp_size ix = 1;
1674N/A
1674N/A w = (mp_word)DIGIT(mp, 0) + d;
1674N/A DIGIT(mp, 0) = ACCUM(w);
1674N/A k = CARRYOUT(w);
1674N/A
1674N/A while(ix < USED(mp) && k) {
1674N/A w = (mp_word)DIGIT(mp, ix) + k;
1674N/A DIGIT(mp, ix) = ACCUM(w);
1674N/A k = CARRYOUT(w);
1674N/A ++ix;
1674N/A }
1674N/A
1674N/A if(k != 0) {
1674N/A mp_err res;
1674N/A
1674N/A if((res = s_mp_pad(mp, USED(mp) + 1)) != MP_OKAY)
1674N/A return res;
1674N/A
1674N/A DIGIT(mp, ix) = (mp_digit)k;
1674N/A }
1674N/A
1674N/A return MP_OKAY;
1674N/A#else
1674N/A mp_digit * pmp = MP_DIGITS(mp);
1674N/A mp_digit sum, mp_i, carry = 0;
1674N/A mp_err res = MP_OKAY;
1674N/A int used = (int)MP_USED(mp);
1674N/A
1674N/A mp_i = *pmp;
1674N/A *pmp++ = sum = d + mp_i;
1674N/A carry = (sum < d);
1674N/A while (carry && --used > 0) {
1674N/A mp_i = *pmp;
1674N/A *pmp++ = sum = carry + mp_i;
1674N/A carry = !sum;
1674N/A }
1674N/A if (carry && !used) {
1674N/A /* mp is growing */
1674N/A used = MP_USED(mp);
1674N/A MP_CHECKOK( s_mp_pad(mp, used + 1) );
1674N/A MP_DIGIT(mp, used) = carry;
1674N/A }
1674N/ACLEANUP:
1674N/A return res;
1674N/A#endif
1674N/A} /* end s_mp_add_d() */
1674N/A
1674N/A/* }}} */
1674N/A
1674N/A/* {{{ s_mp_sub_d(mp, d) */
1674N/A
1674N/A/* Subtract d from |mp| in place, assumes |mp| > d */
1674N/Amp_err s_mp_sub_d(mp_int *mp, mp_digit d) /* unsigned digit subtract */
1674N/A{
1674N/A#if !defined(MP_NO_MP_WORD) && !defined(MP_NO_SUB_WORD)
1674N/A mp_word w, b = 0;
1674N/A mp_size ix = 1;
1674N/A
1674N/A /* Compute initial subtraction */
1674N/A w = (RADIX + (mp_word)DIGIT(mp, 0)) - d;
1674N/A b = CARRYOUT(w) ? 0 : 1;
1674N/A DIGIT(mp, 0) = ACCUM(w);
1674N/A
1674N/A /* Propagate borrows leftward */
1674N/A while(b && ix < USED(mp)) {
1674N/A w = (RADIX + (mp_word)DIGIT(mp, ix)) - b;
1674N/A b = CARRYOUT(w) ? 0 : 1;
1674N/A DIGIT(mp, ix) = ACCUM(w);
1674N/A ++ix;
1674N/A }
1674N/A
1674N/A /* Remove leading zeroes */
1674N/A s_mp_clamp(mp);
1674N/A
1674N/A /* If we have a borrow out, it's a violation of the input invariant */
1674N/A if(b)
1674N/A return MP_RANGE;
1674N/A else
1674N/A return MP_OKAY;
1674N/A#else
1674N/A mp_digit *pmp = MP_DIGITS(mp);
1674N/A mp_digit mp_i, diff, borrow;
1674N/A mp_size used = MP_USED(mp);
1674N/A
1674N/A mp_i = *pmp;
1674N/A *pmp++ = diff = mp_i - d;
1674N/A borrow = (diff > mp_i);
1674N/A while (borrow && --used) {
1674N/A mp_i = *pmp;
1674N/A *pmp++ = diff = mp_i - borrow;
1674N/A borrow = (diff > mp_i);
1674N/A }
1674N/A s_mp_clamp(mp);
1674N/A return (borrow && !used) ? MP_RANGE : MP_OKAY;
1674N/A#endif
1674N/A} /* end s_mp_sub_d() */
1674N/A
1674N/A/* }}} */
1674N/A
1674N/A/* {{{ s_mp_mul_d(a, d) */
1674N/A
1674N/A/* Compute a = a * d, single digit multiplication */
1674N/Amp_err s_mp_mul_d(mp_int *a, mp_digit d)
1674N/A{
1674N/A mp_err res;
1674N/A mp_size used;
1674N/A int pow;
1674N/A
1674N/A if (!d) {
1674N/A mp_zero(a);
1674N/A return MP_OKAY;
1674N/A }
1674N/A if (d == 1)
1674N/A return MP_OKAY;
1674N/A if (0 <= (pow = s_mp_ispow2d(d))) {
1674N/A return s_mp_mul_2d(a, (mp_digit)pow);
1674N/A }
1674N/A
1674N/A used = MP_USED(a);
1674N/A MP_CHECKOK( s_mp_pad(a, used + 1) );
1674N/A
1674N/A s_mpv_mul_d(MP_DIGITS(a), used, d, MP_DIGITS(a));
1674N/A
1674N/A s_mp_clamp(a);
1674N/A
1674N/ACLEANUP:
1674N/A return res;
1674N/A
1674N/A} /* end s_mp_mul_d() */
1674N/A
1674N/A/* }}} */
1674N/A
1674N/A/* {{{ s_mp_div_d(mp, d, r) */
1674N/A
1674N/A/*
1674N/A s_mp_div_d(mp, d, r)
1674N/A
1674N/A Compute the quotient mp = mp / d and remainder r = mp mod d, for a
1674N/A single digit d. If r is null, the remainder will be discarded.
1674N/A */
1674N/A
1674N/Amp_err s_mp_div_d(mp_int *mp, mp_digit d, mp_digit *r)
1674N/A{
1674N/A#if !defined(MP_NO_MP_WORD) && !defined(MP_NO_DIV_WORD)
1674N/A mp_word w = 0, q;
1674N/A#else
1674N/A mp_digit w, q;
1674N/A#endif
1674N/A int ix;
1674N/A mp_err res;
1674N/A mp_int quot;
1674N/A mp_int rem;
1674N/A
1674N/A if(d == 0)
1674N/A return MP_RANGE;
1674N/A if (d == 1) {
1674N/A if (r)
1674N/A *r = 0;
1674N/A return MP_OKAY;
1674N/A }
1674N/A /* could check for power of 2 here, but mp_div_d does that. */
1674N/A if (MP_USED(mp) == 1) {
1674N/A mp_digit n = MP_DIGIT(mp,0);
1674N/A mp_digit rem;
1674N/A
1674N/A q = n / d;
1674N/A rem = n % d;
1674N/A MP_DIGIT(mp,0) = q;
1674N/A if (r)
1674N/A *r = rem;
1674N/A return MP_OKAY;
1674N/A }
1674N/A
1674N/A MP_DIGITS(&rem) = 0;
1674N/A MP_DIGITS(&quot) = 0;
1674N/A /* Make room for the quotient */
1674N/A MP_CHECKOK( mp_init_size(&quot, USED(mp), FLAG(mp)) );
1674N/A
1674N/A#if !defined(MP_NO_MP_WORD) && !defined(MP_NO_DIV_WORD)
1674N/A for(ix = USED(mp) - 1; ix >= 0; ix--) {
1674N/A w = (w << DIGIT_BIT) | DIGIT(mp, ix);
1674N/A
1674N/A if(w >= d) {
1674N/A q = w / d;
1674N/A w = w % d;
1674N/A } else {
1674N/A q = 0;
1674N/A }
1674N/A
1674N/A s_mp_lshd(&quot, 1);
1674N/A DIGIT(&quot, 0) = (mp_digit)q;
1674N/A }
1674N/A#else
1674N/A {
1674N/A mp_digit p;
1674N/A#if !defined(MP_ASSEMBLY_DIV_2DX1D)
1674N/A mp_digit norm;
1674N/A#endif
1674N/A
1674N/A MP_CHECKOK( mp_init_copy(&rem, mp) );
1674N/A
1674N/A#if !defined(MP_ASSEMBLY_DIV_2DX1D)
1674N/A MP_DIGIT(&quot, 0) = d;
1674N/A MP_CHECKOK( s_mp_norm(&rem, &quot, &norm) );
1674N/A if (norm)
1674N/A d <<= norm;
1674N/A MP_DIGIT(&quot, 0) = 0;
1674N/A#endif
1674N/A
1674N/A p = 0;
1674N/A for (ix = USED(&rem) - 1; ix >= 0; ix--) {
1674N/A w = DIGIT(&rem, ix);
1674N/A
1674N/A if (p) {
1674N/A MP_CHECKOK( s_mpv_div_2dx1d(p, w, d, &q, &w) );
1674N/A } else if (w >= d) {
1674N/A q = w / d;
1674N/A w = w % d;
1674N/A } else {
1674N/A q = 0;
1674N/A }
1674N/A
1674N/A MP_CHECKOK( s_mp_lshd(&quot, 1) );
1674N/A DIGIT(&quot, 0) = q;
1674N/A p = w;
1674N/A }
1674N/A#if !defined(MP_ASSEMBLY_DIV_2DX1D)
1674N/A if (norm)
1674N/A w >>= norm;
1674N/A#endif
1674N/A }
1674N/A#endif
1674N/A
1674N/A /* Deliver the remainder, if desired */
1674N/A if(r)
1674N/A *r = (mp_digit)w;
1674N/A
1674N/A s_mp_clamp(&quot);
1674N/A mp_exch(&quot, mp);
1674N/ACLEANUP:
1674N/A mp_clear(&quot);
1674N/A mp_clear(&rem);
1674N/A
1674N/A return res;
1674N/A} /* end s_mp_div_d() */
1674N/A
1674N/A/* }}} */
1674N/A
1674N/A
1674N/A/* }}} */
1674N/A
1674N/A/* {{{ Primitive full arithmetic */
1674N/A
1674N/A/* {{{ s_mp_add(a, b) */
1674N/A
1674N/A/* Compute a = |a| + |b| */
1674N/Amp_err s_mp_add(mp_int *a, const mp_int *b) /* magnitude addition */
1674N/A{
1674N/A#if !defined(MP_NO_MP_WORD) && !defined(MP_NO_ADD_WORD)
1674N/A mp_word w = 0;
1674N/A#else
1674N/A mp_digit d, sum, carry = 0;
1674N/A#endif
1674N/A mp_digit *pa, *pb;
1674N/A mp_size ix;
1674N/A mp_size used;
1674N/A mp_err res;
1674N/A
1674N/A /* Make sure a has enough precision for the output value */
1674N/A if((USED(b) > USED(a)) && (res = s_mp_pad(a, USED(b))) != MP_OKAY)
1674N/A return res;
1674N/A
1674N/A /*
1674N/A Add up all digits up to the precision of b. If b had initially
1674N/A the same precision as a, or greater, we took care of it by the
1674N/A padding step above, so there is no problem. If b had initially
1674N/A less precision, we'll have to make sure the carry out is duly
1674N/A propagated upward among the higher-order digits of the sum.
1674N/A */
1674N/A pa = MP_DIGITS(a);
1674N/A pb = MP_DIGITS(b);
1674N/A used = MP_USED(b);
1674N/A for(ix = 0; ix < used; ix++) {
1674N/A#if !defined(MP_NO_MP_WORD) && !defined(MP_NO_ADD_WORD)
1674N/A w = w + *pa + *pb++;
1674N/A *pa++ = ACCUM(w);
1674N/A w = CARRYOUT(w);
1674N/A#else
1674N/A d = *pa;
1674N/A sum = d + *pb++;
1674N/A d = (sum < d); /* detect overflow */
1674N/A *pa++ = sum += carry;
1674N/A carry = d + (sum < carry); /* detect overflow */
1674N/A#endif
1674N/A }
1674N/A
1674N/A /* If we run out of 'b' digits before we're actually done, make
1674N/A sure the carries get propagated upward...
1674N/A */
1674N/A used = MP_USED(a);
1674N/A#if !defined(MP_NO_MP_WORD) && !defined(MP_NO_ADD_WORD)
1674N/A while (w && ix < used) {
1674N/A w = w + *pa;
1674N/A *pa++ = ACCUM(w);
1674N/A w = CARRYOUT(w);
1674N/A ++ix;
1674N/A }
1674N/A#else
1674N/A while (carry && ix < used) {
1674N/A sum = carry + *pa;
1674N/A *pa++ = sum;
1674N/A carry = !sum;
1674N/A ++ix;
1674N/A }
1674N/A#endif
1674N/A
1674N/A /* If there's an overall carry out, increase precision and include
1674N/A it. We could have done this initially, but why touch the memory
1674N/A allocator unless we're sure we have to?
1674N/A */
1674N/A#if !defined(MP_NO_MP_WORD) && !defined(MP_NO_ADD_WORD)
1674N/A if (w) {
1674N/A if((res = s_mp_pad(a, used + 1)) != MP_OKAY)
1674N/A return res;
1674N/A
1674N/A DIGIT(a, ix) = (mp_digit)w;
1674N/A }
1674N/A#else
1674N/A if (carry) {
1674N/A if((res = s_mp_pad(a, used + 1)) != MP_OKAY)
1674N/A return res;
1674N/A
1674N/A DIGIT(a, used) = carry;
1674N/A }
1674N/A#endif
1674N/A
1674N/A return MP_OKAY;
1674N/A} /* end s_mp_add() */
1674N/A
1674N/A/* }}} */
1674N/A
1674N/A/* Compute c = |a| + |b| */ /* magnitude addition */
1674N/Amp_err s_mp_add_3arg(const mp_int *a, const mp_int *b, mp_int *c)
1674N/A{
1674N/A mp_digit *pa, *pb, *pc;
1674N/A#if !defined(MP_NO_MP_WORD) && !defined(MP_NO_ADD_WORD)
1674N/A mp_word w = 0;
1674N/A#else
1674N/A mp_digit sum, carry = 0, d;
1674N/A#endif
1674N/A mp_size ix;
1674N/A mp_size used;
1674N/A mp_err res;
1674N/A
1674N/A MP_SIGN(c) = MP_SIGN(a);
1674N/A if (MP_USED(a) < MP_USED(b)) {
1674N/A const mp_int *xch = a;
1674N/A a = b;
1674N/A b = xch;
1674N/A }
1674N/A
1674N/A /* Make sure a has enough precision for the output value */
1674N/A if (MP_OKAY != (res = s_mp_pad(c, MP_USED(a))))
1674N/A return res;
1674N/A
1674N/A /*
1674N/A Add up all digits up to the precision of b. If b had initially
1674N/A the same precision as a, or greater, we took care of it by the
1674N/A exchange step above, so there is no problem. If b had initially
1674N/A less precision, we'll have to make sure the carry out is duly
1674N/A propagated upward among the higher-order digits of the sum.
1674N/A */
1674N/A pa = MP_DIGITS(a);
1674N/A pb = MP_DIGITS(b);
1674N/A pc = MP_DIGITS(c);
1674N/A used = MP_USED(b);
1674N/A for (ix = 0; ix < used; ix++) {
1674N/A#if !defined(MP_NO_MP_WORD) && !defined(MP_NO_ADD_WORD)
1674N/A w = w + *pa++ + *pb++;
1674N/A *pc++ = ACCUM(w);
1674N/A w = CARRYOUT(w);
1674N/A#else
1674N/A d = *pa++;
1674N/A sum = d + *pb++;
1674N/A d = (sum < d); /* detect overflow */
1674N/A *pc++ = sum += carry;
1674N/A carry = d + (sum < carry); /* detect overflow */
1674N/A#endif
1674N/A }
1674N/A
1674N/A /* If we run out of 'b' digits before we're actually done, make
1674N/A sure the carries get propagated upward...
1674N/A */
1674N/A for (used = MP_USED(a); ix < used; ++ix) {
1674N/A#if !defined(MP_NO_MP_WORD) && !defined(MP_NO_ADD_WORD)
1674N/A w = w + *pa++;
1674N/A *pc++ = ACCUM(w);
1674N/A w = CARRYOUT(w);
1674N/A#else
1674N/A *pc++ = sum = carry + *pa++;
1674N/A carry = (sum < carry);
1674N/A#endif
1674N/A }
1674N/A
1674N/A /* If there's an overall carry out, increase precision and include
1674N/A it. We could have done this initially, but why touch the memory
1674N/A allocator unless we're sure we have to?
1674N/A */
1674N/A#if !defined(MP_NO_MP_WORD) && !defined(MP_NO_ADD_WORD)
1674N/A if (w) {
1674N/A if((res = s_mp_pad(c, used + 1)) != MP_OKAY)
1674N/A return res;
1674N/A
1674N/A DIGIT(c, used) = (mp_digit)w;
1674N/A ++used;
1674N/A }
1674N/A#else
1674N/A if (carry) {
1674N/A if((res = s_mp_pad(c, used + 1)) != MP_OKAY)
1674N/A return res;
1674N/A
1674N/A DIGIT(c, used) = carry;
1674N/A ++used;
1674N/A }
1674N/A#endif
1674N/A MP_USED(c) = used;
1674N/A return MP_OKAY;
1674N/A}
1674N/A/* {{{ s_mp_add_offset(a, b, offset) */
1674N/A
1674N/A/* Compute a = |a| + ( |b| * (RADIX ** offset) ) */
1674N/Amp_err s_mp_add_offset(mp_int *a, mp_int *b, mp_size offset)
1674N/A{
1674N/A#if !defined(MP_NO_MP_WORD) && !defined(MP_NO_ADD_WORD)
1674N/A mp_word w, k = 0;
1674N/A#else
1674N/A mp_digit d, sum, carry = 0;
1674N/A#endif
1674N/A mp_size ib;
1674N/A mp_size ia;
1674N/A mp_size lim;
1674N/A mp_err res;
1674N/A
1674N/A /* Make sure a has enough precision for the output value */
1674N/A lim = MP_USED(b) + offset;
1674N/A if((lim > USED(a)) && (res = s_mp_pad(a, lim)) != MP_OKAY)
1674N/A return res;
1674N/A
1674N/A /*
1674N/A Add up all digits up to the precision of b. If b had initially
1674N/A the same precision as a, or greater, we took care of it by the
1674N/A padding step above, so there is no problem. If b had initially
1674N/A less precision, we'll have to make sure the carry out is duly
1674N/A propagated upward among the higher-order digits of the sum.
1674N/A */
1674N/A lim = USED(b);
1674N/A for(ib = 0, ia = offset; ib < lim; ib++, ia++) {
1674N/A#if !defined(MP_NO_MP_WORD) && !defined(MP_NO_ADD_WORD)
1674N/A w = (mp_word)DIGIT(a, ia) + DIGIT(b, ib) + k;
1674N/A DIGIT(a, ia) = ACCUM(w);
1674N/A k = CARRYOUT(w);
1674N/A#else
1674N/A d = MP_DIGIT(a, ia);
1674N/A sum = d + MP_DIGIT(b, ib);
1674N/A d = (sum < d);
1674N/A MP_DIGIT(a,ia) = sum += carry;
1674N/A carry = d + (sum < carry);
1674N/A#endif
1674N/A }
1674N/A
1674N/A /* If we run out of 'b' digits before we're actually done, make
1674N/A sure the carries get propagated upward...
1674N/A */
1674N/A#if !defined(MP_NO_MP_WORD) && !defined(MP_NO_ADD_WORD)
1674N/A for (lim = MP_USED(a); k && (ia < lim); ++ia) {
1674N/A w = (mp_word)DIGIT(a, ia) + k;
1674N/A DIGIT(a, ia) = ACCUM(w);
1674N/A k = CARRYOUT(w);
1674N/A }
1674N/A#else
1674N/A for (lim = MP_USED(a); carry && (ia < lim); ++ia) {
1674N/A d = MP_DIGIT(a, ia);
1674N/A MP_DIGIT(a,ia) = sum = d + carry;
1674N/A carry = (sum < d);
1674N/A }
1674N/A#endif
1674N/A
1674N/A /* If there's an overall carry out, increase precision and include
1674N/A it. We could have done this initially, but why touch the memory
1674N/A allocator unless we're sure we have to?
1674N/A */
1674N/A#if !defined(MP_NO_MP_WORD) && !defined(MP_NO_ADD_WORD)
1674N/A if(k) {
1674N/A if((res = s_mp_pad(a, USED(a) + 1)) != MP_OKAY)
1674N/A return res;
1674N/A
1674N/A DIGIT(a, ia) = (mp_digit)k;
1674N/A }
1674N/A#else
1674N/A if (carry) {
1674N/A if((res = s_mp_pad(a, lim + 1)) != MP_OKAY)
1674N/A return res;
1674N/A
1674N/A DIGIT(a, lim) = carry;
1674N/A }
1674N/A#endif
1674N/A s_mp_clamp(a);
1674N/A
1674N/A return MP_OKAY;
1674N/A
1674N/A} /* end s_mp_add_offset() */
1674N/A
1674N/A/* }}} */
1674N/A
1674N/A/* {{{ s_mp_sub(a, b) */
1674N/A
1674N/A/* Compute a = |a| - |b|, assumes |a| >= |b| */
1674N/Amp_err s_mp_sub(mp_int *a, const mp_int *b) /* magnitude subtract */
1674N/A{
1674N/A mp_digit *pa, *pb, *limit;
1674N/A#if !defined(MP_NO_MP_WORD) && !defined(MP_NO_SUB_WORD)
1674N/A mp_sword w = 0;
1674N/A#else
1674N/A mp_digit d, diff, borrow = 0;
1674N/A#endif
1674N/A
1674N/A /*
1674N/A Subtract and propagate borrow. Up to the precision of b, this
1674N/A accounts for the digits of b; after that, we just make sure the
1674N/A carries get to the right place. This saves having to pad b out to
1674N/A the precision of a just to make the loops work right...
1674N/A */
1674N/A pa = MP_DIGITS(a);
1674N/A pb = MP_DIGITS(b);
1674N/A limit = pb + MP_USED(b);
1674N/A while (pb < limit) {
1674N/A#if !defined(MP_NO_MP_WORD) && !defined(MP_NO_SUB_WORD)
1674N/A w = w + *pa - *pb++;
1674N/A *pa++ = ACCUM(w);
1674N/A w >>= MP_DIGIT_BIT;
1674N/A#else
1674N/A d = *pa;
1674N/A diff = d - *pb++;
1674N/A d = (diff > d); /* detect borrow */
1674N/A if (borrow && --diff == MP_DIGIT_MAX)
1674N/A ++d;
1674N/A *pa++ = diff;
1674N/A borrow = d;
1674N/A#endif
1674N/A }
1674N/A limit = MP_DIGITS(a) + MP_USED(a);
1674N/A#if !defined(MP_NO_MP_WORD) && !defined(MP_NO_SUB_WORD)
1674N/A while (w && pa < limit) {
1674N/A w = w + *pa;
1674N/A *pa++ = ACCUM(w);
1674N/A w >>= MP_DIGIT_BIT;
1674N/A }
1674N/A#else
1674N/A while (borrow && pa < limit) {
1674N/A d = *pa;
1674N/A *pa++ = diff = d - borrow;
1674N/A borrow = (diff > d);
1674N/A }
1674N/A#endif
1674N/A
1674N/A /* Clobber any leading zeroes we created */
1674N/A s_mp_clamp(a);
1674N/A
1674N/A /*
1674N/A If there was a borrow out, then |b| > |a| in violation
1674N/A of our input invariant. We've already done the work,
1674N/A but we'll at least complain about it...
1674N/A */
1674N/A#if !defined(MP_NO_MP_WORD) && !defined(MP_NO_SUB_WORD)
1674N/A return w ? MP_RANGE : MP_OKAY;
1674N/A#else
1674N/A return borrow ? MP_RANGE : MP_OKAY;
1674N/A#endif
1674N/A} /* end s_mp_sub() */
1674N/A
1674N/A/* }}} */
1674N/A
1674N/A/* Compute c = |a| - |b|, assumes |a| >= |b| */ /* magnitude subtract */
1674N/Amp_err s_mp_sub_3arg(const mp_int *a, const mp_int *b, mp_int *c)
1674N/A{
1674N/A mp_digit *pa, *pb, *pc;
1674N/A#if !defined(MP_NO_MP_WORD) && !defined(MP_NO_SUB_WORD)
1674N/A mp_sword w = 0;
1674N/A#else
1674N/A mp_digit d, diff, borrow = 0;
1674N/A#endif
1674N/A int ix, limit;
1674N/A mp_err res;
1674N/A
1674N/A MP_SIGN(c) = MP_SIGN(a);
1674N/A
1674N/A /* Make sure a has enough precision for the output value */
1674N/A if (MP_OKAY != (res = s_mp_pad(c, MP_USED(a))))
1674N/A return res;
1674N/A
1674N/A /*
1674N/A Subtract and propagate borrow. Up to the precision of b, this
1674N/A accounts for the digits of b; after that, we just make sure the
1674N/A carries get to the right place. This saves having to pad b out to
1674N/A the precision of a just to make the loops work right...
1674N/A */
1674N/A pa = MP_DIGITS(a);
1674N/A pb = MP_DIGITS(b);
1674N/A pc = MP_DIGITS(c);
1674N/A limit = MP_USED(b);
1674N/A for (ix = 0; ix < limit; ++ix) {
1674N/A#if !defined(MP_NO_MP_WORD) && !defined(MP_NO_SUB_WORD)
1674N/A w = w + *pa++ - *pb++;
1674N/A *pc++ = ACCUM(w);
1674N/A w >>= MP_DIGIT_BIT;
1674N/A#else
1674N/A d = *pa++;
1674N/A diff = d - *pb++;
1674N/A d = (diff > d);
1674N/A if (borrow && --diff == MP_DIGIT_MAX)
1674N/A ++d;
1674N/A *pc++ = diff;
1674N/A borrow = d;
1674N/A#endif
1674N/A }
1674N/A for (limit = MP_USED(a); ix < limit; ++ix) {
1674N/A#if !defined(MP_NO_MP_WORD) && !defined(MP_NO_SUB_WORD)
1674N/A w = w + *pa++;
1674N/A *pc++ = ACCUM(w);
1674N/A w >>= MP_DIGIT_BIT;
1674N/A#else
1674N/A d = *pa++;
1674N/A *pc++ = diff = d - borrow;
1674N/A borrow = (diff > d);
1674N/A#endif
1674N/A }
1674N/A
1674N/A /* Clobber any leading zeroes we created */
1674N/A MP_USED(c) = ix;
1674N/A s_mp_clamp(c);
1674N/A
1674N/A /*
1674N/A If there was a borrow out, then |b| > |a| in violation
1674N/A of our input invariant. We've already done the work,
1674N/A but we'll at least complain about it...
1674N/A */
1674N/A#if !defined(MP_NO_MP_WORD) && !defined(MP_NO_SUB_WORD)
1674N/A return w ? MP_RANGE : MP_OKAY;
1674N/A#else
1674N/A return borrow ? MP_RANGE : MP_OKAY;
1674N/A#endif
1674N/A}
1674N/A/* {{{ s_mp_mul(a, b) */
1674N/A
1674N/A/* Compute a = |a| * |b| */
1674N/Amp_err s_mp_mul(mp_int *a, const mp_int *b)
1674N/A{
1674N/A return mp_mul(a, b, a);
1674N/A} /* end s_mp_mul() */
1674N/A
1674N/A/* }}} */
1674N/A
1674N/A#if defined(MP_USE_UINT_DIGIT) && defined(MP_USE_LONG_LONG_MULTIPLY)
1674N/A/* This trick works on Sparc V8 CPUs with the Workshop compilers. */
1674N/A#define MP_MUL_DxD(a, b, Phi, Plo) \
1674N/A { unsigned long long product = (unsigned long long)a * b; \
1674N/A Plo = (mp_digit)product; \
1674N/A Phi = (mp_digit)(product >> MP_DIGIT_BIT); }
1674N/A#elif defined(OSF1)
1674N/A#define MP_MUL_DxD(a, b, Phi, Plo) \
1674N/A { Plo = asm ("mulq %a0, %a1, %v0", a, b);\
1674N/A Phi = asm ("umulh %a0, %a1, %v0", a, b); }
1674N/A#else
1674N/A#define MP_MUL_DxD(a, b, Phi, Plo) \
1674N/A { mp_digit a0b1, a1b0; \
1674N/A Plo = (a & MP_HALF_DIGIT_MAX) * (b & MP_HALF_DIGIT_MAX); \
1674N/A Phi = (a >> MP_HALF_DIGIT_BIT) * (b >> MP_HALF_DIGIT_BIT); \
1674N/A a0b1 = (a & MP_HALF_DIGIT_MAX) * (b >> MP_HALF_DIGIT_BIT); \
1674N/A a1b0 = (a >> MP_HALF_DIGIT_BIT) * (b & MP_HALF_DIGIT_MAX); \
1674N/A a1b0 += a0b1; \
1674N/A Phi += a1b0 >> MP_HALF_DIGIT_BIT; \
1674N/A if (a1b0 < a0b1) \
1674N/A Phi += MP_HALF_RADIX; \
1674N/A a1b0 <<= MP_HALF_DIGIT_BIT; \
1674N/A Plo += a1b0; \
1674N/A if (Plo < a1b0) \
1674N/A ++Phi; \
1674N/A }
1674N/A#endif
1674N/A
1674N/A#if !defined(MP_ASSEMBLY_MULTIPLY)
1674N/A/* c = a * b */
1674N/Avoid s_mpv_mul_d(const mp_digit *a, mp_size a_len, mp_digit b, mp_digit *c)
1674N/A{
1674N/A#if !defined(MP_NO_MP_WORD) && !defined(MP_NO_MUL_WORD)
1674N/A mp_digit d = 0;
1674N/A
1674N/A /* Inner product: Digits of a */
1674N/A while (a_len--) {
1674N/A mp_word w = ((mp_word)b * *a++) + d;
1674N/A *c++ = ACCUM(w);
1674N/A d = CARRYOUT(w);
1674N/A }
1674N/A *c = d;
1674N/A#else
1674N/A mp_digit carry = 0;
1674N/A while (a_len--) {
1674N/A mp_digit a_i = *a++;
1674N/A mp_digit a0b0, a1b1;
1674N/A
1674N/A MP_MUL_DxD(a_i, b, a1b1, a0b0);
1674N/A
1674N/A a0b0 += carry;
1674N/A if (a0b0 < carry)
1674N/A ++a1b1;
1674N/A *c++ = a0b0;
1674N/A carry = a1b1;
1674N/A }
1674N/A *c = carry;
1674N/A#endif
1674N/A}
1674N/A
1674N/A/* c += a * b */
1674N/Avoid s_mpv_mul_d_add(const mp_digit *a, mp_size a_len, mp_digit b,
1674N/A mp_digit *c)
1674N/A{
1674N/A#if !defined(MP_NO_MP_WORD) && !defined(MP_NO_MUL_WORD)
1674N/A mp_digit d = 0;
1674N/A
1674N/A /* Inner product: Digits of a */
1674N/A while (a_len--) {
1674N/A mp_word w = ((mp_word)b * *a++) + *c + d;
1674N/A *c++ = ACCUM(w);
1674N/A d = CARRYOUT(w);
1674N/A }
1674N/A *c = d;
1674N/A#else
1674N/A mp_digit carry = 0;
1674N/A while (a_len--) {
1674N/A mp_digit a_i = *a++;
1674N/A mp_digit a0b0, a1b1;
1674N/A
1674N/A MP_MUL_DxD(a_i, b, a1b1, a0b0);
1674N/A
1674N/A a0b0 += carry;
1674N/A if (a0b0 < carry)
1674N/A ++a1b1;
1674N/A a0b0 += a_i = *c;
1674N/A if (a0b0 < a_i)
1674N/A ++a1b1;
1674N/A *c++ = a0b0;
1674N/A carry = a1b1;
1674N/A }
1674N/A *c = carry;
1674N/A#endif
1674N/A}
1674N/A
1674N/A/* Presently, this is only used by the Montgomery arithmetic code. */
1674N/A/* c += a * b */
1674N/Avoid s_mpv_mul_d_add_prop(const mp_digit *a, mp_size a_len, mp_digit b, mp_digit *c)
1674N/A{
1674N/A#if !defined(MP_NO_MP_WORD) && !defined(MP_NO_MUL_WORD)
1674N/A mp_digit d = 0;
1674N/A
1674N/A /* Inner product: Digits of a */
1674N/A while (a_len--) {
1674N/A mp_word w = ((mp_word)b * *a++) + *c + d;
1674N/A *c++ = ACCUM(w);
1674N/A d = CARRYOUT(w);
1674N/A }
1674N/A
1674N/A while (d) {
1674N/A mp_word w = (mp_word)*c + d;
1674N/A *c++ = ACCUM(w);
1674N/A d = CARRYOUT(w);
1674N/A }
1674N/A#else
1674N/A mp_digit carry = 0;
1674N/A while (a_len--) {
1674N/A mp_digit a_i = *a++;
1674N/A mp_digit a0b0, a1b1;
1674N/A
1674N/A MP_MUL_DxD(a_i, b, a1b1, a0b0);
1674N/A
1674N/A a0b0 += carry;
1674N/A if (a0b0 < carry)
1674N/A ++a1b1;
1674N/A
1674N/A a0b0 += a_i = *c;
1674N/A if (a0b0 < a_i)
1674N/A ++a1b1;
1674N/A
1674N/A *c++ = a0b0;
1674N/A carry = a1b1;
1674N/A }
1674N/A while (carry) {
1674N/A mp_digit c_i = *c;
1674N/A carry += c_i;
1674N/A *c++ = carry;
1674N/A carry = carry < c_i;
1674N/A }
1674N/A#endif
1674N/A}
1674N/A#endif
1674N/A
1674N/A#if defined(MP_USE_UINT_DIGIT) && defined(MP_USE_LONG_LONG_MULTIPLY)
1674N/A/* This trick works on Sparc V8 CPUs with the Workshop compilers. */
1674N/A#define MP_SQR_D(a, Phi, Plo) \
1674N/A { unsigned long long square = (unsigned long long)a * a; \
1674N/A Plo = (mp_digit)square; \
1674N/A Phi = (mp_digit)(square >> MP_DIGIT_BIT); }
1674N/A#elif defined(OSF1)
1674N/A#define MP_SQR_D(a, Phi, Plo) \
1674N/A { Plo = asm ("mulq %a0, %a0, %v0", a);\
1674N/A Phi = asm ("umulh %a0, %a0, %v0", a); }
1674N/A#else
1674N/A#define MP_SQR_D(a, Phi, Plo) \
1674N/A { mp_digit Pmid; \
1674N/A Plo = (a & MP_HALF_DIGIT_MAX) * (a & MP_HALF_DIGIT_MAX); \
1674N/A Phi = (a >> MP_HALF_DIGIT_BIT) * (a >> MP_HALF_DIGIT_BIT); \
1674N/A Pmid = (a & MP_HALF_DIGIT_MAX) * (a >> MP_HALF_DIGIT_BIT); \
1674N/A Phi += Pmid >> (MP_HALF_DIGIT_BIT - 1); \
1674N/A Pmid <<= (MP_HALF_DIGIT_BIT + 1); \
1674N/A Plo += Pmid; \
1674N/A if (Plo < Pmid) \
1674N/A ++Phi; \
1674N/A }
1674N/A#endif
1674N/A
1674N/A#if !defined(MP_ASSEMBLY_SQUARE)
1674N/A/* Add the squares of the digits of a to the digits of b. */
1674N/Avoid s_mpv_sqr_add_prop(const mp_digit *pa, mp_size a_len, mp_digit *ps)
1674N/A{
1674N/A#if !defined(MP_NO_MP_WORD) && !defined(MP_NO_MUL_WORD)
1674N/A mp_word w;
1674N/A mp_digit d;
1674N/A mp_size ix;
1674N/A
1674N/A w = 0;
1674N/A#define ADD_SQUARE(n) \
1674N/A d = pa[n]; \
1674N/A w += (d * (mp_word)d) + ps[2*n]; \
1674N/A ps[2*n] = ACCUM(w); \
1674N/A w = (w >> DIGIT_BIT) + ps[2*n+1]; \
1674N/A ps[2*n+1] = ACCUM(w); \
1674N/A w = (w >> DIGIT_BIT)
1674N/A
1674N/A for (ix = a_len; ix >= 4; ix -= 4) {
1674N/A ADD_SQUARE(0);
1674N/A ADD_SQUARE(1);
1674N/A ADD_SQUARE(2);
1674N/A ADD_SQUARE(3);
1674N/A pa += 4;
1674N/A ps += 8;
1674N/A }
1674N/A if (ix) {
1674N/A ps += 2*ix;
1674N/A pa += ix;
1674N/A switch (ix) {
1674N/A case 3: ADD_SQUARE(-3); /* FALLTHRU */
1674N/A case 2: ADD_SQUARE(-2); /* FALLTHRU */
1674N/A case 1: ADD_SQUARE(-1); /* FALLTHRU */
1674N/A case 0: break;
1674N/A }
1674N/A }
1674N/A while (w) {
1674N/A w += *ps;
1674N/A *ps++ = ACCUM(w);
1674N/A w = (w >> DIGIT_BIT);
1674N/A }
1674N/A#else
1674N/A mp_digit carry = 0;
1674N/A while (a_len--) {
1674N/A mp_digit a_i = *pa++;
1674N/A mp_digit a0a0, a1a1;
1674N/A
1674N/A MP_SQR_D(a_i, a1a1, a0a0);
1674N/A
1674N/A /* here a1a1 and a0a0 constitute a_i ** 2 */
1674N/A a0a0 += carry;
1674N/A if (a0a0 < carry)
1674N/A ++a1a1;
1674N/A
1674N/A /* now add to ps */
1674N/A a0a0 += a_i = *ps;
1674N/A if (a0a0 < a_i)
1674N/A ++a1a1;
1674N/A *ps++ = a0a0;
1674N/A a1a1 += a_i = *ps;
1674N/A carry = (a1a1 < a_i);
1674N/A *ps++ = a1a1;
1674N/A }
1674N/A while (carry) {
1674N/A mp_digit s_i = *ps;
1674N/A carry += s_i;
1674N/A *ps++ = carry;
1674N/A carry = carry < s_i;
1674N/A }
1674N/A#endif
1674N/A}
1674N/A#endif
1674N/A
1674N/A#if (defined(MP_NO_MP_WORD) || defined(MP_NO_DIV_WORD)) \
1674N/A&& !defined(MP_ASSEMBLY_DIV_2DX1D)
1674N/A/*
1674N/A** Divide 64-bit (Nhi,Nlo) by 32-bit divisor, which must be normalized
1674N/A** so its high bit is 1. This code is from NSPR.
1674N/A*/
1674N/Amp_err s_mpv_div_2dx1d(mp_digit Nhi, mp_digit Nlo, mp_digit divisor,
1674N/A mp_digit *qp, mp_digit *rp)
1674N/A{
1674N/A mp_digit d1, d0, q1, q0;
1674N/A mp_digit r1, r0, m;
1674N/A
1674N/A d1 = divisor >> MP_HALF_DIGIT_BIT;
1674N/A d0 = divisor & MP_HALF_DIGIT_MAX;
1674N/A r1 = Nhi % d1;
1674N/A q1 = Nhi / d1;
1674N/A m = q1 * d0;
1674N/A r1 = (r1 << MP_HALF_DIGIT_BIT) | (Nlo >> MP_HALF_DIGIT_BIT);
1674N/A if (r1 < m) {
1674N/A q1--, r1 += divisor;
1674N/A if (r1 >= divisor && r1 < m) {
1674N/A q1--, r1 += divisor;
1674N/A }
1674N/A }
1674N/A r1 -= m;
1674N/A r0 = r1 % d1;
1674N/A q0 = r1 / d1;
1674N/A m = q0 * d0;
1674N/A r0 = (r0 << MP_HALF_DIGIT_BIT) | (Nlo & MP_HALF_DIGIT_MAX);
1674N/A if (r0 < m) {
1674N/A q0--, r0 += divisor;
1674N/A if (r0 >= divisor && r0 < m) {
1674N/A q0--, r0 += divisor;
1674N/A }
1674N/A }
1674N/A if (qp)
1674N/A *qp = (q1 << MP_HALF_DIGIT_BIT) | q0;
1674N/A if (rp)
1674N/A *rp = r0 - m;
1674N/A return MP_OKAY;
1674N/A}
1674N/A#endif
1674N/A
1674N/A#if MP_SQUARE
1674N/A/* {{{ s_mp_sqr(a) */
1674N/A
1674N/Amp_err s_mp_sqr(mp_int *a)
1674N/A{
1674N/A mp_err res;
1674N/A mp_int tmp;
1674N/A
1674N/A if((res = mp_init_size(&tmp, 2 * USED(a), FLAG(a))) != MP_OKAY)
1674N/A return res;
1674N/A res = mp_sqr(a, &tmp);
1674N/A if (res == MP_OKAY) {
1674N/A s_mp_exch(&tmp, a);
1674N/A }
1674N/A mp_clear(&tmp);
1674N/A return res;
1674N/A}
1674N/A
1674N/A/* }}} */
1674N/A#endif
1674N/A
1674N/A/* {{{ s_mp_div(a, b) */
1674N/A
1674N/A/*
1674N/A s_mp_div(a, b)
1674N/A
1674N/A Compute a = a / b and b = a mod b. Assumes b > a.
1674N/A */
1674N/A
1674N/Amp_err s_mp_div(mp_int *rem, /* i: dividend, o: remainder */
1674N/A mp_int *div, /* i: divisor */
1674N/A mp_int *quot) /* i: 0; o: quotient */
1674N/A{
1674N/A mp_int part, t;
1674N/A#if !defined(MP_NO_MP_WORD) && !defined(MP_NO_DIV_WORD)
1674N/A mp_word q_msd;
1674N/A#else
1674N/A mp_digit q_msd;
1674N/A#endif
1674N/A mp_err res;
1674N/A mp_digit d;
1674N/A mp_digit div_msd;
1674N/A int ix;
1674N/A
1674N/A if(mp_cmp_z(div) == 0)
1674N/A return MP_RANGE;
1674N/A
1674N/A /* Shortcut if divisor is power of two */
1674N/A if((ix = s_mp_ispow2(div)) >= 0) {
1674N/A MP_CHECKOK( mp_copy(rem, quot) );
1674N/A s_mp_div_2d(quot, (mp_digit)ix);
1674N/A s_mp_mod_2d(rem, (mp_digit)ix);
1674N/A
1674N/A return MP_OKAY;
1674N/A }
1674N/A
1674N/A DIGITS(&t) = 0;
1674N/A MP_SIGN(rem) = ZPOS;
1674N/A MP_SIGN(div) = ZPOS;
1674N/A
1674N/A /* A working temporary for division */
1674N/A MP_CHECKOK( mp_init_size(&t, MP_ALLOC(rem), FLAG(rem)));
1674N/A
1674N/A /* Normalize to optimize guessing */
1674N/A MP_CHECKOK( s_mp_norm(rem, div, &d) );
1674N/A
1674N/A part = *rem;
1674N/A
1674N/A /* Perform the division itself...woo! */
1674N/A MP_USED(quot) = MP_ALLOC(quot);
1674N/A
1674N/A /* Find a partial substring of rem which is at least div */
1674N/A /* If we didn't find one, we're finished dividing */
1674N/A while (MP_USED(rem) > MP_USED(div) || s_mp_cmp(rem, div) >= 0) {
1674N/A int i;
1674N/A int unusedRem;
1674N/A
1674N/A unusedRem = MP_USED(rem) - MP_USED(div);
1674N/A MP_DIGITS(&part) = MP_DIGITS(rem) + unusedRem;
1674N/A MP_ALLOC(&part) = MP_ALLOC(rem) - unusedRem;
1674N/A MP_USED(&part) = MP_USED(div);
1674N/A if (s_mp_cmp(&part, div) < 0) {
1674N/A -- unusedRem;
1674N/A#if MP_ARGCHK == 2
1674N/A assert(unusedRem >= 0);
1674N/A#endif
1674N/A -- MP_DIGITS(&part);
1674N/A ++ MP_USED(&part);
1674N/A ++ MP_ALLOC(&part);
1674N/A }
1674N/A
1674N/A /* Compute a guess for the next quotient digit */
1674N/A q_msd = MP_DIGIT(&part, MP_USED(&part) - 1);
1674N/A div_msd = MP_DIGIT(div, MP_USED(div) - 1);
1674N/A if (q_msd >= div_msd) {
1674N/A q_msd = 1;
1674N/A } else if (MP_USED(&part) > 1) {
1674N/A#if !defined(MP_NO_MP_WORD) && !defined(MP_NO_DIV_WORD)
1674N/A q_msd = (q_msd << MP_DIGIT_BIT) | MP_DIGIT(&part, MP_USED(&part) - 2);
1674N/A q_msd /= div_msd;
1674N/A if (q_msd == RADIX)
1674N/A --q_msd;
1674N/A#else
1674N/A mp_digit r;
1674N/A MP_CHECKOK( s_mpv_div_2dx1d(q_msd, MP_DIGIT(&part, MP_USED(&part) - 2),
1674N/A div_msd, &q_msd, &r) );
1674N/A#endif
1674N/A } else {
1674N/A q_msd = 0;
1674N/A }
1674N/A#if MP_ARGCHK == 2
1674N/A assert(q_msd > 0); /* This case should never occur any more. */
1674N/A#endif
1674N/A if (q_msd <= 0)
1674N/A break;
1674N/A
1674N/A /* See what that multiplies out to */
1674N/A mp_copy(div, &t);
1674N/A MP_CHECKOK( s_mp_mul_d(&t, (mp_digit)q_msd) );
1674N/A
1674N/A /*
1674N/A If it's too big, back it off. We should not have to do this
1674N/A more than once, or, in rare cases, twice. Knuth describes a
1674N/A method by which this could be reduced to a maximum of once, but
1674N/A I didn't implement that here.
1674N/A * When using s_mpv_div_2dx1d, we may have to do this 3 times.
1674N/A */
1674N/A for (i = 4; s_mp_cmp(&t, &part) > 0 && i > 0; --i) {
1674N/A --q_msd;
1674N/A s_mp_sub(&t, div); /* t -= div */
1674N/A }
1674N/A if (i < 0) {
1674N/A res = MP_RANGE;
1674N/A goto CLEANUP;
1674N/A }
1674N/A
1674N/A /* At this point, q_msd should be the right next digit */
1674N/A MP_CHECKOK( s_mp_sub(&part, &t) ); /* part -= t */
1674N/A s_mp_clamp(rem);
1674N/A
1674N/A /*
1674N/A Include the digit in the quotient. We allocated enough memory
1674N/A for any quotient we could ever possibly get, so we should not
1674N/A have to check for failures here
1674N/A */
1674N/A MP_DIGIT(quot, unusedRem) = (mp_digit)q_msd;
1674N/A }
1674N/A
1674N/A /* Denormalize remainder */
1674N/A if (d) {
1674N/A s_mp_div_2d(rem, d);
1674N/A }
1674N/A
1674N/A s_mp_clamp(quot);
1674N/A
1674N/ACLEANUP:
1674N/A mp_clear(&t);
1674N/A
1674N/A return res;
1674N/A
1674N/A} /* end s_mp_div() */
1674N/A
1674N/A
1674N/A/* }}} */
1674N/A
1674N/A/* {{{ s_mp_2expt(a, k) */
1674N/A
1674N/Amp_err s_mp_2expt(mp_int *a, mp_digit k)
1674N/A{
1674N/A mp_err res;
1674N/A mp_size dig, bit;
1674N/A
1674N/A dig = k / DIGIT_BIT;
1674N/A bit = k % DIGIT_BIT;
1674N/A
1674N/A mp_zero(a);
1674N/A if((res = s_mp_pad(a, dig + 1)) != MP_OKAY)
1674N/A return res;
1674N/A
1674N/A DIGIT(a, dig) |= ((mp_digit)1 << bit);
1674N/A
1674N/A return MP_OKAY;
1674N/A
1674N/A} /* end s_mp_2expt() */
1674N/A
1674N/A/* }}} */
1674N/A
1674N/A/* {{{ s_mp_reduce(x, m, mu) */
1674N/A
1674N/A/*
1674N/A Compute Barrett reduction, x (mod m), given a precomputed value for
1674N/A mu = b^2k / m, where b = RADIX and k = #digits(m). This should be
1674N/A faster than straight division, when many reductions by the same
1674N/A value of m are required (such as in modular exponentiation). This
1674N/A can nearly halve the time required to do modular exponentiation,
1674N/A as compared to using the full integer divide to reduce.
1674N/A
1674N/A This algorithm was derived from the _Handbook of Applied
1674N/A Cryptography_ by Menezes, Oorschot and VanStone, Ch. 14,
1674N/A pp. 603-604.
1674N/A */
1674N/A
1674N/Amp_err s_mp_reduce(mp_int *x, const mp_int *m, const mp_int *mu)
1674N/A{
1674N/A mp_int q;
1674N/A mp_err res;
1674N/A
1674N/A if((res = mp_init_copy(&q, x)) != MP_OKAY)
1674N/A return res;
1674N/A
1674N/A s_mp_rshd(&q, USED(m) - 1); /* q1 = x / b^(k-1) */
1674N/A s_mp_mul(&q, mu); /* q2 = q1 * mu */
1674N/A s_mp_rshd(&q, USED(m) + 1); /* q3 = q2 / b^(k+1) */
1674N/A
1674N/A /* x = x mod b^(k+1), quick (no division) */
1674N/A s_mp_mod_2d(x, DIGIT_BIT * (USED(m) + 1));
1674N/A
1674N/A /* q = q * m mod b^(k+1), quick (no division) */
1674N/A s_mp_mul(&q, m);
1674N/A s_mp_mod_2d(&q, DIGIT_BIT * (USED(m) + 1));
1674N/A
1674N/A /* x = x - q */
1674N/A if((res = mp_sub(x, &q, x)) != MP_OKAY)
1674N/A goto CLEANUP;
1674N/A
1674N/A /* If x < 0, add b^(k+1) to it */
1674N/A if(mp_cmp_z(x) < 0) {
1674N/A mp_set(&q, 1);
1674N/A if((res = s_mp_lshd(&q, USED(m) + 1)) != MP_OKAY)
1674N/A goto CLEANUP;
1674N/A if((res = mp_add(x, &q, x)) != MP_OKAY)
1674N/A goto CLEANUP;
1674N/A }
1674N/A
1674N/A /* Back off if it's too big */
1674N/A while(mp_cmp(x, m) >= 0) {
1674N/A if((res = s_mp_sub(x, m)) != MP_OKAY)
1674N/A break;
1674N/A }
1674N/A
1674N/A CLEANUP:
1674N/A mp_clear(&q);
1674N/A
1674N/A return res;
1674N/A
1674N/A} /* end s_mp_reduce() */
1674N/A
1674N/A/* }}} */
1674N/A
1674N/A/* }}} */
1674N/A
1674N/A/* {{{ Primitive comparisons */
1674N/A
1674N/A/* {{{ s_mp_cmp(a, b) */
1674N/A
1674N/A/* Compare |a| <=> |b|, return 0 if equal, <0 if a<b, >0 if a>b */
1674N/Aint s_mp_cmp(const mp_int *a, const mp_int *b)
1674N/A{
1674N/A mp_size used_a = MP_USED(a);
1674N/A {
1674N/A mp_size used_b = MP_USED(b);
1674N/A
1674N/A if (used_a > used_b)
1674N/A goto IS_GT;
1674N/A if (used_a < used_b)
1674N/A goto IS_LT;
1674N/A }
1674N/A {
1674N/A mp_digit *pa, *pb;
1674N/A mp_digit da = 0, db = 0;
1674N/A
1674N/A#define CMP_AB(n) if ((da = pa[n]) != (db = pb[n])) goto done
1674N/A
1674N/A pa = MP_DIGITS(a) + used_a;
1674N/A pb = MP_DIGITS(b) + used_a;
1674N/A while (used_a >= 4) {
1674N/A pa -= 4;
1674N/A pb -= 4;
1674N/A used_a -= 4;
1674N/A CMP_AB(3);
1674N/A CMP_AB(2);
1674N/A CMP_AB(1);
1674N/A CMP_AB(0);
1674N/A }
1674N/A while (used_a-- > 0 && ((da = *--pa) == (db = *--pb)))
1674N/A /* do nothing */;
1674N/Adone:
1674N/A if (da > db)
1674N/A goto IS_GT;
1674N/A if (da < db)
1674N/A goto IS_LT;
1674N/A }
1674N/A return MP_EQ;
1674N/AIS_LT:
1674N/A return MP_LT;
1674N/AIS_GT:
1674N/A return MP_GT;
1674N/A} /* end s_mp_cmp() */
1674N/A
1674N/A/* }}} */
1674N/A
1674N/A/* {{{ s_mp_cmp_d(a, d) */
1674N/A
1674N/A/* Compare |a| <=> d, return 0 if equal, <0 if a<d, >0 if a>d */
1674N/Aint s_mp_cmp_d(const mp_int *a, mp_digit d)
1674N/A{
1674N/A if(USED(a) > 1)
1674N/A return MP_GT;
1674N/A
1674N/A if(DIGIT(a, 0) < d)
1674N/A return MP_LT;
1674N/A else if(DIGIT(a, 0) > d)
1674N/A return MP_GT;
1674N/A else
1674N/A return MP_EQ;
1674N/A
1674N/A} /* end s_mp_cmp_d() */
1674N/A
1674N/A/* }}} */
1674N/A
1674N/A/* {{{ s_mp_ispow2(v) */
1674N/A
1674N/A/*
1674N/A Returns -1 if the value is not a power of two; otherwise, it returns
1674N/A k such that v = 2^k, i.e. lg(v).
1674N/A */
1674N/Aint s_mp_ispow2(const mp_int *v)
1674N/A{
1674N/A mp_digit d;
1674N/A int extra = 0, ix;
1674N/A
1674N/A ix = MP_USED(v) - 1;
1674N/A d = MP_DIGIT(v, ix); /* most significant digit of v */
1674N/A
1674N/A extra = s_mp_ispow2d(d);
1674N/A if (extra < 0 || ix == 0)
1674N/A return extra;
1674N/A
1674N/A while (--ix >= 0) {
1674N/A if (DIGIT(v, ix) != 0)
1674N/A return -1; /* not a power of two */
1674N/A extra += MP_DIGIT_BIT;
1674N/A }
1674N/A
1674N/A return extra;
1674N/A
1674N/A} /* end s_mp_ispow2() */
1674N/A
1674N/A/* }}} */
1674N/A
1674N/A/* {{{ s_mp_ispow2d(d) */
1674N/A
1674N/Aint s_mp_ispow2d(mp_digit d)
1674N/A{
1674N/A if ((d != 0) && ((d & (d-1)) == 0)) { /* d is a power of 2 */
1674N/A int pow = 0;
1674N/A#if defined (MP_USE_UINT_DIGIT)
1674N/A if (d & 0xffff0000U)
1674N/A pow += 16;
1674N/A if (d & 0xff00ff00U)
1674N/A pow += 8;
1674N/A if (d & 0xf0f0f0f0U)
1674N/A pow += 4;
1674N/A if (d & 0xccccccccU)
1674N/A pow += 2;
1674N/A if (d & 0xaaaaaaaaU)
1674N/A pow += 1;
1674N/A#elif defined(MP_USE_LONG_LONG_DIGIT)
1674N/A if (d & 0xffffffff00000000ULL)
1674N/A pow += 32;
1674N/A if (d & 0xffff0000ffff0000ULL)
1674N/A pow += 16;
1674N/A if (d & 0xff00ff00ff00ff00ULL)
1674N/A pow += 8;
1674N/A if (d & 0xf0f0f0f0f0f0f0f0ULL)
1674N/A pow += 4;
1674N/A if (d & 0xccccccccccccccccULL)
1674N/A pow += 2;
1674N/A if (d & 0xaaaaaaaaaaaaaaaaULL)
1674N/A pow += 1;
1674N/A#elif defined(MP_USE_LONG_DIGIT)
1674N/A if (d & 0xffffffff00000000UL)
1674N/A pow += 32;
1674N/A if (d & 0xffff0000ffff0000UL)
1674N/A pow += 16;
1674N/A if (d & 0xff00ff00ff00ff00UL)
1674N/A pow += 8;
1674N/A if (d & 0xf0f0f0f0f0f0f0f0UL)
1674N/A pow += 4;
1674N/A if (d & 0xccccccccccccccccUL)
1674N/A pow += 2;
1674N/A if (d & 0xaaaaaaaaaaaaaaaaUL)
1674N/A pow += 1;
1674N/A#else
1674N/A#error "unknown type for mp_digit"
1674N/A#endif
1674N/A return pow;
1674N/A }
1674N/A return -1;
1674N/A
1674N/A} /* end s_mp_ispow2d() */
1674N/A
1674N/A/* }}} */
1674N/A
1674N/A/* }}} */
1674N/A
1674N/A/* {{{ Primitive I/O helpers */
1674N/A
1674N/A/* {{{ s_mp_tovalue(ch, r) */
1674N/A
1674N/A/*
1674N/A Convert the given character to its digit value, in the given radix.
1674N/A If the given character is not understood in the given radix, -1 is
1674N/A returned. Otherwise the digit's numeric value is returned.
1674N/A
1674N/A The results will be odd if you use a radix < 2 or > 62, you are
1674N/A expected to know what you're up to.
1674N/A */
1674N/Aint s_mp_tovalue(char ch, int r)
1674N/A{
1674N/A int val, xch;
1674N/A
1674N/A if(r > 36)
1674N/A xch = ch;
1674N/A else
1674N/A xch = toupper(ch);
1674N/A
1674N/A if(isdigit(xch))
1674N/A val = xch - '0';
1674N/A else if(isupper(xch))
1674N/A val = xch - 'A' + 10;
1674N/A else if(islower(xch))
1674N/A val = xch - 'a' + 36;
1674N/A else if(xch == '+')
1674N/A val = 62;
1674N/A else if(xch == '/')
1674N/A val = 63;
1674N/A else
1674N/A return -1;
1674N/A
1674N/A if(val < 0 || val >= r)
1674N/A return -1;
1674N/A
1674N/A return val;
1674N/A
1674N/A} /* end s_mp_tovalue() */
1674N/A
1674N/A/* }}} */
1674N/A
1674N/A/* {{{ s_mp_todigit(val, r, low) */
1674N/A
1674N/A/*
1674N/A Convert val to a radix-r digit, if possible. If val is out of range
1674N/A for r, returns zero. Otherwise, returns an ASCII character denoting
1674N/A the value in the given radix.
1674N/A
1674N/A The results may be odd if you use a radix < 2 or > 64, you are
1674N/A expected to know what you're doing.
1674N/A */
1674N/A
1674N/Achar s_mp_todigit(mp_digit val, int r, int low)
1674N/A{
1674N/A char ch;
1674N/A
3488N/A if(val >= (unsigned int)r)
1674N/A return 0;
1674N/A
1674N/A ch = s_dmap_1[val];
1674N/A
1674N/A if(r <= 36 && low)
1674N/A ch = tolower(ch);
1674N/A
1674N/A return ch;
1674N/A
1674N/A} /* end s_mp_todigit() */
1674N/A
1674N/A/* }}} */
1674N/A
1674N/A/* {{{ s_mp_outlen(bits, radix) */
1674N/A
1674N/A/*
1674N/A Return an estimate for how long a string is needed to hold a radix
1674N/A r representation of a number with 'bits' significant bits, plus an
1674N/A extra for a zero terminator (assuming C style strings here)
1674N/A */
1674N/Aint s_mp_outlen(int bits, int r)
1674N/A{
1674N/A return (int)((double)bits * LOG_V_2(r) + 1.5) + 1;
1674N/A
1674N/A} /* end s_mp_outlen() */
1674N/A
1674N/A/* }}} */
1674N/A
1674N/A/* }}} */
1674N/A
1674N/A/* {{{ mp_read_unsigned_octets(mp, str, len) */
1674N/A/* mp_read_unsigned_octets(mp, str, len)
1674N/A Read in a raw value (base 256) into the given mp_int
1674N/A No sign bit, number is positive. Leading zeros ignored.
1674N/A */
1674N/A
1674N/Amp_err
1674N/Amp_read_unsigned_octets(mp_int *mp, const unsigned char *str, mp_size len)
1674N/A{
1674N/A int count;
1674N/A mp_err res;
1674N/A mp_digit d;
1674N/A
1674N/A ARGCHK(mp != NULL && str != NULL && len > 0, MP_BADARG);
1674N/A
1674N/A mp_zero(mp);
1674N/A
1674N/A count = len % sizeof(mp_digit);
1674N/A if (count) {
1674N/A for (d = 0; count-- > 0; --len) {
1674N/A d = (d << 8) | *str++;
1674N/A }
1674N/A MP_DIGIT(mp, 0) = d;
1674N/A }
1674N/A
1674N/A /* Read the rest of the digits */
1674N/A for(; len > 0; len -= sizeof(mp_digit)) {
1674N/A for (d = 0, count = sizeof(mp_digit); count > 0; --count) {
1674N/A d = (d << 8) | *str++;
1674N/A }
1674N/A if (MP_EQ == mp_cmp_z(mp)) {
1674N/A if (!d)
1674N/A continue;
1674N/A } else {
1674N/A if((res = s_mp_lshd(mp, 1)) != MP_OKAY)
1674N/A return res;
1674N/A }
1674N/A MP_DIGIT(mp, 0) = d;
1674N/A }
1674N/A return MP_OKAY;
1674N/A} /* end mp_read_unsigned_octets() */
1674N/A/* }}} */
1674N/A
1674N/A/* {{{ mp_unsigned_octet_size(mp) */
1674N/Aint
1674N/Amp_unsigned_octet_size(const mp_int *mp)
1674N/A{
1674N/A int bytes;
1674N/A int ix;
1674N/A mp_digit d = 0;
1674N/A
1674N/A ARGCHK(mp != NULL, MP_BADARG);
1674N/A ARGCHK(MP_ZPOS == SIGN(mp), MP_BADARG);
1674N/A
1674N/A bytes = (USED(mp) * sizeof(mp_digit));
1674N/A
1674N/A /* subtract leading zeros. */
1674N/A /* Iterate over each digit... */
1674N/A for(ix = USED(mp) - 1; ix >= 0; ix--) {
1674N/A d = DIGIT(mp, ix);
1674N/A if (d)
1674N/A break;
1674N/A bytes -= sizeof(d);
1674N/A }
1674N/A if (!bytes)
1674N/A return 1;
1674N/A
1674N/A /* Have MSD, check digit bytes, high order first */
1674N/A for(ix = sizeof(mp_digit) - 1; ix >= 0; ix--) {
1674N/A unsigned char x = (unsigned char)(d >> (ix * CHAR_BIT));
1674N/A if (x)
1674N/A break;
1674N/A --bytes;
1674N/A }
1674N/A return bytes;
1674N/A} /* end mp_unsigned_octet_size() */
1674N/A/* }}} */
1674N/A
1674N/A/* {{{ mp_to_unsigned_octets(mp, str) */
1674N/A/* output a buffer of big endian octets no longer than specified. */
1674N/Amp_err
1674N/Amp_to_unsigned_octets(const mp_int *mp, unsigned char *str, mp_size maxlen)
1674N/A{
1674N/A int ix, pos = 0;
3488N/A unsigned int bytes;
1674N/A
1674N/A ARGCHK(mp != NULL && str != NULL && !SIGN(mp), MP_BADARG);
1674N/A
1674N/A bytes = mp_unsigned_octet_size(mp);
1674N/A ARGCHK(bytes <= maxlen, MP_BADARG);
1674N/A
1674N/A /* Iterate over each digit... */
1674N/A for(ix = USED(mp) - 1; ix >= 0; ix--) {
1674N/A mp_digit d = DIGIT(mp, ix);
1674N/A int jx;
1674N/A
1674N/A /* Unpack digit bytes, high order first */
1674N/A for(jx = sizeof(mp_digit) - 1; jx >= 0; jx--) {
1674N/A unsigned char x = (unsigned char)(d >> (jx * CHAR_BIT));
1674N/A if (!pos && !x) /* suppress leading zeros */
1674N/A continue;
1674N/A str[pos++] = x;
1674N/A }
1674N/A }
1674N/A if (!pos)
1674N/A str[pos++] = 0;
1674N/A return pos;
1674N/A} /* end mp_to_unsigned_octets() */
1674N/A/* }}} */
1674N/A
1674N/A/* {{{ mp_to_signed_octets(mp, str) */
1674N/A/* output a buffer of big endian octets no longer than specified. */
1674N/Amp_err
1674N/Amp_to_signed_octets(const mp_int *mp, unsigned char *str, mp_size maxlen)
1674N/A{
1674N/A int ix, pos = 0;
3488N/A unsigned int bytes;
1674N/A
1674N/A ARGCHK(mp != NULL && str != NULL && !SIGN(mp), MP_BADARG);
1674N/A
1674N/A bytes = mp_unsigned_octet_size(mp);
1674N/A ARGCHK(bytes <= maxlen, MP_BADARG);
1674N/A
1674N/A /* Iterate over each digit... */
1674N/A for(ix = USED(mp) - 1; ix >= 0; ix--) {
1674N/A mp_digit d = DIGIT(mp, ix);
1674N/A int jx;
1674N/A
1674N/A /* Unpack digit bytes, high order first */
1674N/A for(jx = sizeof(mp_digit) - 1; jx >= 0; jx--) {
1674N/A unsigned char x = (unsigned char)(d >> (jx * CHAR_BIT));
1674N/A if (!pos) {
1674N/A if (!x) /* suppress leading zeros */
1674N/A continue;
1674N/A if (x & 0x80) { /* add one leading zero to make output positive. */
1674N/A ARGCHK(bytes + 1 <= maxlen, MP_BADARG);
1674N/A if (bytes + 1 > maxlen)
1674N/A return MP_BADARG;
1674N/A str[pos++] = 0;
1674N/A }
1674N/A }
1674N/A str[pos++] = x;
1674N/A }
1674N/A }
1674N/A if (!pos)
1674N/A str[pos++] = 0;
1674N/A return pos;
1674N/A} /* end mp_to_signed_octets() */
1674N/A/* }}} */
1674N/A
1674N/A/* {{{ mp_to_fixlen_octets(mp, str) */
1674N/A/* output a buffer of big endian octets exactly as long as requested. */
1674N/Amp_err
1674N/Amp_to_fixlen_octets(const mp_int *mp, unsigned char *str, mp_size length)
1674N/A{
1674N/A int ix, pos = 0;
3488N/A unsigned int bytes;
1674N/A
1674N/A ARGCHK(mp != NULL && str != NULL && !SIGN(mp), MP_BADARG);
1674N/A
1674N/A bytes = mp_unsigned_octet_size(mp);
1674N/A ARGCHK(bytes <= length, MP_BADARG);
1674N/A
1674N/A /* place any needed leading zeros */
1674N/A for (;length > bytes; --length) {
1674N/A *str++ = 0;
1674N/A }
1674N/A
1674N/A /* Iterate over each digit... */
1674N/A for(ix = USED(mp) - 1; ix >= 0; ix--) {
1674N/A mp_digit d = DIGIT(mp, ix);
1674N/A int jx;
1674N/A
1674N/A /* Unpack digit bytes, high order first */
1674N/A for(jx = sizeof(mp_digit) - 1; jx >= 0; jx--) {
1674N/A unsigned char x = (unsigned char)(d >> (jx * CHAR_BIT));
1674N/A if (!pos && !x) /* suppress leading zeros */
1674N/A continue;
1674N/A str[pos++] = x;
1674N/A }
1674N/A }
1674N/A if (!pos)
1674N/A str[pos++] = 0;
1674N/A return MP_OKAY;
1674N/A} /* end mp_to_fixlen_octets() */
1674N/A/* }}} */
1674N/A
1674N/A
1674N/A/*------------------------------------------------------------------------*/
1674N/A/* HERE THERE BE DRAGONS */