ecc-mul-a-test.c 2.56 KB
Newer Older
Niels Möller's avatar
Niels Möller committed
1 2 3 4 5
#include "testutils.h"

void
test_main (void)
{
6
  gmp_randstate_t rands;
Niels Möller's avatar
Niels Möller committed
7 8 9
  mpz_t r;
  unsigned i;

10
  gmp_randinit_default (rands);
Niels Möller's avatar
Niels Möller committed
11 12 13 14 15 16 17 18 19
  mpz_init (r);
  
  for (i = 0; ecc_curves[i]; i++)
    {
      const struct ecc_curve *ecc = ecc_curves[i];
      mp_size_t size = ecc_size (ecc);
      mp_limb_t *p = xalloc_limbs (ecc_size_j (ecc));
      mp_limb_t *q = xalloc_limbs (ecc_size_j (ecc));
      mp_limb_t *n = xalloc_limbs (size);
20
      mp_limb_t *scratch = xalloc_limbs (ecc->mul_itch);
Niels Möller's avatar
Niels Möller committed
21 22 23 24 25
      unsigned j;
      
      mpn_zero (n, size);

      n[0] = 1;
26 27
      ecc->mul (ecc, p, n, ecc->g, scratch);
      ecc->h_to_a (ecc, 0, p, p, scratch);
Niels Möller's avatar
Niels Möller committed
28 29

      if (mpn_cmp (p, ecc->g, 2*size != 0))
Niels Möller's avatar
Niels Möller committed
30
	die ("curve %d: ecc->mul with n = 1 failed.\n", ecc->p.bit_size);
Niels Möller's avatar
Niels Möller committed
31 32 33

      for (n[0] = 2; n[0] <= 4; n[0]++)
	{
34
	  ecc->mul (ecc, p, n, ecc->g, scratch);
35
	  test_ecc_mul_h (i, n[0], p);
Niels Möller's avatar
Niels Möller committed
36 37 38
	}

      /* (order - 1) * g = - g */
Niels Möller's avatar
Niels Möller committed
39
      mpn_sub_1 (n, ecc->q.m, size, 1);
40 41
      ecc->mul (ecc, p, n, ecc->g, scratch);
      ecc->h_to_a (ecc, 0, p, p, scratch);
Niels Möller's avatar
Niels Möller committed
42
      if (ecc->p.bit_size == 255)
43 44
	/* For edwards curves, - (x,y ) == (-x, y). FIXME: Swap x and
	   y, to get identical negation? */
Niels Möller's avatar
Niels Möller committed
45
	mpn_sub_n (p, ecc->p.m, p, size);
46
      else
Niels Möller's avatar
Niels Möller committed
47
	mpn_sub_n (p + size, ecc->p.m, p + size, size);
Niels Möller's avatar
Niels Möller committed
48 49
      if (mpn_cmp (p, ecc->g, 2*size) != 0)
	{
50
	  fprintf (stderr, "ecc->mul with n = order - 1 failed.\n");
Niels Möller's avatar
Niels Möller committed
51 52 53 54 55 56 57 58
	  abort ();
	}

      mpn_zero (n, size);

      for (j = 0; j < 100; j++)
	{
	  if (j & 1)
59
	    mpz_rrandomb (r, rands, size * GMP_NUMB_BITS);
Niels Möller's avatar
Niels Möller committed
60
	  else
61
	    mpz_urandomb (r, rands, size * GMP_NUMB_BITS);
Niels Möller's avatar
Niels Möller committed
62 63

	  /* Reduce so that (almost surely) n < q */
64
	  mpz_limbs_copy (n, r, size);
Niels Möller's avatar
Niels Möller committed
65
	  n[size - 1] %= ecc->q.m[size - 1];
Niels Möller's avatar
Niels Möller committed
66

67 68
	  ecc->mul (ecc, p, n, ecc->g, scratch);
	  ecc->h_to_a (ecc, 0, p, p, scratch);
Niels Möller's avatar
Niels Möller committed
69

70 71
	  ecc->mul_g (ecc, q, n, scratch);
	  ecc->h_to_a (ecc, 0, q, q, scratch);
Niels Möller's avatar
Niels Möller committed
72 73 74

	  if (mpn_cmp (p, q, 2*size))
	    {
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
	      fprintf (stderr,
		       "Different results from ecc->mul and ecc->mul_g.\n"
		       " bits = %u\n",
		       ecc->p.bit_size);
	      fprintf (stderr, " n = ");
	      mpn_out_str (stderr, 16, n, size);
	      
	      fprintf (stderr, "\np = ");
	      mpn_out_str (stderr, 16, p, size);
	      fprintf (stderr, ",\n    ");
	      mpn_out_str (stderr, 16, p + size, size);

	      fprintf (stderr, "\nq = ");
	      mpn_out_str (stderr, 16, q, size);
	      fprintf (stderr, ",\n    ");
	      mpn_out_str (stderr, 16, q + size, size);
	      fprintf (stderr, "\n");
Niels Möller's avatar
Niels Möller committed
92 93 94 95 96 97 98 99 100
	      abort ();
	    }
	}
      free (n);
      free (p);
      free (q);
      free (scratch);
    }
  mpz_clear (r); 
101
  gmp_randclear (rands);
Niels Möller's avatar
Niels Möller committed
102
}