Skip to content

Commit

Permalink
Workaround for crypto3 issue
Browse files Browse the repository at this point in the history
  • Loading branch information
color-typea committed Jun 27, 2023
1 parent d3fb1d7 commit ce98e41
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 17 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
*.app

.idea/
.vscode

build/
cmake-build-debug/
13 changes: 12 additions & 1 deletion src/lib.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<hashes::sha2<256>>(left, right);
}
}
#else
template<typename HashType>
typename HashType::digest_type hash_pair(typename HashType::block_type block0, typename HashType::block_type block1) {
accumulator_set<HashType> acc;
acc(block0, accumulators::bits = HashType::block_bits);
acc(block1, accumulators::bits = HashType::block_bits);

return accumulators::extract::hash<HashType>(acc);
}
#endif
2 changes: 1 addition & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<hashes::sha2<256>>(left, right);
}
36 changes: 21 additions & 15 deletions test/lib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,38 @@
#include <boost/test/unit_test.hpp>
#include <boost/test/data/test_case.hpp>
#include <boost/algorithm/string/join.hpp>
#include <iostream>

#include <nil/crypto3/hash/algorithm/hash.hpp>
#include <nil/crypto3/hash/sha2.hpp>

#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<std::string, 16> 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<std::string, 16> 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<hashes::sha2<256>>(
left, right
);
BOOST_TEST(hash256ToHex(root) == expected);
auto actual = std::to_string(root);
BOOST_TEST(actual == expected);
}

BOOST_AUTO_TEST_SUITE_END()

0 comments on commit ce98e41

Please sign in to comment.