jdk/src/share/native/sun/security/ec/impl/mpi.c
author ohair
Wed, 06 Apr 2011 22:06:11 -0700
changeset 9035 1255eb81cc2f
parent 8175 27e80dc06104
child 9774 50a2b28ca54c
permissions -rw-r--r--
7033660: Update copyright year to 2011 on any files changed in 2011 Reviewed-by: dholmes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
3863
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
     1
/* *********************************************************************
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
     2
 *
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
     3
 * Sun elects to have this file available under and governed by the
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
     4
 * Mozilla Public License Version 1.1 ("MPL") (see
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
     5
 * http://www.mozilla.org/MPL/ for full license text). For the avoidance
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
     6
 * of doubt and subject to the following, Sun also elects to allow
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
     7
 * licensees to use this file under the MPL, the GNU General Public
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
     8
 * License version 2 only or the Lesser General Public License version
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
     9
 * 2.1 only. Any references to the "GNU General Public License version 2
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    10
 * or later" or "GPL" in the following shall be construed to mean the
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    11
 * GNU General Public License version 2 only. Any references to the "GNU
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    12
 * Lesser General Public License version 2.1 or later" or "LGPL" in the
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    13
 * following shall be construed to mean the GNU Lesser General Public
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    14
 * License version 2.1 only. However, the following notice accompanied
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    15
 * the original version of this file:
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    16
 *
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    17
 *
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    18
 *  Arbitrary precision integer arithmetic library
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    19
 *
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    20
 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    21
 *
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    22
 * The contents of this file are subject to the Mozilla Public License Version
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    23
 * 1.1 (the "License"); you may not use this file except in compliance with
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    24
 * the License. You may obtain a copy of the License at
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    25
 * http://www.mozilla.org/MPL/
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    26
 *
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    27
 * Software distributed under the License is distributed on an "AS IS" basis,
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    28
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    29
 * for the specific language governing rights and limitations under the
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    30
 * License.
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    31
 *
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    32
 * The Original Code is the MPI Arbitrary Precision Integer Arithmetic library.
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    33
 *
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    34
 * The Initial Developer of the Original Code is
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    35
 * Michael J. Fromberger.
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    36
 * Portions created by the Initial Developer are Copyright (C) 1998
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    37
 * the Initial Developer. All Rights Reserved.
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    38
 *
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    39
 * Contributor(s):
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    40
 *   Netscape Communications Corporation
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    41
 *   Douglas Stebila <douglas@stebila.ca> of Sun Laboratories.
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    42
 *
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    43
 * Alternatively, the contents of this file may be used under the terms of
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    44
 * either the GNU General Public License Version 2 or later (the "GPL"), or
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    45
 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    46
 * in which case the provisions of the GPL or the LGPL are applicable instead
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    47
 * of those above. If you wish to allow use of your version of this file only
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    48
 * under the terms of either the GPL or the LGPL, and not to allow others to
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    49
 * use your version of this file under the terms of the MPL, indicate your
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    50
 * decision by deleting the provisions above and replace them with the notice
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    51
 * and other provisions required by the GPL or the LGPL. If you do not delete
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    52
 * the provisions above, a recipient may use your version of this file under
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    53
 * the terms of any one of the MPL, the GPL or the LGPL.
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    54
 *
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    55
 *********************************************************************** */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    56
/*
9035
1255eb81cc2f 7033660: Update copyright year to 2011 on any files changed in 2011
ohair
parents: 8175
diff changeset
    57
 * Copyright (c) 2007, 2011, Oracle and/or its affiliates. All rights reserved.
3863
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    58
 * Use is subject to license terms.
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    59
 */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    60
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    61
/* $Id: mpi.c,v 1.45 2006/09/29 20:12:21 alexei.volkov.bugs%sun.com Exp $ */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    62
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    63
#include "mpi-priv.h"
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    64
#if defined(OSF1)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    65
#include <c_asm.h>
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    66
#endif
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    67
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    68
#if MP_LOGTAB
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    69
/*
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    70
  A table of the logs of 2 for various bases (the 0 and 1 entries of
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    71
  this table are meaningless and should not be referenced).
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    72
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    73
  This table is used to compute output lengths for the mp_toradix()
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    74
  function.  Since a number n in radix r takes up about log_r(n)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    75
  digits, we estimate the output size by taking the least integer
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    76
  greater than log_r(n), where:
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    77
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    78
  log_r(n) = log_2(n) * log_r(2)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    79
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    80
  This table, therefore, is a table of log_r(2) for 2 <= r <= 36,
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    81
  which are the output bases supported.
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    82
 */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    83
#include "logtab.h"
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    84
#endif
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    85
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    86
/* {{{ Constant strings */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    87
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    88
/* Constant strings returned by mp_strerror() */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    89
static const char *mp_err_string[] = {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    90
  "unknown result code",     /* say what?            */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    91
  "boolean true",            /* MP_OKAY, MP_YES      */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    92
  "boolean false",           /* MP_NO                */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    93
  "out of memory",           /* MP_MEM               */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    94
  "argument out of range",   /* MP_RANGE             */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    95
  "invalid input parameter", /* MP_BADARG            */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    96
  "result is undefined"      /* MP_UNDEF             */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    97
};
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    98
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
    99
/* Value to digit maps for radix conversion   */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   100
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   101
/* s_dmap_1 - standard digits and letters */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   102
static const char *s_dmap_1 =
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   103
  "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz+/";
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   104
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   105
/* }}} */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   106
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   107
unsigned long mp_allocs;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   108
unsigned long mp_frees;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   109
unsigned long mp_copies;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   110
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   111
/* {{{ Default precision manipulation */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   112
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   113
/* Default precision for newly created mp_int's      */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   114
static mp_size s_mp_defprec = MP_DEFPREC;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   115
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   116
mp_size mp_get_prec(void)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   117
{
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   118
  return s_mp_defprec;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   119
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   120
} /* end mp_get_prec() */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   121
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   122
void         mp_set_prec(mp_size prec)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   123
{
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   124
  if(prec == 0)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   125
    s_mp_defprec = MP_DEFPREC;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   126
  else
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   127
    s_mp_defprec = prec;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   128
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   129
} /* end mp_set_prec() */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   130
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   131
/* }}} */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   132
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   133
/*------------------------------------------------------------------------*/
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   134
/* {{{ mp_init(mp, kmflag) */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   135
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   136
/*
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   137
  mp_init(mp, kmflag)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   138
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   139
  Initialize a new zero-valued mp_int.  Returns MP_OKAY if successful,
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   140
  MP_MEM if memory could not be allocated for the structure.
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   141
 */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   142
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   143
mp_err mp_init(mp_int *mp, int kmflag)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   144
{
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   145
  return mp_init_size(mp, s_mp_defprec, kmflag);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   146
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   147
} /* end mp_init() */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   148
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   149
/* }}} */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   150
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   151
/* {{{ mp_init_size(mp, prec, kmflag) */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   152
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   153
/*
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   154
  mp_init_size(mp, prec, kmflag)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   155
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   156
  Initialize a new zero-valued mp_int with at least the given
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   157
  precision; returns MP_OKAY if successful, or MP_MEM if memory could
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   158
  not be allocated for the structure.
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   159
 */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   160
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   161
mp_err mp_init_size(mp_int *mp, mp_size prec, int kmflag)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   162
{
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   163
  ARGCHK(mp != NULL && prec > 0, MP_BADARG);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   164
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   165
  prec = MP_ROUNDUP(prec, s_mp_defprec);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   166
  if((DIGITS(mp) = s_mp_alloc(prec, sizeof(mp_digit), kmflag)) == NULL)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   167
    return MP_MEM;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   168
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   169
  SIGN(mp) = ZPOS;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   170
  USED(mp) = 1;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   171
  ALLOC(mp) = prec;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   172
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   173
  return MP_OKAY;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   174
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   175
} /* end mp_init_size() */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   176
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   177
/* }}} */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   178
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   179
/* {{{ mp_init_copy(mp, from) */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   180
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   181
/*
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   182
  mp_init_copy(mp, from)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   183
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   184
  Initialize mp as an exact copy of from.  Returns MP_OKAY if
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   185
  successful, MP_MEM if memory could not be allocated for the new
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   186
  structure.
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   187
 */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   188
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   189
mp_err mp_init_copy(mp_int *mp, const mp_int *from)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   190
{
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   191
  ARGCHK(mp != NULL && from != NULL, MP_BADARG);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   192
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   193
  if(mp == from)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   194
    return MP_OKAY;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   195
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   196
  if((DIGITS(mp) = s_mp_alloc(ALLOC(from), sizeof(mp_digit), FLAG(from))) == NULL)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   197
    return MP_MEM;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   198
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   199
  s_mp_copy(DIGITS(from), DIGITS(mp), USED(from));
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   200
  USED(mp) = USED(from);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   201
  ALLOC(mp) = ALLOC(from);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   202
  SIGN(mp) = SIGN(from);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   203
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   204
#ifndef _WIN32
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   205
  FLAG(mp) = FLAG(from);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   206
#endif /* _WIN32 */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   207
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   208
  return MP_OKAY;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   209
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   210
} /* end mp_init_copy() */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   211
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   212
/* }}} */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   213
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   214
/* {{{ mp_copy(from, to) */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   215
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   216
/*
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   217
  mp_copy(from, to)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   218
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   219
  Copies the mp_int 'from' to the mp_int 'to'.  It is presumed that
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   220
  'to' has already been initialized (if not, use mp_init_copy()
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   221
  instead). If 'from' and 'to' are identical, nothing happens.
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   222
 */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   223
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   224
mp_err mp_copy(const mp_int *from, mp_int *to)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   225
{
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   226
  ARGCHK(from != NULL && to != NULL, MP_BADARG);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   227
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   228
  if(from == to)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   229
    return MP_OKAY;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   230
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   231
  ++mp_copies;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   232
  { /* copy */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   233
    mp_digit   *tmp;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   234
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   235
    /*
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   236
      If the allocated buffer in 'to' already has enough space to hold
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   237
      all the used digits of 'from', we'll re-use it to avoid hitting
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   238
      the memory allocater more than necessary; otherwise, we'd have
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   239
      to grow anyway, so we just allocate a hunk and make the copy as
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   240
      usual
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   241
     */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   242
    if(ALLOC(to) >= USED(from)) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   243
      s_mp_setz(DIGITS(to) + USED(from), ALLOC(to) - USED(from));
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   244
      s_mp_copy(DIGITS(from), DIGITS(to), USED(from));
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   245
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   246
    } else {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   247
      if((tmp = s_mp_alloc(ALLOC(from), sizeof(mp_digit), FLAG(from))) == NULL)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   248
        return MP_MEM;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   249
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   250
      s_mp_copy(DIGITS(from), tmp, USED(from));
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   251
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   252
      if(DIGITS(to) != NULL) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   253
#if MP_CRYPTO
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   254
        s_mp_setz(DIGITS(to), ALLOC(to));
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   255
#endif
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   256
        s_mp_free(DIGITS(to), ALLOC(to));
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   257
      }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   258
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   259
      DIGITS(to) = tmp;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   260
      ALLOC(to) = ALLOC(from);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   261
    }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   262
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   263
    /* Copy the precision and sign from the original */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   264
    USED(to) = USED(from);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   265
    SIGN(to) = SIGN(from);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   266
  } /* end copy */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   267
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   268
  return MP_OKAY;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   269
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   270
} /* end mp_copy() */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   271
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   272
/* }}} */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   273
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   274
/* {{{ mp_exch(mp1, mp2) */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   275
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   276
/*
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   277
  mp_exch(mp1, mp2)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   278
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   279
  Exchange mp1 and mp2 without allocating any intermediate memory
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   280
  (well, unless you count the stack space needed for this call and the
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   281
  locals it creates...).  This cannot fail.
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   282
 */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   283
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   284
void mp_exch(mp_int *mp1, mp_int *mp2)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   285
{
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   286
#if MP_ARGCHK == 2
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   287
  assert(mp1 != NULL && mp2 != NULL);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   288
#else
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   289
  if(mp1 == NULL || mp2 == NULL)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   290
    return;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   291
#endif
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   292
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   293
  s_mp_exch(mp1, mp2);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   294
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   295
} /* end mp_exch() */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   296
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   297
/* }}} */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   298
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   299
/* {{{ mp_clear(mp) */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   300
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   301
/*
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   302
  mp_clear(mp)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   303
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   304
  Release the storage used by an mp_int, and void its fields so that
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   305
  if someone calls mp_clear() again for the same int later, we won't
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   306
  get tollchocked.
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   307
 */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   308
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   309
void   mp_clear(mp_int *mp)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   310
{
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   311
  if(mp == NULL)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   312
    return;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   313
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   314
  if(DIGITS(mp) != NULL) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   315
#if MP_CRYPTO
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   316
    s_mp_setz(DIGITS(mp), ALLOC(mp));
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   317
#endif
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   318
    s_mp_free(DIGITS(mp), ALLOC(mp));
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   319
    DIGITS(mp) = NULL;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   320
  }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   321
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   322
  USED(mp) = 0;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   323
  ALLOC(mp) = 0;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   324
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   325
} /* end mp_clear() */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   326
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   327
/* }}} */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   328
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   329
/* {{{ mp_zero(mp) */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   330
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   331
/*
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   332
  mp_zero(mp)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   333
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   334
  Set mp to zero.  Does not change the allocated size of the structure,
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   335
  and therefore cannot fail (except on a bad argument, which we ignore)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   336
 */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   337
void   mp_zero(mp_int *mp)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   338
{
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   339
  if(mp == NULL)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   340
    return;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   341
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   342
  s_mp_setz(DIGITS(mp), ALLOC(mp));
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   343
  USED(mp) = 1;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   344
  SIGN(mp) = ZPOS;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   345
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   346
} /* end mp_zero() */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   347
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   348
/* }}} */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   349
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   350
/* {{{ mp_set(mp, d) */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   351
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   352
void   mp_set(mp_int *mp, mp_digit d)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   353
{
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   354
  if(mp == NULL)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   355
    return;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   356
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   357
  mp_zero(mp);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   358
  DIGIT(mp, 0) = d;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   359
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   360
} /* end mp_set() */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   361
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   362
/* }}} */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   363
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   364
/* {{{ mp_set_int(mp, z) */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   365
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   366
mp_err mp_set_int(mp_int *mp, long z)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   367
{
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   368
  int            ix;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   369
  unsigned long  v = labs(z);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   370
  mp_err         res;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   371
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   372
  ARGCHK(mp != NULL, MP_BADARG);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   373
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   374
  mp_zero(mp);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   375
  if(z == 0)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   376
    return MP_OKAY;  /* shortcut for zero */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   377
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   378
  if (sizeof v <= sizeof(mp_digit)) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   379
    DIGIT(mp,0) = v;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   380
  } else {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   381
    for (ix = sizeof(long) - 1; ix >= 0; ix--) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   382
      if ((res = s_mp_mul_d(mp, (UCHAR_MAX + 1))) != MP_OKAY)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   383
        return res;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   384
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   385
      res = s_mp_add_d(mp, (mp_digit)((v >> (ix * CHAR_BIT)) & UCHAR_MAX));
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   386
      if (res != MP_OKAY)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   387
        return res;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   388
    }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   389
  }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   390
  if(z < 0)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   391
    SIGN(mp) = NEG;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   392
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   393
  return MP_OKAY;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   394
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   395
} /* end mp_set_int() */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   396
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   397
/* }}} */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   398
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   399
/* {{{ mp_set_ulong(mp, z) */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   400
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   401
mp_err mp_set_ulong(mp_int *mp, unsigned long z)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   402
{
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   403
  int            ix;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   404
  mp_err         res;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   405
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   406
  ARGCHK(mp != NULL, MP_BADARG);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   407
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   408
  mp_zero(mp);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   409
  if(z == 0)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   410
    return MP_OKAY;  /* shortcut for zero */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   411
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   412
  if (sizeof z <= sizeof(mp_digit)) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   413
    DIGIT(mp,0) = z;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   414
  } else {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   415
    for (ix = sizeof(long) - 1; ix >= 0; ix--) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   416
      if ((res = s_mp_mul_d(mp, (UCHAR_MAX + 1))) != MP_OKAY)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   417
        return res;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   418
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   419
      res = s_mp_add_d(mp, (mp_digit)((z >> (ix * CHAR_BIT)) & UCHAR_MAX));
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   420
      if (res != MP_OKAY)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   421
        return res;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   422
    }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   423
  }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   424
  return MP_OKAY;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   425
} /* end mp_set_ulong() */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   426
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   427
/* }}} */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   428
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   429
/*------------------------------------------------------------------------*/
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   430
/* {{{ Digit arithmetic */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   431
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   432
/* {{{ mp_add_d(a, d, b) */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   433
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   434
/*
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   435
  mp_add_d(a, d, b)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   436
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   437
  Compute the sum b = a + d, for a single digit d.  Respects the sign of
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   438
  its primary addend (single digits are unsigned anyway).
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   439
 */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   440
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   441
mp_err mp_add_d(const mp_int *a, mp_digit d, mp_int *b)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   442
{
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   443
  mp_int   tmp;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   444
  mp_err   res;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   445
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   446
  ARGCHK(a != NULL && b != NULL, MP_BADARG);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   447
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   448
  if((res = mp_init_copy(&tmp, a)) != MP_OKAY)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   449
    return res;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   450
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   451
  if(SIGN(&tmp) == ZPOS) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   452
    if((res = s_mp_add_d(&tmp, d)) != MP_OKAY)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   453
      goto CLEANUP;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   454
  } else if(s_mp_cmp_d(&tmp, d) >= 0) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   455
    if((res = s_mp_sub_d(&tmp, d)) != MP_OKAY)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   456
      goto CLEANUP;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   457
  } else {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   458
    mp_neg(&tmp, &tmp);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   459
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   460
    DIGIT(&tmp, 0) = d - DIGIT(&tmp, 0);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   461
  }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   462
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   463
  if(s_mp_cmp_d(&tmp, 0) == 0)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   464
    SIGN(&tmp) = ZPOS;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   465
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   466
  s_mp_exch(&tmp, b);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   467
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   468
CLEANUP:
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   469
  mp_clear(&tmp);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   470
  return res;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   471
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   472
} /* end mp_add_d() */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   473
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   474
/* }}} */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   475
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   476
/* {{{ mp_sub_d(a, d, b) */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   477
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   478
/*
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   479
  mp_sub_d(a, d, b)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   480
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   481
  Compute the difference b = a - d, for a single digit d.  Respects the
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   482
  sign of its subtrahend (single digits are unsigned anyway).
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   483
 */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   484
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   485
mp_err mp_sub_d(const mp_int *a, mp_digit d, mp_int *b)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   486
{
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   487
  mp_int   tmp;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   488
  mp_err   res;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   489
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   490
  ARGCHK(a != NULL && b != NULL, MP_BADARG);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   491
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   492
  if((res = mp_init_copy(&tmp, a)) != MP_OKAY)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   493
    return res;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   494
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   495
  if(SIGN(&tmp) == NEG) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   496
    if((res = s_mp_add_d(&tmp, d)) != MP_OKAY)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   497
      goto CLEANUP;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   498
  } else if(s_mp_cmp_d(&tmp, d) >= 0) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   499
    if((res = s_mp_sub_d(&tmp, d)) != MP_OKAY)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   500
      goto CLEANUP;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   501
  } else {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   502
    mp_neg(&tmp, &tmp);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   503
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   504
    DIGIT(&tmp, 0) = d - DIGIT(&tmp, 0);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   505
    SIGN(&tmp) = NEG;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   506
  }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   507
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   508
  if(s_mp_cmp_d(&tmp, 0) == 0)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   509
    SIGN(&tmp) = ZPOS;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   510
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   511
  s_mp_exch(&tmp, b);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   512
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   513
CLEANUP:
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   514
  mp_clear(&tmp);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   515
  return res;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   516
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   517
} /* end mp_sub_d() */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   518
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   519
/* }}} */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   520
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   521
/* {{{ mp_mul_d(a, d, b) */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   522
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   523
/*
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   524
  mp_mul_d(a, d, b)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   525
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   526
  Compute the product b = a * d, for a single digit d.  Respects the sign
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   527
  of its multiplicand (single digits are unsigned anyway)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   528
 */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   529
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   530
mp_err mp_mul_d(const mp_int *a, mp_digit d, mp_int *b)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   531
{
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   532
  mp_err  res;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   533
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   534
  ARGCHK(a != NULL && b != NULL, MP_BADARG);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   535
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   536
  if(d == 0) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   537
    mp_zero(b);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   538
    return MP_OKAY;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   539
  }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   540
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   541
  if((res = mp_copy(a, b)) != MP_OKAY)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   542
    return res;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   543
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   544
  res = s_mp_mul_d(b, d);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   545
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   546
  return res;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   547
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   548
} /* end mp_mul_d() */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   549
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   550
/* }}} */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   551
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   552
/* {{{ mp_mul_2(a, c) */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   553
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   554
mp_err mp_mul_2(const mp_int *a, mp_int *c)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   555
{
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   556
  mp_err  res;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   557
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   558
  ARGCHK(a != NULL && c != NULL, MP_BADARG);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   559
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   560
  if((res = mp_copy(a, c)) != MP_OKAY)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   561
    return res;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   562
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   563
  return s_mp_mul_2(c);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   564
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   565
} /* end mp_mul_2() */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   566
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   567
/* }}} */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   568
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   569
/* {{{ mp_div_d(a, d, q, r) */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   570
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   571
/*
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   572
  mp_div_d(a, d, q, r)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   573
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   574
  Compute the quotient q = a / d and remainder r = a mod d, for a
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   575
  single digit d.  Respects the sign of its divisor (single digits are
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   576
  unsigned anyway).
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   577
 */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   578
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   579
mp_err mp_div_d(const mp_int *a, mp_digit d, mp_int *q, mp_digit *r)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   580
{
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   581
  mp_err   res;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   582
  mp_int   qp;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   583
  mp_digit rem;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   584
  int      pow;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   585
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   586
  ARGCHK(a != NULL, MP_BADARG);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   587
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   588
  if(d == 0)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   589
    return MP_RANGE;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   590
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   591
  /* Shortcut for powers of two ... */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   592
  if((pow = s_mp_ispow2d(d)) >= 0) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   593
    mp_digit  mask;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   594
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   595
    mask = ((mp_digit)1 << pow) - 1;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   596
    rem = DIGIT(a, 0) & mask;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   597
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   598
    if(q) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   599
      mp_copy(a, q);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   600
      s_mp_div_2d(q, pow);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   601
    }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   602
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   603
    if(r)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   604
      *r = rem;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   605
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   606
    return MP_OKAY;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   607
  }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   608
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   609
  if((res = mp_init_copy(&qp, a)) != MP_OKAY)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   610
    return res;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   611
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   612
  res = s_mp_div_d(&qp, d, &rem);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   613
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   614
  if(s_mp_cmp_d(&qp, 0) == 0)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   615
    SIGN(q) = ZPOS;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   616
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   617
  if(r)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   618
    *r = rem;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   619
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   620
  if(q)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   621
    s_mp_exch(&qp, q);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   622
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   623
  mp_clear(&qp);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   624
  return res;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   625
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   626
} /* end mp_div_d() */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   627
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   628
/* }}} */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   629
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   630
/* {{{ mp_div_2(a, c) */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   631
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   632
/*
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   633
  mp_div_2(a, c)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   634
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   635
  Compute c = a / 2, disregarding the remainder.
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   636
 */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   637
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   638
mp_err mp_div_2(const mp_int *a, mp_int *c)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   639
{
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   640
  mp_err  res;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   641
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   642
  ARGCHK(a != NULL && c != NULL, MP_BADARG);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   643
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   644
  if((res = mp_copy(a, c)) != MP_OKAY)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   645
    return res;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   646
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   647
  s_mp_div_2(c);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   648
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   649
  return MP_OKAY;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   650
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   651
} /* end mp_div_2() */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   652
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   653
/* }}} */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   654
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   655
/* {{{ mp_expt_d(a, d, b) */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   656
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   657
mp_err mp_expt_d(const mp_int *a, mp_digit d, mp_int *c)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   658
{
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   659
  mp_int   s, x;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   660
  mp_err   res;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   661
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   662
  ARGCHK(a != NULL && c != NULL, MP_BADARG);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   663
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   664
  if((res = mp_init(&s, FLAG(a))) != MP_OKAY)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   665
    return res;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   666
  if((res = mp_init_copy(&x, a)) != MP_OKAY)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   667
    goto X;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   668
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   669
  DIGIT(&s, 0) = 1;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   670
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   671
  while(d != 0) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   672
    if(d & 1) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   673
      if((res = s_mp_mul(&s, &x)) != MP_OKAY)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   674
        goto CLEANUP;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   675
    }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   676
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   677
    d /= 2;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   678
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   679
    if((res = s_mp_sqr(&x)) != MP_OKAY)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   680
      goto CLEANUP;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   681
  }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   682
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   683
  s_mp_exch(&s, c);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   684
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   685
CLEANUP:
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   686
  mp_clear(&x);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   687
