2 #define TEST_NAME "randombytes"
5 static unsigned char x
[65536];
6 static unsigned long long freq
[256];
8 static int compat_tests(void)
12 memset(x
, 0, sizeof x
);
13 randombytes(x
, sizeof x
);
14 for (i
= 0; i
< 256; ++i
) {
17 for (i
= 0; i
< sizeof x
; ++i
) {
18 ++freq
[255 & (int)x
[i
]];
20 for (i
= 0; i
< 256; ++i
) {
22 printf("nacl_tests failed\n");
28 static int randombytes_tests(void)
35 assert(strcmp(randombytes_implementation_name(), "sysrandom"));
37 assert(strcmp(randombytes_implementation_name(), "js"));
41 n
= randombytes_random();
43 f
|= ((n
>> 16) > 1) << 1;
44 f
|= ((n
>> 8) > 1) << 2;
46 f
|= (n
> 0x7fffffff) << 4;
50 for (i
= 0; i
< 256; ++i
) {
53 for (i
= 0; i
< 65536; ++i
) {
54 ++freq
[randombytes_uniform(256)];
56 for (i
= 0; i
< 256; ++i
) {
58 printf("randombytes_uniform() test failed\n");
61 assert(randombytes_uniform(1U) == 0U);
63 #ifndef __EMSCRIPTEN__
64 randombytes_set_implementation(&randombytes_salsa20_implementation
);
65 assert(strcmp(randombytes_implementation_name(), "salsa20") == 0);
68 for (i
= 0; i
< 256; ++i
) {
71 for (i
= 0; i
< 65536; ++i
) {
72 ++freq
[randombytes_uniform(256)];
74 for (i
= 0; i
< 256; ++i
) {
76 printf("randombytes_uniform() test failed\n");
79 memset(x
, 0, sizeof x
);
80 randombytes_buf(x
, sizeof x
);
81 for (i
= 0; i
< 256; ++i
) {
84 for (i
= 0; i
< sizeof x
; ++i
) {
85 ++freq
[255 & (int)x
[i
]];
87 for (i
= 0; i
< 256; ++i
) {
89 printf("randombytes_buf() test failed\n");
92 assert(randombytes_uniform(1U) == 0U);
101 static uint32_t randombytes_uniform_impl(const uint32_t upper_bound
)
106 static int impl_tests(void)
108 #ifndef __native_client__
109 randombytes_implementation impl
= randombytes_sysrandom_implementation
;
111 randombytes_implementation impl
= randombytes_nativeclient_implementation
;
113 uint32_t v
= randombytes_random();
115 impl
.uniform
= randombytes_uniform_impl
;
117 randombytes_set_implementation(&impl
);
118 assert(randombytes_uniform(v
) == v
);
119 assert(randombytes_uniform(v
) == v
);
120 assert(randombytes_uniform(v
) == v
);
121 assert(randombytes_uniform(v
) == v
);
133 #ifndef __EMSCRIPTEN__