1c4d0c05af0d66e1207af7012b3b4f7375cd0fdb
[platform/upstream/nettle.git] / testsuite / ecc-mul-g-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_g_itch);
21
22       mpn_zero (n, size);
23
24       n[0] = 1;
25       ecc->mul_g (ecc, p, n, scratch);
26       ecc->h_to_a (ecc, 0, p, p, scratch);
27
28       if (mpn_cmp (p, ecc->g, 2*size != 0))
29         {
30           fprintf (stderr, "ecc->mul_g with n = 1 failed.\n");
31           abort ();
32         }
33
34       for (n[0] = 2; n[0] <= 4; n[0]++)
35         {
36           ecc->mul_g (ecc, p, n, scratch);
37           test_ecc_mul_h (i, n[0], p);
38         }
39
40       /* (order - 1) * g = - g */
41       mpn_sub_1 (n, ecc->q.m, size, 1);
42       ecc->mul_g (ecc, p, n, scratch);
43       ecc->h_to_a (ecc, 0, p, p, scratch);
44       if (ecc->p.bit_size == 255)
45         /* For edwards curves, - (x,y ) == (-x, y). FIXME: Swap x and
46            y, to get identical negation? */
47         mpn_sub_n (p, ecc->p.m, p, size);
48       else
49         mpn_sub_n (p + size, ecc->p.m, p + size, size);
50       if (mpn_cmp (p, ecc->g, 2*size) != 0)
51         {
52           fprintf (stderr, "ecc->mul_g with n = order - 1 failed.\n");
53           abort ();
54         }
55
56       free (n);
57       free (p);
58       free (q);
59       free (scratch);
60     }
61   mpz_clear (r);
62   gmp_randclear (rands);
63 }