X:
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   688
  mp_clear(&s);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   689
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   690
  return res;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   691
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   692
} /* end mp_expt_d() */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   693
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   694
/* }}} */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   695
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   696
/* }}} */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   697
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   698
/*------------------------------------------------------------------------*/
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   699
/* {{{ Full arithmetic */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   700
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   701
/* {{{ mp_abs(a, b) */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   702
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   703
/*
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   704
  mp_abs(a, b)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   705
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   706
  Compute b = |a|.  'a' and 'b' may be identical.
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   707
 */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   708
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   709
mp_err mp_abs(const mp_int *a, mp_int *b)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   710
{
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   711
  mp_err   res;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   712
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   713
  ARGCHK(a != NULL && b != NULL, MP_BADARG);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   714
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   715
  if((res = mp_copy(a, b)) != MP_OKAY)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   716
    return res;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   717
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   718
  SIGN(b) = ZPOS;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   719
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   720
  return MP_OKAY;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   721
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   722
} /* end mp_abs() */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   723
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   724
/* }}} */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   725
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   726
/* {{{ mp_neg(a, b) */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   727
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   728
/*
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   729
  mp_neg(a, b)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   730
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   731
  Compute b = -a.  'a' and 'b' may be identical.
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   732
 */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   733
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   734
mp_err mp_neg(const mp_int *a, mp_int *b)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   735
{
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   736
  mp_err   res;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   737
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   738
  ARGCHK(a != NULL && b != NULL, MP_BADARG);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   739
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   740
  if((res = mp_copy(a, b)) != MP_OKAY)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   741
    return res;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   742
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   743
  if(s_mp_cmp_d(b, 0) == MP_EQ)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   744
    SIGN(b) = ZPOS;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   745
  else
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   746
    SIGN(b) = (SIGN(b) == NEG) ? ZPOS : NEG;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   747
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   748
  return MP_OKAY;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   749
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   750
} /* end mp_neg() */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   751
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   752
/* }}} */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   753
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   754
/* {{{ mp_add(a, b, c) */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   755
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   756
/*
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   757
  mp_add(a, b, c)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   758
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   759
  Compute c = a + b.  All parameters may be identical.
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   760
 */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   761
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   762
mp_err mp_add(const mp_int *a, const mp_int *b, mp_int *c)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   763
{
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   764
  mp_err  res;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   765
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   766
  ARGCHK(a != NULL && b != NULL && c != NULL, MP_BADARG);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   767
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   768
  if(SIGN(a) == SIGN(b)) { /* same sign:  add values, keep sign */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   769
    MP_CHECKOK( s_mp_add_3arg(a, b, c) );
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   770
  } else if(s_mp_cmp(a, b) >= 0) {  /* different sign: |a| >= |b|   */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   771
    MP_CHECKOK( s_mp_sub_3arg(a, b, c) );
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   772
  } else {                          /* different sign: |a|  < |b|   */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   773
    MP_CHECKOK( s_mp_sub_3arg(b, a, c) );
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   774
  }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   775
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   776
  if (s_mp_cmp_d(c, 0) == MP_EQ)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   777
    SIGN(c) = ZPOS;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   778
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   779
CLEANUP:
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   780
  return res;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   781
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   782
} /* end mp_add() */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   783
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   784
/* }}} */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   785
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   786
/* {{{ mp_sub(a, b, c) */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   787
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   788
/*
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   789
  mp_sub(a, b, c)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   790
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   791
  Compute c = a - b.  All parameters may be identical.
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   792
 */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   793
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   794
mp_err mp_sub(const mp_int *a, const mp_int *b, mp_int *c)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   795
{
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   796
  mp_err  res;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   797
  int     magDiff;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   798
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   799
  ARGCHK(a != NULL && b != NULL && c != NULL, MP_BADARG);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   800
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   801
  if (a == b) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   802
    mp_zero(c);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   803
    return MP_OKAY;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   804
  }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   805
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   806
  if (MP_SIGN(a) != MP_SIGN(b)) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   807
    MP_CHECKOK( s_mp_add_3arg(a, b, c) );
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   808
  } else if (!(magDiff = s_mp_cmp(a, b))) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   809
    mp_zero(c);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   810
    res = MP_OKAY;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   811
  } else if (magDiff > 0) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   812
    MP_CHECKOK( s_mp_sub_3arg(a, b, c) );
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   813
  } else {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   814
    MP_CHECKOK( s_mp_sub_3arg(b, a, c) );
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   815
    MP_SIGN(c) = !MP_SIGN(a);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   816
  }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   817
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   818
  if (s_mp_cmp_d(c, 0) == MP_EQ)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   819
    MP_SIGN(c) = MP_ZPOS;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   820
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   821
CLEANUP:
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   822
  return res;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   823
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   824
} /* end mp_sub() */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   825
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   826
/* }}} */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   827
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   828
/* {{{ mp_mul(a, b, c) */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   829
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   830
/*
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   831
  mp_mul(a, b, c)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   832
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   833
  Compute c = a * b.  All parameters may be identical.
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   834
 */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   835
mp_err   mp_mul(const mp_int *a, const mp_int *b, mp_int * c)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   836
{
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   837
  mp_digit *pb;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   838
  mp_int   tmp;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   839
  mp_err   res;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   840
  mp_size  ib;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   841
  mp_size  useda, usedb;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   842
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   843
  ARGCHK(a != NULL && b != NULL && c != NULL, MP_BADARG);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   844
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   845
  if (a == c) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   846
    if ((res = mp_init_copy(&tmp, a)) != MP_OKAY)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   847
      return res;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   848
    if (a == b)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   849
      b = &tmp;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   850
    a = &tmp;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   851
  } else if (b == c) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   852
    if ((res = mp_init_copy(&tmp, b)) != MP_OKAY)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   853
      return res;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   854
    b = &tmp;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   855
  } else {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   856
    MP_DIGITS(&tmp) = 0;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   857
  }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   858
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   859
  if (MP_USED(a) < MP_USED(b)) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   860
    const mp_int *xch = b;      /* switch a and b, to do fewer outer loops */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   861
    b = a;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   862
    a = xch;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   863
  }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   864
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   865
  MP_USED(c) = 1; MP_DIGIT(c, 0) = 0;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   866
  if((res = s_mp_pad(c, USED(a) + USED(b))) != MP_OKAY)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   867
    goto CLEANUP;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   868
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   869
#ifdef NSS_USE_COMBA
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   870
  if ((MP_USED(a) == MP_USED(b)) && IS_POWER_OF_2(MP_USED(b))) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   871
      if (MP_USED(a) == 4) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   872
          s_mp_mul_comba_4(a, b, c);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   873
          goto CLEANUP;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   874
      }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   875
      if (MP_USED(a) == 8) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   876
          s_mp_mul_comba_8(a, b, c);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   877
          goto CLEANUP;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   878
      }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   879
      if (MP_USED(a) == 16) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   880
          s_mp_mul_comba_16(a, b, c);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   881
          goto CLEANUP;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   882
      }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   883
      if (MP_USED(a) == 32) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   884
          s_mp_mul_comba_32(a, b, c);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   885
          goto CLEANUP;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   886
      }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   887
  }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   888
#endif
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   889
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   890
  pb = MP_DIGITS(b);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   891
  s_mpv_mul_d(MP_DIGITS(a), MP_USED(a), *pb++, MP_DIGITS(c));
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   892
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   893
  /* Outer loop:  Digits of b */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   894
  useda = MP_USED(a);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   895
  usedb = MP_USED(b);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   896
  for (ib = 1; ib < usedb; ib++) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   897
    mp_digit b_i    = *pb++;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   898
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   899
    /* Inner product:  Digits of a */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   900
    if (b_i)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   901
      s_mpv_mul_d_add(MP_DIGITS(a), useda, b_i, MP_DIGITS(c) + ib);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   902
    else
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   903
      MP_DIGIT(c, ib + useda) = b_i;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   904
  }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   905
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   906
  s_mp_clamp(c);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   907
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   908
  if(SIGN(a) == SIGN(b) || s_mp_cmp_d(c, 0) == MP_EQ)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   909
    SIGN(c) = ZPOS;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   910
  else
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   911
    SIGN(c) = NEG;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   912
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   913
CLEANUP:
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   914
  mp_clear(&tmp);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   915
  return res;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   916
} /* end mp_mul() */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   917
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   918
/* }}} */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   919
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   920
/* {{{ mp_sqr(a, sqr) */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   921
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   922
#if MP_SQUARE
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   923
/*
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   924
  Computes the square of a.  This can be done more
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   925
  efficiently than a general multiplication, because many of the
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   926
  computation steps are redundant when squaring.  The inner product
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   927
  step is a bit more complicated, but we save a fair number of
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   928
  iterations of the multiplication loop.
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   929
 */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   930
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   931
/* sqr = a^2;   Caller provides both a and tmp; */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   932
mp_err   mp_sqr(const mp_int *a, mp_int *sqr)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   933
{
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   934
  mp_digit *pa;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   935
  mp_digit d;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   936
  mp_err   res;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   937
  mp_size  ix;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   938
  mp_int   tmp;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   939
  int      count;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   940
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   941
  ARGCHK(a != NULL && sqr != NULL, MP_BADARG);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   942
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   943
  if (a == sqr) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   944
    if((res = mp_init_copy(&tmp, a)) != MP_OKAY)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   945
      return res;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   946
    a = &tmp;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   947
  } else {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   948
    DIGITS(&tmp) = 0;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   949
    res = MP_OKAY;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   950
  }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   951
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   952
  ix = 2 * MP_USED(a);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   953
  if (ix > MP_ALLOC(sqr)) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   954
    MP_USED(sqr) = 1;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   955
    MP_CHECKOK( s_mp_grow(sqr, ix) );
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   956
  }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   957
  MP_USED(sqr) = ix;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   958
  MP_DIGIT(sqr, 0) = 0;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   959
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   960
#ifdef NSS_USE_COMBA
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   961
  if (IS_POWER_OF_2(MP_USED(a))) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   962
      if (MP_USED(a) == 4) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   963
          s_mp_sqr_comba_4(a, sqr);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   964
          goto CLEANUP;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   965
      }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   966
      if (MP_USED(a) == 8) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   967
          s_mp_sqr_comba_8(a, sqr);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   968
          goto CLEANUP;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   969
      }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   970
      if (MP_USED(a) == 16) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   971
          s_mp_sqr_comba_16(a, sqr);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   972
          goto CLEANUP;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   973
      }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   974
      if (MP_USED(a) == 32) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   975
          s_mp_sqr_comba_32(a, sqr);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   976
          goto CLEANUP;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   977
      }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   978
  }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   979
#endif
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   980
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   981
  pa = MP_DIGITS(a);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   982
  count = MP_USED(a) - 1;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   983
  if (count > 0) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   984
    d = *pa++;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   985
    s_mpv_mul_d(pa, count, d, MP_DIGITS(sqr) + 1);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   986
    for (ix = 3; --count > 0; ix += 2) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   987
      d = *pa++;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   988
      s_mpv_mul_d_add(pa, count, d, MP_DIGITS(sqr) + ix);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   989
    } /* for(ix ...) */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   990
    MP_DIGIT(sqr, MP_USED(sqr)-1) = 0; /* above loop stopped short of this. */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   991
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   992
    /* now sqr *= 2 */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   993
    s_mp_mul_2(sqr);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   994
  } else {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   995
    MP_DIGIT(sqr, 1) = 0;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   996
  }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   997
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   998
  /* now add the squares of the digits of a to sqr. */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
   999
  s_mpv_sqr_add_prop(MP_DIGITS(a), MP_USED(a), MP_DIGITS(sqr));
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1000
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1001
  SIGN(sqr) = ZPOS;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1002
  s_mp_clamp(sqr);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1003
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1004
CLEANUP:
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1005
  mp_clear(&tmp);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1006
  return res;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1007
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1008
} /* end mp_sqr() */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1009
#endif
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1010
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1011
/* }}} */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1012
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1013
/* {{{ mp_div(a, b, q, r) */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1014
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1015
/*
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1016
  mp_div(a, b, q, r)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1017
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1018
  Compute q = a / b and r = a mod b.  Input parameters may be re-used
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1019
  as output parameters.  If q or r is NULL, that portion of the
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1020
  computation will be discarded (although it will still be computed)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1021
 */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1022
mp_err mp_div(const mp_int *a, const mp_int *b, mp_int *q, mp_int *r)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1023
{
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1024
  mp_err   res;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1025
  mp_int   *pQ, *pR;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1026
  mp_int   qtmp, rtmp, btmp;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1027
  int      cmp;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1028
  mp_sign  signA;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1029
  mp_sign  signB;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1030
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1031
  ARGCHK(a != NULL && b != NULL, MP_BADARG);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1032
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1033
  signA = MP_SIGN(a);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1034
  signB = MP_SIGN(b);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1035
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1036
  if(mp_cmp_z(b) == MP_EQ)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1037
    return MP_RANGE;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1038
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1039
  DIGITS(&qtmp) = 0;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1040
  DIGITS(&rtmp) = 0;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1041
  DIGITS(&btmp) = 0;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1042
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1043
  /* Set up some temporaries... */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1044
  if (!r || r == a || r == b) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1045
    MP_CHECKOK( mp_init_copy(&rtmp, a) );
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1046
    pR = &rtmp;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1047
  } else {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1048
    MP_CHECKOK( mp_copy(a, r) );
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1049
    pR = r;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1050
  }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1051
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1052
  if (!q || q == a || q == b) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1053
    MP_CHECKOK( mp_init_size(&qtmp, MP_USED(a), FLAG(a)) );
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1054
    pQ = &qtmp;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1055
  } else {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1056
    MP_CHECKOK( s_mp_pad(q, MP_USED(a)) );
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1057
    pQ = q;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1058
    mp_zero(pQ);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1059
  }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1060
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1061
  /*
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1062
    If |a| <= |b|, we can compute the solution without division;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1063
    otherwise, we actually do the work required.
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1064
   */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1065
  if ((cmp = s_mp_cmp(a, b)) <= 0) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1066
    if (cmp) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1067
      /* r was set to a above. */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1068
      mp_zero(pQ);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1069
    } else {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1070
      mp_set(pQ, 1);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1071
      mp_zero(pR);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1072
    }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1073
  } else {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1074
    MP_CHECKOK( mp_init_copy(&btmp, b) );
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1075
    MP_CHECKOK( s_mp_div(pR, &btmp, pQ) );
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1076
  }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1077
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1078
  /* Compute the signs for the output  */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1079
  MP_SIGN(pR) = signA;   /* Sr = Sa              */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1080
  /* Sq = ZPOS if Sa == Sb */ /* Sq = NEG if Sa != Sb */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1081
  MP_SIGN(pQ) = (signA == signB) ? ZPOS : NEG;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1082
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1083
  if(s_mp_cmp_d(pQ, 0) == MP_EQ)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1084
    SIGN(pQ) = ZPOS;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1085
  if(s_mp_cmp_d(pR, 0) == MP_EQ)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1086
    SIGN(pR) = ZPOS;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1087
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1088
  /* Copy output, if it is needed      */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1089
  if(q && q != pQ)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1090
    s_mp_exch(pQ, q);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1091
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1092
  if(r && r != pR)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1093
    s_mp_exch(pR, r);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1094
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1095
CLEANUP:
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1096
  mp_clear(&btmp);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1097
  mp_clear(&rtmp);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1098
  mp_clear(&qtmp);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1099
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1100
  return res;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1101
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1102
} /* end mp_div() */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1103
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1104
/* }}} */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1105
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1106
/* {{{ mp_div_2d(a, d, q, r) */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1107
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1108
mp_err mp_div_2d(const mp_int *a, mp_digit d, mp_int *q, mp_int *r)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1109
{
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1110
  mp_err  res;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1111
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1112
  ARGCHK(a != NULL, MP_BADARG);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1113
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1114
  if(q) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1115
    if((res = mp_copy(a, q)) != MP_OKAY)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1116
      return res;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1117
  }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1118
  if(r) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1119
    if((res = mp_copy(a, r)) != MP_OKAY)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1120
      return res;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1121
  }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1122
  if(q) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1123
    s_mp_div_2d(q, d);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1124
  }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1125
  if(r) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1126
    s_mp_mod_2d(r, d);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1127
  }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1128
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1129
  return MP_OKAY;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1130
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1131
} /* end mp_div_2d() */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1132
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1133
/* }}} */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1134
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1135
/* {{{ mp_expt(a, b, c) */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1136
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1137
/*
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1138
  mp_expt(a, b, c)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1139
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1140
  Compute c = a ** b, that is, raise a to the b power.  Uses a
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1141
  standard iterative square-and-multiply technique.
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1142
 */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1143
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1144
mp_err mp_expt(mp_int *a, mp_int *b, mp_int *c)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1145
{
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1146
  mp_int   s, x;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1147
  mp_err   res;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1148
  mp_digit d;
8175
27e80dc06104 6989705: ECC security code native code compiler warnings
vinnie
parents: 5506
diff changeset
  1149
  unsigned int      dig, bit;
3863
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1150
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1151
  ARGCHK(a != NULL && b != NULL && c != NULL, MP_BADARG);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1152
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1153
  if(mp_cmp_z(b) < 0)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1154
    return MP_RANGE;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1155
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1156
  if((res = mp_init(&s, FLAG(a))) != MP_OKAY)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1157
    return res;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1158
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1159
  mp_set(&s, 1);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1160
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1161
  if((res = mp_init_copy(&x, a)) != MP_OKAY)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1162
    goto X;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1163
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1164
  /* Loop over low-order digits in ascending order */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1165
  for(dig = 0; dig < (USED(b) - 1); dig++) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1166
    d = DIGIT(b, dig);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1167
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1168
    /* Loop over bits of each non-maximal digit */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1169
    for(bit = 0; bit < DIGIT_BIT; bit++) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1170
      if(d & 1) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1171
        if((res = s_mp_mul(&s, &x)) != MP_OKAY)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1172
          goto CLEANUP;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1173
      }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1174
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1175
      d >>= 1;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1176
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1177
      if((res = s_mp_sqr(&x)) != MP_OKAY)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1178
        goto CLEANUP;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1179
    }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1180
  }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1181
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1182
  /* Consider now the last digit... */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1183
  d = DIGIT(b, dig);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1184
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1185
  while(d) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1186
    if(d & 1) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1187
      if((res = s_mp_mul(&s, &x)) != MP_OKAY)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1188
        goto CLEANUP;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1189
    }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1190
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1191
    d >>= 1;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1192
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1193
    if((res = s_mp_sqr(&x)) != MP_OKAY)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1194
      goto CLEANUP;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1195
  }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1196
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1197
  if(mp_iseven(b))
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1198
    SIGN(&s) = SIGN(a);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1199
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1200
  res = mp_copy(&s, c);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1201
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1202
CLEANUP:
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1203
  mp_clear(&x);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1204
X:
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1205
  mp_clear(&s);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1206
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1207
  return res;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1208
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1209
} /* end mp_expt() */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1210
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1211
/* }}} */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1212
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1213
/* {{{ mp_2expt(a, k) */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1214
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1215
/* Compute a = 2^k */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1216
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1217
mp_err mp_2expt(mp_int *a, mp_digit k)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1218
{
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1219
  ARGCHK(a != NULL, MP_BADARG);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1220
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1221
  return s_mp_2expt(a, k);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1222
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1223
} /* end mp_2expt() */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1224
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1225
/* }}} */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1226
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1227
/* {{{ mp_mod(a, m, c) */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1228
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1229
/*
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1230
  mp_mod(a, m, c)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1231
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1232
  Compute c = a (mod m).  Result will always be 0 <= c < m.
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1233
 */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1234
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1235
mp_err mp_mod(const mp_int *a, const mp_int *m, mp_int *c)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1236
{
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1237
  mp_err  res;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1238
  int     mag;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1239
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1240
  ARGCHK(a != NULL && m != NULL && c != NULL, MP_BADARG);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1241
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1242
  if(SIGN(m) == NEG)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1243
    return MP_RANGE;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1244
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1245
  /*
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1246
     If |a| > m, we need to divide to get the remainder and take the
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1247
     absolute value.
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1248
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1249
     If |a| < m, we don't need to do any division, just copy and adjust
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1250
     the sign (if a is negative).
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1251
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1252
     If |a| == m, we can simply set the result to zero.
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1253
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1254
     This order is intended to minimize the average path length of the
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1255
     comparison chain on common workloads -- the most frequent cases are
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1256
     that |a| != m, so we do those first.
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1257
   */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1258
  if((mag = s_mp_cmp(a, m)) > 0) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1259
    if((res = mp_div(a, m, NULL, c)) != MP_OKAY)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1260
      return res;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1261
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1262
    if(SIGN(c) == NEG) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1263
      if((res = mp_add(c, m, c)) != MP_OKAY)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1264
        return res;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1265
    }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1266
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1267
  } else if(mag < 0) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1268
    if((res = mp_copy(a, c)) != MP_OKAY)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1269
      return res;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1270
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1271
    if(mp_cmp_z(a) < 0) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1272
      if((res = mp_add(c, m, c)) != MP_OKAY)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1273
        return res;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1274
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1275
    }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1276
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1277
  } else {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1278
    mp_zero(c);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1279
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1280
  }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1281
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1282
  return MP_OKAY;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1283
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1284
} /* end mp_mod() */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1285
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1286
/* }}} */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1287
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1288
/* {{{ mp_mod_d(a, d, c) */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1289
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1290
/*
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1291
  mp_mod_d(a, d, c)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1292
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1293
  Compute c = a (mod d).  Result will always be 0 <= c < d
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1294
 */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1295
