summaryrefslogtreecommitdiff
path: root/testsuite/ecc-mod-test.c
blob: 3001d8e2be91bd90776b726f96afc493b9ea7d55 (plain)
1
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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
#include "testutils.h"

static void
ref_mod (mp_limb_t *rp, const mp_limb_t *ap, const mp_limb_t *mp, mp_size_t mn)
{
  mpz_t r, a, m;
  mpz_init (r);
  mpz_mod (r, mpz_roinit_n (a, ap, 2*mn), mpz_roinit_n (m, mp, mn));
  mpz_limbs_copy (rp, r, mn);

  mpz_clear (r);
}

#define MAX_ECC_SIZE (1 + 521 / GMP_NUMB_BITS)
#define MAX_SIZE (2*MAX_ECC_SIZE)
#define COUNT 50000

static void
test_modulo (gmp_randstate_t rands, const char *name,
	     const struct ecc_modulo *m)
{
  mp_limb_t a[MAX_SIZE];
  mp_limb_t t[MAX_SIZE];
  mp_limb_t ref[MAX_SIZE];
  mpz_t r;
  unsigned j;

  mpz_init (r);
  
  for (j = 0; j < COUNT; j++)
    {
      if (j & 1)
	mpz_rrandomb (r, rands, 2*m->size * GMP_NUMB_BITS);
      else
	mpz_urandomb (r, rands, 2*m->size * GMP_NUMB_BITS);

      mpz_limbs_copy (a, r, 2*m->size);

      ref_mod (ref, a, m->m, m->size);

      mpn_copyi (t, a, 2*m->size);
      m->mod (m, t);
      if (mpn_cmp (t, m->m, m->size) >= 0)
	mpn_sub_n (t, t, m->m, m->size);

      if (mpn_cmp (t, ref, m->size))
	{
	  fprintf (stderr, "m->mod %s failed: bit_size = %u\n",
		   name, m->bit_size);

	  fprintf (stderr, "a   = ");
	  mpn_out_str (stderr, 16, a, 2*m->size);
	  fprintf (stderr, "\nt   = ");
	  mpn_out_str (stderr, 16, t, m->size);
	  fprintf (stderr, " (bad)\nref = ");
	  mpn_out_str (stderr, 16, ref, m->size);
	  fprintf (stderr, "\n");
	  abort ();
	}

      if (m->B_size < m->size)
	{
	  mpn_copyi (t, a, 2*m->size);
	  ecc_mod (m, t);
	  if (mpn_cmp (t, m->m, m->size) >= 0)
	    mpn_sub_n (t, t, m->m, m->size);

	  if (mpn_cmp (t, ref, m->size))
	    {
	      fprintf (stderr, "ecc_mod %s failed: bit_size = %u\n",
		       name, m->bit_size);
	      fprintf (stderr, "a   = ");
	      mpn_out_str (stderr, 16, a, 2*m->size);
	      fprintf (stderr, "\nt   = ");
	      mpn_out_str (stderr, 16, t, m->size);
	      fprintf (stderr, " (bad)\nref = ");
	      mpn_out_str (stderr, 16, ref, m->size);
	      fprintf (stderr, "\n");
	      abort ();
	    }
	}
    }
  mpz_clear (r);
}

void
test_main (void)
{
  gmp_randstate_t rands;
  unsigned i;

  gmp_randinit_default (rands);
  
  for (i = 0; ecc_curves[i]; i++)
    {
      test_modulo (rands, "p", &ecc_curves[i]->p);
      test_modulo (rands, "q", &ecc_curves[i]->q);
    }
  gmp_randclear (rands);
}