summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--source/kit/bigint.h32
-rw-r--r--source/kit/mersenne_twister_64.c41
-rw-r--r--source/kit/mersenne_twister_64.h10
-rw-r--r--source/kit/secure_random.c33
4 files changed, 63 insertions, 53 deletions
diff --git a/source/kit/bigint.h b/source/kit/bigint.h
index f8e76db..7a00cba 100644
--- a/source/kit/bigint.h
+++ b/source/kit/bigint.h
@@ -341,35 +341,35 @@ typedef struct {
*/
static kit_bi_division_t kit_bi_udiv(kit_bigint_t const x,
kit_bigint_t y) {
- kit_bi_division_t result;
- memset(&result, 0, sizeof result);
+ kit_bi_division_t z;
+ memset(&z, 0, sizeof z);
ptrdiff_t const y_bits = kit_bi_significant_bit_count(y);
if (y_bits == 0) {
- result.undefined = 1;
- return result;
+ z.undefined = 1;
+ return z;
}
ptrdiff_t const x_bits = kit_bi_significant_bit_count(x);
ptrdiff_t shift = x_bits - y_bits;
- result.remainder = x;
- result.quotient = kit_bi_uint32(0);
+ z.remainder = x;
+ z.quotient = kit_bi_uint32(0);
y = kit_bi_shl_uint(y, (uint32_t) shift);
while (shift >= 0) {
- if (kit_bi_compare(result.remainder, y) >= 0) {
- result.remainder = kit_bi_sub(result.remainder, y);
- result.quotient.v[shift / 32] |= (1u << (shift % 32));
+ if (kit_bi_compare(z.remainder, y) >= 0) {
+ z.remainder = kit_bi_sub(z.remainder, y);
+ z.quotient.v[shift / 32] |= (1u << (shift % 32));
}
y = kit_bi_shr_uint(y, 1);
shift--;
}
- return result;
+ return z;
}
/* Signed division.
@@ -382,14 +382,12 @@ static kit_bi_division_t kit_bi_div(kit_bigint_t const x,
int const x_neg = kit_bi_is_neg(x);
int const y_neg = kit_bi_is_neg(y);
- if (!x_neg && !y_neg)
- return kit_bi_udiv(x, y);
- if (x_neg && y_neg)
- return kit_bi_udiv(kit_bi_neg(x), kit_bi_neg(y));
-
kit_bigint_t const x_abs = x_neg ? kit_bi_neg(x) : x;
kit_bigint_t const y_abs = y_neg ? kit_bi_neg(y) : y;
+ if (x_neg == y_neg)
+ return kit_bi_udiv(x_abs, y_abs);
+
kit_bi_division_t z = kit_bi_udiv(x_abs, y_abs);
if (!kit_bi_is_zero(z.remainder) && !y_neg)
@@ -402,6 +400,8 @@ static kit_bi_division_t kit_bi_div(kit_bigint_t const x,
static void kit_bi_serialize(kit_bigint_t const in,
uint8_t *const out) {
+ assert(out != NULL);
+
for (ptrdiff_t i = 0; i < KIT_BIGINT_SIZE / 4; i++) {
out[i * 4] = (uint8_t) (in.v[i] & 0xff);
out[i * 4 + 1] = (uint8_t) ((in.v[i] >> 8) & 0xff);
@@ -411,6 +411,8 @@ static void kit_bi_serialize(kit_bigint_t const in,
}
static kit_bigint_t kit_bi_deserialize(uint8_t const *const in) {
+ assert(in != NULL);
+
kit_bigint_t out;
memset(&out, 0, sizeof out);
diff --git a/source/kit/mersenne_twister_64.c b/source/kit/mersenne_twister_64.c
index 0570a1f..926a886 100644
--- a/source/kit/mersenne_twister_64.c
+++ b/source/kit/mersenne_twister_64.c
@@ -23,33 +23,36 @@ void kit_mt64_init(kit_mt64_state_t *const state,
kit_mt64_init_array(state, 1, &seed);
}
-uint64_t kit_mt64_generate(kit_mt64_state_t *const state) {
+void kit_mt64_rotate(kit_mt64_state_t *const state) {
static uint64_t const mag01[2] = { 0ull, MATRIX_A };
- int i;
uint64_t x;
+ int i;
- if (state->index >= KIT_MT64_N) {
- for (i = 0; i < KIT_MT64_N - MM; i++) {
- x = (state->mt[i] & UM) | (state->mt[i + 1] & LM);
- state->mt[i] = state->mt[i + MM] ^ (x >> 1u) ^
- mag01[(int) (x & 1ull)];
- }
+ for (i = 0; i < KIT_MT64_N - MM; i++) {
+ x = (state->mt[i] & UM) | (state->mt[i + 1] & LM);
+ state->mt[i] = state->mt[i + MM] ^ (x >> 1u) ^
+ mag01[(int) (x & 1ull)];
+ }
- for (; i < KIT_MT64_N - 1; i++) {
- x = (state->mt[i] & UM) | (state->mt[i + 1] & LM);
- state->mt[i] = state->mt[i + (MM - KIT_MT64_N)] ^ (x >> 1u) ^
- mag01[(int) (x & 1ull)];
- }
+ for (; i < KIT_MT64_N - 1; i++) {
+ x = (state->mt[i] & UM) | (state->mt[i + 1] & LM);
+ state->mt[i] = state->mt[i + (MM - KIT_MT64_N)] ^ (x >> 1u) ^
+ mag01[(int) (x & 1ull)];
+ }
- x = (state->mt[KIT_MT64_N - 1] & UM) | (state->mt[0] & LM);
- state->mt[KIT_MT64_N - 1] = state->mt[MM - 1] ^ (x >> 1u) ^
- mag01[(int) (x & 1ull)];
+ x = (state->mt[KIT_MT64_N - 1] & UM) | (state->mt[0] & LM);
+ state->mt[KIT_MT64_N - 1] = state->mt[MM - 1] ^ (x >> 1u) ^
+ mag01[(int) (x & 1ull)];
- state->index = 0;
- }
+ state->index = 0;
+}
+
+uint64_t kit_mt64_generate(kit_mt64_state_t *const state) {
+ if (state->index >= KIT_MT64_N)
+ kit_mt64_rotate(state);
- x = state->mt[state->index++];
+ uint64_t x = state->mt[state->index++];
x ^= (x >> 29u) & 0x5555555555555555ull;
x ^= (x << 17u) & 0x71d67fffeda60000ull;
diff --git a/source/kit/mersenne_twister_64.h b/source/kit/mersenne_twister_64.h
index 67a4d22..cc1f3d7 100644
--- a/source/kit/mersenne_twister_64.h
+++ b/source/kit/mersenne_twister_64.h
@@ -17,17 +17,17 @@ typedef struct {
uint64_t index;
} kit_mt64_state_t;
-void kit_mt64_init_array(kit_mt64_state_t *state, ptrdiff_t size,
- uint64_t const *seed);
-
-void kit_mt64_init(kit_mt64_state_t *state, uint64_t seed);
-
+void kit_mt64_init_array(kit_mt64_state_t *state, ptrdiff_t size,
+ uint64_t const *seed);
+void kit_mt64_init(kit_mt64_state_t *state, uint64_t seed);
+void kit_mt64_rotate(kit_mt64_state_t *state);
uint64_t kit_mt64_generate(kit_mt64_state_t *state);
#ifndef KIT_DISABLE_SHORT_NAMES
# define mt64_state_t kit_mt64_state_t
# define mt64_init_array kit_mt64_init_array
# define mt64_init kit_mt64_init
+# define mt64_rotate kit_mt64_rotate
# define mt64_generate kit_mt64_generate
# define mt64_seed kit_mt64_seed
#endif
diff --git a/source/kit/secure_random.c b/source/kit/secure_random.c
index 013fd52..65b567c 100644
--- a/source/kit/secure_random.c
+++ b/source/kit/secure_random.c
@@ -60,23 +60,21 @@ static void secure_random_fallback(ptrdiff_t const size,
kit_mt64_state_t state;
if (time_sec == 0 && time_nsec == 0) {
- uint64_t seed[4] = { n, get_available_memory(),
- (uint64_t) t.tv_sec, (uint64_t) t.tv_nsec };
+ uint64_t const seed[] = { n, get_available_memory(),
+ (uint64_t) t.tv_sec,
+ (uint64_t) t.tv_nsec };
kit_mt64_init_array(&state, sizeof seed / sizeof *seed, seed);
} else {
- uint64_t seed[6] = { n,
- get_available_memory(),
- (uint64_t) t.tv_sec,
- (uint64_t) t.tv_nsec,
- (uint64_t) t.tv_sec - time_sec,
- (uint64_t) t.tv_nsec - time_nsec };
+ uint64_t const seed[] = { n,
+ get_available_memory(),
+ (uint64_t) t.tv_sec,
+ (uint64_t) t.tv_nsec,
+ (uint64_t) t.tv_sec - time_sec,
+ (uint64_t) t.tv_nsec - time_nsec };
kit_mt64_init_array(&state, sizeof seed / sizeof *seed, seed);
}
- /* Bootstrap the generator.
- */
- for (ptrdiff_t i = 0; i < KIT_MT64_N; i++)
- kit_mt64_generate(&state);
+ kit_mt64_rotate(&state);
n = kit_mt64_generate(&state);
time_sec = (uint64_t) t.tv_sec;
@@ -92,20 +90,27 @@ static void secure_random_fallback(ptrdiff_t const size,
}
void kit_secure_random(ptrdiff_t const size, void *const data) {
- if (size <= 0)
+ assert(size > 0);
+ assert(data != NULL);
+
+ if (size <= 0 || data == NULL)
return;
#if defined(_WIN32) && !defined(__CYGWIN__)
secure_random_fallback(size, data);
#else
FILE *f = fopen("/dev/urandom", "rb");
+ assert(f != NULL);
if (f == NULL) {
secure_random_fallback(size, data);
return;
}
- if (fread(data, 1, size, f) != size) {
+ size_t const n = fread(data, 1, size, f);
+ assert(n == size);
+
+ if (n != size) {
secure_random_fallback(size, data);
fclose(f);
return;