3 Copyright (C) 2013, 2014 Niels Möller
5 This file is part of GNU Nettle.
7 GNU Nettle is free software: you can redistribute it and/or
8 modify it under the terms of either:
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.
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.
20 or both in parallel, as here.
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.
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/.
39 #include "ecc-internal.h"
41 /* Binary algorithm needs 6*ecc->p.size + scratch for ecc_add_ehh,
44 Window algorithm needs (3<<w) * ecc->p.size for the table,
45 3*ecc->p.size for a temporary point, and scratch for
48 #if ECC_MUL_A_EH_WBITS == 0
50 ecc_mul_a_eh (const struct ecc_curve *ecc,
52 const mp_limb_t *np, const mp_limb_t *p,
56 #define tp (scratch + 3*ecc->p.size)
57 #define scratch_out (scratch + 6*ecc->p.size)
61 ecc_a_to_j (ecc, pe, p);
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;
67 for (i = ecc->p.size; i-- > 0; )
72 for (bit = (mp_limb_t) 1 << (GMP_NUMB_BITS - 1);
78 ecc_dup_eh (ecc, r, r, scratch_out);
79 ecc_add_ehh (ecc, tp, r, pe, scratch_out);
81 digit = (w & bit) > 0;
82 /* If we had a one-bit, use the sum. */
83 cnd_copy (digit, r, tp, 3*ecc->p.size);
87 #else /* ECC_MUL_A_EH_WBITS > 1 */
89 #define TABLE_SIZE (1U << ECC_MUL_A_EH_WBITS)
90 #define TABLE_MASK (TABLE_SIZE - 1)
92 #define TABLE(j) (table + (j) * 3*ecc->p.size)
95 table_init (const struct ecc_curve *ecc,
96 mp_limb_t *table, unsigned bits,
100 unsigned size = 1 << bits;
103 mpn_zero (TABLE(0), 3*ecc->p.size);
104 TABLE(0)[ecc->p.size] = TABLE(0)[2*ecc->p.size] = 1;
106 ecc_a_to_j (ecc, TABLE(1), p);
108 for (j = 2; j < size; j += 2)
110 ecc_dup_eh (ecc, TABLE(j), TABLE(j/2), scratch);
111 ecc_add_ehh (ecc, TABLE(j+1), TABLE(j), TABLE(1), scratch);
116 ecc_mul_a_eh (const struct ecc_curve *ecc,
118 const mp_limb_t *np, const mp_limb_t *p,
122 #define table (scratch + 3*ecc->p.size)
123 mp_limb_t *scratch_out = table + (3*ecc->p.size << ECC_MUL_A_EH_WBITS);
125 /* Avoid the mp_bitcnt_t type for compatibility with older GMP
127 unsigned blocks = (ecc->p.bit_size + ECC_MUL_A_EH_WBITS - 1) / ECC_MUL_A_EH_WBITS;
128 unsigned bit_index = (blocks-1) * ECC_MUL_A_EH_WBITS;
130 mp_size_t limb_index = bit_index / GMP_NUMB_BITS;
131 unsigned shift = bit_index % GMP_NUMB_BITS;
134 table_init (ecc, table, ECC_MUL_A_EH_WBITS, p, scratch_out);
138 if (limb_index < ecc->p.size - 1)
139 bits |= np[limb_index + 1] << (GMP_NUMB_BITS - shift);
141 assert (bits < TABLE_SIZE);
143 sec_tabselect (r, 3*ecc->p.size, table, TABLE_SIZE, bits);
148 if (shift >= ECC_MUL_A_EH_WBITS)
150 shift -= ECC_MUL_A_EH_WBITS;
160 bits = w << (ECC_MUL_A_EH_WBITS - shift);
161 w = np[--limb_index];
162 shift = shift + GMP_NUMB_BITS - ECC_MUL_A_EH_WBITS;
165 for (j = 0; j < ECC_MUL_A_EH_WBITS; j++)
166 ecc_dup_eh (ecc, r, r, scratch_out);
169 sec_tabselect (tp, 3*ecc->p.size, table, TABLE_SIZE, bits);
170 ecc_add_ehh (ecc, r, tp, r, scratch_out);
176 #endif /* ECC_MUL_A_EH_WBITS > 1 */