mp_err mp_mod_d(const mp_int *a, mp_digit d, mp_digit *c)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1296
{
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1297
  mp_err   res;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1298
  mp_digit rem;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1299
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1300
  ARGCHK(a != NULL && c != NULL, MP_BADARG);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1301
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1302
  if(s_mp_cmp_d(a, d) > 0) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1303
    if((res = mp_div_d(a, d, NULL, &rem)) != MP_OKAY)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1304
      return res;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1305
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1306
  } else {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1307
    if(SIGN(a) == NEG)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1308
      rem = d - DIGIT(a, 0);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1309
    else
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1310
      rem = DIGIT(a, 0);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1311
  }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1312
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1313
  if(c)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1314
    *c = rem;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1315
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1316
  return MP_OKAY;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1317
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1318
} /* end mp_mod_d() */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1319
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1320
/* }}} */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1321
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1322
/* {{{ mp_sqrt(a, b) */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1323
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1324
/*
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1325
  mp_sqrt(a, b)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1326
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1327
  Compute the integer square root of a, and store the result in b.
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1328
  Uses an integer-arithmetic version of Newton's iterative linear
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1329
  approximation technique to determine this value; the result has the
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1330
  following two properties:
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1331
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1332
     b^2 <= a
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1333
     (b+1)^2 >= a
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1334
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1335
  It is a range error to pass a negative value.
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1336
 */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1337
mp_err mp_sqrt(const mp_int *a, mp_int *b)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1338
{
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1339
  mp_int   x, t;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1340
  mp_err   res;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1341
  mp_size  used;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1342
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1343
  ARGCHK(a != NULL && b != NULL, MP_BADARG);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1344
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1345
  /* Cannot take square root of a negative value */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1346
  if(SIGN(a) == NEG)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1347
    return MP_RANGE;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1348
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1349
  /* Special cases for zero and one, trivial     */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1350
  if(mp_cmp_d(a, 1) <= 0)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1351
    return mp_copy(a, b);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1352
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1353
  /* Initialize the temporaries we'll use below  */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1354
  if((res = mp_init_size(&t, USED(a), FLAG(a))) != MP_OKAY)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1355
    return res;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1356
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1357
  /* Compute an initial guess for the iteration as a itself */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1358
  if((res = mp_init_copy(&x, a)) != MP_OKAY)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1359
    goto X;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1360
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1361
  used = MP_USED(&x);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1362
  if (used > 1) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1363
    s_mp_rshd(&x, used / 2);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1364
  }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1365
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1366
  for(;;) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1367
    /* t = (x * x) - a */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1368
    mp_copy(&x, &t);      /* can't fail, t is big enough for original x */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1369
    if((res = mp_sqr(&t, &t)) != MP_OKAY ||
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1370
       (res = mp_sub(&t, a, &t)) != MP_OKAY)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1371
      goto CLEANUP;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1372
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1373
    /* t = t / 2x       */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1374
    s_mp_mul_2(&x);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1375
    if((res = mp_div(&t, &x, &t, NULL)) != MP_OKAY)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1376
      goto CLEANUP;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1377
    s_mp_div_2(&x);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1378
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1379
    /* Terminate the loop, if the quotient is zero */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1380
    if(mp_cmp_z(&t) == MP_EQ)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1381
      break;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1382
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1383
    /* x = x - t       */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1384
    if((res = mp_sub(&x, &t, &x)) != MP_OKAY)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1385
      goto CLEANUP;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1386
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1387
  }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1388
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1389
  /* Copy result to output parameter */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1390
  mp_sub_d(&x, 1, &x);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1391
  s_mp_exch(&x, b);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1392
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1393
 CLEANUP:
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1394
  mp_clear(&x);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1395
 X:
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1396
  mp_clear(&t);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1397
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1398
  return res;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1399
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1400
} /* end mp_sqrt() */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1401
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1402
/* }}} */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1403
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1404
/* }}} */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1405
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1406
/*------------------------------------------------------------------------*/
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1407
/* {{{ Modular arithmetic */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1408
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1409
#if MP_MODARITH
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1410
/* {{{ mp_addmod(a, b, m, c) */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1411
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1412
/*
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1413
  mp_addmod(a, b, m, c)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1414
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1415
  Compute c = (a + b) mod m
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1416
 */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1417
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1418
mp_err mp_addmod(const mp_int *a, const mp_int *b, const mp_int *m, mp_int *c)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1419
{
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1420
  mp_err  res;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1421
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1422
  ARGCHK(a != NULL && b != NULL && m != NULL && c != NULL, MP_BADARG);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1423
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1424
  if((res = mp_add(a, b, c)) != MP_OKAY)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1425
    return res;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1426
  if((res = mp_mod(c, m, c)) != MP_OKAY)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1427
    return res;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1428
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1429
  return MP_OKAY;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1430
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1431
}
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1432
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1433
/* }}} */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1434
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1435
/* {{{ mp_submod(a, b, m, c) */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1436
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1437
/*
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1438
  mp_submod(a, b, m, c)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1439
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1440
  Compute c = (a - b) mod m
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1441
 */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1442
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1443
mp_err mp_submod(const mp_int *a, const mp_int *b, const mp_int *m, mp_int *c)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1444
{
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1445
  mp_err  res;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1446
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1447
  ARGCHK(a != NULL && b != NULL && m != NULL && c != NULL, MP_BADARG);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1448
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1449
  if((res = mp_sub(a, b, c)) != MP_OKAY)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1450
    return res;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1451
  if((res = mp_mod(c, m, c)) != MP_OKAY)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1452
    return res;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1453
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1454
  return MP_OKAY;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1455
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1456
}
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1457
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1458
/* }}} */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1459
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1460
/* {{{ mp_mulmod(a, b, m, c) */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1461
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1462
/*
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1463
  mp_mulmod(a, b, m, c)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1464
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1465
  Compute c = (a * b) mod m
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1466
 */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1467
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1468
mp_err mp_mulmod(const mp_int *a, const mp_int *b, const mp_int *m, mp_int *c)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1469
{
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1470
  mp_err  res;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1471
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1472
  ARGCHK(a != NULL && b != NULL && m != NULL && c != NULL, MP_BADARG);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1473
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1474
  if((res = mp_mul(a, b, c)) != MP_OKAY)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1475
    return res;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1476
  if((res = mp_mod(c, m, c)) != MP_OKAY)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1477
    return res;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1478
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1479
  return MP_OKAY;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1480
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1481
}
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1482
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1483
/* }}} */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1484
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1485
/* {{{ mp_sqrmod(a, m, c) */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1486
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1487
#if MP_SQUARE
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1488
mp_err mp_sqrmod(const mp_int *a, const mp_int *m, mp_int *c)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1489
{
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1490
  mp_err  res;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1491
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1492
  ARGCHK(a != NULL && m != NULL && c != NULL, MP_BADARG);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1493
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1494
  if((res = mp_sqr(a, c)) != MP_OKAY)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1495
    return res;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1496
  if((res = mp_mod(c, m, c)) != MP_OKAY)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1497
    return res;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1498
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1499
  return MP_OKAY;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1500
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1501
} /* end mp_sqrmod() */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1502
#endif
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1503
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1504
/* }}} */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1505
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1506
/* {{{ s_mp_exptmod(a, b, m, c) */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1507
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1508
/*
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1509
  s_mp_exptmod(a, b, m, c)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1510
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1511
  Compute c = (a ** b) mod m.  Uses a standard square-and-multiply
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1512
  method with modular reductions at each step. (This is basically the
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1513
  same code as mp_expt(), except for the addition of the reductions)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1514
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1515
  The modular reductions are done using Barrett's algorithm (see
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1516
  s_mp_reduce() below for details)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1517
 */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1518
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1519
mp_err s_mp_exptmod(const mp_int *a, const mp_int *b, const mp_int *m, mp_int *c)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1520
{
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1521
  mp_int   s, x, mu;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1522
  mp_err   res;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1523
  mp_digit d;
8175
27e80dc06104 6989705: ECC security code native code compiler warnings
vinnie
parents: 5506
diff changeset
  1524
  unsigned int      dig, bit;
3863
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1525
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1526
  ARGCHK(a != NULL && b != NULL && c != NULL, MP_BADARG);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1527
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1528
  if(mp_cmp_z(b) < 0 || mp_cmp_z(m) <= 0)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1529
    return MP_RANGE;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1530
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1531
  if((res = mp_init(&s, FLAG(a))) != MP_OKAY)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1532
    return res;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1533
  if((res = mp_init_copy(&x, a)) != MP_OKAY ||
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1534
     (res = mp_mod(&x, m, &x)) != MP_OKAY)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1535
    goto X;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1536
  if((res = mp_init(&mu, FLAG(a))) != MP_OKAY)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1537
    goto MU;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1538
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1539
  mp_set(&s, 1);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1540
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1541
  /* mu = b^2k / m */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1542
  s_mp_add_d(&mu, 1);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1543
  s_mp_lshd(&mu, 2 * USED(m));
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1544
  if((res = mp_div(&mu, m, &mu, NULL)) != MP_OKAY)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1545
    goto CLEANUP;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1546
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1547
  /* Loop over digits of b in ascending order, except highest order */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1548
  for(dig = 0; dig < (USED(b) - 1); dig++) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1549
    d = DIGIT(b, dig);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1550
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1551
    /* Loop over the bits of the lower-order digits */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1552
    for(bit = 0; bit < DIGIT_BIT; bit++) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1553
      if(d & 1) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1554
        if((res = s_mp_mul(&s, &x)) != MP_OKAY)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1555
          goto CLEANUP;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1556
        if((res = s_mp_reduce(&s, m, &mu)) != MP_OKAY)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1557
          goto CLEANUP;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1558
      }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1559
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1560
      d >>= 1;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1561
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1562
      if((res = s_mp_sqr(&x)) != MP_OKAY)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1563
        goto CLEANUP;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1564
      if((res = s_mp_reduce(&x, m, &mu)) != MP_OKAY)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1565
        goto CLEANUP;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1566
    }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1567
  }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1568
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1569
  /* Now do the last digit... */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1570
  d = DIGIT(b, dig);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1571
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1572
  while(d) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1573
    if(d & 1) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1574
      if((res = s_mp_mul(&s, &x)) != MP_OKAY)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1575
        goto CLEANUP;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1576
      if((res = s_mp_reduce(&s, m, &mu)) != MP_OKAY)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1577
        goto CLEANUP;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1578
    }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1579
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1580
    d >>= 1;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1581
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1582
    if((res = s_mp_sqr(&x)) != MP_OKAY)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1583
      goto CLEANUP;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1584
    if((res = s_mp_reduce(&x, m, &mu)) != MP_OKAY)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1585
      goto CLEANUP;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1586
  }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1587
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1588
  s_mp_exch(&s, c);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1589
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1590
 CLEANUP:
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1591
  mp_clear(&mu);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1592
 MU:
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1593
  mp_clear(&x);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1594
 X:
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1595
  mp_clear(&s);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1596
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1597
  return res;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1598
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1599
} /* end s_mp_exptmod() */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1600
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1601
/* }}} */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1602
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1603
/* {{{ mp_exptmod_d(a, d, m, c) */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1604
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1605
mp_err mp_exptmod_d(const mp_int *a, mp_digit d, const mp_int *m, mp_int *c)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1606
{
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1607
  mp_int   s, x;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1608
  mp_err   res;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1609
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1610
  ARGCHK(a != NULL && c != NULL, MP_BADARG);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1611
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1612
  if((res = mp_init(&s, FLAG(a))) != MP_OKAY)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1613
    return res;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1614
  if((res = mp_init_copy(&x, a)) != MP_OKAY)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1615
    goto X;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1616
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1617
  mp_set(&s, 1);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1618
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1619
  while(d != 0) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1620
    if(d & 1) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1621
      if((res = s_mp_mul(&s, &x)) != MP_OKAY ||
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1622
         (res = mp_mod(&s, m, &s)) != MP_OKAY)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1623
        goto CLEANUP;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1624
    }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1625
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1626
    d /= 2;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1627
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1628
    if((res = s_mp_sqr(&x)) != MP_OKAY ||
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1629
       (res = mp_mod(&x, m, &x)) != MP_OKAY)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1630
      goto CLEANUP;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1631
  }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1632
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1633
  s_mp_exch(&s, c);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1634
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1635
CLEANUP:
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1636
  mp_clear(&x);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1637
X:
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1638
  mp_clear(&s);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1639
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1640
  return res;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1641
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1642
} /* end mp_exptmod_d() */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1643
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1644
/* }}} */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1645
#endif /* if MP_MODARITH */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1646
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1647
/* }}} */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1648
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1649
/*------------------------------------------------------------------------*/
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1650
/* {{{ Comparison functions */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1651
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1652
/* {{{ mp_cmp_z(a) */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1653
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1654
/*
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1655
  mp_cmp_z(a)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1656
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1657
  Compare a <=> 0.  Returns <0 if a<0, 0 if a=0, >0 if a>0.
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1658
 */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1659
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1660
int    mp_cmp_z(const mp_int *a)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1661
{
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1662
  if(SIGN(a) == NEG)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1663
    return MP_LT;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1664
  else if(USED(a) == 1 && DIGIT(a, 0) == 0)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1665
    return MP_EQ;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1666
  else
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1667
    return MP_GT;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1668
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1669
} /* end mp_cmp_z() */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1670
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1671
/* }}} */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1672
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1673
/* {{{ mp_cmp_d(a, d) */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1674
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1675
/*
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1676
  mp_cmp_d(a, d)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1677
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1678
  Compare a <=> d.  Returns <0 if a<d, 0 if a=d, >0 if a>d
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1679
 */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1680
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1681
int    mp_cmp_d(const mp_int *a, mp_digit d)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1682
{
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1683
  ARGCHK(a != NULL, MP_EQ);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1684
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1685
  if(SIGN(a) == NEG)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1686
    return MP_LT;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1687
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1688
  return s_mp_cmp_d(a, d);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1689
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1690
} /* end mp_cmp_d() */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1691
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1692
/* }}} */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1693
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1694
/* {{{ mp_cmp(a, b) */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1695
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1696
int    mp_cmp(const mp_int *a, const mp_int *b)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1697
{
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1698
  ARGCHK(a != NULL && b != NULL, MP_EQ);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1699
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1700
  if(SIGN(a) == SIGN(b)) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1701
    int  mag;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1702
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1703
    if((mag = s_mp_cmp(a, b)) == MP_EQ)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1704
      return MP_EQ;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1705
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1706
    if(SIGN(a) == ZPOS)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1707
      return mag;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1708
    else
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1709
      return -mag;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1710
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1711
  } else if(SIGN(a) == ZPOS) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1712
    return MP_GT;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1713
  } else {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1714
    return MP_LT;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1715
  }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1716
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1717
} /* end mp_cmp() */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1718
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1719
/* }}} */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1720
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1721
/* {{{ mp_cmp_mag(a, b) */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1722
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1723
/*
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1724
  mp_cmp_mag(a, b)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1725
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1726
  Compares |a| <=> |b|, and returns an appropriate comparison result
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1727
 */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1728
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1729
int    mp_cmp_mag(mp_int *a, mp_int *b)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1730
{
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1731
  ARGCHK(a != NULL && b != NULL, MP_EQ);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1732
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1733
  return s_mp_cmp(a, b);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1734
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1735
} /* end mp_cmp_mag() */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1736
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1737
/* }}} */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1738
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1739
/* {{{ mp_cmp_int(a, z, kmflag) */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1740
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1741
/*
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1742
  This just converts z to an mp_int, and uses the existing comparison
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1743
  routines.  This is sort of inefficient, but it's not clear to me how
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1744
  frequently this wil get used anyway.  For small positive constants,
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1745
  you can always use mp_cmp_d(), and for zero, there is mp_cmp_z().
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1746
 */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1747
int    mp_cmp_int(const mp_int *a, long z, int kmflag)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1748
{
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1749
  mp_int  tmp;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1750
  int     out;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1751
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1752
  ARGCHK(a != NULL, MP_EQ);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1753
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1754
  mp_init(&tmp, kmflag); mp_set_int(&tmp, z);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1755
  out = mp_cmp(a, &tmp);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1756
  mp_clear(&tmp);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1757
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1758
  return out;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1759
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1760
} /* end mp_cmp_int() */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1761
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1762
/* }}} */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1763
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1764
/* {{{ mp_isodd(a) */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1765
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1766
/*
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1767
  mp_isodd(a)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1768
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1769
  Returns a true (non-zero) value if a is odd, false (zero) otherwise.
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1770
 */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1771
int    mp_isodd(const mp_int *a)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1772
{
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1773
  ARGCHK(a != NULL, 0);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1774
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1775
  return (int)(DIGIT(a, 0) & 1);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1776
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1777
} /* end mp_isodd() */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1778
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1779
/* }}} */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1780
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1781
/* {{{ mp_iseven(a) */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1782
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1783
int    mp_iseven(const mp_int *a)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1784
{
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1785
  return !mp_isodd(a);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1786
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1787
} /* end mp_iseven() */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1788
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1789
/* }}} */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1790
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1791
/* }}} */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1792
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1793
/*------------------------------------------------------------------------*/
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1794
/* {{{ Number theoretic functions */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1795
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1796
#if MP_NUMTH
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1797
/* {{{ mp_gcd(a, b, c) */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1798
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1799
/*
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1800
  Like the old mp_gcd() function, except computes the GCD using the
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1801
  binary algorithm due to Josef Stein in 1961 (via Knuth).
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1802
 */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1803
