ecc-384.c 7.03 KB
Newer Older
1
/* ecc-384.c
2 3 4

   Compile time constant (but machine dependent) tables.

5
   Copyright (C) 2013, 2014 Niels Möller
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32

   This file is part of GNU Nettle.

   GNU Nettle is free software: you can redistribute it and/or
   modify it under the terms of either:

     * the GNU Lesser General Public License as published by the Free
       Software Foundation; either version 3 of the License, or (at your
       option) any later version.

   or

     * the GNU General Public License as published by the Free
       Software Foundation; either version 2 of the License, or (at your
       option) any later version.

   or both in parallel, as here.

   GNU Nettle is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   You should have received copies of the GNU General Public License and
   the GNU Lesser General Public License along with this program.  If
   not, see http://www.gnu.org/licenses/.
*/
Niels Möller's avatar
Niels Möller committed
33

34
/* Development of Nettle's ECC support was funded by the .SE Internet Fund. */
Niels Möller's avatar
Niels Möller committed
35 36 37 38 39 40 41

#if HAVE_CONFIG_H
# include "config.h"
#endif

#include <assert.h>

42
#include "ecc.h"
Niels Möller's avatar
Niels Möller committed
43 44 45 46 47 48
#include "ecc-internal.h"

#define USE_REDC 0

#include "ecc-384.h"

49 50 51
#if HAVE_NATIVE_ecc_384_modp
#define ecc_384_modp nettle_ecc_384_modp
void
52
ecc_384_modp (const struct ecc_modulo *m, mp_limb_t *rp);
53 54
#elif GMP_NUMB_BITS == 32

Niels Möller's avatar
Niels Möller committed
55 56 57 58 59 60 61 62 63 64
/* Use that 2^{384} = 2^{128} + 2^{96} - 2^{32} + 1, and eliminate 256
   bits at a time.

   We can get carry == 2 in the first iteration, and I think *only* in
   the first iteration. */

/* p is 12 limbs, and B^12 - p = B^4 + B^3 - B + 1. We can eliminate
   almost 8 at a time. Do only 7, to avoid additional carry
   propagation, followed by 5. */
static void
65
ecc_384_modp (const struct ecc_modulo *p, mp_limb_t *rp)
Niels Möller's avatar
Niels Möller committed
66 67 68 69 70 71 72 73
{
  mp_limb_t cy, bw;

  /* Reduce from 24 to 17 limbs. */
  cy = mpn_add_n (rp + 4, rp + 4, rp + 16, 8);
  cy = sec_add_1 (rp + 12, rp + 12, 3, cy);

  bw = mpn_sub_n (rp + 5, rp + 5, rp + 16, 8);
74
  bw = sec_sub_1 (rp + 13, rp + 13, 3, bw);
Niels Möller's avatar
Niels Möller committed
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90

  cy += mpn_add_n (rp + 7, rp + 7, rp + 16, 8);
  cy = sec_add_1 (rp + 15, rp + 15, 1, cy);

  cy += mpn_add_n (rp + 8, rp + 8, rp + 16, 8);
  assert (bw <= cy);
  cy -= bw;

  assert (cy <= 2);  
  rp[16] = cy;

  /* Reduce from 17 to 12 limbs */
  cy = mpn_add_n (rp, rp, rp + 12, 5);
  cy = sec_add_1 (rp + 5, rp + 5, 3, cy);
  
  bw = mpn_sub_n (rp + 1, rp + 1, rp + 12, 5);
91
  bw = sec_sub_1 (rp + 6, rp + 6, 6, bw);
Niels Möller's avatar
Niels Möller committed
92 93 94 95 96 97 98 99 100 101
  
  cy += mpn_add_n (rp + 3, rp + 3, rp + 12, 5);
  cy = sec_add_1 (rp + 8, rp + 8, 1, cy);

  cy += mpn_add_n (rp + 4, rp + 4, rp + 12, 5);
  cy = sec_add_1 (rp + 9, rp + 9, 3, cy);

  assert (cy >= bw);
  cy -= bw;
  assert (cy <= 1);
102
  cy = cnd_add_n (cy, rp, p->B, ECC_LIMB_SIZE);
Niels Möller's avatar
Niels Möller committed
103 104 105 106 107 108
  assert (cy == 0);
}
#elif GMP_NUMB_BITS == 64
/* p is 6 limbs, and B^6 - p = B^2 + 2^32 (B - 1) + 1. Eliminate 3
   (almost 4) limbs at a time. */
