b206b8484e0a34c9ee5672d415c7c957ea161c35
[platform/upstream/nettle.git] / testsuite / ecc-mul-a-test.c
1 #include "testutils.h"
2
3 void
4 test_main (void)
5 {
6   gmp_randstate_t rands;
7   mpz_t r;
8   unsigned i;
9
10   gmp_randinit_default (rands);
11   mpz_init (r);
12   
13   for (i = 0; ecc_curves[i]; i++)
14     {
15       const struct ecc_curve *ecc = ecc_curves[i];
16       mp_size_t size = ecc_size (ecc);
17       mp_limb_t *p = xalloc_limbs (ecc_size_j (ecc));
18       mp_limb_t *q = xalloc_limbs (ecc_size_j (ecc));
19       mp_limb_t *n = xalloc_limbs (size);
20       mp_limb_t *scratch = xalloc_limbs (ecc->mul_itch);
21       unsigned j;
22       
23       mpn_zero (n, size);
24
25       n[0] = 1;
26       ecc->mul (ecc, p, n, ecc->g, scratch);
27       ecc->h_to_a (ecc, 0, p, p, scratch);
28
29       if (mpn_cmp (p, ecc->g, 2*size != 0))
30         die ("curve %d: ecc->mul with n = 1 failed.\n", ecc->p.bit_size);
31
32       for (n[0] = 2; n[0] <= 4; n[0]++)
33         {
34           ecc->mul (ecc, p, n, ecc->g, scratch);
35           test_ecc_mul_h (i, n[0], p);
36         }
37
38       /* (order - 1) * g = - g */
39       mpn_sub_1 (n, ecc->q.m, size, 1);
40       ecc->mul (ecc, p, n, ecc->g, scratch);
41       ecc->h_to_a (ecc, 0, p, p, scratch);
42       if (ecc->p.bit_size == 255)
43         /* For edwards curves, - (x,y ) == (-x, y). FIXME: Swap x and
44            y, to get identical negation? */
45         mpn_sub_n (p, ecc->p.m, p, size);
46       else
47         mpn_sub_n (p + size, ecc->p.m, p + size, size);
48       if (mpn_cmp (p, ecc->g, 2*size) != 0)
49         {
50           fprintf (stderr, "ecc->mul with n = order - 1 failed.\n");
51           abort ();
52         }
53
54       mpn_zero (n, size);
55
56       for (j = 0; j < 100; j++)
57         {
58           if (j & 1)
59             mpz_rrandomb (r, rands, size * GMP_NUMB_BITS);
60           else
61             mpz_urandomb (r, rands, size * GMP_NUMB_BITS);
62
63           /* Reduce so that (almost surely) n < q */
64           mpz_limbs_copy (n, r, size);
65           n[size - 1] %= ecc->q.m[size - 1];
66
67           ecc->mul (ecc, p, n, ecc->g, scratch);
68           ecc->h_to_a (ecc, 0, p, p, scratch);
69
70           ecc->mul_g (ecc, q, n, scratch);
71           ecc->h_to_a (ecc, 0, q, q, scratch);
72
73           if (mpn_cmp (p, q, 2*size))
74             {
75               fprintf (stderr,
76                        "Different results from ecc->mul and ecc->mul_g.\n"
77                        " bits = %u\n",
78                        ecc->p.bit_size);
79               fprintf (stderr, " n = ");
80               mpn_out_str (stderr, 16, n, size);
81               
82               fprintf (stderr, "\np = ");
83               mpn_out_str (stderr, 16, p, size);
84               fprintf (stderr, ",\n    ");
85               mpn_out_str (stderr, 16, p + size, size);
86
87               fprintf (stderr, "\nq = ");
88               mpn_out_str (stderr, 16, q, size);
89               fprintf (stderr, ",\n    ");
90               mpn_out_str (stderr, 16, q + size, size);
91               fprintf (stderr, "\n");
92               abort ();
93             }
94         }
95       free (n);
96       free (p);
97       free (q);
98       free (scratch);
99     }
100   mpz_clear (r); 
101   gmp_randclear (rands);
102 }