mp_err mp_gcd(mp_int *a, mp_int *b, mp_int *c)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1804
{
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1805
  mp_err   res;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1806
  mp_int   u, v, t;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1807
  mp_size  k = 0;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1808
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1809
  ARGCHK(a != NULL && b != NULL && c != NULL, MP_BADARG);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1810
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1811
  if(mp_cmp_z(a) == MP_EQ && mp_cmp_z(b) == MP_EQ)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1812
      return MP_RANGE;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1813
  if(mp_cmp_z(a) == MP_EQ) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1814
    return mp_copy(b, c);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1815
  } else if(mp_cmp_z(b) == MP_EQ) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1816
    return mp_copy(a, c);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1817
  }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1818
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1819
  if((res = mp_init(&t, FLAG(a))) != MP_OKAY)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1820
    return res;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1821
  if((res = mp_init_copy(&u, a)) != MP_OKAY)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1822
    goto U;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1823
  if((res = mp_init_copy(&v, b)) != MP_OKAY)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1824
    goto V;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1825
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1826
  SIGN(&u) = ZPOS;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1827
  SIGN(&v) = ZPOS;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1828
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1829
  /* Divide out common factors of 2 until at least 1 of a, b is even */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1830
  while(mp_iseven(&u) && mp_iseven(&v)) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1831
    s_mp_div_2(&u);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1832
    s_mp_div_2(&v);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1833
    ++k;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1834
  }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1835
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1836
  /* Initialize t */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1837
  if(mp_isodd(&u)) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1838
    if((res = mp_copy(&v, &t)) != MP_OKAY)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1839
      goto CLEANUP;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1840
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1841
    /* t = -v */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1842
    if(SIGN(&v) == ZPOS)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1843
      SIGN(&t) = NEG;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1844
    else
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1845
      SIGN(&t) = ZPOS;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1846
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1847
  } else {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1848
    if((res = mp_copy(&u, &t)) != MP_OKAY)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1849
      goto CLEANUP;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1850
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1851
  }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1852
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1853
  for(;;) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1854
    while(mp_iseven(&t)) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1855
      s_mp_div_2(&t);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1856
    }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1857
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1858
    if(mp_cmp_z(&t) == MP_GT) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1859
      if((res = mp_copy(&t, &u)) != MP_OKAY)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1860
        goto CLEANUP;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1861
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1862
    } else {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1863
      if((res = mp_copy(&t, &v)) != MP_OKAY)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1864
        goto CLEANUP;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1865
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1866
      /* v = -t */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1867
      if(SIGN(&t) == ZPOS)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1868
        SIGN(&v) = NEG;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1869
      else
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1870
        SIGN(&v) = ZPOS;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1871
    }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1872
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1873
    if((res = mp_sub(&u, &v, &t)) != MP_OKAY)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1874
      goto CLEANUP;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1875
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1876
    if(s_mp_cmp_d(&t, 0) == MP_EQ)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1877
      break;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1878
  }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1879
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1880
  s_mp_2expt(&v, k);       /* v = 2^k   */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1881
  res = mp_mul(&u, &v, c); /* c = u * v */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1882
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1883
 CLEANUP:
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1884
  mp_clear(&v);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1885
 V:
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1886
  mp_clear(&u);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1887
 U:
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1888
  mp_clear(&t);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1889
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1890
  return res;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1891
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1892
} /* end mp_gcd() */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1893
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1894
/* }}} */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1895
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1896
/* {{{ mp_lcm(a, b, c) */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1897
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1898
/* We compute the least common multiple using the rule:
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1899
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1900
   ab = [a, b](a, b)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1901
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1902
   ... by computing the product, and dividing out the gcd.
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1903
 */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1904
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1905
mp_err mp_lcm(mp_int *a, mp_int *b, mp_int *c)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1906
{
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1907
  mp_int  gcd, prod;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1908
  mp_err  res;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1909
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1910
  ARGCHK(a != NULL && b != NULL && c != NULL, MP_BADARG);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1911
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1912
  /* Set up temporaries */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1913
  if((res = mp_init(&gcd, FLAG(a))) != MP_OKAY)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1914
    return res;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1915
  if((res = mp_init(&prod, FLAG(a))) != MP_OKAY)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1916
    goto GCD;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1917
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1918
  if((res = mp_mul(a, b, &prod)) != MP_OKAY)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1919
    goto CLEANUP;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1920
  if((res = mp_gcd(a, b, &gcd)) != MP_OKAY)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1921
    goto CLEANUP;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1922
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1923
  res = mp_div(&prod, &gcd, c, NULL);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1924
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1925
 CLEANUP:
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1926
  mp_clear(&prod);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1927
 GCD:
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1928
  mp_clear(&gcd);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1929
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1930
  return res;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1931
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1932
} /* end mp_lcm() */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1933
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1934
/* }}} */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1935
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1936
/* {{{ mp_xgcd(a, b, g, x, y) */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1937
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1938
/*
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1939
  mp_xgcd(a, b, g, x, y)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1940
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1941
  Compute g = (a, b) and values x and y satisfying Bezout's identity
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1942
  (that is, ax + by = g).  This uses the binary extended GCD algorithm
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1943
  based on the Stein algorithm used for mp_gcd()
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1944
  See algorithm 14.61 in Handbook of Applied Cryptogrpahy.
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1945
 */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1946
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1947
mp_err mp_xgcd(const mp_int *a, const mp_int *b, mp_int *g, mp_int *x, mp_int *y)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1948
{
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1949
  mp_int   gx, xc, yc, u, v, A, B, C, D;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1950
  mp_int  *clean[9];
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1951
  mp_err   res;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1952
  int      last = -1;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1953
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1954
  if(mp_cmp_z(b) == 0)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1955
    return MP_RANGE;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1956
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1957
  /* Initialize all these variables we need */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1958
  MP_CHECKOK( mp_init(&u, FLAG(a)) );
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1959
  clean[++last] = &u;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1960
  MP_CHECKOK( mp_init(&v, FLAG(a)) );
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1961
  clean[++last] = &v;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1962
  MP_CHECKOK( mp_init(&gx, FLAG(a)) );
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1963
  clean[++last] = &gx;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1964
  MP_CHECKOK( mp_init(&A, FLAG(a)) );
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1965
  clean[++last] = &A;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1966
  MP_CHECKOK( mp_init(&B, FLAG(a)) );
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1967
  clean[++last] = &B;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1968
  MP_CHECKOK( mp_init(&C, FLAG(a)) );
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1969
  clean[++last] = &C;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1970
  MP_CHECKOK( mp_init(&D, FLAG(a)) );
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1971
  clean[++last] = &D;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1972
  MP_CHECKOK( mp_init_copy(&xc, a) );
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1973
  clean[++last] = &xc;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1974
  mp_abs(&xc, &xc);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1975
  MP_CHECKOK( mp_init_copy(&yc, b) );
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1976
  clean[++last] = &yc;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1977
  mp_abs(&yc, &yc);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1978
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1979
  mp_set(&gx, 1);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1980
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1981
  /* Divide by two until at least one of them is odd */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1982
  while(mp_iseven(&xc) && mp_iseven(&yc)) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1983
    mp_size nx = mp_trailing_zeros(&xc);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1984
    mp_size ny = mp_trailing_zeros(&yc);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1985
    mp_size n  = MP_MIN(nx, ny);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1986
    s_mp_div_2d(&xc,n);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1987
    s_mp_div_2d(&yc,n);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1988
    MP_CHECKOK( s_mp_mul_2d(&gx,n) );
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1989
  }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1990
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1991
  mp_copy(&xc, &u);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1992
  mp_copy(&yc, &v);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1993
  mp_set(&A, 1); mp_set(&D, 1);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1994
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1995
  /* Loop through binary GCD algorithm */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1996
  do {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1997
    while(mp_iseven(&u)) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1998
      s_mp_div_2(&u);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  1999
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2000
      if(mp_iseven(&A) && mp_iseven(&B)) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2001
        s_mp_div_2(&A); s_mp_div_2(&B);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2002
      } else {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2003
        MP_CHECKOK( mp_add(&A, &yc, &A) );
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2004
        s_mp_div_2(&A);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2005
        MP_CHECKOK( mp_sub(&B, &xc, &B) );
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2006
        s_mp_div_2(&B);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2007
      }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2008
    }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2009
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2010
    while(mp_iseven(&v)) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2011
      s_mp_div_2(&v);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2012
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2013
      if(mp_iseven(&C) && mp_iseven(&D)) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2014
        s_mp_div_2(&C); s_mp_div_2(&D);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2015
      } else {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2016
        MP_CHECKOK( mp_add(&C, &yc, &C) );
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2017
        s_mp_div_2(&C);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2018
        MP_CHECKOK( mp_sub(&D, &xc, &D) );
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2019
        s_mp_div_2(&D);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2020
      }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2021
    }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2022
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2023
    if(mp_cmp(&u, &v) >= 0) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2024
      MP_CHECKOK( mp_sub(&u, &v, &u) );
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2025
      MP_CHECKOK( mp_sub(&A, &C, &A) );
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2026
      MP_CHECKOK( mp_sub(&B, &D, &B) );
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2027
    } else {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2028
      MP_CHECKOK( mp_sub(&v, &u, &v) );
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2029
      MP_CHECKOK( mp_sub(&C, &A, &C) );
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2030
      MP_CHECKOK( mp_sub(&D, &B, &D) );
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2031
    }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2032
  } while (mp_cmp_z(&u) != 0);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2033
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2034
  /* copy results to output */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2035
  if(x)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2036
    MP_CHECKOK( mp_copy(&C, x) );
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2037
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2038
  if(y)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2039
    MP_CHECKOK( mp_copy(&D, y) );
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2040
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2041
  if(g)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2042
    MP_CHECKOK( mp_mul(&gx, &v, g) );
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2043
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2044
 CLEANUP:
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2045
  while(last >= 0)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2046
    mp_clear(clean[last--]);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2047
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2048
  return res;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2049
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2050
} /* end mp_xgcd() */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2051
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2052
/* }}} */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2053
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2054
mp_size mp_trailing_zeros(const mp_int *mp)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2055
{
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2056
  mp_digit d;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2057
  mp_size  n = 0;
8175
27e80dc06104 6989705: ECC security code native code compiler warnings
vinnie
parents: 5506
diff changeset
  2058
  unsigned int      ix;
3863
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2059
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2060
  if (!mp || !MP_DIGITS(mp) || !mp_cmp_z(mp))
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2061
    return n;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2062
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2063
  for (ix = 0; !(d = MP_DIGIT(mp,ix)) && (ix < MP_USED(mp)); ++ix)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2064
    n += MP_DIGIT_BIT;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2065
  if (!d)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2066
    return 0;   /* shouldn't happen, but ... */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2067
#if !defined(MP_USE_UINT_DIGIT)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2068
  if (!(d & 0xffffffffU)) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2069
    d >>= 32;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2070
    n  += 32;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2071
  }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2072
#endif
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2073
  if (!(d & 0xffffU)) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2074
    d >>= 16;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2075
    n  += 16;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2076
  }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2077
  if (!(d & 0xffU)) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2078
    d >>= 8;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2079
    n  += 8;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2080
  }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2081
  if (!(d & 0xfU)) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2082
    d >>= 4;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2083
    n  += 4;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2084
  }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2085
  if (!(d & 0x3U)) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2086
    d >>= 2;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2087
    n  += 2;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2088
  }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2089
  if (!(d & 0x1U)) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2090
    d >>= 1;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2091
    n  += 1;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2092
  }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2093
#if MP_ARGCHK == 2
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2094
  assert(0 != (d & 1));
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2095
#endif
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2096
  return n;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2097
}
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2098
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2099
/* Given a and prime p, computes c and k such that a*c == 2**k (mod p).
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2100
** Returns k (positive) or error (negative).
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2101
** This technique from the paper "Fast Modular Reciprocals" (unpublished)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2102
** by Richard Schroeppel (a.k.a. Captain Nemo).
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2103
*/
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2104
mp_err s_mp_almost_inverse(const mp_int *a, const mp_int *p, mp_int *c)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2105
{
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2106
  mp_err res;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2107
  mp_err k    = 0;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2108
  mp_int d, f, g;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2109
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2110
  ARGCHK(a && p && c, MP_BADARG);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2111
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2112
  MP_DIGITS(&d) = 0;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2113
  MP_DIGITS(&f) = 0;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2114
  MP_DIGITS(&g) = 0;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2115
  MP_CHECKOK( mp_init(&d, FLAG(a)) );
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2116
  MP_CHECKOK( mp_init_copy(&f, a) );    /* f = a */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2117
  MP_CHECKOK( mp_init_copy(&g, p) );    /* g = p */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2118
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2119
  mp_set(c, 1);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2120
  mp_zero(&d);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2121
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2122
  if (mp_cmp_z(&f) == 0) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2123
    res = MP_UNDEF;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2124
  } else
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2125
  for (;;) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2126
    int diff_sign;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2127
    while (mp_iseven(&f)) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2128
      mp_size n = mp_trailing_zeros(&f);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2129
      if (!n) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2130
        res = MP_UNDEF;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2131
        goto CLEANUP;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2132
      }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2133
      s_mp_div_2d(&f, n);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2134
      MP_CHECKOK( s_mp_mul_2d(&d, n) );
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2135
      k += n;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2136
    }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2137
    if (mp_cmp_d(&f, 1) == MP_EQ) {     /* f == 1 */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2138
      res = k;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2139
      break;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2140
    }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2141
    diff_sign = mp_cmp(&f, &g);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2142
    if (diff_sign < 0) {                /* f < g */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2143
      s_mp_exch(&f, &g);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2144
      s_mp_exch(c, &d);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2145
    } else if (diff_sign == 0) {                /* f == g */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2146
      res = MP_UNDEF;           /* a and p are not relatively prime */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2147
      break;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2148
    }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2149
    if ((MP_DIGIT(&f,0) % 4) == (MP_DIGIT(&g,0) % 4)) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2150
      MP_CHECKOK( mp_sub(&f, &g, &f) ); /* f = f - g */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2151
      MP_CHECKOK( mp_sub(c,  &d,  c) ); /* c = c - d */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2152
    } else {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2153
      MP_CHECKOK( mp_add(&f, &g, &f) ); /* f = f + g */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2154
      MP_CHECKOK( mp_add(c,  &d,  c) ); /* c = c + d */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2155
    }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2156
  }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2157
  if (res >= 0) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2158
    while (MP_SIGN(c) != MP_ZPOS) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2159
      MP_CHECKOK( mp_add(c, p, c) );
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2160
    }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2161
    res = k;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2162
  }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2163
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2164
CLEANUP:
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2165
  mp_clear(&d);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2166
  mp_clear(&f);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2167
  mp_clear(&g);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2168
  return res;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2169
}
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2170
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2171
/* Compute T = (P ** -1) mod MP_RADIX.  Also works for 16-bit mp_digits.
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2172
** This technique from the paper "Fast Modular Reciprocals" (unpublished)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2173
** by Richard Schroeppel (a.k.a. Captain Nemo).
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2174
*/
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2175
mp_digit  s_mp_invmod_radix(mp_digit P)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2176
{
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2177
  mp_digit T = P;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2178
  T *= 2 - (P * T);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2179
  T *= 2 - (P * T);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2180
  T *= 2 - (P * T);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2181
  T *= 2 - (P * T);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2182
#if !defined(MP_USE_UINT_DIGIT)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2183
  T *= 2 - (P * T);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2184
  T *= 2 - (P * T);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2185
#endif
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2186
  return T;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2187
}
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2188
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2189
/* Given c, k, and prime p, where a*c == 2**k (mod p),
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2190
** Compute x = (a ** -1) mod p.  This is similar to Montgomery reduction.
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2191
** This technique from the paper "Fast Modular Reciprocals" (unpublished)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2192
** by Richard Schroeppel (a.k.a. Captain Nemo).
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2193
*/
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2194
mp_err  s_mp_fixup_reciprocal(const mp_int *c, const mp_int *p, int k, mp_int *x)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2195
{
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2196
  int      k_orig = k;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2197
  mp_digit r;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2198
  mp_size  ix;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2199
  mp_err   res;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2200
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2201
  if (mp_cmp_z(c) < 0) {                /* c < 0 */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2202
    MP_CHECKOK( mp_add(c, p, x) );      /* x = c + p */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2203
  } else {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2204
    MP_CHECKOK( mp_copy(c, x) );        /* x = c */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2205
  }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2206
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2207
  /* make sure x is large enough */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2208
  ix = MP_HOWMANY(k, MP_DIGIT_BIT) + MP_USED(p) + 1;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2209
  ix = MP_MAX(ix, MP_USED(x));
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2210
  MP_CHECKOK( s_mp_pad(x, ix) );
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2211
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2212
  r = 0 - s_mp_invmod_radix(MP_DIGIT(p,0));
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2213
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2214
  for (ix = 0; k > 0; ix++) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2215
    int      j = MP_MIN(k, MP_DIGIT_BIT);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2216
    mp_digit v = r * MP_DIGIT(x, ix);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2217
    if (j < MP_DIGIT_BIT) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2218
      v &= ((mp_digit)1 << j) - 1;      /* v = v mod (2 ** j) */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2219
    }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2220
    s_mp_mul_d_add_offset(p, v, x, ix); /* x += p * v * (RADIX ** ix) */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2221
    k -= j;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2222
  }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2223
  s_mp_clamp(x);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2224
  s_mp_div_2d(x, k_orig);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2225
  res = MP_OKAY;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2226
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2227
CLEANUP:
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2228
  return res;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2229
}
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2230
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2231
/* compute mod inverse using Schroeppel's method, only if m is odd */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2232
mp_err s_mp_invmod_odd_m(const mp_int *a, const mp_int *m, mp_int *c)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2233
{
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2234
  int k;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2235
  mp_err  res;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2236
  mp_int  x;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2237
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2238
  ARGCHK(a && m && c, MP_BADARG);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2239
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2240
  if(mp_cmp_z(a) == 0 || mp_cmp_z(m) == 0)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2241
    return MP_RANGE;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2242
  if (mp_iseven(m))
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2243
    return MP_UNDEF;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2244
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2245
  MP_DIGITS(&x) = 0;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2246
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2247
  if (a == c) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2248
    if ((res = mp_init_copy(&x, a)) != MP_OKAY)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2249
      return res;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2250
    if (a == m)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2251
      m = &x;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2252
    a = &x;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2253
  } else if (m == c) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2254
    if ((res = mp_init_copy(&x, m)) != MP_OKAY)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2255
      return res;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2256
    m = &x;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2257
  } else {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2258
    MP_DIGITS(&x) = 0;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2259
  }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2260
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2261
  MP_CHECKOK( s_mp_almost_inverse(a, m, c) );
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2262
  k = res;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2263
  MP_CHECKOK( s_mp_fixup_reciprocal(c, m, k, c) );
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2264
CLEANUP:
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2265
  mp_clear(&x);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2266
  return res;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2267
}
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2268
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2269
/* Known good algorithm for computing modular inverse.  But slow. */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2270
mp_err mp_invmod_xgcd(const mp_int *a, const mp_int *m, mp_int *c)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2271
{
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2272
  mp_int  g, x;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2273
  mp_err  res;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2274
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2275
  ARGCHK(a && m && c, MP_BADARG);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2276
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2277
  if(mp_cmp_z(a) == 0 || mp_cmp_z(m) == 0)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2278
    return MP_RANGE;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2279
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2280
  MP_DIGITS(&g) = 0;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2281
  MP_DIGITS(&x) = 0;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2282
  MP_CHECKOK( mp_init(&x, FLAG(a)) );
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2283
  MP_CHECKOK( mp_init(&g, FLAG(a)) );
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2284
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2285
  MP_CHECKOK( mp_xgcd(a, m, &g, &x, NULL) );
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2286
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2287
  if (mp_cmp_d(&g, 1) != MP_EQ) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2288
    res = MP_UNDEF;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2289
    goto CLEANUP;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2290
  }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2291
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2292
  res = mp_mod(&x, m, c);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2293
  SIGN(c) = SIGN(a);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2294
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2295
CLEANUP:
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2296
  mp_clear(&x);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2297
  mp_clear(&g);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2298
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2299
  return res;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2300
}
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2301
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2302
/* modular inverse where modulus is 2**k. */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2303
/* c = a**-1 mod 2**k */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2304
mp_err s_mp_invmod_2d(const mp_int *a, mp_size k, mp_int *c)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2305
{
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2306
  mp_err res;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2307
  mp_size ix = k + 4;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2308
  mp_int t0, t1, val, tmp, two2k;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2309
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2310
  static const mp_digit d2 = 2;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2311
  static const mp_int two = { 0, MP_ZPOS, 1, 1, (mp_digit *)&d2 };
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2312
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2313
  if (mp_iseven(a))
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2314
    return MP_UNDEF;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2315
  if (k <= MP_DIGIT_BIT) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2316
    mp_digit i = s_mp_invmod_radix(MP_DIGIT(a,0));
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2317
    if (k < MP_DIGIT_BIT)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2318
      i &= ((mp_digit)1 << k) - (mp_digit)1;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2319
    mp_set(c, i);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2320
    return MP_OKAY;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2321
  }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2322
  MP_DIGITS(&t0) = 0;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2323
  MP_DIGITS(&t1) = 0;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2324
  MP_DIGITS(&val) = 0;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2325
  MP_DIGITS(&tmp) = 0;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2326
  MP_DIGITS(&two2k) = 0;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2327
  MP_CHECKOK( mp_init_copy(&val, a) );
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2328
  s_mp_mod_2d(&val, k);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2329
  MP_CHECKOK( mp_init_copy(&t0, &val) );
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2330
  MP_CHECKOK( mp_init_copy(&t1, &t0)  );
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2331
  MP_CHECKOK( mp_init(&tmp, FLAG(a)) );
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2332
  MP_CHECKOK( mp_init(&two2k, FLAG(a)) );
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2333
  MP_CHECKOK( s_mp_2expt(&two2k, k) );
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2334
  do {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2335
    MP_CHECKOK( mp_mul(&val, &t1, &tmp)  );
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2336
    MP_CHECKOK( mp_sub(&two, &tmp, &tmp) );
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2337
    MP_CHECKOK( mp_mul(&t1, &tmp, &t1)   );
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2338
    s_mp_mod_2d(&t1, k);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2339
    while (MP_SIGN(&t1) != MP_ZPOS) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2340
      MP_CHECKOK( mp_add(&t1, &two2k, &t1) );
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2341
    }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2342
    if (mp_cmp(&t1, &t0) == MP_EQ)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2343
      break;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2344
    MP_CHECKOK( mp_copy(&t1, &t0) );
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2345
  } while (--ix > 0);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2346
  if (!ix) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2347
    res = MP_UNDEF;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2348
  } else {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2349
    mp_exch(c, &t1);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2350
  }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2351
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2352
CLEANUP:
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2353
  mp_clear(&t0);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2354
  mp_clear(&t1);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2355
  mp_clear(&val);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2356
  mp_clear(&tmp);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2357
  mp_clear(&two2k);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2358
  return res;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2359
}
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2360
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2361
mp_err s_mp_invmod_even_m(const mp_int *a, const mp_int *m, mp_int *c)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2362
{
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2363
  mp_err res;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2364
  mp_size k;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2365
  mp_int oddFactor, evenFactor; /* factors of the modulus */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2366
  mp_int oddPart, evenPart;     /* parts to combine via CRT. */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2367
  mp_int C2, tmp1, tmp2;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2368
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2369
  /*static const mp_digit d1 = 1; */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2370
  /*static const mp_int one = { MP_ZPOS, 1, 1, (mp_digit *)&d1 }; */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2371
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2372
  if ((res = s_mp_ispow2(m)) >= 0) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2373
    k = res;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2374
    return s_mp_invmod_2d(a, k, c);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2375
  }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2376
  MP_DIGITS(&oddFactor) = 0;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2377
  MP_DIGITS(&evenFactor) = 0;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2378
  MP_DIGITS(&oddPart) = 0;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2379
  MP_DIGITS(&evenPart) = 0;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2380
  MP_DIGITS(&C2)     = 0;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2381
  MP_DIGITS(&tmp1)   = 0;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2382
  MP_DIGITS(&tmp2)   = 0;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2383
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2384
  MP_CHECKOK( mp_init_copy(&oddFactor, m) );    /* oddFactor = m */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2385
  MP_CHECKOK( mp_init(&evenFactor, FLAG(m)) );
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2386
  MP_CHECKOK( mp_init(&oddPart, FLAG(m)) );
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2387
  MP_CHECKOK( mp_init(&evenPart, FLAG(m)) );
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2388
  MP_CHECKOK( mp_init(&C2, FLAG(m))     );
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2389
  MP_CHECKOK( mp_init(&tmp1, FLAG(m))   );
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2390
  MP_CHECKOK( mp_init(&tmp2, FLAG(m))   );
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2391
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2392
  k = mp_trailing_zeros(m);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2393
  s_mp_div_2d(&oddFactor, k);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2394
  MP_CHECKOK( s_mp_2expt(&evenFactor, k) );
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2395
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2396
  /* compute a**-1 mod oddFactor. */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2397
  MP_CHECKOK( s_mp_invmod_odd_m(a, &oddFactor, &oddPart) );
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2398
  /* compute a**-1 mod evenFactor, where evenFactor == 2**k. */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2399
  MP_CHECKOK( s_mp_invmod_2d(   a,       k,    &evenPart) );
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2400
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2401
  /* Use Chinese Remainer theorem to compute a**-1 mod m. */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2402
  /* let m1 = oddFactor,  v1 = oddPart,
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2403
   * let m2 = evenFactor, v2 = evenPart.
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2404
   */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2405
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2406
  /* Compute C2 = m1**-1 mod m2. */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2407
  MP_CHECKOK( s_mp_invmod_2d(&oddFactor, k,    &C2) );
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2408
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2409
  /* compute u = (v2 - v1)*C2 mod m2 */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2410
  MP_CHECKOK( mp_sub(&evenPart, &oddPart,   &tmp1) );
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2411
  MP_CHECKOK( mp_mul(&tmp1,     &C2,        &tmp2) );
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2412
  s_mp_mod_2d(&tmp2, k);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2413
  while (MP_SIGN(&tmp2) != MP_ZPOS) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2414
    MP_CHECKOK( mp_add(&tmp2, &evenFactor, &tmp2) );
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2415
  }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2416
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2417
  /* compute answer = v1 + u*m1 */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2418
  MP_CHECKOK( mp_mul(&tmp2,     &oddFactor, c) );
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2419
  MP_CHECKOK( mp_add(&oddPart,  c,          c) );
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2420
  /* not sure this is necessary, but it's low cost if not. */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2421
  MP_CHECKOK( mp_mod(c,         m,          c) );
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2422
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2423
CLEANUP:
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2424
  mp_clear(&oddFactor);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2425
  mp_clear(&evenFactor);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2426
  mp_clear(&oddPart);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2427
  mp_clear(&evenPart);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2428
  mp_clear(&C2);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2429
  mp_clear(&tmp1);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2430
  mp_clear(&tmp2);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2431
  return res;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2432
}
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2433
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2434
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2435
/* {{{ mp_invmod(a, m, c) */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2436
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2437
/*
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2438
  mp_invmod(a, m, c)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2439
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2440
  Compute c = a^-1 (mod m), if there is an inverse for a (mod m).
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2441
  This is equivalent to the question of whether (a, m) = 1.  If not,
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2442
  MP_UNDEF is returned, and there is no inverse.
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2443
 */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2444
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2445
mp_err mp_invmod(const mp_int *a, const mp_int *m, mp_int *c)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2446
{
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2447
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2448
  ARGCHK(a && m && c, MP_BADARG);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2449
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2450
  if(mp_cmp_z(a) == 0 || mp_cmp_z(m) == 0)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2451
    return MP_RANGE;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2452
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2453
  if (mp_isodd(m)) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2454
    return s_mp_invmod_odd_m(a, m, c);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2455
  }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2456
  if (mp_iseven(a))
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2457
    return MP_UNDEF;    /* not invertable */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2458
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2459
  return s_mp_invmod_even_m(a, m, c);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2460
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2461
} /* end mp_invmod() */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2462
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2463
/* }}} */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2464
#endif /* if MP_NUMTH */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2465
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2466
/* }}} */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2467
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2468
/*------------------------------------------------------------------------*/
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2469
/* {{{ mp_print(mp, ofp) */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2470
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2471
#if MP_IOFUNC
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2472
/*
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2473
  mp_print(mp, ofp)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2474
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2475
  Print a textual representation of the given mp_int on the output
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2476
  stream 'ofp'.  Output is generated using the internal radix.
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2477
 */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2478
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2479
void   mp_print(mp_int *mp, FILE *ofp)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2480
{
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2481
  int   ix;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2482
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2483
  if(mp == NULL || ofp == NULL)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2484
    return;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2485
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2486
  fputc((SIGN(mp) == NEG) ? '-' : '+', ofp);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2487
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2488
  for(ix = USED(mp) - 1; ix >= 0; ix--) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2489
    fprintf(ofp, DIGIT_FMT, DIGIT(mp, ix));
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2490
  }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2491
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2492
} /* end mp_print() */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2493
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2494
#endif /* if MP_IOFUNC */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2495
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2496
/* }}} */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2497
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2498
/*------------------------------------------------------------------------*/
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2499
/* {{{ More I/O Functions */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2500
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2501
/* {{{ mp_read_raw(mp, str, len) */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2502
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2503
/*
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2504
   mp_read_raw(mp, str, len)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2505
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2506
   Read in a raw value (base 256) into the given mp_int
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2507
 */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2508
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2509
mp_err  mp_read_raw(mp_int *mp, char *str, int len)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2510
{
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2511
  int            ix;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2512
  mp_err         res;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2513
  unsigned char *ustr = (unsigned char *)str;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2514
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2515
  ARGCHK(mp != NULL && str != NULL && len > 0, MP_BADARG);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2516
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2517
  mp_zero(mp);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2518
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2519
  /* Get sign from first byte */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2520
  if(ustr[0])
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2521
    SIGN(mp) = NEG;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2522
  else
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2523
    SIGN(mp) = ZPOS;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2524
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2525
  /* Read the rest of the digits */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2526
  for(ix = 1; ix < len; ix++) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2527
    if((res = mp_mul_d(mp, 256, mp)) != MP_OKAY)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2528
      return res;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2529
    if((res = mp_add_d(mp, ustr[ix], mp)) != MP_OKAY)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2530
      return res;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2531
  }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2532
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2533
  return MP_OKAY;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2534
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2535
} /* end mp_read_raw() */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2536
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2537
/* }}} */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2538
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2539
/* {{{ mp_raw_size(mp) */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2540
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2541
int    mp_raw_size(mp_int *mp)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2542
{
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2543
  ARGCHK(mp != NULL, 0);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2544
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2545
  return (USED(mp) * sizeof(mp_digit)) + 1;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2546
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2547
} /* end mp_raw_size() */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2548
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2549
/* }}} */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2550
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2551
/* {{{ mp_toraw(mp, str) */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2552
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2553
mp_err mp_toraw(mp_int *mp, char *str)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2554
{
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2555
  int  ix, jx, pos = 1;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2556
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2557
  ARGCHK(mp != NULL && str != NULL, MP_BADARG);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2558
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2559
  str[0] = (char)SIGN(mp);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2560
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2561
  /* Iterate over each digit... */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2562
  for(ix = USED(mp) - 1; ix >= 0; ix--) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2563
    mp_digit  d = DIGIT(mp, ix);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2564
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2565
    /* Unpack digit bytes, high order first */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2566
    for(jx = sizeof(mp_digit) - 1; jx >= 0; jx--) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2567
      str[pos++] = (char)(d >> (jx * CHAR_BIT));
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2568
    }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2569
  }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2570
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2571
  return MP_OKAY;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2572
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2573
} /* end mp_toraw() */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2574
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2575
/* }}} */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2576
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2577
/* {{{ mp_read_radix(mp, str, radix) */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2578
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2579
/*
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2580
  mp_read_radix(mp, str, radix)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2581
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2582
  Read an integer from the given string, and set mp to the resulting
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2583
  value.  The input is presumed to be in base 10.  Leading non-digit
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2584
  characters are ignored, and the function reads until a non-digit
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2585
  character or the end of the string.
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2586
 */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2587
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2588
mp_err  mp_read_radix(mp_int *mp, const char *str, int radix)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2589
{
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2590
  int     ix = 0, val = 0;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2591
  mp_err  res;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2592
  mp_sign sig = ZPOS;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2593
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2594
  ARGCHK(mp != NULL && str != NULL && radix >= 2 && radix <= MAX_RADIX,
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2595
         MP_BADARG);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2596
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2597
  mp_zero(mp);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2598
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2599
  /* Skip leading non-digit characters until a digit or '-' or '+' */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2600
  while(str[ix] &&
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2601
        (s_mp_tovalue(str[ix], radix) < 0) &&
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2602
        str[ix] != '-' &&
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2603
        str[ix] != '+') {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2604
    ++ix;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2605
  }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2606
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2607
  if(str[ix] == '-') {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2608
    sig = NEG;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2609
    ++ix;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2610
  } else if(str[ix] == '+') {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2611
    sig = ZPOS; /* this is the default anyway... */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2612
    ++ix;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2613
  }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2614
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2615
  while((val = s_mp_tovalue(str[ix], radix)) >= 0) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2616
    if((res = s_mp_mul_d(mp, radix)) != MP_OKAY)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2617
      return res;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2618
    if((res = s_mp_add_d(mp, val)) != MP_OKAY)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2619
      return res;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2620
    ++ix;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2621
  }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2622
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2623
  if(s_mp_cmp_d(mp, 0) == MP_EQ)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2624
    SIGN(mp) = ZPOS;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2625
  else
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2626
    SIGN(mp) = sig;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2627
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2628
  return MP_OKAY;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2629
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2630
} /* end mp_read_radix() */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2631
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2632
mp_err mp_read_variable_radix(mp_int *a, const char * str, int default_radix)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2633
{
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2634
  int     radix = default_radix;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2635
  int     cx;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2636
  mp_sign sig   = ZPOS;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2637
  mp_err  res;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2638
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2639
  /* Skip leading non-digit characters until a digit or '-' or '+' */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2640
  while ((cx = *str) != 0 &&
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2641
        (s_mp_tovalue(cx, radix) < 0) &&
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2642
        cx != '-' &&
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2643
        cx != '+') {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2644
    ++str;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2645
  }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2646
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2647
  if (cx == '-') {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2648
    sig = NEG;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2649
    ++str;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2650
  } else if (cx == '+') {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2651
    sig = ZPOS; /* this is the default anyway... */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2652
    ++str;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2653
  }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2654
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2655
  if (str[0] == '0') {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2656
    if ((str[1] | 0x20) == 'x') {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2657
      radix = 16;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2658
      str += 2;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2659
    } else {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2660
      radix = 8;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2661
      str++;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2662
    }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2663
  }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2664
  res = mp_read_radix(a, str, radix);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2665
  if (res == MP_OKAY) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2666
    MP_SIGN(a) = (s_mp_cmp_d(a, 0) == MP_EQ) ? ZPOS : sig;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2667
  }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2668
  return res;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2669
}
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2670
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2671
/* }}} */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2672
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2673
/* {{{ mp_radix_size(mp, radix) */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2674
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2675
int    mp_radix_size(mp_int *mp, int radix)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2676
{
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2677
  int  bits;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2678
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2679
  if(!mp || radix < 2 || radix > MAX_RADIX)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2680
    return 0;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2681
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2682
  bits = USED(mp) * DIGIT_BIT - 1;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2683
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2684
  return s_mp_outlen(bits, radix);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2685
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2686
} /* end mp_radix_size() */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2687
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2688
/* }}} */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2689
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2690
/* {{{ mp_toradix(mp, str, radix) */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2691
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2692
mp_err mp_toradix(mp_int *mp, char *str, int radix)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2693
{
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2694
  int  ix, pos = 0;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2695
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2696
  ARGCHK(mp != NULL && str != NULL, MP_BADARG);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2697
  ARGCHK(radix > 1 && radix <= MAX_RADIX, MP_RANGE);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2698
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2699
  if(mp_cmp_z(mp) == MP_EQ) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2700
    str[0] = '0';
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2701
    str[1] = '\0';
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2702
  } else {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2703
    mp_err   res;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2704
    mp_int   tmp;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2705
    mp_sign  sgn;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2706
    mp_digit rem, rdx = (mp_digit)radix;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2707
    char     ch;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2708
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2709
    if((res = mp_init_copy(&tmp, mp)) != MP_OKAY)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2710
      return res;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2711
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2712
    /* Save sign for later, and take absolute value */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2713
    sgn = SIGN(&tmp); SIGN(&tmp) = ZPOS;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2714
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2715
    /* Generate output digits in reverse order      */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2716
    while(mp_cmp_z(&tmp) != 0) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2717
      if((res = mp_div_d(&tmp, rdx, &tmp, &rem)) != MP_OKAY) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2718
        mp_clear(&tmp);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2719
        return res;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2720
      }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2721
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2722
      /* Generate digits, use capital letters */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2723
      ch = s_mp_todigit(rem, radix, 0);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2724
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2725
      str[pos++] = ch;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2726
    }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2727
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2728
    /* Add - sign if original value was negative */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2729
    if(sgn == NEG)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2730
      str[pos++] = '-';
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2731
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2732
    /* Add trailing NUL to end the string        */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2733
    str[pos--] = '\0';
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2734
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2735
    /* Reverse the digits and sign indicator     */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2736
    ix = 0;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2737
    while(ix < pos) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2738
      char tmp = str[ix];
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2739
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2740
      str[ix] = str[pos];
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2741
      str[pos] = tmp;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2742
      ++ix;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2743
      --pos;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2744
    }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2745
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2746
    mp_clear(&tmp);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2747
  }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2748
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2749
  return MP_OKAY;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2750
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2751
} /* end mp_toradix() */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2752
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2753
/* }}} */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2754
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2755
/* {{{ mp_tovalue(ch, r) */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2756
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2757
int    mp_tovalue(char ch, int r)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2758
{
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2759
  return s_mp_tovalue(ch, r);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2760
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2761
} /* end mp_tovalue() */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2762
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2763
/* }}} */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2764
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2765
/* }}} */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2766
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2767
/* {{{ mp_strerror(ec) */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2768
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2769
/*
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2770
  mp_strerror(ec)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2771
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2772
  Return a string describing the meaning of error code 'ec'.  The
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2773
  string returned is allocated in static memory, so the caller should
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2774
  not attempt to modify or free the memory associated with this
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2775
  string.
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2776
 */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2777
