3 Compile time constant (but machine dependent) tables.
5 Copyright (C) 2013, 2014 Niels Möller
7 This file is part of GNU Nettle.
9 GNU Nettle is free software: you can redistribute it and/or
10 modify it under the terms of either:
12 * the GNU Lesser General Public License as published by the Free
13 Software Foundation; either version 3 of the License, or (at your
14 option) any later version.
18 * the GNU General Public License as published by the Free
19 Software Foundation; either version 2 of the License, or (at your
20 option) any later version.
22 or both in parallel, as here.
24 GNU Nettle is distributed in the hope that it will be useful,
25 but WITHOUT ANY WARRANTY; without even the implied warranty of
26 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
27 General Public License for more details.
29 You should have received copies of the GNU General Public License and
30 the GNU Lesser General Public License along with this program. If
31 not, see http://www.gnu.org/licenses/.
34 /* Development of Nettle's ECC support was funded by the .SE Internet Fund. */
43 #include "ecc-internal.h"
49 #if HAVE_NATIVE_ecc_384_modp
50 #define ecc_384_modp nettle_ecc_384_modp
52 ecc_384_modp (const struct ecc_modulo *m, mp_limb_t *rp);
53 #elif GMP_NUMB_BITS == 32
55 /* Use that 2^{384} = 2^{128} + 2^{96} - 2^{32} + 1, and eliminate 256
58 We can get carry == 2 in the first iteration, and I think *only* in
59 the first iteration. */
61 /* p is 12 limbs, and B^12 - p = B^4 + B^3 - B + 1. We can eliminate
62 almost 8 at a time. Do only 7, to avoid additional carry
63 propagation, followed by 5. */
65 ecc_384_modp (const struct ecc_modulo *p, mp_limb_t *rp)
69 /* Reduce from 24 to 17 limbs. */
70 cy = mpn_add_n (rp + 4, rp + 4, rp + 16, 8);
71 cy = sec_add_1 (rp + 12, rp + 12, 3, cy);
73 bw = mpn_sub_n (rp + 5, rp + 5, rp + 16, 8);
74 bw = sec_sub_1 (rp + 13, rp + 13, 3, bw);
76 cy += mpn_add_n (rp + 7, rp + 7, rp + 16, 8);
77 cy = sec_add_1 (rp + 15, rp + 15, 1, cy);
79 cy += mpn_add_n (rp + 8, rp + 8, rp + 16, 8);
86 /* Reduce from 17 to 12 limbs */
87 cy = mpn_add_n (rp, rp, rp + 12, 5);
88 cy = sec_add_1 (rp + 5, rp + 5, 3, cy);
90 bw = mpn_sub_n (rp + 1, rp + 1, rp + 12, 5);
91 bw = sec_sub_1 (rp + 6, rp + 6, 6, bw);
93 cy += mpn_add_n (rp + 3, rp + 3, rp + 12, 5);
94 cy = sec_add_1 (rp + 8, rp + 8, 1, cy);
96 cy += mpn_add_n (rp + 4, rp + 4, rp + 12, 5);
97 cy = sec_add_1 (rp + 9, rp + 9, 3, cy);
102 cy = cnd_add_n (cy, rp, p->B, ECC_LIMB_SIZE);
105 #elif GMP_NUMB_BITS == 64
106 /* p is 6 limbs, and B^6 - p = B^2 + 2^32 (B - 1) + 1. Eliminate 3
107 (almost 4) limbs at a time. */
109 ecc_384_modp (const struct ecc_modulo *p, mp_limb_t *rp)
114 /* Reduce from 12 to 9 limbs */
115 tp[0] = 0; /* FIXME: Could use mpn_sub_nc */
116 mpn_copyi (tp + 1, rp + 8, 3);
117 tp[4] = rp[11] - mpn_sub_n (tp, tp, rp + 8, 4);
118 tp[5] = mpn_lshift (tp, tp, 5, 32);
120 cy = mpn_add_n (rp + 2, rp + 2, rp + 8, 4);
121 cy = sec_add_1 (rp + 6, rp + 6, 2, cy);
123 cy += mpn_add_n (rp + 2, rp + 2, tp, 6);
124 cy += mpn_add_n (rp + 4, rp + 4, rp + 8, 4);
129 /* Reduce from 9 to 6 limbs */
131 mpn_copyi (tp + 1, rp + 6, 2);
132 tp[3] = rp[8] - mpn_sub_n (tp, tp, rp + 6, 3);
133 tp[4] = mpn_lshift (tp, tp, 4, 32);
135 cy = mpn_add_n (rp, rp, rp + 6, 3);
136 cy = sec_add_1 (rp + 3, rp + 3, 2, cy);
137 cy += mpn_add_n (rp, rp, tp, 5);
138 cy += mpn_add_n (rp + 2, rp + 2, rp + 6, 3);
140 cy = sec_add_1 (rp + 5, rp + 5, 1, cy);
143 cy = cnd_add_n (cy, rp, p->B, ECC_LIMB_SIZE);
147 #define ecc_384_modp ecc_mod
150 const struct ecc_curve nettle_secp_384r1 =
157 ECC_MOD_INV_ITCH (ECC_LIMB_SIZE),
176 ECC_MOD_INV_ITCH (ECC_LIMB_SIZE),
195 ECC_ADD_JJJ_ITCH (ECC_LIMB_SIZE),
196 ECC_MUL_A_ITCH (ECC_LIMB_SIZE),
197 ECC_MUL_G_ITCH (ECC_LIMB_SIZE),
198 ECC_J_TO_A_ITCH (ECC_LIMB_SIZE),