Skip to content

Commit

Permalink
Minimal test with crypto3::sha256 usage
Browse files Browse the repository at this point in the history
  • Loading branch information
color-typea committed Jun 27, 2023
1 parent 45b35aa commit d3fb1d7
Show file tree
Hide file tree
Showing 8 changed files with 135 additions and 47 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ add_subdirectory("${CMAKE_CURRENT_LIST_DIR}/libs/crypto3")

#Example directories
add_subdirectory("${CMAKE_CURRENT_LIST_DIR}/src")
add_subdirectory("${CMAKE_CURRENT_LIST_DIR}/test")
2 changes: 1 addition & 1 deletion libs/crypto3
Submodule crypto3 updated 1 files
+0 −7 CMakeLists.txt
61 changes: 35 additions & 26 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,36 @@
set(CRYPTO3_DEPENDENCIES
crypto3::algebra
crypto3::block
crypto3::codec
crypto3::containers
crypto3::hash
crypto3::kdf
crypto3::mac
marshalling::core
marshalling::crypto3_algebra
marshalling::crypto3_multiprecision
marshalling::crypto3_zk
crypto3::math
crypto3::modes
crypto3::multiprecision
crypto3::passhash
crypto3::pbkdf
crypto3::threshold
crypto3::pkpad
crypto3::pubkey
crypto3::random
crypto3::stream
crypto3::vdf
crypto3::zk
)

add_library(template_lib INTERFACE)

target_include_directories(template_lib
INTERFACE "$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}>" "$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>")
target_link_libraries(template_lib INTERFACE ${CRYPTO3_DEPENDENCIES})


function(add_example example_target)
set(prefix ARG)
set(noValues "")
Expand All @@ -11,32 +44,8 @@ function(add_example example_target)
add_circuit(${example_target}
SOURCES ${ARG_SOURCES}

LINK_LIBRARIES
crypto3::algebra
crypto3::block
crypto3::codec
crypto3::containers
crypto3::hash
crypto3::kdf
crypto3::mac
marshalling::core
marshalling::crypto3_algebra
marshalling::crypto3_multiprecision
marshalling::crypto3_zk
crypto3::math
crypto3::modes
crypto3::multiprecision
crypto3::passhash
crypto3::pbkdf
crypto3::threshold
crypto3::pkpad
crypto3::pubkey
crypto3::random
crypto3::stream
crypto3::vdf
crypto3::zk

${Boost_LIBRARIES})
LINK_LIBRARIES ${CRYPTO3_DEPENDENCIES} ${Boost_LIBRARIES} template_lib)
endfunction()

add_example(template SOURCES main.cpp INPUT main.inp)

11 changes: 11 additions & 0 deletions src/lib.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include <nil/crypto3/hash/algorithm/hash.hpp>
#include <nil/crypto3/hash/sha2.hpp>

using namespace nil::crypto3;

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);
}
26 changes: 7 additions & 19 deletions src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,22 +1,10 @@
#include <nil/crypto3/algebra/curves/pallas.hpp>
#include "lib.hpp"

using namespace nil::crypto3::algebra::curves;
typename pallas::base_field_type::value_type pow(typename pallas::base_field_type::value_type a, int n) {
if (n == 0)
return 1;
using namespace nil::crypto3;

typename pallas::base_field_type::value_type res = 1;
for (int i = 0; i < n; ++i) {
res *= a;
}
return res;
}

[[circuit]] typename pallas::base_field_type::value_type
field_arithmetic_example(typename pallas::base_field_type::value_type a,
typename pallas::base_field_type::value_type b) {

typename pallas::base_field_type::value_type c = (a + b) * a + b * (a + b) * (a + b);
const typename pallas::base_field_type::value_type constant = 0x12345678901234567890_cppui255;
return c * c * c / (b - a) + pow(a, 2) + constant;
[[circuit]] typename hashes::sha2<256>::block_type circuit(
hashes::sha2<256>::block_type left,
hashes::sha2<256>::block_type right
) {
return hash_pair(left, right);
}
46 changes: 46 additions & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#---------------------------------------------------------------------------#
# Copyright (c) 2018-2020 Mikhail Komarov <nemo@nil.foundation>
#
# Distributed under the Boost Software License, Version 1.0
# See accompanying file LICENSE_1_0.txt or copy at
# http://www.boost.org/LICENSE_1_0.txt
#---------------------------------------------------------------------------#

include(CMTest)

if(NOT Boost_UNIT_TEST_FRAMEWORK_FOUND)
cm_find_package(Boost REQUIRED COMPONENTS unit_test_framework)
endif()

cm_test_link_libraries(${Boost_LIBRARIES} template_lib)

macro(define_test name)
cm_test(NAME ${name}_test SOURCES ${name}.cpp)

set_target_properties(${name}_test PROPERTIES CXX_STANDARD 20 CXX_STANDARD_REQUIRED TRUE)

get_target_property(target_type Boost::unit_test_framework TYPE)
if(target_type STREQUAL "SHARED_LIB")
target_compile_definitions(${name}_test PRIVATE BOOST_TEST_DYN_LINK)
elseif(target_type STREQUAL "STATIC_LIB")

endif()

# if(CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
# target_compile_options(${name}_test PRIVATE "-fconstexpr-steps=2147483647")
# elseif(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
# target_compile_options(${name}_test PRIVATE "-fconstexpr-ops-limit=4294967295")
# endif()

# string(CONCAT TEST_DATA ${CMAKE_CURRENT_SOURCE_DIR} "/data/" "${name}" ".json")
# target_compile_definitions(hash_${name}_test PRIVATE TEST_DATA="${TEST_DATA}")

endmacro()

set(TESTS_NAMES
"lib"
)

foreach(TEST_NAME ${TESTS_NAMES})
define_test(${TEST_NAME})
endforeach()
33 changes: 33 additions & 0 deletions test/lib.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +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 "lib.hpp"

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, "");
}

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}
);
BOOST_TEST(hash256ToHex(root) == expected);
}
BOOST_AUTO_TEST_SUITE_END()

0 comments on commit d3fb1d7

Please sign in to comment.