Skip to content

Commit

Permalink
Merge pull request #3 from NilFoundation/directly-include-parallel-co…
Browse files Browse the repository at this point in the history
…ntainers

Directly include parallel containers
  • Loading branch information
AndreyMlashkin authored Jun 7, 2024
2 parents 1eb6a3e + 56dfb4c commit 8a7886c
Show file tree
Hide file tree
Showing 13 changed files with 2,009 additions and 4 deletions.
3 changes: 0 additions & 3 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@
[submodule "libs/threaded-math"]
path = libs/parallel-math
url = [email protected]:NilFoundation/actor-math
[submodule "libs/threaded-containers"]
path = libs/parallel-containers
url = [email protected]:NilFoundation/actor-containers
[submodule "cmake/modules"]
path = cmake/modules
url = [email protected]:BoostCMake/cmake_modules.git
1 change: 0 additions & 1 deletion libs/parallel-containers
Submodule parallel-containers deleted from 39e2c8
98 changes: 98 additions & 0 deletions libs/parallel-containers/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
#---------------------------------------------------------------------------//
# MIT License
#
# Copyright (c) 2020 Mikhail Komarov <[email protected]>
# Copyright (c) 2021 Aleksei Moskvin <[email protected]>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#---------------------------------------------------------------------------//

cmake_minimum_required(VERSION 2.8.12)

cmake_policy(SET CMP0042 NEW)
cmake_policy(SET CMP0028 NEW)
cmake_policy(SET CMP0048 NEW)
cmake_policy(SET CMP0057 NEW)
cmake_policy(SET CMP0079 OLD)

list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/cmake"
"${CMAKE_CURRENT_LIST_DIR}/cmake/modules/share/modules/cmake")

include(CMConfig)
include(CMSetupVersion)

cm_workspace(actor)

include(CMDeploy)
include(CMSetupVersion)

if (NOT Boost_FOUND AND NOT CMAKE_CROSSCOMPILING)
find_package(Boost COMPONENTS REQUIRED filesystem)
endif ()

cm_project(containers WORKSPACE_NAME ${CMAKE_WORKSPACE_NAME})

option(BUILD_DOXYGEN_DOCS "Build with configuring Doxygen documentation compiler" TRUE)

set(DOXYGEN_OUTPUT_DIR "${CMAKE_CURRENT_LIST_DIR}/docs" CACHE STRING "Specify doxygen output directory")

list(APPEND ${CURRENT_PROJECT_NAME}_PUBLIC_HEADERS
)

list(APPEND ${CURRENT_PROJECT_NAME}_UNGROUPED_SOURCES)

list(APPEND ${CURRENT_PROJECT_NAME}_HEADERS
${${CURRENT_PROJECT_NAME}_PUBLIC_HEADERS}
${${CURRENT_PROJECT_NAME}_UNGROUPED_SOURCES})

list(APPEND ${CURRENT_PROJECT_NAME}_SOURCES
${${CURRENT_PROJECT_NAME}_UNGROUPED_SOURCES})

cm_setup_version(VERSION 0.1.0 PREFIX ${CMAKE_WORKSPACE_NAME}_${CURRENT_PROJECT_NAME})

add_library(${CMAKE_WORKSPACE_NAME}_${CURRENT_PROJECT_NAME} INTERFACE)

set_target_properties(${CMAKE_WORKSPACE_NAME}_${CURRENT_PROJECT_NAME} PROPERTIES
EXPORT_NAME ${CURRENT_PROJECT_NAME})

target_link_libraries(${CMAKE_WORKSPACE_NAME}_${CURRENT_PROJECT_NAME} INTERFACE

${CMAKE_WORKSPACE_NAME}::core

crypto3::algebra
crypto3::hash

${Boost_LIBRARIES})

target_include_directories(${CMAKE_WORKSPACE_NAME}_${CURRENT_PROJECT_NAME} INTERFACE
"$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>"
"$<BUILD_INTERFACE:${CMAKE_BINARY_DIR}/include>"

${Boost_INCLUDE_DIRS})

cm_deploy(TARGETS ${CMAKE_WORKSPACE_NAME}_${CURRENT_PROJECT_NAME} INCLUDE include NAMESPACE ${CMAKE_WORKSPACE_NAME}::)

if (BUILD_TESTS)
add_subdirectory(test)
endif ()

if (BUILD_EXAMPLES)
add_subdirectory(example)
endif ()

