fix for starting problems of SUID binaries
[oweals/gnunet.git] / src / util / perf_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      SPDX-License-Identifier: AGPL3.0-or-later
19
20 */
21 /**
22  * @file util/perf_crypto_ecc_dlog.c
23  * @brief benchmark for ECC DLOG calculation
24  * @author Christian Grothoff
25  */
26 #include "platform.h"
27 #include "gnunet_util_lib.h"
28 #include <gcrypt.h>
29 #include <gauger.h>
30
31
32 /**
33  * Name of the curve we are using.  Note that we have hard-coded
34  * structs that use 256 bits, so using a bigger curve will require
35  * changes that break stuff badly.  The name of the curve given here
36  * must be agreed by all peers and be supported by libgcrypt.
37  */
38 #define CURVE "Ed25519"
39
40 /**
41  * Maximum value we benchmark dlog for.
42  */
43 #define MAX_FACT (1024 * 1024)
44
45 /**
46  * Maximum memory to use, sqrt(MAX_FACT) is a good choice.
47  */
48 #define MAX_MEM 1024
49
50 /**
51  * How many values do we test?
52  */  
53 #define TEST_ITER 10
54
55 /**
56  * Range of values to use for MATH tests.
57  */  
58 #define MATH_MAX 500000
59
60
61 /**
62  * Do some DLOG operations for testing.
63  *
64  * @param edc context for ECC operations
65  * @param do_dlog #GNUNET_YES if we want to actually do the bencharked operation
66  */
67 static void
68 test_dlog (struct GNUNET_CRYPTO_EccDlogContext *edc, 
69            int do_dlog)
70 {
71   gcry_mpi_t fact;
72   gcry_mpi_t n;
73   gcry_ctx_t ctx;
74   gcry_mpi_point_t q;
75   gcry_mpi_point_t g;
76   unsigned int i;
77   int x;
78   int iret;
79
80   GNUNET_assert (0 == gcry_mpi_ec_new (&ctx, NULL, CURVE));
81   g = gcry_mpi_ec_get_point ("g", ctx, 0);
82   GNUNET_assert (NULL != g);
83   n = gcry_mpi_ec_get_mpi ("n", ctx, 0);
84   q = gcry_mpi_point_new (0);
85   fact = gcry_mpi_new (0);
86   for (i=0;i<TEST_ITER;i++)
87   {
88     fprintf (stderr, ".");
89     x = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
90                                   MAX_FACT);
91     if (0 == GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
92                                        2))
93     {
94       gcry_mpi_set_ui (fact, x);
95       gcry_mpi_sub (fact, n, fact);
96       x = - x;
97     }
98     else 
99     {
100       gcry_mpi_set_ui (fact, x);
101     }
102     gcry_mpi_ec_mul (q, fact, g, ctx);
103     if ( (GNUNET_YES == do_dlog) &&
104          (x !=
105           (iret = GNUNET_CRYPTO_ecc_dlog (edc,
106                                           q))) )
107     {
108       fprintf (stderr, 
109                "DLOG failed for value %d (%d)\n", 
110                x,
111                iret);
112       GNUNET_assert (0);
113     }
114   }
115   gcry_mpi_release (fact);
116   gcry_mpi_release (n);
117   gcry_mpi_point_release (g);
118   gcry_mpi_point_release (q);
119   gcry_ctx_release (ctx);
120   fprintf (stderr, "\n");
121 }
122
123
124 int
125 main (int argc, char *argv[])
126 {
127   struct GNUNET_CRYPTO_EccDlogContext *edc;
128   struct GNUNET_TIME_Absolute start;
129   struct GNUNET_TIME_Relative delta;
130
131   if (! gcry_check_version ("1.6.0"))
132   {
133     FPRINTF (stderr,
134              _
135              ("libgcrypt has not the expected version (version %s is required).\n"),
136              "1.6.0");
137     return 0;
138   }
139   if (getenv ("GNUNET_GCRYPT_DEBUG"))
140     gcry_control (GCRYCTL_SET_DEBUG_FLAGS, 1u , 0);
141   GNUNET_log_setup ("perf-crypto-ecc-dlog", 
142                     "WARNING", 
143                     NULL);
144   start = GNUNET_TIME_absolute_get ();
145   edc = GNUNET_CRYPTO_ecc_dlog_prepare (MAX_FACT,
146                                         MAX_MEM);
147   printf ("DLOG precomputation 1M/1K took %s\n",
148           GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration (start),
149                                                   GNUNET_YES));
150   GAUGER ("UTIL", "ECC DLOG initialization",
151           GNUNET_TIME_absolute_get_duration
152           (start).rel_value_us / 1000LL, "ms/op");
153   start = GNUNET_TIME_absolute_get ();
154   /* first do a baseline run without the DLOG */
155   test_dlog (edc, GNUNET_NO);
156   delta = GNUNET_TIME_absolute_get_duration (start);
157   start = GNUNET_TIME_absolute_get ();
158   test_dlog (edc, GNUNET_YES);
159   delta = GNUNET_TIME_relative_subtract (GNUNET_TIME_absolute_get_duration (start),
160                                          delta);
161   printf ("%u DLOG calculations took %s\n",
162           TEST_ITER,
163           GNUNET_STRINGS_relative_time_to_string (delta,
164                                                   GNUNET_YES));
165   GAUGER ("UTIL", "ECC DLOG operations",
166           delta.rel_value_us / 1000LL / TEST_ITER, 
167           "ms/op");
168
169   GNUNET_CRYPTO_ecc_dlog_release (edc);
170   return 0;
171 }
172
173 /* end of perf_crypto_ecc_dlog.c */