From ce98e415a86c9cc8eba795c4b75346cdf22e31af Mon Sep 17 00:00:00 2001 From: color-typea Date: Tue, 27 Jun 2023 19:31:15 +0800 Subject: [PATCH] Workaround for crypto3 issue --- .gitignore | 1 + src/lib.hpp | 13 ++++++++++++- src/main.cpp | 2 +- test/lib.cpp | 36 +++++++++++++++++++++--------------- 4 files changed, 35 insertions(+), 17 deletions(-) diff --git a/.gitignore b/.gitignore index 6d82c44..5583819 100644 --- a/.gitignore +++ b/.gitignore @@ -32,6 +32,7 @@ *.app .idea/ +.vscode build/ cmake-build-debug/ \ No newline at end of file diff --git a/src/lib.hpp b/src/lib.hpp index 22c82af..7871bb1 100644 --- a/src/lib.hpp +++ b/src/lib.hpp @@ -3,9 +3,20 @@ using namespace nil::crypto3; +#ifdef __ZKLLVM__ typename hashes::sha2<256>::block_type hash_pair( hashes::sha2<256>::block_type left, hashes::sha2<256>::block_type right ) { return hash>(left, right); -} \ No newline at end of file +} +#else +template +typename HashType::digest_type hash_pair(typename HashType::block_type block0, typename HashType::block_type block1) { + accumulator_set acc; + acc(block0, accumulators::bits = HashType::block_bits); + acc(block1, accumulators::bits = HashType::block_bits); + + return accumulators::extract::hash(acc); +} +#endif \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index a8bf8ad..a3cafc0 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -6,5 +6,5 @@ using namespace nil::crypto3; hashes::sha2<256>::block_type left, hashes::sha2<256>::block_type right ) { - return hash_pair(left, right); + return hash_pair>(left, right); } diff --git a/test/lib.cpp b/test/lib.cpp index 7f0b8d0..ed0c728 100644 --- a/test/lib.cpp +++ b/test/lib.cpp @@ -2,32 +2,38 @@ #include #include #include +#include #include #include #include "lib.hpp" -std::string intToHex(unsigned int value) { - std::stringstream sstream; - sstream << std::hex << value; - return sstream.str(); -} +// std::string intToHex(unsigned int value) { +// std::stringstream sstream; +// sstream << std::hex << value; +// return sstream.str(); +// } -std::string hash256ToHex(hashes::sha2<256>::block_type hash) { - std::array elems; - std::transform(hash.begin(), hash.end(), elems.begin(), intToHex); - return boost::algorithm::join(elems, ""); -} +// std::string hash256ToHex(const hashes::sha2<256>::digest_type &hash) { +// std::array elems; +// std::transform(hash.begin(), hash.end(), elems.begin(), intToHex); +// return boost::algorithm::join(elems, ""); +// } BOOST_AUTO_TEST_SUITE(lib_test) BOOST_AUTO_TEST_CASE(test_balance) { - std::string expected = "9a5ee745fda52931b4174b0ea83af76e48f32e03c9ad6fc563c580c2497302fd"; - typename hashes::sha2<256>::block_type root = hash_pair( - hashes::sha2<256>::block_type {1}, - hashes::sha2<256>::block_type {2} + std::string expected = "ff55c97976a840b4ced964ed49e3794594ba3f675238b5fd25d282b60f70a194"; + + auto left = hashes::sha2<256>::block_type {1}; + auto right = hashes::sha2<256>::block_type {2}; + + typename hashes::sha2<256>::digest_type root = hash_pair>( + left, right ); - BOOST_TEST(hash256ToHex(root) == expected); + auto actual = std::to_string(root); + BOOST_TEST(actual == expected); } + BOOST_AUTO_TEST_SUITE_END()