eccdata.c 30.6 KB
Newer Older
1 2 3 4
/* eccdata.c

   Generate compile time constant (but machine dependent) tables.

5 6 7
   Copyright (C) 2013, 2014, 2017 Niels Möller
   Copyright (C) 2017 Daiki Ueno
   Copyright (C) 2017 Red Hat, Inc.
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

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

36
/* Development of Nettle's ECC support was funded by the .SE Internet Fund. */
37 38 39 40 41 42

#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

43
#include "mini-gmp.c"
44

45
/* Affine coordinates, for simplicity. Infinity point, i.e., te
46
   neutral group element, is represented using the is_zero flag. */
47 48
struct ecc_point
{
49
  int is_zero;
50 51 52 53
  mpz_t x;
  mpz_t y;
};

54 55 56 57
enum ecc_type
  {
    /* y^2 = x^3 - 3x + b (mod p) */
    ECC_TYPE_WEIERSTRASS,
58 59 60 61 62 63
#if 0
    /* x^2 + y^2 = 1 - d x^2 y^2 */
    ECC_TYPE_EDWARDS,
#endif
    /* -x^2 + y^2 = 1 - d x^2 y^2 */
    ECC_TYPE_TWISTED_EDWARDS,
64
  };
65 66 67 68 69 70 71

struct ecc_curve
{
  unsigned bit_size;
  unsigned pippenger_k;
  unsigned pippenger_c;

72 73
  enum ecc_type type;

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 101 102 103 104 105 106 107 108 109 110 111 112 113 114
  /* Prime */
  mpz_t p;
  mpz_t b;

  /* Curve order */
  mpz_t q;
  struct ecc_point g;

  /* Table for pippenger's algorithm.
     Element

       i 2^c + j_0 + j_1 2 + j_2 2^2 + ... + j_{c-1} 2^{c-1}

     holds

       2^{ikc} ( j_0 + j_1 2^k + j_2 2^{2k} + ... + j_{c-1} 2^{(c-1)k}) g
   */
  mp_size_t table_size;
  struct ecc_point *table;

  /* If non-NULL, holds 2g, 3g, 4g */
  struct ecc_point *ref;
};

static void
ecc_init (struct ecc_point *p)
{
  mpz_init (p->x);
  mpz_init (p->y);
}

static void
ecc_clear (struct ecc_point *p)
{
  mpz_clear (p->x);
  mpz_clear (p->y);
}

static int
ecc_zero_p (const struct ecc_point *p)
{
115
  return p->is_zero;
116 117 118 119 120
}

static int
ecc_equal_p (const struct ecc_point *p, const struct ecc_point *q)
{
121 122
  return p->is_zero ? q->is_zero
    : !q->is_zero && mpz_cmp (p->x, q->x) == 0 && mpz_cmp (p->y, q->y) == 0;
123 124 125
}

static void
126
ecc_set_zero (const struct ecc_curve *ecc, struct ecc_point *r)
127
{
128
  r->is_zero = 1;
129 130
  mpz_set_ui (r->x, 0);
  mpz_set_ui (r->y, ecc->type != ECC_TYPE_WEIERSTRASS);
131 132 133 134 135
}

static void
ecc_set (struct ecc_point *r, const struct ecc_point *p)
{
136
  r->is_zero = p->is_zero;
137 138 139 140
  mpz_set (r->x, p->x);
  mpz_set (r->y, p->y);
}

141 142 143 144
static void
ecc_add (const struct ecc_curve *ecc, struct ecc_point *r,
	 const struct ecc_point *p, const struct ecc_point *q);

145
/* Needs to support in-place operation. */
146 147 148 149
static void
ecc_dup (const struct ecc_curve *ecc,
	 struct ecc_point *r, const struct ecc_point *p)
{
150 151 152 153 154
  if (ecc->type != ECC_TYPE_WEIERSTRASS)
    {
      ecc_add (ecc, r, p, p);
      return;
    }
155
  if (ecc_zero_p (p))
156
    ecc_set_zero (ecc, r);
157 158 159 160

  else
    {
      mpz_t m, t, x, y;
161

162 163 164 165 166 167 168 169 170
      mpz_init (m);
      mpz_init (t);
      mpz_init (x);
      mpz_init (y);

      /* m = (2 y)^-1 */
      mpz_mul_ui (m, p->y, 2);
      mpz_invert (m, m, ecc->p);

171 172 173 174 175 176
      /* t = 3 (x^2 - 1) * m */
      mpz_mul (t, p->x, p->x);
      mpz_mod (t, t, ecc->p);
      mpz_sub_ui (t, t, 1);
      mpz_mul_ui (t, t, 3);

177
      mpz_mul (t, t, m);
178
      mpz_mod (t, t, ecc->p);
179 180 181

      /* x' = t^2 - 2 x */
      mpz_mul (x, t, t);
Niels Möller's avatar
Niels Möller committed
182
      mpz_submul_ui (x, p->x, 2);
183

184 185 186 187 188 189 190 191
      mpz_mod (x, x, ecc->p);

      /* y' = (x - x') * t - y */
      mpz_sub (y, p->x, x);
      mpz_mul (y, y, t);
      mpz_sub (y, y, p->y);
      mpz_mod (y, y, ecc->p);

192
      r->is_zero = 0;
193 194
      mpz_swap (x, r->x);
      mpz_swap (y, r->y);
195

196 197 198 199 200 201 202 203
      mpz_clear (m);
      mpz_clear (t);
      mpz_clear (x);
      mpz_clear (y);
    }
}

static void
204 205
ecc_add (const struct ecc_curve *ecc, struct ecc_point *r,
	 const struct ecc_point *p, const struct ecc_point *q)