const char  *mp_strerror(mp_err ec)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2778
{
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2779
  int   aec = (ec < 0) ? -ec : ec;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2780
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2781
  /* Code values are negative, so the senses of these comparisons
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2782
     are accurate */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2783
  if(ec < MP_LAST_CODE || ec > MP_OKAY) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2784
    return mp_err_string[0];  /* unknown error code */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2785
  } else {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2786
    return mp_err_string[aec + 1];
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2787
  }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2788
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2789
} /* end mp_strerror() */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2790
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2791
/* }}} */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2792
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2793
/*========================================================================*/
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2794
/*------------------------------------------------------------------------*/
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2795
/* Static function definitions (internal use only)                        */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2796
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2797
/* {{{ Memory management */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2798
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2799
/* {{{ s_mp_grow(mp, min) */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2800
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2801
/* Make sure there are at least 'min' digits allocated to mp              */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2802
mp_err   s_mp_grow(mp_int *mp, mp_size min)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2803
{
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2804
  if(min > ALLOC(mp)) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2805
    mp_digit   *tmp;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2806
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2807
    /* Set min to next nearest default precision block size */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2808
    min = MP_ROUNDUP(min, s_mp_defprec);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2809
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2810
    if((tmp = s_mp_alloc(min, sizeof(mp_digit), FLAG(mp))) == NULL)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2811
      return MP_MEM;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2812
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2813
    s_mp_copy(DIGITS(mp), tmp, USED(mp));
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2814
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2815
#if MP_CRYPTO
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2816
    s_mp_setz(DIGITS(mp), ALLOC(mp));
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2817
#endif
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2818
    s_mp_free(DIGITS(mp), ALLOC(mp));
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2819
    DIGITS(mp) = tmp;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2820
    ALLOC(mp) = min;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2821
  }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2822
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2823
  return MP_OKAY;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2824
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2825
} /* end s_mp_grow() */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2826
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2827
/* }}} */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2828
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2829
/* {{{ s_mp_pad(mp, min) */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2830
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2831
/* Make sure the used size of mp is at least 'min', growing if needed     */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2832
mp_err   s_mp_pad(mp_int *mp, mp_size min)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2833
{
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2834
  if(min > USED(mp)) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2835
    mp_err  res;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2836
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2837
    /* Make sure there is room to increase precision  */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2838
    if (min > ALLOC(mp)) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2839
      if ((res = s_mp_grow(mp, min)) != MP_OKAY)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2840
        return res;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2841
    } else {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2842
      s_mp_setz(DIGITS(mp) + USED(mp), min - USED(mp));
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2843
    }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2844
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2845
    /* Increase precision; should already be 0-filled */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2846
    USED(mp) = min;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2847
  }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2848
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2849
  return MP_OKAY;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2850
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2851
} /* end s_mp_pad() */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2852
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2853
/* }}} */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2854
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2855
/* {{{ s_mp_setz(dp, count) */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2856
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2857
#if MP_MACRO == 0
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2858
/* Set 'count' digits pointed to by dp to be zeroes                       */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2859
void s_mp_setz(mp_digit *dp, mp_size count)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2860
{
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2861
#if MP_MEMSET == 0
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2862
  int  ix;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2863
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2864
  for(ix = 0; ix < count; ix++)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2865
    dp[ix] = 0;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2866
#else
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2867
  memset(dp, 0, count * sizeof(mp_digit));
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2868
#endif
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2869
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2870
} /* end s_mp_setz() */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2871
#endif
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2872
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2873
/* }}} */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2874
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2875
/* {{{ s_mp_copy(sp, dp, count) */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2876
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2877
#if MP_MACRO == 0
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2878
/* Copy 'count' digits from sp to dp                                      */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2879
void s_mp_copy(const mp_digit *sp, mp_digit *dp, mp_size count)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2880
{
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2881
#if MP_MEMCPY == 0
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2882
  int  ix;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2883
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2884
  for(ix = 0; ix < count; ix++)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2885
    dp[ix] = sp[ix];
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2886
#else
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2887
  memcpy(dp, sp, count * sizeof(mp_digit));
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2888
#endif
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2889
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2890
} /* end s_mp_copy() */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2891
#endif
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2892
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2893
/* }}} */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2894
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2895
/* {{{ s_mp_alloc(nb, ni, kmflag) */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2896
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2897
#if MP_MACRO == 0
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2898
/* Allocate ni records of nb bytes each, and return a pointer to that     */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2899
void    *s_mp_alloc(size_t nb, size_t ni, int kmflag)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2900
{
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2901
  ++mp_allocs;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2902
#ifdef _KERNEL
8175
27e80dc06104 6989705: ECC security code native code compiler warnings
vinnie
parents: 5506
diff changeset
  2903
  mp_int *mp;
3863
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2904
  mp = kmem_zalloc(nb * ni, kmflag);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2905
  if (mp != NULL)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2906
    FLAG(mp) = kmflag;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2907
  return (mp);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2908
#else
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2909
  return calloc(nb, ni);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2910
#endif
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2911
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2912
} /* end s_mp_alloc() */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2913
#endif
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2914
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2915
/* }}} */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2916
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2917
/* {{{ s_mp_free(ptr) */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2918
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2919
#if MP_MACRO == 0
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2920
/* Free the memory pointed to by ptr                                      */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2921
void     s_mp_free(void *ptr, mp_size alloc)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2922
{
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2923
  if(ptr) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2924
    ++mp_frees;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2925
#ifdef _KERNEL
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2926
    kmem_free(ptr, alloc * sizeof (mp_digit));
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2927
#else
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2928
    free(ptr);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2929
#endif
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2930
  }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2931
} /* end s_mp_free() */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2932
#endif
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2933
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2934
/* }}} */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2935
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2936
/* {{{ s_mp_clamp(mp) */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2937
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2938
#if MP_MACRO == 0
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2939
/* Remove leading zeroes from the given value                             */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2940
void     s_mp_clamp(mp_int *mp)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2941
{
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2942
  mp_size used = MP_USED(mp);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2943
  while (used > 1 && DIGIT(mp, used - 1) == 0)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2944
    --used;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2945
  MP_USED(mp) = used;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2946
} /* end s_mp_clamp() */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2947
#endif
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2948
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2949
/* }}} */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2950
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2951
/* {{{ s_mp_exch(a, b) */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2952
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2953
/* Exchange the data for a and b; (b, a) = (a, b)                         */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2954
void     s_mp_exch(mp_int *a, mp_int *b)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2955
{
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2956
  mp_int   tmp;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2957
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2958
  tmp = *a;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2959
  *a = *b;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2960
  *b = tmp;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2961
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2962
} /* end s_mp_exch() */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2963
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2964
/* }}} */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2965
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2966
/* }}} */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2967
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2968
/* {{{ Arithmetic helpers */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2969
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2970
/* {{{ s_mp_lshd(mp, p) */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2971
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2972
/*
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2973
   Shift mp leftward by p digits, growing if needed, and zero-filling
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2974
   the in-shifted digits at the right end.  This is a convenient
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2975
   alternative to multiplication by powers of the radix
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2976
   The value of USED(mp) must already have been set to the value for
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2977
   the shifted result.
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2978
 */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2979
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2980
mp_err   s_mp_lshd(mp_int *mp, mp_size p)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2981
{
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2982
  mp_err  res;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2983
  mp_size pos;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2984
  int     ix;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2985
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2986
  if(p == 0)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2987
    return MP_OKAY;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2988
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2989
  if (MP_USED(mp) == 1 && MP_DIGIT(mp, 0) == 0)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2990
    return MP_OKAY;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2991
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2992
  if((res = s_mp_pad(mp, USED(mp) + p)) != MP_OKAY)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2993
    return res;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2994
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2995
  pos = USED(mp) - 1;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2996
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2997
  /* Shift all the significant figures over as needed */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2998
  for(ix = pos - p; ix >= 0; ix--)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  2999
    DIGIT(mp, ix + p) = DIGIT(mp, ix);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3000
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3001
  /* Fill the bottom digits with zeroes */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3002
  for(ix = 0; ix < p; ix++)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3003
    DIGIT(mp, ix) = 0;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3004
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3005
  return MP_OKAY;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3006
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3007
} /* end s_mp_lshd() */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3008
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3009
/* }}} */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3010
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3011
/* {{{ s_mp_mul_2d(mp, d) */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3012
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3013
/*
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3014
  Multiply the integer by 2^d, where d is a number of bits.  This
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3015
  amounts to a bitwise shift of the value.
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3016
 */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3017
mp_err   s_mp_mul_2d(mp_int *mp, mp_digit d)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3018
{
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3019
  mp_err   res;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3020
  mp_digit dshift, bshift;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3021
  mp_digit mask;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3022
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3023
  ARGCHK(mp != NULL,  MP_BADARG);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3024
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3025
  dshift = d / MP_DIGIT_BIT;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3026
  bshift = d % MP_DIGIT_BIT;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3027
  /* bits to be shifted out of the top word */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3028
  mask   = ((mp_digit)~0 << (MP_DIGIT_BIT - bshift));
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3029
  mask  &= MP_DIGIT(mp, MP_USED(mp) - 1);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3030
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3031
  if (MP_OKAY != (res = s_mp_pad(mp, MP_USED(mp) + dshift + (mask != 0) )))
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3032
    return res;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3033
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3034
  if (dshift && MP_OKAY != (res = s_mp_lshd(mp, dshift)))
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3035
    return res;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3036
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3037
  if (bshift) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3038
    mp_digit *pa = MP_DIGITS(mp);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3039
    mp_digit *alim = pa + MP_USED(mp);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3040
    mp_digit  prev = 0;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3041
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3042
    for (pa += dshift; pa < alim; ) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3043
      mp_digit x = *pa;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3044
      *pa++ = (x << bshift) | prev;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3045
      prev = x >> (DIGIT_BIT - bshift);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3046
    }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3047
  }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3048
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3049
  s_mp_clamp(mp);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3050
  return MP_OKAY;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3051
} /* end s_mp_mul_2d() */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3052
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3053
/* {{{ s_mp_rshd(mp, p) */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3054
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3055
/*
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3056
   Shift mp rightward by p digits.  Maintains the invariant that
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3057
   digits above the precision are all zero.  Digits shifted off the
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3058
   end are lost.  Cannot fail.
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3059
 */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3060
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3061
void     s_mp_rshd(mp_int *mp, mp_size p)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3062
{
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3063
  mp_size  ix;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3064
  mp_digit *src, *dst;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3065
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3066
  if(p == 0)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3067
    return;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3068
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3069
  /* Shortcut when all digits are to be shifted off */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3070
  if(p >= USED(mp)) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3071
    s_mp_setz(DIGITS(mp), ALLOC(mp));
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3072
    USED(mp) = 1;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3073
    SIGN(mp) = ZPOS;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3074
    return;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3075
  }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3076
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3077
  /* Shift all the significant figures over as needed */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3078
  dst = MP_DIGITS(mp);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3079
  src = dst + p;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3080
  for (ix = USED(mp) - p; ix > 0; ix--)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3081
    *dst++ = *src++;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3082
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3083
  MP_USED(mp) -= p;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3084
  /* Fill the top digits with zeroes */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3085
  while (p-- > 0)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3086
    *dst++ = 0;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3087
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3088
#if 0
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3089
  /* Strip off any leading zeroes    */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3090
  s_mp_clamp(mp);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3091
#endif
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3092
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3093
} /* end s_mp_rshd() */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3094
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3095
/* }}} */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3096
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3097
/* {{{ s_mp_div_2(mp) */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3098
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3099
/* Divide by two -- take advantage of radix properties to do it fast      */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3100
void     s_mp_div_2(mp_int *mp)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3101
{
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3102
  s_mp_div_2d(mp, 1);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3103
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3104
} /* end s_mp_div_2() */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3105
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3106
/* }}} */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3107
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3108
/* {{{ s_mp_mul_2(mp) */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3109
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3110
mp_err s_mp_mul_2(mp_int *mp)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3111
{
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3112
  mp_digit *pd;
8175
27e80dc06104 6989705: ECC security code native code compiler warnings
vinnie
parents: 5506
diff changeset
  3113
  unsigned int      ix, used;
3863
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3114
  mp_digit kin = 0;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3115
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3116
  /* Shift digits leftward by 1 bit */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3117
  used = MP_USED(mp);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3118
  pd = MP_DIGITS(mp);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3119
  for (ix = 0; ix < used; ix++) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3120
    mp_digit d = *pd;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3121
    *pd++ = (d << 1) | kin;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3122
    kin = (d >> (DIGIT_BIT - 1));
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3123
  }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3124
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3125
  /* Deal with rollover from last digit */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3126
  if (kin) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3127
    if (ix >= ALLOC(mp)) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3128
      mp_err res;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3129
      if((res = s_mp_grow(mp, ALLOC(mp) + 1)) != MP_OKAY)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3130
        return res;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3131
    }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3132
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3133
    DIGIT(mp, ix) = kin;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3134
    USED(mp) += 1;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3135
  }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3136
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3137
  return MP_OKAY;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3138
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3139
} /* end s_mp_mul_2() */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3140
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3141
/* }}} */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3142
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3143
/* {{{ s_mp_mod_2d(mp, d) */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3144
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3145
/*
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3146
  Remainder the integer by 2^d, where d is a number of bits.  This
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3147
  amounts to a bitwise AND of the value, and does not require the full
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3148
  division code
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3149
 */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3150
