ecc-curve448.c 8.34 KB
Newer Older
1
/* ecc-curve448.c
Daiki Ueno's avatar
Daiki Ueno committed
2 3 4 5 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 33 34 35 36 37 38

   Arithmetic and tables for curve448,

   Copyright (C) 2017 Daiki Ueno
   Copyright (C) 2017 Red Hat, Inc.

   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/.
*/

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

39 40
#include <assert.h>

Daiki Ueno's avatar
Daiki Ueno committed
41 42 43 44 45
#include "ecc.h"
#include "ecc-internal.h"

#define USE_REDC 0

46
#include "ecc-curve448.h"
Daiki Ueno's avatar
Daiki Ueno committed
47

48 49 50 51 52
#if HAVE_NATIVE_ecc_curve448_modp
#define ecc_448_modp nettle_ecc_curve448_modp
void
ecc_448_modp (const struct ecc_modulo *m, mp_limb_t *rp);
#elif GMP_NUMB_BITS == 64
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
static void
ecc_448_modp(const struct ecc_modulo *m, mp_limb_t *rp)
{
  /* Let B = 2^64, b = 2^32 = sqrt(B).
     p = B^7 - b B^3 - 1 ==> B^7 = b B^3 + 1

     We use this to reduce

     {r_{13}, ..., r_0} =
       {r_6,...,r_0}
     + {r_{10},...,r_7}
     + 2 {r_{13},r_{12}, r_{11}} B^4
     + b {r_{10},...,r_7,r_{13},r_{12},r_{11} (mod p)

     or

             +----+----+----+----+----+----+----+
             |r_6 |r_5 |r_4 |r_3 |r_2 |r_1 |r_0 |
             +----+----+----+----+----+----+----+
                            |r_10|r_9 |r_8 |r_7 |
             +----+----+----+----+----+----+----+
         2 * |r_13|r_12|r_11|
             +----+----+----+----+----+----+----+
      +  b * |r_10|r_9 |r_8 |r_7 |r_13|r_12|r_11|
      -------+----+----+----+----+----+----+----+
         c_7 |r_6 |r_5 |r_4 |r_3 |r_2 |r_1 |r_0 |
             +----+----+----+----+----+----+----+
  */
  mp_limb_t c3, c4, c7;
  mp_limb_t *tp = rp + 7;

  c4 = mpn_add_n (rp, rp, rp + 7, 4);
  c7 = mpn_addmul_1 (rp + 4, rp + 11, 3, 2);
  c3 = mpn_addmul_1 (rp, rp + 11, 3, (mp_limb_t) 1 << 32);
  c7 += mpn_addmul_1 (rp + 3, rp + 7, 4, (mp_limb_t) 1 << 32);
  tp[0] = c7;
  tp[1] = tp[2] = 0;
  tp[3] = c3 + (c7 << 32);
  tp[4] = c4 + (c7 >> 32) + (tp[3] < c3);
  tp[5] = tp[6] = 0;
  c7 = mpn_add_n (rp, rp, tp, 7);
  c7 = cnd_add_n (c7, rp, m->B, 7);
  assert (c7 == 0);
}
#else
#define ecc_448_modp ecc_mod
#endif

Daiki Ueno's avatar
Daiki Ueno committed
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
/* Needs 2*ecc->size limbs at rp, and 2*ecc->size additional limbs of
   scratch space. No overlap allowed. */
static void
ecc_mod_pow_2k (const struct ecc_modulo *m,
		mp_limb_t *rp, const mp_limb_t *xp,
		unsigned k, mp_limb_t *tp)
{
  if (k & 1)
    {
      ecc_mod_sqr (m, rp, xp);
      k--;
    }
  else
    {
      ecc_mod_sqr (m, tp, xp);
      ecc_mod_sqr (m, rp, tp);
      k -= 2;
    }
  while (k > 0)
    {
      ecc_mod_sqr (m, tp, rp);
      ecc_mod_sqr (m, rp, tp);
      k -= 2;
    }
}

127 128 129 130 131 132 133 134 135 136
static void
ecc_mod_pow_2kp1 (const struct ecc_modulo *m,
		  mp_limb_t *rp, const mp_limb_t *xp,
		  unsigned k, mp_limb_t *tp)
{
  ecc_mod_pow_2k (m, tp, xp, k, rp);
  ecc_mod_mul (m, rp, tp, xp);
}

/* Computes a^{(p-3)/4} = a^{2^446-2^222-1} mod m. Needs 5 * n scratch
Daiki Ueno's avatar
Daiki Ueno committed
137 138 139 140 141 142
   space. */
