Add automatic self test.

This commit is contained in:
XMRig 2017-04-21 17:29:03 +03:00
parent 8235ae0fa6
commit 361394be21
5 changed files with 54 additions and 40 deletions

View file

@ -26,6 +26,7 @@
#include <stddef.h>
#include <stdint.h>
#include <stdbool.h>
#define MEMORY 2097152 /* 2 MiB */
@ -38,7 +39,7 @@ struct cryptonight_ctx {
extern void (* const extra_hashes[4])(const void *, size_t, char *);
void cryptonight_init(int variant);
bool cryptonight_init(int variant);
void cryptonight_hash(void* output, const void* input, size_t input_len);
int scanhash_cryptonight(int thr_id, uint32_t *hash, uint32_t *restrict pdata, const uint32_t *restrict ptarget, uint32_t max_nonce, unsigned long *restrict hashes_done, struct cryptonight_ctx *restrict ctx);

View file

@ -25,7 +25,7 @@
#include <stdlib.h>
#ifndef BUILD_TEST
# include "xmrig.h"
# include "xmrig.h"
#endif
#include "crypto/c_groestl.h"
@ -34,6 +34,22 @@
#include "crypto/c_skein.h"
#include "cryptonight.h"
#include "options.h"
#include "utils/applog.h"
const static char test_input[76] = {
0x03, 0x05, 0xA0, 0xDB, 0xD6, 0xBF, 0x05, 0xCF, 0x16, 0xE5, 0x03, 0xF3, 0xA6, 0x6F, 0x78, 0x00,
0x7C, 0xBF, 0x34, 0x14, 0x43, 0x32, 0xEC, 0xBF, 0xC2, 0x2E, 0xD9, 0x5C, 0x87, 0x00, 0x38, 0x3B,
0x30, 0x9A, 0xCE, 0x19, 0x23, 0xA0, 0x96, 0x4B, 0x00, 0x00, 0x00, 0x08, 0xBA, 0x93, 0x9A, 0x62,
0x72, 0x4C, 0x0D, 0x75, 0x81, 0xFC, 0xE5, 0x76, 0x1E, 0x9D, 0x8A, 0x0E, 0x6A, 0x1C, 0x3F, 0x92,
0x4F, 0xDD, 0x84, 0x93, 0xD1, 0x11, 0x56, 0x49, 0xC0, 0x5E, 0xB6, 0x01
};
const static char test_output[32] = {
0x1A, 0x3F, 0xFB, 0xEE, 0x90, 0x9B, 0x42, 0x0D, 0x91, 0xF7, 0xBE, 0x6E, 0x5F, 0xB5, 0x6D, 0xB7,
0x1B, 0x31, 0x10, 0xD8, 0x86, 0x01, 0x1E, 0x87, 0x7E, 0xE5, 0x78, 0x6A, 0xFD, 0x08, 0x01, 0x00
};
void cryptonight_av1_aesni(void* output, const void* input, struct cryptonight_ctx* ctx);
@ -48,7 +64,22 @@ void cryptonight_av4_softaes(void* output, const void* input, struct cryptonight
void (*cryptonight_hash_ctx)(void* output, const void* input, struct cryptonight_ctx* ctx) = NULL;
void cryptonight_init(int variant)
static bool self_test() {
char output[32];
struct cryptonight_ctx *ctx = (struct cryptonight_ctx*) malloc(sizeof(struct cryptonight_ctx));
ctx->memory = (uint8_t *) malloc(MEMORY);
cryptonight_hash_ctx(output, test_input, ctx);
free(ctx->memory);
free(ctx);
return memcmp(output, test_output, 32) == 0;
}
bool cryptonight_init(int variant)
{
switch (variant) {
case XMR_AV1_AESNI:
@ -77,6 +108,7 @@ void cryptonight_init(int variant)
break;
}
return self_test();
}