21 changes: 21 additions & 0 deletions libs/parallel-containers/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Containers for =nil; Foundation's Cryptography Suite

Containers using =nil; Foundation's cryptography suite.

## Building

This library uses Boost CMake build modules (https://github.com/BoostCMake/cmake_modules.git).
To actually include this library in a project it is required to:

1. Add [CMake Modules](https://github.com/BoostCMake/cmake_modules.git) as submodule to target project repository.
2. Add all the internal dependencies using [CMake Modules](https://github.com/BoostCMake/cmake_modules.git) as submodules to target project repository.
3. Initialize parent project with [CMake Modules](https://github.com/BoostCMake/cmake_modules.git) (Look at [crypto3](https://github.com/nilfoundation/crypto3.git) for the example)

## Dependencies

### Internal

* [Hash](https://github.com/nilfoundation/crypto3-hash.git).

### External
* [Boost](https://boost.org) (>= 1.76)
48 changes: 48 additions & 0 deletions libs/parallel-containers/example/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#---------------------------------------------------------------------------//
# MIT License
#
# Copyright (c) 2020 Mikhail Komarov <[email protected]>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#---------------------------------------------------------------------------//


include_directories("${CMAKE_CURRENT_SOURCE_DIR}/../include"
"${CMAKE_CURRENT_BINARY_DIR}/include"

${Boost_INCLUDE_DIRS})

macro(define_containers_example example)
get_filename_component(test_name ${example} NAME)
set(target_name ${CMAKE_WORKSPACE_NAME}_${CURRENT_PROJECT_NAME}_${test_name}_example)

add_executable(${target_name} ${example}.cpp)
target_link_libraries(${target_name} PRIVATE
${CMAKE_WORKSPACE_NAME}::algebra
${CMAKE_WORKSPACE_NAME}::hash
${Boost_LIBRARIES})
set_target_properties(${target_name} PROPERTIES CXX_STANDARD 17)
endmacro()

set(EXAMPLES_NAMES
"merkle/merkle")

foreach(EXAMPLE_NAME ${EXAMPLES_NAMES})
define_containers_example(${EXAMPLE_NAME})
endforeach()
67 changes: 67 additions & 0 deletions libs/parallel-containers/example/merkle/merkle.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
//---------------------------------------------------------------------------//
// Copyright (c) 2018-2020 Mikhail Komarov <[email protected]>
// Copyright (c) 2021-2022 Aleksei Moskvin <[email protected]>
//
// MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//---------------------------------------------------------------------------//

#include <nil/crypto3/hash/sha2.hpp>
#include <nil/crypto3/hash/md5.hpp>
#include <nil/crypto3/hash/blake2b.hpp>

#include <nil/crypto3/container/merkle/tree.hpp>
#include <nil/crypto3/container/merkle/proof.hpp>

using namespace nil::crypto3;
using namespace nil::crypto3::containers;

int main() {
std::vector<std::array<char, 1> > data_on_leafs = {{'0'}, {'1'}, {'2'}, {'3'}, {'4'}, {'5'}, {'6'}, {'7'}, {'8'}};
std::array<char, 1> element_not_in_tree = {'9'};
merkle_tree<hashes::blake2b<224>, 3> tree = make_merkle_tree<hashes::blake2b<224>, 3>(data_on_leafs.begin(), data_on_leafs.end());
merkle_proof<hashes::blake2b<224>, 3> proof_leaf_3(tree, 3);
merkle_proof<hashes::blake2b<224>, 3> proof_leaf_0(tree, 0);
// std::cout << "Tree structure:" << std::endl;
// std::cout << tree << std::endl;
std::vector<std::array<char, 1>> data_to_check = {{data_on_leafs[2]}, {data_on_leafs[0]}, element_not_in_tree};
for (size_t i = 0; i < data_to_check.size(); ++i) {
std::cout << "Is leaf " << data_to_check[i][0] << " was in tree in position 0: ";
std::cout << std::boolalpha << proof_leaf_0.validate(data_to_check[i]) << std::endl;
std::cout << "Is leaf " << data_to_check[i][0] << " was in tree in position 3: ";
std::cout << std::boolalpha << proof_leaf_3.validate(data_to_check[i]) << std::endl;
}
std::cout << std::endl;

std::array<char, 7> left = {'\x6d', '\x65', '\x73', '\x73', '\x61', '\x67', '\x65'};
std::array<char, 7> right = {'\x20', '\x64', '\x69', '\x67', '\x65', '\x73', '\x74'};
std::vector<std::array<char, 7> > simple_binary_tree_data = {left, right};
merkle_tree<hashes::blake2b<224>, 2> simple_binary_tree = make_merkle_tree<hashes::blake2b<224>, 2>(simple_binary_tree_data.begin(), simple_binary_tree_data.end());
merkle_proof<hashes::blake2b<224>, 2> simple_binary_proof_leaf_1(simple_binary_tree, 1);
// std::cout << "Tree simple binary structure:" << std::endl;
// std::cout << simple_binary_tree << std::endl;
std::cout << "Is leaf " << data_on_leafs[1][0] << " was in tree in position 1: ";
std::cout << std::boolalpha << simple_binary_proof_leaf_1.validate(data_on_leafs[1]) << std::endl;
std::cout << "Is leaf left was in tree in position 1: ";
std::cout << std::boolalpha << simple_binary_proof_leaf_1.validate(left) << std::endl;
std::cout << "Is leaf right was in tree in position 1: ";
std::cout << std::boolalpha << simple_binary_proof_leaf_1.validate(right) << std::endl;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
//---------------------------------------------------------------------------//
// Copyright (c) 2018-2021 Mikhail Komarov <[email protected]>
// Copyright (c) 2020-2021 Nikita Kaskov <[email protected]>
// Copyright (c) 2021 Ilias Khairullin <[email protected]>
//
// MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//---------------------------------------------------------------------------//

#ifndef CRYPTO3_ZK_SNARK_ACCUMULATION_VECTOR_HPP
#define CRYPTO3_ZK_SNARK_ACCUMULATION_VECTOR_HPP

#include <iostream>
#include <iterator>

#include <nil/crypto3/container/sparse_vector.hpp>

namespace nil {
namespace crypto3 {
namespace container {

/**
* An accumulation vector comprises an accumulation value and a sparse vector.
* The method "accumulate_chunk" allows one to accumulate portions of the sparse
* vector into the accumulation value.
*/
template<typename Type>
class accumulation_vector {
using underlying_value_type = typename Type::value_type;

public:
using group_type = Type;

underlying_value_type first;
sparse_vector<Type> rest;

accumulation_vector() = default;
accumulation_vector(const accumulation_vector<Type> &other) = default;
accumulation_vector(accumulation_vector<Type> &&other) = default;
accumulation_vector(const underlying_value_type &first, sparse_vector<Type> &&rest) :
first(first), rest(std::move(rest)) {};
accumulation_vector(underlying_value_type &&first, sparse_vector<Type> &&rest) :
first(std::move(first)), rest(std::move(rest)) {};
accumulation_vector(underlying_value_type &&first, std::vector<underlying_value_type> &&v) :
first(std::move(first)), rest(std::move(v)) {
}
accumulation_vector(std::vector<underlying_value_type> &&v) :
first(underlying_value_type::zero()), rest(std::move(v)) {};

accumulation_vector<Type> &operator=(const accumulation_vector<Type> &other) = default;
accumulation_vector<Type> &operator=(accumulation_vector<Type> &&other) = default;

bool operator==(const accumulation_vector<Type> &other) const {
return (this->first == other.first && this->rest == other.rest);
}

bool is_fully_accumulated() const {
return rest.empty();
}

std::size_t domain_size() const {
return rest.domain_size();
}

std::size_t size() const {
return rest.domain_size();
}

std::size_t size_in_bits() const {
const std::size_t first_size_in_bits = Type::value_bits;
const std::size_t rest_size_in_bits = rest.size_in_bits();
return first_size_in_bits + rest_size_in_bits;
}

template<typename InputIterator>
accumulation_vector<Type> accumulate_chunk(InputIterator begin, InputIterator end,
std::size_t offset) const {
std::pair<underlying_value_type, sparse_vector<Type>> acc_result = rest.insert(offset, begin, end);
underlying_value_type new_first = first + acc_result.first;
return accumulation_vector<Type>(std::move(new_first), std::move(acc_result.second));
}
};
} // namespace container
} // namespace crypto3
} // namespace nil

#endif // CRYPTO3_ZK_SNARK_ACCUMULATION_VECTOR_HPP
Loading

0 comments on commit 8a7886c

Please sign in to comment.