void     s_mp_mod_2d(mp_int *mp, mp_digit d)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3151
{
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3152
  mp_size  ndig = (d / DIGIT_BIT), nbit = (d % DIGIT_BIT);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3153
  mp_size  ix;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3154
  mp_digit dmask;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3155
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3156
  if(ndig >= USED(mp))
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3157
    return;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3158
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3159
  /* Flush all the bits above 2^d in its digit */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3160
  dmask = ((mp_digit)1 << nbit) - 1;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3161
  DIGIT(mp, ndig) &= dmask;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3162
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3163
  /* Flush all digits above the one with 2^d in it */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3164
  for(ix = ndig + 1; ix < USED(mp); ix++)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3165
    DIGIT(mp, ix) = 0;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3166
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3167
  s_mp_clamp(mp);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3168
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3169
} /* end s_mp_mod_2d() */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3170
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3171
/* }}} */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3172
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3173
/* {{{ s_mp_div_2d(mp, d) */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3174
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3175
/*
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3176
  Divide the integer by 2^d, where d is a number of bits.  This
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3177
  amounts to a bitwise shift of the value, and does not require the
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3178
  full division code (used in Barrett reduction, see below)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3179
 */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3180
void     s_mp_div_2d(mp_int *mp, mp_digit d)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3181
{
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3182
  int       ix;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3183
  mp_digit  save, next, mask;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3184
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3185
  s_mp_rshd(mp, d / DIGIT_BIT);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3186
  d %= DIGIT_BIT;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3187
  if (d) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3188
    mask = ((mp_digit)1 << d) - 1;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3189
    save = 0;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3190
    for(ix = USED(mp) - 1; ix >= 0; ix--) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3191
      next = DIGIT(mp, ix) & mask;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3192
      DIGIT(mp, ix) = (DIGIT(mp, ix) >> d) | (save << (DIGIT_BIT - d));
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3193
      save = next;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3194
    }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3195
  }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3196
  s_mp_clamp(mp);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3197
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3198
} /* end s_mp_div_2d() */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3199
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3200
/* }}} */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3201
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3202
/* {{{ s_mp_norm(a, b, *d) */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3203
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3204
/*
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3205
  s_mp_norm(a, b, *d)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3206
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3207
  Normalize a and b for division, where b is the divisor.  In order
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3208
  that we might make good guesses for quotient digits, we want the
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3209
  leading digit of b to be at least half the radix, which we
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3210
  accomplish by multiplying a and b by a power of 2.  The exponent
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3211
  (shift count) is placed in *pd, so that the remainder can be shifted
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3212
  back at the end of the division process.
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3213
 */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3214
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3215
mp_err   s_mp_norm(mp_int *a, mp_int *b, mp_digit *pd)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3216
{
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3217
  mp_digit  d;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3218
  mp_digit  mask;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3219
  mp_digit  b_msd;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3220
  mp_err    res    = MP_OKAY;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3221
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3222
  d = 0;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3223
  mask  = DIGIT_MAX & ~(DIGIT_MAX >> 1);        /* mask is msb of digit */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3224
  b_msd = DIGIT(b, USED(b) - 1);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3225
  while (!(b_msd & mask)) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3226
    b_msd <<= 1;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3227
    ++d;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3228
  }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3229
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3230
  if (d) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3231
    MP_CHECKOK( s_mp_mul_2d(a, d) );
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3232
    MP_CHECKOK( s_mp_mul_2d(b, d) );
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3233
  }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3234
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3235
  *pd = d;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3236
CLEANUP:
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3237
  return res;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3238
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3239
} /* end s_mp_norm() */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3240
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3241
/* }}} */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3242
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3243
/* }}} */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3244
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3245
/* {{{ Primitive digit arithmetic */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3246
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3247
/* {{{ s_mp_add_d(mp, d) */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3248
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3249
/* Add d to |mp| in place                                                 */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3250
mp_err   s_mp_add_d(mp_int *mp, mp_digit d)    /* unsigned digit addition */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3251
{
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3252
#if !defined(MP_NO_MP_WORD) && !defined(MP_NO_ADD_WORD)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3253
  mp_word   w, k = 0;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3254
  mp_size   ix = 1;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3255
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3256
  w = (mp_word)DIGIT(mp, 0) + d;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3257
  DIGIT(mp, 0) = ACCUM(w);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3258
  k = CARRYOUT(w);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3259
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3260
  while(ix < USED(mp) && k) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3261
    w = (mp_word)DIGIT(mp, ix) + k;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3262
    DIGIT(mp, ix) = ACCUM(w);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3263
    k = CARRYOUT(w);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3264
    ++ix;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3265
  }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3266
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3267
  if(k != 0) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3268
    mp_err  res;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3269
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3270
    if((res = s_mp_pad(mp, USED(mp) + 1)) != MP_OKAY)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3271
      return res;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3272
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3273
    DIGIT(mp, ix) = (mp_digit)k;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3274
  }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3275
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3276
  return MP_OKAY;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3277
#else
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3278
  mp_digit * pmp = MP_DIGITS(mp);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3279
  mp_digit sum, mp_i, carry = 0;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3280
  mp_err   res = MP_OKAY;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3281
  int used = (int)MP_USED(mp);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3282
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3283
  mp_i = *pmp;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3284
  *pmp++ = sum = d + mp_i;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3285
  carry = (sum < d);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3286
  while (carry && --used > 0) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3287
    mp_i = *pmp;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3288
    *pmp++ = sum = carry + mp_i;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3289
    carry = !sum;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3290
  }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3291
  if (carry && !used) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3292
    /* mp is growing */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3293
    used = MP_USED(mp);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3294
    MP_CHECKOK( s_mp_pad(mp, used + 1) );
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3295
    MP_DIGIT(mp, used) = carry;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3296
  }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3297
CLEANUP:
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3298
  return res;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3299
#endif
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3300
} /* end s_mp_add_d() */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3301
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3302
/* }}} */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3303
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3304
/* {{{ s_mp_sub_d(mp, d) */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3305
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3306
/* Subtract d from |mp| in place, assumes |mp| > d                        */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3307
mp_err   s_mp_sub_d(mp_int *mp, mp_digit d)    /* unsigned digit subtract */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3308
{
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3309
#if !defined(MP_NO_MP_WORD) && !defined(MP_NO_SUB_WORD)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3310
  mp_word   w, b = 0;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3311
  mp_size   ix = 1;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3312
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3313
  /* Compute initial subtraction    */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3314
  w = (RADIX + (mp_word)DIGIT(mp, 0)) - d;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3315
  b = CARRYOUT(w) ? 0 : 1;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3316
  DIGIT(mp, 0) = ACCUM(w);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3317
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3318
  /* Propagate borrows leftward     */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3319
  while(b && ix < USED(mp)) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3320
    w = (RADIX + (mp_word)DIGIT(mp, ix)) - b;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3321
    b = CARRYOUT(w) ? 0 : 1;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3322
    DIGIT(mp, ix) = ACCUM(w);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3323
    ++ix;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3324
  }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3325
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3326
  /* Remove leading zeroes          */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3327
  s_mp_clamp(mp);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3328
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3329
  /* If we have a borrow out, it's a violation of the input invariant */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3330
  if(b)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3331
    return MP_RANGE;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3332
  else
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3333
    return MP_OKAY;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3334
#else
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3335
  mp_digit *pmp = MP_DIGITS(mp);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3336
  mp_digit mp_i, diff, borrow;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3337
  mp_size  used = MP_USED(mp);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3338
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3339
  mp_i = *pmp;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3340
  *pmp++ = diff = mp_i - d;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3341
  borrow = (diff > mp_i);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3342
  while (borrow && --used) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3343
    mp_i = *pmp;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3344
    *pmp++ = diff = mp_i - borrow;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3345
    borrow = (diff > mp_i);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3346
  }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3347
  s_mp_clamp(mp);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3348
  return (borrow && !used) ? MP_RANGE : MP_OKAY;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3349
#endif
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3350
} /* end s_mp_sub_d() */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3351
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3352
/* }}} */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3353
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3354
/* {{{ s_mp_mul_d(a, d) */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3355
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3356
/* Compute a = a * d, single digit multiplication                         */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3357
mp_err   s_mp_mul_d(mp_int *a, mp_digit d)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3358
{
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3359
  mp_err  res;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3360
  mp_size used;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3361
  int     pow;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3362
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3363
  if (!d) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3364
    mp_zero(a);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3365
    return MP_OKAY;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3366
  }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3367
  if (d == 1)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3368
    return MP_OKAY;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3369
  if (0 <= (pow = s_mp_ispow2d(d))) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3370
    return s_mp_mul_2d(a, (mp_digit)pow);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3371
  }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3372
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3373
  used = MP_USED(a);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3374
  MP_CHECKOK( s_mp_pad(a, used + 1) );
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3375
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3376
  s_mpv_mul_d(MP_DIGITS(a), used, d, MP_DIGITS(a));
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3377
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3378
  s_mp_clamp(a);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3379
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3380
CLEANUP:
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3381
  return res;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3382
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3383
} /* end s_mp_mul_d() */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3384
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3385
/* }}} */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3386
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3387
/* {{{ s_mp_div_d(mp, d, r) */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3388
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3389
/*
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3390
  s_mp_div_d(mp, d, r)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3391
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3392
  Compute the quotient mp = mp / d and remainder r = mp mod d, for a
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3393
  single digit d.  If r is null, the remainder will be discarded.
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3394
 */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3395
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3396
mp_err   s_mp_div_d(mp_int *mp, mp_digit d, mp_digit *r)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3397
{
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3398
#if !defined(MP_NO_MP_WORD) && !defined(MP_NO_DIV_WORD)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3399
  mp_word   w = 0, q;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3400
#else
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3401
  mp_digit  w, q;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3402
#endif
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3403
  int       ix;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3404
  mp_err    res;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3405
  mp_int    quot;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3406
  mp_int    rem;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3407
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3408
  if(d == 0)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3409
    return MP_RANGE;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3410
  if (d == 1) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3411
    if (r)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3412
      *r = 0;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3413
    return MP_OKAY;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3414
  }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3415
  /* could check for power of 2 here, but mp_div_d does that. */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3416
  if (MP_USED(mp) == 1) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3417
    mp_digit n   = MP_DIGIT(mp,0);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3418
    mp_digit rem;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3419
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3420
    q   = n / d;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3421
    rem = n % d;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3422
    MP_DIGIT(mp,0) = q;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3423
    if (r)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3424
      *r = rem;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3425
    return MP_OKAY;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3426
  }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3427
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3428
  MP_DIGITS(&rem)  = 0;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3429
  MP_DIGITS(&quot) = 0;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3430
  /* Make room for the quotient */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3431
  MP_CHECKOK( mp_init_size(&quot, USED(mp), FLAG(mp)) );
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3432
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3433
#if !defined(MP_NO_MP_WORD) && !defined(MP_NO_DIV_WORD)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3434
  for(ix = USED(mp) - 1; ix >= 0; ix--) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3435
    w = (w << DIGIT_BIT) | DIGIT(mp, ix);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3436
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3437
    if(w >= d) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3438
      q = w / d;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3439
      w = w % d;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3440
    } else {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3441
      q = 0;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3442
    }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3443
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3444
    s_mp_lshd(&quot, 1);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3445
    DIGIT(&quot, 0) = (mp_digit)q;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3446
  }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3447
#else
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3448
  {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3449
    mp_digit p;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3450
#if !defined(MP_ASSEMBLY_DIV_2DX1D)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3451
    mp_digit norm;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3452
#endif
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3453
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3454
    MP_CHECKOK( mp_init_copy(&rem, mp) );
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3455
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3456
#if !defined(MP_ASSEMBLY_DIV_2DX1D)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3457
    MP_DIGIT(&quot, 0) = d;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3458
    MP_CHECKOK( s_mp_norm(&rem, &quot, &norm) );
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3459
    if (norm)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3460
      d <<= norm;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3461
    MP_DIGIT(&quot, 0) = 0;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3462
#endif
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3463
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3464
    p = 0;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3465
    for (ix = USED(&rem) - 1; ix >= 0; ix--) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3466
      w = DIGIT(&rem, ix);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3467
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3468
      if (p) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3469
        MP_CHECKOK( s_mpv_div_2dx1d(p, w, d, &q, &w) );
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3470
      } else if (w >= d) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3471
        q = w / d;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3472
        w = w % d;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3473
      } else {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3474
        q = 0;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3475
      }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3476
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3477
      MP_CHECKOK( s_mp_lshd(&quot, 1) );
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3478
      DIGIT(&quot, 0) = q;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3479
      p = w;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3480
    }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3481
#if !defined(MP_ASSEMBLY_DIV_2DX1D)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3482
    if (norm)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3483
      w >>= norm;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3484
#endif
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3485
  }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3486
#endif
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3487
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3488
  /* Deliver the remainder, if desired */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3489
  if(r)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3490
    *r = (mp_digit)w;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3491
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3492
  s_mp_clamp(&quot);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3493
  mp_exch(&quot, mp);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3494
CLEANUP:
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3495
  mp_clear(&quot);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3496
  mp_clear(&rem);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3497
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3498
  return res;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3499
} /* end s_mp_div_d() */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3500
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3501
/* }}} */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3502
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3503
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3504
/* }}} */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3505
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3506
/* {{{ Primitive full arithmetic */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3507
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3508
/* {{{ s_mp_add(a, b) */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3509
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3510
/* Compute a = |a| + |b|                                                  */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3511
mp_err   s_mp_add(mp_int *a, const mp_int *b)  /* magnitude addition      */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3512
{
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3513
#if !defined(MP_NO_MP_WORD) && !defined(MP_NO_ADD_WORD)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3514
  mp_word   w = 0;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3515
#else
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3516
  mp_digit  d, sum, carry = 0;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3517
#endif
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3518
  mp_digit *pa, *pb;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3519
  mp_size   ix;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3520
  mp_size   used;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3521
  mp_err    res;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3522
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3523
  /* Make sure a has enough precision for the output value */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3524
  if((USED(b) > USED(a)) && (res = s_mp_pad(a, USED(b))) != MP_OKAY)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3525
    return res;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3526
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3527
  /*
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3528
    Add up all digits up to the precision of b.  If b had initially
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3529
    the same precision as a, or greater, we took care of it by the
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3530
    padding step above, so there is no problem.  If b had initially
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3531
    less precision, we'll have to make sure the carry out is duly
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3532
    propagated upward among the higher-order digits of the sum.
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3533
   */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3534
  pa = MP_DIGITS(a);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3535
  pb = MP_DIGITS(b);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3536
  used = MP_USED(b);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3537
  for(ix = 0; ix < used; ix++) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3538
#if !defined(MP_NO_MP_WORD) && !defined(MP_NO_ADD_WORD)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3539
    w = w + *pa + *pb++;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3540
    *pa++ = ACCUM(w);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3541
    w = CARRYOUT(w);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3542
#else
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3543
    d = *pa;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3544
    sum = d + *pb++;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3545
    d = (sum < d);                      /* detect overflow */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3546
    *pa++ = sum += carry;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3547
    carry = d + (sum < carry);          /* detect overflow */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3548
#endif
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3549
  }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3550
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3551
  /* If we run out of 'b' digits before we're actually done, make
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3552
     sure the carries get propagated upward...
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3553
   */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3554
  used = MP_USED(a);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3555
#if !defined(MP_NO_MP_WORD) && !defined(MP_NO_ADD_WORD)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3556
  while (w && ix < used) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3557
    w = w + *pa;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3558
    *pa++ = ACCUM(w);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3559
    w = CARRYOUT(w);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3560
    ++ix;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3561
  }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3562
#else
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3563
  while (carry && ix < used) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3564
    sum = carry + *pa;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3565
    *pa++ = sum;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3566
    carry = !sum;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3567
    ++ix;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3568
  }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3569
#endif
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3570
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3571
  /* If there's an overall carry out, increase precision and include
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3572
     it.  We could have done this initially, but why touch the memory
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3573
     allocator unless we're sure we have to?
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3574
   */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3575
#if !defined(MP_NO_MP_WORD) && !defined(MP_NO_ADD_WORD)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3576
  if (w) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3577
    if((res = s_mp_pad(a, used + 1)) != MP_OKAY)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3578
      return res;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3579
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3580
    DIGIT(a, ix) = (mp_digit)w;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3581
  }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3582
#else
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3583
  if (carry) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3584
    if((res = s_mp_pad(a, used + 1)) != MP_OKAY)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3585
      return res;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3586
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3587
    DIGIT(a, used) = carry;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3588
  }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3589
#endif
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3590
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3591
  return MP_OKAY;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3592
} /* end s_mp_add() */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3593
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3594
/* }}} */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3595
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3596
/* Compute c = |a| + |b|         */ /* magnitude addition      */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3597
mp_err   s_mp_add_3arg(const mp_int *a, const mp_int *b, mp_int *c)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3598
{
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3599
  mp_digit *pa, *pb, *pc;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3600
#if !defined(MP_NO_MP_WORD) && !defined(MP_NO_ADD_WORD)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3601
  mp_word   w = 0;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3602
#else
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3603
  mp_digit  sum, carry = 0, d;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3604
#endif
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3605
  mp_size   ix;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3606
  mp_size   used;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3607
  mp_err    res;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3608
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3609
  MP_SIGN(c) = MP_SIGN(a);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3610
  if (MP_USED(a) < MP_USED(b)) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3611
    const mp_int *xch = a;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3612
    a = b;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3613
    b = xch;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3614
  }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3615
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3616
  /* Make sure a has enough precision for the output value */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3617
  if (MP_OKAY != (res = s_mp_pad(c, MP_USED(a))))
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3618
    return res;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3619
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3620
  /*
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3621
    Add up all digits up to the precision of b.  If b had initially
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3622
    the same precision as a, or greater, we took care of it by the
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3623
    exchange step above, so there is no problem.  If b had initially
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3624
    less precision, we'll have to make sure the carry out is duly
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3625
    propagated upward among the higher-order digits of the sum.
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3626
   */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3627
  pa = MP_DIGITS(a);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3628
  pb = MP_DIGITS(b);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3629
  pc = MP_DIGITS(c);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3630
  used = MP_USED(b);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3631
  for (ix = 0; ix < used; ix++) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3632
#if !defined(MP_NO_MP_WORD) && !defined(MP_NO_ADD_WORD)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3633
    w = w + *pa++ + *pb++;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3634
    *pc++ = ACCUM(w);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3635
    w = CARRYOUT(w);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3636
#else
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3637
    d = *pa++;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3638
    sum = d + *pb++;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3639
    d = (sum < d);                      /* detect overflow */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3640
    *pc++ = sum += carry;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3641
    carry = d + (sum < carry);          /* detect overflow */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3642
#endif
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3643
  }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3644
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3645
  /* If we run out of 'b' digits before we're actually done, make
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3646
     sure the carries get propagated upward...
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3647
   */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3648
  for (used = MP_USED(a); ix < used; ++ix) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3649
#if !defined(MP_NO_MP_WORD) && !defined(MP_NO_ADD_WORD)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3650
    w = w + *pa++;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3651
    *pc++ = ACCUM(w);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3652
    w = CARRYOUT(w);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3653
#else
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3654
    *pc++ = sum = carry + *pa++;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3655
    carry = (sum < carry);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3656
#endif
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3657
  }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3658
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3659
  /* If there's an overall carry out, increase precision and include
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3660
     it.  We could have done this initially, but why touch the memory
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3661
     allocator unless we're sure we have to?
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3662
   */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3663
#if !defined(MP_NO_MP_WORD) && !defined(MP_NO_ADD_WORD)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3664
  if (w) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3665
    if((res = s_mp_pad(c, used + 1)) != MP_OKAY)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3666
      return res;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3667
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3668
    DIGIT(c, used) = (mp_digit)w;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3669
    ++used;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3670
  }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3671
#else
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3672
  if (carry) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3673
    if((res = s_mp_pad(c, used + 1)) != MP_OKAY)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3674
      return res;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3675
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3676
    DIGIT(c, used) = carry;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3677
    ++used;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3678
  }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3679
#endif
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3680
  MP_USED(c) = used;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3681
  return MP_OKAY;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3682
}
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3683
/* {{{ s_mp_add_offset(a, b, offset) */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3684
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3685
/* Compute a = |a| + ( |b| * (RADIX ** offset) )             */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3686
mp_err   s_mp_add_offset(mp_int *a, mp_int *b, mp_size offset)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3687
{
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3688
#if !defined(MP_NO_MP_WORD) && !defined(MP_NO_ADD_WORD)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3689
  mp_word   w, k = 0;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3690
#else
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3691
  mp_digit  d, sum, carry = 0;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3692
#endif
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3693
  mp_size   ib;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3694
  mp_size   ia;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3695
  mp_size   lim;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3696
  mp_err    res;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3697
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3698
  /* Make sure a has enough precision for the output value */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3699
  lim = MP_USED(b) + offset;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3700
  if((lim > USED(a)) && (res = s_mp_pad(a, lim)) != MP_OKAY)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3701
    return res;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3702
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3703
  /*
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3704
    Add up all digits up to the precision of b.  If b had initially
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3705
    the same precision as a, or greater, we took care of it by the
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3706
    padding step above, so there is no problem.  If b had initially
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3707
    less precision, we'll have to make sure the carry out is duly
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3708
    propagated upward among the higher-order digits of the sum.
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3709
   */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3710
  lim = USED(b);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3711
  for(ib = 0, ia = offset; ib < lim; ib++, ia++) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3712
#if !defined(MP_NO_MP_WORD) && !defined(MP_NO_ADD_WORD)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3713
    w = (mp_word)DIGIT(a, ia) + DIGIT(b, ib) + k;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3714
    DIGIT(a, ia) = ACCUM(w);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3715
    k = CARRYOUT(w);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3716
#else
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3717
    d = MP_DIGIT(a, ia);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3718
    sum = d + MP_DIGIT(b, ib);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3719
    d = (sum < d);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3720
    MP_DIGIT(a,ia) = sum += carry;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3721
    carry = d + (sum < carry);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3722
#endif
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3723
  }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3724
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3725
  /* If we run out of 'b' digits before we're actually done, make
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3726
     sure the carries get propagated upward...
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3727
   */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3728
#if !defined(MP_NO_MP_WORD) && !defined(MP_NO_ADD_WORD)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3729
  for (lim = MP_USED(a); k && (ia < lim); ++ia) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3730
    w = (mp_word)DIGIT(a, ia) + k;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3731
    DIGIT(a, ia) = ACCUM(w);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3732
    k = CARRYOUT(w);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3733
  }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3734
