1 /* ecc-mul-g.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 
39 #include <nettle/ecc.h>
40 #include "ecc-internal.h"
41 
42 void
ecc_mul_g(const struct ecc_curve * ecc,mp_limb_t * r,const mp_limb_t * np,mp_limb_t * scratch)43 ecc_mul_g (const struct ecc_curve *ecc, mp_limb_t *r,
44 	   const mp_limb_t *np, mp_limb_t *scratch)
45 {
46   /* Scratch need determined by the ecc_add_jja call. Current total is
47      9 * ecc->p.size, at most 648 bytes. */
48 #define tp scratch
49 #define scratch_out (scratch + 3*ecc->p.size)
50 
51   unsigned k, c;
52   unsigned i, j;
53   unsigned bit_rows;
54 
55   int is_zero;
56 
57   k = ecc->pippenger_k;
58   c = ecc->pippenger_c;
59 
60   bit_rows = (ecc->p.bit_size + k - 1) / k;
61 
62   mpn_zero (r, 3*ecc->p.size);
63 
64   for (i = k, is_zero = 1; i-- > 0; )
65     {
66       ecc_dup_jj (ecc, r, r, scratch);
67       for (j = 0; j * c < bit_rows; j++)
68 	{
69 	  unsigned bits;
70 	  /* Avoid the mp_bitcnt_t type for compatibility with older GMP
71 	     versions. */
72 	  unsigned bit_index;
73 
74 	  /* Extract c bits from n, stride k, starting at i + kcj,
75 	     ending at i + k (cj + c - 1)*/
76 	  for (bits = 0, bit_index = i + k*(c*j+c); bit_index > i + k*c*j; )
77 	    {
78 	      mp_size_t limb_index;
79 	      unsigned shift;
80 
81 	      bit_index -= k;
82 
83 	      limb_index = bit_index / GMP_NUMB_BITS;
84 	      if (limb_index >= ecc->p.size)
85 		continue;
86 
87 	      shift = bit_index % GMP_NUMB_BITS;
88 	      bits = (bits << 1) | ((np[limb_index] >> shift) & 1);
89 	    }
90 	  sec_tabselect (tp, 2*ecc->p.size,
91 			 (ecc->pippenger_table
92 			  + (2*ecc->p.size * (mp_size_t) j << c)),
93 			 1<<c, bits);
94 	  cnd_copy (is_zero, r, tp, 2*ecc->p.size);
95 	  cnd_copy (is_zero, r + 2*ecc->p.size, ecc->unit, ecc->p.size);
96 
97 	  ecc_add_jja (ecc, tp, r, tp, scratch_out);
98 	  /* Use the sum when valid. ecc_add_jja produced garbage if
99 	     is_zero != 0 or bits == 0, . */
100 	  cnd_copy (bits & (is_zero - 1), r, tp, 3*ecc->p.size);
101 	  is_zero &= (bits == 0);
102 	}
103     }
104 #undef tp
105 #undef scratch_out
106 }
107