206
{
207 208 209 210
  if (ecc->type == ECC_TYPE_WEIERSTRASS)
    {
      if (ecc_zero_p (p))
	ecc_set (r, q);
211

212 213
      else if (ecc_zero_p (q))
	ecc_set (r, p);
214

215 216 217 218 219 220 221
      else if (mpz_cmp (p->x, q->x) == 0)
	{
	  if (mpz_cmp (p->y, q->y) == 0)
	    ecc_dup (ecc, r, p);
	  else
	    ecc_set_zero (ecc, r);
	}
222
      else
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
	{
	  mpz_t s, t, x, y;
	  mpz_init (s);
	  mpz_init (t);
	  mpz_init (x);
	  mpz_init (y);

	  /* t = (q_y - p_y) / (q_x - p_x) */
	  mpz_sub (t, q->x, p->x);
	  mpz_invert (t, t, ecc->p);
	  mpz_sub (s, q->y, p->y);
	  mpz_mul (t, t, s);
	  mpz_mod (t, t, ecc->p);

	  /* x' = t^2 - p_x - q_x */
	  mpz_mul (x, t, t);
	  mpz_sub (x, x, p->x);
	  mpz_sub (x, x, q->x);
	  mpz_mod (x, x, ecc->p);

	  /* y' = (x - x') * t - y */
	  mpz_sub (y, p->x, x);
	  mpz_mul (y, y, t);
	  mpz_sub (y, y, p->y);
	  mpz_mod (y, y, ecc->p);

	  r->is_zero = 0;
	  mpz_swap (x, r->x);
	  mpz_swap (y, r->y);

	  mpz_clear (s);
	  mpz_clear (t);
	  mpz_clear (x);
	  mpz_clear (y);
	}
258 259 260
    }
  else
    {
261 262 263 264 265 266 267 268 269 270 271 272
      /* Untwisted:
	 x = (p_x q_y + p_y q_x) / (1 - d p_x p_y q_x q_y)
	 y = (p_y q_y - p_x q_x) / (1 + d p_x p_y q_x q_y)

	 Twisted:
	 x = (p_x q_y + p_y q_x) / (1 - d p_x p_y q_x q_y)
	 y = (p_y q_y + p_x q_x) / (1 + d p_x p_y q_x q_y)

	 So they differ only by a sign in the expression for the new y
	 coordinate.
      */

273 274 275 276 277 278
      mpz_t s, t, x, y;
      mpz_init (s);
      mpz_init (t);
      mpz_init (x);
      mpz_init (y);

279 280 281 282 283 284 285 286
      /* t = d p_x p_y q_x q_y */
      mpz_mul (t, ecc->b, p->x);
      mpz_mod (t, t, ecc->p);
      mpz_mul (t, t, p->y);
      mpz_mod (t, t, ecc->p);
      mpz_mul (t, t, q->x);
      mpz_mod (t, t, ecc->p);
      mpz_mul (t, t, q->y);
287 288
      mpz_mod (t, t, ecc->p);

289 290 291 292 293 294 295 296
      /* x' = (p_x q_y + q_x p_y) / (1 - t) */
      mpz_mul (x, p->x, q->y);
      mpz_mod (x, x, ecc->p);
      mpz_addmul (x, q->x, p->y);
      mpz_mod (x, x, ecc->p);
      mpz_ui_sub (s, 1, t);
      mpz_invert (s, s, ecc->p);
      mpz_mul (x, x, s);
297 298
      mpz_mod (x, x, ecc->p);

299 300 301 302 303 304 305 306
      /* y' = (p_y q_y - p_x q_x) / (1 + t) */
      mpz_mul (y, p->y, q->y);
      mpz_mod (y, y, ecc->p);
      mpz_addmul (y, p->x, q->x);
      mpz_mod (y, y, ecc->p);
      mpz_add_ui (s, t, 1);
      mpz_invert (s, s, ecc->p);
      mpz_mul (y, y, s);
307 308 309 310
      mpz_mod (y, y, ecc->p);

      mpz_swap (x, r->x);
      mpz_swap (y, r->y);
311
      r->is_zero = (mpz_cmp_ui (r->x, 0) == 0 && mpz_cmp_ui (r->y, 1) == 0);
312 313 314 315 316 317 318 319 320 321 322 323

      mpz_clear (s);
      mpz_clear (t);
      mpz_clear (x);
      mpz_clear (y);
    }
}

static void 
ecc_mul_binary (const struct ecc_curve *ecc,
		struct ecc_point *r, const mpz_t n, const struct ecc_point *p)
{
324 325 326
  /* Avoid the mp_bitcnt_t type for compatibility with older GMP
     versions. */
  unsigned k;
327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362

  assert (r != p);
  assert (mpz_sgn (n) > 0);

  ecc_set (r, p);

  /* Index of highest one bit */
  for (k = mpz_sizeinbase (n, 2) - 1; k-- > 0; )
    {
      ecc_dup (ecc, r, r);
      if (mpz_tstbit (n, k))
	ecc_add (ecc, r, r, p);
    }  
}

static struct ecc_point *
ecc_alloc (size_t n)
{
  struct ecc_point *p = malloc (n * sizeof(*p));
  size_t i;

  if (!p)
    {
      fprintf (stderr, "Virtual memory exhausted.\n");
      exit (EXIT_FAILURE);
    }
  for (i = 0; i < n; i++)
    ecc_init (&p[i]);

  return p;
}

static void
ecc_set_str (struct ecc_point *p,
	     const char *x, const char *y)
{
363
  p->is_zero = 0;
364 365 366 367 368
  mpz_set_str (p->x, x, 16);
  mpz_set_str (p->y, y, 16);  
}

static void
369
ecc_curve_init_str (struct ecc_curve *ecc, enum ecc_type type,
370
		    const char *p, const char *b, const char *q,
371
		    const char *gx, const char *gy,
372
		    const char *d, const char *t)
373
{
374 375
  ecc->type = type;

376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394
  mpz_init_set_str (ecc->p, p, 16);
  mpz_init_set_str (ecc->b, b, 16);
  mpz_init_set_str (ecc->q, q, 16);
  ecc_init (&ecc->g);
  ecc_set_str (&ecc->g, gx, gy);

  ecc->pippenger_k = 0;
  ecc->pippenger_c = 0;
  ecc->table = NULL;

  ecc->ref = NULL;
}

