Commit 2ccf3268 authored by George Kadianakis's avatar George Kadianakis
Browse files

Implement and test probability distributions used by WTF-PAD.



This project introduces the prob_distr.c subsystem which implements all the
probability distributions that WTF-PAD needs. It also adds unittests for all of
them.

Code and tests courtesy of Riastradh.

Co-authored-by: default avatarTaylor R Campbell <campbell+tor@mumble.net>
Co-authored-by: default avatarMike Perry <mikeperry-git@torproject.org>
parent 8ad497bb
Loading
Loading
Loading
Loading
+4 −4
Original line number Diff line number Diff line
@@ -516,10 +516,10 @@ circpad_distribution_sample(circpad_distribution_t dist)
       * param1 is Alpha, param2 is Beta */
      return dist.param1 * pow(p/(1.0-p), 1.0/dist.param2);
    case CIRCPAD_DIST_GEOMETRIC:
      p = crypto_rand_double();
      /* https://github.com/distributions-io/geometric-quantile/
       * param1 is 'p' (success probability) */
      return ceil(tor_mathlog(1.0-p)/tor_mathlog(1.0-dist.param1));
      {
        /* param1 is 'p' (success probability) */
        return geometric_sample(dist.param1);
      }
    case CIRCPAD_DIST_WEIBULL:
      p = crypto_rand_double();
      /* https://en.wikipedia.org/wiki/Weibull_distribution \
+11 −0
Original line number Diff line number Diff line
@@ -528,6 +528,17 @@ crypto_rand_unmocked(char *to, size_t n)
#endif
}

/**
 * Draw an unsigned 32-bit integer uniformly at random.
 */
uint32_t
crypto_rand_uint32(void)
{
  uint32_t rand;
  crypto_rand((void*)&rand, sizeof(rand));
  return rand;
}

/**
 * Return a pseudorandom integer, chosen uniformly from the values
 * between 0 and <b>max</b>-1 inclusive.  <b>max</b> must be between 1 and
+1 −0
Original line number Diff line number Diff line
@@ -27,6 +27,7 @@ int crypto_rand_int(unsigned int max);
int crypto_rand_int_range(unsigned int min, unsigned int max);
uint64_t crypto_rand_uint64_range(uint64_t min, uint64_t max);
time_t crypto_rand_time_range(time_t min, time_t max);
uint32_t crypto_rand_uint32(void);
uint64_t crypto_rand_uint64(uint64_t max);
double crypto_rand_double(void);
struct tor_weak_rng_t;
+2 −0
Original line number Diff line number Diff line
@@ -3,3 +3,5 @@ orconfig.h
lib/cc/*.h
lib/log/*.h
lib/math/*.h
lib/testsupport/*.h
lib/crypt_ops/*.h
+25 −0
Original line number Diff line number Diff line
@@ -117,3 +117,28 @@ ENABLE_GCC_WARNING(double-promotion)
ENABLE_GCC_WARNING(float-conversion)
#endif
}

/* isinf() wrapper for tor */
int
tor_isinf(double x)
{
  /* Same as above, work around the "double promotion" warnings */
#if defined(MINGW_ANY) && GCC_VERSION >= 409
#define PROBLEMATIC_FLOAT_CONVERSION_WARNING
DISABLE_GCC_WARNING(float-conversion)
#endif /* defined(MINGW_ANY) && GCC_VERSION >= 409 */
#if defined(__clang__)
#if __has_warning("-Wdouble-promotion")
#define PROBLEMATIC_DOUBLE_PROMOTION_WARNING
DISABLE_GCC_WARNING(double-promotion)
#endif
#endif /* defined(__clang__) */
  return isinf(x);
#ifdef PROBLEMATIC_DOUBLE_PROMOTION_WARNING
ENABLE_GCC_WARNING(double-promotion)
#endif
#ifdef PROBLEMATIC_FLOAT_CONVERSION_WARNING
ENABLE_GCC_WARNING(float-conversion)
#endif
}
Loading