3 /* nettle, low-level cryptographics library
5 * Copyright (C) 2013 Niels Möller
7 * The nettle library is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU Lesser General Public License as published by
9 * the Free Software Foundation; either version 2.1 of the License, or (at your
10 * option) any later version.
12 * The nettle library is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
15 * License for more details.
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with the nettle library; see the file COPYING.LIB. If not, write to
19 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
23 /* Development of Nettle's ECC support was funded by the .SE Internet Fund. */
31 #include "ecc-internal.h"
33 /* Routines for modp arithmetic. All values are ecc->size limbs, but
34 not necessarily < p. */
37 ecc_modp_add (const struct ecc_curve *ecc, mp_limb_t *rp,
38 const mp_limb_t *ap, const mp_limb_t *bp)
41 cy = mpn_add_n (rp, ap, bp, ecc->size);
42 cy = cnd_add_n (cy, rp, ecc->Bmodp, ecc->size);
43 cy = cnd_add_n (cy, rp, ecc->Bmodp, ecc->size);
48 ecc_modp_sub (const struct ecc_curve *ecc, mp_limb_t *rp,
49 const mp_limb_t *ap, const mp_limb_t *bp)
52 cy = mpn_sub_n (rp, ap, bp, ecc->size);
53 cy = cnd_sub_n (cy, rp, ecc->Bmodp, ecc->size);
54 cy = cnd_sub_n (cy, rp, ecc->Bmodp, ecc->size);
59 ecc_modp_sub_1 (const struct ecc_curve *ecc, mp_limb_t *rp,
60 const mp_limb_t *ap, mp_limb_t b)
64 for (i = 0; i < ecc->size; i++)
66 mp_limb_t cy = ap[i] < b;
70 b = cnd_sub_n (b, rp, ecc->Bmodp, ecc->size);
75 ecc_modp_mul_1 (const struct ecc_curve *ecc, mp_limb_t *rp,
76 const mp_limb_t *ap, mp_limb_t b)
80 assert (b <= 0xffffffff);
81 hi = mpn_mul_1 (rp, ap, ecc->size, b);
82 hi = mpn_addmul_1 (rp, ecc->Bmodp, ecc->size, hi);
84 hi = cnd_add_n (hi, rp, ecc->Bmodp, ecc->size);
85 /* Sufficient if b < B^size / p */
90 ecc_modp_addmul_1 (const struct ecc_curve *ecc, mp_limb_t *rp,
91 const mp_limb_t *ap, mp_limb_t b)
95 assert (b <= 0xffffffff);
96 hi = mpn_addmul_1 (rp, ap, ecc->size, b);
97 hi = mpn_addmul_1 (rp, ecc->Bmodp, ecc->size, hi);
99 hi = cnd_add_n (hi, rp, ecc->Bmodp, ecc->size);
100 /* Sufficient roughly if b < B^size / p */
105 ecc_modp_submul_1 (const struct ecc_curve *ecc, mp_limb_t *rp,
106 const mp_limb_t *ap, mp_limb_t b)
110 assert (b <= 0xffffffff);
111 hi = mpn_submul_1 (rp, ap, ecc->size, b);
112 hi = mpn_submul_1 (rp, ecc->Bmodp, ecc->size, hi);
114 hi = cnd_sub_n (hi, rp, ecc->Bmodp, ecc->size);
115 /* Sufficient roughly if b < B^size / p */
119 /* NOTE: mul and sqr needs 2*ecc->size limbs at rp */
121 ecc_modp_mul (const struct ecc_curve *ecc, mp_limb_t *rp,
122 const mp_limb_t *ap, const mp_limb_t *bp)
124 mpn_mul_n (rp, ap, bp, ecc->size);
125 ecc->reduce (ecc, rp);
129 ecc_modp_sqr (const struct ecc_curve *ecc, mp_limb_t *rp,
132 mpn_sqr (rp, ap, ecc->size);
133 ecc->reduce (ecc, rp);
137 ecc_modp_inv (const struct ecc_curve *ecc, mp_limb_t *rp, mp_limb_t *ap,
140 sec_modinv (rp, ap, ecc->size, ecc->p, ecc->pp1h, ecc->bit_size, scratch);