summaryrefslogtreecommitdiff
path: root/testsuite/ecc-mod-arith-test.c
blob: 14b3bd1c4abdcf158f148c13d3eb859c16fd70cb (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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#include "testutils.h"

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

static void
test_add(const char *name,
	 const struct ecc_modulo *m,
	 const mpz_t az, const mpz_t bz)
{
  mp_limb_t a[MAX_SIZE];
  mp_limb_t b[MAX_SIZE];
  mp_limb_t t[MAX_SIZE];
  mpz_t mz;
  mpz_t tz;
  mpz_t ref;

  mpz_init (ref);
  mpz_add (ref, az, bz);
  mpz_mod (ref, ref, mpz_roinit_n (mz, m->m, m->size));

  mpz_limbs_copy (a, az, m->size);
  mpz_limbs_copy (b, bz, m->size);
  ecc_mod_add (m, t, a, b);

  if (!mpz_congruent_p (ref, mpz_roinit_n (tz, t, m->size), mz))
    {
      fprintf (stderr, "ecc_mod_add %s failed: bit_size = %u\n",
	       name, m->bit_size);

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

static void
test_sub(const char *name,
	 const struct ecc_modulo *m,
	 /* If range is non-null, check that 0 <= r < range. */
	 const mp_limb_t *range,
	 const mpz_t az, const mpz_t bz)
{
  mp_limb_t a[MAX_SIZE];
  mp_limb_t b[MAX_SIZE];
  mp_limb_t t[MAX_SIZE];
  mpz_t mz;
  mpz_t tz;
  mpz_t ref;

  mpz_init (ref);
  mpz_sub (ref, az, bz);
  mpz_mod (ref, ref, mpz_roinit_n (mz, m->m, m->size));

  mpz_limbs_copy (a, az, m->size);
  mpz_limbs_copy (b, bz, m->size);
  ecc_mod_sub (m, t, a, b);

  if (!mpz_congruent_p (ref, mpz_roinit_n (tz, t, m->size), mz))
    {
      fprintf (stderr, "ecc_mod_sub %s failed: bit_size = %u\n",
	       name, m->bit_size);

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

  if (range && mpn_cmp (t, range, m->size) >= 0)
    {
      fprintf (stderr, "ecc_mod_sub %s out of range: bit_size = %u\n",
	       name, m->bit_size);

      fprintf (stderr, "a   = ");
      mpn_out_str (stderr, 16, a, m->size);
      fprintf (stderr, "\nb   = ");
      mpn_out_str (stderr, 16, b, m->size);
      fprintf (stderr, "\nt   = ");
      mpn_out_str (stderr, 16, t, m->size);
      fprintf (stderr, " \nrange = ");
      mpn_out_str (stderr, 16, range, m->size);
      fprintf (stderr, "\n");
      abort ();
    }
  mpz_clear (ref);
}

static void
test_modulo (gmp_randstate_t rands, const char *name,
	     const struct ecc_modulo *m, unsigned count)
{
  mpz_t a, b;
  unsigned j;

  mpz_init (a);
  mpz_init (b);

  for (j = 0; j < count; j++)
    {
      if (j & 1)
	{
	  mpz_rrandomb (a, rands, m->size * GMP_NUMB_BITS);
	  mpz_rrandomb (b, rands, m->size * GMP_NUMB_BITS);
	}
      else
	{
	  mpz_urandomb (a, rands, m->size * GMP_NUMB_BITS);
	  mpz_urandomb (b, rands, m->size * GMP_NUMB_BITS);
	}
      test_add (name, m, a, b);
      test_sub (name, m, NULL, a, b);
    }
  if (m->bit_size < m->size * GMP_NUMB_BITS)
    {
      mp_limb_t two_p[MAX_SIZE];
      mpn_lshift (two_p, m->m, m->size, 1);
      mpz_t range;
      mpz_roinit_n (range, two_p, m->size);
      mpz_urandomm (a, rands, range);
      mpz_urandomm (b, rands, range);
      test_sub (name, m, two_p, a, b);
    }
  mpz_clear (a);
  mpz_clear (b);
}

void
test_main (void)
{
  gmp_randstate_t rands;
  unsigned count = COUNT;
  unsigned i;

  gmp_randinit_default (rands);

  if (test_randomize(rands))
    count *= 20;

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