static void
ecc_curve_init (struct ecc_curve *ecc, unsigned bit_size)
{
  switch (bit_size)
    {
    case 192:      
395
      ecc_curve_init_str (ecc, ECC_TYPE_WEIERSTRASS,
396 397 398 399 400 401 402 403 404 405 406 407 408 409
			  /* p = 2^{192} - 2^{64} - 1 */
			  "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFE"
			  "FFFFFFFFFFFFFFFF",

			  "64210519e59c80e70fa7e9ab72243049"
			  "feb8deecc146b9b1", 

			  "ffffffffffffffffffffffff99def836"
			  "146bc9b1b4d22831",

			  "188da80eb03090f67cbf20eb43a18800"
			  "f4ff0afd82ff1012",

			  "07192b95ffc8da78631011ed6b24cdd5"
410
			  "73f977a11e794811",
411
			  NULL, NULL);
412 413 414 415 416 417 418 419 420 421 422 423 424 425 426
      ecc->ref = ecc_alloc (3);
      ecc_set_str (&ecc->ref[0], /* 2 g */
		   "dafebf5828783f2ad35534631588a3f629a70fb16982a888",
		   "dd6bda0d993da0fa46b27bbc141b868f59331afa5c7e93ab");
      
      ecc_set_str (&ecc->ref[1], /* 3 g */
		   "76e32a2557599e6edcd283201fb2b9aadfd0d359cbb263da",
		   "782c37e372ba4520aa62e0fed121d49ef3b543660cfd05fd");

      ecc_set_str (&ecc->ref[2], /* 4 g */
		   "35433907297cc378b0015703374729d7a4fe46647084e4ba",
		   "a2649984f2135c301ea3acb0776cd4f125389b311db3be32");

      break;
    case 224:
427
      ecc_curve_init_str (ecc, ECC_TYPE_WEIERSTRASS,
428 429 430 431 432 433 434 435 436 437 438 439 440 441
			  /* p = 2^{224} - 2^{96} + 1 */
			  "ffffffffffffffffffffffffffffffff"
			  "000000000000000000000001",

			  "b4050a850c04b3abf54132565044b0b7"
			  "d7bfd8ba270b39432355ffb4",

			  "ffffffffffffffffffffffffffff16a2"
			  "e0b8f03e13dd29455c5c2a3d",

			  "b70e0cbd6bb4bf7f321390b94a03c1d3"
			  "56c21122343280d6115c1d21",

			  "bd376388b5f723fb4c22dfe6cd4375a0"
442
			  "5a07476444d5819985007e34",
443
			  NULL, NULL);
444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459

      ecc->ref = ecc_alloc (3);
      ecc_set_str (&ecc->ref[0], /* 2 g */
		   "706a46dc76dcb76798e60e6d89474788d16dc18032d268fd1a704fa6",
		   "1c2b76a7bc25e7702a704fa986892849fca629487acf3709d2e4e8bb");
      
      ecc_set_str (&ecc->ref[1], /* 3 g */
		   "df1b1d66a551d0d31eff822558b9d2cc75c2180279fe0d08fd896d04",
		   "a3f7f03cadd0be444c0aa56830130ddf77d317344e1af3591981a925");

      ecc_set_str (&ecc->ref[2], /* 4 g */
		   "ae99feebb5d26945b54892092a8aee02912930fa41cd114e40447301",
		   "482580a0ec5bc47e88bc8c378632cd196cb3fa058a7114eb03054c9");

      break;
    case 256:
460
      ecc_curve_init_str (ecc, ECC_TYPE_WEIERSTRASS,
461 462 463 464 465 466 467 468 469 470 471 472 473 474
			  /* p = 2^{256} - 2^{224} + 2^{192} + 2^{96} - 1 */
			  "FFFFFFFF000000010000000000000000"
			  "00000000FFFFFFFFFFFFFFFFFFFFFFFF",

			  "5AC635D8AA3A93E7B3EBBD55769886BC"
			  "651D06B0CC53B0F63BCE3C3E27D2604B",

			  "FFFFFFFF00000000FFFFFFFFFFFFFFFF"
			  "BCE6FAADA7179E84F3B9CAC2FC632551",

			  "6B17D1F2E12C4247F8BCE6E563A440F2"
			  "77037D812DEB33A0F4A13945D898C296",

			  "4FE342E2FE1A7F9B8EE7EB4A7C0F9E16"
475
			  "2BCE33576B315ECECBB6406837BF51F5",
476
			  NULL, NULL);
477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492

      ecc->ref = ecc_alloc (3);
      ecc_set_str (&ecc->ref[0], /* 2 g */
		   "7cf27b188d034f7e8a52380304b51ac3c08969e277f21b35a60b48fc47669978",
		   "7775510db8ed040293d9ac69f7430dbba7dade63ce982299e04b79d227873d1");
      
      ecc_set_str (&ecc->ref[1], /* 3 g */
		   "5ecbe4d1a6330a44c8f7ef951d4bf165e6c6b721efada985fb41661bc6e7fd6c",
		   "8734640c4998ff7e374b06ce1a64a2ecd82ab036384fb83d9a79b127a27d5032");

      ecc_set_str (&ecc->ref[2], /* 4 g */
		   "e2534a3532d08fbba02dde659ee62bd0031fe2db785596ef509302446b030852",
		   "e0f1575a4c633cc719dfee5fda862d764efc96c3f30ee0055c42c23f184ed8c6");

      break;
    case 384:
493
      ecc_curve_init_str (ecc, ECC_TYPE_WEIERSTRASS,
494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512
			  /* p = 2^{384} - 2^{128} - 2^{96} + 2^{32} - 1 */
			  "ffffffffffffffffffffffffffffffff"
			  "fffffffffffffffffffffffffffffffe"
			  "ffffffff0000000000000000ffffffff",
			  
			  "b3312fa7e23ee7e4988e056be3f82d19"
			  "181d9c6efe8141120314088f5013875a"
			  "c656398d8a2ed19d2a85c8edd3ec2aef",
			  
			  "ffffffffffffffffffffffffffffffff"
			  "ffffffffffffffffc7634d81f4372ddf"
			  "581a0db248b0a77aecec196accc52973",
			  
			  "aa87ca22be8b05378eb1c71ef320ad74"
			  "6e1d3b628ba79b9859f741e082542a38"
			  "5502f25dbf55296c3a545e3872760ab7",
			  
			  "3617de4a96262c6f5d9e98bf9292dc29"
			  "f8f41dbd289a147ce9da3113b5f0b8c0"
513
			  "0a60b1ce1d7e819d7a431d7c90ea0e5f",
514
			  NULL, NULL);
515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530

      ecc->ref = ecc_alloc (3);
      ecc_set_str (&ecc->ref[0], /* 2 g */
		   "8d999057ba3d2d969260045c55b97f089025959a6f434d651d207d19fb96e9e4fe0e86ebe0e64f85b96a9c75295df61",
		   "8e80f1fa5b1b3cedb7bfe8dffd6dba74b275d875bc6cc43e904e505f256ab4255ffd43e94d39e22d61501e700a940e80");

      ecc_set_str (&ecc->ref[1], /* 3 g */
		   "77a41d4606ffa1464793c7e5fdc7d98cb9d3910202dcd06bea4f240d3566da6b408bbae5026580d02d7e5c70500c831",
		   "c995f7ca0b0c42837d0bbe9602a9fc998520b41c85115aa5f7684c0edc111eacc24abd6be4b5d298b65f28600a2f1df1");

      ecc_set_str (&ecc->ref[2], /* 4 g */
		   "138251cd52ac9298c1c8aad977321deb97e709bd0b4ca0aca55dc8ad51dcfc9d1589a1597e3a5120e1efd631c63e1835",
		   "cacae29869a62e1631e8a28181ab56616dc45d918abc09f3ab0e63cf792aa4dced7387be37bba569549f1c02b270ed67");

      break;
    case 521:
531
      ecc_curve_init_str (ecc, ECC_TYPE_WEIERSTRASS,
532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559
			  "1ff" /* p = 2^{521} - 1 */
			  "ffffffffffffffffffffffffffffffff"
			  "ffffffffffffffffffffffffffffffff"
			  "ffffffffffffffffffffffffffffffff"
			  "ffffffffffffffffffffffffffffffff",

			  "051"
			  "953eb9618e1c9a1f929a21a0b68540ee"
			  "a2da725b99b315f3b8b489918ef109e1"
			  "56193951ec7e937b1652c0bd3bb1bf07"
			  "3573df883d2c34f1ef451fd46b503f00",

			  "1ff"
			  "ffffffffffffffffffffffffffffffff"
			  "fffffffffffffffffffffffffffffffa"
			  "51868783bf2f966b7fcc0148f709a5d0"
			  "3bb5c9b8899c47aebb6fb71e91386409",

			  "c6"
			  "858e06b70404e9cd9e3ecb662395b442"
			  "9c648139053fb521f828af606b4d3dba"
			  "a14b5e77efe75928fe1dc127a2ffa8de"
			  "3348b3c1856a429bf97e7e31c2e5bd66",

			  "118"
			  "39296a789a3bc0045c8a5fb42c7d1bd9"
			  "98f54449579b446817afbd17273e662c"
			  "97ee72995ef42640c550b9013fad0761"
560
			  "353c7086a272c24088be94769fd16650",
561
			  NULL, NULL);
562 563 564 565 566 567 568 569 570 571 572 573 574 575 576

      ecc->ref = ecc_alloc (3);
      ecc_set_str (&ecc->ref[0], /* 2 g */
		   "433c219024277e7e682fcb288148c282747403279b1ccc06352c6e5505d769be97b3b204da6ef55507aa104a3a35c5af41cf2fa364d60fd967f43e3933ba6d783d",
		   "f4bb8cc7f86db26700a7f3eceeeed3f0b5c6b5107c4da97740ab21a29906c42dbbb3e377de9f251f6b93937fa99a3248f4eafcbe95edc0f4f71be356d661f41b02");
      
      ecc_set_str (&ecc->ref[1], /* 3 g */
		   "1a73d352443de29195dd91d6a64b5959479b52a6e5b123d9ab9e5ad7a112d7a8dd1ad3f164a3a4832051da6bd16b59fe21baeb490862c32ea05a5919d2ede37ad7d",
		   "13e9b03b97dfa62ddd9979f86c6cab814f2f1557fa82a9d0317d2f8ab1fa355ceec2e2dd4cf8dc575b02d5aced1dec3c70cf105c9bc93a590425f588ca1ee86c0e5");

      ecc_set_str (&ecc->ref[2], /* 4 g */
		   "35b5df64ae2ac204c354b483487c9070cdc61c891c5ff39afc06c5d55541d3ceac8659e24afe3d0750e8b88e9f078af066a1d5025b08e5a5e2fbc87412871902f3",
		   "82096f84261279d2b673e0178eb0b4abb65521aef6e6e32e1b5ae63fe2f19907f279f283e54ba385405224f750a95b85eebb7faef04699d1d9e21f47fc346e4d0d");

      break;
577
    case 255:
578
      /* Edwards curve used for eddsa25519 and curve25519,
579

580
	   -x^2 + y^2 = 1 - (121665/121666) x^2 y^2, with p = 2^{255} - 19.
581

582 583 584
	 The generator is
	   x = 0x216936d3cd6e53fec0a4e231fdd6dc5c692cc7609525a7b2c9562d608f25d51a
           y = 0x6666666666666666666666666666666666666666666666666666666666666658
585

586
	 Also birationally equivalent to the curve25519 Montgomery curve,
587

588 589 590
	   y^2 = x^3 + 486662 x^2 + x (mod p)
      */
      ecc_curve_init_str (ecc, ECC_TYPE_TWISTED_EDWARDS,
591 592
			  "7fffffffffffffffffffffffffffffff"
			  "ffffffffffffffffffffffffffffffed",
593 594 595 596 597
			  /* (121665/121666) mod p, from PARI/GP
			     c = Mod(121665, p); c / (c+1)
			  */
			  "2dfc9311d490018c7338bf8688861767"
			  "ff8ff5b2bebe27548a14b235eca6874a",
598 599 600 601
			  /* Order of the subgroup is 2^252 + q_0, where
			     q_0 = 27742317777372353535851937790883648493,
			     125 bits.
			  */
602 603
			  "10000000000000000000000000000000"
			  "14def9dea2f79cd65812631a5cf5d3ed",
604 605 606 607 608
			  /* Generator */
			  "216936d3cd6e53fec0a4e231fdd6dc5c"
			  "692cc7609525a7b2c9562d608f25d51a",
			  "66666666666666666666666666666666"
			  "66666666666666666666666666666658",
609 610 611 612 613
			  /* (121665/121666) mod p, from PARI/GP
			     c = Mod(121665, p); c / (c+1)
			  */
			  "2dfc9311d490018c7338bf8688861767"
			  "ff8ff5b2bebe27548a14b235eca6874a",
614 615 616 617 618 619 620 621 622
			  /* A square root of -486664 mod p, PARI/GP
			     -sqrt(Mod(-486664, p)) in PARI/GP.

			     Sign is important to map to the right
			     generator on the twisted edwards curve
			     used for EdDSA. */
			  "70d9120b9f5ff9442d84f723fc03b081"
			  "3a5e2c2eb482e57d3391fb5500ba81e7"
			  );
623 624
      ecc->ref = ecc_alloc (3);
      ecc_set_str (&ecc->ref[0], /* 2 g */
625 626 627 628
		   "36ab384c9f5a046c3d043b7d1833e7ac"
		   "080d8e4515d7a45f83c5a14e2843ce0e",
		   "2260cdf3092329c21da25ee8c9a21f56"
		   "97390f51643851560e5f46ae6af8a3c9");
629
      ecc_set_str (&ecc->ref[1], /* 3 g */
630 631 632 633
		   "67ae9c4a22928f491ff4ae743edac83a"
		   "6343981981624886ac62485fd3f8e25c",
		   "1267b1d177ee69aba126a18e60269ef7"
		   "9f16ec176724030402c3684878f5b4d4");
634 635

      ecc_set_str (&ecc->ref[2], /* 4 g */
636 637 638 639
		   "203da8db56cff1468325d4b87a3520f9"
		   "1a739ec193ce1547493aa657c4c9f870",
		   "47d0e827cb1595e1470eb88580d5716c"
		   "4cf22832ea2f0ff0df38ab61ca32112f");
640 641
      break;

642 643 644 645 646 647 648
    default:
      fprintf (stderr, "No known curve for size %d\n", bit_size);
      exit(EXIT_FAILURE);     
    }
  ecc->bit_size = bit_size;
}

