File: | ecc-mod.c |
Location: | line 104, column 16 |
Description: | The left operand of '<<' is a garbage value |
1 | /* ecc-mod.c | |||
2 | ||||
3 | Copyright (C) 2013 Niels Möller | |||
4 | ||||
5 | This file is part of GNU Nettle. | |||
6 | ||||
7 | GNU Nettle is free software: you can redistribute it and/or | |||
8 | modify it under the terms of either: | |||
9 | ||||
10 | * the GNU Lesser General Public License as published by the Free | |||
11 | Software Foundation; either version 3 of the License, or (at your | |||
12 | option) any later version. | |||
13 | ||||
14 | or | |||
15 | ||||
16 | * the GNU General Public License as published by the Free | |||
17 | Software Foundation; either version 2 of the License, or (at your | |||
18 | option) any later version. | |||
19 | ||||
20 | or both in parallel, as here. | |||
21 | ||||
22 | GNU Nettle is distributed in the hope that it will be useful, | |||
23 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |||
24 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |||
25 | General Public License for more details. | |||
26 | ||||
27 | You should have received copies of the GNU General Public License and | |||
28 | the GNU Lesser General Public License along with this program. If | |||
29 | not, see http://www.gnu.org/licenses/. | |||
30 | */ | |||
31 | ||||
32 | /* Development of Nettle's ECC support was funded by the .SE Internet Fund. */ | |||
33 | ||||
34 | #if HAVE_CONFIG_H1 | |||
35 | # include "config.h" | |||
36 | #endif | |||
37 | ||||
38 | #include <assert.h> | |||
39 | ||||
40 | #include "ecc-internal.h" | |||
41 | ||||
42 | /* Computes r mod m, input 2*m->size, output m->size. */ | |||
43 | void | |||
44 | ecc_mod_nettle_ecc_mod (const struct ecc_modulo *m, mp_limb_t *rp) | |||
45 | { | |||
46 | mp_limb_t hi; | |||
| ||||
47 | mp_size_t mn = m->size; | |||
48 | mp_size_t bn = m->B_size; | |||
49 | mp_size_t sn = mn - bn; | |||
50 | mp_size_t rn = 2*mn; | |||
51 | mp_size_t i; | |||
52 | unsigned shift; | |||
53 | ||||
54 | assert (sn > 0)((sn > 0) ? (void) (0) : __assert_fail ("sn > 0", "ecc-mod.c" , 54, __PRETTY_FUNCTION__)); | |||
55 | ||||
56 | /* FIXME: Could use mpn_addmul_2. */ | |||
57 | /* Eliminate sn limbs at a time */ | |||
58 | if (m->B[bn-1] < ((mp_limb_t) 1 << (GMP_NUMB_BITS(64 - 0) - 1))) | |||
59 | { | |||
60 | /* Multiply sn + 1 limbs at a time, so we get a mn+1 limb | |||
61 | product. Then we can absorb the carry in the high limb */ | |||
62 | while (rn > 2 * mn - bn) | |||
63 | { | |||
64 | rn -= sn; | |||
65 | ||||
66 | for (i = 0; i <= sn; i++) | |||
67 | rp[rn+i-1] = mpn_addmul_1__gmpn_addmul_1 (rp + rn - mn - 1 + i, m->B, bn, rp[rn+i-1]); | |||
68 | rp[rn-1] = rp[rn+sn-1] | |||
69 | + mpn_add_n__gmpn_add_n (rp + rn - sn - 1, rp + rn - sn - 1, rp + rn - 1, sn); | |||
70 | } | |||
71 | goto final_limbs; | |||
72 | } | |||
73 | else | |||
74 | { | |||
75 | while (rn >= 2 * mn - bn) | |||
76 | { | |||
77 | rn -= sn; | |||
78 | ||||
79 | for (i = 0; i < sn; i++) | |||
80 | rp[rn+i] = mpn_addmul_1__gmpn_addmul_1 (rp + rn - mn + i, m->B, bn, rp[rn+i]); | |||
81 | ||||
82 | hi = mpn_add_n__gmpn_add_n (rp + rn - sn, rp + rn - sn, rp + rn, sn); | |||
83 | hi = cnd_add_n (hi, rp + rn - mn, m->B, mn)__gmpn_cnd_add_n ((hi), (rp + rn - mn), (rp + rn - mn), (m-> B), (mn)); | |||
84 | assert (hi == 0)((hi == 0) ? (void) (0) : __assert_fail ("hi == 0", "ecc-mod.c" , 84, __PRETTY_FUNCTION__)); | |||
85 | } | |||
86 | } | |||
87 | ||||
88 | if (rn > mn) | |||
89 | { | |||
90 | final_limbs: | |||
91 | sn = rn - mn; | |||
92 | ||||
93 | for (i = 0; i < sn; i++) | |||
94 | rp[mn+i] = mpn_addmul_1__gmpn_addmul_1 (rp + i, m->B, bn, rp[mn+i]); | |||
95 | ||||
96 | hi = mpn_add_n__gmpn_add_n (rp + bn, rp + bn, rp + mn, sn); | |||
97 | hi = sec_add_1_nettle_sec_add_1 (rp + bn + sn, rp + bn + sn, mn - bn - sn, hi); | |||
98 | } | |||
99 | ||||
100 | shift = m->size * GMP_NUMB_BITS(64 - 0) - m->bit_size; | |||
101 | if (shift > 0) | |||
102 | { | |||
103 | /* Combine hi with top bits, add in */ | |||
104 | hi = (hi << shift) | (rp[mn-1] >> (GMP_NUMB_BITS(64 - 0) - shift)); | |||
| ||||
105 | rp[mn-1] = (rp[mn-1] & (((mp_limb_t) 1 << (GMP_NUMB_BITS(64 - 0) - shift)) - 1)) | |||
106 | + mpn_addmul_1__gmpn_addmul_1 (rp, m->B_shifted, mn-1, hi); | |||
107 | } | |||
108 | else | |||
109 | { | |||
110 | hi = cnd_add_n (hi, rp, m->B_shifted, mn)__gmpn_cnd_add_n ((hi), (rp), (rp), (m->B_shifted), (mn)); | |||
111 | assert (hi == 0)((hi == 0) ? (void) (0) : __assert_fail ("hi == 0", "ecc-mod.c" , 111, __PRETTY_FUNCTION__)); | |||
112 | } | |||
113 | } |