Merge branch 'master' of gnunet.org:gnunet
[oweals/gnunet.git] / src / util / test_crypto_ecc_dlog.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2015 GNUnet e.V.
4
5      GNUnet is free software: you can redistribute it and/or modify it
6      under the terms of the GNU Affero General Public License as published
7      by the Free Software Foundation, either version 3 of the License,
8      or (at your option) any later version.
9
10      GNUnet is distributed in the hope that it will be useful, but
11      WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13      Affero General Public License for more details.
14     
15      You should have received a copy of the GNU Affero General Public License
16      along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18 */
19 /**
20  * @file util/test_crypto_ecc_dlog.c
21  * @brief testcase for ECC DLOG calculation
22  * @author Christian Grothoff
23  */
24 #include "platform.h"
25 #include "gnunet_util_lib.h"
26 #include <gcrypt.h>
27
28
29 /**
30  * Name of the curve we are using.  Note that we have hard-coded
31  * structs that use 256 bits, so using a bigger curve will require
32  * changes that break stuff badly.  The name of the curve given here
33  * must be agreed by all peers and be supported by libgcrypt.
34  */
35 #define CURVE "Ed25519"
36
37 /**
38  * Maximum value we test dlog for.
39  */
40 #define MAX_FACT 100
41
42 /**
43  * Maximum memory to use, sqrt(MAX_FACT) is a good choice.
44  */
45 #define MAX_MEM 10
46
47 /**
48  * How many values do we test?
49  */  
50 #define TEST_ITER 10
51
52 /**
53  * Range of values to use for MATH tests.
54  */  
55 #define MATH_MAX 5
56
57
58 /**
59  * Do some DLOG operations for testing.
60  *
61  * @param edc context for ECC operations
62  */
63 static void
64 test_dlog (struct GNUNET_CRYPTO_EccDlogContext *edc)
65 {
66   gcry_mpi_t fact;
67   gcry_mpi_t n;
68   gcry_ctx_t ctx;
69   gcry_mpi_point_t q;
70   gcry_mpi_point_t g;
71   unsigned int i;
72   int x;
73   int iret;
74
75   GNUNET_assert (0 == gcry_mpi_ec_new (&ctx, NULL, CURVE));
76   g = gcry_mpi_ec_get_point ("g", ctx, 0);
77   GNUNET_assert (NULL != g);
78   n = gcry_mpi_ec_get_mpi ("n", ctx, 0);
79   q = gcry_mpi_point_new (0);
80   fact = gcry_mpi_new (0);
81   for (i=0;i<TEST_ITER;i++)
82   {
83     fprintf (stderr, ".");
84     x = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
85                                   MAX_FACT);
86     if (0 == GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
87                                        2))
88     {
89       gcry_mpi_set_ui (fact, x);
90       gcry_mpi_sub (fact, n, fact);
91       x = - x;
92     }
93     else 
94     {
95       gcry_mpi_set_ui (fact, x);
96     }
97     gcry_mpi_ec_mul (q, fact, g, ctx);
98     if  (x !=
99          (iret = GNUNET_CRYPTO_ecc_dlog (edc,
100                                          q)))
101     {
102       fprintf (stderr, 
103                "DLOG failed for value %d (%d)\n", 
104                x,
105                iret);
106       GNUNET_assert (0);
107     }
108   }
109   gcry_mpi_release (fact);
110   gcry_mpi_release (n);
111   gcry_mpi_point_release (g);
112   gcry_mpi_point_release (q);
113   gcry_ctx_release (ctx);
114   fprintf (stderr, "\n");
115 }
116
117
118 /**
119  * Do some arithmetic operations for testing.
120  *
121  * @param edc context for ECC operations
122  */
123 static void
124 test_math (struct GNUNET_CRYPTO_EccDlogContext *edc)
125 {
126   int i;
127   int j;
128   gcry_mpi_point_t ip;
129   gcry_mpi_point_t jp;
130   gcry_mpi_point_t r;
131   gcry_mpi_point_t ir;
132   gcry_mpi_point_t irj;
133   gcry_mpi_point_t r_inv;
134   gcry_mpi_point_t sum;
135
136   for (i=-MATH_MAX;i<MATH_MAX;i++)
137   {
138     ip = GNUNET_CRYPTO_ecc_dexp (edc, i);
139     for (j=-MATH_MAX;j<MATH_MAX;j++)
140     {
141       fprintf (stderr, ".");
142       jp = GNUNET_CRYPTO_ecc_dexp (edc, j);
143       GNUNET_CRYPTO_ecc_rnd (edc,
144                              &r,
145                              &r_inv);
146       ir = GNUNET_CRYPTO_ecc_add (edc, ip, r);
147       irj = GNUNET_CRYPTO_ecc_add (edc, ir, jp);
148       sum = GNUNET_CRYPTO_ecc_add (edc, irj, r_inv);
149       GNUNET_assert (i + j ==
150                      GNUNET_CRYPTO_ecc_dlog (edc,
151                                              sum));
152       GNUNET_CRYPTO_ecc_free (jp);
153       GNUNET_CRYPTO_ecc_free (ir);
154       GNUNET_CRYPTO_ecc_free (irj);
155       GNUNET_CRYPTO_ecc_free (r);
156       GNUNET_CRYPTO_ecc_free (r_inv);
157       GNUNET_CRYPTO_ecc_free (sum);
158     }
159     GNUNET_CRYPTO_ecc_free (ip);
160   }
161   fprintf (stderr, "\n");
162 }
163
164
165
166 int
167 main (int argc, char *argv[])
168 {
169   struct GNUNET_CRYPTO_EccDlogContext *edc;
170
171   if (! gcry_check_version ("1.6.0"))
172   {
173     FPRINTF (stderr,
174              _
175              ("libgcrypt has not the expected version (version %s is required).\n"),
176              "1.6.0");
177     return 0;
178   }
179   if (getenv ("GNUNET_GCRYPT_DEBUG"))
180     gcry_control (GCRYCTL_SET_DEBUG_FLAGS, 1u , 0);
181   GNUNET_log_setup ("test-crypto-ecc-dlog", 
182                     "WARNING", 
183                     NULL);
184   edc = GNUNET_CRYPTO_ecc_dlog_prepare (MAX_FACT,
185                                         MAX_MEM);
186   test_dlog (edc);
187   test_math (edc);
188   GNUNET_CRYPTO_ecc_dlog_release (edc);
189   return 0;
190 }
191
192 /* end of test_crypto_ecc_dlog.c */