-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
32912dd
commit c3bb577
Showing
5 changed files
with
55 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,27 @@ | ||
#include <nil/crypto3/hash/algorithm/hash.hpp> | ||
#include <nil/crypto3/hash/sha2.hpp> | ||
#pragma once | ||
#include "hash.hpp" | ||
#include "utils.hpp" | ||
|
||
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 | ||
using hash_type = hashes::sha2<256>; | ||
constexpr size_t half_block_length = hash_type::block_words / 2; | ||
using half_block = std::array<hash_type::block_type::value_type, half_block_length>; | ||
|
||
unsigned int circuitImpl( | ||
uint64_t left, | ||
uint64_t right, | ||
hashes::sha2<256>::digest_type expected | ||
) { | ||
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); | ||
hash_type::block_type block = padAndJoinToBlock<hash_type, 2, 2>( | ||
uint64ToLittleEndianWords<hash_type>(left), | ||
uint64ToLittleEndianWords<hash_type>(right) | ||
); | ||
|
||
return accumulators::extract::hash<HashType>(acc); | ||
hash_type::digest_type actual = hash_one<hash_type>(block); | ||
if (actual == expected) { | ||
return 1; | ||
} else { | ||
return 0; | ||
} | ||
} | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,33 @@ | ||
#define BOOST_TEST_MODULE LibTest | ||
#include <boost/test/unit_test.hpp> | ||
#include <boost/test/data/test_case.hpp> | ||
#include <boost/algorithm/string/join.hpp> | ||
|
||
#include <nil/crypto3/hash/algorithm/hash.hpp> | ||
#include <nil/crypto3/hash/sha2.hpp> | ||
#include <boost/algorithm/hex.hpp> | ||
|
||
#include "lib.hpp" | ||
|
||
#include "testutil.hpp" | ||
|
||
using hash_type = hashes::sha2<256>; | ||
using half_block = std::array<hash_type::block_type::value_type, hash_type::block_words / 2>; | ||
|
||
BOOST_AUTO_TEST_SUITE(lib_test) | ||
|
||
BOOST_AUTO_TEST_CASE(test_balance) { | ||
std::string expected = "ff55c97976a840b4ced964ed49e3794594ba3f675238b5fd25d282b60f70a194"; | ||
BOOST_AUTO_TEST_CASE(circuitImpl_zeroes) { | ||
hashes::sha2<256>::digest_type expected; | ||
|
||
boost::algorithm::unhex("f5a5fd42d16a20302798ef6ed309979b43003d2320d9f0e8ea9831a92759fb4b", expected.begin()); | ||
|
||
uint result = circuitImpl(0u, 0u, expected); | ||
BOOST_TEST(result == 1); | ||
} | ||
|
||
auto left = hashes::sha2<256>::block_type {1}; | ||
auto right = hashes::sha2<256>::block_type {2}; | ||
BOOST_AUTO_TEST_CASE(hash_single_values) { | ||
hashes::sha2<256>::digest_type expected; | ||
|
||
boost::algorithm::unhex("a50dbf1b92471fd9d5f142060e67c71976b6728ff03df56f3968e4be017ebbcd", expected.begin()); | ||
|
||
typename hashes::sha2<256>::digest_type root = hash_pair<hashes::sha2<256>>( | ||
left, right | ||
); | ||
auto actual = std::to_string(root); | ||
BOOST_TEST(actual == expected); | ||
uint result = circuitImpl(0xabcdef012345ul, 0xaabbccddeefful, expected); | ||
BOOST_TEST(result == 1); | ||
} | ||
|
||
BOOST_AUTO_TEST_SUITE_END() |