static void
109
ecc_384_modp (const struct ecc_modulo *p, mp_limb_t *rp)
Niels Möller's avatar
Niels Möller committed
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
{
  mp_limb_t tp[6];
  mp_limb_t cy;

  /* Reduce from 12 to 9 limbs */
  tp[0] = 0; /* FIXME: Could use mpn_sub_nc */
  mpn_copyi (tp + 1, rp + 8, 3);
  tp[4] = rp[11] - mpn_sub_n (tp, tp, rp + 8, 4);
  tp[5] = mpn_lshift (tp, tp, 5, 32);

  cy = mpn_add_n (rp + 2, rp + 2, rp + 8, 4);
  cy = sec_add_1 (rp + 6, rp + 6, 2, cy);

  cy += mpn_add_n (rp + 2, rp + 2, tp, 6);
  cy += mpn_add_n (rp + 4, rp + 4, rp + 8, 4);

  assert (cy <= 2);
  rp[8] = cy;

  /* Reduce from 9 to 6 limbs */
  tp[0] = 0;
  mpn_copyi (tp + 1, rp + 6, 2);
132
  tp[3] = rp[8] - mpn_sub_n (tp, tp, rp + 6, 3);
Niels Möller's avatar
Niels Möller committed
133 134 135 136 137 138 139 140 141 142
  tp[4] = mpn_lshift (tp, tp, 4, 32);

  cy = mpn_add_n (rp, rp, rp + 6, 3);
  cy = sec_add_1 (rp + 3, rp + 3, 2, cy);
  cy += mpn_add_n (rp, rp, tp, 5);
  cy += mpn_add_n (rp + 2, rp + 2, rp + 6, 3);

  cy = sec_add_1 (rp + 5, rp + 5, 1, cy);
  assert (cy <= 1);

143
  cy = cnd_add_n (cy, rp, p->B, ECC_LIMB_SIZE);
Niels Möller's avatar
Niels Möller committed
144 145 146
  assert (cy == 0);  
}
#else
147
#define ecc_384_modp ecc_mod
Niels Möller's avatar
Niels Möller committed
148
#endif
149 150 151 152 153 154 155 156 157 158 159 160 161

static void
ecc_384_sqrt (const struct ecc_modulo *m,
	      mp_limb_t *rp,
	      const mp_limb_t *cp,
	      mp_limb_t *scratch)
{
  mp_size_t size = ECC_LIMB_SIZE;

  /* This computes the square root modulo p256 using the identity:

     sqrt(c) = c^(2^382 − 2^126 - 2^94 + 2^30)  (mod P-384)

162 163 164 165 166
     which can be seen as a special case of Tonelli-Shanks with e=1.

     The specific sqr/mul schedule is from Routine 3.2.12 of
     "Mathematical routines for the NIST prime elliptic curves", April
     5, 2010, author unknown.
167 168 169 170
  */

  /* We use our scratch space for several temporaries, all of
     which are 2*size long to allow for multiplication/squaring */
Niels Möller's avatar
Niels Möller committed
171
  
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
#define T1 scratch
#define T2 (scratch + 2*size)
#define T3 (scratch + 4*size)
#define T4 (scratch + 6*size)
#define Tr T2  /* T2 and R are not live at the same time */
#define Tx (scratch + 8*size)

  ecc_mod_pow2n_mul(m, T1, cp, cp, T2, 1);   /* [ 1] T1 <- c^(2^1 - 1) */
  ecc_mod_pow2n_mul(m, Tx, T1, T1, T3, 2);   /* [ 2] T2 <- c^(2^4 - 1) */
  ecc_mod_pow2n_mul(m, T2, Tx, cp, T3, 1);   /* [ 3] T2 <- c^(2^5 - 1) */
  ecc_mod_pow2n_mul(m, T3, T2, T2, Tx, 5);   /* [ 4] T3 <- c^(2^10 - 1) */
  ecc_mod_pow2n_mul(m, T4, T3, T2, Tx, 5);   /* [ 5] T4 <- c^(2^15 - 1) */
  ecc_mod_pow2n_mul(m, T2, T4, T4, Tx, 15);  /* [ 6] T2 <- c^(2^30 - 1) */
  ecc_mod_pow2n    (m, T3, T2,     Tx, 2);   /* [ 7] T3 <- c^(2^32 - 4) */
  ecc_mod_inplc_mul(m, T1, T3,     Tx);      /* [ 8] T1 <- c^(2^32 - 1) */
  ecc_mod_pow2n    (m, T3, T3,     Tx, 28);  /* [ 9] T3 <- c^(2^60 - 2^30) */
  ecc_mod_inplc_mul(m, T2, T3,     Tx);      /*      T2 <- c^(2^60 - 1) */
  ecc_mod_pow2n_mul(m, T3, T2, T2, Tx, 60);  /* [10] T3 <- c^(2^120 - 1) */
  ecc_mod_pow2n_mul(m, Tr, T3, T3, Tx, 120); /* [11] r  <- c^(2^240 - 1) */
  ecc_mod_pow2n_mul(m, T3, Tr, T4, Tx, 15);  /* [12] r  <- c^(2^255 - 1) */
  ecc_mod_pow2n_mul(m, Tr, T3, T1, Tx, 33);  /* [13] r  <- c^(2^288 - 2^32 - 1) */
  ecc_mod_pow2n_mul(m, T3, Tr, cp, T1, 64);  /* [14] r  <- c^(2^352 - 2^96 - 2^64 + 1) */
  ecc_mod_pow2n    (m, rp, T3,     T1, 30);  /* [15] r  <- c^(2^382 - 2^126 - 2^94 + 2^30) */

#undef T1
#undef T2
#undef T3
#undef T4
#undef Tr
#undef Tx
}


205
const struct ecc_curve _nettle_secp_384r1 =
Niels Möller's avatar
Niels Möller committed
206
{
207 208 209 210 211
  {
    384,
    ECC_LIMB_SIZE,    
    ECC_BMODP_SIZE,
    ECC_REDC_SIZE,
212
    ECC_MOD_INV_ITCH (ECC_LIMB_SIZE),
213
    10 * ECC_LIMB_SIZE,
214

215 216 217 218
    ecc_p,
    ecc_Bmodp,
    ecc_Bmodp_shifted,
    ecc_redc_ppm1,
219 220
    ecc_pp1h,

221 222
    ecc_384_modp,
    ecc_384_modp,
223
    ecc_mod_inv,
224
    NULL,
225
    ecc_384_sqrt,
226 227 228 229 230 231
  },
  {
    384,
    ECC_LIMB_SIZE,    
    ECC_BMODQ_SIZE,
    0,
232
    ECC_MOD_INV_ITCH (ECC_LIMB_SIZE),
233
    0,
234

235 236 237 238
    ecc_q,
    ecc_Bmodq,
    ecc_Bmodq_shifted,
    NULL,
239 240
    ecc_qp1h,

241 242
    ecc_mod,
    ecc_mod,
243
    ecc_mod_inv,
244
    NULL,
245
    NULL,
246 247
  },

Niels Möller's avatar
Niels Möller committed
248 249 250
  USE_REDC,
  ECC_PIPPENGER_K,
  ECC_PIPPENGER_C,
251

252
  ECC_ADD_JJJ_ITCH (ECC_LIMB_SIZE),
253 254 255 256
  ECC_MUL_A_ITCH (ECC_LIMB_SIZE),
  ECC_MUL_G_ITCH (ECC_LIMB_SIZE),
  ECC_J_TO_A_ITCH (ECC_LIMB_SIZE),

257
  ecc_add_jjj,
258 259 260 261
  ecc_mul_a,
  ecc_mul_g,
  ecc_j_to_a,

Niels Möller's avatar
Niels Möller committed
262 263
  ecc_b,
  ecc_g,
264
  NULL,
Niels Möller's avatar
Niels Möller committed
265 266 267
  ecc_unit,
  ecc_table
};
268 269 270

const struct ecc_curve *nettle_get_secp_384r1(void)
{
271
  return &_nettle_secp_384r1;
272
}