aboutsummaryrefslogtreecommitdiff
path: root/src/util/perf_crypto_asymmetric.c
blob: 418d6dab39f7f5668d5bcab0aaaec2c6ffbc1029 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*
     This file is part of GNUnet.
     Copyright (C) 2015 GNUnet e.V.

     GNUnet is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published
     by the Free Software Foundation; either version 3, or (at your
     option) any later version.

     GNUnet is distributed in the hope that it will be useful, but
     WITHOUT ANY WARRANTY; without even the implied warranty of
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     General Public License for more details.

     You should have received a copy of the GNU General Public License
     along with GNUnet; see the file COPYING.  If not, write to the
     Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
     Boston, MA 02110-1301, USA.
*/

/**
 * @author Bart Polot
 * @file util/perf_crypto_asymmetric.c
 * @brief measure performance of public key functions
 */
#include "platform.h"
#include "gnunet_util_lib.h"
#include <gauger.h>

static struct GNUNET_TIME_Absolute start;

#define l 50

struct TestSig
{
  struct GNUNET_CRYPTO_EccSignaturePurpose purp;
  struct GNUNET_HashCode h;
  struct GNUNET_CRYPTO_EddsaSignature sig;
};


static void
log_duration (const char *cryptosystem,
              const char *description)
{
  struct GNUNET_TIME_Relative t;
  char s[64];

  sprintf (s, "%6s %15s", cryptosystem, description);
  t = GNUNET_TIME_absolute_get_duration (start);
  t = GNUNET_TIME_relative_divide (t, l);
  FPRINTF (stdout,
           "%s: %10s\n",
           s,
           GNUNET_STRINGS_relative_time_to_string (t,
                                                   GNUNET_NO));
  GAUGER ("UTIL", s, t.rel_value_us, "us");
}


int
main (int argc, char *argv[])
{
  int i;
  struct GNUNET_CRYPTO_EcdhePrivateKey *ecdhe[l];
  struct GNUNET_CRYPTO_EcdhePublicKey dhpub[l];
  struct GNUNET_CRYPTO_EddsaPrivateKey *eddsa[l];
  struct GNUNET_CRYPTO_EddsaPublicKey dspub[l];
  struct TestSig sig[l];

  start = GNUNET_TIME_absolute_get();
  for (i = 0; i < l; i++)
  {
    sig[i].purp.purpose = 0;
    sig[i].purp.size = htonl (sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose)
                              + sizeof (struct GNUNET_HashCode));
    GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_WEAK,
                                &sig[i].h,
                                sizeof (sig[i].h));
  }
  log_duration ("", "Init");

  start = GNUNET_TIME_absolute_get();
  for (i = 0; i < l; i++)
    eddsa[i] = GNUNET_CRYPTO_eddsa_key_create();
  log_duration ("EdDSA", "create key");

  start = GNUNET_TIME_absolute_get();
  for (i = 0; i < l; i++)
    GNUNET_CRYPTO_eddsa_key_get_public (eddsa[i], &dspub[i]);
  log_duration ("EdDSA", "get public");

  start = GNUNET_TIME_absolute_get();
  for (i = 0; i < l; i++)
    GNUNET_assert (GNUNET_OK ==
                   GNUNET_CRYPTO_eddsa_sign (eddsa[i],
                                             &sig[i].purp,
                                             &sig[i].sig));
  log_duration ("EdDSA", "sign HashCode");

  start = GNUNET_TIME_absolute_get();
  for (i = 0; i < l; i++)
    GNUNET_assert (GNUNET_OK ==
                   GNUNET_CRYPTO_eddsa_verify (0,
                                               &sig[i].purp,
                                               &sig[i].sig,
                                               &dspub[i]));
  log_duration ("EdDSA", "verify HashCode");

  start = GNUNET_TIME_absolute_get();
  for (i = 0; i < l; i++)
    ecdhe[i] = GNUNET_CRYPTO_ecdhe_key_create();
  log_duration ("ECDH", "create key");

  start = GNUNET_TIME_absolute_get();
  for (i = 0; i < l; i++)
    GNUNET_CRYPTO_ecdhe_key_get_public (ecdhe[i], &dhpub[i]);
  log_duration ("ECDH", "get public");

  start = GNUNET_TIME_absolute_get();
  for (i = 0; i < l - 1; i+=2)
  {
    GNUNET_CRYPTO_ecc_ecdh (ecdhe[i], &dhpub[i+1], &sig[i].h);
    GNUNET_CRYPTO_ecc_ecdh (ecdhe[i+1], &dhpub[i], &sig[i+1].h);
  }
  log_duration ("ECDH", "do DH");

  return 0;
}

/* end of perf_crypto_asymmetric.c */