Make it compile and run (fairly quick) on Apple M1/M2.

This commit is contained in:
User
2023-11-21 23:39:05 +00:00
parent 4f42fb3447
commit e465d099c7
4 changed files with 34 additions and 17 deletions
+4 -3
View File
@@ -1,11 +1,12 @@
#include <math.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
int main(void) int main(void)
{ {
printf("RAND_MAX = %d\n", RAND_MAX); printf("RAND_MAX = %d (%.1lf bit)\n", RAND_MAX, log2(RAND_MAX));
for (uint64_t i = 0; i < 1000000000; ++i) const uint64_t rounds = 1000000000ull * 8 / (unsigned)log2(RAND_MAX);
{ for (uint64_t i = rounds; i-->0;) {
rand(); rand();
} }
} }
+2 -2
View File
@@ -1,3 +1,3 @@
from _version import __version__ # from _version import __version__
__version__ # Public API # __version__ # Public API
+7 -3
View File
@@ -1,3 +1,4 @@
import sys
from pathlib import Path from pathlib import Path
from typing import Any from typing import Any
@@ -24,9 +25,12 @@ ffi.cdef(
int cha_update(cha_ctx* ctx, uint8_t* out, uint64_t outlen); int cha_update(cha_ctx* ctx, uint8_t* out, uint64_t outlen);
""" """
) )
lib = ffi.dlopen( libname = "librandquik-chacha20.so"
(Path(__file__).parent.parent / "build/librandquik-chacha20.so").as_posix() 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): def _processKeys(key, iv):
+21 -9
View File
@@ -1,10 +1,25 @@
#include "chacha20.h"
#if defined(__x86_64__)
#ifdef __GNUC__ #ifdef __GNUC__
#pragma GCC target("sse2") #pragma GCC target("sse2")
#pragma GCC target("ssse3") #pragma GCC target("ssse3")
#pragma GCC target("avx2") #pragma GCC target("avx2")
#endif #endif
#include "chacha20.h" #include <emmintrin.h> // SSE2
#include "cha4block.h"
#include <immintrin.h> // AVX2
#include <tmmintrin.h> // SSSE3
#include "cha8block.h"
#elif defined(__aarch64__)
#include "sse2neon.h"
#include "cha4block.h"
#endif
#include "cha1block.h"
#include <assert.h> #include <assert.h>
#include <stdbool.h> #include <stdbool.h>
@@ -12,17 +27,10 @@
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <emmintrin.h>
#include <immintrin.h>
#include <pthread.h> #include <pthread.h>
#include <time.h> #include <time.h>
#include <tmmintrin.h>
#include <unistd.h> #include <unistd.h>
#include "cha1block.h"
#include "cha4block.h"
#include "cha8block.h"
void cha_init(cha_ctx* ctx, const uint8_t* key, const uint8_t* iv) { void cha_init(cha_ctx* ctx, const uint8_t* key, const uint8_t* iv) {
ctx->state[0] = 0x61707865; ctx->state[0] = 0x61707865;
ctx->state[1] = 0x3320646e; ctx->state[1] = 0x3320646e;
@@ -43,7 +51,7 @@ int cha_update(cha_ctx* ctx, uint8_t* out, uint64_t outlen) {
if (ctx->uncount) { if (ctx->uncount) {
// Deliver stored bytes first // Deliver stored bytes first
uint64_t N = ctx->uncount >= outlen ? outlen : ctx->uncount; 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); memcpy(c, ctx->unconsumed, N);
ctx->uncount -= N; ctx->uncount -= N;
c += N; c += N;
@@ -53,6 +61,7 @@ int cha_update(cha_ctx* ctx, uint8_t* out, uint64_t outlen) {
if (c == out + outlen) if (c == out + outlen)
return 0; return 0;
} }
#if defined(__x86_64__)
// TODO: Handle resume if we are not at block boundary // TODO: Handle resume if we are not at block boundary
if (__builtin_cpu_supports("ssse3")) { if (__builtin_cpu_supports("ssse3")) {
if (__builtin_cpu_supports("avx2")) { 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); c += _cha_4block(ctx, c, end);
assert(end - c < 256); assert(end - c < 256);
} }
#elif defined(__aarch64__)
c += _cha_4block(ctx, c, end);
#endif
c += _cha_block(ctx, c, end); c += _cha_block(ctx, c, end);
assert(c == end); assert(c == end);
return 0; return 0;