3 /* Compile time constant (but machine dependent) tables. */
5 /* nettle, low-level cryptographics library
7 * Copyright (C) 2013 Niels Möller
9 * The nettle library is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU Lesser General Public License as published by
11 * the Free Software Foundation; either version 2.1 of the License, or (at your
12 * option) any later version.
14 * The nettle library is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
17 * License for more details.
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with the nettle library; see the file COPYING.LIB. If not, write to
21 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
25 /* Development of Nettle's ECC support was funded by the .SE Internet Fund. */
31 #include "ecc-internal.h"
37 #if HAVE_NATIVE_ecc_521_modp
38 #define ecc_521_modp nettle_ecc_521_modp
40 ecc_521_modp (const struct ecc_curve
*ecc
, mp_limb_t
*rp
);
44 #define B_SHIFT (521 % GMP_NUMB_BITS)
45 #define BMODP_SHIFT (GMP_NUMB_BITS - B_SHIFT)
46 #define BMODP ((mp_limb_t) 1 << BMODP_SHIFT)
48 /* Result may be *slightly* larger than 2^521 */
50 ecc_521_modp (const struct ecc_curve
*ecc UNUSED
, mp_limb_t
*rp
)
52 /* FIXME: Should use mpn_addlsh_n_ip1 */
54 /* Reduce from 2*ECC_LIMB_SIZE to ECC_LIMB_SIZE + 1 */
56 = mpn_addmul_1 (rp
, rp
+ ECC_LIMB_SIZE
, ECC_LIMB_SIZE
, BMODP
);
57 hi
= mpn_addmul_1 (rp
, rp
+ ECC_LIMB_SIZE
, 1, BMODP
);
58 hi
= sec_add_1 (rp
+ 1, rp
+ 1, ECC_LIMB_SIZE
- 1, hi
);
60 /* Combine hi with top bits, and add in. */
61 hi
= (hi
<< BMODP_SHIFT
) | (rp
[ECC_LIMB_SIZE
-1] >> B_SHIFT
);
62 rp
[ECC_LIMB_SIZE
-1] = (rp
[ECC_LIMB_SIZE
-1]
63 & (((mp_limb_t
) 1 << B_SHIFT
)-1))
64 + sec_add_1 (rp
, rp
, ECC_LIMB_SIZE
- 1, hi
);
68 const struct ecc_curve nettle_secp_521r1
=