Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minimal test with crypto3::sha256 usage #33

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ concurrency:
format('{0}/{1}', github.workflow, github.ref) }}
cancel-in-progress: true

env:
ZKLLVM_VERSION: 0.0.58
TOOLCHAIN_VERSION: 0.0.31

jobs:
test-zkllvm-workflow:
runs-on: ubuntu-22.04
Expand Down
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")
4 changes: 3 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
# build to ghcr.io/nilfoundation/zkllvm-template:latest
FROM ghcr.io/nilfoundation/build-base:1.76.0

ARG ZKLLVM_VERSION=0.0.58

RUN DEBIAN_FRONTEND=noninteractive \
echo 'deb [trusted=yes] http://deb.nil.foundation/ubuntu/ all main' >> /etc/apt/sources.list \
&& apt-get update \
&& apt-get -y --no-install-recommends --no-install-suggests install \
build-essential \
cmake \
git \
zkllvm \
zkllvm=${ZKLLVM_VERSION} \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/*

Expand Down
9 changes: 5 additions & 4 deletions scripts/ci.sh
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ compile() {
--platform=linux/amd64 \
--user $(id -u ${USER}):$(id -g ${USER}) \
--volume $(pwd):/opt/zkllvm-template \
ghcr.io/nilfoundation/zkllvm-template:latest \
ghcr.io/nilfoundation/zkllvm-template:${ZKLLVM_VERSION} \
sh -c "bash ./scripts/ci.sh compile"
cd -
else
Expand All @@ -62,7 +62,7 @@ run_assigner() {
--platform=linux/amd64 \
--user $(id -u ${USER}):$(id -g ${USER}) \
--volume $(pwd):/opt/zkllvm-template \
ghcr.io/nilfoundation/zkllvm-template:latest \
ghcr.io/nilfoundation/zkllvm-template:${ZKLLVM_VERSION} \
sh -c "bash ./scripts/ci.sh run_assigner"
cd -
else
Expand Down Expand Up @@ -90,7 +90,7 @@ build_statement() {
--platform=linux/amd64 \
--user $(id -u ${USER}):$(id -g ${USER}) \
--volume $(pwd):/opt/zkllvm-template \
ghcr.io/nilfoundation/proof-market-toolchain:latest \
ghcr.io/nilfoundation/proof-market-toolchain:${TOOLCHAIN_VERSION} \
sh -c "bash /opt/zkllvm-template/scripts/ci.sh build_statement"
cd -
else
Expand Down Expand Up @@ -119,8 +119,9 @@ prove() {
--platform=linux/amd64 \
--user $(id -u ${USER}):$(id -g ${USER}) \
--volume $(pwd):/opt/zkllvm-template \
--volume $(pwd)/.config:/root/.config \
--volume $(pwd)/.config:/proof-market-toolchain/.config \
ghcr.io/nilfoundation/proof-market-toolchain:latest \
ghcr.io/nilfoundation/proof-market-toolchain:${TOOLCHAIN_VERSION} \
sh -c "bash /opt/zkllvm-template/scripts/ci.sh prove"
cd -
else
Expand Down
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()
Loading