static void
ecc_mod_pow_446m224m1 (const struct ecc_modulo *p,
		       mp_limb_t *rp, const mp_limb_t *ap,
		       mp_limb_t *scratch)
{
143
/* Note overlap: operations writing to t0 clobber t1. */
Daiki Ueno's avatar
Daiki Ueno committed
144
#define t0 scratch
145 146
#define t1 (scratch + 1*ECC_LIMB_SIZE)
#define t2 (scratch + 3*ECC_LIMB_SIZE)
Daiki Ueno's avatar
Daiki Ueno committed
147 148 149 150 151

  ecc_mod_sqr (p, rp, ap);	        /* a^2 */
  ecc_mod_mul (p, t0, ap, rp);		/* a^3 */
  ecc_mod_sqr (p, rp, t0);		/* a^6 */
  ecc_mod_mul (p, t0, ap, rp);		/* a^{2^3-1} */
152 153

  ecc_mod_pow_2kp1 (p, t1, t0, 3, rp);	/* a^{2^6-1} */
Daiki Ueno's avatar
Daiki Ueno committed
154
  ecc_mod_pow_2k (p, rp, t1, 3, t2);	/* a^{2^9-2^3} */
155 156 157 158 159 160 161 162 163 164
  ecc_mod_mul (p, t2, t0, rp);		/* a^{2^9-1} */
  ecc_mod_pow_2kp1 (p, t0, t2, 9, rp);	/* a^{2^18-1} */

  ecc_mod_sqr (p, t1, t0);		/* a^{2^19-2} */
  ecc_mod_mul (p, rp, ap, t1);		/* a^{2^19-1} */
  ecc_mod_pow_2k (p, t1, rp, 18, t2);	/* a^{2^37-2^18} */
  ecc_mod_mul (p, rp, t0, t1);		/* a^{2^37-1} */
  mpn_copyi (t0, rp, p->size);

  ecc_mod_pow_2kp1 (p, rp, t0, 37, t2);	/* a^{2^74-1} */
Daiki Ueno's avatar
Daiki Ueno committed
165 166
  ecc_mod_pow_2k (p, t1, rp, 37, t2);	/* a^{2^111-2^37} */
  ecc_mod_mul (p, rp, t0, t1);		/* a^{2^111-1} */
167 168
  ecc_mod_pow_2kp1 (p, t0, rp, 111, t2);/* a^{2^222-1} */

Daiki Ueno's avatar
Daiki Ueno committed
169 170 171 172 173 174 175 176 177
  ecc_mod_sqr (p, t1, t0);		/* a^{2^223-2} */
  ecc_mod_mul (p, rp, ap, t1);		/* a^{2^223-1} */
  ecc_mod_pow_2k (p, t1, rp, 223, t2);	/* a^{2^446-2^223} */
  ecc_mod_mul (p, rp, t0, t1);		/* a^{2^446-2^222-1} */
#undef t0
#undef t1
#undef t2
}

178
#define ECC_448_INV_ITCH (5*ECC_LIMB_SIZE)
Daiki Ueno's avatar
Daiki Ueno committed
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 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219

static void ecc_448_inv (const struct ecc_modulo *p,
			 mp_limb_t *rp, const mp_limb_t *ap,
			 mp_limb_t *scratch)
{
#define t0 scratch

  ecc_mod_pow_446m224m1 (p, rp, ap, scratch); /* a^{2^446-2^222-1} */
  ecc_mod_sqr (p, t0, rp);		      /* a^{2^447-2^223-2} */
  ecc_mod_sqr (p, rp, t0);		      /* a^{2^448-2^224-4} */
  ecc_mod_mul (p, t0, ap, rp);		      /* a^{2^448-2^224-3} */

  mpn_copyi (rp, t0, ECC_LIMB_SIZE); /* FIXME: Eliminate copy? */
#undef t0
}

/* First, do a canonical reduction, then check if zero */
static int
ecc_448_zero_p (const struct ecc_modulo *p, mp_limb_t *xp)
{
  mp_limb_t cy;
  mp_limb_t w;
  mp_size_t i;
  cy = mpn_sub_n (xp, xp, p->m, ECC_LIMB_SIZE);
  cnd_add_n (cy, xp, p->m, ECC_LIMB_SIZE);

  for (i = 0, w = 0; i < ECC_LIMB_SIZE; i++)
    w |= xp[i];
  return w == 0;
}

/* Compute x such that x^2 = u/v (mod p). Returns one on success, zero
   on failure.

   To avoid a separate inversion, we use a trick of djb's, to
   compute the candidate root as

     x = (u/v)^{(p+1)/4} = u^3 v (u^5 v^3)^{(p-3)/4}.
*/

