Revert "Merge branch 'upstream' into tizen"
[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 state;
7   mpz_t r;
8   unsigned i;
9
10   gmp_randinit_default (state);
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 (ecc));
21
22       mpn_zero (n, size);
23
24       n[0] = 1;
25       ecc_mul_g (ecc, p, n, scratch);
26       ecc_j_to_a (ecc, 1, 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_j (i, n[0], p);
38         }
39
40       /* (order - 1) * g = - g */
41       mpn_sub_1 (n, ecc->q, size, 1);
42       ecc_mul_g (ecc, p, n, scratch);
43       ecc_j_to_a (ecc, 1, p, p, scratch);
44       mpn_sub_n (p + size, ecc->p, p + size, size);
45       if (mpn_cmp (p, ecc->g, 2*size) != 0)
46         {
47           fprintf (stderr, "ecc_mul_g with n = order - 1 failed.\n");
48           abort ();
49         }
50
51       free (n);
52       free (p);
53       free (q);
54       free (scratch);
55     }
56   mpz_clear (r);
57   gmp_randclear (state);
58 }