Tests: Add blake2b Test Cases for hash testing.

* Adding blake2b test function to src/tests/hash
	* New files for testvector.
	* Adding the test to CMakeLists.txt
	* Adding brief documentation for the test.
This commit is contained in:
0xFFFC0000 2023-06-12 08:29:35 +03:30
parent 94e67bf96b
commit 6bae91304e
4 changed files with 1112 additions and 2 deletions

View File

@ -108,6 +108,12 @@ ctest
To run the same tests on a release build, replace `debug` with `release`. To run the same tests on a release build, replace `debug` with `release`.
To run specific hash test, you can use `ctest` `-R` parameter. For exmaple to run only `blake2b` hash tests:
```
ctest -R hash-blake2b
```
# Libwallet API tests # Libwallet API tests
[TODO] [TODO]
@ -155,3 +161,4 @@ When writing new tests, please implement all functions in `.cpp` or `.c` files,
## Writing fuzz tests ## Writing fuzz tests
[TODO] [TODO]
hash

View File

@ -43,7 +43,7 @@ set_property(TARGET hash-tests
PROPERTY PROPERTY
FOLDER "tests") FOLDER "tests")
foreach (hash IN ITEMS fast slow slow-1 slow-2 slow-4 tree extra-blake extra-groestl extra-jh extra-skein) foreach (hash IN ITEMS fast slow slow-1 slow-2 slow-4 tree extra-blake extra-groestl extra-jh extra-skein blake2b)
add_test( add_test(
NAME "hash-${hash}" NAME "hash-${hash}"
COMMAND hash-tests "${hash}" "${CMAKE_CURRENT_SOURCE_DIR}/tests-${hash}.txt") COMMAND hash-tests "${hash}" "${CMAKE_CURRENT_SOURCE_DIR}/tests-${hash}.txt")

View File

@ -28,6 +28,8 @@
// //
// Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers // Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers
#include <algorithm>
#include <cassert>
#include <cstddef> #include <cstddef>
#include <fstream> #include <fstream>
#include <iomanip> #include <iomanip>
@ -35,10 +37,13 @@
#include <string> #include <string>
#include <cfenv> #include <cfenv>
#include <boost/algorithm/hex.hpp>
#include "misc_log_ex.h" #include "misc_log_ex.h"
#include "warnings.h" #include "warnings.h"
#include "crypto/hash.h" #include "crypto/hash.h"
#include "crypto/variant2_int_sqrt.h" #include "crypto/variant2_int_sqrt.h"
#include "randomx/src/blake2/blake2.h"
#include "../io.h" #include "../io.h"
using namespace std; using namespace std;
@ -74,6 +79,10 @@ extern "C" {
const V4_Data* p = reinterpret_cast<const V4_Data*>(data); const V4_Data* p = reinterpret_cast<const V4_Data*>(data);
return cn_slow_hash(p->data, p->length, hash, 4/*variant*/, 0/*prehashed*/, p->height); return cn_slow_hash(p->data, p->length, hash, 4/*variant*/, 0/*prehashed*/, p->height);
} }
static void hash_blake2b(const void *data, size_t length, char *hash_out){
// data = key[BLAKE2B_KEYBYTES] || hash data[HASH_DATA_LEN]
return (void) blake2b(hash_out, BLAKE2B_OUTBYTES, (char*) data + BLAKE2B_KEYBYTES, length, data, BLAKE2B_KEYBYTES);
}
} }
POP_WARNINGS POP_WARNINGS
@ -84,7 +93,7 @@ struct hash_func {
} hashes[] = {{"fast", cn_fast_hash}, {"slow", cn_slow_hash_0}, {"tree", hash_tree}, } hashes[] = {{"fast", cn_fast_hash}, {"slow", cn_slow_hash_0}, {"tree", hash_tree},
{"extra-blake", hash_extra_blake}, {"extra-groestl", hash_extra_groestl}, {"extra-blake", hash_extra_blake}, {"extra-groestl", hash_extra_groestl},
{"extra-jh", hash_extra_jh}, {"extra-skein", hash_extra_skein}, {"extra-jh", hash_extra_jh}, {"extra-skein", hash_extra_skein},
{"slow-1", cn_slow_hash_1}, {"slow-2", cn_slow_hash_2}, {"slow-4", cn_slow_hash_4}}; {"slow-1", cn_slow_hash_1}, {"slow-2", cn_slow_hash_2}, {"slow-4", cn_slow_hash_4}, {"blake2b", hash_blake2b}};
int test_variant2_int_sqrt(); int test_variant2_int_sqrt();
int test_variant2_int_sqrt_ref(); int test_variant2_int_sqrt_ref();
@ -144,6 +153,75 @@ int main(int argc, char *argv[]) {
} }
} }
input.open(argv[2], ios_base::in); input.open(argv[2], ios_base::in);
if (f == hash_blake2b) {
// blake2b does use different format and has key for hashing.
while (true) {
#define HASH_DATA_LEN 1024
// data = key[BLAKE2B_KEYBYTES] || hash data[HASH_DATA_LEN]
char data[BLAKE2B_KEYBYTES + HASH_DATA_LEN] = { 0 };
size_t datalen = 0;
char hash_result[BLAKE2B_OUTBYTES] = { 0 };
char hash_expected[BLAKE2B_OUTBYTES] = { 0 };
std::string temp; // t as in temporary
input >> temp;
if (temp.empty()) {
break;
} else if (test) { // first hash record special, does not have any "in:" value
assert(temp == "in:");
input >> temp; // actual in data
temp = boost::algorithm::unhex(temp);
if(temp.size() > HASH_DATA_LEN) {
std::cerr << "For case number " << test
<< " input data to hash is more than maximum(" << HASH_DATA_LEN << ")";
return -1;
}
std::copy(temp.begin(), temp.end(), data + BLAKE2B_KEYBYTES);
datalen = temp.size();
}
++test;
input >> temp;
assert(temp == "key:");
input >> temp; // actual keybytes data
temp = boost::algorithm::unhex(temp);
if(temp.size() != BLAKE2B_KEYBYTES) {
std::cerr << "For case number " << test
<< " key input does not have correct size.";
return -1;
}
std::copy(temp.begin(), temp.end(), data);
input >> temp;
assert(temp == "hash:");
input >> temp; // actual hashbytes data
temp = boost::algorithm::unhex(temp);
if(temp.size() != BLAKE2B_OUTBYTES) {
std::cerr << "For case number " << test
<< " hash input data does not have correct size.";
return -1;
}
std::copy(temp.begin(), temp.end(), hash_expected);
f(data, datalen, hash_result);
if (!std::equal(hash_result,
hash_result + BLAKE2B_OUTBYTES,
std::begin(hash_expected))) {
std::cerr << "For case number " << test
<< " computed hash value and given hash value does not match in blake2b";
return -1;
}
// Clean up the mess
memset(data, 0, sizeof(char) * (BLAKE2B_KEYBYTES + datalen));
memset(hash_result, 0, sizeof(char) * BLAKE2B_OUTBYTES);
memset(hash_expected, 0, sizeof(char) * BLAKE2B_OUTBYTES);
}
return 0;
}
for (;;) { for (;;) {
++test; ++test;
input.exceptions(ios_base::badbit); input.exceptions(ios_base::badbit);

1025
tests/hash/tests-blake2b.txt Normal file

File diff suppressed because it is too large Load Diff