From a5095b2c6eaf8b4d0d79f43b5c65228c35eb092e Mon Sep 17 00:00:00 2001 From: fixbitnote <37100952+fixbitnote@users.noreply.github.com> Date: Tue, 6 Mar 2018 16:19:29 +0800 Subject: [PATCH 1/6] Update the slow_hash tests with dark test cases. Added the reference hash for cryptonight dark for the reference hash. --- tests/performance_tests/cn_slow_hash.h | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/tests/performance_tests/cn_slow_hash.h b/tests/performance_tests/cn_slow_hash.h index 9eee0eb..3300d50 100644 --- a/tests/performance_tests/cn_slow_hash.h +++ b/tests/performance_tests/cn_slow_hash.h @@ -44,7 +44,7 @@ class test_cn_slow_hash }; #pragma pack(pop) - static_assert(13 == sizeof(data_t), "Invalid structure size"); + //static_assert(13 == sizeof(data_t), "Invalid structure size"); bool init() { @@ -54,17 +54,25 @@ class test_cn_slow_hash if (!epee::string_tools::hex_to_pod("bbec2cacf69866a8e740380fe7b818fc78f8571221742d729d9d02d7f8989b87", m_expected_hash)) return false; + if (!epee::string_tools::hex_to_pod("98c48cda9ab5d26ef2717e7375f665fc18b703aac6c059bb814685f21946647f", m_expected_hash_dark)) + return false; + + return true; } bool test() { crypto::hash hash; + crypto::hash hash_dark; + crypto::cn_slow_hash(&m_data, sizeof(m_data), hash, 0); - return hash == m_expected_hash; + crypto::cn_slow_hash(&m_data, sizeof(m_data), hash_dark, 1); + return (hash == m_expected_hash) && (hash_dark == m_expected_hash_dark); } private: data_t m_data; crypto::hash m_expected_hash; + crypto::hash m_expected_hash_dark; }; From 407bb4be57fd6c69b3e0f1505145824c466e20a9 Mon Sep 17 00:00:00 2001 From: fixbitnote <37100952+fixbitnote@users.noreply.github.com> Date: Tue, 6 Mar 2018 16:22:02 +0800 Subject: [PATCH 2/6] Re-enable the static assertions. --- tests/performance_tests/cn_slow_hash.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/performance_tests/cn_slow_hash.h b/tests/performance_tests/cn_slow_hash.h index 3300d50..0faa157 100644 --- a/tests/performance_tests/cn_slow_hash.h +++ b/tests/performance_tests/cn_slow_hash.h @@ -44,7 +44,7 @@ class test_cn_slow_hash }; #pragma pack(pop) - //static_assert(13 == sizeof(data_t), "Invalid structure size"); + static_assert(13 == sizeof(data_t), "Invalid structure size"); bool init() { From 0740d9ec1aaa28403848c47e8831559dc43fd67b Mon Sep 17 00:00:00 2001 From: fixbitnote <37100952+fixbitnote@users.noreply.github.com> Date: Tue, 6 Mar 2018 16:47:03 +0800 Subject: [PATCH 3/6] Added the testcase for slow-1m algorithm (dark) --- tests/hash/tests-slow1m.txt | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 tests/hash/tests-slow1m.txt diff --git a/tests/hash/tests-slow1m.txt b/tests/hash/tests-slow1m.txt new file mode 100644 index 0000000..784ee99 --- /dev/null +++ b/tests/hash/tests-slow1m.txt @@ -0,0 +1,5 @@ +98c48cda9ab5d26ef2717e7375f665fc18b703aac6c059bb814685f21946647f 63617665617420656d70746f72 +04d2b27b1595ddaa6286aa2e2933f7ae9d7f41ceaf94fb3cf16e46a926f0e1ba 6465206f6d6e69627573206475626974616e64756d +1d6fcfc843cee5070b140f72278082c89e57fb2d9f050fd04b380702672e77ab 6162756e64616e732063617574656c61206e6f6e206e6f636574 +98c48cda9ab5d26ef2717e7375f665fc18b703aac6c059bb814685f21946647f 63617665617420656d70746f72 +63711b8f6ba33da18059d5ab808df6138f6492e1b9565042bd018dbeeac45afa 6578206e6968696c6f206e6968696c20666974 From 91496ec5583eb112e8b12c9263b85e045acd3202 Mon Sep 17 00:00:00 2001 From: fixbitnote <37100952+fixbitnote@users.noreply.github.com> Date: Tue, 6 Mar 2018 16:47:32 +0800 Subject: [PATCH 4/6] Separated the old slow test case into it's own. --- tests/hash/tests-slow2m.txt | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 tests/hash/tests-slow2m.txt diff --git a/tests/hash/tests-slow2m.txt b/tests/hash/tests-slow2m.txt new file mode 100644 index 0000000..1489cc5 --- /dev/null +++ b/tests/hash/tests-slow2m.txt @@ -0,0 +1,5 @@ +bbec2cacf69866a8e740380fe7b818fc78f8571221742d729d9d02d7f8989b87 63617665617420656d70746f72 +2f8e3df40bd11f9ac90c743ca8e32bb391da4fb98612aa3b6cdc639ee00b31f5 6465206f6d6e69627573206475626974616e64756d +722fa8ccd594d40e4a41f3822734304c8d5eff7e1b528408e2229da38ba553c4 6162756e64616e732063617574656c61206e6f6e206e6f636574 +bbec2cacf69866a8e740380fe7b818fc78f8571221742d729d9d02d7f8989b87 63617665617420656d70746f72 +b1257de4efc5ce28c6b40ceb1c6c8f812a64634eb3e81c5220bee9b2b76a6f05 6578206e6968696c6f206e6968696c20666974 From 6560c7fda18804ffcdc25dc91b57e7aac50e9b08 Mon Sep 17 00:00:00 2001 From: fixbitnote <37100952+fixbitnote@users.noreply.github.com> Date: Tue, 6 Mar 2018 16:48:01 +0800 Subject: [PATCH 5/6] Delete the obsolete old slow test case. --- tests/hash/tests-slow.txt | 4 ---- 1 file changed, 4 deletions(-) delete mode 100644 tests/hash/tests-slow.txt diff --git a/tests/hash/tests-slow.txt b/tests/hash/tests-slow.txt deleted file mode 100644 index 9085daf..0000000 --- a/tests/hash/tests-slow.txt +++ /dev/null @@ -1,4 +0,0 @@ -2f8e3df40bd11f9ac90c743ca8e32bb391da4fb98612aa3b6cdc639ee00b31f5 6465206f6d6e69627573206475626974616e64756d -722fa8ccd594d40e4a41f3822734304c8d5eff7e1b528408e2229da38ba553c4 6162756e64616e732063617574656c61206e6f6e206e6f636574 -bbec2cacf69866a8e740380fe7b818fc78f8571221742d729d9d02d7f8989b87 63617665617420656d70746f72 -b1257de4efc5ce28c6b40ceb1c6c8f812a64634eb3e81c5220bee9b2b76a6f05 6578206e6968696c6f206e6968696c20666974 From db6fa2e9d3685cf17f5e6049c1a89083b19b7479 Mon Sep 17 00:00:00 2001 From: fixbitnote <37100952+fixbitnote@users.noreply.github.com> Date: Tue, 6 Mar 2018 17:58:24 +0800 Subject: [PATCH 6/6] Fix incorrect change to state_index in slow_hash. Fixes issue: https://github.com/xbn-project/bitnote/issues/3 Properly adjusts the indexing when calculating the cryptonight dark slow hash. --- src/crypto/slow-hash.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/crypto/slow-hash.c b/src/crypto/slow-hash.c index 9f8033e..4c2928f 100644 --- a/src/crypto/slow-hash.c +++ b/src/crypto/slow-hash.c @@ -69,6 +69,8 @@ #define AES_KEY_SIZE 32 #define INIT_SIZE_BLK 8 #define INIT_SIZE_BYTE (INIT_SIZE_BLK * AES_BLOCK_SIZE) +#define TOTALBLOCKS (MEMORY / AES_BLOCK_SIZE) +#define state_index(x,div) (((*((uint64_t *)x) >> 4) & (TOTALBLOCKS /(div) - 1)) << 4) #define U64(x) ((uint64_t *) (x)) #define R128(x) ((__m128i *) (x)) @@ -234,11 +236,10 @@ void cn_slow_hash(const void *data, size_t length, char *hash, int dark) for(i = 0; i < ITER / 2; i++) { - #define TOTALBLOCKS (MEMORY / AES_BLOCK_SIZE) - #define state_index(x) (((*((uint64_t *)x) >> 4) & (TOTALBLOCKS - 1)) << 4) + // Iteration 1 - p = &long_state[state_index(a)]; + p = &long_state[state_index(a, (dark?4:1))]; if(useAes) _mm_storeu_si128(R128(p), _mm_aesenc_si128(_mm_loadu_si128(R128(p)), _mm_loadu_si128(R128(a)))); @@ -250,7 +251,7 @@ void cn_slow_hash(const void *data, size_t length, char *hash, int dark) swap_blocks(a, b); // Iteration 2 - p = &long_state[state_index(a)]; + p = &long_state[state_index(a, (dark?4:1))]; mul(a, p, d); sum_half_blocks(b, d);