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"
45 #if HAVE_NATIVE_ecc_256_redc
48 # define USE_REDC (ECC_REDC_SIZE != 0)
53 #if HAVE_NATIVE_ecc_256_redc
54 # define ecc_256_redc nettle_ecc_256_redc
56 ecc_256_redc (const struct ecc_modulo *p, mp_limb_t *rp);
57 #else /* !HAVE_NATIVE_ecc_256_redc */
58 # if ECC_REDC_SIZE > 0
59 # define ecc_256_redc ecc_pp1_redc
60 # elif ECC_REDC_SIZE == 0
61 # define ecc_256_redc NULL
63 # error Configuration error
65 #endif /* !HAVE_NATIVE_ecc_256_redc */
67 #if ECC_BMODP_SIZE < ECC_LIMB_SIZE
68 #define ecc_256_modp ecc_mod
69 #define ecc_256_modq ecc_mod
70 #elif GMP_NUMB_BITS == 64
73 ecc_256_modp (const struct ecc_modulo *p, mp_limb_t *rp)
82 /* This is not particularly fast, but should work well with assembly implementation. */
83 for (; n >= p->size; n--)
85 mp_limb_t q2, q1, q0, t, cy;
87 /* <q2, q1, q0> = v * u1 + <u1,u0>, with v = 2^32 - 1:
103 t = (u1 >> 32) + (q0 < t) + 1;
107 /* Compute candidate remainder */
108 u1 = u0 + (q1 << 32) - q1;
109 t = -(mp_limb_t) (u1 > q0);
110 u1 -= t & 0xffffffff;
126 We multiply by two low limbs of p, 2^96 - 1, so we could use
127 shifts rather than mul.
129 t = mpn_submul_1 (rp + n - 4, p->m, 2, q1);
130 t += cnd_sub_n (q2, rp + n - 3, p->m, 1);
131 t += (-q2) & 0xffffffff;
139 cy = cnd_add_n (t, rp + n - 4, p->m, 2);
142 u1 -= (-t) & 0xffffffff;
149 ecc_256_modq (const struct ecc_modulo *q, mp_limb_t *rp)
151 mp_limb_t u2, u1, u0;
158 /* This is not particularly fast, but should work well with assembly implementation. */
159 for (; n >= q->size; n--)
161 mp_limb_t q2, q1, q0, t, c1, c0;
165 /* <q2, q1, q0> = v * u2 + <u2,u1>, same method as above.
181 t = (u2 >> 32) + (q0 < t) + 1;
185 /* Compute candidate remainder, <u1, u0> - <q2, q1> * (2^128 - 2^96 + 2^64 - 1)
186 <u1, u0> + 2^64 q2 + (2^96 - 2^64 + 1) q1 (mod 2^128)
205 t = -(mp_limb_t) (u2 >= q0);
209 u2 += (t << 32) + (u1 < t);
213 c0 = cnd_sub_n (q2, rp + n - 3, q->m, 1);
214 c0 += (-q2) & q->m[1];
215 t = mpn_submul_1 (rp + n - 4, q->m, 2, q1);
219 /* Construct underflow condition. */
221 t = - (mp_limb_t) (u2 < c1);
226 /* Conditional add of p */
228 u2 += (t<<32) + (u1 < t);
230 t = cnd_add_n (t, rp + n - 4, q->m, 2);
239 #error Unsupported parameters
242 const struct ecc_curve _nettle_secp_256r1 =
249 ECC_MOD_INV_ITCH (ECC_LIMB_SIZE),
259 USE_REDC ? ecc_256_redc : ecc_256_modp,
268 ECC_MOD_INV_ITCH (ECC_LIMB_SIZE),
287 ECC_ADD_JJJ_ITCH (ECC_LIMB_SIZE),
288 ECC_MUL_A_ITCH (ECC_LIMB_SIZE),
289 ECC_MUL_G_ITCH (ECC_LIMB_SIZE),
290 ECC_J_TO_A_ITCH (ECC_LIMB_SIZE),
304 const struct ecc_curve *nettle_get_secp_256r1(void)
306 return &_nettle_secp_256r1;