From a787100a5a10fb365fc6153688e713eb597da662 Mon Sep 17 00:00:00 2001 From: Nick Volynkin Date: Wed, 21 Jun 2023 12:44:46 +0300 Subject: [PATCH 1/4] ci: make extra directory with toolchain config Sometimes `proof-producer` looks for the config at `/root/.config`. --- scripts/ci.sh | 1 + 1 file changed, 1 insertion(+) diff --git a/scripts/ci.sh b/scripts/ci.sh index 8ed669f..ac721c0 100755 --- a/scripts/ci.sh +++ b/scripts/ci.sh @@ -119,6 +119,7 @@ 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 \ sh -c "bash /opt/zkllvm-template/scripts/ci.sh prove" From 81df0eab64b663774aa3687219b0de21906e46b7 Mon Sep 17 00:00:00 2001 From: Nick Volynkin Date: Wed, 21 Jun 2023 11:41:01 +0300 Subject: [PATCH 2/4] ci: pin zkLLVM 0.0.58 and toolchain 0.0.31 zkLLVM and Proof Market toolchain are based on a few common libraries, which should be updated simultaneously in both tools. Now CI and ci.sh script will use explicitly pinned versions of both tools to show how to use them together. Resolve #30 --- .github/workflows/main.yml | 4 ++++ Dockerfile | 4 +++- scripts/ci.sh | 8 ++++---- 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 1c3d1ba..7142730 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -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 diff --git a/Dockerfile b/Dockerfile index 0beaa6f..f7ec834 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,6 +1,8 @@ # 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 \ @@ -8,7 +10,7 @@ RUN DEBIAN_FRONTEND=noninteractive \ build-essential \ cmake \ git \ - zkllvm \ + zkllvm=${ZKLLVM_VERSION} \ && apt-get clean \ && rm -rf /var/lib/apt/lists/* diff --git a/scripts/ci.sh b/scripts/ci.sh index ac721c0..704aa0e 100755 --- a/scripts/ci.sh +++ b/scripts/ci.sh @@ -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 @@ -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 @@ -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 @@ -121,7 +121,7 @@ prove() { --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 From d5fdba60726a38f86746956243dd4534a3ffe449 Mon Sep 17 00:00:00 2001 From: color-typea Date: Mon, 19 Jun 2023 23:35:21 +0800 Subject: [PATCH 3/4] Minimal test with crypto3::sha256 usage --- CMakeLists.txt | 1 + cmake/modules | 2 +- libs/crypto3 | 2 +- src/CMakeLists.txt | 61 ++++++++++++++++++++++++++------------------- src/lib.hpp | 11 ++++++++ src/main.cpp | 26 ++++++------------- test/CMakeLists.txt | 46 ++++++++++++++++++++++++++++++++++ test/lib.cpp | 33 ++++++++++++++++++++++++ 8 files changed, 135 insertions(+), 47 deletions(-) create mode 100644 src/lib.hpp create mode 100644 test/CMakeLists.txt create mode 100644 test/lib.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index a9e6b72..8fc3933 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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") \ No newline at end of file diff --git a/cmake/modules b/cmake/modules index dd67cd7..f88b32a 160000 --- a/cmake/modules +++ b/cmake/modules @@ -1 +1 @@ -Subproject commit dd67cd7f0ac26cf79627377874dd40415c96c9e7 +Subproject commit f88b32ae8216289bdf4d067f00732b3917884fad diff --git a/libs/crypto3 b/libs/crypto3 index 3106e9e..afd0925 160000 --- a/libs/crypto3 +++ b/libs/crypto3 @@ -1 +1 @@ -Subproject commit 3106e9ef189093d95a561c8c1904b2bffd4d04b7 +Subproject commit afd0925f9fd7a31c306a6fcd9f118c43271c19a5 diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index e4dc54b..dbdf6cd 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -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 "$" "$") +target_link_libraries(template_lib INTERFACE ${CRYPTO3_DEPENDENCIES}) + + function(add_example example_target) set(prefix ARG) set(noValues "") @@ -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) + diff --git a/src/lib.hpp b/src/lib.hpp new file mode 100644 index 0000000..22c82af --- /dev/null +++ b/src/lib.hpp @@ -0,0 +1,11 @@ +#include +#include + +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>(left, right); +} \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index fd1994d..a8bf8ad 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,22 +1,10 @@ -#include +#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); } diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt new file mode 100644 index 0000000..ce862b2 --- /dev/null +++ b/test/CMakeLists.txt @@ -0,0 +1,46 @@ +#---------------------------------------------------------------------------# +# Copyright (c) 2018-2020 Mikhail Komarov +# +# 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() \ No newline at end of file diff --git a/test/lib.cpp b/test/lib.cpp new file mode 100644 index 0000000..7f0b8d0 --- /dev/null +++ b/test/lib.cpp @@ -0,0 +1,33 @@ +#define BOOST_TEST_MODULE LibTest +#include +#include +#include + +#include +#include + +#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 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() From 8161c5f008444801c1eb4122004da9f263c61050 Mon Sep 17 00:00:00 2001 From: Nick Volynkin Date: Mon, 26 Jun 2023 17:37:01 +0300 Subject: [PATCH 4/4] update submodule versions --- cmake/modules | 2 +- libs/crypto3 | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/cmake/modules b/cmake/modules index f88b32a..dd67cd7 160000 --- a/cmake/modules +++ b/cmake/modules @@ -1 +1 @@ -Subproject commit f88b32ae8216289bdf4d067f00732b3917884fad +Subproject commit dd67cd7f0ac26cf79627377874dd40415c96c9e7 diff --git a/libs/crypto3 b/libs/crypto3 index afd0925..3106e9e 160000 --- a/libs/crypto3 +++ b/libs/crypto3 @@ -1 +1 @@ -Subproject commit afd0925f9fd7a31c306a6fcd9f118c43271c19a5 +Subproject commit 3106e9ef189093d95a561c8c1904b2bffd4d04b7