Skip to content

Commit

Permalink
Hashing a pair of uint64 (+tests)
Browse files Browse the repository at this point in the history
  • Loading branch information
color-typea committed Jun 29, 2023
1 parent 32912dd commit c3bb577
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 34 deletions.
2 changes: 1 addition & 1 deletion libs/crypto3
Submodule crypto3 updated 2 files
+1 −1 .gitmodules
+0 −0 libs/threshold
37 changes: 21 additions & 16 deletions src/lib.hpp
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
11 changes: 7 additions & 4 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

using namespace nil::crypto3;

[[circuit]] typename hashes::sha2<256>::block_type circuit(
hashes::sha2<256>::block_type left,
hashes::sha2<256>::block_type right
constexpr size_t INPUT_SIZE = 16;

[[circuit]] unsigned int circuit(
uint64_t left,
uint64_t right,
hashes::sha2<256>::digest_type expected
) {
return hash_pair<hashes::sha2<256>>(left, right);
circuitImpl(left, right, expected);
}
7 changes: 7 additions & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ if(NOT Boost_UNIT_TEST_FRAMEWORK_FOUND)
endif()

cm_test_link_libraries(${Boost_LIBRARIES} template_lib)
option(TEST_TRACING "OUTPUT TRACE DATA" OFF)

macro(define_test name)
cm_test(NAME ${name}_test SOURCES ${name}.cpp)
Expand All @@ -23,9 +24,14 @@ macro(define_test name)
if(target_type STREQUAL "SHARED_LIB")
target_compile_definitions(${name}_test PRIVATE BOOST_TEST_DYN_LINK)
elseif(target_type STREQUAL "STATIC_LIB")
endif()

if (${TEST_TRACING})
target_compile_definitions(${name}_test PRIVATE TEST_TRACING=1)
endif()



# if(CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
# target_compile_options(${name}_test PRIVATE "-fconstexpr-steps=2147483647")
# elseif(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
Expand All @@ -38,6 +44,7 @@ macro(define_test name)
endmacro()

set(TESTS_NAMES
"hash"
"lib"
)

Expand Down
32 changes: 19 additions & 13 deletions test/lib.cpp
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()

0 comments on commit c3bb577

Please sign in to comment.