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. */
40 #include "ecc-internal.h"
44 #include "ecc-secp521r1.h"
46 #define B_SHIFT (521 % GMP_NUMB_BITS)
48 #if HAVE_NATIVE_ecc_secp521r1_modp
49 #define ecc_secp521r1_modp _nettle_ecc_secp521r1_modp
51 ecc_secp521r1_modp (const struct ecc_modulo
*m
, mp_limb_t
*rp
, mp_limb_t
*xp
);
55 #define BMODP_SHIFT (GMP_NUMB_BITS - B_SHIFT)
56 #define BMODP ((mp_limb_t) 1 << BMODP_SHIFT)
58 /* Result may be *slightly* larger than 2^521 */
60 ecc_secp521r1_modp (const struct ecc_modulo
*m UNUSED
, mp_limb_t
*rp
, mp_limb_t
*xp
)
62 /* FIXME: Should use mpn_addlsh_n_ip1 */
64 /* Reduce from 2*ECC_LIMB_SIZE to ECC_LIMB_SIZE + 1 */
66 = mpn_addmul_1 (xp
, xp
+ ECC_LIMB_SIZE
, ECC_LIMB_SIZE
, BMODP
);
67 hi
= mpn_addmul_1 (xp
, xp
+ ECC_LIMB_SIZE
, 1, BMODP
);
68 hi
= sec_add_1 (xp
+ 1, xp
+ 1, ECC_LIMB_SIZE
- 1, hi
);
70 /* Combine hi with top bits, and add in. */
71 hi
= (hi
<< BMODP_SHIFT
) | (xp
[ECC_LIMB_SIZE
-1] >> B_SHIFT
);
72 rp
[ECC_LIMB_SIZE
-1] = (xp
[ECC_LIMB_SIZE
-1]
73 & (((mp_limb_t
) 1 << B_SHIFT
)-1))
74 + sec_add_1 (rp
, xp
, ECC_LIMB_SIZE
- 1, hi
);
78 #define ECC_SECP521R1_INV_ITCH (3*ECC_LIMB_SIZE)
81 ecc_secp521r1_inv (const struct ecc_modulo
*p
,
82 mp_limb_t
*rp
, const mp_limb_t
*ap
,
86 #define tp (scratch + ECC_LIMB_SIZE)
88 /* Addition chain for p - 2:
92 = 1 + 2^2(1 + 2 (2^518 - 1)
93 = 1 + 2^2(1 + 2 (2^259 + 1) (1 + 2(2^258 - 1)))
94 = 1 + 2^2(1 + 2 (2^259 + 1) (1 + 2(2^129 + 1) (2^129 - 1)))
95 = 1 + 2^2(1 + 2 (2^259 + 1) (1 + 2(2^129 + 1) (1 + 2 (2^128 - 1))))
99 2^{128} - 1 = (2^64 + 1) (2^32+1) (2^16 + 1) (2^8 + 1) (2^4 + 1) (2^2 + 1) (2 + 1)
101 This addition chain needs 520 squarings and 13 multiplies.
104 ecc_mod_sqr (p
, rp
, ap
, tp
); /* a^2 */
105 ecc_mod_mul (p
, rp
, ap
, rp
, tp
); /* a^3 = a^{2^2 - 1} */
106 ecc_mod_pow_2kp1 (p
, t0
, rp
, 2, tp
); /* a^15 = a^{2^4 - 1} */
107 ecc_mod_pow_2kp1 (p
, rp
, t0
, 4, tp
); /* a^{2^8 - 1} */
108 ecc_mod_pow_2kp1 (p
, t0
, rp
, 8, tp
); /* a^{2^16 - 1} */
109 ecc_mod_pow_2kp1 (p
, rp
, t0
, 16, tp
); /* a^{2^32 - 1} */
110 ecc_mod_pow_2kp1 (p
, t0
, rp
, 32, tp
); /* a^{2^64 - 1} */
111 ecc_mod_pow_2kp1 (p
, rp
, t0
, 64, tp
); /* a^{2^128 - 1} */
112 ecc_mod_sqr (p
, rp
, rp
, tp
); /* a^{2^129 - 2} */
113 ecc_mod_mul (p
, rp
, rp
, ap
, tp
); /* a^{2^129 - 1} */
114 ecc_mod_pow_2kp1 (p
, t0
, rp
, 129, tp
);/* a^{2^258 - 1} */
115 ecc_mod_sqr (p
, rp
, t0
, tp
); /* a^{2^259 - 2} */
116 ecc_mod_mul (p
, rp
, rp
, ap
, tp
); /* a^{2^259 - 1} */
117 ecc_mod_pow_2kp1 (p
, t0
, rp
, 259, tp
);/* a^{2^518 - 1} */
118 ecc_mod_sqr (p
, rp
, t0
, tp
); /* a^{2^519 - 2} */
119 ecc_mod_mul (p
, rp
, rp
, ap
, tp
); /* a^{2^519 - 1} */
120 ecc_mod_sqr (p
, rp
, rp
, tp
); /* a^{2^520 - 2} */
121 ecc_mod_sqr (p
, rp
, rp
, tp
); /* a^{2^521 - 4} */
122 ecc_mod_mul (p
, rp
, rp
, ap
, tp
); /* a^{2^521 - 3} */
125 #define ECC_SECP521R1_SQRT_ITCH (2*ECC_LIMB_SIZE)
128 ecc_secp521r1_sqrt (const struct ecc_modulo
*m
,
135 /* This computes the square root modulo p256 using the identity:
137 sqrt(c) = c^(2^519) (mod P-521)
139 which can be seen as a special case of Tonelli-Shanks with e=1.
142 ecc_mod_pow_2k (m
, rp
, cp
, 519, scratch
);
145 ecc_mod_sqr (m
, scratch
, rp
, scratch
);
146 ecc_mod_sub (m
, scratch
, scratch
, cp
);
148 /* Reduce top bits, since ecc_mod_zero_p requires input < 2p */
149 hi
= scratch
[ECC_LIMB_SIZE
-1] >> B_SHIFT
;
150 scratch
[ECC_LIMB_SIZE
-1] = (scratch
[ECC_LIMB_SIZE
-1]
151 & (((mp_limb_t
) 1 << B_SHIFT
)-1))
152 + sec_add_1 (scratch
, scratch
, ECC_LIMB_SIZE
- 1, hi
);
154 return ecc_mod_zero_p (m
, scratch
);
158 const struct ecc_curve _nettle_secp_521r1
=
165 ECC_SECP521R1_INV_ITCH
,
166 ECC_SECP521R1_SQRT_ITCH
,
187 ECC_MOD_INV_ITCH (ECC_LIMB_SIZE
),
209 ECC_ADD_JJA_ITCH (ECC_LIMB_SIZE
),
210 ECC_ADD_JJJ_ITCH (ECC_LIMB_SIZE
),
211 ECC_DUP_JJ_ITCH (ECC_LIMB_SIZE
),
212 ECC_MUL_A_ITCH (ECC_LIMB_SIZE
),
213 ECC_MUL_G_ITCH (ECC_LIMB_SIZE
),
214 ECC_J_TO_A_ITCH(ECC_LIMB_SIZE
, ECC_SECP521R1_INV_ITCH
),
228 const struct ecc_curve
*nettle_get_secp_521r1(void)
230 return &_nettle_secp_521r1
;