Tor 0.4.9.0-alpha-dev
Macros | Functions
crypto_rand_numeric.c File Reference

Functions for retrieving uniformly distributed numbers from our PRNGs. More...

#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.

◆ 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.

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.

Referenced by circuit_reset_sendme_randomness(), and extend_info_pick_orport().

◆ 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.

◆ 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.

Referenced by channelpadding_get_circuits_available_timeout(), channelpadding_get_netflow_inactive_timeout_ms(), 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.

Referenced by crypto_random_hostname().

◆ 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.

Referenced by randomize_time().

◆ 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 greater than 0, and less than UINT_MAX.

Definition at line 39 of file crypto_rand_numeric.c.

Referenced by clip_dns_fuzzy_ttl(), and 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.

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.