ecc-mul-g-test.c 1.4 KB
Newer Older
Niels Möller's avatar
Niels Möller committed
1 2
#include "testutils.h"

3 4 5 6 7 8 9 10
#if NETTLE_USE_MINI_GMP
void
test_main (void)
{
  SKIP();
}
#else /* ! NETTLE_USE_MINI_GMP */

Niels Möller's avatar
Niels Möller committed
11 12 13
void
test_main (void)
{
14
  gmp_randstate_t rands;
Niels Möller's avatar
Niels Möller committed
15 16 17
  mpz_t r;
  unsigned i;

18
  gmp_randinit_default (rands);
Niels Möller's avatar
Niels Möller committed
19
  mpz_init (r);
Niels Möller's avatar
Niels Möller committed
20

Niels Möller's avatar
Niels Möller committed
21 22 23
  for (i = 0; ecc_curves[i]; i++)
    {
      const struct ecc_curve *ecc = ecc_curves[i];
Niels Möller's avatar
Niels Möller committed
24
      mp_size_t size = ecc_size (ecc);
Niels Möller's avatar
Niels Möller committed
25 26 27
      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);
28
      mp_limb_t *scratch = xalloc_limbs (ecc->mul_g_itch);
Niels Möller's avatar
Niels Möller committed
29

Niels Möller's avatar
Niels Möller committed
30 31 32
      mpn_zero (n, size);

      n[0] = 1;
33 34
      ecc->mul_g (ecc, p, n, scratch);
      ecc->h_to_a (ecc, 0, p, p, scratch);
Niels Möller's avatar
Niels Möller committed
35 36 37

      if (mpn_cmp (p, ecc->g, 2*size != 0))
	{
38
	  fprintf (stderr, "ecc->mul_g with n = 1 failed.\n");
Niels Möller's avatar
Niels Möller committed
39 40 41 42 43
	  abort ();
	}

      for (n[0] = 2; n[0] <= 4; n[0]++)
	{
44
	  ecc->mul_g (ecc, p, n, scratch);
45
	  test_ecc_mul_h (i, n[0], p);
Niels Möller's avatar
Niels Möller committed
46 47 48 49
	}

      /* (order - 1) * g = - g */
      mpn_sub_1 (n, ecc->q, size, 1);
50 51
      ecc->mul_g (ecc, p, n, scratch);
      ecc->h_to_a (ecc, 0, p, p, scratch);
Niels Möller's avatar
Niels Möller committed
52 53 54
      mpn_sub_n (p + size, ecc->p, p + size, size);
      if (mpn_cmp (p, ecc->g, 2*size) != 0)
	{
55
	  fprintf (stderr, "ecc->mul_g with n = order - 1 failed.\n");
Niels Möller's avatar
Niels Möller committed
56 57 58 59 60 61 62 63
	  abort ();
	}

      free (n);
      free (p);
      free (q);
      free (scratch);
    }
Niels Möller's avatar
Niels Möller committed
64
  mpz_clear (r);
65
  gmp_randclear (rands);
Niels Möller's avatar
Niels Möller committed
66
}
67
#endif /* ! NETTLE_USE_MINI_GMP */