File: eddsa-compress-test.c

package info (click to toggle)
nettle 3.10.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 10,508 kB
  • sloc: ansic: 72,410; asm: 21,179; sh: 4,328; makefile: 837; cpp: 71; awk: 7
file content (123 lines) | stat: -rw-r--r-- 3,278 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/* eddsa-compress-test.c

   Copyright (C) 2014 Niels Möller

   This file is part of GNU Nettle.

   GNU Nettle is free software: you can redistribute it and/or
   modify it under the terms of either:

     * the GNU Lesser General Public License as published by the Free
       Software Foundation; either version 3 of the License, or (at your
       option) any later version.

   or

     * the GNU General Public License as published by the Free
       Software Foundation; either version 2 of the License, or (at your
       option) any later version.

   or both in parallel, as here.

   GNU Nettle is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   You should have received copies of the GNU General Public License and
   the GNU Lesser General Public License along with this program.  If
   not, see http://www.gnu.org/licenses/.
*/

#include "testutils.h"

#include "eddsa.h"
#include "eddsa-internal.h"

#define COUNT 500

void test_main (void)
{
  gmp_randstate_t rands;
  unsigned i;

  gmp_randinit_default (rands);
  test_randomize(rands);

  for (i = 0; ecc_curves[i]; i++)
    {
      const struct ecc_curve *ecc = ecc_curves[i];
      mp_size_t size, itch;
      mpz_t zp, t;
      mp_limb_t *s;
      mp_limb_t *p;
      mp_limb_t *pa1;
      mp_limb_t *pa2;
      mp_limb_t *scratch;
      size_t clen;
      uint8_t *c;
      unsigned j;

      if (!(ecc->p.bit_size == 255 || ecc->p.bit_size == 448))
	continue;
  
      size = ecc_size (ecc);
      clen = 1 + ecc->p.bit_size / 8;

      mpz_roinit_n (zp, ecc->p.m, size);

      mpz_init (t);
      s = xalloc_limbs (size);
      p = xalloc_limbs (ecc_size_j (ecc));
      pa1 = xalloc_limbs (ecc_size_a (ecc));
      pa2 = xalloc_limbs (ecc_size_a (ecc));
      c = xalloc (clen);

      itch = _eddsa_decompress_itch (ecc);
      if (itch < ecc->mul_g_itch)
	itch = ecc->mul_g_itch;
      ASSERT (_eddsa_compress_itch (ecc) <= itch);

      scratch = xalloc_limbs (itch);

      for (j = 0; j < COUNT; j++)
	{
	  mpz_t x1, y1, x2, y2;

	  mpz_urandomb (t, rands, ecc->q.bit_size);
	  mpz_limbs_copy (s, t, ecc->q.size);
	  ecc->mul_g (ecc, p, s, scratch);
	  _eddsa_compress (ecc, c, p, scratch);
	  ecc->h_to_a (ecc, 0, pa1, p, scratch);
	  _eddsa_decompress (ecc, pa2, c, scratch);
	  mpz_roinit_n (x1, pa1, size);
	  mpz_roinit_n (y1, pa1 + size, size);
	  mpz_roinit_n (x2, pa2, size);
	  mpz_roinit_n (y2, pa2 + size, size);
	  if (!(mpz_congruent_p (x1, x2, zp)
		&& mpz_congruent_p (y1, y2, zp)))
	    {
	      fprintf (stderr, "eddsa compression failed:\nc = ");
	      print_hex (clen, c);
	      fprintf (stderr, "\np1 = 0x");
	      mpz_out_str (stderr, 16, x1);
	      fprintf (stderr, ",\n     0x");
	      mpz_out_str (stderr, 16, y1);
	      fprintf (stderr, "\np2 = 0x");
	      mpz_out_str (stderr, 16, x2);
	      fprintf (stderr, ",\n     0x");
	      mpz_out_str (stderr, 16, y2);
	      fprintf (stderr, "\n");
	      FAIL();
	    }
	}
      mpz_clear (t);
      free (s);
      free (p);
      free (c);
      free (pa1);
      free (pa2);
      free (scratch);
    }
  gmp_randclear (rands);
}