649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671
static void
ecc_curve_clear (struct ecc_curve *ecc)
{
  mpz_clear (ecc->p);
  mpz_clear (ecc->b);
  mpz_clear (ecc->q);
  ecc_clear (&ecc->g);
  if (ecc->table)
    {
      size_t i;
      for (i = 0; i < ecc->table_size; i++)
	ecc_clear (&ecc->table[i]);
      free (ecc->table);
    }
  if (ecc->ref)
    {
      size_t i;
      for (i = 0; i < 3; i++)
	ecc_clear (&ecc->ref[i]);
      free (ecc->ref);
    }
}

672 673 674 675 676 677 678 679
static unsigned
ecc_table_size(unsigned bits, unsigned k, unsigned c)
{
  unsigned p = (bits + k-1) / k;
  unsigned M = (p + c-1)/c;
  return M;
}

680 681 682
static void
ecc_pippenger_precompute (struct ecc_curve *ecc, unsigned k, unsigned c)
{
683
  unsigned M = ecc_table_size (ecc->bit_size, k, c);
684 685
  unsigned i, j;

686 687 688 689 690 691
  if (M < 2)
    {
      fprintf (stderr, "Invalid parameters, implies M = %u\n", M);
      exit (EXIT_FAILURE);
    }

692 693 694 695 696
  if (M == ecc_table_size (ecc->bit_size, k-1, c))
    fprintf(stderr,
	    "warn: Parameters k = %u, c = %d are suboptimal, could use smaller k\n",
	    k, c);

697 698 699
  ecc->pippenger_k = k;
  ecc->pippenger_c = c;
  ecc->table_size = M << c;
Niels Möller's avatar
Niels Möller committed
700
  assert (ecc->table_size >= 2);
701
  ecc->table = ecc_alloc (ecc->table_size);
702

703
  /* Compute the first 2^c entries */
704
  ecc_set_zero (ecc, &ecc->table[0]);
705 706 707 708 709
  ecc_set (&ecc->table[1], &ecc->g);

  for (j = 2; j < (1U<<c); j <<= 1)
    {
      /* T[j] = 2^k T[j/2] */
710
      assert (j < ecc->table_size);
711 712 713 714 715
      ecc_dup (ecc, &ecc->table[j], &ecc->table[j/2]);
      for (i = 1; i < k; i++)
	ecc_dup (ecc, &ecc->table[j], &ecc->table[j]);

      for (i = 1; i < j; i++)
716 717 718 719
	{
	  assert (j + i < ecc->table_size);
	  ecc_add (ecc, &ecc->table[j + i], &ecc->table[j], &ecc->table[i]);
	}
720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741
    }
  for (j = 1<<c; j < ecc->table_size; j++)
    {
      /* T[j] = 2^{kc} T[j-2^c] */
      ecc_dup (ecc, &ecc->table[j], &ecc->table[j - (1<<c)]);
      for (i = 1; i < k*c; i++)
	ecc_dup (ecc, &ecc->table[j], &ecc->table[j]);
    }
}

