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/.
32 /* Development of Nettle's ECC support was funded by the .SE Internet Fund. */
40 #include "ecc-internal.h"
42 /* Routines for modp arithmetic. All values are ecc->size limbs, but
43 not necessarily < p. */
46 ecc_mod_add (const struct ecc_modulo *m, mp_limb_t *rp,
47 const mp_limb_t *ap, const mp_limb_t *bp)
50 cy = mpn_add_n (rp, ap, bp, m->size);
51 cy = cnd_add_n (cy, rp, m->B, m->size);
52 cy = cnd_add_n (cy, rp, m->B, m->size);
57 ecc_mod_sub (const struct ecc_modulo *m, mp_limb_t *rp,
58 const mp_limb_t *ap, const mp_limb_t *bp)
61 cy = mpn_sub_n (rp, ap, bp, m->size);
62 cy = cnd_sub_n (cy, rp, m->B, m->size);
63 cy = cnd_sub_n (cy, rp, m->B, m->size);
68 ecc_mod_mul_1 (const struct ecc_modulo *m, mp_limb_t *rp,
69 const mp_limb_t *ap, mp_limb_t b)
73 assert (b <= 0xffffffff);
74 hi = mpn_mul_1 (rp, ap, m->size, b);
75 hi = mpn_addmul_1 (rp, m->B, m->size, hi);
77 hi = cnd_add_n (hi, rp, m->B, m->size);
78 /* Sufficient if b < B^size / p */
83 ecc_mod_addmul_1 (const struct ecc_modulo *m, mp_limb_t *rp,
84 const mp_limb_t *ap, mp_limb_t b)
88 assert (b <= 0xffffffff);
89 hi = mpn_addmul_1 (rp, ap, m->size, b);
90 hi = mpn_addmul_1 (rp, m->B, m->size, hi);
92 hi = cnd_add_n (hi, rp, m->B, m->size);
93 /* Sufficient roughly if b < B^size / p */
98 ecc_mod_submul_1 (const struct ecc_modulo *m, mp_limb_t *rp,
99 const mp_limb_t *ap, mp_limb_t b)
103 assert (b <= 0xffffffff);
104 hi = mpn_submul_1 (rp, ap, m->size, b);
105 hi = mpn_submul_1 (rp, m->B, m->size, hi);
107 hi = cnd_sub_n (hi, rp, m->B, m->size);
108 /* Sufficient roughly if b < B^size / p */
112 /* NOTE: mul and sqr needs 2*m->size limbs at rp */
114 ecc_mod_mul (const struct ecc_modulo *m, mp_limb_t *rp,
115 const mp_limb_t *ap, const mp_limb_t *bp)
117 mpn_mul_n (rp, ap, bp, m->size);
122 ecc_mod_sqr (const struct ecc_modulo *m, mp_limb_t *rp,
125 mpn_sqr (rp, ap, m->size);