tor  0.4.2.1-alpha-dev
Macros | Functions
crypto_rand_numeric.c File Reference
#include "lib/crypt_ops/crypto_rand.h"
#include "lib/log/util_bug.h"

Go to the source code of this file.

Macros

#define IMPLEMENT_RAND_UNSIGNED(type, maxval, limit, fill_stmt)
 

Functions

unsigned crypto_rand_uint (unsigned limit)
 
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)
 
uint64_t crypto_rand_uint64 (uint64_t max)
 
double crypto_rand_double (void)
 
unsigned crypto_fast_rng_get_uint (crypto_fast_rng_t *rng, unsigned limit)
 
uint64_t crypto_fast_rng_get_uint64 (crypto_fast_rng_t *rng, uint64_t limit)
 
uint32_t crypto_fast_rng_get_u32 (crypto_fast_rng_t *rng)
 
uint64_t crypto_fast_rng_uint64_range (crypto_fast_rng_t *rng, uint64_t min, uint64_t max)
 
double crypto_fast_rng_get_double (crypto_fast_rng_t *rng)
 

Detailed Description

Functions for retrieving uniformly distributed numbers from our PRNGs.

Definition in file crypto_rand_numeric.c.

Macro Definition Documentation

◆ IMPLEMENT_RAND_UNSIGNED

#define IMPLEMENT_RAND_UNSIGNED (   type,
  maxval,
  limit,
  fill_stmt 
)
Value:
do { \
type val; \
type cutoff; \
tor_assert((limit) > 0); \
\
/* We ignore any values that are >= 'cutoff,' to avoid biasing */ \
/* the distribution with clipping at the upper end of the type's */ \
/* range. */ \
cutoff = (maxval) - ((maxval)%(limit)); \
while (1) { \
fill_stmt; \
if (val < cutoff) \
return val % (limit); \
} \
} while (0)

Implementation macro: yields code that returns a uniform unbiased random number between 0 and limit. "type" is the type of the number to return; "maxval" is the largest possible value of "type"; and "fill_stmt" is a code snippet that fills an object named "val" with random bits.

Definition at line 17 of file crypto_rand_numeric.c.

Function Documentation

◆ crypto_fast_rng_get_double()

double crypto_fast_rng_get_double ( crypto_fast_rng_t rng)

As crypto_rand_get_double() but extract the result from a crypto_fast_rng_t.

Definition at line 188 of file crypto_rand_numeric.c.

References crypto_fast_rng_getbytes().

◆ crypto_fast_rng_get_u32()

uint32_t crypto_fast_rng_get_u32 ( crypto_fast_rng_t rng)

As crypto_rand_u32, but extract the result from a crypto_fast_rng_t.

Definition at line 161 of file crypto_rand_numeric.c.

References crypto_fast_rng_getbytes().

Referenced by genpareto_sample(), geometric_sample(), log_logistic_sample(), logistic_sample(), random_uniform_01(), and weibull_sample().

◆ crypto_fast_rng_get_uint()

unsigned crypto_fast_rng_get_uint ( crypto_fast_rng_t rng,
unsigned  limit 
)

As crypto_rand_uint, but extract the result from a crypto_fast_rng_t

Definition at line 139 of file crypto_rand_numeric.c.

References crypto_fast_rng_getbytes(), IMPLEMENT_RAND_UNSIGNED, and tor_assert().

Referenced by circuit_reset_sendme_randomness().

◆ crypto_fast_rng_get_uint64()

uint64_t crypto_fast_rng_get_uint64 ( crypto_fast_rng_t rng,
uint64_t  limit 
)

As crypto_rand_uint64, but extract the result from a crypto_fast_rng_t.

Definition at line 150 of file crypto_rand_numeric.c.

References crypto_fast_rng_getbytes(), IMPLEMENT_RAND_UNSIGNED, and tor_assert().

◆ crypto_fast_rng_uint64_range()

uint64_t crypto_fast_rng_uint64_range ( crypto_fast_rng_t rng,
uint64_t  min,
uint64_t  max 
)

As crypto_rand_uint64_range(), but extract the result from a crypto_fast_rng_t.

Definition at line 173 of file crypto_rand_numeric.c.

◆ crypto_rand_double()

double crypto_rand_double ( void  )

Return a pseudorandom double d, chosen uniformly from the range 0.0 <= d < 1.0.

Definition at line 126 of file crypto_rand_numeric.c.

◆ crypto_rand_int()

int crypto_rand_int ( unsigned int  max)

Return a pseudorandom integer, chosen uniformly from the values between 0 and max-1 inclusive. max must be between 1 and INT_MAX+1, inclusive.

Definition at line 52 of file crypto_rand_numeric.c.

References crypto_rand_uint(), and tor_assert().

Referenced by choose_array_element_by_weight(), and crypto_rand_int_range().

◆ crypto_rand_int_range()

int crypto_rand_int_range ( unsigned int  min,
unsigned int  max 
)

Return a pseudorandom integer, chosen uniformly from the values i such that min <= i < max.

min MUST be in range [0, max). max MUST be in range (min, INT_MAX].

Definition at line 71 of file crypto_rand_numeric.c.

References crypto_rand_int(), and tor_assert().

Referenced by crypto_random_hostname(), and intro_point_should_expire_now().

◆ crypto_rand_time_range()

time_t crypto_rand_time_range ( time_t  min,
time_t  max 
)

As crypto_rand_int_range, but supports time_t.

Definition at line 95 of file crypto_rand_numeric.c.

References crypto_rand_uint64(), and tor_assert().

◆ crypto_rand_uint()

unsigned crypto_rand_uint ( unsigned  limit)

Return a pseudorandom integer chosen uniformly from the values between 0 and limit-1 inclusive. limit must be strictly between 0 and UINT_MAX.

Definition at line 39 of file crypto_rand_numeric.c.

References IMPLEMENT_RAND_UNSIGNED, and tor_assert().

Referenced by crypto_rand_int().

◆ crypto_rand_uint64()

uint64_t crypto_rand_uint64 ( uint64_t  max)

Return a pseudorandom 64-bit integer, chosen uniformly from the values between 0 and max-1 inclusive.

Definition at line 106 of file crypto_rand_numeric.c.

References IMPLEMENT_RAND_UNSIGNED, and tor_assert().

Referenced by choose_array_element_by_weight(), crypto_rand_time_range(), and crypto_rand_uint64_range().

◆ crypto_rand_uint64_range()

uint64_t crypto_rand_uint64_range ( uint64_t  min,
uint64_t  max 
)

As crypto_rand_int_range, but supports uint64_t.

Definition at line 85 of file crypto_rand_numeric.c.

References crypto_rand_uint64(), and tor_assert().