Line  | Count  | Source (jump to first uncovered line)  | 
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_H  | 
35  |  | # include "config.h"  | 
36  |  | #endif  | 
37  |  |  | 
38  |  | #include <assert.h>  | 
39  |  |  | 
40  |  | #include "ecc-internal.h"  | 
41  |  |  | 
42  |  | /* Computes r <-- x mod m, input 2*m->size, output m->size. It's  | 
43  |  |  * allowed to have rp == xp or rp == xp + m->size, but no other kind  | 
44  |  |  * of overlap is allowed. */  | 
45  |  | void  | 
46  |  | ecc_mod (const struct ecc_modulo *m, mp_limb_t *rp, mp_limb_t *xp)  | 
47  | 0  | { | 
48  | 0  |   mp_limb_t hi;  | 
49  | 0  |   mp_size_t mn = m->size;  | 
50  | 0  |   mp_size_t bn = m->B_size;  | 
51  | 0  |   mp_size_t sn = mn - bn;  | 
52  | 0  |   mp_size_t rn = 2*mn;  | 
53  | 0  |   mp_size_t i;  | 
54  | 0  |   unsigned shift;  | 
55  |  | 
  | 
56  | 0  |   assert (bn < mn);  | 
57  |  |  | 
58  |  |   /* FIXME: Could use mpn_addmul_2. */  | 
59  |  |   /* Eliminate sn limbs at a time */  | 
60  | 0  |   if (m->B[bn-1] < ((mp_limb_t) 1 << (GMP_NUMB_BITS - 1)))  | 
61  | 0  |     { | 
62  |  |       /* Multiply sn + 1 limbs at a time, so we get a mn+1 limb  | 
63  |  |    product. Then we can absorb the carry in the high limb */  | 
64  | 0  |       while (rn > 2 * mn - bn)  | 
65  | 0  |   { | 
66  | 0  |     rn -= sn;  | 
67  |  | 
  | 
68  | 0  |     for (i = 0; i <= sn; i++)  | 
69  | 0  |       xp[rn+i-1] = mpn_addmul_1 (xp + rn - mn - 1 + i, m->B, bn, xp[rn+i-1]);  | 
70  | 0  |     xp[rn-1] = xp[rn+sn-1]  | 
71  | 0  |       + mpn_add_n (xp + rn - sn - 1, xp + rn - sn - 1, xp + rn - 1, sn);  | 
72  | 0  |   }  | 
73  | 0  |     }  | 
74  | 0  |   else  | 
75  | 0  |     { | 
76  | 0  |       while (rn > 2 * mn - bn)  | 
77  | 0  |   { | 
78  | 0  |     rn -= sn;  | 
79  |  | 
  | 
80  | 0  |     for (i = 0; i < sn; i++)  | 
81  | 0  |       xp[rn+i] = mpn_addmul_1 (xp + rn - mn + i, m->B, bn, xp[rn+i]);  | 
82  |  |                | 
83  | 0  |     hi = mpn_add_n (xp + rn - sn, xp + rn - sn, xp + rn, sn);  | 
84  | 0  |     hi = mpn_cnd_add_n (hi, xp + rn - mn, xp + rn - mn, m->B, mn);  | 
85  | 0  |     assert_maybe (hi == 0);  | 
86  | 0  |   }  | 
87  | 0  |     }  | 
88  |  | 
  | 
89  | 0  |   assert (rn > mn);  | 
90  | 0  |   rn -= mn;  | 
91  | 0  |   assert (rn <= sn);  | 
92  |  |  | 
93  | 0  |   for (i = 0; i < rn; i++)  | 
94  | 0  |     xp[mn+i] = mpn_addmul_1 (xp + i, m->B, bn, xp[mn+i]);  | 
95  |  | 
  | 
96  | 0  |   hi = mpn_add_n (xp + bn, xp + bn, xp + mn, rn);  | 
97  | 0  |   if (rn < sn)  | 
98  | 0  |     hi = sec_add_1 (xp + bn + rn, xp + bn + rn, sn - rn, hi);  | 
99  |  | 
  | 
100  | 0  |   shift = m->size * GMP_NUMB_BITS - m->bit_size;  | 
101  | 0  |   if (shift > 0)  | 
102  | 0  |     { | 
103  |  |       /* Combine hi with top bits, add in */  | 
104  | 0  |       hi = (hi << shift) | (xp[mn-1] >> (GMP_NUMB_BITS - shift));  | 
105  | 0  |       xp[mn-1] = (xp[mn-1] & (((mp_limb_t) 1 << (GMP_NUMB_BITS - shift)) - 1))  | 
106  | 0  |   + mpn_addmul_1 (xp, m->B_shifted, mn-1, hi);  | 
107  |  |       /* FIXME: Can this copying be eliminated? */  | 
108  | 0  |       if (rp != xp)  | 
109  | 0  |   mpn_copyi (rp, xp, mn);  | 
110  | 0  |     }  | 
111  | 0  |   else  | 
112  | 0  |     { | 
113  | 0  |       hi = mpn_cnd_add_n (hi, rp, xp, m->B, mn);  | 
114  | 0  |       assert_maybe (hi == 0);  | 
115  | 0  |     }  | 
116  | 0  | }  |