2 This file is part of GNUnet
3 (C) 2014 Christian Grothoff (and other contributing authors)
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.
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.
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.
22 * @file secretsharing/gnunet-secretsharing-profiler.c
23 * @brief profiling tool for distributed key generation and decryption
24 * @author Florian Dold
27 #include "gnunet_util_lib.h"
28 #include "gnunet_secretsharing_service.h"
29 #include "gnunet_testbed_service.h"
32 * How many peers should participate in the key generation?
34 static unsigned int num_peers = 3;
37 * What should the threshold for then key be?
39 static unsigned int threshold = 2;
42 * Should we try to decrypt a value after the key generation?
44 static unsigned int decrypt = GNUNET_NO;
47 * When would we like to see the operation finished?
49 static struct GNUNET_TIME_Relative timeout;
52 * When should dkg communication start?
54 static struct GNUNET_TIME_Relative delay;
57 * Handles for secretsharing sessions.
59 static struct GNUNET_SECRETSHARING_Session **session_handles;
61 static struct GNUNET_SECRETSHARING_DecryptionHandle **decrypt_handles;
64 * Shares we got from the distributed key generation.
66 static struct GNUNET_SECRETSHARING_Share **shares;
68 static struct GNUNET_SECRETSHARING_PublicKey common_pubkey;
73 static struct GNUNET_TESTBED_Operation **testbed_operations;
75 static unsigned int num_connected_sessions;
77 static unsigned int num_connected_decrypt;
79 static struct GNUNET_TESTBED_Peer **peers;
81 static struct GNUNET_PeerIdentity *peer_ids;
83 static unsigned int num_retrieved_peer_ids;
85 static unsigned int num_generated;
87 static unsigned int num_decrypted;
89 static struct GNUNET_HashCode session_id;
93 static struct GNUNET_SECRETSHARING_Plaintext reference_plaintext;
95 static struct GNUNET_SECRETSHARING_Ciphertext ciphertext;
97 static struct GNUNET_TIME_Absolute dkg_start;
99 static struct GNUNET_TIME_Absolute dkg_deadline;
102 static struct GNUNET_TIME_Absolute decrypt_start;
104 static struct GNUNET_TIME_Absolute decrypt_deadline;
108 * Signature of the event handler function called by the
109 * respective event controller.
112 * @param event information about the event
115 controller_cb (void *cls,
116 const struct GNUNET_TESTBED_EventInformation *event)
123 * Callback to be called when a service connect operation is completed
125 * @param cls the callback closure from functions generating an operation
126 * @param op the operation that has been finished
127 * @param ca_result the service handle returned from GNUNET_TESTBED_ConnectAdapter()
128 * @param emsg error message in case the operation has failed; will be NULL if
129 * operation has executed successfully.
132 session_connect_complete (void *cls,
133 struct GNUNET_TESTBED_Operation *op,
140 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
141 "testbed connect emsg: %s\n",
146 num_connected_sessions++;
148 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
149 "dkg: session connect complete\n");
151 if (num_connected_sessions == num_peers)
153 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
154 "dkg: all peers connected\n");
160 * Callback to be called when a service connect operation is completed
162 * @param cls the callback closure from functions generating an operation
163 * @param op the operation that has been finished
164 * @param ca_result the service handle returned from GNUNET_TESTBED_ConnectAdapter()
165 * @param emsg error message in case the operation has failed; will be NULL if
166 * operation has executed successfully.
169 decrypt_connect_complete (void *cls,
170 struct GNUNET_TESTBED_Operation *op,
177 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
178 "testbed connect emsg: %s\n",
183 num_connected_decrypt++;
185 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
186 "decrypt: session connect complete\n");
188 if (num_connected_decrypt == num_peers)
190 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
191 "decrypt: all peers connected\n");
197 * Called when a decryption has succeeded.
199 * @param cls Plaintext
200 * @param plaintext Plaintext
202 static void decrypt_cb (void *cls,
203 const struct GNUNET_SECRETSHARING_Plaintext *plaintext)
205 struct GNUNET_SECRETSHARING_DecryptionHandle **dhp = cls;
206 unsigned int n = dhp - decrypt_handles;
211 if (NULL == plaintext)
213 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "decrypt failed for peer %u\n", n);
216 else if (0 == memcmp (&reference_plaintext, plaintext, sizeof (struct GNUNET_SECRETSHARING_Plaintext)))
217 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "decrypt got correct result for peer %u\n", n);
219 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "decrypt got wrong result for peer %u\n", n);
221 if (num_decrypted == num_peers)
223 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "every peer decrypted\n");
224 GNUNET_SCHEDULER_shutdown ();
233 * Adapter function called to establish a connection to
237 * @param cfg configuration of the peer to connect to; will be available until
238 * GNUNET_TESTBED_operation_done() is called on the operation returned
239 * from GNUNET_TESTBED_service_connect()
240 * @return service handle to return in 'op_result', NULL on error
243 decrypt_connect_adapter (void *cls,
244 const struct GNUNET_CONFIGURATION_Handle *cfg)
246 struct GNUNET_SECRETSHARING_DecryptionHandle **hp = cls;
247 unsigned int n = hp - decrypt_handles;
249 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
250 "decrypt connect adapter, %d peers\n",
252 *hp = GNUNET_SECRETSHARING_decrypt (cfg, shares[n], &ciphertext,
253 decrypt_start, decrypt_deadline,
262 * Adapter function called to destroy a connection to
266 * @param op_result service handle returned from the connect adapter
269 decrypt_disconnect_adapter(void *cls, void *op_result)
271 struct GNUNET_SECRETSHARING_DecryptionHandle **dh = cls;
274 GNUNET_SECRETSHARING_decrypt_cancel (*dh);
281 secret_ready_cb (void *cls,
282 struct GNUNET_SECRETSHARING_Share *my_share,
283 struct GNUNET_SECRETSHARING_PublicKey *public_key,
284 unsigned int num_ready_peers,
285 struct GNUNET_PeerIdentity *ready_peers)
287 struct GNUNET_SECRETSHARING_Session **sp = cls;
288 unsigned int n = sp - session_handles;
289 char pubkey_str[1024];
294 shares[n] = my_share;
295 if (NULL == my_share)
297 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "key generation failed for peer #%u\n", n);
301 ret = GNUNET_STRINGS_data_to_string (public_key, sizeof *public_key, pubkey_str, 1024);
302 GNUNET_assert (NULL != ret);
304 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "key generation successful for peer #%u, pubkey %s\n", n,
307 /* we're the first to get the key -> store it */
308 if (num_generated == 1)
310 common_pubkey = *public_key;
312 else if (0 != memcmp (public_key, &common_pubkey, sizeof (struct GNUNET_SECRETSHARING_PublicKey)))
314 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "generated public keys do not match\n");
315 GNUNET_SCHEDULER_shutdown ();
320 // FIXME: destroy testbed operation
322 if (num_generated == num_peers)
325 if (GNUNET_NO == decrypt)
327 GNUNET_SCHEDULER_shutdown ();
331 decrypt_start = GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (), delay);
332 decrypt_deadline = GNUNET_TIME_absolute_add (decrypt_start, timeout);
336 GNUNET_SECRETSHARING_plaintext_generate_i (&reference_plaintext, 42);
337 GNUNET_SECRETSHARING_encrypt (&common_pubkey, &reference_plaintext, &ciphertext);
339 // FIXME: store the ops somewhere!
340 for (i = 0; i < num_peers; i++)
341 GNUNET_TESTBED_service_connect (NULL, peers[i], "secretsharing", &decrypt_connect_complete, NULL,
342 &decrypt_connect_adapter, &decrypt_disconnect_adapter, &decrypt_handles[i]);
348 * Adapter function called to establish a connection to
352 * @param cfg configuration of the peer to connect to; will be available until
353 * GNUNET_TESTBED_operation_done() is called on the operation returned
354 * from GNUNET_TESTBED_service_connect()
355 * @return service handle to return in 'op_result', NULL on error
358 session_connect_adapter (void *cls,
359 const struct GNUNET_CONFIGURATION_Handle *cfg)
361 struct GNUNET_SECRETSHARING_Session **sp = cls;
363 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
364 "connect adapter, %d peers\n",
366 *sp = GNUNET_SECRETSHARING_create_session (cfg,
373 &secret_ready_cb, sp);
380 * Adapter function called to destroy a connection to
384 * @param op_result service handle returned from the connect adapter
387 session_disconnect_adapter (void *cls, void *op_result)
389 struct GNUNET_SECRETSHARING_Session **sp = cls;
392 GNUNET_SECRETSHARING_session_destroy (*sp);
399 * Callback to be called when the requested peer information is available
401 * @param cb_cls the closure from GNUNET_TETSBED_peer_get_information()
402 * @param op the operation this callback corresponds to
403 * @param pinfo the result; will be NULL if the operation has failed
404 * @param emsg error message if the operation has failed; will be NULL if the
405 * operation is successfull
408 peer_info_cb (void *cb_cls,
409 struct GNUNET_TESTBED_Operation *op,
410 const struct GNUNET_TESTBED_PeerInformation *pinfo,
413 struct GNUNET_PeerIdentity *p;
416 GNUNET_assert (NULL == emsg);
418 p = (struct GNUNET_PeerIdentity *) cb_cls;
420 if (pinfo->pit == GNUNET_TESTBED_PIT_IDENTITY)
422 *p = *pinfo->result.id;
423 num_retrieved_peer_ids++;
424 if (num_retrieved_peer_ids == num_peers)
425 for (i = 0; i < num_peers; i++)
426 testbed_operations[i] =
427 GNUNET_TESTBED_service_connect (NULL, peers[i], "secretsharing", session_connect_complete, NULL,
428 session_connect_adapter, session_disconnect_adapter, &session_handles[i]);
435 GNUNET_TESTBED_operation_done (op);
440 * Signature of a main function for a testcase.
443 * @param h the run handle
444 * @param num_peers number of peers in 'peers'
445 * @param started_peers handle to peers run in the testbed. NULL upon timeout (see
446 * GNUNET_TESTBED_test_run()).
447 * @param links_succeeded the number of overlay link connection attempts that
449 * @param links_failed the number of overlay link connection attempts that
453 test_master (void *cls,
454 struct GNUNET_TESTBED_RunHandle *h,
455 unsigned int num_peers,
456 struct GNUNET_TESTBED_Peer **started_peers,
457 unsigned int links_succeeded,
458 unsigned int links_failed)
462 GNUNET_log_setup ("gnunet-secretsharing-profiler", "INFO", NULL);
464 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "test master\n");
466 peers = started_peers;
468 peer_ids = GNUNET_malloc (num_peers * sizeof (struct GNUNET_PeerIdentity));
470 session_handles = GNUNET_new_array (num_peers, struct GNUNET_SECRETSHARING_Session *);
471 decrypt_handles = GNUNET_new_array (num_peers, struct GNUNET_SECRETSHARING_DecryptionHandle *);
472 testbed_operations = GNUNET_new_array (num_peers, struct GNUNET_TESTBED_Operation *);
473 shares = GNUNET_new_array (num_peers, struct GNUNET_SECRETSHARING_Share *);
476 for (i = 0; i < num_peers; i++)
477 GNUNET_TESTBED_peer_get_information (peers[i],
478 GNUNET_TESTBED_PIT_IDENTITY,
485 run (void *cls, char *const *args, const char *cfgfile,
486 const struct GNUNET_CONFIGURATION_Handle *cfg)
488 static char *session_str = "gnunet-secretsharing/test";
490 int topology_cmp_result;
492 dkg_start = GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (), delay);
493 dkg_deadline = GNUNET_TIME_absolute_add (dkg_start, timeout);
495 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (cfg, "testbed", "OVERLAY_TOPOLOGY", &topology))
498 "'OVERLAY_TOPOLOGY' not found in 'testbed' config section, "
499 "seems like you passed the wrong configuration file\n");
503 topology_cmp_result = strcasecmp (topology, "NONE");
504 GNUNET_free (topology);
506 if (0 == topology_cmp_result)
509 "'OVERLAY_TOPOLOGY' set to 'NONE', "
510 "seems like you passed the wrong configuration file\n");
514 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
515 "running gnunet-secretsharing-profiler\n");
517 GNUNET_CRYPTO_hash (session_str, strlen (session_str), &session_id);
519 (void) GNUNET_TESTBED_test_run ("gnunet-secretsharing-profiler",
531 main (int argc, char **argv)
533 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
534 { 'n', "num-peers", NULL,
535 gettext_noop ("number of peers in consensus"),
536 GNUNET_YES, &GNUNET_GETOPT_set_uint, &num_peers },
537 { 'D', "delay", NULL,
538 gettext_noop ("dkg start delay"),
539 GNUNET_YES, &GNUNET_GETOPT_set_relative_time, &delay },
540 { 't', "timeout", NULL,
541 gettext_noop ("dkg timeout"),
542 GNUNET_YES, &GNUNET_GETOPT_set_relative_time, &timeout },
543 { 'k', "threshold", NULL,
544 gettext_noop ("threshold"),
545 GNUNET_YES, &GNUNET_GETOPT_set_uint, &threshold },
546 { 'd', "decrypt", NULL,
547 gettext_noop ("also profile decryption"),
548 GNUNET_NO, &GNUNET_GETOPT_set_one, &decrypt },
549 { 'V', "verbose", NULL,
550 gettext_noop ("be more verbose (print received values)"),
551 GNUNET_NO, &GNUNET_GETOPT_set_one, &verbose },
552 GNUNET_GETOPT_OPTION_END
554 delay = GNUNET_TIME_UNIT_ZERO;
555 timeout = GNUNET_TIME_UNIT_MINUTES;
556 GNUNET_PROGRAM_run2 (argc, argv, "gnunet-secretsharing-profiler",
558 options, &run, NULL, GNUNET_YES);