aboutsummaryrefslogtreecommitdiff
path: root/src/util/perf_crypto_ecc_dlog.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/util/perf_crypto_ecc_dlog.c')
-rw-r--r--src/util/perf_crypto_ecc_dlog.c118
1 files changed, 62 insertions, 56 deletions
diff --git a/src/util/perf_crypto_ecc_dlog.c b/src/util/perf_crypto_ecc_dlog.c
index 8a0e4676b..f32ffbd67 100644
--- a/src/util/perf_crypto_ecc_dlog.c
+++ b/src/util/perf_crypto_ecc_dlog.c
@@ -52,72 +52,88 @@
52 */ 52 */
53#define TEST_ITER 10 53#define TEST_ITER 10
54 54
55/**
56 * Range of values to use for MATH tests.
57 */
58#define MATH_MAX 500000
59
60 55
61/** 56/**
62 * Do some DLOG operations for testing. 57 * Do some DLOG operations for testing.
63 * 58 *
64 * @param edc context for ECC operations 59 * @param edc context for ECC operations
65 * @param do_dlog #GNUNET_YES if we want to actually do the bencharked operation 60 * @param do_dlog true if we want to actually do the bencharked operation
66 */ 61 */
67static void 62static void
68test_dlog (struct GNUNET_CRYPTO_EccDlogContext *edc, 63test_dlog (struct GNUNET_CRYPTO_EccDlogContext *edc,
69 int do_dlog) 64 bool do_dlog)
70{ 65{
71 gcry_mpi_t fact; 66 for (unsigned int i = 0; i < TEST_ITER; i++)
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 { 67 {
68 struct GNUNET_CRYPTO_EccScalar fact;
69 struct GNUNET_CRYPTO_EccScalar n;
70 struct GNUNET_CRYPTO_EccPoint q;
71 int x;
72
88 fprintf (stderr, "."); 73 fprintf (stderr, ".");
89 x = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 74 x = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
90 MAX_FACT); 75 MAX_FACT);
76 memset (&n,
77 0,
78 sizeof (n));
79 for (unsigned int j = 0; j < x; j++)
80 sodium_increment (n.v,
81 sizeof (n.v));
91 if (0 == GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 82 if (0 == GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK,
92 2)) 83 2))
93 { 84 {
94 gcry_mpi_set_ui (fact, x); 85 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
95 gcry_mpi_sub (fact, n, fact); 86 "Trying negative %d\n",
87 -x);
88 crypto_core_ed25519_scalar_negate (fact.v,
89 n.v);
96 x = -x; 90 x = -x;
97 } 91 }
98 else 92 else
99 { 93 {
100 gcry_mpi_set_ui (fact, x); 94 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
95 "Trying positive %d\n",
96 x);
97 fact = n;
101 } 98 }
102 gcry_mpi_ec_mul (q, fact, g, ctx); 99 if (0 == x)
103 if ((GNUNET_YES == do_dlog) &&
104 (x !=
105 (iret = GNUNET_CRYPTO_ecc_dlog (edc,
106 q))))
107 { 100 {
108 fprintf (stderr, 101 /* libsodium does not like to multiply with zero; make sure
109 "DLOG failed for value %d (%d)\n", 102 'q' is a valid point (g) first, then use q = q - q to get
110 x, 103 the product with zero */
111 iret); 104 sodium_increment (fact.v,
112 GNUNET_assert (0); 105 sizeof (fact.v));
106 GNUNET_assert (0 ==
107 crypto_scalarmult_ed25519_base_noclamp (q.v,
108 fact.v));
109 GNUNET_assert (
110 0 ==
111 crypto_core_ed25519_sub (q.v,
112 q.v,
113 q.v));
114 }
115 else
116 GNUNET_assert (0 ==
117 crypto_scalarmult_ed25519_base_noclamp (q.v,
118 fact.v));
119 if (do_dlog)
120 {
121 int iret;
122
123 if (x !=
124 (iret = GNUNET_CRYPTO_ecc_dlog (edc,
125 &q)))
126 {
127 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
128 "DLOG failed for value %d (got: %d)\n",
129 x,
130 iret);
131 GNUNET_assert (0);
132 }
113 } 133 }
114 } 134 }
115 gcry_mpi_release (fact); 135 fprintf (stderr,
116 gcry_mpi_release (n); 136 "\n");
117 gcry_mpi_point_release (g);
118 gcry_mpi_point_release (q);
119 gcry_ctx_release (ctx);
120 fprintf (stderr, "\n");
121} 137}
122 138
123 139
@@ -128,17 +144,6 @@ main (int argc, char *argv[])
128 struct GNUNET_TIME_Absolute start; 144 struct GNUNET_TIME_Absolute start;
129 struct GNUNET_TIME_Relative delta; 145 struct GNUNET_TIME_Relative delta;
130 146
131 if (! gcry_check_version ("1.6.0"))
132 {
133 fprintf (stderr,
134 _
135 (
136 "libgcrypt has not the expected version (version %s is required).\n"),
137 "1.6.0");
138 return 0;
139 }
140 if (getenv ("GNUNET_GCRYPT_DEBUG"))
141 gcry_control (GCRYCTL_SET_DEBUG_FLAGS, 1u, 0);
142 GNUNET_log_setup ("perf-crypto-ecc-dlog", 147 GNUNET_log_setup ("perf-crypto-ecc-dlog",
143 "WARNING", 148 "WARNING",
144 NULL); 149 NULL);
@@ -154,10 +159,10 @@ main (int argc, char *argv[])
154 (start).rel_value_us / 1000LL, "ms/op"); 159 (start).rel_value_us / 1000LL, "ms/op");
155 start = GNUNET_TIME_absolute_get (); 160 start = GNUNET_TIME_absolute_get ();
156 /* first do a baseline run without the DLOG */ 161 /* first do a baseline run without the DLOG */
157 test_dlog (edc, GNUNET_NO); 162 test_dlog (edc, false);
158 delta = GNUNET_TIME_absolute_get_duration (start); 163 delta = GNUNET_TIME_absolute_get_duration (start);
159 start = GNUNET_TIME_absolute_get (); 164 start = GNUNET_TIME_absolute_get ();
160 test_dlog (edc, GNUNET_YES); 165 test_dlog (edc, true);
161 delta = GNUNET_TIME_relative_subtract (GNUNET_TIME_absolute_get_duration ( 166 delta = GNUNET_TIME_relative_subtract (GNUNET_TIME_absolute_get_duration (
162 start), 167 start),
163 delta); 168 delta);
@@ -165,7 +170,8 @@ main (int argc, char *argv[])
165 TEST_ITER, 170 TEST_ITER,
166 GNUNET_STRINGS_relative_time_to_string (delta, 171 GNUNET_STRINGS_relative_time_to_string (delta,
167 GNUNET_YES)); 172 GNUNET_YES));
168 GAUGER ("UTIL", "ECC DLOG operations", 173 GAUGER ("UTIL",
174 "ECC DLOG operations",
169 delta.rel_value_us / 1000LL / TEST_ITER, 175 delta.rel_value_us / 1000LL / TEST_ITER,
170 "ms/op"); 176 "ms/op");
171 177