ecc-redc-test.c 2.45 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 14 15 16 17 18 19 20
static void
ref_redc (mp_limb_t *rp, const mp_limb_t *ap, const mp_limb_t *mp, mp_size_t mn)
{
  mpz_t t;
  mpz_t m, a;
  mp_size_t an;

  mpz_init (t);
  mpz_setbit (t, mn * GMP_NUMB_BITS);

21
  mpz_roinit_n (m, mp, mn);
Niels Möller's avatar
Niels Möller committed
22 23 24 25 26

  an = 2*mn;
  while (an > 0 && ap[an-1] == 0)
    an--;

27
  mpz_roinit_n (a, ap, an);
Niels Möller's avatar
Niels Möller committed
28 29 30 31 32
  
  mpz_invert (t, t, m);
  mpz_mul (t, t, a);
  mpz_mod (t, t, m);

33
  mpz_limbs_copy (rp, t, mn);
Niels Möller's avatar
Niels Möller committed
34 35 36 37 38 39

  mpz_clear (t);
}

#define MAX_ECC_SIZE (1 + 521 / GMP_NUMB_BITS)
#define MAX_SIZE (2*MAX_ECC_SIZE)
40
#define COUNT 50000
Niels Möller's avatar
Niels Möller committed
41 42 43 44

void
test_main (void)
{
45
  gmp_randstate_t rands;
Niels Möller's avatar
Niels Möller committed
46 47 48 49 50 51
  mp_limb_t a[MAX_SIZE];
  mp_limb_t m[MAX_SIZE];
  mp_limb_t ref[MAX_SIZE];
  unsigned i;
  mpz_t r;

52
  gmp_randinit_default (rands);
Niels Möller's avatar
Niels Möller committed
53 54 55 56 57 58 59 60 61
  
  mpz_init (r);
  
  for (i = 0; ecc_curves[i]; i++)
    {
      const struct ecc_curve *ecc = ecc_curves[i];
      unsigned j;
      if (!ecc->redc)
	continue;
Niels Möller's avatar
Niels Möller committed
62
      ASSERT (ecc->p.redc_size != 0);
Niels Möller's avatar
Niels Möller committed
63 64 65 66

      for (j = 0; j < COUNT; j++)
	{
	  if (j & 1)
Niels Möller's avatar
Niels Möller committed
67
	    mpz_rrandomb (r, rands, 2*ecc->p.size * GMP_NUMB_BITS);
Niels Möller's avatar
Niels Möller committed
68
	  else
Niels Möller's avatar
Niels Möller committed
69
	    mpz_urandomb (r, rands, 2*ecc->p.size * GMP_NUMB_BITS);
Niels Möller's avatar
Niels Möller committed
70

Niels Möller's avatar
Niels Möller committed
71
	  mpz_limbs_copy (a, r, 2*ecc->p.size);
Niels Möller's avatar
Niels Möller committed
72

Niels Möller's avatar
Niels Möller committed
73
	  ref_redc (ref, a, ecc->p.m, ecc->p.size);
Niels Möller's avatar
Niels Möller committed
74

Niels Möller's avatar
Niels Möller committed
75
	  mpn_copyi (m, a, 2*ecc->p.size);
76
	  ecc->redc (&ecc->p, m);
Niels Möller's avatar
Niels Möller committed
77 78
	  if (mpn_cmp (m, ecc->p.m, ecc->p.size) >= 0)
	    mpn_sub_n (m, m, ecc->p.m, ecc->p.size);
Niels Möller's avatar
Niels Möller committed
79

Niels Möller's avatar
Niels Möller committed
80
	  if (mpn_cmp (m, ref, ecc->p.size))
Niels Möller's avatar
Niels Möller committed
81 82
	    {
	      fprintf (stderr, "ecc->redc failed: bit_size = %u\n",
Niels Möller's avatar
Niels Möller committed
83 84 85 86
		       ecc->p.bit_size);
	      gmp_fprintf (stderr, "a   = %Nx\n", a, 2*ecc->p.size);
	      gmp_fprintf (stderr, "m   = %Nx (bad)\n", m, ecc->p.size);
	      gmp_fprintf (stderr, "ref = %Nx\n", ref, ecc->p.size);
Niels Möller's avatar
Niels Möller committed
87 88 89
	      abort ();
	    }

Niels Möller's avatar
Niels Möller committed
90 91
	  mpn_copyi (m, a, 2*ecc->p.size);
	  if (ecc->p.m[0] == 1)
92
	    ecc_pm1_redc (&ecc->p, m);
Niels Möller's avatar
Niels Möller committed
93
	  else
94
	    ecc_pp1_redc (&ecc->p, m);
95

Niels Möller's avatar
Niels Möller committed
96 97
	  if (mpn_cmp (m, ecc->p.m, ecc->p.size) >= 0)
	    mpn_sub_n (m, m, ecc->p.m, ecc->p.size);
Niels Möller's avatar
Niels Möller committed
98

Niels Möller's avatar
Niels Möller committed
99
	  if (mpn_cmp (m, ref, ecc->p.size))
Niels Möller's avatar
Niels Möller committed
100
	    {
101
	      fprintf (stderr, "ecc_p%c1_redc failed: bit_size = %u\n",
Niels Möller's avatar
Niels Möller committed
102 103 104 105
		       (ecc->p.m[0] == 1) ? 'm' : 'p', ecc->p.bit_size);
	      gmp_fprintf (stderr, "a   = %Nx\n", a, 2*ecc->p.size);
	      gmp_fprintf (stderr, "m   = %Nx (bad)\n", m, ecc->p.size);
	      gmp_fprintf (stderr, "ref = %Nx\n", ref, ecc->p.size);
Niels Möller's avatar
Niels Möller committed
106 107 108 109 110 111
	      abort ();
	    }
	}
    }

  mpz_clear (r);
112
  gmp_randclear (rands);
Niels Möller's avatar
Niels Möller committed
113
}
114
#endif /* ! NETTLE_USE_MINI_GMP */