static void
ecc_mul_pippenger (const struct ecc_curve *ecc,
		   struct ecc_point *r, const mpz_t n_input)
{
  mpz_t n;
  unsigned k, c;
  unsigned i, j;
  unsigned bit_rows;

  mpz_init (n);
  
  mpz_mod (n, n_input, ecc->q);
742
  ecc_set_zero (ecc, r);
743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770

  k = ecc->pippenger_k;
  c = ecc->pippenger_c;

  bit_rows = (ecc->bit_size + k - 1) / k;

  for (i = k; i-- > 0; )
    {
      ecc_dup (ecc, r, r);
      for (j = 0; j * c < bit_rows; j++)
	{
	  unsigned bits;
	  mp_size_t bit_index;
	  
	  /* Extract c bits of the exponent, stride k, starting at i + kcj, ending at
	    i + k (cj + c - 1)*/
	  for (bits = 0, bit_index = i + k*(c*j+c); bit_index > i + k*c*j; )
	    {
	      bit_index -= k;
	      bits = (bits << 1) | mpz_tstbit (n, bit_index);
	    }

	  ecc_add (ecc, r, r, &ecc->table[(j << c) | bits]);
	}
    }
  mpz_clear (n);
}

771 772 773 774 775 776 777
static void
ecc_point_out (FILE *f, const struct ecc_point *p)
{
  if (p->is_zero)
    fprintf (f, "zero");
  else
    {
778 779 780 781 782
	fprintf (f, "(");
	mpz_out_str (f, 16, p->x);
	fprintf (f, ",\n     ");
	mpz_out_str (f, 16, (p)->y);
	fprintf (f, ")");
783 784
    }
}
785 786 787 788 789
#define ASSERT_EQUAL(p, q) do {						\
    if (!ecc_equal_p (p, q))						\
      {									\
	fprintf (stderr, "%s:%d: ASSERT_EQUAL (%s, %s) failed.\n",	\
		 __FILE__, __LINE__, #p, #q);				\
790 791 792 793 794
	fprintf (stderr, "p = ");					\
	ecc_point_out (stderr, (p));					\
	fprintf (stderr, "\nq = ");					\
	ecc_point_out (stderr, (q));					\
	fprintf (stderr, "\n");						\
795 796 797 798 799 800 801 802 803
	abort();							\
      }									\
  } while (0)

