2 This file is part of GNUnet
3 Copyright (C) 2014 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.
17 * @file secretsharing/gnunet-secretsharing-profiler.c
18 * @brief profiling tool for distributed key generation and decryption
19 * @author Florian Dold
22 #include "gnunet_util_lib.h"
23 #include "gnunet_secretsharing_service.h"
24 #include "gnunet_testbed_service.h"
27 * How many peers should participate in the key generation?
29 static unsigned int num_peers = 3;
32 * What should the threshold for then key be?
34 static unsigned int threshold = 2;
37 * Should we try to decrypt a value after the key generation?
39 static int decrypt = GNUNET_NO;
42 * When would we like to see the operation finished?
44 static struct GNUNET_TIME_Relative timeout;
47 * When should dkg communication start?
49 static struct GNUNET_TIME_Relative delay;
52 * Handles for secretsharing sessions.
54 static struct GNUNET_SECRETSHARING_Session **session_handles;
56 static struct GNUNET_SECRETSHARING_DecryptionHandle **decrypt_handles;
59 * Shares we got from the distributed key generation.
61 static struct GNUNET_SECRETSHARING_Share **shares;
63 static struct GNUNET_SECRETSHARING_PublicKey common_pubkey;
66 static unsigned int num_connected_sessions;
68 static unsigned int num_connected_decrypt;
71 * Handles to the running peers.
72 * When peers[i] is NULL, the i-th peer has stopped.
74 static struct GNUNET_TESTBED_Peer **peers;
76 static struct GNUNET_PeerIdentity *peer_ids;
78 static unsigned int num_retrieved_peer_ids;
80 static unsigned int num_generated;
82 static unsigned int num_decrypted;
84 static struct GNUNET_HashCode session_id;
86 static unsigned int verbose;
88 static struct GNUNET_SECRETSHARING_Plaintext reference_plaintext;
90 static struct GNUNET_SECRETSHARING_Ciphertext ciphertext;
92 static struct GNUNET_TIME_Absolute dkg_start;
94 static struct GNUNET_TIME_Absolute dkg_deadline;
97 static struct GNUNET_TIME_Absolute decrypt_start;
99 static struct GNUNET_TIME_Absolute decrypt_deadline;
102 * Connect operations, one for every peer.
104 static struct GNUNET_TESTBED_Operation **connect_ops;
107 * Are we performing a shutdown right now?
109 static int in_shutdown;
113 * Signature of the event handler function called by the
114 * respective event controller.
117 * @param event information about the event
120 controller_cb (void *cls,
121 const struct GNUNET_TESTBED_EventInformation *event)
128 * Callback to be called when a service connect operation is completed
130 * @param cls the callback closure from functions generating an operation
131 * @param op the operation that has been finished
132 * @param ca_result the service handle returned from GNUNET_TESTBED_ConnectAdapter()
133 * @param emsg error message in case the operation has failed; will be NULL if
134 * operation has executed successfully.
137 session_connect_complete (void *cls,
138 struct GNUNET_TESTBED_Operation *op,
145 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
146 "testbed connect emsg: %s\n",
151 num_connected_sessions++;
153 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
154 "dkg: session connect complete\n");
156 if (num_connected_sessions == num_peers)
158 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
159 "dkg: all peers connected\n");
165 * Callback to be called when a service connect operation is completed
167 * @param cls the callback closure from functions generating an operation
168 * @param op the operation that has been finished
169 * @param ca_result the service handle returned from GNUNET_TESTBED_ConnectAdapter()
170 * @param emsg error message in case the operation has failed; will be NULL if
171 * operation has executed successfully.
174 decrypt_connect_complete (void *cls,
175 struct GNUNET_TESTBED_Operation *op,
182 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
183 "testbed connect emsg: %s\n",
188 num_connected_decrypt++;
190 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
191 "decrypt: session connect complete\n");
193 if (num_connected_decrypt == num_peers)
195 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
196 "decrypt: all peers connected\n");
202 * Called when a decryption has succeeded.
204 * @param cls Plaintext
205 * @param plaintext Plaintext
207 static void decrypt_cb (void *cls,
208 const struct GNUNET_SECRETSHARING_Plaintext *plaintext)
210 struct GNUNET_SECRETSHARING_DecryptionHandle **dhp = cls;
211 unsigned int n = dhp - decrypt_handles;
216 // we should still be connected if this is called
217 GNUNET_assert (NULL != connect_ops[n]);
219 GNUNET_TESTBED_operation_done (connect_ops[n]);
221 if (NULL == plaintext)
223 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "decrypt failed for peer %u\n", n);
226 else if (0 == memcmp (&reference_plaintext, plaintext, sizeof (struct GNUNET_SECRETSHARING_Plaintext)))
227 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "decrypt got correct result for peer %u\n", n);
229 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "decrypt got wrong result for peer %u\n", n);
231 if (num_decrypted == num_peers)
233 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "every peer decrypted\n");
234 GNUNET_SCHEDULER_shutdown ();
243 * Adapter function called to establish a connection to
247 * @param cfg configuration of the peer to connect to; will be available until
248 * GNUNET_TESTBED_operation_done() is called on the operation returned
249 * from GNUNET_TESTBED_service_connect()
250 * @return service handle to return in 'op_result', NULL on error
253 decrypt_connect_adapter (void *cls,
254 const struct GNUNET_CONFIGURATION_Handle *cfg)
256 struct GNUNET_SECRETSHARING_DecryptionHandle **hp = cls;
257 unsigned int n = hp - decrypt_handles;
259 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
260 "decrypt connect adapter, %d peers\n",
262 *hp = GNUNET_SECRETSHARING_decrypt (cfg, shares[n], &ciphertext,
263 decrypt_start, decrypt_deadline,
272 * Adapter function called to destroy a connection to
276 * @param op_result service handle returned from the connect adapter
279 decrypt_disconnect_adapter(void *cls, void *op_result)
281 struct GNUNET_SECRETSHARING_DecryptionHandle **dh = cls;
282 unsigned int n = dh - decrypt_handles;
284 GNUNET_assert (*dh == decrypt_handles[n]);
288 GNUNET_SECRETSHARING_decrypt_cancel (*dh);
292 GNUNET_assert (NULL != connect_ops[n]);
293 connect_ops[n] = NULL;
298 secret_ready_cb (void *cls,
299 struct GNUNET_SECRETSHARING_Share *my_share,
300 struct GNUNET_SECRETSHARING_PublicKey *public_key,
301 unsigned int num_ready_peers,
302 const struct GNUNET_PeerIdentity *ready_peers)
304 struct GNUNET_SECRETSHARING_Session **sp = cls;
305 unsigned int n = sp - session_handles;
306 char pubkey_str[1024];
311 shares[n] = my_share;
312 if (NULL == my_share)
314 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "key generation failed for peer #%u\n", n);
318 ret = GNUNET_STRINGS_data_to_string (public_key, sizeof *public_key, pubkey_str, 1024);
319 GNUNET_assert (NULL != ret);
321 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "key generation successful for peer #%u, pubkey %s\n", n,
324 /* we're the first to get the key -> store it */
325 if (num_generated == 1)
327 common_pubkey = *public_key;
329 else if (0 != memcmp (public_key, &common_pubkey, sizeof (struct GNUNET_SECRETSHARING_PublicKey)))
331 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "generated public keys do not match\n");
332 GNUNET_SCHEDULER_shutdown ();
337 // we should still be connected
338 GNUNET_assert (NULL != connect_ops[n]);
340 // disconnect from the service, will call the disconnect callback
341 GNUNET_TESTBED_operation_done (connect_ops[n]);
347 * Adapter function called to establish a connection to
351 * @param cfg configuration of the peer to connect to; will be available until
352 * GNUNET_TESTBED_operation_done() is called on the operation returned
353 * from GNUNET_TESTBED_service_connect()
354 * @return service handle to return in 'op_result', NULL on error
357 session_connect_adapter (void *cls,
358 const struct GNUNET_CONFIGURATION_Handle *cfg)
360 struct GNUNET_SECRETSHARING_Session **sp = cls;
362 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
363 "connect adapter, %d peers\n",
365 *sp = GNUNET_SECRETSHARING_create_session (cfg,
372 &secret_ready_cb, sp);
379 * Adapter function called to destroy a connection to
383 * @param op_result service handle returned from the connect adapter
386 session_disconnect_adapter (void *cls, void *op_result)
388 struct GNUNET_SECRETSHARING_Session **sp = cls;
389 unsigned int n = (sp - session_handles);
391 GNUNET_assert (*sp == session_handles[n]);
395 GNUNET_SECRETSHARING_session_destroy (*sp);
399 GNUNET_assert (NULL != connect_ops[n]);
400 connect_ops[n] = NULL;
402 if (GNUNET_YES == in_shutdown)
405 // all peers received their secret
406 if (num_generated == num_peers)
410 // only do decryption if requested by the user
411 if (GNUNET_NO == decrypt)
413 GNUNET_SCHEDULER_shutdown ();
417 decrypt_start = GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (), delay);
418 decrypt_deadline = GNUNET_TIME_absolute_add (decrypt_start, timeout);
420 // compute g^42 as the plaintext which we will decrypt and then
421 // cooperatively decrypt
422 GNUNET_SECRETSHARING_plaintext_generate_i (&reference_plaintext, 42);
423 GNUNET_SECRETSHARING_encrypt (&common_pubkey, &reference_plaintext, &ciphertext);
425 for (i = 0; i < num_peers; i++)
427 GNUNET_TESTBED_service_connect (NULL, peers[i], "secretsharing", &decrypt_connect_complete, NULL,
428 &decrypt_connect_adapter, &decrypt_disconnect_adapter, &decrypt_handles[i]);
434 * Callback to be called when the requested peer information is available
436 * @param cb_cls the closure from GNUNET_TETSBED_peer_get_information()
437 * @param op the operation this callback corresponds to
438 * @param pinfo the result; will be NULL if the operation has failed
439 * @param emsg error message if the operation has failed; will be NULL if the
440 * operation is successfull
443 peer_info_cb (void *cb_cls,
444 struct GNUNET_TESTBED_Operation *op,
445 const struct GNUNET_TESTBED_PeerInformation *pinfo,
448 struct GNUNET_PeerIdentity *p;
451 GNUNET_assert (NULL == emsg);
453 p = (struct GNUNET_PeerIdentity *) cb_cls;
455 if (pinfo->pit == GNUNET_TESTBED_PIT_IDENTITY)
457 *p = *pinfo->result.id;
458 num_retrieved_peer_ids++;
459 if (num_retrieved_peer_ids == num_peers)
460 for (i = 0; i < num_peers; i++)
462 GNUNET_TESTBED_service_connect (NULL, peers[i], "secretsharing", session_connect_complete, NULL,
463 session_connect_adapter, session_disconnect_adapter, &session_handles[i]);
470 GNUNET_TESTBED_operation_done (op);
475 * Signature of the main function of a task.
480 handle_shutdown (void *cls)
482 in_shutdown = GNUNET_YES;
484 if (NULL != connect_ops)
487 for (i = 0; i < num_peers; i++)
488 if (NULL != connect_ops[i])
490 // the disconnect callback will set the op to NULL
491 GNUNET_TESTBED_operation_done (connect_ops[i]);
493 GNUNET_free (connect_ops);
496 // killing the testbed operation will take care of remaining
497 // service handles in the disconnect callback
502 * Signature of a main function for a testcase.
505 * @param h the run handle
506 * @param num_peers number of peers in 'peers'
507 * @param started_peers handle to peers run in the testbed. NULL upon timeout (see
508 * GNUNET_TESTBED_test_run()).
509 * @param links_succeeded the number of overlay link connection attempts that
511 * @param links_failed the number of overlay link connection attempts that
515 test_master (void *cls,
516 struct GNUNET_TESTBED_RunHandle *h,
517 unsigned int num_peers,
518 struct GNUNET_TESTBED_Peer **started_peers,
519 unsigned int links_succeeded,
520 unsigned int links_failed)
524 GNUNET_log_setup ("gnunet-secretsharing-profiler", "INFO", NULL);
526 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "test master\n");
528 GNUNET_SCHEDULER_add_shutdown (&handle_shutdown, NULL);
530 peers = started_peers;
532 peer_ids = GNUNET_malloc (num_peers * sizeof (struct GNUNET_PeerIdentity));
534 session_handles = GNUNET_new_array (num_peers, struct GNUNET_SECRETSHARING_Session *);
535 decrypt_handles = GNUNET_new_array (num_peers, struct GNUNET_SECRETSHARING_DecryptionHandle *);
536 connect_ops = GNUNET_new_array (num_peers, struct GNUNET_TESTBED_Operation *);
537 shares = GNUNET_new_array (num_peers, struct GNUNET_SECRETSHARING_Share *);
539 for (i = 0; i < num_peers; i++)
541 // we do not store the returned operation, as peer_info_cb
542 // will receive it as a parameter and call GNUNET_TESTBED_operation_done.
543 GNUNET_TESTBED_peer_get_information (peers[i],
544 GNUNET_TESTBED_PIT_IDENTITY,
552 run (void *cls, char *const *args, const char *cfgfile,
553 const struct GNUNET_CONFIGURATION_Handle *cfg)
555 static char *session_str = "gnunet-secretsharing/test";
557 int topology_cmp_result;
559 dkg_start = GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (), delay);
560 dkg_deadline = GNUNET_TIME_absolute_add (dkg_start, timeout);
562 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (cfg, "testbed", "OVERLAY_TOPOLOGY", &topology))
565 "'OVERLAY_TOPOLOGY' not found in 'testbed' config section, "
566 "seems like you passed the wrong configuration file\n");
570 topology_cmp_result = strcasecmp (topology, "NONE");
571 GNUNET_free (topology);
573 if (0 == topology_cmp_result)
576 "'OVERLAY_TOPOLOGY' set to 'NONE', "
577 "seems like you passed the wrong configuration file\n");
581 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
582 "running gnunet-secretsharing-profiler\n");
584 GNUNET_CRYPTO_hash (session_str, strlen (session_str), &session_id);
586 (void) GNUNET_TESTBED_test_run ("gnunet-secretsharing-profiler",
598 main (int argc, char **argv)
600 struct GNUNET_GETOPT_CommandLineOption options[] = {
602 GNUNET_GETOPT_option_uint ('n',
605 gettext_noop ("number of peers in consensus"),
608 GNUNET_GETOPT_option_relative_time ('D',
611 gettext_noop ("dkg start delay"),
614 GNUNET_GETOPT_option_relative_time ('t',
617 gettext_noop ("dkg timeout"),
620 GNUNET_GETOPT_option_uint ('k',
623 gettext_noop ("threshold"),
626 GNUNET_GETOPT_option_flag ('d',
628 gettext_noop ("also profile decryption"),
632 GNUNET_GETOPT_option_verbose (&verbose),
634 GNUNET_GETOPT_OPTION_END
636 delay = GNUNET_TIME_UNIT_ZERO;
637 timeout = GNUNET_TIME_UNIT_MINUTES;
638 GNUNET_PROGRAM_run2 (argc, argv, "gnunet-secretsharing-profiler",
640 options, &run, NULL, GNUNET_YES);