From: Loup Vaillant Date: Tue, 2 Jan 2018 23:10:04 +0000 (+0100) Subject: turned test utils into header only X-Git-Url: https://git.codecow.com/?a=commitdiff_plain;h=268fb93f8abd3a5e5924258891770a89badea6cf;p=Monocypher.git turned test utils into header only Simplifies the makefiles a bit --- diff --git a/makefile b/makefile index 7b4000d..76b973d 100644 --- a/makefile +++ b/makefile @@ -80,7 +80,6 @@ lib/monocypher.o lib/sha512.o: # Test & speed libraries $TEST_COMMON=tests/utils.h src/monocypher.h src/optional/sha512.h -lib/utils.o : tests/utils.c tests/utils.h lib/test.o : tests/test.c $(TEST_COMMON) tests/vectors.h lib/speed.o : tests/speed.c $(TEST_COMMON) tests/speed.h lib/speed-sodium.o: tests/speed-sodium.c $(TEST_COMMON) tests/speed.h @@ -89,12 +88,11 @@ lib/utils.o lib/test.o lib/speed.o lib/speed-sodium.o: $(CC) $(CFLAGS) -I src -I src/optional -fPIC -c -o $@ $< # test & speed executables -test.out : lib/test.o lib/utils.o lib/monocypher.o lib/sha512.o -speed.out : lib/speed.o lib/utils.o lib/monocypher.o lib/sha512.o -speed-sodium.out: lib/speed-sodium.o lib/utils.o +test.out : lib/test.o lib/monocypher.o lib/sha512.o +speed.out: lib/speed.o lib/monocypher.o lib/sha512.o test.out speed.out: $(CC) $(CFLAGS) -I src -I src/optional -o $@ $^ -speed-sodium.out: lib/speed-sodium.o lib/utils.o +speed-sodium.out: lib/speed-sodium.o $(CC) $(CFLAGS) -I src -I src/optional -o $@ $^ \ $$(pkg-config --cflags libsodium) \ $$(pkg-config --libs libsodium) diff --git a/tests/gen/makefile b/tests/gen/makefile index e1ad0e9..5ea6e48 100644 --- a/tests/gen/makefile +++ b/tests/gen/makefile @@ -29,13 +29,10 @@ clean: -I ../../src/optional \ $$(pkg-config --cflags libsodium) -%.out: %.o monocypher.o utils.o ed25519.o +%.out: %.o monocypher.o ed25519.o $(CC) $(CFLAGS) -o $@ $^ \ $$(pkg-config --libs libsodium) -utils.o: ../utils.c ../utils.h - $(CC) $(CFLAGS) -c $< -I .. -I ../../src - ed25519.o: ../ed25519-donna/ed25519.c $(wildcard ../ed25519-donna/*.h) $(CC) $(CFLAGS) -c $< \ -I ../../src \ diff --git a/tests/utils.c b/tests/utils.c deleted file mode 100644 index bedb2de..0000000 --- a/tests/utils.c +++ /dev/null @@ -1,59 +0,0 @@ -#include "utils.h" -#include "stdio.h" - -static void store64_le(u8 out[8], u64 in) -{ - out[0] = in & 0xff; - out[1] = (in >> 8) & 0xff; - out[2] = (in >> 16) & 0xff; - out[3] = (in >> 24) & 0xff; - out[4] = (in >> 32) & 0xff; - out[5] = (in >> 40) & 0xff; - out[6] = (in >> 48) & 0xff; - out[7] = (in >> 56) & 0xff; -} - -u64 load64_le(const u8 s[8]) -{ - return (u64)s[0] - | ((u64)s[1] << 8) - | ((u64)s[2] << 16) - | ((u64)s[3] << 24) - | ((u64)s[4] << 32) - | ((u64)s[5] << 40) - | ((u64)s[6] << 48) - | ((u64)s[7] << 56); -} - - -// Pseudo-random 64 bit number, based on xorshift* -u64 rand64() -{ - static u64 x = 12345; // Must be seeded with a nonzero value. - x ^= x >> 12; - x ^= x << 25; - x ^= x >> 27; - return x * 0x2545F4914F6CDD1D; // magic constant -} - -void p_random(u8 *stream, size_t size) -{ - FOR (i, 0, size) { - stream[i] = (u8)rand64(); - } -} - -void print_vector(u8 *buf, size_t size) -{ - FOR (i, 0, size) { - printf("%x%x", buf[i] >> 4, buf[i] & 0x0f); - } - printf(":\n"); -} - -void print_number(u64 n) -{ - u8 buf[8]; - store64_le(buf, n); - print_vector(buf, 8); -} diff --git a/tests/utils.h b/tests/utils.h index 701dbfa..9d20a42 100644 --- a/tests/utils.h +++ b/tests/utils.h @@ -3,6 +3,7 @@ #include #include +#include typedef int8_t i8; typedef uint8_t u8; @@ -19,10 +20,60 @@ typedef uint64_t u64; } #define RANDOM_INPUT(name, size) u8 name[size]; p_random(name, size) -u64 load64_le(const u8 s[8]); -void p_random(u8 *stream, size_t size); -u64 rand64(); -void print_vector(u8 *buf, size_t size); -void print_number(u64 n); +static void store64_le(u8 out[8], u64 in) +{ + out[0] = in & 0xff; + out[1] = (in >> 8) & 0xff; + out[2] = (in >> 16) & 0xff; + out[3] = (in >> 24) & 0xff; + out[4] = (in >> 32) & 0xff; + out[5] = (in >> 40) & 0xff; + out[6] = (in >> 48) & 0xff; + out[7] = (in >> 56) & 0xff; +} + +u64 load64_le(const u8 s[8]) +{ + return (u64)s[0] + | ((u64)s[1] << 8) + | ((u64)s[2] << 16) + | ((u64)s[3] << 24) + | ((u64)s[4] << 32) + | ((u64)s[5] << 40) + | ((u64)s[6] << 48) + | ((u64)s[7] << 56); +} + +// Pseudo-random 64 bit number, based on xorshift* +u64 rand64() +{ + static u64 x = 12345; // Must be seeded with a nonzero value. + x ^= x >> 12; + x ^= x << 25; + x ^= x >> 27; + return x * 0x2545F4914F6CDD1D; // magic constant +} + +void p_random(u8 *stream, size_t size) +{ + FOR (i, 0, size) { + stream[i] = (u8)rand64(); + } +} + +void print_vector(u8 *buf, size_t size) +{ + FOR (i, 0, size) { + printf("%x%x", buf[i] >> 4, buf[i] & 0x0f); + } + printf(":\n"); +} + +void print_number(u64 n) +{ + u8 buf[8]; + store64_le(buf, n); + print_vector(buf, 8); +} #endif // UTILS_H