#define ASSERT_ZERO(p) do {						\
    if (!ecc_zero_p (p))						\
      {									\
	fprintf (stderr, "%s:%d: ASSERT_ZERO (%s) failed.\n",		\
		 __FILE__, __LINE__, #p);				\
804 805 806
	fprintf (stderr, "p = ");					\
	ecc_point_out (stderr, (p));					\
	fprintf (stderr, "\n");						\
807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824
	abort();							\
      }									\
  } while (0)

static void
ecc_curve_check (const struct ecc_curve *ecc)
{
  struct ecc_point p, q;
  mpz_t n;

  ecc_init (&p);
  ecc_init (&q);
  mpz_init (n);

  ecc_dup (ecc, &p, &ecc->g);
  if (ecc->ref)
    ASSERT_EQUAL (&p, &ecc->ref[0]);
  else
825 826 827 828 829 830 831
    {
      fprintf (stderr, "g2 = ");
      mpz_out_str (stderr, 16, p.x);
      fprintf (stderr, "\n     ");
      mpz_out_str (stderr, 16, p.y);
      fprintf (stderr, "\n");
    }
832 833 834 835
  ecc_add (ecc, &q, &p, &ecc->g);
  if (ecc->ref)
    ASSERT_EQUAL (&q, &ecc->ref[1]);
  else
836 837 838 839 840 841 842
    {
      fprintf (stderr, "g3 = ");
      mpz_out_str (stderr, 16, q.x);
      fprintf (stderr, "\n     ");
      mpz_out_str (stderr, 16, q.y);
      fprintf (stderr, "\n");
    }
843 844 845 846 847

  ecc_add (ecc, &q, &q, &ecc->g);
  if (ecc->ref)
    ASSERT_EQUAL (&q, &ecc->ref[2]);
  else
848 849 850 851 852 853 854
    {
      fprintf (stderr, "g4 = ");
      mpz_out_str (stderr, 16, q.x);
      fprintf (stderr, "\n     ");
      mpz_out_str (stderr, 16, q.y);
      fprintf (stderr, "\n");
    }
855 856 857 858 859

  ecc_dup (ecc, &q, &p);
  if (ecc->ref)
    ASSERT_EQUAL (&q, &ecc->ref[2]);
  else
860 861 862 863 864 865 866
    {
      fprintf (stderr, "g4 = ");
      mpz_out_str (stderr, 16, q.x);
      fprintf (stderr, "\n     ");
      mpz_out_str (stderr, 16, q.y);
      fprintf (stderr, "\n");
    }
867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905

  ecc_mul_binary (ecc, &p, ecc->q, &ecc->g);
  ASSERT_ZERO (&p);

  ecc_mul_pippenger (ecc, &q, ecc->q);
  ASSERT_ZERO (&q);

  ecc_clear (&p);
  ecc_clear (&q);
  mpz_clear (n);
}

static void
output_digits (const mpz_t x,
	       unsigned size, unsigned bits_per_limb)
{  
  mpz_t t;
  mpz_t mask;
  mpz_t limb;
  unsigned i;
  const char *suffix;

  mpz_init (t);
  mpz_init (mask);
  mpz_init (limb);

  mpz_setbit (mask, bits_per_limb);
  mpz_sub_ui (mask, mask, 1);

  suffix = bits_per_limb > 32 ? "ULL" : "UL";

  mpz_init_set (t, x);

  for (i = 0; i < size; i++)
    {
      if ( (i % 8) == 0)
	printf("\n ");
      
      mpz_and (limb, mask, t);
906 907 908
      printf (" 0x");
      mpz_out_str (stdout, 16, limb);
      printf ("%s,", suffix);
909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926
      mpz_tdiv_q_2exp (t, t, bits_per_limb);
    }

  mpz_clear (t);
  mpz_clear (mask);
  mpz_clear (limb);
}