#else
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3735
  for (lim = MP_USED(a); carry && (ia < lim); ++ia) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3736
    d = MP_DIGIT(a, ia);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3737
    MP_DIGIT(a,ia) = sum = d + carry;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3738
    carry = (sum < d);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3739
  }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3740
#endif
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3741
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3742
  /* If there's an overall carry out, increase precision and include
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3743
     it.  We could have done this initially, but why touch the memory
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3744
     allocator unless we're sure we have to?
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3745
   */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3746
#if !defined(MP_NO_MP_WORD) && !defined(MP_NO_ADD_WORD)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3747
  if(k) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3748
    if((res = s_mp_pad(a, USED(a) + 1)) != MP_OKAY)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3749
      return res;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3750
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3751
    DIGIT(a, ia) = (mp_digit)k;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3752
  }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3753
#else
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3754
  if (carry) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3755
    if((res = s_mp_pad(a, lim + 1)) != MP_OKAY)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3756
      return res;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3757
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3758
    DIGIT(a, lim) = carry;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3759
  }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3760
#endif
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3761
  s_mp_clamp(a);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3762
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3763
  return MP_OKAY;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3764
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3765
} /* end s_mp_add_offset() */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3766
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3767
/* }}} */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3768
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3769
/* {{{ s_mp_sub(a, b) */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3770
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3771
/* Compute a = |a| - |b|, assumes |a| >= |b|                              */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3772
mp_err   s_mp_sub(mp_int *a, const mp_int *b)  /* magnitude subtract      */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3773
{
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3774
  mp_digit *pa, *pb, *limit;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3775
#if !defined(MP_NO_MP_WORD) && !defined(MP_NO_SUB_WORD)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3776
  mp_sword  w = 0;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3777
#else
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3778
  mp_digit  d, diff, borrow = 0;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3779
#endif
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3780
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3781
  /*
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3782
    Subtract and propagate borrow.  Up to the precision of b, this
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3783
    accounts for the digits of b; after that, we just make sure the
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3784
    carries get to the right place.  This saves having to pad b out to
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3785
    the precision of a just to make the loops work right...
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3786
   */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3787
  pa = MP_DIGITS(a);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3788
  pb = MP_DIGITS(b);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3789
  limit = pb + MP_USED(b);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3790
  while (pb < limit) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3791
#if !defined(MP_NO_MP_WORD) && !defined(MP_NO_SUB_WORD)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3792
    w = w + *pa - *pb++;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3793
    *pa++ = ACCUM(w);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3794
    w >>= MP_DIGIT_BIT;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3795
#else
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3796
    d = *pa;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3797
    diff = d - *pb++;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3798
    d = (diff > d);                             /* detect borrow */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3799
    if (borrow && --diff == MP_DIGIT_MAX)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3800
      ++d;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3801
    *pa++ = diff;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3802
    borrow = d;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3803
#endif
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3804
  }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3805
  limit = MP_DIGITS(a) + MP_USED(a);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3806
#if !defined(MP_NO_MP_WORD) && !defined(MP_NO_SUB_WORD)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3807
  while (w && pa < limit) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3808
    w = w + *pa;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3809
    *pa++ = ACCUM(w);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3810
    w >>= MP_DIGIT_BIT;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3811
  }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3812
#else
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3813
  while (borrow && pa < limit) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3814
    d = *pa;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3815
    *pa++ = diff = d - borrow;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3816
    borrow = (diff > d);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3817
  }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3818
#endif
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3819
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3820
  /* Clobber any leading zeroes we created    */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3821
  s_mp_clamp(a);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3822
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3823
  /*
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3824
     If there was a borrow out, then |b| > |a| in violation
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3825
     of our input invariant.  We've already done the work,
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3826
     but we'll at least complain about it...
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3827
   */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3828
#if !defined(MP_NO_MP_WORD) && !defined(MP_NO_SUB_WORD)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3829
  return w ? MP_RANGE : MP_OKAY;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3830
#else
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3831
  return borrow ? MP_RANGE : MP_OKAY;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3832
#endif
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3833
} /* end s_mp_sub() */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3834
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3835
/* }}} */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3836
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3837
/* Compute c = |a| - |b|, assumes |a| >= |b| */ /* magnitude subtract      */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3838
mp_err   s_mp_sub_3arg(const mp_int *a, const mp_int *b, mp_int *c)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3839
{
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3840
  mp_digit *pa, *pb, *pc;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3841
#if !defined(MP_NO_MP_WORD) && !defined(MP_NO_SUB_WORD)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3842
  mp_sword  w = 0;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3843
#else
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3844
  mp_digit  d, diff, borrow = 0;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3845
#endif
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3846
  int       ix, limit;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3847
  mp_err    res;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3848
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3849
  MP_SIGN(c) = MP_SIGN(a);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3850
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3851
  /* Make sure a has enough precision for the output value */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3852
  if (MP_OKAY != (res = s_mp_pad(c, MP_USED(a))))
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3853
    return res;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3854
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3855
  /*
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3856
    Subtract and propagate borrow.  Up to the precision of b, this
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3857
    accounts for the digits of b; after that, we just make sure the
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3858
    carries get to the right place.  This saves having to pad b out to
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3859
    the precision of a just to make the loops work right...
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3860
   */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3861
  pa = MP_DIGITS(a);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3862
  pb = MP_DIGITS(b);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3863
  pc = MP_DIGITS(c);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3864
  limit = MP_USED(b);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3865
  for (ix = 0; ix < limit; ++ix) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3866
#if !defined(MP_NO_MP_WORD) && !defined(MP_NO_SUB_WORD)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3867
    w = w + *pa++ - *pb++;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3868
    *pc++ = ACCUM(w);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3869
    w >>= MP_DIGIT_BIT;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3870
#else
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3871
    d = *pa++;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3872
    diff = d - *pb++;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3873
    d = (diff > d);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3874
    if (borrow && --diff == MP_DIGIT_MAX)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3875
      ++d;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3876
    *pc++ = diff;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3877
    borrow = d;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3878
#endif
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3879
  }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3880
  for (limit = MP_USED(a); ix < limit; ++ix) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3881
#if !defined(MP_NO_MP_WORD) && !defined(MP_NO_SUB_WORD)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3882
    w = w + *pa++;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3883
    *pc++ = ACCUM(w);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3884
    w >>= MP_DIGIT_BIT;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3885
#else
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3886
    d = *pa++;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3887
    *pc++ = diff = d - borrow;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3888
    borrow = (diff > d);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3889
#endif
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3890
  }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3891
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3892
  /* Clobber any leading zeroes we created    */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3893
  MP_USED(c) = ix;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3894
  s_mp_clamp(c);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3895
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3896
  /*
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3897
     If there was a borrow out, then |b| > |a| in violation
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3898
     of our input invariant.  We've already done the work,
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3899
     but we'll at least complain about it...
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3900
   */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3901
#if !defined(MP_NO_MP_WORD) && !defined(MP_NO_SUB_WORD)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3902
  return w ? MP_RANGE : MP_OKAY;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3903
#else
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3904
  return borrow ? MP_RANGE : MP_OKAY;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3905
#endif
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3906
}
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3907
/* {{{ s_mp_mul(a, b) */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3908
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3909
/* Compute a = |a| * |b|                                                  */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3910
mp_err   s_mp_mul(mp_int *a, const mp_int *b)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3911
{
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3912
  return mp_mul(a, b, a);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3913
} /* end s_mp_mul() */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3914
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3915
/* }}} */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3916
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3917
#if defined(MP_USE_UINT_DIGIT) && defined(MP_USE_LONG_LONG_MULTIPLY)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3918
/* This trick works on Sparc V8 CPUs with the Workshop compilers. */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3919
#define MP_MUL_DxD(a, b, Phi, Plo) \
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3920
  { unsigned long long product = (unsigned long long)a * b; \
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3921
    Plo = (mp_digit)product; \
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3922
    Phi = (mp_digit)(product >> MP_DIGIT_BIT); }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3923
#elif defined(OSF1)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3924
#define MP_MUL_DxD(a, b, Phi, Plo) \
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3925
  { Plo = asm ("mulq %a0, %a1, %v0", a, b);\
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3926
    Phi = asm ("umulh %a0, %a1, %v0", a, b); }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3927
#else
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3928
#define MP_MUL_DxD(a, b, Phi, Plo) \
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3929
  { mp_digit a0b1, a1b0; \
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3930
    Plo = (a & MP_HALF_DIGIT_MAX) * (b & MP_HALF_DIGIT_MAX); \
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3931
    Phi = (a >> MP_HALF_DIGIT_BIT) * (b >> MP_HALF_DIGIT_BIT); \
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3932
    a0b1 = (a & MP_HALF_DIGIT_MAX) * (b >> MP_HALF_DIGIT_BIT); \
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3933
    a1b0 = (a >> MP_HALF_DIGIT_BIT) * (b & MP_HALF_DIGIT_MAX); \
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3934
    a1b0 += a0b1; \
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3935
    Phi += a1b0 >> MP_HALF_DIGIT_BIT; \
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3936
    if (a1b0 < a0b1)  \
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3937
      Phi += MP_HALF_RADIX; \
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3938
    a1b0 <<= MP_HALF_DIGIT_BIT; \
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3939
    Plo += a1b0; \
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3940
    if (Plo < a1b0) \
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3941
      ++Phi; \
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3942
  }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3943
#endif
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3944
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3945
#if !defined(MP_ASSEMBLY_MULTIPLY)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3946
/* c = a * b */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3947
void s_mpv_mul_d(const mp_digit *a, mp_size a_len, mp_digit b, mp_digit *c)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3948
{
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3949
#if !defined(MP_NO_MP_WORD) && !defined(MP_NO_MUL_WORD)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3950
  mp_digit   d = 0;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3951
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3952
  /* Inner product:  Digits of a */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3953
  while (a_len--) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3954
    mp_word w = ((mp_word)b * *a++) + d;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3955
    *c++ = ACCUM(w);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3956
    d = CARRYOUT(w);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3957
  }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3958
  *c = d;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3959
#else
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3960
  mp_digit carry = 0;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3961
  while (a_len--) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3962
    mp_digit a_i = *a++;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3963
    mp_digit a0b0, a1b1;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3964
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3965
    MP_MUL_DxD(a_i, b, a1b1, a0b0);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3966
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3967
    a0b0 += carry;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3968
    if (a0b0 < carry)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3969
      ++a1b1;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3970
    *c++ = a0b0;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3971
    carry = a1b1;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3972
  }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3973
  *c = carry;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3974
#endif
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3975
}
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3976
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3977
/* c += a * b */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3978
void s_mpv_mul_d_add(const mp_digit *a, mp_size a_len, mp_digit b,
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3979
                              mp_digit *c)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3980
{
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3981
#if !defined(MP_NO_MP_WORD) && !defined(MP_NO_MUL_WORD)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3982
  mp_digit   d = 0;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3983
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3984
  /* Inner product:  Digits of a */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3985
  while (a_len--) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3986
    mp_word w = ((mp_word)b * *a++) + *c + d;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3987
    *c++ = ACCUM(w);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3988
    d = CARRYOUT(w);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3989
  }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3990
  *c = d;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3991
#else
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3992
  mp_digit carry = 0;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3993
  while (a_len--) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3994
    mp_digit a_i = *a++;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3995
    mp_digit a0b0, a1b1;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3996
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3997
    MP_MUL_DxD(a_i, b, a1b1, a0b0);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3998
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  3999
    a0b0 += carry;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4000
    if (a0b0 < carry)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4001
      ++a1b1;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4002
    a0b0 += a_i = *c;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4003
    if (a0b0 < a_i)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4004
      ++a1b1;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4005
    *c++ = a0b0;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4006
    carry = a1b1;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4007
  }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4008
  *c = carry;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4009
#endif
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4010
}
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4011
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4012
/* Presently, this is only used by the Montgomery arithmetic code. */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4013
/* c += a * b */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4014
void s_mpv_mul_d_add_prop(const mp_digit *a, mp_size a_len, mp_digit b, mp_digit *c)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4015
{
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4016
#if !defined(MP_NO_MP_WORD) && !defined(MP_NO_MUL_WORD)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4017
  mp_digit   d = 0;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4018
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4019
  /* Inner product:  Digits of a */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4020
  while (a_len--) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4021
    mp_word w = ((mp_word)b * *a++) + *c + d;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4022
    *c++ = ACCUM(w);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4023
    d = CARRYOUT(w);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4024
  }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4025
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4026
  while (d) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4027
    mp_word w = (mp_word)*c + d;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4028
    *c++ = ACCUM(w);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4029
    d = CARRYOUT(w);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4030
  }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4031
#else
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4032
  mp_digit carry = 0;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4033
  while (a_len--) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4034
    mp_digit a_i = *a++;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4035
    mp_digit a0b0, a1b1;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4036
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4037
    MP_MUL_DxD(a_i, b, a1b1, a0b0);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4038
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4039
    a0b0 += carry;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4040
    if (a0b0 < carry)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4041
      ++a1b1;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4042
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4043
    a0b0 += a_i = *c;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4044
    if (a0b0 < a_i)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4045
      ++a1b1;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4046
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4047
    *c++ = a0b0;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4048
    carry = a1b1;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4049
  }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4050
  while (carry) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4051
    mp_digit c_i = *c;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4052
    carry += c_i;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4053
    *c++ = carry;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4054
    carry = carry < c_i;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4055
  }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4056
#endif
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4057
}
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4058
#endif
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4059
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4060
#if defined(MP_USE_UINT_DIGIT) && defined(MP_USE_LONG_LONG_MULTIPLY)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4061
/* This trick works on Sparc V8 CPUs with the Workshop compilers. */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4062
#define MP_SQR_D(a, Phi, Plo) \
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4063
  { unsigned long long square = (unsigned long long)a * a; \
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4064
    Plo = (mp_digit)square; \
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4065
    Phi = (mp_digit)(square >> MP_DIGIT_BIT); }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4066
#elif defined(OSF1)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4067
#define MP_SQR_D(a, Phi, Plo) \
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4068
  { Plo = asm ("mulq  %a0, %a0, %v0", a);\
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4069
    Phi = asm ("umulh %a0, %a0, %v0", a); }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4070
#else
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4071
#define MP_SQR_D(a, Phi, Plo) \
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4072
  { mp_digit Pmid; \
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4073
    Plo  = (a  & MP_HALF_DIGIT_MAX) * (a  & MP_HALF_DIGIT_MAX); \
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4074
    Phi  = (a >> MP_HALF_DIGIT_BIT) * (a >> MP_HALF_DIGIT_BIT); \
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4075
    Pmid = (a  & MP_HALF_DIGIT_MAX) * (a >> MP_HALF_DIGIT_BIT); \
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4076
    Phi += Pmid >> (MP_HALF_DIGIT_BIT - 1);  \
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4077
    Pmid <<= (MP_HALF_DIGIT_BIT + 1);  \
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4078
    Plo += Pmid;  \
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4079
    if (Plo < Pmid)  \
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4080
      ++Phi;  \
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4081
  }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4082
#endif
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4083
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4084
#if !defined(MP_ASSEMBLY_SQUARE)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4085
/* Add the squares of the digits of a to the digits of b. */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4086
void s_mpv_sqr_add_prop(const mp_digit *pa, mp_size a_len, mp_digit *ps)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4087
{
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4088
#if !defined(MP_NO_MP_WORD) && !defined(MP_NO_MUL_WORD)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4089
  mp_word  w;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4090
  mp_digit d;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4091
  mp_size  ix;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4092
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4093
  w  = 0;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4094
#define ADD_SQUARE(n) \
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4095
    d = pa[n]; \
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4096
    w += (d * (mp_word)d) + ps[2*n]; \
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4097
    ps[2*n] = ACCUM(w); \
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4098
    w = (w >> DIGIT_BIT) + ps[2*n+1]; \
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4099
    ps[2*n+1] = ACCUM(w); \
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4100
    w = (w >> DIGIT_BIT)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4101
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4102
  for (ix = a_len; ix >= 4; ix -= 4) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4103
    ADD_SQUARE(0);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4104
    ADD_SQUARE(1);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4105
    ADD_SQUARE(2);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4106
    ADD_SQUARE(3);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4107
    pa += 4;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4108
    ps += 8;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4109
  }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4110
  if (ix) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4111
    ps += 2*ix;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4112
    pa += ix;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4113
    switch (ix) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4114
    case 3: ADD_SQUARE(-3); /* FALLTHRU */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4115
    case 2: ADD_SQUARE(-2); /* FALLTHRU */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4116
    case 1: ADD_SQUARE(-1); /* FALLTHRU */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4117
    case 0: break;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4118
    }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4119
  }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4120
  while (w) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4121
    w += *ps;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4122
    *ps++ = ACCUM(w);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4123
    w = (w >> DIGIT_BIT);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4124
  }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4125
#else
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4126
  mp_digit carry = 0;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4127
  while (a_len--) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4128
    mp_digit a_i = *pa++;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4129
    mp_digit a0a0, a1a1;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4130
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4131
    MP_SQR_D(a_i, a1a1, a0a0);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4132
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4133
    /* here a1a1 and a0a0 constitute a_i ** 2 */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4134
    a0a0 += carry;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4135
    if (a0a0 < carry)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4136
      ++a1a1;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4137
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4138
    /* now add to ps */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4139
    a0a0 += a_i = *ps;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4140
    if (a0a0 < a_i)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4141
      ++a1a1;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4142
    *ps++ = a0a0;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4143
    a1a1 += a_i = *ps;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4144
    carry = (a1a1 < a_i);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4145
    *ps++ = a1a1;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4146
  }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4147
  while (carry) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4148
    mp_digit s_i = *ps;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4149
    carry += s_i;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4150
    *ps++ = carry;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4151
    carry = carry < s_i;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4152
  }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4153
#endif
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4154
}
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4155
#endif
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4156
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4157
#if (defined(MP_NO_MP_WORD) || defined(MP_NO_DIV_WORD)) \
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4158
&& !defined(MP_ASSEMBLY_DIV_2DX1D)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4159
/*
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4160
** Divide 64-bit (Nhi,Nlo) by 32-bit divisor, which must be normalized
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4161
** so its high bit is 1.   This code is from NSPR.
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4162
*/
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4163
mp_err s_mpv_div_2dx1d(mp_digit Nhi, mp_digit Nlo, mp_digit divisor,
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4164
                       mp_digit *qp, mp_digit *rp)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4165
{
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4166
    mp_digit d1, d0, q1, q0;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4167
    mp_digit r1, r0, m;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4168
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4169
    d1 = divisor >> MP_HALF_DIGIT_BIT;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4170
    d0 = divisor & MP_HALF_DIGIT_MAX;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4171
    r1 = Nhi % d1;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4172
    q1 = Nhi / d1;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4173
    m = q1 * d0;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4174
    r1 = (r1 << MP_HALF_DIGIT_BIT) | (Nlo >> MP_HALF_DIGIT_BIT);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4175
    if (r1 < m) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4176
        q1--, r1 += divisor;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4177
        if (r1 >= divisor && r1 < m) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4178
            q1--, r1 += divisor;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4179
        }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4180
    }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4181
    r1 -= m;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4182
    r0 = r1 % d1;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4183
    q0 = r1 / d1;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4184
    m = q0 * d0;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4185
    r0 = (r0 << MP_HALF_DIGIT_BIT) | (Nlo & MP_HALF_DIGIT_MAX);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4186
    if (r0 < m) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4187
        q0--, r0 += divisor;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4188
        if (r0 >= divisor && r0 < m) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4189
            q0--, r0 += divisor;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4190
        }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4191
    }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4192
    if (qp)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4193
        *qp = (q1 << MP_HALF_DIGIT_BIT) | q0;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4194
    if (rp)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4195
        *rp = r0 - m;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4196
    return MP_OKAY;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4197
}
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4198
#endif
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4199
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4200
#if MP_SQUARE
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4201
/* {{{ s_mp_sqr(a) */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4202
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4203
mp_err   s_mp_sqr(mp_int *a)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4204
{
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4205
  mp_err   res;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4206
  mp_int   tmp;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4207
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4208
  if((res = mp_init_size(&tmp, 2 * USED(a), FLAG(a))) != MP_OKAY)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4209
    return res;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4210
  res = mp_sqr(a, &tmp);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4211
  if (res == MP_OKAY) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4212
    s_mp_exch(&tmp, a);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4213
  }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4214
  mp_clear(&tmp);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4215
  return res;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4216
}
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4217
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4218
/* }}} */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4219
#endif
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4220
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4221
/* {{{ s_mp_div(a, b) */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4222
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4223
/*
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4224
  s_mp_div(a, b)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4225
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4226
  Compute a = a / b and b = a mod b.  Assumes b > a.
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4227
 */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4228
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4229
mp_err   s_mp_div(mp_int *rem,  /* i: dividend, o: remainder */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4230
                  mp_int *div,  /* i: divisor                */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4231
                  mp_int *quot) /* i: 0;        o: quotient  */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4232
{
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4233
  mp_int   part, t;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4234
#if !defined(MP_NO_MP_WORD) && !defined(MP_NO_DIV_WORD)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4235
  mp_word  q_msd;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4236
#else
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4237
  mp_digit q_msd;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4238
#endif
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4239
  mp_err   res;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4240
  mp_digit d;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4241
  mp_digit div_msd;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4242
  int      ix;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4243
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4244
  if(mp_cmp_z(div) == 0)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4245
    return MP_RANGE;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4246
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4247
  /* Shortcut if divisor is power of two */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4248
  if((ix = s_mp_ispow2(div)) >= 0) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4249
    MP_CHECKOK( mp_copy(rem, quot) );
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4250
    s_mp_div_2d(quot, (mp_digit)ix);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4251
    s_mp_mod_2d(rem,  (mp_digit)ix);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4252
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4253
    return MP_OKAY;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4254
  }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4255
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4256
  DIGITS(&t) = 0;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4257
  MP_SIGN(rem) = ZPOS;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4258
  MP_SIGN(div) = ZPOS;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4259
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4260
  /* A working temporary for division     */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4261
  MP_CHECKOK( mp_init_size(&t, MP_ALLOC(rem), FLAG(rem)));
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4262
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4263
  /* Normalize to optimize guessing       */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4264
  MP_CHECKOK( s_mp_norm(rem, div, &d) );
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4265
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4266
  part = *rem;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4267
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4268
  /* Perform the division itself...woo!   */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4269
  MP_USED(quot) = MP_ALLOC(quot);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4270
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4271
  /* Find a partial substring of rem which is at least div */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4272
  /* If we didn't find one, we're finished dividing    */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4273
  while (MP_USED(rem) > MP_USED(div) || s_mp_cmp(rem, div) >= 0) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4274
    int i;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4275
    int unusedRem;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4276
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4277
    unusedRem = MP_USED(rem) - MP_USED(div);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4278
    MP_DIGITS(&part) = MP_DIGITS(rem) + unusedRem;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4279
    MP_ALLOC(&part)  = MP_ALLOC(rem)  - unusedRem;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4280
    MP_USED(&part)   = MP_USED(div);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4281
    if (s_mp_cmp(&part, div) < 0) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4282
      -- unusedRem;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4283
#if MP_ARGCHK == 2
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4284
      assert(unusedRem >= 0);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4285
