diff --git a/benchmarks/crand.c b/benchmarks/crand.c index 52b94ae..591f959 100644 --- a/benchmarks/crand.c +++ b/benchmarks/crand.c @@ -1,11 +1,12 @@ +#include #include #include int main(void) { - printf("RAND_MAX = %d\n", RAND_MAX); - for (uint64_t i = 0; i < 1000000000; ++i) - { + printf("RAND_MAX = %d (%.1lf bit)\n", RAND_MAX, log2(RAND_MAX)); + const uint64_t rounds = 1000000000ull * 8 / (unsigned)log2(RAND_MAX); + for (uint64_t i = rounds; i-->0;) { rand(); } } diff --git a/randquik/__init__.py b/randquik/__init__.py index 0e240c9..52d339b 100644 --- a/randquik/__init__.py +++ b/randquik/__init__.py @@ -1,3 +1,3 @@ -from _version import __version__ +# from _version import __version__ -__version__ # Public API +# __version__ # Public API diff --git a/randquik/cha.py b/randquik/cha.py index 430682e..1e1216e 100644 --- a/randquik/cha.py +++ b/randquik/cha.py @@ -1,3 +1,4 @@ +import sys from pathlib import Path from typing import Any @@ -24,9 +25,12 @@ ffi.cdef( int cha_update(cha_ctx* ctx, uint8_t* out, uint64_t outlen); """ ) -lib = ffi.dlopen( - (Path(__file__).parent.parent / "build/librandquik-chacha20.so").as_posix() -) +libname = "librandquik-chacha20.so" +if sys.platform == "darwin": + libname = "librandquik-chacha20.dylib" +elif sys.platform == "win32": + libname = "randquik-chacha20.dll" +lib = ffi.dlopen((Path(__file__).parent.parent / f"build/{libname}").as_posix()) def _processKeys(key, iv): diff --git a/src/chacha20.c b/src/chacha20.c index 6ab5fa6..4b50d39 100644 --- a/src/chacha20.c +++ b/src/chacha20.c @@ -1,10 +1,25 @@ +#include "chacha20.h" + +#if defined(__x86_64__) #ifdef __GNUC__ #pragma GCC target("sse2") #pragma GCC target("ssse3") #pragma GCC target("avx2") #endif -#include "chacha20.h" +#include // SSE2 +#include "cha4block.h" + +#include // AVX2 +#include // SSSE3 +#include "cha8block.h" + +#elif defined(__aarch64__) +#include "sse2neon.h" +#include "cha4block.h" +#endif + +#include "cha1block.h" #include #include @@ -12,17 +27,10 @@ #include #include -#include -#include #include #include -#include #include -#include "cha1block.h" -#include "cha4block.h" -#include "cha8block.h" - void cha_init(cha_ctx* ctx, const uint8_t* key, const uint8_t* iv) { ctx->state[0] = 0x61707865; ctx->state[1] = 0x3320646e; @@ -43,7 +51,7 @@ int cha_update(cha_ctx* ctx, uint8_t* out, uint64_t outlen) { if (ctx->uncount) { // Deliver stored bytes first uint64_t N = ctx->uncount >= outlen ? outlen : ctx->uncount; - fprintf(stderr, "%lu, %i, %lu\n", N, ctx->uncount, outlen); + fprintf(stderr, "%llu, %i, %llu\n", N, ctx->uncount, outlen); memcpy(c, ctx->unconsumed, N); ctx->uncount -= N; c += N; @@ -53,6 +61,7 @@ int cha_update(cha_ctx* ctx, uint8_t* out, uint64_t outlen) { if (c == out + outlen) return 0; } +#if defined(__x86_64__) // TODO: Handle resume if we are not at block boundary if (__builtin_cpu_supports("ssse3")) { if (__builtin_cpu_supports("avx2")) { @@ -62,6 +71,9 @@ int cha_update(cha_ctx* ctx, uint8_t* out, uint64_t outlen) { c += _cha_4block(ctx, c, end); assert(end - c < 256); } +#elif defined(__aarch64__) + c += _cha_4block(ctx, c, end); +#endif c += _cha_block(ctx, c, end); assert(c == end); return 0;