static void
output_bignum (const char *name, const mpz_t x,
	       unsigned size, unsigned bits_per_limb)
{  
  printf ("static const mp_limb_t %s[%d] = {", name, size);
  output_digits (x, size, bits_per_limb);
  printf("\n};\n");
}

static void
927 928
output_point (const char *name, const struct ecc_curve *ecc,
	      const struct ecc_point *p, int use_redc,
929 930
	      unsigned size, unsigned bits_per_limb)
{
931
  mpz_t x, y, t;
932

933 934
  mpz_init (x);
  mpz_init (y);
935
  mpz_init (t);
936
 
937 938 939
  if (name)
    printf("static const mp_limb_t %s[%u] = {", name, 2*size);

940 941
  mpz_set (x, p->x);
  mpz_set (y, p->y);
942 943 944 945 946 947 948 949

  if (use_redc)
    {
      mpz_mul_2exp (x, x, size * bits_per_limb);
      mpz_mod (x, x, ecc->p);
      mpz_mul_2exp (y, y, size * bits_per_limb);
      mpz_mod (y, y, ecc->p);
    }
950
      
951 952
  output_digits (x, size, bits_per_limb);
  output_digits (y, size, bits_per_limb);
953 954 955 956

  if (name)
    printf("\n};\n");

957 958
  mpz_clear (x);
  mpz_clear (y);
959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985
  mpz_clear (t);
}

static unsigned
output_modulo (const char *name, const mpz_t x,
	       unsigned size, unsigned bits_per_limb)
{
  mpz_t mod;
  unsigned bits;

  mpz_init (mod);

  mpz_setbit (mod, bits_per_limb * size);
  mpz_mod (mod, mod, x);

  bits = mpz_sizeinbase (mod, 2);
  output_bignum (name, mod, size, bits_per_limb);
  
  mpz_clear (mod);
  return bits;
}

