aboutsummaryrefslogtreecommitdiff
path: root/src/proone-rnd.c
blob: 215f930ffca2b3d6a71a554bfcf6aaa5b84265dd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include <stdio.h>
#include <inttypes.h>

#include "rnd.h"
#include "util_rt.h"

#include <mbedtls/ctr_drbg.h>
#include <mbedtls/entropy.h>


int main (const int argc, const char **args) {
	int ret = 0;
	uint32_t max, cnt, n, empty_cnt;
	uint32_t *arr = NULL;
	size_t graph[20], g_max, sn;
	mbedtls_ctr_drbg_context ctr_drbg;
	mbedtls_entropy_context entropy;
	prne_rnd_t rnd;

	prne_memzero(graph, sizeof(graph));
	mbedtls_ctr_drbg_init(&ctr_drbg);
	mbedtls_entropy_init(&entropy);
	prne_init_rnd(&rnd);

	if (argc < 3) {
		fprintf(stderr, "Usage: %s <max> <count>\n", args[0]);
		ret = 2;
		goto END;
	}
	if (sscanf(args[1], "%"SCNu32, &max) != 1 || max == 0) {
		fprintf(stderr, "Invalid <max>\n");
		ret = 2;
		goto END;
	}
	if (sscanf(args[2], "%"SCNu32, &cnt) != 1) {
		fprintf(stderr, "Invalid <count>\n");
		ret = 2;
		goto END;
	}

	prne_assert(mbedtls_ctr_drbg_seed(
		&ctr_drbg,
		mbedtls_entropy_func,
		&entropy,
		NULL,
		0) == 0);
	{
		uint8_t is[PRNE_RND_WELL512_SEEDLEN];

		prne_assert(mbedtls_ctr_drbg_random(&ctr_drbg, is, sizeof(is)) == 0);
		prne_assert(prne_rnd_alloc_well512(&rnd, is));
	}

	arr = prne_calloc(sizeof(uint32_t), max);
	for (uint32_t i = 0; i < cnt; i += 1) {
#if 1
		prne_assert(prne_rnd(&rnd, (uint8_t*)&n, sizeof(n)));
#else
		prne_assert(mbedtls_ctr_drbg_random(
			&ctr_drbg,
			(uint8_t*)&n,
			sizeof(n)) == 0);
#endif
		n = n % max;
		arr[n] += 1;
		graph[n * 20 / max] += 1;
	}

	empty_cnt = 0;
	for (size_t i = 0; i < max; i += 1) {
		if (arr[i] == 0) {
			empty_cnt += 1;
		}
	}

	g_max = 0;
	for (size_t i = 0; i < 20; i += 1) {
		if (graph[i] > g_max) {
			g_max = graph[i];
		}
	}

	for (size_t y = 0; y < 20; y += 1) {
		sn = graph[y] * 75 / g_max;
		printf("%2zu: ", y + 1);
		for (size_t x = 0; x < sn; x += 1) {
			printf("=");
		}
		printf("\n");
	}
	printf("Empty: %"PRIu32"\n", empty_cnt);

END:
	mbedtls_ctr_drbg_free(&ctr_drbg);
	mbedtls_entropy_free(&entropy);
	prne_free_rnd(&rnd);
	prne_free(arr);

	return ret;
}