/* Needs 4*n space + scratch for ecc_mod_pow_446m224m1. */
220
#define ECC_448_SQRT_ITCH (9*ECC_LIMB_SIZE)
Daiki Ueno's avatar
Daiki Ueno committed
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286

static int
ecc_448_sqrt(const struct ecc_modulo *p, mp_limb_t *rp,
	     const mp_limb_t *up, const mp_limb_t *vp,
	     mp_limb_t *scratch)
{
#define u3v scratch
#define u5v3 (scratch + ECC_LIMB_SIZE)
#define u5v3p (scratch + 2*ECC_LIMB_SIZE)
#define u2 (scratch + 2*ECC_LIMB_SIZE)
#define u3 (scratch + 3*ECC_LIMB_SIZE)
#define uv (scratch + 2*ECC_LIMB_SIZE)
#define u2v2 (scratch + 3*ECC_LIMB_SIZE)

#define scratch_out (scratch + 4 * ECC_LIMB_SIZE)

#define x2 scratch
#define vx2 (scratch + ECC_LIMB_SIZE)
#define t0 (scratch + 2*ECC_LIMB_SIZE)

					/* Live values */
  ecc_mod_sqr (p, u2, up);		/* u2 */
  ecc_mod_mul (p, u3, u2, up);		/* u3 */
  ecc_mod_mul (p, u3v, u3, vp);		/* u3v */
  ecc_mod_mul (p, uv, up, vp);		/* u3v, uv */
  ecc_mod_sqr (p, u2v2, uv);		/* u3v, u2v2 */
  ecc_mod_mul (p, u5v3, u3v, u2v2);	/* u3v, u5v3 */
  ecc_mod_pow_446m224m1 (p, u5v3p, u5v3, scratch_out); /* u3v, u5v3p */
  ecc_mod_mul (p, rp, u5v3p, u3v);	/* none */

  /* If square root exists, have v x^2 = u */
  ecc_mod_sqr (p, x2, rp);
  ecc_mod_mul (p, vx2, x2, vp);
  ecc_mod_sub (p, t0, vx2, up);

  return ecc_448_zero_p (p, t0);

#undef u3v
#undef u5v3
#undef u5v3p
#undef u2
#undef u3
#undef uv
#undef u2v2
#undef scratch_out
#undef x2
#undef vx2
#undef t0
}

const struct ecc_curve _nettle_curve448 =
{
  {
    448,
    ECC_LIMB_SIZE,
    ECC_BMODP_SIZE,
    0,
    ECC_448_INV_ITCH,
    ECC_448_SQRT_ITCH,

    ecc_p,
    ecc_Bmodp,
    ecc_Bmodp_shifted,
    NULL,
    ecc_pp1h,

287 288
    ecc_448_modp,
    ecc_448_modp,
Daiki Ueno's avatar
Daiki Ueno committed
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322
    ecc_448_inv,
    ecc_448_sqrt,
  },
  {
    446,
    ECC_LIMB_SIZE,
    ECC_BMODQ_SIZE,
    0,
    ECC_MOD_INV_ITCH (ECC_LIMB_SIZE),
    0,

    ecc_q,
    ecc_Bmodq,
    ecc_Bmodq_shifted,
    NULL,
    ecc_qp1h,

    ecc_mod,	      /* FIXME: Implement optimized mod function */
    ecc_mod,	      /* FIXME: Implement optimized reduce function */
    ecc_mod_inv,
    NULL,
  },

  0, /* No redc */
  ECC_PIPPENGER_K,
  ECC_PIPPENGER_C,

  ECC_ADD_EH_ITCH (ECC_LIMB_SIZE),
  ECC_ADD_EHH_ITCH (ECC_LIMB_SIZE),
  ECC_DUP_EH_ITCH (ECC_LIMB_SIZE),
  ECC_MUL_A_EH_ITCH (ECC_LIMB_SIZE),
  ECC_MUL_G_EH_ITCH (ECC_LIMB_SIZE),
  ECC_EH_TO_A_ITCH (ECC_LIMB_SIZE, ECC_448_INV_ITCH),

323 324 325
  ecc_add_eh,
  ecc_add_ehh,
  ecc_dup_eh,
Daiki Ueno's avatar
Daiki Ueno committed
326 327 328 329 330 331 332 333 334
  ecc_mul_a_eh,
  ecc_mul_g_eh,
  ecc_eh_to_a,

  ecc_b,
  ecc_g,
  ecc_unit,
  ecc_table
};