/src/nettle/ecc-mul-a-eh.c
Line  | Count  | Source (jump to first uncovered line)  | 
1  |  | /* ecc-mul-a-eh.c  | 
2  |  |  | 
3  |  |    Copyright (C) 2013, 2014 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  |  | #if HAVE_CONFIG_H  | 
33  |  | # include "config.h"  | 
34  |  | #endif  | 
35  |  |  | 
36  |  | #include <assert.h>  | 
37  |  |  | 
38  |  | #include "ecc.h"  | 
39  |  | #include "ecc-internal.h"  | 
40  |  |  | 
41  |  | /* Binary algorithm needs 6*ecc->p.size + scratch for ecc_add_ehh,  | 
42  |  |    total 10 ecc->p.size  | 
43  |  |  | 
44  |  |    Window algorithm needs (3<<w) * ecc->p.size for the table,  | 
45  |  |    3*ecc->p.size for a temporary point, and scratch for  | 
46  |  |    ecc_add_ehh. */  | 
47  |  |  | 
48  |  | #if ECC_MUL_A_EH_WBITS == 0  | 
49  |  | void  | 
50  |  | ecc_mul_a_eh (const struct ecc_curve *ecc,  | 
51  |  |         mp_limb_t *r,  | 
52  |  |         const mp_limb_t *np, const mp_limb_t *p,  | 
53  |  |         mp_limb_t *scratch)  | 
54  |  | { | 
55  |  | #define pe scratch  | 
56  |  | #define tp (scratch + 3*ecc->p.size)  | 
57  |  | #define scratch_out (scratch + 6*ecc->p.size)  | 
58  |  |  | 
59  |  |   unsigned i;  | 
60  |  |  | 
61  |  |   ecc_a_to_j (ecc, pe, p);  | 
62  |  |  | 
63  |  |   /* x = 0, y = 1, z = 1 */  | 
64  |  |   mpn_zero (r, 3*ecc->p.size);  | 
65  |  |   r[ecc->p.size] = r[2*ecc->p.size] = 1;  | 
66  |  |     | 
67  |  |   for (i = ecc->p.size; i-- > 0; )  | 
68  |  |     { | 
69  |  |       mp_limb_t w = np[i] << (GMP_LIMB_BITS - GMP_NUMB_BITS);  | 
70  |  |       unsigned j;  | 
71  |  |  | 
72  |  |       for (j = 0; j < GMP_NUMB_BITS; j++, w <<= 1)  | 
73  |  |   { | 
74  |  |     int bit;  | 
75  |  |     ecc->dup (ecc, r, r, scratch_out);  | 
76  |  |     ecc->add_hh (ecc, tp, r, pe, scratch_out);  | 
77  |  |  | 
78  |  |     bit = w >> (GMP_LIMB_BITS - 1);  | 
79  |  |     /* If we had a one-bit, use the sum. */  | 
80  |  |     cnd_copy (bit, r, tp, 3*ecc->p.size);  | 
81  |  |   }  | 
82  |  |     }  | 
83  |  | }  | 
84  |  | #else /* ECC_MUL_A_EH_WBITS > 1 */  | 
85  |  |  | 
86  | 0  | #define TABLE_SIZE (1U << ECC_MUL_A_EH_WBITS)  | 
87  | 0  | #define TABLE_MASK (TABLE_SIZE - 1)  | 
88  |  |  | 
89  | 0  | #define TABLE(j) (table + (j) * 3*ecc->p.size)  | 
90  |  |  | 
91  |  | static void  | 
92  |  | table_init (const struct ecc_curve *ecc,  | 
93  |  |       mp_limb_t *table, unsigned bits,  | 
94  |  |       const mp_limb_t *p,  | 
95  |  |       mp_limb_t *scratch)  | 
96  | 0  | { | 
97  | 0  |   unsigned size = 1 << bits;  | 
98  | 0  |   unsigned j;  | 
99  |  | 
  | 
100  | 0  |   mpn_zero (TABLE(0), 3*ecc->p.size);  | 
101  | 0  |   TABLE(0)[ecc->p.size] = TABLE(0)[2*ecc->p.size] = 1;  | 
102  |  | 
  | 
103  | 0  |   ecc_a_to_j (ecc, TABLE(1), p);  | 
104  |  | 
  | 
105  | 0  |   for (j = 2; j < size; j += 2)  | 
106  | 0  |     { | 
107  | 0  |       ecc->dup (ecc, TABLE(j), TABLE(j/2), scratch);  | 
108  | 0  |       ecc->add_hh (ecc, TABLE(j+1), TABLE(j), TABLE(1), scratch);  | 
109  | 0  |     }  | 
110  | 0  | }  | 
111  |  |  | 
112  |  | void  | 
113  |  | ecc_mul_a_eh (const struct ecc_curve *ecc,  | 
114  |  |         mp_limb_t *r,  | 
115  |  |         const mp_limb_t *np, const mp_limb_t *p,  | 
116  |  |         mp_limb_t *scratch)  | 
117  | 0  | { | 
118  | 0  | #define tp scratch  | 
119  | 0  | #define table (scratch + 3*ecc->p.size)  | 
120  | 0  |   mp_limb_t *scratch_out = table + (3*ecc->p.size << ECC_MUL_A_EH_WBITS);  | 
121  |  |  | 
122  |  |   /* Avoid the mp_bitcnt_t type for compatibility with older GMP  | 
123  |  |      versions. */  | 
124  | 0  |   unsigned blocks = (ecc->p.bit_size + ECC_MUL_A_EH_WBITS - 1) / ECC_MUL_A_EH_WBITS;  | 
125  | 0  |   unsigned bit_index = (blocks-1) * ECC_MUL_A_EH_WBITS;  | 
126  |  | 
  | 
127  | 0  |   mp_size_t limb_index = bit_index / GMP_NUMB_BITS;  | 
128  | 0  |   unsigned shift = bit_index % GMP_NUMB_BITS;  | 
129  | 0  |   mp_limb_t w, bits;  | 
130  |  | 
  | 
131  | 0  |   table_init (ecc, table, ECC_MUL_A_EH_WBITS, p, scratch_out);  | 
132  |  | 
  | 
133  | 0  |   w = np[limb_index];  | 
134  | 0  |   bits = w >> shift;  | 
135  | 0  |   if (limb_index < ecc->p.size - 1)  | 
136  | 0  |     bits |= np[limb_index + 1] << (GMP_NUMB_BITS - shift);  | 
137  |  | 
  | 
138  | 0  |   assert (bits < TABLE_SIZE);  | 
139  |  |  | 
140  | 0  |   mpn_sec_tabselect (r, table, 3*ecc->p.size, TABLE_SIZE, bits);  | 
141  |  | 
  | 
142  | 0  |   for (;;)  | 
143  | 0  |     { | 
144  | 0  |       unsigned j;  | 
145  | 0  |       if (shift >= ECC_MUL_A_EH_WBITS)  | 
146  | 0  |   { | 
147  | 0  |     shift -= ECC_MUL_A_EH_WBITS;  | 
148  | 0  |     bits = w >> shift;  | 
149  | 0  |   }  | 
150  | 0  |       else  | 
151  | 0  |   { | 
152  | 0  |     if (limb_index == 0)  | 
153  | 0  |       { | 
154  | 0  |         assert (shift == 0);  | 
155  | 0  |         break;  | 
156  | 0  |       }  | 
157  | 0  |     bits = w << (ECC_MUL_A_EH_WBITS - shift);  | 
158  | 0  |     w = np[--limb_index];  | 
159  | 0  |     shift = shift + GMP_NUMB_BITS - ECC_MUL_A_EH_WBITS;  | 
160  | 0  |     bits |= w >> shift;  | 
161  | 0  |   }  | 
162  | 0  |       for (j = 0; j < ECC_MUL_A_EH_WBITS; j++)  | 
163  | 0  |   ecc->dup (ecc, r, r, scratch_out);  | 
164  |  | 
  | 
165  | 0  |       bits &= TABLE_MASK;  | 
166  | 0  |       mpn_sec_tabselect (tp, table, 3*ecc->p.size, TABLE_SIZE, bits);  | 
167  | 0  |       ecc->add_hhh (ecc, r, r, tp, scratch_out);  | 
168  | 0  |     }  | 
169  | 0  | #undef table  | 
170  | 0  | #undef tp  | 
171  | 0  | }  | 
172  |  |  | 
173  |  | #endif /* ECC_MUL_A_EH_WBITS > 1 */  |