aboutsummaryrefslogtreecommitdiff
path: root/src/util/perf_crypto_ecc_dlog.c
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2015-09-28 13:22:21 +0000
committerChristian Grothoff <christian@grothoff.org>2015-09-28 13:22:21 +0000
commit2b70eeaf2bce2c777198d91c5a8357c2426540a0 (patch)
treec7bb436fde68f5dbe86fc05d00cb8d9a2b0084ed /src/util/perf_crypto_ecc_dlog.c
parentcab1b047ddcac497e14515fc11f097c4aac8ee27 (diff)
downloadgnunet-2b70eeaf2bce2c777198d91c5a8357c2426540a0.tar.gz
gnunet-2b70eeaf2bce2c777198d91c5a8357c2426540a0.zip
add ecc dlog benchmark
Diffstat (limited to 'src/util/perf_crypto_ecc_dlog.c')
-rw-r--r--src/util/perf_crypto_ecc_dlog.c173
1 files changed, 173 insertions, 0 deletions
diff --git a/src/util/perf_crypto_ecc_dlog.c b/src/util/perf_crypto_ecc_dlog.c
new file mode 100644
index 000000000..ad83e1fbc
--- /dev/null
+++ b/src/util/perf_crypto_ecc_dlog.c
@@ -0,0 +1,173 @@
1/*
2 This file is part of GNUnet.
3 Copyright (C) 2015 Christian Grothoff (and other contributing authors)
4
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 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 General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
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 */
67static void
68test_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
124int
125main (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 test_crypto_ecc_dlog.c */