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 mod m, input 2*m->size, output m->size. */
43 void
ecc_mod(const struct ecc_modulo * m,mp_limb_t * rp)44 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 (bn < mn);
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 - 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 (rp + rn - mn - 1 + i, m->B, bn, rp[rn+i-1]);
68 	  rp[rn-1] = rp[rn+sn-1]
69 	    + mpn_add_n (rp + rn - sn - 1, rp + rn - sn - 1, rp + rn - 1, sn);
70 	}
71       goto final_limbs;
72     }
73   else
74     {
75       /* The loop below always runs at least once. But the analyzer
76 	 doesn't realize that, and complains about hi being used later
77 	 on without a well defined value. */
78 #ifdef __clang_analyzer__
79       hi = 0;
80 #endif
81       while (rn >= 2 * mn - bn)
82 	{
83 	  rn -= sn;
84 
85 	  for (i = 0; i < sn; i++)
86 	    rp[rn+i] = mpn_addmul_1 (rp + rn - mn + i, m->B, bn, rp[rn+i]);
87 
88 	  hi = mpn_add_n (rp + rn - sn, rp + rn - sn, rp + rn, sn);
89 	  hi = cnd_add_n (hi, rp + rn - mn, m->B, mn);
90 	  assert (hi == 0);
91 	}
92     }
93 
94   if (rn > mn)
95     {
96     final_limbs:
97       sn = rn - mn;
98 
99       for (i = 0; i < sn; i++)
100 	rp[mn+i] = mpn_addmul_1 (rp + i, m->B, bn, rp[mn+i]);
101 
102       hi = mpn_add_n (rp + bn, rp + bn, rp + mn, sn);
103       hi = sec_add_1 (rp + bn + sn, rp + bn + sn, mn - bn - sn, hi);
104     }
105 
106   shift = m->size * GMP_NUMB_BITS - m->bit_size;
107   if (shift > 0)
108     {
109       /* Combine hi with top bits, add in */
110       hi = (hi << shift) | (rp[mn-1] >> (GMP_NUMB_BITS - shift));
111       rp[mn-1] = (rp[mn-1] & (((mp_limb_t) 1 << (GMP_NUMB_BITS - shift)) - 1))
112 	+ mpn_addmul_1 (rp, m->B_shifted, mn-1, hi);
113     }
114   else
115     {
116       hi = cnd_add_n (hi, rp, m->B_shifted, mn);
117       assert (hi == 0);
118     }
119 }
120