2 This file is part of GNUnet.
3 Copyright (C) 2015 GNUnet e.V.
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.
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.
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/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
22 * @file util/perf_crypto_ecc_dlog.c
23 * @brief benchmark for ECC DLOG calculation
24 * @author Christian Grothoff
27 #include "gnunet_util_lib.h"
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.
38 #define CURVE "Ed25519"
41 * Maximum value we benchmark dlog for.
43 #define MAX_FACT (1024 * 1024)
46 * Maximum memory to use, sqrt(MAX_FACT) is a good choice.
51 * How many values do we test?
56 * Range of values to use for MATH tests.
58 #define MATH_MAX 500000
62 * Do some DLOG operations for testing.
64 * @param edc context for ECC operations
65 * @param do_dlog #GNUNET_YES if we want to actually do the bencharked operation
68 test_dlog (struct GNUNET_CRYPTO_EccDlogContext *edc,
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++)
88 fprintf (stderr, ".");
89 x = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
91 if (0 == GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
94 gcry_mpi_set_ui (fact, x);
95 gcry_mpi_sub (fact, n, fact);
100 gcry_mpi_set_ui (fact, x);
102 gcry_mpi_ec_mul (q, fact, g, ctx);
103 if ( (GNUNET_YES == do_dlog) &&
105 (iret = GNUNET_CRYPTO_ecc_dlog (edc,
109 "DLOG failed for value %d (%d)\n",
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");
125 main (int argc, char *argv[])
127 struct GNUNET_CRYPTO_EccDlogContext *edc;
128 struct GNUNET_TIME_Absolute start;
129 struct GNUNET_TIME_Relative delta;
131 if (! gcry_check_version ("1.6.0"))
135 ("libgcrypt has not the expected version (version %s is required).\n"),
139 if (getenv ("GNUNET_GCRYPT_DEBUG"))
140 gcry_control (GCRYCTL_SET_DEBUG_FLAGS, 1u , 0);
141 GNUNET_log_setup ("perf-crypto-ecc-dlog",
144 start = GNUNET_TIME_absolute_get ();
145 edc = GNUNET_CRYPTO_ecc_dlog_prepare (MAX_FACT,
147 printf ("DLOG precomputation 1M/1K took %s\n",
148 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration (start),
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),
161 printf ("%u DLOG calculations took %s\n",
163 GNUNET_STRINGS_relative_time_to_string (delta,
165 GAUGER ("UTIL", "ECC DLOG operations",
166 delta.rel_value_us / 1000LL / TEST_ITER,
169 GNUNET_CRYPTO_ecc_dlog_release (edc);
173 /* end of perf_crypto_ecc_dlog.c */