#endif
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4286
      -- MP_DIGITS(&part);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4287
      ++ MP_USED(&part);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4288
      ++ MP_ALLOC(&part);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4289
    }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4290
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4291
    /* Compute a guess for the next quotient digit       */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4292
    q_msd = MP_DIGIT(&part, MP_USED(&part) - 1);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4293
    div_msd = MP_DIGIT(div, MP_USED(div) - 1);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4294
    if (q_msd >= div_msd) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4295
      q_msd = 1;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4296
    } else if (MP_USED(&part) > 1) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4297
#if !defined(MP_NO_MP_WORD) && !defined(MP_NO_DIV_WORD)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4298
      q_msd = (q_msd << MP_DIGIT_BIT) | MP_DIGIT(&part, MP_USED(&part) - 2);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4299
      q_msd /= div_msd;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4300
      if (q_msd == RADIX)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4301
        --q_msd;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4302
#else
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4303
      mp_digit r;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4304
      MP_CHECKOK( s_mpv_div_2dx1d(q_msd, MP_DIGIT(&part, MP_USED(&part) - 2),
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4305
                                  div_msd, &q_msd, &r) );
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4306
#endif
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4307
    } else {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4308
      q_msd = 0;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4309
    }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4310
#if MP_ARGCHK == 2
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4311
    assert(q_msd > 0); /* This case should never occur any more. */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4312
#endif
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4313
    if (q_msd <= 0)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4314
      break;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4315
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4316
    /* See what that multiplies out to                   */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4317
    mp_copy(div, &t);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4318
    MP_CHECKOK( s_mp_mul_d(&t, (mp_digit)q_msd) );
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4319
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4320
    /*
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4321
       If it's too big, back it off.  We should not have to do this
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4322
       more than once, or, in rare cases, twice.  Knuth describes a
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4323
       method by which this could be reduced to a maximum of once, but
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4324
       I didn't implement that here.
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4325
     * When using s_mpv_div_2dx1d, we may have to do this 3 times.
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4326
     */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4327
    for (i = 4; s_mp_cmp(&t, &part) > 0 && i > 0; --i) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4328
      --q_msd;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4329
      s_mp_sub(&t, div);        /* t -= div */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4330
    }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4331
    if (i < 0) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4332
      res = MP_RANGE;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4333
      goto CLEANUP;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4334
    }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4335
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4336
    /* At this point, q_msd should be the right next digit   */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4337
    MP_CHECKOK( s_mp_sub(&part, &t) );  /* part -= t */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4338
    s_mp_clamp(rem);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4339
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4340
    /*
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4341
      Include the digit in the quotient.  We allocated enough memory
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4342
      for any quotient we could ever possibly get, so we should not
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4343
      have to check for failures here
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4344
     */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4345
    MP_DIGIT(quot, unusedRem) = (mp_digit)q_msd;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4346
  }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4347
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4348
  /* Denormalize remainder                */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4349
  if (d) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4350
    s_mp_div_2d(rem, d);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4351
  }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4352
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4353
  s_mp_clamp(quot);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4354
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4355
CLEANUP:
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4356
  mp_clear(&t);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4357
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4358
  return res;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4359
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4360
} /* end s_mp_div() */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4361
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4362
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4363
/* }}} */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4364
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4365
/* {{{ s_mp_2expt(a, k) */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4366
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4367
mp_err   s_mp_2expt(mp_int *a, mp_digit k)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4368
{
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4369
  mp_err    res;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4370
  mp_size   dig, bit;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4371
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4372
  dig = k / DIGIT_BIT;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4373
  bit = k % DIGIT_BIT;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4374
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4375
  mp_zero(a);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4376
  if((res = s_mp_pad(a, dig + 1)) != MP_OKAY)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4377
    return res;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4378
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4379
  DIGIT(a, dig) |= ((mp_digit)1 << bit);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4380
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4381
  return MP_OKAY;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4382
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4383
} /* end s_mp_2expt() */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4384
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4385
/* }}} */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4386
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4387
/* {{{ s_mp_reduce(x, m, mu) */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4388
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4389
/*
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4390
  Compute Barrett reduction, x (mod m), given a precomputed value for
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4391
  mu = b^2k / m, where b = RADIX and k = #digits(m).  This should be
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4392
  faster than straight division, when many reductions by the same
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4393
  value of m are required (such as in modular exponentiation).  This
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4394
  can nearly halve the time required to do modular exponentiation,
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4395
  as compared to using the full integer divide to reduce.
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4396
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4397
  This algorithm was derived from the _Handbook of Applied
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4398
  Cryptography_ by Menezes, Oorschot and VanStone, Ch. 14,
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4399
  pp. 603-604.
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4400
 */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4401
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4402
mp_err   s_mp_reduce(mp_int *x, const mp_int *m, const mp_int *mu)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4403
{
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4404
  mp_int   q;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4405
  mp_err   res;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4406
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4407
  if((res = mp_init_copy(&q, x)) != MP_OKAY)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4408
    return res;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4409
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4410
  s_mp_rshd(&q, USED(m) - 1);  /* q1 = x / b^(k-1)  */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4411
  s_mp_mul(&q, mu);            /* q2 = q1 * mu      */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4412
  s_mp_rshd(&q, USED(m) + 1);  /* q3 = q2 / b^(k+1) */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4413
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4414
  /* x = x mod b^(k+1), quick (no division) */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4415
  s_mp_mod_2d(x, DIGIT_BIT * (USED(m) + 1));
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4416
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4417
  /* q = q * m mod b^(k+1), quick (no division) */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4418
  s_mp_mul(&q, m);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4419
  s_mp_mod_2d(&q, DIGIT_BIT * (USED(m) + 1));
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4420
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4421
  /* x = x - q */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4422
  if((res = mp_sub(x, &q, x)) != MP_OKAY)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4423
    goto CLEANUP;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4424
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4425
  /* If x < 0, add b^(k+1) to it */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4426
  if(mp_cmp_z(x) < 0) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4427
    mp_set(&q, 1);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4428
    if((res = s_mp_lshd(&q, USED(m) + 1)) != MP_OKAY)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4429
      goto CLEANUP;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4430
    if((res = mp_add(x, &q, x)) != MP_OKAY)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4431
      goto CLEANUP;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4432
  }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4433
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4434
  /* Back off if it's too big */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4435
  while(mp_cmp(x, m) >= 0) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4436
    if((res = s_mp_sub(x, m)) != MP_OKAY)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4437
      break;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4438
  }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4439
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4440
 CLEANUP:
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4441
  mp_clear(&q);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4442
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4443
  return res;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4444
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4445
} /* end s_mp_reduce() */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4446
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4447
/* }}} */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4448
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4449
/* }}} */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4450
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4451
/* {{{ Primitive comparisons */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4452
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4453
/* {{{ s_mp_cmp(a, b) */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4454
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4455
/* Compare |a| <=> |b|, return 0 if equal, <0 if a<b, >0 if a>b           */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4456
int      s_mp_cmp(const mp_int *a, const mp_int *b)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4457
{
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4458
  mp_size used_a = MP_USED(a);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4459
  {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4460
    mp_size used_b = MP_USED(b);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4461
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4462
    if (used_a > used_b)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4463
      goto IS_GT;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4464
    if (used_a < used_b)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4465
      goto IS_LT;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4466
  }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4467
  {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4468
    mp_digit *pa, *pb;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4469
    mp_digit da = 0, db = 0;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4470
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4471
#define CMP_AB(n) if ((da = pa[n]) != (db = pb[n])) goto done
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4472
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4473
    pa = MP_DIGITS(a) + used_a;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4474
    pb = MP_DIGITS(b) + used_a;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4475
    while (used_a >= 4) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4476
      pa     -= 4;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4477
      pb     -= 4;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4478
      used_a -= 4;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4479
      CMP_AB(3);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4480
      CMP_AB(2);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4481
      CMP_AB(1);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4482
      CMP_AB(0);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4483
    }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4484
    while (used_a-- > 0 && ((da = *--pa) == (db = *--pb)))
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4485
      /* do nothing */;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4486
done:
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4487
    if (da > db)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4488
      goto IS_GT;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4489
    if (da < db)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4490
      goto IS_LT;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4491
  }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4492
  return MP_EQ;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4493
IS_LT:
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4494
  return MP_LT;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4495
IS_GT:
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4496
  return MP_GT;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4497
} /* end s_mp_cmp() */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4498
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4499
/* }}} */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4500
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4501
/* {{{ s_mp_cmp_d(a, d) */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4502
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4503
/* Compare |a| <=> d, return 0 if equal, <0 if a<d, >0 if a>d             */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4504
int      s_mp_cmp_d(const mp_int *a, mp_digit d)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4505
{
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4506
  if(USED(a) > 1)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4507
    return MP_GT;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4508
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4509
  if(DIGIT(a, 0) < d)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4510
    return MP_LT;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4511
  else if(DIGIT(a, 0) > d)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4512
    return MP_GT;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4513
  else
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4514
    return MP_EQ;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4515
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4516
} /* end s_mp_cmp_d() */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4517
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4518
/* }}} */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4519
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4520
/* {{{ s_mp_ispow2(v) */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4521
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4522
/*
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4523
  Returns -1 if the value is not a power of two; otherwise, it returns
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4524
  k such that v = 2^k, i.e. lg(v).
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4525
 */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4526
int      s_mp_ispow2(const mp_int *v)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4527
{
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4528
  mp_digit d;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4529
  int      extra = 0, ix;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4530
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4531
  ix = MP_USED(v) - 1;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4532
  d = MP_DIGIT(v, ix); /* most significant digit of v */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4533
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4534
  extra = s_mp_ispow2d(d);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4535
  if (extra < 0 || ix == 0)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4536
    return extra;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4537
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4538
  while (--ix >= 0) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4539
    if (DIGIT(v, ix) != 0)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4540
      return -1; /* not a power of two */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4541
    extra += MP_DIGIT_BIT;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4542
  }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4543
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4544
  return extra;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4545
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4546
} /* end s_mp_ispow2() */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4547
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4548
/* }}} */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4549
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4550
/* {{{ s_mp_ispow2d(d) */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4551
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4552
int      s_mp_ispow2d(mp_digit d)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4553
{
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4554
  if ((d != 0) && ((d & (d-1)) == 0)) { /* d is a power of 2 */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4555
    int pow = 0;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4556
#if defined (MP_USE_UINT_DIGIT)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4557
    if (d & 0xffff0000U)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4558
      pow += 16;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4559
    if (d & 0xff00ff00U)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4560
      pow += 8;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4561
    if (d & 0xf0f0f0f0U)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4562
      pow += 4;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4563
    if (d & 0xccccccccU)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4564
      pow += 2;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4565
    if (d & 0xaaaaaaaaU)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4566
      pow += 1;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4567
#elif defined(MP_USE_LONG_LONG_DIGIT)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4568
    if (d & 0xffffffff00000000ULL)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4569
      pow += 32;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4570
    if (d & 0xffff0000ffff0000ULL)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4571
      pow += 16;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4572
    if (d & 0xff00ff00ff00ff00ULL)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4573
      pow += 8;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4574
    if (d & 0xf0f0f0f0f0f0f0f0ULL)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4575
      pow += 4;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4576
    if (d & 0xccccccccccccccccULL)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4577
      pow += 2;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4578
    if (d & 0xaaaaaaaaaaaaaaaaULL)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4579
      pow += 1;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4580
#elif defined(MP_USE_LONG_DIGIT)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4581
    if (d & 0xffffffff00000000UL)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4582
      pow += 32;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4583
    if (d & 0xffff0000ffff0000UL)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4584
      pow += 16;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4585
    if (d & 0xff00ff00ff00ff00UL)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4586
      pow += 8;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4587
    if (d & 0xf0f0f0f0f0f0f0f0UL)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4588
      pow += 4;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4589
    if (d & 0xccccccccccccccccUL)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4590
      pow += 2;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4591
    if (d & 0xaaaaaaaaaaaaaaaaUL)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4592
      pow += 1;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4593
#else
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4594
#error "unknown type for mp_digit"
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4595
#endif
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4596
    return pow;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4597
  }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4598
  return -1;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4599
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4600
} /* end s_mp_ispow2d() */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4601
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4602
/* }}} */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4603
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4604
/* }}} */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4605
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4606
/* {{{ Primitive I/O helpers */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4607
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4608
/* {{{ s_mp_tovalue(ch, r) */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4609
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4610
/*
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4611
  Convert the given character to its digit value, in the given radix.
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4612
  If the given character is not understood in the given radix, -1 is
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4613
  returned.  Otherwise the digit's numeric value is returned.
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4614
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4615
  The results will be odd if you use a radix < 2 or > 62, you are
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4616
  expected to know what you're up to.
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4617
 */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4618
int      s_mp_tovalue(char ch, int r)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4619
{
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4620
  int    val, xch;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4621
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4622
  if(r > 36)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4623
    xch = ch;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4624
  else
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4625
    xch = toupper(ch);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4626
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4627
  if(isdigit(xch))
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4628
    val = xch - '0';
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4629
  else if(isupper(xch))
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4630
    val = xch - 'A' + 10;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4631
  else if(islower(xch))
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4632
    val = xch - 'a' + 36;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4633
  else if(xch == '+')
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4634
    val = 62;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4635
  else if(xch == '/')
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4636
    val = 63;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4637
  else
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4638
    return -1;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4639
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4640
  if(val < 0 || val >= r)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4641
    return -1;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4642
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4643
  return val;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4644
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4645
} /* end s_mp_tovalue() */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4646
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4647
/* }}} */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4648
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4649
/* {{{ s_mp_todigit(val, r, low) */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4650
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4651
/*
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4652
  Convert val to a radix-r digit, if possible.  If val is out of range
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4653
  for r, returns zero.  Otherwise, returns an ASCII character denoting
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4654
  the value in the given radix.
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4655
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4656
  The results may be odd if you use a radix < 2 or > 64, you are
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4657
  expected to know what you're doing.
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4658
 */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4659
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4660
char     s_mp_todigit(mp_digit val, int r, int low)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4661
{
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4662
  char   ch;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4663
8175
27e80dc06104 6989705: ECC security code native code compiler warnings
vinnie
parents: 5506
diff changeset
  4664
  if(val >= (unsigned int)r)
3863
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4665
    return 0;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4666
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4667
  ch = s_dmap_1[val];
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4668
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4669
  if(r <= 36 && low)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4670
    ch = tolower(ch);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4671
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4672
  return ch;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4673
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4674
} /* end s_mp_todigit() */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4675
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4676
/* }}} */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4677
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4678
/* {{{ s_mp_outlen(bits, radix) */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4679
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4680
/*
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4681
   Return an estimate for how long a string is needed to hold a radix
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4682
   r representation of a number with 'bits' significant bits, plus an
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4683
   extra for a zero terminator (assuming C style strings here)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4684
 */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4685
int      s_mp_outlen(int bits, int r)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4686
{
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4687
  return (int)((double)bits * LOG_V_2(r) + 1.5) + 1;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4688
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4689
} /* end s_mp_outlen() */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4690
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4691
/* }}} */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4692
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4693
/* }}} */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4694
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4695
/* {{{ mp_read_unsigned_octets(mp, str, len) */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4696
/* mp_read_unsigned_octets(mp, str, len)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4697
   Read in a raw value (base 256) into the given mp_int
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4698
   No sign bit, number is positive.  Leading zeros ignored.
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4699
 */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4700
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4701
mp_err
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4702
mp_read_unsigned_octets(mp_int *mp, const unsigned char *str, mp_size len)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4703
{
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4704
  int            count;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4705
  mp_err         res;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4706
  mp_digit       d;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4707
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4708
  ARGCHK(mp != NULL && str != NULL && len > 0, MP_BADARG);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4709
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4710
  mp_zero(mp);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4711
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4712
  count = len % sizeof(mp_digit);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4713
  if (count) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4714
    for (d = 0; count-- > 0; --len) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4715
      d = (d << 8) | *str++;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4716
    }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4717
    MP_DIGIT(mp, 0) = d;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4718
  }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4719
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4720
  /* Read the rest of the digits */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4721
  for(; len > 0; len -= sizeof(mp_digit)) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4722
    for (d = 0, count = sizeof(mp_digit); count > 0; --count) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4723
      d = (d << 8) | *str++;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4724
    }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4725
    if (MP_EQ == mp_cmp_z(mp)) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4726
      if (!d)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4727
        continue;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4728
    } else {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4729
      if((res = s_mp_lshd(mp, 1)) != MP_OKAY)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4730
        return res;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4731
    }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4732
    MP_DIGIT(mp, 0) = d;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4733
  }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4734
  return MP_OKAY;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4735
} /* end mp_read_unsigned_octets() */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4736
/* }}} */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4737
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4738
/* {{{ mp_unsigned_octet_size(mp) */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4739
int
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4740
mp_unsigned_octet_size(const mp_int *mp)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4741
{
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4742
  int  bytes;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4743
  int  ix;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4744
  mp_digit  d = 0;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4745
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4746
  ARGCHK(mp != NULL, MP_BADARG);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4747
  ARGCHK(MP_ZPOS == SIGN(mp), MP_BADARG);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4748
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4749
  bytes = (USED(mp) * sizeof(mp_digit));
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4750
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4751
  /* subtract leading zeros. */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4752
  /* Iterate over each digit... */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4753
  for(ix = USED(mp) - 1; ix >= 0; ix--) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4754
    d = DIGIT(mp, ix);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4755
    if (d)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4756
        break;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4757
    bytes -= sizeof(d);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4758
  }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4759
  if (!bytes)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4760
    return 1;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4761
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4762
  /* Have MSD, check digit bytes, high order first */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4763
  for(ix = sizeof(mp_digit) - 1; ix >= 0; ix--) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4764
    unsigned char x = (unsigned char)(d >> (ix * CHAR_BIT));
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4765
    if (x)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4766
        break;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4767
    --bytes;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4768
  }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4769
  return bytes;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4770
} /* end mp_unsigned_octet_size() */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4771
/* }}} */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4772
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4773
/* {{{ mp_to_unsigned_octets(mp, str) */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4774
/* output a buffer of big endian octets no longer than specified. */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4775
mp_err
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4776
mp_to_unsigned_octets(const mp_int *mp, unsigned char *str, mp_size maxlen)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4777
{
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4778
  int  ix, pos = 0;
8175
27e80dc06104 6989705: ECC security code native code compiler warnings
vinnie
parents: 5506
diff changeset
  4779
  unsigned int  bytes;
3863
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4780
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4781
  ARGCHK(mp != NULL && str != NULL && !SIGN(mp), MP_BADARG);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4782
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4783
  bytes = mp_unsigned_octet_size(mp);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4784
  ARGCHK(bytes <= maxlen, MP_BADARG);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4785
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4786
  /* Iterate over each digit... */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4787
  for(ix = USED(mp) - 1; ix >= 0; ix--) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4788
    mp_digit  d = DIGIT(mp, ix);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4789
    int       jx;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4790
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4791
    /* Unpack digit bytes, high order first */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4792
    for(jx = sizeof(mp_digit) - 1; jx >= 0; jx--) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4793
      unsigned char x = (unsigned char)(d >> (jx * CHAR_BIT));
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4794
      if (!pos && !x)   /* suppress leading zeros */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4795
        continue;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4796
      str[pos++] = x;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4797
    }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4798
  }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4799
  if (!pos)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4800
    str[pos++] = 0;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4801
  return pos;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4802
} /* end mp_to_unsigned_octets() */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4803
/* }}} */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4804
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4805
/* {{{ mp_to_signed_octets(mp, str) */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4806
/* output a buffer of big endian octets no longer than specified. */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4807
mp_err
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4808
mp_to_signed_octets(const mp_int *mp, unsigned char *str, mp_size maxlen)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4809
{
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4810
  int  ix, pos = 0;
8175
27e80dc06104 6989705: ECC security code native code compiler warnings
vinnie
parents: 5506
diff changeset
  4811
  unsigned int  bytes;
3863
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4812
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4813
  ARGCHK(mp != NULL && str != NULL && !SIGN(mp), MP_BADARG);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4814
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4815
  bytes = mp_unsigned_octet_size(mp);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4816
  ARGCHK(bytes <= maxlen, MP_BADARG);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4817
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4818
  /* Iterate over each digit... */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4819
  for(ix = USED(mp) - 1; ix >= 0; ix--) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4820
    mp_digit  d = DIGIT(mp, ix);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4821
    int       jx;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4822
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4823
    /* Unpack digit bytes, high order first */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4824
    for(jx = sizeof(mp_digit) - 1; jx >= 0; jx--) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4825
      unsigned char x = (unsigned char)(d >> (jx * CHAR_BIT));
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4826
      if (!pos) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4827
        if (!x)         /* suppress leading zeros */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4828
          continue;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4829
        if (x & 0x80) { /* add one leading zero to make output positive.  */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4830
          ARGCHK(bytes + 1 <= maxlen, MP_BADARG);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4831
          if (bytes + 1 > maxlen)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4832
            return MP_BADARG;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4833
          str[pos++] = 0;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4834
        }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4835
      }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4836
      str[pos++] = x;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4837
    }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4838
  }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4839
  if (!pos)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4840
    str[pos++] = 0;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4841
  return pos;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4842
} /* end mp_to_signed_octets() */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4843
/* }}} */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4844
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4845
/* {{{ mp_to_fixlen_octets(mp, str) */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4846
/* output a buffer of big endian octets exactly as long as requested. */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4847
mp_err
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4848
mp_to_fixlen_octets(const mp_int *mp, unsigned char *str, mp_size length)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4849
{
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4850
  int  ix, pos = 0;
8175
27e80dc06104 6989705: ECC security code native code compiler warnings
vinnie
parents: 5506
diff changeset
  4851
  unsigned int  bytes;
3863
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4852
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4853
  ARGCHK(mp != NULL && str != NULL && !SIGN(mp), MP_BADARG);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4854
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4855
  bytes = mp_unsigned_octet_size(mp);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4856
  ARGCHK(bytes <= length, MP_BADARG);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4857
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4858
  /* place any needed leading zeros */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4859
  for (;length > bytes; --length) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4860
        *str++ = 0;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4861
  }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4862
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4863
  /* Iterate over each digit... */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4864
  for(ix = USED(mp) - 1; ix >= 0; ix--) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4865
    mp_digit  d = DIGIT(mp, ix);
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4866
    int       jx;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4867
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4868
    /* Unpack digit bytes, high order first */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4869
    for(jx = sizeof(mp_digit) - 1; jx >= 0; jx--) {
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4870
      unsigned char x = (unsigned char)(d >> (jx * CHAR_BIT));
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4871
      if (!pos && !x)   /* suppress leading zeros */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4872
        continue;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4873
      str[pos++] = x;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4874
    }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4875
  }
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4876
  if (!pos)
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4877
    str[pos++] = 0;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4878
  return MP_OKAY;
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4879
} /* end mp_to_fixlen_octets() */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4880
/* }}} */
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4881
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4882
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4883
/*------------------------------------------------------------------------*/
8e0f58b1c072 6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff changeset
  4884
/* HERE THERE BE DRAGONS                                                  */