author | vinnie |
Mon, 30 May 2011 16:37:42 +0100 | |
changeset 9774 | 50a2b28ca54c |
parent 9035 | 1255eb81cc2f |
child 24861 | 3377edc1a02d |
permissions | -rw-r--r-- |
9774
50a2b28ca54c
7049173: Replace the software license for ECC native code
vinnie
parents:
9035
diff
changeset
|
1 |
/* |
50a2b28ca54c
7049173: Replace the software license for ECC native code
vinnie
parents:
9035
diff
changeset
|
2 |
* Copyright (c) 2007, 2011, Oracle and/or its affiliates. All rights reserved. |
50a2b28ca54c
7049173: Replace the software license for ECC native code
vinnie
parents:
9035
diff
changeset
|
3 |
* Use is subject to license terms. |
50a2b28ca54c
7049173: Replace the software license for ECC native code
vinnie
parents:
9035
diff
changeset
|
4 |
* |
50a2b28ca54c
7049173: Replace the software license for ECC native code
vinnie
parents:
9035
diff
changeset
|
5 |
* This library is free software; you can redistribute it and/or |
50a2b28ca54c
7049173: Replace the software license for ECC native code
vinnie
parents:
9035
diff
changeset
|
6 |
* modify it under the terms of the GNU Lesser General Public |
50a2b28ca54c
7049173: Replace the software license for ECC native code
vinnie
parents:
9035
diff
changeset
|
7 |
* License as published by the Free Software Foundation; either |
50a2b28ca54c
7049173: Replace the software license for ECC native code
vinnie
parents:
9035
diff
changeset
|
8 |
* version 2.1 of the License, or (at your option) any later version. |
3863
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
9 |
* |
9774
50a2b28ca54c
7049173: Replace the software license for ECC native code
vinnie
parents:
9035
diff
changeset
|
10 |
* This library is distributed in the hope that it will be useful, |
50a2b28ca54c
7049173: Replace the software license for ECC native code
vinnie
parents:
9035
diff
changeset
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
50a2b28ca54c
7049173: Replace the software license for ECC native code
vinnie
parents:
9035
diff
changeset
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
50a2b28ca54c
7049173: Replace the software license for ECC native code
vinnie
parents:
9035
diff
changeset
|
13 |
* Lesser General Public License for more details. |
3863
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
14 |
* |
9774
50a2b28ca54c
7049173: Replace the software license for ECC native code
vinnie
parents:
9035
diff
changeset
|
15 |
* You should have received a copy of the GNU Lesser General Public License |
50a2b28ca54c
7049173: Replace the software license for ECC native code
vinnie
parents:
9035
diff
changeset
|
16 |
* along with this library; if not, write to the Free Software Foundation, |
50a2b28ca54c
7049173: Replace the software license for ECC native code
vinnie
parents:
9035
diff
changeset
|
17 |
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
3863
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
18 |
* |
9774
50a2b28ca54c
7049173: Replace the software license for ECC native code
vinnie
parents:
9035
diff
changeset
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
50a2b28ca54c
7049173: Replace the software license for ECC native code
vinnie
parents:
9035
diff
changeset
|
20 |
* or visit www.oracle.com if you need additional information or have any |
50a2b28ca54c
7049173: Replace the software license for ECC native code
vinnie
parents:
9035
diff
changeset
|
21 |
* questions. |
50a2b28ca54c
7049173: Replace the software license for ECC native code
vinnie
parents:
9035
diff
changeset
|
22 |
*/ |
50a2b28ca54c
7049173: Replace the software license for ECC native code
vinnie
parents:
9035
diff
changeset
|
23 |
|
50a2b28ca54c
7049173: Replace the software license for ECC native code
vinnie
parents:
9035
diff
changeset
|
24 |
/* ********************************************************************* |
3863
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
25 |
* |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
26 |
* 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
|
27 |
* |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
28 |
* 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
|
29 |
* Michael J. Fromberger. |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
30 |
* 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
|
31 |
* 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
|
32 |
* |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
33 |
* Contributor(s): |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
34 |
* Netscape Communications Corporation |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
35 |
* 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
|
36 |
* |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
37 |
*********************************************************************** */ |
9774
50a2b28ca54c
7049173: Replace the software license for ECC native code
vinnie
parents:
9035
diff
changeset
|
38 |
|
50a2b28ca54c
7049173: Replace the software license for ECC native code
vinnie
parents:
9035
diff
changeset
|
39 |
/* Arbitrary precision integer arithmetic library */ |
3863
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
40 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
41 |
#include "mpi-priv.h" |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
42 |
#if defined(OSF1) |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
43 |
#include <c_asm.h> |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
44 |
#endif |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
45 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
46 |
#if MP_LOGTAB |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
47 |
/* |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
48 |
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
|
49 |
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
|
50 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
51 |
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
|
52 |
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
|
53 |
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
|
54 |
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
|
55 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
56 |
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
|
57 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
58 |
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
|
59 |
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
|
60 |
*/ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
61 |
#include "logtab.h" |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
62 |
#endif |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
63 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
64 |
/* {{{ Constant strings */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
65 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
66 |
/* 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
|
67 |
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
|
68 |
"unknown result code", /* say what? */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
69 |
"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
|
70 |
"boolean false", /* MP_NO */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
71 |
"out of memory", /* MP_MEM */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
72 |
"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
|
73 |
"invalid input parameter", /* MP_BADARG */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
74 |
"result is undefined" /* MP_UNDEF */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
75 |
}; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
76 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
77 |
/* 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
|
78 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
79 |
/* 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
|
80 |
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
|
81 |
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz+/"; |
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 |
/* }}} */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
84 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
85 |
unsigned long mp_allocs; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
86 |
unsigned long mp_frees; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
87 |
unsigned long mp_copies; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
88 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
89 |
/* {{{ Default precision manipulation */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
90 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
91 |
/* 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
|
92 |
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
|
93 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
94 |
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
|
95 |
{ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
96 |
return s_mp_defprec; |
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 |
} /* end mp_get_prec() */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
99 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
100 |
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
|
101 |
{ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
102 |
if(prec == 0) |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
103 |
s_mp_defprec = MP_DEFPREC; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
104 |
else |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
105 |
s_mp_defprec = prec; |
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 |
} /* end mp_set_prec() */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
108 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
109 |
/* }}} */ |
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 |
/*------------------------------------------------------------------------*/ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
112 |
/* {{{ mp_init(mp, kmflag) */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
113 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
114 |
/* |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
115 |
mp_init(mp, kmflag) |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
116 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
117 |
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
|
118 |
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
|
119 |
*/ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
120 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
121 |
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
|
122 |
{ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
123 |
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
|
124 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
125 |
} /* end mp_init() */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
126 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
127 |
/* }}} */ |
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 |
/* {{{ 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
|
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 |
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
|
133 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
134 |
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
|
135 |
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
|
136 |
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
|
137 |
*/ |
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 |
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
|
140 |
{ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
141 |
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
|
142 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
143 |
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
|
144 |
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
|
145 |
return MP_MEM; |
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 |
SIGN(mp) = ZPOS; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
148 |
USED(mp) = 1; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
149 |
ALLOC(mp) = prec; |
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 |
return MP_OKAY; |
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 |
} /* end mp_init_size() */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
154 |
|
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 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
157 |
/* {{{ mp_init_copy(mp, from) */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
158 |
|
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 |
mp_init_copy(mp, from) |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
161 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
162 |
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
|
163 |
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
|
164 |
structure. |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
165 |
*/ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
166 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
167 |
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
|
168 |
{ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
169 |
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
|
170 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
171 |
if(mp == from) |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
172 |
return MP_OKAY; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
173 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
174 |
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
|
175 |
return MP_MEM; |
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 |
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
|
178 |
USED(mp) = USED(from); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
179 |
ALLOC(mp) = ALLOC(from); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
180 |
SIGN(mp) = SIGN(from); |
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 |
#ifndef _WIN32 |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
183 |
FLAG(mp) = FLAG(from); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
184 |
#endif /* _WIN32 */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
185 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
186 |
return MP_OKAY; |
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 |
} /* end mp_init_copy() */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
189 |
|
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 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
192 |
/* {{{ mp_copy(from, to) */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
193 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
194 |
/* |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
195 |
mp_copy(from, to) |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
196 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
197 |
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
|
198 |
'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
|
199 |
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
|
200 |
*/ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
201 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
202 |
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
|
203 |
{ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
204 |
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
|
205 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
206 |
if(from == to) |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
207 |
return MP_OKAY; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
208 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
209 |
++mp_copies; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
210 |
{ /* copy */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
211 |
mp_digit *tmp; |
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 |
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
|
215 |
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
|
216 |
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
|
217 |
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
|
218 |
usual |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
219 |
*/ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
220 |
if(ALLOC(to) >= USED(from)) { |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
221 |
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
|
222 |
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
|
223 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
224 |
} else { |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
225 |
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
|
226 |
return MP_MEM; |
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 |
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
|
229 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
230 |
if(DIGITS(to) != NULL) { |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
231 |
#if MP_CRYPTO |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
232 |
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
|
233 |
#endif |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
234 |
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
|
235 |
} |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
236 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
237 |
DIGITS(to) = tmp; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
238 |
ALLOC(to) = ALLOC(from); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
239 |
} |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
240 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
241 |
/* 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
|
242 |
USED(to) = USED(from); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
243 |
SIGN(to) = SIGN(from); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
244 |
} /* end copy */ |
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 |
return MP_OKAY; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
247 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
248 |
} /* end mp_copy() */ |
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 |
/* }}} */ |
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 |
/* {{{ mp_exch(mp1, mp2) */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
253 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
254 |
/* |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
255 |
mp_exch(mp1, mp2) |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
256 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
257 |
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
|
258 |
(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
|
259 |
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
|
260 |
*/ |
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 |
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
|
263 |
{ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
264 |
#if MP_ARGCHK == 2 |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
265 |
assert(mp1 != NULL && mp2 != NULL); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
266 |
#else |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
267 |
if(mp1 == NULL || mp2 == NULL) |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
268 |
return; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
269 |
#endif |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
270 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
271 |
s_mp_exch(mp1, mp2); |
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 |
} /* end mp_exch() */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
274 |
|
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_clear(mp) */ |
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 |
/* |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
280 |
mp_clear(mp) |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
281 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
282 |
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
|
283 |
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
|
284 |
get tollchocked. |
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 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
287 |
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
|
288 |
{ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
289 |
if(mp == 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 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
292 |
if(DIGITS(mp) != NULL) { |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
293 |
#if MP_CRYPTO |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
294 |
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
|
295 |
#endif |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
296 |
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
|
297 |
DIGITS(mp) = NULL; |
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 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
300 |
USED(mp) = 0; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
301 |
ALLOC(mp) = 0; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
302 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
303 |
} /* end mp_clear() */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
304 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
305 |
/* }}} */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
306 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
307 |
/* {{{ mp_zero(mp) */ |
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 |
/* |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
310 |
mp_zero(mp) |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
311 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
312 |
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
|
313 |
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
|
314 |
*/ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
315 |
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
|
316 |
{ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
317 |
if(mp == NULL) |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
318 |
return; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
319 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
320 |
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
|
321 |
USED(mp) = 1; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
322 |
SIGN(mp) = ZPOS; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
323 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
324 |
} /* end mp_zero() */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
325 |
|
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 |
/* {{{ mp_set(mp, d) */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
329 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
330 |
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
|
331 |
{ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
332 |
if(mp == NULL) |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
333 |
return; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
334 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
335 |
mp_zero(mp); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
336 |
DIGIT(mp, 0) = d; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
337 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
338 |
} /* end mp_set() */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
339 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
340 |
/* }}} */ |
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 |
/* {{{ mp_set_int(mp, z) */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
343 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
344 |
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
|
345 |
{ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
346 |
int ix; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
347 |
unsigned long v = labs(z); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
348 |
mp_err res; |
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 |
ARGCHK(mp != NULL, MP_BADARG); |
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 |
mp_zero(mp); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
353 |
if(z == 0) |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
354 |
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
|
355 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
356 |
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
|
357 |
DIGIT(mp,0) = v; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
358 |
} else { |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
359 |
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
|
360 |
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
|
361 |
return res; |
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 |
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
|
364 |
if (res != MP_OKAY) |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
365 |
return res; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
366 |
} |
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 |
if(z < 0) |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
369 |
SIGN(mp) = NEG; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
370 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
371 |
return MP_OKAY; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
372 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
373 |
} /* end mp_set_int() */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
374 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
375 |
/* }}} */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
376 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
377 |
/* {{{ mp_set_ulong(mp, z) */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
378 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
379 |
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
|
380 |
{ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
381 |
int ix; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
382 |
mp_err res; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
383 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
384 |
ARGCHK(mp != NULL, MP_BADARG); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
385 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
386 |
mp_zero(mp); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
387 |
if(z == 0) |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
388 |
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
|
389 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
390 |
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
|
391 |
DIGIT(mp,0) = z; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
392 |
} else { |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
393 |
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
|
394 |
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
|
395 |
return res; |
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 |
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
|
398 |
if (res != MP_OKAY) |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
399 |
return res; |
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 |
} |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
402 |
return MP_OKAY; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
403 |
} /* end mp_set_ulong() */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
404 |
|
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 |
|
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 |
/* {{{ Digit arithmetic */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
409 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
410 |
/* {{{ 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
|
411 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
412 |
/* |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
413 |
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
|
414 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
415 |
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
|
416 |
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
|
417 |
*/ |
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 |
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
|
420 |
{ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
421 |
mp_int tmp; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
422 |
mp_err res; |
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 |
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
|
425 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
426 |
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
|
427 |
return res; |
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 |
if(SIGN(&tmp) == ZPOS) { |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
430 |
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
|
431 |
goto CLEANUP; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
432 |
} 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
|
433 |
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
|
434 |
goto CLEANUP; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
435 |
} else { |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
436 |
mp_neg(&tmp, &tmp); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
437 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
438 |
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
|
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 |
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
|
442 |
SIGN(&tmp) = ZPOS; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
443 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
444 |
s_mp_exch(&tmp, b); |
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 |
CLEANUP: |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
447 |
mp_clear(&tmp); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
448 |
return res; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
449 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
450 |
} /* end mp_add_d() */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
451 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
452 |
/* }}} */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
453 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
454 |
/* {{{ 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
|
455 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
456 |
/* |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
457 |
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
|
458 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
459 |
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
|
460 |
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
|
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 |
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
|
464 |
{ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
465 |
mp_int tmp; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
466 |
mp_err res; |
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 |
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
|
469 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
470 |
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
|
471 |
return res; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
472 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
473 |
if(SIGN(&tmp) == NEG) { |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
474 |
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
|
475 |
goto CLEANUP; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
476 |
} 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
|
477 |
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
|
478 |
goto CLEANUP; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
479 |
} else { |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
480 |
mp_neg(&tmp, &tmp); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
481 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
482 |
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
|
483 |
SIGN(&tmp) = NEG; |
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 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
486 |
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
|
487 |
SIGN(&tmp) = ZPOS; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
488 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
489 |
s_mp_exch(&tmp, b); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
490 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
491 |
CLEANUP: |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
492 |
mp_clear(&tmp); |
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 |
} /* end mp_sub_d() */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
496 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
497 |
/* }}} */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
498 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
499 |
/* {{{ 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
|
500 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
501 |
/* |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
502 |
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
|
503 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
504 |
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
|
505 |
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
|
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 |
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
|
509 |
{ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
510 |
mp_err res; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
511 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
512 |
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
|
513 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
514 |
if(d == 0) { |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
515 |
mp_zero(b); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
516 |
return MP_OKAY; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
517 |
} |
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 |
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
|
520 |
return res; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
521 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
522 |
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
|
523 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
524 |
return res; |
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 |
} /* end mp_mul_d() */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
527 |
|
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_mul_2(a, c) */ |
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 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
|
533 |
{ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
534 |
mp_err res; |
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 |
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
|
537 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
538 |
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
|
539 |
return res; |
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 |
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
|
542 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
543 |
} /* end mp_mul_2() */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
544 |
|
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 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
547 |
/* {{{ 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
|
548 |
|
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 |
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
|
551 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
552 |
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
|
553 |
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
|
554 |
unsigned anyway). |
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 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
557 |
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
|
558 |
{ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
559 |
mp_err res; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
560 |
mp_int qp; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
561 |
mp_digit rem; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
562 |
int pow; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
563 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
564 |
ARGCHK(a != NULL, MP_BADARG); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
565 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
566 |
if(d == 0) |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
567 |
return MP_RANGE; |
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 |
/* Shortcut for powers of two ... */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
570 |
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
|
571 |
mp_digit mask; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
572 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
573 |
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
|
574 |
rem = DIGIT(a, 0) & mask; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
575 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
576 |
if(q) { |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
577 |
mp_copy(a, q); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
578 |
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
|
579 |
} |
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 |
if(r) |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
582 |
*r = rem; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
583 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
584 |
return MP_OKAY; |
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 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
587 |
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
|
588 |
return res; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
589 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
590 |
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
|
591 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
592 |
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
|
593 |
SIGN(q) = ZPOS; |
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 |
if(r) |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
596 |
*r = rem; |
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 |
s_mp_exch(&qp, q); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
600 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
601 |
mp_clear(&qp); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
602 |
return res; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
603 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
604 |
} /* end mp_div_d() */ |
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 |
/* }}} */ |
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 |
/* {{{ mp_div_2(a, c) */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
609 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
610 |
/* |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
611 |
mp_div_2(a, c) |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
612 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
613 |
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
|
614 |
*/ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
615 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
616 |
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
|
617 |
{ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
618 |
mp_err res; |
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 |
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
|
621 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
622 |
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
|
623 |
return res; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
624 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
625 |
s_mp_div_2(c); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
626 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
627 |
return MP_OKAY; |
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 |
} /* end mp_div_2() */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
630 |
|
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_expt_d(a, d, b) */ |
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 |
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
|
636 |
{ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
637 |
mp_int s, x; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
638 |
mp_err res; |
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 |
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
|
641 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
642 |
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
|
643 |
return res; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
644 |
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
|
645 |
goto X; |
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 |
DIGIT(&s, 0) = 1; |
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 |
while(d != 0) { |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
650 |
if(d & 1) { |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
651 |
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
|
652 |
goto CLEANUP; |
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 |
d /= 2; |
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 |
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
|
658 |
goto CLEANUP; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
659 |
} |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
660 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
661 |
s_mp_exch(&s, c); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
662 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
663 |
CLEANUP: |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
664 |
mp_clear(&x); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
665 |
X: |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
666 |
mp_clear(&s); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
667 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
668 |
return res; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
669 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
670 |
} /* end mp_expt_d() */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
671 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
672 |
/* }}} */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
673 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
674 |
/* }}} */ |
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 |
/* {{{ Full arithmetic */ |
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 |
/* {{{ mp_abs(a, b) */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
680 |
|
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 |
mp_abs(a, b) |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
683 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
684 |
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
|
685 |
*/ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
686 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
687 |
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
|
688 |
{ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
689 |
mp_err res; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
690 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
691 |
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
|
692 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
693 |
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
|
694 |
return res; |
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 |
SIGN(b) = ZPOS; |
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 |
return MP_OKAY; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
699 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
700 |
} /* end mp_abs() */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
701 |
|
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_neg(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 |
/* |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
707 |
mp_neg(a, b) |
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 |
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
|
710 |
*/ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
711 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
712 |
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
|
713 |
{ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
714 |
mp_err res; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
715 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
716 |
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
|
717 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
718 |
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
|
719 |
return res; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
720 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
721 |
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
|
722 |
SIGN(b) = ZPOS; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
723 |
else |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
724 |
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
|
725 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
726 |
return MP_OKAY; |
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 |
} /* end mp_neg() */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
729 |
|
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 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
732 |
/* {{{ mp_add(a, b, c) */ |
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 |
/* |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
735 |
mp_add(a, b, c) |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
736 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
737 |
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
|
738 |
*/ |
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 |
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
|
741 |
{ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
742 |
mp_err res; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
743 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
744 |
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
|
745 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
746 |
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
|
747 |
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
|
748 |
} 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
|
749 |
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
|
750 |
} else { /* different sign: |a| < |b| */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
751 |
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
|
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 |
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
|
755 |
SIGN(c) = ZPOS; |
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 |
CLEANUP: |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
758 |
return res; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
759 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
760 |
} /* end mp_add() */ |
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 |
/* }}} */ |
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_sub(a, b, c) */ |
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 |
/* |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
767 |
mp_sub(a, b, c) |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
768 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
769 |
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
|
770 |
*/ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
771 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
772 |
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
|
773 |
{ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
774 |
mp_err res; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
775 |
int magDiff; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
776 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
777 |
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
|
778 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
779 |
if (a == b) { |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
780 |
mp_zero(c); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
781 |
return MP_OKAY; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
782 |
} |
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 |
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
|
785 |
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
|
786 |
} 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
|
787 |
mp_zero(c); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
788 |
res = MP_OKAY; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
789 |
} else if (magDiff > 0) { |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
790 |
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
|
791 |
} else { |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
792 |
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
|
793 |
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
|
794 |
} |
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 |
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
|
797 |
MP_SIGN(c) = MP_ZPOS; |
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 |
CLEANUP: |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
800 |
return res; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
801 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
802 |
} /* end mp_sub() */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
803 |
|
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 |
/* {{{ mp_mul(a, b, c) */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
807 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
808 |
/* |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
809 |
mp_mul(a, b, c) |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
810 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
811 |
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
|
812 |
*/ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
813 |
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
|
814 |
{ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
815 |
mp_digit *pb; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
816 |
mp_int tmp; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
817 |
mp_err res; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
818 |
mp_size ib; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
819 |
mp_size useda, usedb; |
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 |
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
|
822 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
823 |
if (a == c) { |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
824 |
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
|
825 |
return res; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
826 |
if (a == b) |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
827 |
b = &tmp; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
828 |
a = &tmp; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
829 |
} else if (b == c) { |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
830 |
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
|
831 |
return res; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
832 |
b = &tmp; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
833 |
} else { |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
834 |
MP_DIGITS(&tmp) = 0; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
835 |
} |
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 |
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
|
838 |
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
|
839 |
b = a; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
840 |
a = xch; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
841 |
} |
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 |
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
|
844 |
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
|
845 |
goto CLEANUP; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
846 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
847 |
#ifdef NSS_USE_COMBA |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
848 |
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
|
849 |
if (MP_USED(a) == 4) { |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
850 |
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
|
851 |
goto CLEANUP; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
852 |
} |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
853 |
if (MP_USED(a) == 8) { |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
854 |
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
|
855 |
goto CLEANUP; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
856 |
} |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
857 |
if (MP_USED(a) == 16) { |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
858 |
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
|
859 |
goto CLEANUP; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
860 |
} |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
861 |
if (MP_USED(a) == 32) { |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
862 |
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
|
863 |
goto CLEANUP; |
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 |
} |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
866 |
#endif |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
867 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
868 |
pb = MP_DIGITS(b); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
869 |
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
|
870 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
871 |
/* Outer loop: Digits of b */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
872 |
useda = MP_USED(a); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
873 |
usedb = MP_USED(b); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
874 |
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
|
875 |
mp_digit b_i = *pb++; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
876 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
877 |
/* Inner product: Digits of a */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
878 |
if (b_i) |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
879 |
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
|
880 |
else |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
881 |
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
|
882 |
} |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
883 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
884 |
s_mp_clamp(c); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
885 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
886 |
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
|
887 |
SIGN(c) = ZPOS; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
888 |
else |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
889 |
SIGN(c) = NEG; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
890 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
891 |
CLEANUP: |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
892 |
mp_clear(&tmp); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
893 |
return res; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
894 |
} /* end mp_mul() */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
895 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
896 |
/* }}} */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
897 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
898 |
/* {{{ mp_sqr(a, sqr) */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
899 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
900 |
#if MP_SQUARE |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
901 |
/* |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
902 |
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
|
903 |
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
|
904 |
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
|
905 |
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
|
906 |
iterations of the multiplication loop. |
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 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
909 |
/* 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
|
910 |
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
|
911 |
{ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
912 |
mp_digit *pa; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
913 |
mp_digit d; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
914 |
mp_err res; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
915 |
mp_size ix; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
916 |
mp_int tmp; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
917 |
int count; |
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 |
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
|
920 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
921 |
if (a == sqr) { |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
922 |
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
|
923 |
return res; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
924 |
a = &tmp; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
925 |
} else { |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
926 |
DIGITS(&tmp) = 0; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
927 |
res = MP_OKAY; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
928 |
} |
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 |
ix = 2 * MP_USED(a); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
931 |
if (ix > MP_ALLOC(sqr)) { |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
932 |
MP_USED(sqr) = 1; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
933 |
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
|
934 |
} |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
935 |
MP_USED(sqr) = ix; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
936 |
MP_DIGIT(sqr, 0) = 0; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
937 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
938 |
#ifdef NSS_USE_COMBA |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
939 |
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
|
940 |
if (MP_USED(a) == 4) { |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
941 |
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
|
942 |
goto CLEANUP; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
943 |
} |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
944 |
if (MP_USED(a) == 8) { |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
945 |
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
|
946 |
goto CLEANUP; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
947 |
} |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
948 |
if (MP_USED(a) == 16) { |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
949 |
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
|
950 |
goto CLEANUP; |
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 |
if (MP_USED(a) == 32) { |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
953 |
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
|
954 |
goto CLEANUP; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
955 |
} |
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 |
#endif |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
958 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
959 |
pa = MP_DIGITS(a); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
960 |
count = MP_USED(a) - 1; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
961 |
if (count > 0) { |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
962 |
d = *pa++; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
963 |
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
|
964 |
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
|
965 |
d = *pa++; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
966 |
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
|
967 |
} /* for(ix ...) */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
968 |
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
|
969 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
970 |
/* now sqr *= 2 */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
971 |
s_mp_mul_2(sqr); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
972 |
} else { |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
973 |
MP_DIGIT(sqr, 1) = 0; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
974 |
} |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
975 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
976 |
/* 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
|
977 |
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
|
978 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
979 |
SIGN(sqr) = ZPOS; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
980 |
s_mp_clamp(sqr); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
981 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
982 |
CLEANUP: |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
983 |
mp_clear(&tmp); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
984 |
return res; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
985 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
986 |
} /* end mp_sqr() */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
987 |
#endif |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
988 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
989 |
/* }}} */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
990 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
991 |
/* {{{ 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
|
992 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
993 |
/* |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
994 |
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
|
995 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
996 |
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
|
997 |
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
|
998 |
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
|
999 |
*/ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1000 |
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
|
1001 |
{ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1002 |
mp_err res; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1003 |
mp_int *pQ, *pR; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1004 |
mp_int qtmp, rtmp, btmp; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1005 |
int cmp; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1006 |
mp_sign signA; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1007 |
mp_sign signB; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1008 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1009 |
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
|
1010 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1011 |
signA = MP_SIGN(a); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1012 |
signB = MP_SIGN(b); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1013 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1014 |
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
|
1015 |
return MP_RANGE; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1016 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1017 |
DIGITS(&qtmp) = 0; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1018 |
DIGITS(&rtmp) = 0; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1019 |
DIGITS(&btmp) = 0; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1020 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1021 |
/* Set up some temporaries... */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1022 |
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
|
1023 |
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
|
1024 |
pR = &rtmp; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1025 |
} else { |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1026 |
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
|
1027 |
pR = r; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1028 |
} |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1029 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1030 |
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
|
1031 |
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
|
1032 |
pQ = &qtmp; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1033 |
} else { |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1034 |
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
|
1035 |
pQ = q; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1036 |
mp_zero(pQ); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1037 |
} |
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 |
/* |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1040 |
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
|
1041 |
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
|
1042 |
*/ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1043 |
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
|
1044 |
if (cmp) { |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1045 |
/* 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
|
1046 |
mp_zero(pQ); |
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_set(pQ, 1); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1049 |
mp_zero(pR); |
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 |
} else { |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1052 |
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
|
1053 |
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
|
1054 |
} |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1055 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1056 |
/* 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
|
1057 |
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
|
1058 |
/* 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
|
1059 |
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
|
1060 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1061 |
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
|
1062 |
SIGN(pQ) = ZPOS; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1063 |
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
|
1064 |
SIGN(pR) = ZPOS; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1065 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1066 |
/* 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
|
1067 |
if(q && q != pQ) |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1068 |
s_mp_exch(pQ, q); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1069 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1070 |
if(r && r != pR) |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1071 |
s_mp_exch(pR, r); |
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 |
CLEANUP: |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1074 |
mp_clear(&btmp); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1075 |
mp_clear(&rtmp); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1076 |
mp_clear(&qtmp); |
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 |
return res; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1079 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1080 |
} /* end mp_div() */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1081 |
|
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 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1084 |
/* {{{ 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
|
1085 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1086 |
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
|
1087 |
{ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1088 |
mp_err res; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1089 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1090 |
ARGCHK(a != NULL, MP_BADARG); |
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(q) { |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1093 |
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
|
1094 |
return res; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1095 |
} |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1096 |
if(r) { |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1097 |
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
|
1098 |
return res; |
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 |
if(q) { |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1101 |
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
|
1102 |
} |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1103 |
if(r) { |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1104 |
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
|
1105 |
} |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1106 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1107 |
return MP_OKAY; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1108 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1109 |
} /* end mp_div_2d() */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1110 |
|
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 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1113 |
/* {{{ mp_expt(a, b, c) */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1114 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1115 |
/* |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1116 |
mp_expt(a, b, c) |
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 |
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
|
1119 |
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
|
1120 |
*/ |
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 |
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
|
1123 |
{ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1124 |
mp_int s, x; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1125 |
mp_err res; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1126 |
mp_digit d; |
8175
27e80dc06104
6989705: ECC security code native code compiler warnings
vinnie
parents:
5506
diff
changeset
|
1127 |
unsigned int dig, bit; |
3863
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 |
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
|
1130 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1131 |
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
|
1132 |
return MP_RANGE; |
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 |
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
|
1135 |
return res; |
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 |
mp_set(&s, 1); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1138 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1139 |
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
|
1140 |
goto X; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1141 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1142 |
/* 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
|
1143 |
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
|
1144 |
d = DIGIT(b, dig); |
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 |
/* 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
|
1147 |
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
|
1148 |
if(d & 1) { |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1149 |
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
|
1150 |
goto CLEANUP; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1151 |
} |
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 |
d >>= 1; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1154 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1155 |
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
|
1156 |
goto CLEANUP; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1157 |
} |
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 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1160 |
/* Consider now the last digit... */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1161 |
d = DIGIT(b, dig); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1162 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1163 |
while(d) { |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1164 |
if(d & 1) { |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1165 |
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
|
1166 |
goto CLEANUP; |
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 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1169 |
d >>= 1; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1170 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1171 |
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
|
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 |
if(mp_iseven(b)) |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1176 |
SIGN(&s) = SIGN(a); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1177 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1178 |
res = mp_copy(&s, c); |
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 |
CLEANUP: |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1181 |
mp_clear(&x); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1182 |
X: |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1183 |
mp_clear(&s); |
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 |
return res; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1186 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1187 |
} /* end mp_expt() */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1188 |
|
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 |
/* {{{ mp_2expt(a, k) */ |
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 |
/* Compute a = 2^k */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1194 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1195 |
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
|
1196 |
{ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1197 |
ARGCHK(a != NULL, MP_BADARG); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1198 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1199 |
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
|
1200 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1201 |
} /* end mp_2expt() */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1202 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1203 |
/* }}} */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1204 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1205 |
/* {{{ mp_mod(a, m, c) */ |
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 |
/* |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1208 |
mp_mod(a, m, c) |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1209 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1210 |
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
|
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_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
|
1214 |
{ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1215 |
mp_err res; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1216 |
int mag; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1217 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1218 |
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
|
1219 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1220 |
if(SIGN(m) == NEG) |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1221 |
return MP_RANGE; |
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 |
/* |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1224 |
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
|
1225 |
absolute value. |
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 |
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
|
1228 |
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
|
1229 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1230 |
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
|
1231 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1232 |
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
|
1233 |
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
|
1234 |
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
|
1235 |
*/ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1236 |
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
|
1237 |
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
|
1238 |
return res; |
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 |
if(SIGN(c) == NEG) { |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1241 |
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
|
1242 |
return res; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1243 |
} |
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 |
} else if(mag < 0) { |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1246 |
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
|
1247 |
return res; |
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(mp_cmp_z(a) < 0) { |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1250 |
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
|
1251 |
return res; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1252 |
|
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 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1255 |
} else { |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1256 |
mp_zero(c); |
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 |
} |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1259 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1260 |
return MP_OKAY; |
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 |
} /* end mp_mod() */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1263 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1264 |
/* }}} */ |
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 |
/* {{{ 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
|
1267 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1268 |
/* |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1269 |
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
|
1270 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1271 |
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
|
1272 |
*/ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1273 |
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
|
1274 |
{ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1275 |
mp_err res; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1276 |
mp_digit rem; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1277 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1278 |
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
|
1279 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1280 |
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
|
1281 |
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
|
1282 |
return res; |
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 |
} else { |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1285 |
if(SIGN(a) == NEG) |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1286 |
rem = d - DIGIT(a, 0); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1287 |
else |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1288 |
rem = DIGIT(a, 0); |
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 |
if(c) |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1292 |
*c = rem; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1293 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1294 |
return MP_OKAY; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1295 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1296 |
} /* end mp_mod_d() */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1297 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1298 |
/* }}} */ |
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 |
/* {{{ mp_sqrt(a, b) */ |
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 |
/* |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1303 |
mp_sqrt(a, b) |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1304 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1305 |
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
|
1306 |
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
|
1307 |
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
|
1308 |
following two properties: |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1309 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1310 |
b^2 <= a |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1311 |
(b+1)^2 >= a |
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 |
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
|
1314 |
*/ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1315 |
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
|
1316 |
{ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1317 |
mp_int x, t; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1318 |
mp_err res; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1319 |
mp_size used; |
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 |
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
|
1322 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1323 |
/* 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
|
1324 |
if(SIGN(a) == NEG) |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1325 |
return MP_RANGE; |
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 |
/* 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
|
1328 |
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
|
1329 |
return mp_copy(a, b); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1330 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1331 |
/* 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
|
1332 |
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
|
1333 |
return res; |
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 |
/* 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
|
1336 |
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
|
1337 |
goto X; |
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 |
used = MP_USED(&x); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1340 |
if (used > 1) { |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1341 |
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
|
1342 |
} |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1343 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1344 |
for(;;) { |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1345 |
/* t = (x * x) - a */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1346 |
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
|
1347 |
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
|
1348 |
(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
|
1349 |
goto CLEANUP; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1350 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1351 |
/* t = t / 2x */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1352 |
s_mp_mul_2(&x); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1353 |
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
|
1354 |
goto CLEANUP; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1355 |
s_mp_div_2(&x); |
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 |
/* 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
|
1358 |
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
|
1359 |
break; |
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 |
/* x = x - t */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1362 |
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
|
1363 |
goto CLEANUP; |
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 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1367 |
/* Copy result to output parameter */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1368 |
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
|
1369 |
s_mp_exch(&x, b); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1370 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1371 |
CLEANUP: |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1372 |
mp_clear(&x); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1373 |
X: |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1374 |
mp_clear(&t); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1375 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1376 |
return res; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1377 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1378 |
} /* end mp_sqrt() */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1379 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1380 |
/* }}} */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1381 |
|
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 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1384 |
/*------------------------------------------------------------------------*/ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1385 |
/* {{{ Modular arithmetic */ |
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 |
#if MP_MODARITH |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1388 |
/* {{{ 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
|
1389 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1390 |
/* |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1391 |
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
|
1392 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1393 |
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
|
1394 |
*/ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1395 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1396 |
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
|
1397 |
{ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1398 |
mp_err 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 |
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
|
1401 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1402 |
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
|
1403 |
return res; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1404 |
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
|
1405 |
return res; |
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 |
return MP_OKAY; |
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 |
} |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1410 |
|
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_submod(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 |
/* |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1416 |
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
|
1417 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1418 |
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
|
1419 |
*/ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1420 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1421 |
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
|
1422 |
{ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1423 |
mp_err res; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1424 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1425 |
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
|
1426 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1427 |
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
|
1428 |
return res; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1429 |
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
|
1430 |
return res; |
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 |
return MP_OKAY; |
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 |
|
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_mulmod(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 |
/* |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1441 |
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
|
1442 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1443 |
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
|
1444 |
*/ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1445 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1446 |
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
|
1447 |
{ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1448 |
mp_err res; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1449 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1450 |
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
|
1451 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1452 |
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
|
1453 |
return res; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1454 |
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
|
1455 |
return res; |
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 |
return MP_OKAY; |
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 |
|
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_sqrmod(a, 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 |
#if MP_SQUARE |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1466 |
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
|
1467 |
{ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1468 |
mp_err res; |
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 |
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
|
1471 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1472 |
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
|
1473 |
return res; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1474 |
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
|
1475 |
return res; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1476 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1477 |
return MP_OKAY; |
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 |
} /* end mp_sqrmod() */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1480 |
#endif |
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 |
/* {{{ 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
|
1485 |
|
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 |
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
|
1488 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1489 |
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
|
1490 |
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
|
1491 |
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
|
1492 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1493 |
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
|
1494 |
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
|
1495 |
*/ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1496 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1497 |
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
|
1498 |
{ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1499 |
mp_int s, x, mu; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1500 |
mp_err res; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1501 |
mp_digit d; |
8175
27e80dc06104
6989705: ECC security code native code compiler warnings
vinnie
parents:
5506
diff
changeset
|
1502 |
unsigned int dig, bit; |
3863
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 |
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
|
1505 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1506 |
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
|
1507 |
return MP_RANGE; |
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 |
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
|
1510 |
return res; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1511 |
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
|
1512 |
(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
|
1513 |
goto X; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1514 |
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
|
1515 |
goto MU; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1516 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1517 |
mp_set(&s, 1); |
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 |
/* mu = b^2k / m */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1520 |
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
|
1521 |
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
|
1522 |
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
|
1523 |
goto CLEANUP; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1524 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1525 |
/* 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
|
1526 |
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
|
1527 |
d = DIGIT(b, dig); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1528 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1529 |
/* 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
|
1530 |
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
|
1531 |
if(d & 1) { |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1532 |
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
|
1533 |
goto CLEANUP; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1534 |
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
|
1535 |
goto CLEANUP; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1536 |
} |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1537 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1538 |
d >>= 1; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1539 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1540 |
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
|
1541 |
goto CLEANUP; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1542 |
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
|
1543 |
goto CLEANUP; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1544 |
} |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1545 |
} |
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 |
/* Now do the last digit... */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1548 |
d = DIGIT(b, dig); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1549 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1550 |
while(d) { |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1551 |
if(d & 1) { |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1552 |
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
|
1553 |
goto CLEANUP; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1554 |
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
|
1555 |
goto CLEANUP; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1556 |
} |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1557 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1558 |
d >>= 1; |
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 |
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
|
1561 |
goto CLEANUP; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1562 |
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
|
1563 |
goto CLEANUP; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1564 |
} |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1565 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1566 |
s_mp_exch(&s, c); |
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 |
CLEANUP: |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1569 |
mp_clear(&mu); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1570 |
MU: |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1571 |
mp_clear(&x); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1572 |
X: |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1573 |
mp_clear(&s); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1574 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1575 |
return res; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1576 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1577 |
} /* end s_mp_exptmod() */ |
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 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1581 |
/* {{{ 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
|
1582 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1583 |
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
|
1584 |
{ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1585 |
mp_int s, x; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1586 |
mp_err res; |
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 |
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
|
1589 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1590 |
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
|
1591 |
return res; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1592 |
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
|
1593 |
goto X; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1594 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1595 |
mp_set(&s, 1); |
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 |
while(d != 0) { |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1598 |
if(d & 1) { |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1599 |
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
|
1600 |
(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
|
1601 |
goto CLEANUP; |
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 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1604 |
d /= 2; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1605 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1606 |
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
|
1607 |
(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
|
1608 |
goto CLEANUP; |
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 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1611 |
s_mp_exch(&s, c); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1612 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1613 |
CLEANUP: |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1614 |
mp_clear(&x); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1615 |
X: |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1616 |
mp_clear(&s); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1617 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1618 |
return res; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1619 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1620 |
} /* end mp_exptmod_d() */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1621 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1622 |
/* }}} */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1623 |
#endif /* if MP_MODARITH */ |
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 |
|
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 |
/* {{{ Comparison functions */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1629 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1630 |
/* {{{ mp_cmp_z(a) */ |
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 |
mp_cmp_z(a) |
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 |
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
|
1636 |
*/ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1637 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1638 |
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
|
1639 |
{ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1640 |
if(SIGN(a) == NEG) |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1641 |
return MP_LT; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1642 |
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
|
1643 |
return MP_EQ; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1644 |
else |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1645 |
return MP_GT; |
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 |
} /* end mp_cmp_z() */ |
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 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1651 |
/* {{{ mp_cmp_d(a, d) */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1652 |
|
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 |
mp_cmp_d(a, d) |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1655 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1656 |
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
|
1657 |
*/ |
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 |
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
|
1660 |
{ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1661 |
ARGCHK(a != NULL, MP_EQ); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1662 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1663 |
if(SIGN(a) == NEG) |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1664 |
return MP_LT; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1665 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1666 |
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
|
1667 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1668 |
} /* end mp_cmp_d() */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1669 |
|
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 |
/* {{{ mp_cmp(a, b) */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1673 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1674 |
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
|
1675 |
{ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1676 |
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
|
1677 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1678 |
if(SIGN(a) == SIGN(b)) { |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1679 |
int mag; |
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 |
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
|
1682 |
return MP_EQ; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1683 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1684 |
if(SIGN(a) == ZPOS) |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1685 |
return mag; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1686 |
else |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1687 |
return -mag; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1688 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1689 |
} else if(SIGN(a) == ZPOS) { |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1690 |
return MP_GT; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1691 |
} else { |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1692 |
return MP_LT; |
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 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1695 |
} /* end mp_cmp() */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1696 |
|
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 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1699 |
/* {{{ mp_cmp_mag(a, b) */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1700 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1701 |
/* |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1702 |
mp_cmp_mag(a, b) |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1703 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1704 |
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
|
1705 |
*/ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1706 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1707 |
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
|
1708 |
{ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1709 |
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
|
1710 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1711 |
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
|
1712 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1713 |
} /* end mp_cmp_mag() */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1714 |
|
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 |
/* {{{ 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
|
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 |
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
|
1721 |
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
|
1722 |
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
|
1723 |
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
|
1724 |
*/ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1725 |
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
|
1726 |
{ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1727 |
mp_int tmp; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1728 |
int out; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1729 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1730 |
ARGCHK(a != NULL, MP_EQ); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1731 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1732 |
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
|
1733 |
out = mp_cmp(a, &tmp); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1734 |
mp_clear(&tmp); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1735 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1736 |
return out; |
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 |
} /* end mp_cmp_int() */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1739 |
|
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 |
/* {{{ mp_isodd(a) */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1743 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1744 |
/* |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1745 |
mp_isodd(a) |
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 |
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
|
1748 |
*/ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1749 |
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
|
1750 |
{ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1751 |
ARGCHK(a != NULL, 0); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1752 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1753 |
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
|
1754 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1755 |
} /* end mp_isodd() */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1756 |
|
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 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1759 |
/* {{{ mp_iseven(a) */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1760 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1761 |
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
|
1762 |
{ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1763 |
return !mp_isodd(a); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1764 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1765 |
} /* end mp_iseven() */ |
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 |
/* }}} */ |
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 |
/* }}} */ |
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 |
/*------------------------------------------------------------------------*/ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1772 |
/* {{{ Number theoretic functions */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1773 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1774 |
#if MP_NUMTH |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1775 |
/* {{{ mp_gcd(a, b, c) */ |
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 |
/* |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1778 |
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
|
1779 |
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
|
1780 |
*/ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1781 |
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
|
1782 |
{ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1783 |
mp_err res; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1784 |
mp_int u, v, t; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1785 |
mp_size k = 0; |
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 |
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
|
1788 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1789 |
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
|
1790 |
return MP_RANGE; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1791 |
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
|
1792 |
return mp_copy(b, c); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1793 |
} 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
|
1794 |
return mp_copy(a, c); |
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 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1797 |
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
|
1798 |
return res; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1799 |
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
|
1800 |
goto U; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1801 |
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
|
1802 |
goto V; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1803 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1804 |
SIGN(&u) = ZPOS; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1805 |
SIGN(&v) = ZPOS; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1806 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1807 |
/* 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
|
1808 |
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
|
1809 |
s_mp_div_2(&u); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1810 |
s_mp_div_2(&v); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1811 |
++k; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1812 |
} |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1813 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1814 |
/* Initialize t */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1815 |
if(mp_isodd(&u)) { |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1816 |
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
|
1817 |
goto CLEANUP; |
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 |
/* t = -v */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1820 |
if(SIGN(&v) == ZPOS) |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1821 |
SIGN(&t) = NEG; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1822 |
else |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1823 |
SIGN(&t) = ZPOS; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1824 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1825 |
} else { |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1826 |
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
|
1827 |
goto CLEANUP; |
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 |
} |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1830 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1831 |
for(;;) { |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1832 |
while(mp_iseven(&t)) { |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1833 |
s_mp_div_2(&t); |
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 |
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
|
1837 |
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
|
1838 |
goto CLEANUP; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1839 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1840 |
} else { |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1841 |
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
|
1842 |
goto CLEANUP; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1843 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1844 |
/* v = -t */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1845 |
if(SIGN(&t) == ZPOS) |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1846 |
SIGN(&v) = NEG; |
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 |
SIGN(&v) = ZPOS; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1849 |
} |
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 |
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
|
1852 |
goto CLEANUP; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1853 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1854 |
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
|
1855 |
break; |
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 |
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
|
1859 |
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
|
1860 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1861 |
CLEANUP: |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1862 |
mp_clear(&v); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1863 |
V: |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1864 |
mp_clear(&u); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1865 |
U: |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1866 |
mp_clear(&t); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1867 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1868 |
return res; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1869 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1870 |
} /* end mp_gcd() */ |
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 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1874 |
/* {{{ mp_lcm(a, b, c) */ |
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 |
/* 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
|
1877 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1878 |
ab = [a, b](a, b) |
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 |
... 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
|
1881 |
*/ |
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 |
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
|
1884 |
{ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1885 |
mp_int gcd, prod; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1886 |
mp_err res; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1887 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1888 |
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
|
1889 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1890 |
/* Set up temporaries */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1891 |
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
|
1892 |
return res; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1893 |
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
|
1894 |
goto GCD; |
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 |
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
|
1897 |
goto CLEANUP; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1898 |
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
|
1899 |
goto CLEANUP; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1900 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1901 |
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
|
1902 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1903 |
CLEANUP: |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1904 |
mp_clear(&prod); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1905 |
GCD: |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1906 |
mp_clear(&gcd); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1907 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1908 |
return 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 |
} /* end mp_lcm() */ |
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 |
/* }}} */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1913 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1914 |
/* {{{ 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
|
1915 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1916 |
/* |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1917 |
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
|
1918 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1919 |
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
|
1920 |
(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
|
1921 |
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
|
1922 |
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
|
1923 |
*/ |
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 |
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
|
1926 |
{ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1927 |
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
|
1928 |
mp_int *clean[9]; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1929 |
mp_err res; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1930 |
int last = -1; |
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 |
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
|
1933 |
return MP_RANGE; |
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 |
/* 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
|
1936 |
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
|
1937 |
clean[++last] = &u; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1938 |
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
|
1939 |
clean[++last] = &v; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1940 |
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
|
1941 |
clean[++last] = &gx; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1942 |
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
|
1943 |
clean[++last] = &A; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1944 |
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
|
1945 |
clean[++last] = &B; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1946 |
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
|
1947 |
clean[++last] = &C; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1948 |
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
|
1949 |
clean[++last] = &D; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1950 |
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
|
1951 |
clean[++last] = &xc; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1952 |
mp_abs(&xc, &xc); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1953 |
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
|
1954 |
clean[++last] = &yc; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1955 |
mp_abs(&yc, &yc); |
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 |
mp_set(&gx, 1); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1958 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1959 |
/* 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
|
1960 |
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
|
1961 |
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
|
1962 |
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
|
1963 |
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
|
1964 |
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
|
1965 |
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
|
1966 |
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
|
1967 |
} |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1968 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1969 |
mp_copy(&xc, &u); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1970 |
mp_copy(&yc, &v); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1971 |
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
|
1972 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1973 |
/* Loop through binary GCD algorithm */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1974 |
do { |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1975 |
while(mp_iseven(&u)) { |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1976 |
s_mp_div_2(&u); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1977 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1978 |
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
|
1979 |
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
|
1980 |
} else { |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1981 |
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
|
1982 |
s_mp_div_2(&A); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1983 |
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
|
1984 |
s_mp_div_2(&B); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1985 |
} |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1986 |
} |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1987 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1988 |
while(mp_iseven(&v)) { |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1989 |
s_mp_div_2(&v); |
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 |
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
|
1992 |
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
|
1993 |
} else { |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1994 |
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
|
1995 |
s_mp_div_2(&C); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1996 |
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
|
1997 |
s_mp_div_2(&D); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
1998 |
} |
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 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2001 |
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
|
2002 |
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
|
2003 |
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
|
2004 |
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
|
2005 |
} else { |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2006 |
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
|
2007 |
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
|
2008 |
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
|
2009 |
} |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2010 |
} 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
|
2011 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2012 |
/* copy results to output */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2013 |
if(x) |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2014 |
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
|
2015 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2016 |
if(y) |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2017 |
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
|
2018 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2019 |
if(g) |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2020 |
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
|
2021 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2022 |
CLEANUP: |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2023 |
while(last >= 0) |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2024 |
mp_clear(clean[last--]); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2025 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2026 |
return res; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2027 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2028 |
} /* end mp_xgcd() */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2029 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2030 |
/* }}} */ |
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 |
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
|
2033 |
{ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2034 |
mp_digit d; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2035 |
mp_size n = 0; |
8175
27e80dc06104
6989705: ECC security code native code compiler warnings
vinnie
parents:
5506
diff
changeset
|
2036 |
unsigned int ix; |
3863
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 (!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
|
2039 |
return n; |
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 |
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
|
2042 |
n += MP_DIGIT_BIT; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2043 |
if (!d) |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2044 |
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
|
2045 |
#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
|
2046 |
if (!(d & 0xffffffffU)) { |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2047 |
d >>= 32; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2048 |
n += 32; |
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 |
#endif |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2051 |
if (!(d & 0xffffU)) { |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2052 |
d >>= 16; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2053 |
n += 16; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2054 |
} |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2055 |
if (!(d & 0xffU)) { |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2056 |
d >>= 8; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2057 |
n += 8; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2058 |
} |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2059 |
if (!(d & 0xfU)) { |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2060 |
d >>= 4; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2061 |
n += 4; |
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 |
if (!(d & 0x3U)) { |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2064 |
d >>= 2; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2065 |
n += 2; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2066 |
} |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2067 |
if (!(d & 0x1U)) { |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2068 |
d >>= 1; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2069 |
n += 1; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2070 |
} |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2071 |
#if MP_ARGCHK == 2 |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2072 |
assert(0 != (d & 1)); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2073 |
#endif |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2074 |
return n; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2075 |
} |
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 |
/* 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
|
2078 |
** 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
|
2079 |
** 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
|
2080 |
** 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
|
2081 |
*/ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2082 |
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
|
2083 |
{ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2084 |
mp_err res; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2085 |
mp_err k = 0; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2086 |
mp_int d, f, g; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2087 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2088 |
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
|
2089 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2090 |
MP_DIGITS(&d) = 0; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2091 |
MP_DIGITS(&f) = 0; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2092 |
MP_DIGITS(&g) = 0; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2093 |
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
|
2094 |
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
|
2095 |
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
|
2096 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2097 |
mp_set(c, 1); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2098 |
mp_zero(&d); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2099 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2100 |
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
|
2101 |
res = MP_UNDEF; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2102 |
} else |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2103 |
for (;;) { |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2104 |
int diff_sign; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2105 |
while (mp_iseven(&f)) { |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2106 |
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
|
2107 |
if (!n) { |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2108 |
res = MP_UNDEF; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2109 |
goto CLEANUP; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2110 |
} |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2111 |
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
|
2112 |
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
|
2113 |
k += n; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2114 |
} |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2115 |
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
|
2116 |
res = k; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2117 |
break; |
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 |
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
|
2120 |
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
|
2121 |
s_mp_exch(&f, &g); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2122 |
s_mp_exch(c, &d); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2123 |
} 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
|
2124 |
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
|
2125 |
break; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2126 |
} |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2127 |
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
|
2128 |
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
|
2129 |
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
|
2130 |
} else { |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2131 |
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
|
2132 |
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
|
2133 |
} |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2134 |
} |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2135 |
if (res >= 0) { |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2136 |
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
|
2137 |
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
|
2138 |
} |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2139 |
res = k; |
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 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2142 |
CLEANUP: |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2143 |
mp_clear(&d); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2144 |
mp_clear(&f); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2145 |
mp_clear(&g); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2146 |
return res; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2147 |
} |
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 |
/* 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
|
2150 |
** 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
|
2151 |
** 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
|
2152 |
*/ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2153 |
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
|
2154 |
{ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2155 |
mp_digit T = P; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2156 |
T *= 2 - (P * T); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2157 |
T *= 2 - (P * T); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2158 |
T *= 2 - (P * T); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2159 |
T *= 2 - (P * T); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2160 |
#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
|
2161 |
T *= 2 - (P * T); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2162 |
T *= 2 - (P * T); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2163 |
#endif |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2164 |
return T; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2165 |
} |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2166 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2167 |
/* 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
|
2168 |
** 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
|
2169 |
** 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
|
2170 |
** 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
|
2171 |
*/ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2172 |
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
|
2173 |
{ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2174 |
int k_orig = k; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2175 |
mp_digit r; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2176 |
mp_size ix; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2177 |
mp_err res; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2178 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2179 |
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
|
2180 |
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
|
2181 |
} else { |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2182 |
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
|
2183 |
} |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2184 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2185 |
/* 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
|
2186 |
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
|
2187 |
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
|
2188 |
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
|
2189 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2190 |
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
|
2191 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2192 |
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
|
2193 |
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
|
2194 |
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
|
2195 |
if (j < MP_DIGIT_BIT) { |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2196 |
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
|
2197 |
} |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2198 |
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
|
2199 |
k -= j; |
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 |
s_mp_clamp(x); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2202 |
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
|
2203 |
res = MP_OKAY; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2204 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2205 |
CLEANUP: |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2206 |
return res; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2207 |
} |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2208 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2209 |
/* 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
|
2210 |
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
|
2211 |
{ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2212 |
int k; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2213 |
mp_err res; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2214 |
mp_int x; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2215 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2216 |
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
|
2217 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2218 |
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
|
2219 |
return MP_RANGE; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2220 |
if (mp_iseven(m)) |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2221 |
return MP_UNDEF; |
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 |
MP_DIGITS(&x) = 0; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2224 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2225 |
if (a == c) { |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2226 |
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
|
2227 |
return res; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2228 |
if (a == m) |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2229 |
m = &x; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2230 |
a = &x; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2231 |
} else if (m == c) { |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2232 |
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
|
2233 |
return res; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2234 |
m = &x; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2235 |
} else { |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2236 |
MP_DIGITS(&x) = 0; |
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 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2239 |
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
|
2240 |
k = res; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2241 |
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
|
2242 |
CLEANUP: |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2243 |
mp_clear(&x); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2244 |
return res; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2245 |
} |
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 |
/* 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
|
2248 |
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
|
2249 |
{ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2250 |
mp_int g, x; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2251 |
mp_err res; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2252 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2253 |
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
|
2254 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2255 |
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
|
2256 |
return MP_RANGE; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2257 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2258 |
MP_DIGITS(&g) = 0; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2259 |
MP_DIGITS(&x) = 0; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2260 |
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
|
2261 |
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
|
2262 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2263 |
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
|
2264 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2265 |
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
|
2266 |
res = MP_UNDEF; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2267 |
goto CLEANUP; |
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 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2270 |
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
|
2271 |
SIGN(c) = SIGN(a); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2272 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2273 |
CLEANUP: |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2274 |
mp_clear(&x); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2275 |
mp_clear(&g); |
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 |
return res; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2278 |
} |
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 |
/* 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
|
2281 |
/* 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
|
2282 |
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
|
2283 |
{ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2284 |
mp_err res; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2285 |
mp_size ix = k + 4; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2286 |
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
|
2287 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2288 |
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
|
2289 |
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
|
2290 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2291 |
if (mp_iseven(a)) |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2292 |
return MP_UNDEF; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2293 |
if (k <= MP_DIGIT_BIT) { |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2294 |
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
|
2295 |
if (k < MP_DIGIT_BIT) |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2296 |
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
|
2297 |
mp_set(c, i); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2298 |
return MP_OKAY; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2299 |
} |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2300 |
MP_DIGITS(&t0) = 0; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2301 |
MP_DIGITS(&t1) = 0; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2302 |
MP_DIGITS(&val) = 0; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2303 |
MP_DIGITS(&tmp) = 0; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2304 |
MP_DIGITS(&two2k) = 0; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2305 |
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
|
2306 |
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
|
2307 |
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
|
2308 |
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
|
2309 |
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
|
2310 |
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
|
2311 |
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
|
2312 |
do { |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2313 |
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
|
2314 |
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
|
2315 |
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
|
2316 |
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
|
2317 |
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
|
2318 |
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
|
2319 |
} |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2320 |
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
|
2321 |
break; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2322 |
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
|
2323 |
} while (--ix > 0); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2324 |
if (!ix) { |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2325 |
res = MP_UNDEF; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2326 |
} else { |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2327 |
mp_exch(c, &t1); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2328 |
} |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2329 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2330 |
CLEANUP: |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2331 |
mp_clear(&t0); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2332 |
mp_clear(&t1); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2333 |
mp_clear(&val); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2334 |
mp_clear(&tmp); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2335 |
mp_clear(&two2k); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2336 |
return res; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2337 |
} |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2338 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2339 |
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
|
2340 |
{ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2341 |
mp_err res; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2342 |
mp_size k; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2343 |
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
|
2344 |
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
|
2345 |
mp_int C2, tmp1, tmp2; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2346 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2347 |
/*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
|
2348 |
/*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
|
2349 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2350 |
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
|
2351 |
k = res; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2352 |
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
|
2353 |
} |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2354 |
MP_DIGITS(&oddFactor) = 0; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2355 |
MP_DIGITS(&evenFactor) = 0; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2356 |
MP_DIGITS(&oddPart) = 0; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2357 |
MP_DIGITS(&evenPart) = 0; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2358 |
MP_DIGITS(&C2) = 0; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2359 |
MP_DIGITS(&tmp1) = 0; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2360 |
MP_DIGITS(&tmp2) = 0; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2361 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2362 |
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
|
2363 |
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
|
2364 |
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
|
2365 |
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
|
2366 |
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
|
2367 |
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
|
2368 |
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
|
2369 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2370 |
k = mp_trailing_zeros(m); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2371 |
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
|
2372 |
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
|
2373 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2374 |
/* compute a**-1 mod oddFactor. */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2375 |
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
|
2376 |
/* 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
|
2377 |
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
|
2378 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2379 |
/* 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
|
2380 |
/* let m1 = oddFactor, v1 = oddPart, |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2381 |
* let m2 = evenFactor, v2 = evenPart. |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2382 |
*/ |
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 |
/* 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
|
2385 |
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
|
2386 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2387 |
/* 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
|
2388 |
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
|
2389 |
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
|
2390 |
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
|
2391 |
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
|
2392 |
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
|
2393 |
} |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2394 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2395 |
/* compute answer = v1 + u*m1 */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2396 |
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
|
2397 |
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
|
2398 |
/* 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
|
2399 |
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
|
2400 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2401 |
CLEANUP: |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2402 |
mp_clear(&oddFactor); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2403 |
mp_clear(&evenFactor); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2404 |
mp_clear(&oddPart); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2405 |
mp_clear(&evenPart); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2406 |
mp_clear(&C2); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2407 |
mp_clear(&tmp1); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2408 |
mp_clear(&tmp2); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2409 |
return res; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2410 |
} |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2411 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2412 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2413 |
/* {{{ mp_invmod(a, m, c) */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2414 |
|
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 |
mp_invmod(a, m, c) |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2417 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2418 |
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
|
2419 |
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
|
2420 |
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
|
2421 |
*/ |
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 |
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
|
2424 |
{ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2425 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2426 |
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
|
2427 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2428 |
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
|
2429 |
return MP_RANGE; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2430 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2431 |
if (mp_isodd(m)) { |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2432 |
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
|
2433 |
} |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2434 |
if (mp_iseven(a)) |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2435 |
return MP_UNDEF; /* not invertable */ |
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 |
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
|
2438 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2439 |
} /* end mp_invmod() */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2440 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2441 |
/* }}} */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2442 |
#endif /* if MP_NUMTH */ |
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 |
|
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 |
/* {{{ mp_print(mp, ofp) */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2448 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2449 |
#if MP_IOFUNC |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2450 |
/* |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2451 |
mp_print(mp, ofp) |
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 |
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
|
2454 |
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
|
2455 |
*/ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2456 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2457 |
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
|
2458 |
{ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2459 |
int ix; |
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 |
if(mp == NULL || ofp == NULL) |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2462 |
return; |
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 |
fputc((SIGN(mp) == NEG) ? '-' : '+', ofp); |
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 |
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
|
2467 |
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
|
2468 |
} |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2469 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2470 |
} /* end mp_print() */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2471 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2472 |
#endif /* if MP_IOFUNC */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2473 |
|
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 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2476 |
/*------------------------------------------------------------------------*/ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2477 |
/* {{{ More I/O Functions */ |
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 |
/* {{{ 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
|
2480 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2481 |
/* |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2482 |
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
|
2483 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2484 |
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
|
2485 |
*/ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2486 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2487 |
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
|
2488 |
{ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2489 |
int ix; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2490 |
mp_err res; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2491 |
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
|
2492 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2493 |
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
|
2494 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2495 |
mp_zero(mp); |
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 |
/* Get sign from first byte */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2498 |
if(ustr[0]) |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2499 |
SIGN(mp) = NEG; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2500 |
else |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2501 |
SIGN(mp) = ZPOS; |
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 |
/* 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
|
2504 |
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
|
2505 |
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
|
2506 |
return res; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2507 |
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
|
2508 |
return res; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2509 |
} |
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 |
return MP_OKAY; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2512 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2513 |
} /* end mp_read_raw() */ |
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 |
/* }}} */ |
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_raw_size(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 |
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
|
2520 |
{ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2521 |
ARGCHK(mp != NULL, 0); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2522 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2523 |
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
|
2524 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2525 |
} /* end mp_raw_size() */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2526 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2527 |
/* }}} */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2528 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2529 |
/* {{{ mp_toraw(mp, str) */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2530 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2531 |
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
|
2532 |
{ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2533 |
int ix, jx, pos = 1; |
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 |
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
|
2536 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2537 |
str[0] = (char)SIGN(mp); |
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 |
/* Iterate over each digit... */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2540 |
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
|
2541 |
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
|
2542 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2543 |
/* 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
|
2544 |
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
|
2545 |
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
|
2546 |
} |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2547 |
} |
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 |
return MP_OKAY; |
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 |
} /* end mp_toraw() */ |
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 |
/* }}} */ |
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 |
/* {{{ 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
|
2556 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2557 |
/* |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2558 |
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
|
2559 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2560 |
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
|
2561 |
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
|
2562 |
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
|
2563 |
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
|
2564 |
*/ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2565 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2566 |
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
|
2567 |
{ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2568 |
int ix = 0, val = 0; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2569 |
mp_err res; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2570 |
mp_sign sig = ZPOS; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2571 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2572 |
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
|
2573 |
MP_BADARG); |
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 |
mp_zero(mp); |
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 |
/* 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
|
2578 |
while(str[ix] && |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2579 |
(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
|
2580 |
str[ix] != '-' && |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2581 |
str[ix] != '+') { |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2582 |
++ix; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2583 |
} |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2584 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2585 |
if(str[ix] == '-') { |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2586 |
sig = NEG; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2587 |
++ix; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2588 |
} else if(str[ix] == '+') { |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2589 |
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
|
2590 |
++ix; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2591 |
} |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2592 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2593 |
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
|
2594 |
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
|
2595 |
return res; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2596 |
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
|
2597 |
return res; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2598 |
++ix; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2599 |
} |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2600 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2601 |
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
|
2602 |
SIGN(mp) = ZPOS; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2603 |
else |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2604 |
SIGN(mp) = sig; |
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 |
return MP_OKAY; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2607 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2608 |
} /* end mp_read_radix() */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2609 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2610 |
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
|
2611 |
{ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2612 |
int radix = default_radix; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2613 |
int cx; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2614 |
mp_sign sig = ZPOS; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2615 |
mp_err res; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2616 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2617 |
/* 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
|
2618 |
while ((cx = *str) != 0 && |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2619 |
(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
|
2620 |
cx != '-' && |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2621 |
cx != '+') { |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2622 |
++str; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2623 |
} |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2624 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2625 |
if (cx == '-') { |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2626 |
sig = NEG; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2627 |
++str; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2628 |
} else if (cx == '+') { |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2629 |
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
|
2630 |
++str; |
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 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2633 |
if (str[0] == '0') { |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2634 |
if ((str[1] | 0x20) == 'x') { |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2635 |
radix = 16; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2636 |
str += 2; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2637 |
} else { |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2638 |
radix = 8; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2639 |
str++; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2640 |
} |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2641 |
} |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2642 |
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
|
2643 |
if (res == MP_OKAY) { |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2644 |
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
|
2645 |
} |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2646 |
return res; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2647 |
} |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2648 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2649 |
/* }}} */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2650 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2651 |
/* {{{ mp_radix_size(mp, radix) */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2652 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2653 |
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
|
2654 |
{ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2655 |
int bits; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2656 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2657 |
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
|
2658 |
return 0; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2659 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2660 |
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
|
2661 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2662 |
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
|
2663 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2664 |
} /* end mp_radix_size() */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2665 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2666 |
/* }}} */ |
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 |
/* {{{ mp_toradix(mp, str, radix) */ |
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 |
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
|
2671 |
{ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2672 |
int ix, pos = 0; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2673 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2674 |
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
|
2675 |
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
|
2676 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2677 |
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
|
2678 |
str[0] = '0'; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2679 |
str[1] = '\0'; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2680 |
} else { |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2681 |
mp_err res; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2682 |
mp_int tmp; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2683 |
mp_sign sgn; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2684 |
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
|
2685 |
char ch; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2686 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2687 |
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
|
2688 |
return res; |
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 |
/* 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
|
2691 |
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
|
2692 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2693 |
/* 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
|
2694 |
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
|
2695 |
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
|
2696 |
mp_clear(&tmp); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2697 |
return res; |
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 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2700 |
/* Generate digits, use capital letters */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2701 |
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
|
2702 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2703 |
str[pos++] = ch; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2704 |
} |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2705 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2706 |
/* 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
|
2707 |
if(sgn == NEG) |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2708 |
str[pos++] = '-'; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2709 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2710 |
/* 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
|
2711 |
str[pos--] = '\0'; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2712 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2713 |
/* 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
|
2714 |
ix = 0; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2715 |
while(ix < pos) { |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2716 |
char tmp = str[ix]; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2717 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2718 |
str[ix] = str[pos]; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2719 |
str[pos] = tmp; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2720 |
++ix; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2721 |
--pos; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2722 |
} |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2723 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2724 |
mp_clear(&tmp); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2725 |
} |
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 |
return MP_OKAY; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2728 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2729 |
} /* end mp_toradix() */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2730 |
|
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 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2733 |
/* {{{ mp_tovalue(ch, r) */ |
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 |
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
|
2736 |
{ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2737 |
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
|
2738 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2739 |
} /* end mp_tovalue() */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2740 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2741 |
/* }}} */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2742 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2743 |
/* }}} */ |
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 |
/* {{{ mp_strerror(ec) */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2746 |
|
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 |
mp_strerror(ec) |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2749 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2750 |
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
|
2751 |
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
|
2752 |
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
|
2753 |
string. |
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 |
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
|
2756 |
{ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2757 |
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
|
2758 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2759 |
/* 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
|
2760 |
are accurate */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2761 |
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
|
2762 |
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
|
2763 |
} else { |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2764 |
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
|
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 |
} /* end mp_strerror() */ |
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 |
|
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 |
/*------------------------------------------------------------------------*/ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2773 |
/* 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
|
2774 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2775 |
/* {{{ Memory management */ |
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 |
/* {{{ s_mp_grow(mp, min) */ |
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 |
/* 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
|
2780 |
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
|
2781 |
{ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2782 |
if(min > ALLOC(mp)) { |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2783 |
mp_digit *tmp; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2784 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2785 |
/* 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
|
2786 |
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
|
2787 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2788 |
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
|
2789 |
return MP_MEM; |
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 |
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
|
2792 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2793 |
#if MP_CRYPTO |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2794 |
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
|
2795 |
#endif |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2796 |
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
|
2797 |
DIGITS(mp) = tmp; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2798 |
ALLOC(mp) = min; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2799 |
} |
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 |
return MP_OKAY; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2802 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2803 |
} /* end s_mp_grow() */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2804 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2805 |
/* }}} */ |
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 |
/* {{{ s_mp_pad(mp, min) */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2808 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2809 |
/* 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
|
2810 |
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
|
2811 |
{ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2812 |
if(min > USED(mp)) { |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2813 |
mp_err res; |
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 |
/* 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
|
2816 |
if (min > ALLOC(mp)) { |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2817 |
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
|
2818 |
return res; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2819 |
} else { |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2820 |
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
|
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 |
/* 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
|
2824 |
USED(mp) = min; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2825 |
} |
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 |
return MP_OKAY; |
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 |
} /* end s_mp_pad() */ |
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 |
/* }}} */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2832 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2833 |
/* {{{ s_mp_setz(dp, count) */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2834 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2835 |
#if MP_MACRO == 0 |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2836 |
/* 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
|
2837 |
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
|
2838 |
{ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2839 |
#if MP_MEMSET == 0 |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2840 |
int ix; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2841 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2842 |
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
|
2843 |
dp[ix] = 0; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2844 |
#else |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2845 |
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
|
2846 |
#endif |
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 |
} /* end s_mp_setz() */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2849 |
#endif |
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 |
/* }}} */ |
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 |
/* {{{ 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
|
2854 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2855 |
#if MP_MACRO == 0 |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2856 |
/* 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
|
2857 |
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
|
2858 |
{ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2859 |
#if MP_MEMCPY == 0 |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2860 |
int ix; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2861 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2862 |
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
|
2863 |
dp[ix] = sp[ix]; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2864 |
#else |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2865 |
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
|
2866 |
#endif |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2867 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2868 |
} /* end s_mp_copy() */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2869 |
#endif |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2870 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2871 |
/* }}} */ |
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 |
/* {{{ 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
|
2874 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2875 |
#if MP_MACRO == 0 |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2876 |
/* 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
|
2877 |
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
|
2878 |
{ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2879 |
++mp_allocs; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2880 |
#ifdef _KERNEL |
8175
27e80dc06104
6989705: ECC security code native code compiler warnings
vinnie
parents:
5506
diff
changeset
|
2881 |
mp_int *mp; |
3863
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2882 |
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
|
2883 |
if (mp != NULL) |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2884 |
FLAG(mp) = kmflag; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2885 |
return (mp); |
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 |
return calloc(nb, ni); |
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_alloc() */ |
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_free(ptr) */ |
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 |
/* 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
|
2899 |
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
|
2900 |
{ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2901 |
if(ptr) { |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2902 |
++mp_frees; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2903 |
#ifdef _KERNEL |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2904 |
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
|
2905 |
#else |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2906 |
free(ptr); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2907 |
#endif |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2908 |
} |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2909 |
} /* end s_mp_free() */ |
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 |
/* }}} */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2913 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2914 |
/* {{{ s_mp_clamp(mp) */ |
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 |
#if MP_MACRO == 0 |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2917 |
/* 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
|
2918 |
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
|
2919 |
{ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2920 |
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
|
2921 |
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
|
2922 |
--used; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2923 |
MP_USED(mp) = used; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2924 |
} /* end s_mp_clamp() */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2925 |
#endif |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2926 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2927 |
/* }}} */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2928 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2929 |
/* {{{ s_mp_exch(a, b) */ |
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 |
/* 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
|
2932 |
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
|
2933 |
{ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2934 |
mp_int tmp; |
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 |
tmp = *a; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2937 |
*a = *b; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2938 |
*b = tmp; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2939 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2940 |
} /* end s_mp_exch() */ |
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 |
/* }}} */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2943 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2944 |
/* }}} */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2945 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2946 |
/* {{{ Arithmetic helpers */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2947 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2948 |
/* {{{ s_mp_lshd(mp, p) */ |
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 |
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
|
2952 |
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
|
2953 |
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
|
2954 |
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
|
2955 |
the shifted result. |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2956 |
*/ |
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 |
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
|
2959 |
{ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2960 |
mp_err res; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2961 |
mp_size pos; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2962 |
int ix; |
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 |
if(p == 0) |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2965 |
return MP_OKAY; |
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 |
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
|
2968 |
return MP_OKAY; |
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 |
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
|
2971 |
return res; |
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 |
pos = USED(mp) - 1; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2974 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2975 |
/* 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
|
2976 |
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
|
2977 |
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
|
2978 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2979 |
/* 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
|
2980 |
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
|
2981 |
DIGIT(mp, ix) = 0; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2982 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2983 |
return MP_OKAY; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2984 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2985 |
} /* end s_mp_lshd() */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2986 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2987 |
/* }}} */ |
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 |
/* {{{ 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
|
2990 |
|
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 |
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
|
2993 |
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
|
2994 |
*/ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2995 |
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
|
2996 |
{ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2997 |
mp_err res; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2998 |
mp_digit dshift, bshift; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
2999 |
mp_digit mask; |
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 |
ARGCHK(mp != NULL, MP_BADARG); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3002 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3003 |
dshift = d / MP_DIGIT_BIT; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3004 |
bshift = d % MP_DIGIT_BIT; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3005 |
/* 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
|
3006 |
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
|
3007 |
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
|
3008 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3009 |
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
|
3010 |
return res; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3011 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3012 |
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
|
3013 |
return res; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3014 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3015 |
if (bshift) { |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3016 |
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
|
3017 |
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
|
3018 |
mp_digit prev = 0; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3019 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3020 |
for (pa += dshift; pa < alim; ) { |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3021 |
mp_digit x = *pa; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3022 |
*pa++ = (x << bshift) | prev; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3023 |
prev = x >> (DIGIT_BIT - bshift); |
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 |
} |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3026 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3027 |
s_mp_clamp(mp); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3028 |
return MP_OKAY; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3029 |
} /* end s_mp_mul_2d() */ |
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 |
/* {{{ s_mp_rshd(mp, p) */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3032 |
|
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 |
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
|
3035 |
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
|
3036 |
end are lost. Cannot fail. |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3037 |
*/ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3038 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3039 |
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
|
3040 |
{ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3041 |
mp_size ix; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3042 |
mp_digit *src, *dst; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3043 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3044 |
if(p == 0) |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3045 |
return; |
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 |
/* 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
|
3048 |
if(p >= USED(mp)) { |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3049 |
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
|
3050 |
USED(mp) = 1; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3051 |
SIGN(mp) = ZPOS; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3052 |
return; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3053 |
} |
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 |
/* 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
|
3056 |
dst = MP_DIGITS(mp); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3057 |
src = dst + p; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3058 |
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
|
3059 |
*dst++ = *src++; |
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 |
MP_USED(mp) -= p; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3062 |
/* 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
|
3063 |
while (p-- > 0) |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3064 |
*dst++ = 0; |
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 0 |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3067 |
/* Strip off any leading zeroes */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3068 |
s_mp_clamp(mp); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3069 |
#endif |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3070 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3071 |
} /* end s_mp_rshd() */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3072 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3073 |
/* }}} */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3074 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3075 |
/* {{{ s_mp_div_2(mp) */ |
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 |
/* 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
|
3078 |
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
|
3079 |
{ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3080 |
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
|
3081 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3082 |
} /* end s_mp_div_2() */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3083 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3084 |
/* }}} */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3085 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3086 |
/* {{{ s_mp_mul_2(mp) */ |
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 |
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
|
3089 |
{ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3090 |
mp_digit *pd; |
8175
27e80dc06104
6989705: ECC security code native code compiler warnings
vinnie
parents:
5506
diff
changeset
|
3091 |
unsigned int ix, used; |
3863
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3092 |
mp_digit kin = 0; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3093 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3094 |
/* 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
|
3095 |
used = MP_USED(mp); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3096 |
pd = MP_DIGITS(mp); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3097 |
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
|
3098 |
mp_digit d = *pd; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3099 |
*pd++ = (d << 1) | kin; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3100 |
kin = (d >> (DIGIT_BIT - 1)); |
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 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3103 |
/* 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
|
3104 |
if (kin) { |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3105 |
if (ix >= ALLOC(mp)) { |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3106 |
mp_err res; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3107 |
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
|
3108 |
return res; |
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 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3111 |
DIGIT(mp, ix) = kin; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3112 |
USED(mp) += 1; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3113 |
} |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3114 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3115 |
return MP_OKAY; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3116 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3117 |
} /* end s_mp_mul_2() */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3118 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3119 |
/* }}} */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3120 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3121 |
/* {{{ 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
|
3122 |
|
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 |
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
|
3125 |
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
|
3126 |
division code |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3127 |
*/ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3128 |
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
|
3129 |
{ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3130 |
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
|
3131 |
mp_size ix; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3132 |
mp_digit dmask; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3133 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3134 |
if(ndig >= USED(mp)) |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3135 |
return; |
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 |
/* 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
|
3138 |
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
|
3139 |
DIGIT(mp, ndig) &= dmask; |
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 |
/* 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
|
3142 |
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
|
3143 |
DIGIT(mp, ix) = 0; |
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 |
s_mp_clamp(mp); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3146 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3147 |
} /* end s_mp_mod_2d() */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3148 |
|
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 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3151 |
/* {{{ 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
|
3152 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3153 |
/* |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3154 |
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
|
3155 |
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
|
3156 |
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
|
3157 |
*/ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3158 |
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
|
3159 |
{ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3160 |
int ix; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3161 |
mp_digit save, next, mask; |
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 |
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
|
3164 |
d %= DIGIT_BIT; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3165 |
if (d) { |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3166 |
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
|
3167 |
save = 0; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3168 |
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
|
3169 |
next = DIGIT(mp, ix) & mask; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3170 |
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
|
3171 |
save = next; |
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 |
} |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3174 |
s_mp_clamp(mp); |
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 |
} /* end s_mp_div_2d() */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3177 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3178 |
/* }}} */ |
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 |
/* {{{ 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
|
3181 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3182 |
/* |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3183 |
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
|
3184 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3185 |
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
|
3186 |
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
|
3187 |
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
|
3188 |
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
|
3189 |
(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
|
3190 |
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
|
3191 |
*/ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3192 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3193 |
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
|
3194 |
{ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3195 |
mp_digit d; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3196 |
mp_digit mask; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3197 |
mp_digit b_msd; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3198 |
mp_err res = MP_OKAY; |
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 |
d = 0; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3201 |
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
|
3202 |
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
|
3203 |
while (!(b_msd & mask)) { |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3204 |
b_msd <<= 1; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3205 |
++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 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3208 |
if (d) { |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3209 |
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
|
3210 |
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
|
3211 |
} |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3212 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3213 |
*pd = d; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3214 |
CLEANUP: |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3215 |
return res; |
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 |
} /* end s_mp_norm() */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3218 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3219 |
/* }}} */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3220 |
|
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 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3223 |
/* {{{ Primitive digit arithmetic */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3224 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3225 |
/* {{{ 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
|
3226 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3227 |
/* 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
|
3228 |
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
|
3229 |
{ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3230 |
#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
|
3231 |
mp_word w, k = 0; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3232 |
mp_size ix = 1; |
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 |
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
|
3235 |
DIGIT(mp, 0) = ACCUM(w); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3236 |
k = CARRYOUT(w); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3237 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3238 |
while(ix < USED(mp) && k) { |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3239 |
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
|
3240 |
DIGIT(mp, ix) = ACCUM(w); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3241 |
k = CARRYOUT(w); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3242 |
++ix; |
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 |
if(k != 0) { |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3246 |
mp_err res; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3247 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3248 |
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
|
3249 |
return res; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3250 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3251 |
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
|
3252 |
} |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3253 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3254 |
return MP_OKAY; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3255 |
#else |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3256 |
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
|
3257 |
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
|
3258 |
mp_err res = MP_OKAY; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3259 |
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
|
3260 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3261 |
mp_i = *pmp; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3262 |
*pmp++ = sum = d + mp_i; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3263 |
carry = (sum < d); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3264 |
while (carry && --used > 0) { |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3265 |
mp_i = *pmp; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3266 |
*pmp++ = sum = carry + mp_i; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3267 |
carry = !sum; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3268 |
} |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3269 |
if (carry && !used) { |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3270 |
/* mp is growing */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3271 |
used = MP_USED(mp); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3272 |
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
|
3273 |
MP_DIGIT(mp, used) = carry; |
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 |
CLEANUP: |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3276 |
return res; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3277 |
#endif |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3278 |
} /* end s_mp_add_d() */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3279 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3280 |
/* }}} */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3281 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3282 |
/* {{{ 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
|
3283 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3284 |
/* 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
|
3285 |
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
|
3286 |
{ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3287 |
#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
|
3288 |
mp_word w, b = 0; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3289 |
mp_size ix = 1; |
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 |
/* Compute initial subtraction */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3292 |
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
|
3293 |
b = CARRYOUT(w) ? 0 : 1; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3294 |
DIGIT(mp, 0) = ACCUM(w); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3295 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3296 |
/* Propagate borrows leftward */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3297 |
while(b && ix < USED(mp)) { |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3298 |
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
|
3299 |
b = CARRYOUT(w) ? 0 : 1; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3300 |
DIGIT(mp, ix) = ACCUM(w); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3301 |
++ix; |
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 |
/* Remove leading zeroes */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3305 |
s_mp_clamp(mp); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3306 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3307 |
/* 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
|
3308 |
if(b) |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3309 |
return MP_RANGE; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3310 |
else |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3311 |
return MP_OKAY; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3312 |
#else |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3313 |
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
|
3314 |
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
|
3315 |
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
|
3316 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3317 |
mp_i = *pmp; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3318 |
*pmp++ = diff = mp_i - d; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3319 |
borrow = (diff > mp_i); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3320 |
while (borrow && --used) { |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3321 |
mp_i = *pmp; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3322 |
*pmp++ = diff = mp_i - borrow; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3323 |
borrow = (diff > mp_i); |
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 |
s_mp_clamp(mp); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3326 |
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
|
3327 |
#endif |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3328 |
} /* end s_mp_sub_d() */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3329 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3330 |
/* }}} */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3331 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3332 |
/* {{{ 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
|
3333 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3334 |
/* 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
|
3335 |
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
|
3336 |
{ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3337 |
mp_err res; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3338 |
mp_size used; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3339 |
int pow; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3340 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3341 |
if (!d) { |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3342 |
mp_zero(a); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3343 |
return MP_OKAY; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3344 |
} |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3345 |
if (d == 1) |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3346 |
return MP_OKAY; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3347 |
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
|
3348 |
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
|
3349 |
} |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3350 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3351 |
used = MP_USED(a); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3352 |
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
|
3353 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3354 |
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
|
3355 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3356 |
s_mp_clamp(a); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3357 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3358 |
CLEANUP: |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3359 |
return res; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3360 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3361 |
} /* end s_mp_mul_d() */ |
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 |
/* }}} */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3364 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3365 |
/* {{{ 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
|
3366 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3367 |
/* |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3368 |
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
|
3369 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3370 |
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
|
3371 |
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
|
3372 |
*/ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3373 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3374 |
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
|
3375 |
{ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3376 |
#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
|
3377 |
mp_word w = 0, q; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3378 |
#else |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3379 |
mp_digit w, q; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3380 |
#endif |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3381 |
int ix; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3382 |
mp_err res; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3383 |
mp_int quot; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3384 |
mp_int rem; |
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 |
if(d == 0) |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3387 |
return MP_RANGE; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3388 |
if (d == 1) { |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3389 |
if (r) |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3390 |
*r = 0; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3391 |
return MP_OKAY; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3392 |
} |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3393 |
/* 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
|
3394 |
if (MP_USED(mp) == 1) { |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3395 |
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
|
3396 |
mp_digit rem; |
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 |
q = n / d; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3399 |
rem = n % d; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3400 |
MP_DIGIT(mp,0) = q; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3401 |
if (r) |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3402 |
*r = rem; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3403 |
return MP_OKAY; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3404 |
} |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3405 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3406 |
MP_DIGITS(&rem) = 0; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3407 |
MP_DIGITS(") = 0; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3408 |
/* Make room for the quotient */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3409 |
MP_CHECKOK( mp_init_size(", USED(mp), FLAG(mp)) ); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3410 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3411 |
#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
|
3412 |
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
|
3413 |
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
|
3414 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3415 |
if(w >= d) { |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3416 |
q = w / d; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3417 |
w = w % d; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3418 |
} else { |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3419 |
q = 0; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3420 |
} |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3421 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3422 |
s_mp_lshd(", 1); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3423 |
DIGIT(", 0) = (mp_digit)q; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3424 |
} |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3425 |
#else |
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 |
mp_digit p; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3428 |
#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
|
3429 |
mp_digit norm; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3430 |
#endif |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3431 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3432 |
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
|
3433 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3434 |
#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
|
3435 |
MP_DIGIT(", 0) = d; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3436 |
MP_CHECKOK( s_mp_norm(&rem, ", &norm) ); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3437 |
if (norm) |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3438 |
d <<= norm; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3439 |
MP_DIGIT(", 0) = 0; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3440 |
#endif |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3441 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3442 |
p = 0; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3443 |
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
|
3444 |
w = DIGIT(&rem, ix); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3445 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3446 |
if (p) { |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3447 |
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
|
3448 |
} else if (w >= d) { |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3449 |
q = w / d; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3450 |
w = w % d; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3451 |
} else { |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3452 |
q = 0; |
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 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3455 |
MP_CHECKOK( s_mp_lshd(", 1) ); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3456 |
DIGIT(", 0) = q; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3457 |
p = w; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3458 |
} |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3459 |
#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
|
3460 |
if (norm) |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3461 |
w >>= norm; |
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 |
#endif |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3465 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3466 |
/* Deliver the remainder, if desired */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3467 |
if(r) |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3468 |
*r = (mp_digit)w; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3469 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3470 |
s_mp_clamp("); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3471 |
mp_exch(", mp); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3472 |
CLEANUP: |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3473 |
mp_clear("); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3474 |
mp_clear(&rem); |
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 |
return res; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3477 |
} /* end s_mp_div_d() */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3478 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3479 |
/* }}} */ |
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 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3482 |
/* }}} */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3483 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3484 |
/* {{{ Primitive full arithmetic */ |
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 |
/* {{{ s_mp_add(a, b) */ |
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 |
/* Compute a = |a| + |b| */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3489 |
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
|
3490 |
{ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3491 |
#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
|
3492 |
mp_word w = 0; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3493 |
#else |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3494 |
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
|
3495 |
#endif |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3496 |
mp_digit *pa, *pb; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3497 |
mp_size ix; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3498 |
mp_size used; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3499 |
mp_err res; |
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 |
/* 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
|
3502 |
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
|
3503 |
return res; |
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 |
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
|
3507 |
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
|
3508 |
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
|
3509 |
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
|
3510 |
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
|
3511 |
*/ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3512 |
pa = MP_DIGITS(a); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3513 |
pb = MP_DIGITS(b); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3514 |
used = MP_USED(b); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3515 |
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
|
3516 |
#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
|
3517 |
w = w + *pa + *pb++; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3518 |
*pa++ = ACCUM(w); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3519 |
w = CARRYOUT(w); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3520 |
#else |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3521 |
d = *pa; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3522 |
sum = d + *pb++; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3523 |
d = (sum < d); /* detect overflow */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3524 |
*pa++ = sum += carry; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3525 |
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
|
3526 |
#endif |
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 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3529 |
/* 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
|
3530 |
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
|
3531 |
*/ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3532 |
used = MP_USED(a); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3533 |
#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
|
3534 |
while (w && ix < used) { |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3535 |
w = w + *pa; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3536 |
*pa++ = ACCUM(w); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3537 |
w = CARRYOUT(w); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3538 |
++ix; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3539 |
} |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3540 |
#else |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3541 |
while (carry && ix < used) { |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3542 |
sum = carry + *pa; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3543 |
*pa++ = sum; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3544 |
carry = !sum; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3545 |
++ix; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3546 |
} |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3547 |
#endif |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3548 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3549 |
/* 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
|
3550 |
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
|
3551 |
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
|
3552 |
*/ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3553 |
#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
|
3554 |
if (w) { |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3555 |
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
|
3556 |
return res; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3557 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3558 |
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
|
3559 |
} |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3560 |
#else |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3561 |
if (carry) { |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3562 |
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
|
3563 |
return res; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3564 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3565 |
DIGIT(a, used) = carry; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3566 |
} |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3567 |
#endif |
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 |
return MP_OKAY; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3570 |
} /* end s_mp_add() */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3571 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3572 |
/* }}} */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3573 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3574 |
/* 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
|
3575 |
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
|
3576 |
{ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3577 |
mp_digit *pa, *pb, *pc; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3578 |
#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
|
3579 |
mp_word w = 0; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3580 |
#else |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3581 |
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
|
3582 |
#endif |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3583 |
mp_size ix; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3584 |
mp_size used; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3585 |
mp_err 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 |
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
|
3588 |
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
|
3589 |
const mp_int *xch = a; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3590 |
a = b; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3591 |
b = xch; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3592 |
} |
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 |
/* 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
|
3595 |
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
|
3596 |
return res; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3597 |
|
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 |
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
|
3600 |
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
|
3601 |
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
|
3602 |
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
|
3603 |
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
|
3604 |
*/ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3605 |
pa = MP_DIGITS(a); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3606 |
pb = MP_DIGITS(b); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3607 |
pc = MP_DIGITS(c); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3608 |
used = MP_USED(b); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3609 |
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
|
3610 |
#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
|
3611 |
w = w + *pa++ + *pb++; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3612 |
*pc++ = ACCUM(w); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3613 |
w = CARRYOUT(w); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3614 |
#else |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3615 |
d = *pa++; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3616 |
sum = d + *pb++; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3617 |
d = (sum < d); /* detect overflow */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3618 |
*pc++ = sum += carry; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3619 |
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
|
3620 |
#endif |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3621 |
} |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3622 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3623 |
/* 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
|
3624 |
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
|
3625 |
*/ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3626 |
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
|
3627 |
#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
|
3628 |
w = w + *pa++; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3629 |
*pc++ = ACCUM(w); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3630 |
w = CARRYOUT(w); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3631 |
#else |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3632 |
*pc++ = sum = carry + *pa++; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3633 |
carry = (sum < carry); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3634 |
#endif |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3635 |
} |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3636 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3637 |
/* 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
|
3638 |
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
|
3639 |
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
|
3640 |
*/ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3641 |
#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
|
3642 |
if (w) { |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3643 |
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
|
3644 |
return res; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3645 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3646 |
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
|
3647 |
++used; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3648 |
} |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3649 |
#else |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3650 |
if (carry) { |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3651 |
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
|
3652 |
return res; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3653 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3654 |
DIGIT(c, used) = carry; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3655 |
++used; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3656 |
} |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3657 |
#endif |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3658 |
MP_USED(c) = used; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3659 |
return MP_OKAY; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3660 |
} |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3661 |
/* {{{ 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
|
3662 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3663 |
/* 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
|
3664 |
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
|
3665 |
{ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3666 |
#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
|
3667 |
mp_word w, k = 0; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3668 |
#else |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3669 |
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
|
3670 |
#endif |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3671 |
mp_size ib; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3672 |
mp_size ia; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3673 |
mp_size lim; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3674 |
mp_err 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 |
/* 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
|
3677 |
lim = MP_USED(b) + offset; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3678 |
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
|
3679 |
return res; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3680 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3681 |
/* |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3682 |
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
|
3683 |
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
|
3684 |
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
|
3685 |
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
|
3686 |
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
|
3687 |
*/ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3688 |
lim = USED(b); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3689 |
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
|
3690 |
#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
|
3691 |
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
|
3692 |
DIGIT(a, ia) = ACCUM(w); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3693 |
k = CARRYOUT(w); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3694 |
#else |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3695 |
d = MP_DIGIT(a, ia); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3696 |
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
|
3697 |
d = (sum < d); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3698 |
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
|
3699 |
carry = d + (sum < carry); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3700 |
#endif |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3701 |
} |
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 |
/* 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
|
3704 |
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
|
3705 |
*/ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3706 |
#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
|
3707 |
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
|
3708 |
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
|
3709 |
DIGIT(a, ia) = ACCUM(w); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3710 |
k = CARRYOUT(w); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3711 |
} |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3712 |
#else |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3713 |
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
|
3714 |
d = MP_DIGIT(a, ia); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3715 |
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
|
3716 |
carry = (sum < d); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3717 |
} |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3718 |
#endif |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3719 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3720 |
/* 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
|
3721 |
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
|
3722 |
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
|
3723 |
*/ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3724 |
#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
|
3725 |
if(k) { |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3726 |
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
|
3727 |
return res; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3728 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3729 |
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
|
3730 |
} |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3731 |
#else |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3732 |
if (carry) { |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3733 |
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
|
3734 |
return res; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3735 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3736 |
DIGIT(a, lim) = carry; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3737 |
} |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3738 |
#endif |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3739 |
s_mp_clamp(a); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3740 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3741 |
return MP_OKAY; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3742 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3743 |
} /* end s_mp_add_offset() */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3744 |
|
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 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3747 |
/* {{{ s_mp_sub(a, b) */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3748 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3749 |
/* 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
|
3750 |
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
|
3751 |
{ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3752 |
mp_digit *pa, *pb, *limit; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3753 |
#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
|
3754 |
mp_sword w = 0; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3755 |
#else |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3756 |
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
|
3757 |
#endif |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3758 |
|
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 |
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
|
3761 |
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
|
3762 |
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
|
3763 |
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
|
3764 |
*/ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3765 |
pa = MP_DIGITS(a); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3766 |
pb = MP_DIGITS(b); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3767 |
limit = pb + MP_USED(b); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3768 |
while (pb < limit) { |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3769 |
#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
|
3770 |
w = w + *pa - *pb++; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3771 |
*pa++ = ACCUM(w); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3772 |
w >>= MP_DIGIT_BIT; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3773 |
#else |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3774 |
d = *pa; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3775 |
diff = d - *pb++; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3776 |
d = (diff > d); /* detect borrow */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3777 |
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
|
3778 |
++d; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3779 |
*pa++ = diff; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3780 |
borrow = d; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3781 |
#endif |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3782 |
} |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3783 |
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
|
3784 |
#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
|
3785 |
while (w && pa < limit) { |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3786 |
w = w + *pa; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3787 |
*pa++ = ACCUM(w); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3788 |
w >>= MP_DIGIT_BIT; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3789 |
} |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3790 |
#else |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3791 |
while (borrow && pa < limit) { |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3792 |
d = *pa; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3793 |
*pa++ = diff = d - borrow; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3794 |
borrow = (diff > d); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3795 |
} |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3796 |
#endif |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3797 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3798 |
/* 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
|
3799 |
s_mp_clamp(a); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3800 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3801 |
/* |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3802 |
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
|
3803 |
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
|
3804 |
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
|
3805 |
*/ |
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 |
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
|
3808 |
#else |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3809 |
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
|
3810 |
#endif |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3811 |
} /* end s_mp_sub() */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3812 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3813 |
/* }}} */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3814 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3815 |
/* 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
|
3816 |
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
|
3817 |
{ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3818 |
mp_digit *pa, *pb, *pc; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3819 |
#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
|
3820 |
mp_sword w = 0; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3821 |
#else |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3822 |
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
|
3823 |
#endif |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3824 |
int ix, limit; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3825 |
mp_err res; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3826 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3827 |
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
|
3828 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3829 |
/* 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
|
3830 |
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
|
3831 |
return res; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3832 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3833 |
/* |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3834 |
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
|
3835 |
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
|
3836 |
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
|
3837 |
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
|
3838 |
*/ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3839 |
pa = MP_DIGITS(a); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3840 |
pb = MP_DIGITS(b); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3841 |
pc = MP_DIGITS(c); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3842 |
limit = MP_USED(b); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3843 |
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
|
3844 |
#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
|
3845 |
w = w + *pa++ - *pb++; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3846 |
*pc++ = ACCUM(w); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3847 |
w >>= MP_DIGIT_BIT; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3848 |
#else |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3849 |
d = *pa++; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3850 |
diff = d - *pb++; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3851 |
d = (diff > d); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3852 |
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
|
3853 |
++d; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3854 |
*pc++ = diff; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3855 |
borrow = d; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3856 |
#endif |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3857 |
} |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3858 |
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
|
3859 |
#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
|
3860 |
w = w + *pa++; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3861 |
*pc++ = ACCUM(w); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3862 |
w >>= MP_DIGIT_BIT; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3863 |
#else |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3864 |
d = *pa++; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3865 |
*pc++ = diff = d - borrow; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3866 |
borrow = (diff > d); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3867 |
#endif |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3868 |
} |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3869 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3870 |
/* 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
|
3871 |
MP_USED(c) = ix; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3872 |
s_mp_clamp(c); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3873 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3874 |
/* |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3875 |
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
|
3876 |
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
|
3877 |
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
|
3878 |
*/ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3879 |
#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
|
3880 |
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
|
3881 |
#else |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3882 |
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
|
3883 |
#endif |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3884 |
} |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3885 |
/* {{{ s_mp_mul(a, b) */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3886 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3887 |
/* Compute a = |a| * |b| */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3888 |
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
|
3889 |
{ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3890 |
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
|
3891 |
} /* end s_mp_mul() */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3892 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3893 |
/* }}} */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3894 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3895 |
#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
|
3896 |
/* 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
|
3897 |
#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
|
3898 |
{ 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
|
3899 |
Plo = (mp_digit)product; \ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3900 |
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
|
3901 |
#elif defined(OSF1) |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3902 |
#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
|
3903 |
{ 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
|
3904 |
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
|
3905 |
#else |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3906 |
#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
|
3907 |
{ mp_digit a0b1, a1b0; \ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3908 |
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
|
3909 |
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
|
3910 |
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
|
3911 |
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
|
3912 |
a1b0 += a0b1; \ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3913 |
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
|
3914 |
if (a1b0 < a0b1) \ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3915 |
Phi += MP_HALF_RADIX; \ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3916 |
a1b0 <<= MP_HALF_DIGIT_BIT; \ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3917 |
Plo += a1b0; \ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3918 |
if (Plo < a1b0) \ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3919 |
++Phi; \ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3920 |
} |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3921 |
#endif |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3922 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3923 |
#if !defined(MP_ASSEMBLY_MULTIPLY) |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3924 |
/* c = a * b */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3925 |
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
|
3926 |
{ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3927 |
#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
|
3928 |
mp_digit d = 0; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3929 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3930 |
/* Inner product: Digits of a */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3931 |
while (a_len--) { |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3932 |
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
|
3933 |
*c++ = ACCUM(w); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3934 |
d = CARRYOUT(w); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3935 |
} |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3936 |
*c = d; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3937 |
#else |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3938 |
mp_digit carry = 0; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3939 |
while (a_len--) { |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3940 |
mp_digit a_i = *a++; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3941 |
mp_digit a0b0, a1b1; |
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 |
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
|
3944 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3945 |
a0b0 += carry; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3946 |
if (a0b0 < carry) |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3947 |
++a1b1; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3948 |
*c++ = a0b0; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3949 |
carry = a1b1; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3950 |
} |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3951 |
*c = carry; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3952 |
#endif |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3953 |
} |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3954 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3955 |
/* c += a * b */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3956 |
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
|
3957 |
mp_digit *c) |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3958 |
{ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3959 |
#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
|
3960 |
mp_digit d = 0; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3961 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3962 |
/* Inner product: Digits of a */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3963 |
while (a_len--) { |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3964 |
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
|
3965 |
*c++ = ACCUM(w); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3966 |
d = CARRYOUT(w); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3967 |
} |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3968 |
*c = d; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3969 |
#else |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3970 |
mp_digit carry = 0; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3971 |
while (a_len--) { |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3972 |
mp_digit a_i = *a++; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3973 |
mp_digit a0b0, a1b1; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3974 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3975 |
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
|
3976 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3977 |
a0b0 += carry; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3978 |
if (a0b0 < carry) |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3979 |
++a1b1; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3980 |
a0b0 += a_i = *c; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3981 |
if (a0b0 < a_i) |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3982 |
++a1b1; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3983 |
*c++ = a0b0; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3984 |
carry = a1b1; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3985 |
} |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3986 |
*c = carry; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3987 |
#endif |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3988 |
} |
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 |
/* 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
|
3991 |
/* c += a * b */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3992 |
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
|
3993 |
{ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3994 |
#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
|
3995 |
mp_digit d = 0; |
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 |
/* Inner product: Digits of a */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3998 |
while (a_len--) { |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
3999 |
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
|
4000 |
*c++ = ACCUM(w); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4001 |
d = CARRYOUT(w); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4002 |
} |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4003 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4004 |
while (d) { |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4005 |
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
|
4006 |
*c++ = ACCUM(w); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4007 |
d = CARRYOUT(w); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4008 |
} |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4009 |
#else |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4010 |
mp_digit carry = 0; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4011 |
while (a_len--) { |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4012 |
mp_digit a_i = *a++; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4013 |
mp_digit a0b0, a1b1; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4014 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4015 |
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
|
4016 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4017 |
a0b0 += carry; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4018 |
if (a0b0 < carry) |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4019 |
++a1b1; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4020 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4021 |
a0b0 += a_i = *c; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4022 |
if (a0b0 < a_i) |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4023 |
++a1b1; |
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 |
*c++ = a0b0; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4026 |
carry = a1b1; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4027 |
} |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4028 |
while (carry) { |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4029 |
mp_digit c_i = *c; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4030 |
carry += c_i; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4031 |
*c++ = carry; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4032 |
carry = carry < c_i; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4033 |
} |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4034 |
#endif |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4035 |
} |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4036 |
#endif |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4037 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4038 |
#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
|
4039 |
/* 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
|
4040 |
#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
|
4041 |
{ 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
|
4042 |
Plo = (mp_digit)square; \ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4043 |
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
|
4044 |
#elif defined(OSF1) |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4045 |
#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
|
4046 |
{ 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
|
4047 |
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
|
4048 |
#else |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4049 |
#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
|
4050 |
{ mp_digit Pmid; \ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4051 |
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
|
4052 |
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
|
4053 |
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
|
4054 |
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
|
4055 |
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
|
4056 |
Plo += Pmid; \ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4057 |
if (Plo < Pmid) \ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4058 |
++Phi; \ |
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 |
#endif |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4061 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4062 |
#if !defined(MP_ASSEMBLY_SQUARE) |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4063 |
/* 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
|
4064 |
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
|
4065 |
{ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4066 |
#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
|
4067 |
mp_word w; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4068 |
mp_digit d; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4069 |
mp_size ix; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4070 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4071 |
w = 0; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4072 |
#define ADD_SQUARE(n) \ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4073 |
d = pa[n]; \ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4074 |
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
|
4075 |
ps[2*n] = ACCUM(w); \ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4076 |
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
|
4077 |
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
|
4078 |
w = (w >> DIGIT_BIT) |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4079 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4080 |
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
|
4081 |
ADD_SQUARE(0); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4082 |
ADD_SQUARE(1); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4083 |
ADD_SQUARE(2); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4084 |
ADD_SQUARE(3); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4085 |
pa += 4; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4086 |
ps += 8; |
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 (ix) { |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4089 |
ps += 2*ix; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4090 |
pa += ix; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4091 |
switch (ix) { |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4092 |
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
|
4093 |
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
|
4094 |
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
|
4095 |
case 0: break; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4096 |
} |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4097 |
} |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4098 |
while (w) { |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4099 |
w += *ps; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4100 |
*ps++ = ACCUM(w); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4101 |
w = (w >> DIGIT_BIT); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4102 |
} |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4103 |
#else |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4104 |
mp_digit carry = 0; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4105 |
while (a_len--) { |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4106 |
mp_digit a_i = *pa++; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4107 |
mp_digit a0a0, a1a1; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4108 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4109 |
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
|
4110 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4111 |
/* 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
|
4112 |
a0a0 += carry; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4113 |
if (a0a0 < carry) |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4114 |
++a1a1; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4115 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4116 |
/* now add to ps */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4117 |
a0a0 += a_i = *ps; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4118 |
if (a0a0 < a_i) |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4119 |
++a1a1; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4120 |
*ps++ = a0a0; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4121 |
a1a1 += a_i = *ps; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4122 |
carry = (a1a1 < a_i); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4123 |
*ps++ = a1a1; |
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 |
while (carry) { |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4126 |
mp_digit s_i = *ps; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4127 |
carry += s_i; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4128 |
*ps++ = carry; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4129 |
carry = carry < s_i; |
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 |
#endif |
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 |
#endif |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4134 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4135 |
#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
|
4136 |
&& !defined(MP_ASSEMBLY_DIV_2DX1D) |
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 |
** 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
|
4139 |
** 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
|
4140 |
*/ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4141 |
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
|
4142 |
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
|
4143 |
{ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4144 |
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
|
4145 |
mp_digit r1, r0, m; |
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 |
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
|
4148 |
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
|
4149 |
r1 = Nhi % d1; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4150 |
q1 = Nhi / d1; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4151 |
m = q1 * d0; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4152 |
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
|
4153 |
if (r1 < m) { |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4154 |
q1--, r1 += divisor; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4155 |
if (r1 >= divisor && r1 < m) { |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4156 |
q1--, r1 += divisor; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4157 |
} |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4158 |
} |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4159 |
r1 -= m; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4160 |
r0 = r1 % d1; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4161 |
q0 = r1 / d1; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4162 |
m = q0 * d0; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4163 |
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
|
4164 |
if (r0 < m) { |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4165 |
q0--, r0 += divisor; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4166 |
if (r0 >= divisor && r0 < m) { |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4167 |
q0--, r0 += divisor; |
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 |
} |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4170 |
if (qp) |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4171 |
*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
|
4172 |
if (rp) |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4173 |
*rp = r0 - m; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4174 |
return MP_OKAY; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4175 |
} |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4176 |
#endif |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4177 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4178 |
#if MP_SQUARE |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4179 |
/* {{{ s_mp_sqr(a) */ |
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 |
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
|
4182 |
{ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4183 |
mp_err res; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4184 |
mp_int tmp; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4185 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4186 |
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
|
4187 |
return res; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4188 |
res = mp_sqr(a, &tmp); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4189 |
if (res == MP_OKAY) { |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4190 |
s_mp_exch(&tmp, a); |
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 |
mp_clear(&tmp); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4193 |
return res; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4194 |
} |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4195 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4196 |
/* }}} */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4197 |
#endif |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4198 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4199 |
/* {{{ s_mp_div(a, b) */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4200 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4201 |
/* |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4202 |
s_mp_div(a, b) |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4203 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4204 |
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
|
4205 |
*/ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4206 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4207 |
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
|
4208 |
mp_int *div, /* i: divisor */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4209 |
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
|
4210 |
{ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4211 |
mp_int part, t; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4212 |
#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
|
4213 |
mp_word q_msd; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4214 |
#else |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4215 |
mp_digit q_msd; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4216 |
#endif |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4217 |
mp_err res; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4218 |
mp_digit d; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4219 |
mp_digit div_msd; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4220 |
int ix; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4221 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4222 |
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
|
4223 |
return MP_RANGE; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4224 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4225 |
/* 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
|
4226 |
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
|
4227 |
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
|
4228 |
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
|
4229 |
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
|
4230 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4231 |
return MP_OKAY; |
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 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4234 |
DIGITS(&t) = 0; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4235 |
MP_SIGN(rem) = ZPOS; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4236 |
MP_SIGN(div) = ZPOS; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4237 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4238 |
/* A working temporary for division */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4239 |
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
|
4240 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4241 |
/* Normalize to optimize guessing */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4242 |
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
|
4243 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4244 |
part = *rem; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4245 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4246 |
/* Perform the division itself...woo! */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4247 |
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
|
4248 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4249 |
/* 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
|
4250 |
/* 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
|
4251 |
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
|
4252 |
int i; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4253 |
int unusedRem; |
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 |
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
|
4256 |
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
|
4257 |
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
|
4258 |
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
|
4259 |
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
|
4260 |
-- unusedRem; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4261 |
#if MP_ARGCHK == 2 |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4262 |
assert(unusedRem >= 0); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4263 |
#endif |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4264 |
-- MP_DIGITS(&part); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4265 |
++ MP_USED(&part); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4266 |
++ MP_ALLOC(&part); |
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 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4269 |
/* 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
|
4270 |
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
|
4271 |
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
|
4272 |
if (q_msd >= div_msd) { |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4273 |
q_msd = 1; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4274 |
} 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
|
4275 |
#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
|
4276 |
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
|
4277 |
q_msd /= div_msd; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4278 |
if (q_msd == RADIX) |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4279 |
--q_msd; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4280 |
#else |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4281 |
mp_digit r; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4282 |
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
|
4283 |
div_msd, &q_msd, &r) ); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4284 |
#endif |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4285 |
} else { |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4286 |
q_msd = 0; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4287 |
} |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4288 |
#if MP_ARGCHK == 2 |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4289 |
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
|
4290 |
#endif |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4291 |
if (q_msd <= 0) |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4292 |
break; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4293 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4294 |
/* 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
|
4295 |
mp_copy(div, &t); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4296 |
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
|
4297 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4298 |
/* |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4299 |
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
|
4300 |
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
|
4301 |
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
|
4302 |
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
|
4303 |
* 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
|
4304 |
*/ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4305 |
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
|
4306 |
--q_msd; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4307 |
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
|
4308 |
} |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4309 |
if (i < 0) { |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4310 |
res = MP_RANGE; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4311 |
goto CLEANUP; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4312 |
} |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4313 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4314 |
/* 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
|
4315 |
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
|
4316 |
s_mp_clamp(rem); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4317 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4318 |
/* |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4319 |
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
|
4320 |
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
|
4321 |
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
|
4322 |
*/ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4323 |
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
|
4324 |
} |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4325 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4326 |
/* Denormalize remainder */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4327 |
if (d) { |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4328 |
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
|
4329 |
} |
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 |
s_mp_clamp(quot); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4332 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4333 |
CLEANUP: |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4334 |
mp_clear(&t); |
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 |
return res; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4337 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4338 |
} /* end s_mp_div() */ |
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 |
/* }}} */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4342 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4343 |
/* {{{ s_mp_2expt(a, k) */ |
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_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
|
4346 |
{ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4347 |
mp_err res; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4348 |
mp_size dig, bit; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4349 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4350 |
dig = k / DIGIT_BIT; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4351 |
bit = k % DIGIT_BIT; |
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 |
mp_zero(a); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4354 |
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
|
4355 |
return res; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4356 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4357 |
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
|
4358 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4359 |
return MP_OKAY; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4360 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4361 |
} /* end s_mp_2expt() */ |
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_reduce(x, m, mu) */ |
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 |
/* |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4368 |
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
|
4369 |
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
|
4370 |
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
|
4371 |
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
|
4372 |
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
|
4373 |
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
|
4374 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4375 |
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
|
4376 |
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
|
4377 |
pp. 603-604. |
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 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4380 |
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
|
4381 |
{ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4382 |
mp_int q; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4383 |
mp_err res; |
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 |
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
|
4386 |
return res; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4387 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4388 |
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
|
4389 |
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
|
4390 |
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
|
4391 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4392 |
/* 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
|
4393 |
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
|
4394 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4395 |
/* 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
|
4396 |
s_mp_mul(&q, m); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4397 |
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
|
4398 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4399 |
/* x = x - q */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4400 |
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
|
4401 |
goto CLEANUP; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4402 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4403 |
/* 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
|
4404 |
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
|
4405 |
mp_set(&q, 1); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4406 |
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
|
4407 |
goto CLEANUP; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4408 |
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
|
4409 |
goto CLEANUP; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4410 |
} |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4411 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4412 |
/* 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
|
4413 |
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
|
4414 |
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
|
4415 |
break; |
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 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4418 |
CLEANUP: |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4419 |
mp_clear(&q); |
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 |
return res; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4422 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4423 |
} /* end s_mp_reduce() */ |
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 |
/* }}} */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4426 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4427 |
/* }}} */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4428 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4429 |
/* {{{ Primitive comparisons */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4430 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4431 |
/* {{{ s_mp_cmp(a, b) */ |
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 |
/* 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
|
4434 |
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
|
4435 |
{ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4436 |
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
|
4437 |
{ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4438 |
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
|
4439 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4440 |
if (used_a > used_b) |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4441 |
goto IS_GT; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4442 |
if (used_a < used_b) |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4443 |
goto IS_LT; |
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 |
{ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4446 |
mp_digit *pa, *pb; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4447 |
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
|
4448 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4449 |
#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
|
4450 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4451 |
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
|
4452 |
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
|
4453 |
while (used_a >= 4) { |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4454 |
pa -= 4; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4455 |
pb -= 4; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4456 |
used_a -= 4; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4457 |
CMP_AB(3); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4458 |
CMP_AB(2); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4459 |
CMP_AB(1); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4460 |
CMP_AB(0); |
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 |
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
|
4463 |
/* do nothing */; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4464 |
done: |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4465 |
if (da > db) |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4466 |
goto IS_GT; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4467 |
if (da < db) |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4468 |
goto IS_LT; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4469 |
} |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4470 |
return MP_EQ; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4471 |
IS_LT: |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4472 |
return MP_LT; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4473 |
IS_GT: |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4474 |
return MP_GT; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4475 |
} /* end s_mp_cmp() */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4476 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4477 |
/* }}} */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4478 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4479 |
/* {{{ 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
|
4480 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4481 |
/* 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
|
4482 |
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
|
4483 |
{ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4484 |
if(USED(a) > 1) |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4485 |
return MP_GT; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4486 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4487 |
if(DIGIT(a, 0) < d) |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4488 |
return MP_LT; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4489 |
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
|
4490 |
return MP_GT; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4491 |
else |
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 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4494 |
} /* end s_mp_cmp_d() */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4495 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4496 |
/* }}} */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4497 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4498 |
/* {{{ s_mp_ispow2(v) */ |
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 |
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
|
4502 |
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
|
4503 |
*/ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4504 |
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
|
4505 |
{ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4506 |
mp_digit d; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4507 |
int extra = 0, ix; |
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 |
ix = MP_USED(v) - 1; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4510 |
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
|
4511 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4512 |
extra = s_mp_ispow2d(d); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4513 |
if (extra < 0 || ix == 0) |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4514 |
return extra; |
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 |
while (--ix >= 0) { |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4517 |
if (DIGIT(v, ix) != 0) |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4518 |
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
|
4519 |
extra += MP_DIGIT_BIT; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4520 |
} |
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 |
return extra; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4523 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4524 |
} /* end s_mp_ispow2() */ |
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 |
/* }}} */ |
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 |
/* {{{ s_mp_ispow2d(d) */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4529 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4530 |
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
|
4531 |
{ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4532 |
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
|
4533 |
int pow = 0; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4534 |
#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
|
4535 |
if (d & 0xffff0000U) |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4536 |
pow += 16; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4537 |
if (d & 0xff00ff00U) |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4538 |
pow += 8; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4539 |
if (d & 0xf0f0f0f0U) |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4540 |
pow += 4; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4541 |
if (d & 0xccccccccU) |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4542 |
pow += 2; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4543 |
if (d & 0xaaaaaaaaU) |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4544 |
pow += 1; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4545 |
#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
|
4546 |
if (d & 0xffffffff00000000ULL) |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4547 |
pow += 32; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4548 |
if (d & 0xffff0000ffff0000ULL) |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4549 |
pow += 16; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4550 |
if (d & 0xff00ff00ff00ff00ULL) |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4551 |
pow += 8; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4552 |
if (d & 0xf0f0f0f0f0f0f0f0ULL) |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4553 |
pow += 4; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4554 |
if (d & 0xccccccccccccccccULL) |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4555 |
pow += 2; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4556 |
if (d & 0xaaaaaaaaaaaaaaaaULL) |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4557 |
pow += 1; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4558 |
#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
|
4559 |
if (d & 0xffffffff00000000UL) |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4560 |
pow += 32; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4561 |
if (d & 0xffff0000ffff0000UL) |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4562 |
pow += 16; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4563 |
if (d & 0xff00ff00ff00ff00UL) |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4564 |
pow += 8; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4565 |
if (d & 0xf0f0f0f0f0f0f0f0UL) |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4566 |
pow += 4; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4567 |
if (d & 0xccccccccccccccccUL) |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4568 |
pow += 2; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4569 |
if (d & 0xaaaaaaaaaaaaaaaaUL) |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4570 |
pow += 1; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4571 |
#else |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4572 |
#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
|
4573 |
#endif |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4574 |
return pow; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4575 |
} |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4576 |
return -1; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4577 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4578 |
} /* end s_mp_ispow2d() */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4579 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4580 |
/* }}} */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4581 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4582 |
/* }}} */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4583 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4584 |
/* {{{ Primitive I/O helpers */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4585 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4586 |
/* {{{ s_mp_tovalue(ch, r) */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4587 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4588 |
/* |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4589 |
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
|
4590 |
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
|
4591 |
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
|
4592 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4593 |
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
|
4594 |
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
|
4595 |
*/ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4596 |
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
|
4597 |
{ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4598 |
int val, xch; |
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 |
if(r > 36) |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4601 |
xch = ch; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4602 |
else |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4603 |
xch = toupper(ch); |
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 |
if(isdigit(xch)) |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4606 |
val = xch - '0'; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4607 |
else if(isupper(xch)) |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4608 |
val = xch - 'A' + 10; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4609 |
else if(islower(xch)) |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4610 |
val = xch - 'a' + 36; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4611 |
else if(xch == '+') |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4612 |
val = 62; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4613 |
else if(xch == '/') |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4614 |
val = 63; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4615 |
else |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4616 |
return -1; |
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 |
if(val < 0 || val >= r) |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4619 |
return -1; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4620 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4621 |
return val; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4622 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4623 |
} /* end s_mp_tovalue() */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4624 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4625 |
/* }}} */ |
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 |
/* {{{ 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
|
4628 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4629 |
/* |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4630 |
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
|
4631 |
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
|
4632 |
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
|
4633 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4634 |
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
|
4635 |
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
|
4636 |
*/ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4637 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4638 |
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
|
4639 |
{ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4640 |
char ch; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4641 |
|
8175
27e80dc06104
6989705: ECC security code native code compiler warnings
vinnie
parents:
5506
diff
changeset
|
4642 |
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
|
4643 |
return 0; |
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 |
ch = s_dmap_1[val]; |
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 |
if(r <= 36 && low) |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4648 |
ch = tolower(ch); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4649 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4650 |
return ch; |
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 |
} /* end s_mp_todigit() */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4653 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4654 |
/* }}} */ |
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 |
/* {{{ s_mp_outlen(bits, radix) */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4657 |
|
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 |
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
|
4660 |
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
|
4661 |
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
|
4662 |
*/ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4663 |
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
|
4664 |
{ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4665 |
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
|
4666 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4667 |
} /* end s_mp_outlen() */ |
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 |
/* }}} */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4670 |
|
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 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4673 |
/* {{{ 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
|
4674 |
/* 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
|
4675 |
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
|
4676 |
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
|
4677 |
*/ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4678 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4679 |
mp_err |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4680 |
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
|
4681 |
{ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4682 |
int count; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4683 |
mp_err res; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4684 |
mp_digit d; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4685 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4686 |
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
|
4687 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4688 |
mp_zero(mp); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4689 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4690 |
count = len % sizeof(mp_digit); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4691 |
if (count) { |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4692 |
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
|
4693 |
d = (d << 8) | *str++; |
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_DIGIT(mp, 0) = d; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4696 |
} |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4697 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4698 |
/* 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
|
4699 |
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
|
4700 |
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
|
4701 |
d = (d << 8) | *str++; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4702 |
} |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4703 |
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
|
4704 |
if (!d) |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4705 |
continue; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4706 |
} else { |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4707 |
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
|
4708 |
return res; |
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_DIGIT(mp, 0) = d; |
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 |
return MP_OKAY; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4713 |
} /* end mp_read_unsigned_octets() */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4714 |
/* }}} */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4715 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4716 |
/* {{{ mp_unsigned_octet_size(mp) */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4717 |
int |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4718 |
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
|
4719 |
{ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4720 |
int bytes; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4721 |
int ix; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4722 |
mp_digit d = 0; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4723 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4724 |
ARGCHK(mp != NULL, MP_BADARG); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4725 |
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
|
4726 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4727 |
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
|
4728 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4729 |
/* subtract leading zeros. */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4730 |
/* Iterate over each digit... */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4731 |
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
|
4732 |
d = DIGIT(mp, ix); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4733 |
if (d) |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4734 |
break; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4735 |
bytes -= sizeof(d); |
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 |
if (!bytes) |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4738 |
return 1; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4739 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4740 |
/* 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
|
4741 |
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
|
4742 |
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
|
4743 |
if (x) |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4744 |
break; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4745 |
--bytes; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4746 |
} |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4747 |
return bytes; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4748 |
} /* end mp_unsigned_octet_size() */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4749 |
/* }}} */ |
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 |
/* {{{ 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
|
4752 |
/* 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
|
4753 |
mp_err |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4754 |
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
|
4755 |
{ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4756 |
int ix, pos = 0; |
8175
27e80dc06104
6989705: ECC security code native code compiler warnings
vinnie
parents:
5506
diff
changeset
|
4757 |
unsigned int bytes; |
3863
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 |
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
|
4760 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4761 |
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
|
4762 |
ARGCHK(bytes <= maxlen, MP_BADARG); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4763 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4764 |
/* Iterate over each digit... */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4765 |
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
|
4766 |
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
|
4767 |
int jx; |
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 |
/* 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
|
4770 |
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
|
4771 |
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
|
4772 |
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
|
4773 |
continue; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4774 |
str[pos++] = x; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4775 |
} |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4776 |
} |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4777 |
if (!pos) |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4778 |
str[pos++] = 0; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4779 |
return pos; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4780 |
} /* end mp_to_unsigned_octets() */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4781 |
/* }}} */ |
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 |
/* {{{ 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
|
4784 |
/* 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
|
4785 |
mp_err |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4786 |
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
|
4787 |
{ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4788 |
int ix, pos = 0; |
8175
27e80dc06104
6989705: ECC security code native code compiler warnings
vinnie
parents:
5506
diff
changeset
|
4789 |
unsigned int bytes; |
3863
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 |
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
|
4792 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4793 |
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
|
4794 |
ARGCHK(bytes <= maxlen, MP_BADARG); |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4795 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4796 |
/* Iterate over each digit... */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4797 |
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
|
4798 |
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
|
4799 |
int jx; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4800 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4801 |
/* 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
|
4802 |
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
|
4803 |
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
|
4804 |
if (!pos) { |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4805 |
if (!x) /* suppress leading zeros */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4806 |
continue; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4807 |
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
|
4808 |
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
|
4809 |
if (bytes + 1 > maxlen) |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4810 |
return MP_BADARG; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4811 |
str[pos++] = 0; |
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 |
} |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4814 |
str[pos++] = x; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4815 |
} |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4816 |
} |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4817 |
if (!pos) |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4818 |
str[pos++] = 0; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4819 |
return pos; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4820 |
} /* end mp_to_signed_octets() */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4821 |
/* }}} */ |
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 |
/* {{{ 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
|
4824 |
/* 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
|
4825 |
mp_err |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4826 |
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
|
4827 |
{ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4828 |
int ix, pos = 0; |
8175
27e80dc06104
6989705: ECC security code native code compiler warnings
vinnie
parents:
5506
diff
changeset
|
4829 |
unsigned int bytes; |
3863
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4830 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4831 |
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
|
4832 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4833 |
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
|
4834 |
ARGCHK(bytes <= length, MP_BADARG); |
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 |
/* place any needed leading zeros */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4837 |
for (;length > bytes; --length) { |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4838 |
*str++ = 0; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4839 |
} |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4840 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4841 |
/* Iterate over each digit... */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4842 |
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
|
4843 |
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
|
4844 |
int jx; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4845 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4846 |
/* 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
|
4847 |
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
|
4848 |
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
|
4849 |
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
|
4850 |
continue; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4851 |
str[pos++] = x; |
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 |
} |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4854 |
if (!pos) |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4855 |
str[pos++] = 0; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4856 |
return MP_OKAY; |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4857 |
} /* end mp_to_fixlen_octets() */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4858 |
/* }}} */ |
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4859 |
|
8e0f58b1c072
6884175: CR cleanup for 6840752: Provide out-of-the-box support for ECC algorithms
vinnie
parents:
diff
changeset
|
4860 |
|
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 |
/* HERE THERE BE DRAGONS */ |