static void
output_curve (const struct ecc_curve *ecc, unsigned bits_per_limb)
{
  unsigned limb_size = (ecc->bit_size + bits_per_limb - 1)/bits_per_limb;
  unsigned i;
986
  unsigned bits, e;
987 988 989 990 991 992 993 994 995 996 997 998 999 1000
  int redc_limbs;
  mpz_t t;

  mpz_init (t);

  printf ("/* For NULL. */\n#include <stddef.h>\n");

  printf ("#define ECC_LIMB_SIZE %u\n", limb_size);
  printf ("#define ECC_PIPPENGER_K %u\n", ecc->pippenger_k);
  printf ("#define ECC_PIPPENGER_C %u\n", ecc->pippenger_c);

  output_bignum ("ecc_p", ecc->p, limb_size, bits_per_limb);
  output_bignum ("ecc_b", ecc->b, limb_size, bits_per_limb);
  output_bignum ("ecc_q", ecc->q, limb_size, bits_per_limb);
1001
  output_point ("ecc_g", ecc, &ecc->g, 0, limb_size, bits_per_limb);
1002 1003 1004 1005 1006 1007 1008
  
  bits = output_modulo ("ecc_Bmodp", ecc->p, limb_size, bits_per_limb);
  printf ("#define ECC_BMODP_SIZE %u\n",
	  (bits + bits_per_limb - 1) / bits_per_limb);
  bits = output_modulo ("ecc_Bmodq", ecc->q, limb_size, bits_per_limb);
  printf ("#define ECC_BMODQ_SIZE %u\n",
	  (bits + bits_per_limb - 1) / bits_per_limb);
Niels Möller's avatar
Niels Möller committed
1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030
  bits = mpz_sizeinbase (ecc->q, 2);
  if (bits < ecc->bit_size)
    {
      /* for curve25519, with q = 2^k + q', with a much smaller q' */
      unsigned mbits;
      unsigned shift;

      /* Shift to align the one bit at B */
      shift = bits_per_limb * limb_size + 1 - bits;
      
      mpz_set (t, ecc->q);
      mpz_clrbit (t, bits-1);
      mbits = mpz_sizeinbase (t, 2);

      /* The shifted value must be a limb smaller than q. */
      if (mbits + shift + bits_per_limb <= bits)
	{
	  /* q of the form 2^k + q', with q' a limb smaller */
	  mpz_mul_2exp (t, t, shift);
	  output_bignum ("ecc_mBmodq_shifted", t, limb_size, bits_per_limb);
	}
    }
1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066

  if (ecc->bit_size < limb_size * bits_per_limb)
    {
      int shift;

      mpz_set_ui (t, 0);
      mpz_setbit (t, ecc->bit_size);
      mpz_sub (t, t, ecc->p);      
      output_bignum ("ecc_Bmodp_shifted", t, limb_size, bits_per_limb);

      shift = limb_size * bits_per_limb - ecc->bit_size;
      if (shift > 0)
	{
	  /* Check condition for reducing hi limbs. If s is the
	     normalization shift and n is the bit size (so that s + n
	     = limb_size * bite_per_limb), then we need

	       (2^n - 1) + (2^s - 1) (2^n - p) < 2p

	     or equivalently,

	       2^s (2^n - p) <= p

	     To a allow a carry limb to be added in at the same time,
	     substitute s+1 for s.
	  */
	  /* FIXME: For ecdsa verify, we actually need the stricter
	     inequality < 2 q. */
	  mpz_mul_2exp (t, t, shift + 1);
	  if (mpz_cmp (t, ecc->p) > 0)
	    {
	      fprintf (stderr, "Reduction condition failed for %u-bit curve.\n",
		       ecc->bit_size);
	      exit (EXIT_FAILURE);
	    }
	}
1067 1068 1069 1070 1071 1072
    }
  else
    printf ("#define ecc_Bmodp_shifted ecc_Bmodp\n");

  if (bits < limb_size * bits_per_limb)
    {
1073
      mpz_set_ui (t, 0);
1074
      mpz_setbit (t, bits);
1075 1076 1077 1078
      mpz_sub (t, t, ecc->q);      
      output_bignum ("ecc_Bmodq_shifted", t, limb_size, bits_per_limb);      
    }
  else
1079
    printf ("#define ecc_Bmodq_shifted ecc_Bmodq\n");
1080 1081 1082 1083 1084 1085 1086 1087

  mpz_add_ui (t, ecc->p, 1);
  mpz_fdiv_q_2exp (t, t, 1);
  output_bignum ("ecc_pp1h", t, limb_size, bits_per_limb);      

  mpz_add_ui (t, ecc->q, 1);
  mpz_fdiv_q_2exp (t, t, 1);
  output_bignum ("ecc_qp1h", t, limb_size, bits_per_limb);  
1088

1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112
  /* Trailing zeros in p+1 correspond to trailing ones in p. */
  redc_limbs = mpz_scan0 (ecc->p, 0) / bits_per_limb;
  if (redc_limbs > 0)
    {
      mpz_add_ui (t, ecc->p, 1);
      mpz_fdiv_q_2exp (t, t, redc_limbs * bits_per_limb);
      output_bignum ("ecc_redc_ppm1", t, limb_size - redc_limbs, bits_per_limb);
    }
  else
    {    
      /* Trailing zeros in p-1 correspond to zeros just above the low
	 bit of p */
      redc_limbs = mpz_scan1 (ecc->p, 1) / bits_per_limb;
      if (redc_limbs > 0)
	{
	  printf ("#define ecc_redc_ppm1 (ecc_p + %d)\n",
		  redc_limbs);
	  redc_limbs = -redc_limbs;
	}
      else
	printf ("#define ecc_redc_ppm1 NULL\n");
    }
  printf ("#define ECC_REDC_SIZE %d\n", redc_limbs);

1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168
  /* For mod p square root computation. */
  if (mpz_fdiv_ui (ecc->p, 4) == 3)
    {
      /* x = a^{(p+1)/4} gives square root of a (if it exists,
	 otherwise the square root of -a). */
      e = 1;
      mpz_add_ui (t, ecc->p, 1);
      mpz_fdiv_q_2exp (t, t, 2); 
    }
  else
    {
      /* p-1 = 2^e s, s odd, t = (s-1)/2*/
      unsigned g, i;
      mpz_t s;
      mpz_t z;

      mpz_init (s);
      mpz_init (z);

      mpz_sub_ui (s, ecc->p, 1);
      e = mpz_scan1 (s, 0);
      assert (e > 1);

      mpz_fdiv_q_2exp (s, s, e);

      /* Find a non-square g, g^{(p-1)/2} = -1,
	 and z = g^{(p-1)/4 */
      for (g = 2; ; g++)
	{
	  mpz_set_ui (z, g);
	  mpz_powm (z, z, s, ecc->p);
	  mpz_mul (t, z, z);
	  mpz_mod (t, t, ecc->p);

	  for (i = 2; i < e; i++)
	    {
	      mpz_mul (t, t, t);
	      mpz_mod (t, t, ecc->p);
	    }
	  if (mpz_cmp_ui (t, 1) != 0)
	    break;
	}
      mpz_add_ui (t, t, 1);
      assert (mpz_cmp (t, ecc->p) == 0);
      output_bignum ("ecc_sqrt_z", z, limb_size, bits_per_limb);

      mpz_fdiv_q_2exp (t, s, 1);

      mpz_clear (s);
      mpz_clear (z);
    }
  printf ("#define ECC_SQRT_E %u\n", e);
  printf ("#define ECC_SQRT_T_BITS %u\n",
	  (unsigned) mpz_sizeinbase (t, 2));
  output_bignum ("ecc_sqrt_t", t, limb_size, bits_per_limb);      

1169 1170 1171 1172 1173 1174
  printf ("#if USE_REDC\n");
  printf ("#define ecc_unit ecc_Bmodp\n");

  printf ("static const mp_limb_t ecc_table[%lu] = {",
	 (unsigned long) (2*ecc->table_size * limb_size));
  for (i = 0; i < ecc->table_size; i++)
1175
    output_point (NULL, ecc, &ecc->table[i], 1, limb_size, bits_per_limb);
1176 1177 1178 1179 1180

  printf("\n};\n");

  printf ("#else\n");

Niels Möller's avatar
Niels Möller committed
1181
  mpz_set_ui (t, 1);
1182 1183 1184 1185 1186
  output_bignum ("ecc_unit", t, limb_size, bits_per_limb);
  
  printf ("static const mp_limb_t ecc_table[%lu] = {",
	 (unsigned long) (2*ecc->table_size * limb_size));
  for (i = 0; i < ecc->table_size; i++)
1187
    output_point (NULL, ecc, &ecc->table[i], 0, limb_size, bits_per_limb);
1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217

  printf("\n};\n");
  printf ("#endif\n");
  
  mpz_clear (t);
}

int
main (int argc, char **argv)
{
  struct ecc_curve ecc;

  if (argc < 4)
    {
      fprintf (stderr, "Usage: %s CURVE-BITS K C [BITS-PER-LIMB]\n", argv[0]);
      return EXIT_FAILURE;
    }

  ecc_curve_init (&ecc, atoi(argv[1]));

  ecc_pippenger_precompute (&ecc, atoi(argv[2]), atoi(argv[3]));

  fprintf (stderr, "Table size: %lu entries\n",
	   (unsigned long) ecc.table_size);

  ecc_curve_check (&ecc);

  if (argc > 4)
    output_curve (&ecc, atoi(argv[4]));

1218
  ecc_curve_clear (&ecc);
1219 1220
  return EXIT_SUCCESS;
}