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   gmp_randstate_t rands;
42   unsigned i;
43 
44   gmp_randinit_default (rands);
45 
46   for (i = 0; ecc_curves[i]; i++)
47     {
48       const struct ecc_curve *ecc = ecc_curves[i];
49       mp_size_t size, itch;
50       mpz_t zp, t;
51       mp_limb_t *s;
52       mp_limb_t *p;
53       mp_limb_t *pa1;
54       mp_limb_t *pa2;
55       mp_limb_t *scratch;
56       size_t clen;
57       uint8_t *c;
58       unsigned j;
59 
60       if (!(ecc->p.bit_size == 255 || ecc->p.bit_size == 448))
61 	continue;
62 
63       size = ecc_size (ecc);
64       clen = 1 + ecc->p.bit_size / 8;
65 
66       mpz_roinit_n (zp, ecc->p.m, size);
67 
68       mpz_init (t);
69       s = xalloc_limbs (size);
70       p = xalloc_limbs (ecc_size_j (ecc));
71       pa1 = xalloc_limbs (ecc_size_a (ecc));
72       pa2 = xalloc_limbs (ecc_size_a (ecc));
73       c = xalloc (clen);
74 
75       itch = _eddsa_decompress_itch (ecc);
76       if (itch < ecc->mul_g_itch)
77 	itch = ecc->mul_g_itch;
78       ASSERT (_eddsa_compress_itch (ecc) <= itch);
79 
80       scratch = xalloc_limbs (itch);
81 
82       for (j = 0; j < COUNT; j++)
83 	{
84 	  mpz_t x1, y1, x2, y2;
85 
86 	  mpz_urandomb (t, rands, ecc->q.bit_size);
87 	  mpz_limbs_copy (s, t, ecc->q.size);
88 	  ecc->mul_g (ecc, p, s, scratch);
89 	  _eddsa_compress (ecc, c, p, scratch);
90 	  ecc->h_to_a (ecc, 0, pa1, p, scratch);
91 	  _eddsa_decompress (ecc, pa2, c, scratch);
92 	  mpz_roinit_n (x1, pa1, size);
93 	  mpz_roinit_n (y1, pa1 + size, size);
94 	  mpz_roinit_n (x2, pa2, size);
95 	  mpz_roinit_n (y2, pa2 + size, size);
96 	  if (!(mpz_congruent_p (x1, x2, zp)
97 		&& mpz_congruent_p (y1, y2, zp)))
98 	    {
99 	      fprintf (stderr, "eddsa compression failed:\nc = ");
100 	      print_hex (clen, c);
101 	      fprintf (stderr, "\np1 = 0x");
102 	      mpz_out_str (stderr, 16, x1);
103 	      fprintf (stderr, ",\n     0x");
104 	      mpz_out_str (stderr, 16, y1);
105 	      fprintf (stderr, "\np2 = 0x");
106 	      mpz_out_str (stderr, 16, x2);
107 	      fprintf (stderr, ",\n     0x");
108 	      mpz_out_str (stderr, 16, y2);
109 	      fprintf (stderr, "\n");
110 	      FAIL();
111 	    }
112 	}
113       mpz_clear (t);
114       free (s);
115       free (p);
116       free (c);
117       free (pa1);
118       free (pa2);
119       free (scratch);
120     }
121   gmp_randclear (rands);
122 }
123