mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-07-04 10:18:31 +00:00
Make numerous improvements
- Python static hello world now 1.8mb - Python static fully loaded now 10mb - Python HTTPS client now uses MbedTLS - Python REPL now completes import stmts - Increase stack size for Python for now - Begin synthesizing posixpath and ntpath - Restore Python \N{UNICODE NAME} support - Restore Python NFKD symbol normalization - Add optimized code path for Intel SHA-NI - Get more Python unit tests passing faster - Get Python help() pagination working on NT - Python hashlib now supports MbedTLS PBKDF2 - Make memcpy/memmove/memcmp/bcmp/etc. faster - Add Mersenne Twister and Vigna to LIBC_RAND - Provide privileged __printf() for error code - Fix zipos opendir() so that it reports ENOTDIR - Add basic chmod() implementation for Windows NT - Add Cosmo's best functions to Python cosmo module - Pin function trace indent depth to that of caller - Show memory diagram on invalid access in MODE=dbg - Differentiate stack overflow on crash in MODE=dbg - Add stb_truetype and tools for analyzing font files - Upgrade to UNICODE 13 and reduce its binary footprint - COMPILE.COM now logs resource usage of build commands - Start implementing basic poll() support on bare metal - Set getauxval(AT_EXECFN) to GetModuleFileName() on NT - Add descriptions to strerror() in non-TINY build modes - Add COUNTBRANCH() macro to help with micro-optimizations - Make error / backtrace / asan / memory code more unbreakable - Add fast perfect C implementation of μ-Law and a-Law audio codecs - Make strtol() functions consistent with other libc implementations - Improve Linenoise implementation (see also github.com/jart/bestline) - COMPILE.COM now suppresses stdout/stderr of successful build commands
This commit is contained in:
parent
fa7b4f5bd1
commit
39bf41f4eb
806 changed files with 77494 additions and 63859 deletions
|
@ -184,22 +184,6 @@ uint64_t Rand64LowByte(void) {
|
|||
return x;
|
||||
}
|
||||
|
||||
uint64_t GetRandomNoRdrrnd(void) {
|
||||
uint64_t x;
|
||||
ASSERT_EQ(8, getrandom(&x, 8, GRND_NORDRND));
|
||||
return x;
|
||||
}
|
||||
|
||||
uint64_t GetRandomNoSystem(void) {
|
||||
uint64_t x;
|
||||
if (X86_HAVE(RDRND) || X86_HAVE(RDSEED)) {
|
||||
ASSERT_EQ(8, getrandom(&x, 8, GRND_NOSYSTEM));
|
||||
} else {
|
||||
ASSERT_EQ(8, getrandom(&x, 8, 0));
|
||||
}
|
||||
return x;
|
||||
}
|
||||
|
||||
uint64_t GetRandom(void) {
|
||||
uint64_t x;
|
||||
ASSERT_EQ(8, getrandom(&x, 8, 0));
|
||||
|
@ -220,8 +204,6 @@ static const struct RandomFunction {
|
|||
{"KnuthLcg", KnuthLcg, false}, //
|
||||
{"rand64", rand64, true}, //
|
||||
{"Rand64LowByte", Rand64LowByte, true}, //
|
||||
{"GetRandomNoRdrrnd", GetRandomNoRdrrnd, true}, //
|
||||
{"GetRandomNoSystem", GetRandomNoSystem, true}, //
|
||||
{"GetRandom", GetRandom, true}, //
|
||||
};
|
||||
|
||||
|
|
|
@ -18,9 +18,11 @@
|
|||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/math.h"
|
||||
#include "libc/rand/rand.h"
|
||||
#include "libc/runtime/gc.internal.h"
|
||||
#include "libc/testlib/ezbench.h"
|
||||
#include "libc/testlib/hyperion.h"
|
||||
#include "libc/testlib/testlib.h"
|
||||
#include "libc/x/x.h"
|
||||
|
||||
TEST(MeasureEntropy, test) {
|
||||
MeasureEntropy(kMoby, 1000);
|
||||
|
|
212
test/libc/rand/mt19937_test.c
Normal file
212
test/libc/rand/mt19937_test.c
Normal file
|
@ -0,0 +1,212 @@
|
|||
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
|
||||
│vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│
|
||||
╞══════════════════════════════════════════════════════════════════════════════╡
|
||||
│ Copyright 2021 Justine Alexandra Roberts Tunney │
|
||||
│ │
|
||||
│ Permission to use, copy, modify, and/or distribute this software for │
|
||||
│ any purpose with or without fee is hereby granted, provided that the │
|
||||
│ above copyright notice and this permission notice appear in all copies. │
|
||||
│ │
|
||||
│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │
|
||||
│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │
|
||||
│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │
|
||||
│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │
|
||||
│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │
|
||||
│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │
|
||||
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
||||
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/bits/bits.h"
|
||||
#include "libc/log/check.h"
|
||||
#include "libc/macros.internal.h"
|
||||
#include "libc/mem/mem.h"
|
||||
#include "libc/rand/rand.h"
|
||||
#include "libc/runtime/gc.internal.h"
|
||||
#include "libc/runtime/runtime.h"
|
||||
#include "libc/stdio/stdio.h"
|
||||
#include "libc/sysv/consts/grnd.h"
|
||||
#include "libc/testlib/ezbench.h"
|
||||
#include "libc/testlib/testlib.h"
|
||||
#include "third_party/mbedtls/ctr_drbg.h"
|
||||
|
||||
void GetRandom(void *p, size_t n) {
|
||||
ssize_t rc;
|
||||
size_t i, m;
|
||||
for (i = 0; i < n; i += rc) {
|
||||
m = MIN(n - i, 256);
|
||||
rc = getrandom((char *)p + i, m, 0);
|
||||
if (rc == -1 && errno == EINTR) continue;
|
||||
if (rc <= 0) abort();
|
||||
}
|
||||
}
|
||||
|
||||
int GetEntropy1(void *c, unsigned char *p, size_t n) {
|
||||
rngset(p, n, rdseed, 0);
|
||||
return 0;
|
||||
}
|
||||
|
||||
void ctrdrbg1(void *p, size_t n) {
|
||||
int rc;
|
||||
size_t i, m;
|
||||
mbedtls_ctr_drbg_context rng;
|
||||
mbedtls_ctr_drbg_init(&rng);
|
||||
DCHECK_EQ(0, mbedtls_ctr_drbg_seed(&rng, GetEntropy1, 0, "justine", 7));
|
||||
for (i = 0; i < n; i += m) {
|
||||
m = MIN(n - i, MBEDTLS_CTR_DRBG_MAX_REQUEST);
|
||||
DCHECK_EQ(0, mbedtls_ctr_drbg_random(&rng, (unsigned char *)p + i, m));
|
||||
}
|
||||
mbedtls_ctr_drbg_free(&rng);
|
||||
}
|
||||
|
||||
int GetEntropy2(void *c, unsigned char *p, size_t n) {
|
||||
rngset(p, n, rdseed, -1);
|
||||
return 0;
|
||||
}
|
||||
|
||||
void ctrdrbg2(void *p, size_t n) {
|
||||
int rc;
|
||||
size_t i, m;
|
||||
mbedtls_ctr_drbg_context rng;
|
||||
mbedtls_ctr_drbg_init(&rng);
|
||||
DCHECK_EQ(0, mbedtls_ctr_drbg_seed(&rng, GetEntropy2, 0, "justine", 7));
|
||||
for (i = 0; i < n; i += m) {
|
||||
m = MIN(n - i, MBEDTLS_CTR_DRBG_MAX_REQUEST);
|
||||
DCHECK_EQ(0, mbedtls_ctr_drbg_random(&rng, (unsigned char *)p + i, m));
|
||||
}
|
||||
mbedtls_ctr_drbg_free(&rng);
|
||||
}
|
||||
|
||||
noinline uint64_t xorshift(void) {
|
||||
static uint64_t s = 88172645463325252;
|
||||
uint64_t x = s;
|
||||
x ^= x << 13;
|
||||
x ^= x >> 7;
|
||||
x ^= x << 17;
|
||||
return (s = x);
|
||||
}
|
||||
|
||||
noinline void xorshifta(char *p, size_t n) {
|
||||
static uint64_t s = 88172645463325252;
|
||||
uint64_t x = s;
|
||||
while (n >= 8) {
|
||||
x ^= x << 13;
|
||||
x ^= x >> 7;
|
||||
x ^= x << 17;
|
||||
__builtin_memcpy(p, &x, 8);
|
||||
n -= 8;
|
||||
p += 8;
|
||||
}
|
||||
s = x;
|
||||
while (n--) {
|
||||
*p++ = x;
|
||||
x >>= 8;
|
||||
}
|
||||
}
|
||||
|
||||
noinline uint64_t knuth(void) {
|
||||
uint64_t a, b;
|
||||
static uint64_t x = 1;
|
||||
x *= 6364136223846793005;
|
||||
x += 1442695040888963407;
|
||||
a = x >> 32;
|
||||
x *= 6364136223846793005;
|
||||
x += 1442695040888963407;
|
||||
b = x >> 32;
|
||||
return a | b << 32;
|
||||
}
|
||||
|
||||
noinline void knutha(char *p, size_t n) {
|
||||
static uint64_t s = 1;
|
||||
uint32_t u;
|
||||
uint64_t x = s;
|
||||
while (n >= 4) {
|
||||
x *= 6364136223846793005;
|
||||
x += 1442695040888963407;
|
||||
u = x >> 32;
|
||||
p[0] = (0x000000FF & u) >> 000;
|
||||
p[1] = (0x0000FF00 & u) >> 010;
|
||||
p[2] = (0x00FF0000 & u) >> 020;
|
||||
p[3] = (0xFF000000 & u) >> 030;
|
||||
n -= 4;
|
||||
p += 4;
|
||||
}
|
||||
s = x;
|
||||
while (n--) {
|
||||
*p++ = x;
|
||||
x >>= 8;
|
||||
}
|
||||
}
|
||||
|
||||
uint64_t urandom(void) {
|
||||
return random();
|
||||
}
|
||||
|
||||
TEST(mt19937, test) {
|
||||
uint64_t init[] = {0x12345ULL, 0x23456ULL, 0x34567ULL, 0x45678ULL};
|
||||
uint64_t want[] = {
|
||||
7266447313870364031ull, 4946485549665804864ull, 16945909448695747420ull,
|
||||
16394063075524226720ull, 4873882236456199058ull,
|
||||
};
|
||||
_Smt19937(init, ARRAYLEN(init));
|
||||
for (int i = 0; i < ARRAYLEN(want); i++) {
|
||||
ASSERT_EQ(want[i], _mt19937());
|
||||
}
|
||||
}
|
||||
|
||||
BENCH(mt19937, bench8) {
|
||||
volatile uint64_t x;
|
||||
EZBENCH2("vigna", donothing, x = vigna());
|
||||
EZBENCH2("vigna_r", donothing, vigna_r(&x));
|
||||
EZBENCH2("xorshift", donothing, x = xorshift());
|
||||
EZBENCH2("knuth", donothing, x = knuth());
|
||||
EZBENCH2("random", donothing, x = urandom());
|
||||
EZBENCH2("mt19937", donothing, x = _mt19937());
|
||||
EZBENCH2("rand64char", donothing, x = rand64());
|
||||
size_t i = 0;
|
||||
volatile uint8_t *p = gc(malloc(3 * 2048 * 2 * 8));
|
||||
EZBENCH3("rdrand", 2048, donothing, p[i++] = rdrand());
|
||||
EZBENCH3("rdseed", 2048, donothing, p[i++] = rdseed());
|
||||
EZBENCH3("getrandom", 2048, donothing, GetRandom(p + i++, 8));
|
||||
}
|
||||
|
||||
BENCH(mt19937, bench32k) {
|
||||
volatile char *p = gc(malloc(32768));
|
||||
EZBENCH_N("rngset(rand64,-1)", 32768, rngset(p, 32768, rand64, -1));
|
||||
EZBENCH_N("rngset(rdseed,512)", 32768, rngset(p, 32768, rdseed, 512));
|
||||
EZBENCH_N("ctrdrbg+rdseed [blk]", 32768, ctrdrbg1(p, 32768));
|
||||
EZBENCH_N("getrandom [block]", 32768, GetRandom(p, 32768));
|
||||
EZBENCH_N("vigna [word]", 32768, rngset(p, 32768, vigna, 0));
|
||||
EZBENCH_N("xorshift [word]", 32768, xorshifta(p, 32768));
|
||||
EZBENCH_N("knuth [word]", 32768, knutha(p, 32768));
|
||||
EZBENCH_N("random [word]", 32768, rngset(p, 32768, urandom, 0));
|
||||
EZBENCH_N("mt19937 [word]", 32768, rngset(p, 32768, _mt19937, 0));
|
||||
EZBENCH_N("rand64 [word]", 32768, rngset(p, 32768, rand64, 0));
|
||||
EZBENCH_N("rdrand [word]", 32768, rngset(p, 32768, rdrand, 0));
|
||||
EZBENCH_N("rdseed [word]", 32768, rngset(p, 32768, rdseed, 0));
|
||||
}
|
||||
|
||||
BENCH(mt19937, bench48) {
|
||||
volatile char *p = gc(malloc(48));
|
||||
EZBENCH_N("rngset(rdrand,0)", 48, rngset(p, 48, rdrand, 0));
|
||||
EZBENCH_N("rngset(rdseed,0)", 48, rngset(p, 48, rdseed, 0));
|
||||
EZBENCH_N("getrandom", 48, GetRandom(p, 48));
|
||||
}
|
||||
|
||||
#if 0
|
||||
TEST(mt19937, test) {
|
||||
int i;
|
||||
uint64_t init[4] = {0x12345ULL, 0x23456ULL, 0x34567ULL, 0x45678ULL};
|
||||
uint64_t length = 4;
|
||||
mt19937_init_by_array64(init, length);
|
||||
printf("1000 outputs of genrand64_int64()\n");
|
||||
for (i = 0; i < 1000; i++) {
|
||||
printf("%20llu ", mt19937_genrand64_int64());
|
||||
if (i % 5 == 4) printf("\n");
|
||||
}
|
||||
printf("\n1000 outputs of genrand64_real2()\n");
|
||||
for (i = 0; i < 1000; i++) {
|
||||
printf("%10.8f ", mt19937_genrand64_real2());
|
||||
if (i % 5 == 4) printf("\n");
|
||||
}
|
||||
}
|
||||
#endif
|
67
test/libc/rand/rngset_test.c
Normal file
67
test/libc/rand/rngset_test.c
Normal file
|
@ -0,0 +1,67 @@
|
|||
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
|
||||
│vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│
|
||||
╞══════════════════════════════════════════════════════════════════════════════╡
|
||||
│ Copyright 2021 Justine Alexandra Roberts Tunney │
|
||||
│ │
|
||||
│ Permission to use, copy, modify, and/or distribute this software for │
|
||||
│ any purpose with or without fee is hereby granted, provided that the │
|
||||
│ above copyright notice and this permission notice appear in all copies. │
|
||||
│ │
|
||||
│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │
|
||||
│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │
|
||||
│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │
|
||||
│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │
|
||||
│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │
|
||||
│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │
|
||||
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
||||
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/bits/bits.h"
|
||||
#include "libc/rand/rand.h"
|
||||
#include "libc/testlib/testlib.h"
|
||||
|
||||
uint64_t counter(void) {
|
||||
static uint8_t t;
|
||||
return 0x0101010101010101ull * t++;
|
||||
}
|
||||
|
||||
TEST(rngset, testZeroReseedInterval_doesntApplyPrng) {
|
||||
char buf[32];
|
||||
EXPECT_EQ(buf, rngset(buf, sizeof(buf), counter, 0));
|
||||
EXPECT_EQ(0x0000000000000000, READ64LE(buf + 0));
|
||||
EXPECT_EQ(0x0101010101010101, READ64LE(buf + 8));
|
||||
EXPECT_EQ(0x0202020202020202, READ64LE(buf + 16));
|
||||
EXPECT_EQ(0x0303030303030303, READ64LE(buf + 24));
|
||||
}
|
||||
|
||||
uint64_t eleet(void) {
|
||||
return 0x31337;
|
||||
}
|
||||
|
||||
TEST(rngset, testReseedIsNeg_usesInternalVignaPrng) {
|
||||
char buf[32];
|
||||
svigna(0x31337);
|
||||
EXPECT_EQ(buf, rngset(buf, sizeof(buf), eleet, -1));
|
||||
EXPECT_EQ(vigna(), READ64LE(buf + 0));
|
||||
EXPECT_EQ(vigna(), READ64LE(buf + 8));
|
||||
EXPECT_EQ(vigna(), READ64LE(buf + 16));
|
||||
EXPECT_EQ(vigna(), READ64LE(buf + 24));
|
||||
}
|
||||
|
||||
TEST(rngset, testNullSeedFunction_reseedBecomesVignaSeed) {
|
||||
char buf[32];
|
||||
svigna(123);
|
||||
EXPECT_EQ(buf, rngset(buf, sizeof(buf), 0, 123));
|
||||
EXPECT_EQ(vigna(), READ64LE(buf + 0));
|
||||
EXPECT_EQ(vigna(), READ64LE(buf + 8));
|
||||
EXPECT_EQ(vigna(), READ64LE(buf + 16));
|
||||
EXPECT_EQ(vigna(), READ64LE(buf + 24));
|
||||
}
|
||||
|
||||
TEST(rngset, testWeirdlyShaped_doesntCrash) {
|
||||
char buf[7];
|
||||
rngset(buf, sizeof(buf), 0, 0);
|
||||
rngset(buf, sizeof(buf), vigna, 0);
|
||||
rngset(buf, sizeof(buf), vigna, 9);
|
||||
rngset(buf, sizeof(buf), vigna, 8);
|
||||
}
|
|
@ -29,12 +29,14 @@ TEST_LIBC_RAND_DIRECTDEPS = \
|
|||
LIBC_STDIO \
|
||||
LIBC_STR \
|
||||
LIBC_STUBS \
|
||||
LIBC_CALLS \
|
||||
LIBC_LOG \
|
||||
LIBC_SYSV \
|
||||
LIBC_TESTLIB \
|
||||
LIBC_UNICODE \
|
||||
LIBC_X \
|
||||
THIRD_PARTY_GDTOA
|
||||
THIRD_PARTY_GDTOA \
|
||||
THIRD_PARTY_MBEDTLS
|
||||
|
||||
TEST_LIBC_RAND_DEPS := \
|
||||
$(call uniq,$(foreach x,$(TEST_LIBC_RAND_DIRECTDEPS),$($(x))))
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue