exchange

Base system with REST service to issue digital coins, run by the payment service provider
Log | Files | Refs | Submodules | README | LICENSE

bench_age_restriction.c (4972B)


      1 /**
      2  * @file util/bench_age_restriction.c
      3  * @brief Measure Commit, Attest, Verify, Derive and Compare
      4  * @author Özgür Kesim
      5  *
      6  * compile in exchange/src/util with
      7  *
      8  * gcc benc_age_restriction.c \
      9  *    -lgnunetutil -lgnunetjson -lsodium -ljansson \
     10  *    -L/usr/lib/x86_64-linux-gnu -lmicrohttpd -ltalerutil \
     11  *    -I../include \
     12  *    -o bench_age_restriction
     13  *
     14  */
     15 #include "taler/platform.h"
     16 #include <math.h>
     17 #include <gnunet/gnunet_util_lib.h>
     18 #include <taler/taler_util.h>
     19 #include <taler/taler_crypto_lib.h>
     20 
     21 static struct TALER_AgeMask
     22   age_mask = { .bits = 1
     23                        | 1 << 8 | 1 << 10 | 1 << 12
     24                        | 1 << 14 | 1 << 16 | 1 << 18 | 1 << 21 };
     25 
     26 extern uint8_t
     27 get_age_group (
     28   const struct TALER_AgeMask *mask,
     29   uint8_t age);
     30 
     31 /**
     32  * Encodes the age mask into a string, like "8:10:12:14:16:18:21"
     33  */
     34 char *
     35 age_mask_to_string (
     36   const struct TALER_AgeMask *m)
     37 {
     38   uint32_t bits = m->bits;
     39   unsigned int n = 0;
     40   char *buf = GNUNET_malloc (32 * 3); // max characters possible
     41   char *pos = buf;
     42 
     43   if (NULL == buf)
     44   {
     45     return buf;
     46   }
     47 
     48   while (bits != 0)
     49   {
     50     bits >>= 1;
     51     n++;
     52     if (0 == (bits & 1))
     53     {
     54       continue;
     55     }
     56 
     57     if (n > 9)
     58     {
     59       *(pos++) = '0' + n / 10;
     60     }
     61     *(pos++) = '0' + n % 10;
     62 
     63     if (0 != (bits >> 1))
     64     {
     65       *(pos++) = ':';
     66     }
     67   }
     68   return buf;
     69 }
     70 
     71 
     72 #define ITER 2000
     73 
     74 double
     75 average (long *times, size_t size)
     76 {
     77   double mean = 0.0;
     78   for (int i = 0; i < size; i++)
     79   {
     80     mean += times[i];
     81   }
     82   return mean / size;
     83 }
     84 
     85 
     86 double
     87 stdev (long *times, size_t size)
     88 {
     89   double mean = average (times, size);
     90   double V = 0.0;
     91   for (int i = 0; i < size; i++)
     92   {
     93     double d = times[i] - mean;
     94     d *= d;
     95     V += d;
     96   }
     97   return sqrt (V / size);
     98 }
     99 
    100 
    101 #define pr(n,t, i) printf ("%10s (%dx):\t%.2f ± %.2fµs\n", (n), i, average ( \
    102                              &t[0], ITER) / 1000, stdev (&t[0], ITER) / 1000); \
    103         i = 0;
    104 
    105 #define starttime clock_gettime (CLOCK_MONOTONIC, &tstart)
    106 #define stoptime clock_gettime (CLOCK_MONOTONIC, &tend); \
    107         times[i] = ((long) tend.tv_sec * 1000 * 1000 * 1000 + tend.tv_nsec) \
    108                    - ((long) tstart.tv_sec * 1000 * 1000 * 1000 + tstart.tv_nsec \
    109                       );
    110 
    111 
    112 int
    113 main (int argc,
    114       const char *const argv[])
    115 {
    116   struct timespec tstart = {0,0}, tend = {0,0};
    117   enum GNUNET_GenericReturnValue ret;
    118   struct TALER_AgeCommitmentProof acp = {0};
    119   uint8_t age = 21;
    120   uint8_t age_group = get_age_group (&age_mask, age);
    121   struct GNUNET_HashCode seed;
    122   long times[ITER] = {0};
    123   int i = 0;
    124 
    125   //  commit
    126   for (; i < ITER; i++)
    127   {
    128     starttime;
    129     GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_WEAK,
    130                                 &seed,
    131                                 sizeof(seed));
    132 
    133     ret = TALER_age_restriction_commit (&age_mask,
    134                                         age,
    135                                         &seed,
    136                                         &acp);
    137     stoptime;
    138 
    139   }
    140   pr ("commit", times, i);
    141 
    142   // attest
    143   for (; i < ITER; i++)
    144   {
    145     GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_WEAK,
    146                                 &seed,
    147                                 sizeof(seed));
    148 
    149     ret = TALER_age_restriction_commit (&age_mask,
    150                                         age,
    151                                         &seed,
    152                                         &acp);
    153 
    154     starttime;
    155     uint8_t min_group = get_age_group (&age_mask, 13);
    156     struct TALER_AgeAttestationP at = {0};
    157     ret = TALER_age_commitment_attest (&acp,
    158                                        13,
    159                                        &at);
    160     stoptime;
    161   }
    162   pr ("attest", times, i);
    163 
    164   // verify
    165   for (; i < ITER; i++)
    166   {
    167     GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_WEAK,
    168                                 &seed,
    169                                 sizeof(seed));
    170 
    171     ret = TALER_age_restriction_commit (&age_mask,
    172                                         age,
    173                                         &seed,
    174                                         &acp);
    175 
    176     uint8_t min_group = get_age_group (&age_mask, 13);
    177     struct TALER_AgeAttestationP at = {0};
    178 
    179     ret = TALER_age_commitment_attest (&acp,
    180                                        13,
    181                                        &at);
    182     starttime;
    183     ret = TALER_age_commitment_verify (&acp.commitment,
    184                                        13,
    185                                        &at);
    186     stoptime;
    187   }
    188   pr ("verify", times, i);
    189 
    190   // derive
    191   for (; i < ITER; i++)
    192   {
    193     struct TALER_AgeCommitmentProof acp2 = {0};
    194     GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_WEAK,
    195                                 &seed,
    196                                 sizeof(seed));
    197     starttime;
    198     TALER_age_commitment_derive (&acp,
    199                                  &seed,
    200                                  &acp2);
    201     stoptime;
    202   }
    203   pr ("derive", times, i);
    204 
    205   return 0;
    206 }
    207 
    208 
    209 /* end of tv_age_restriction.c */