Revert "Merge branch 'upstream' into tizen"
[platform/upstream/nettle.git] / testsuite / ecc-mod-test.c
1 #include "testutils.h"
2
3 static void
4 ref_mod (mp_limb_t *rp, const mp_limb_t *ap, const mp_limb_t *mp, mp_size_t mn)
5 {
6   mp_limb_t q[mn + 1];
7   mpn_tdiv_qr (q, rp, 0, ap, 2*mn, mp, mn);
8 }
9
10 #define MAX_ECC_SIZE (1 + 521 / GMP_NUMB_BITS)
11 #define MAX_SIZE (2*MAX_ECC_SIZE)
12 #define COUNT 50000
13
14 void
15 test_main (void)
16 {
17   gmp_randstate_t state;
18   mp_limb_t a[MAX_SIZE];
19   mp_limb_t m[MAX_SIZE];
20   mp_limb_t ref[MAX_SIZE];
21   unsigned i;
22   mpz_t r;
23
24   gmp_randinit_default (state);
25   
26   mpz_init (r);
27   
28   for (i = 0; ecc_curves[i]; i++)
29     {
30       const struct ecc_curve *ecc = ecc_curves[i];
31       unsigned j;
32       for (j = 0; j < COUNT; j++)
33         {
34           if (j & 1)
35             mpz_rrandomb (r, state, 2*ecc->size * GMP_NUMB_BITS);
36           else
37             mpz_urandomb (r, state, 2*ecc->size * GMP_NUMB_BITS);
38
39           mpz_limbs_copy (a, r, 2*ecc->size);
40
41           ref_mod (ref, a, ecc->p, ecc->size);
42
43           mpn_copyi (m, a, 2*ecc->size);
44           ecc->modp (ecc, m);
45           if (mpn_cmp (m, ecc->p, ecc->size) >= 0)
46             mpn_sub_n (m, m, ecc->p, ecc->size);
47
48           if (mpn_cmp (m, ref, ecc->size))
49             {
50               fprintf (stderr, "ecc->modp failed: bit_size = %u\n",
51                        ecc->bit_size);
52               gmp_fprintf (stderr, "a   = %Nx\n", a, 2*ecc->size);
53               gmp_fprintf (stderr, "m   = %Nx (bad)\n", m, ecc->size);
54               gmp_fprintf (stderr, "ref = %Nx\n", ref, ecc->size);
55               abort ();
56             }
57
58           if (ecc->Bmodp_size < ecc->size)
59             {
60               mpn_copyi (m, a, 2*ecc->size);
61               ecc_generic_modp (ecc, m);
62               if (mpn_cmp (m, ecc->p, ecc->size) >= 0)
63                 mpn_sub_n (m, m, ecc->p, ecc->size);
64
65               if (mpn_cmp (m, ref, ecc->size))
66                 {
67                   fprintf (stderr, "ecc_generic_modp failed: bit_size = %u\n",
68                            ecc->bit_size);
69                   gmp_fprintf (stderr, "a   = %Nx\n", a, 2*ecc->size);
70                   gmp_fprintf (stderr, "m   = %Nx (bad)\n", m, ecc->size);
71                   gmp_fprintf (stderr, "ref = %Nx\n", ref, ecc->size);
72                   abort ();
73                 }
74             }
75
76           ref_mod (ref, a, ecc->q, ecc->size);
77
78           mpn_copyi (m, a, 2*ecc->size);
79           ecc->modq (ecc, m);
80           if (mpn_cmp (m, ecc->q, ecc->size) >= 0)
81             mpn_sub_n (m, m, ecc->q, ecc->size);
82
83           if (mpn_cmp (m, ref, ecc->size))
84             {
85               fprintf (stderr, "ecc->modq failed: bit_size = %u\n",
86                        ecc->bit_size);
87               gmp_fprintf (stderr, "a   = %Nx\n", a, 2*ecc->size);
88               gmp_fprintf (stderr, "m   = %Nx (bad)\n", m, ecc->size);
89               gmp_fprintf (stderr, "ref = %Nx\n", ref, ecc->size);
90               abort ();
91             }
92
93           if (ecc->Bmodp_size < ecc->size)
94             {
95               mpn_copyi (m, a, 2*ecc->size);
96               ecc_generic_modq (ecc, m);
97               if (mpn_cmp (m, ecc->q, ecc->size) >= 0)
98                 mpn_sub_n (m, m, ecc->q, ecc->size);
99
100               if (mpn_cmp (m, ref, ecc->size))
101                 {
102                   fprintf (stderr, "ecc_generic_modp failed: bit_size = %u\n",
103                            ecc->bit_size);
104                   gmp_fprintf (stderr, "a   = %Nx\n", a, 2*ecc->size);
105                   gmp_fprintf (stderr, "m   = %Nx (bad)\n", m, ecc->size);
106                   gmp_fprintf (stderr, "ref = %Nx\n", ref, ecc->size);
107                   abort ();
108                 }
109             }
110         }
111     }
112
113   mpz_clear (r);
114   gmp_randclear (state);
115 }