1 /* eddsa-compress-test.c
2 
3    Copyright (C) 2014 Niels Möller
4 
5    This file is part of GNU Nettle.
6 
7    GNU Nettle is free software: you can redistribute it and/or
8    modify it under the terms of either:
9 
10      * the GNU Lesser General Public License as published by the Free
11        Software Foundation; either version 3 of the License, or (at your
12        option) any later version.
13 
14    or
15 
16      * the GNU General Public License as published by the Free
17        Software Foundation; either version 2 of the License, or (at your
18        option) any later version.
19 
20    or both in parallel, as here.
21 
22    GNU Nettle is distributed in the hope that it will be useful,
23    but WITHOUT ANY WARRANTY; without even the implied warranty of
24    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
25    General Public License for more details.
26 
27    You should have received copies of the GNU General Public License and
28    the GNU Lesser General Public License along with this program.  If
29    not, see http://www.gnu.org/licenses/.
30 */
31 
32 #include "testutils.h"
33 
34 #include "eddsa.h"
35 #include "eddsa-internal.h"
36 
37 #define COUNT 1000
38 
test_main(void)39 void test_main (void)
40 {
41   const struct ecc_curve *ecc = &_nettle_curve25519;
42   gmp_randstate_t rands;
43   mp_size_t size, itch;
44   mpz_t zp, t;
45   mp_limb_t *s;
46   mp_limb_t *p;
47   mp_limb_t *pa1;
48   mp_limb_t *pa2;
49   mp_limb_t *scratch;
50   size_t clen;
51   uint8_t *c;
52   unsigned j;
53 
54   gmp_randinit_default (rands);
55 
56   size = ecc_size (ecc);
57   clen = 1 + ecc->p.bit_size / 8;
58 
59   mpz_roinit_n (zp, ecc->p.m, size);
60 
61   mpz_init (t);
62   s = xalloc_limbs (size);
63   p = xalloc_limbs (ecc_size_j (ecc));
64   pa1 = xalloc_limbs (ecc_size_a (ecc));
65   pa2 = xalloc_limbs (ecc_size_a (ecc));
66   c = xalloc (clen);
67 
68   itch = _eddsa_decompress_itch (ecc);
69   if (itch < ecc->mul_g_itch)
70     itch = ecc->mul_g_itch;
71 
72   scratch = xalloc_limbs (itch);
73 
74   for (j = 0; j < COUNT; j++)
75     {
76       mpz_t x1, y1, x2, y2;
77 
78       mpz_urandomb (t, rands, ecc->q.bit_size);
79       mpz_limbs_copy (s, t, ecc->q.size);
80       ecc->mul_g (ecc, p, s, scratch);
81       _eddsa_compress (ecc, c, p, scratch);
82       ecc->h_to_a (ecc, 0, pa1, p, scratch);
83       _eddsa_decompress (ecc, pa2, c, scratch);
84       mpz_roinit_n (x1, pa1, size);
85       mpz_roinit_n (y1, pa1 + size, size);
86       mpz_roinit_n (x2, pa2, size);
87       mpz_roinit_n (y2, pa2 + size, size);
88       if (!(mpz_congruent_p (x1, x2, zp)
89 	    && mpz_congruent_p (y1, y2, zp)))
90 	{
91 	  fprintf (stderr, "eddsa compression failed:\nc = ");
92 	  print_hex (clen, c);
93 	  fprintf (stderr, "\np1 = 0x");
94 	  mpz_out_str (stderr, 16, x1);
95 	  fprintf (stderr, ",\n     0x");
96 	  mpz_out_str (stderr, 16, y1);
97 	  fprintf (stderr, "\np2 = 0x");
98 	  mpz_out_str (stderr, 16, x2);
99 	  fprintf (stderr, ",\n     0x");
100 	  mpz_out_str (stderr, 16, y2);
101 	  fprintf (stderr, "\n");
102 	  abort ();
103 	}
104     }
105   mpz_clear (t);
106   free (s);
107   free (p);
108   free (c);
109   free (pa1);
110   free (pa2);
111   free (scratch);
112   gmp_randclear (rands);
113 }
114