Skip to content
This repository was archived by the owner on Feb 17, 2025. It is now read-only.

Commit 8008335

Browse files
authored
Merge pull request #24 from NilFoundation/12-implement-elgamal
Verifiable El-Gamal added
2 parents fcd356d + 666558f commit 8008335

23 files changed

+2558
-5
lines changed

.gitmodules

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[submodule "cmake/modules"]
2+
path = cmake/modules
3+
url = [email protected]:BoostCMake/cmake_modules.git

CMakeLists.txt

+9-4
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,12 @@ cmake_policy(SET CMP0042 NEW)
55
cmake_policy(SET CMP0048 NEW)
66
cmake_policy(SET CMP0076 NEW)
77

8-
if(NOT ${CMAKE_WORKSPACE_NAME} STREQUAL crypto3)
8+
list(APPEND CMAKE_MODULE_PATH
9+
"${CMAKE_CURRENT_LIST_DIR}/cmake"
10+
"${CMAKE_CURRENT_LIST_DIR}/cmake/packages"
11+
"${CMAKE_CURRENT_LIST_DIR}/cmake/modules/share/modules/cmake")
12+
13+
if(NOT CMAKE_WORKSPACE_NAME OR NOT ("${CMAKE_WORKSPACE_NAME}" STREQUAL "crypto3"))
914
cm_workspace(crypto3)
1015
endif()
1116

@@ -21,15 +26,15 @@ endif()
2126

2227
cm_find_package(${CMAKE_WORKSPACE_NAME}_algebra)
2328
cm_find_package(${CMAKE_WORKSPACE_NAME}_pkpad)
24-
cm_find_package(${CMAKE_WORKSPACE_NAME}_random)
29+
cm_find_package(${CMAKE_WORKSPACE_NAME}_zk)
2530

2631
option(BUILD_TESTS "Build unit tests" FALSE)
2732
option(BUILD_EXAMPLES "Build examples" FALSE)
2833

2934
list(APPEND ${CURRENT_PROJECT_NAME}_LIBRARIES
3035
${CMAKE_WORKSPACE_NAME}::algebra
31-
${CMAKE_WORKSPACE_NAME}::pkpad
32-
${CMAKE_WORKSPACE_NAME}::random)
36+
# TODO: add conditional link of zk depending on ElGamal
37+
${CMAKE_WORKSPACE_NAME}::zk)
3338

3439
list(APPEND ${CURRENT_PROJECT_NAME}_PUBLIC_HEADERS
3540
include/nil/crypto3/pubkey/algorithm/sign.hpp

cmake/modules

Submodule modules added at 5e6b354
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
//---------------------------------------------------------------------------//
2+
// Copyright (c) 2021 Mikhail Komarov <[email protected]>
3+
// Copyright (c) 2021 Ilias Khairullin <[email protected]>
4+
//
5+
// MIT License
6+
//
7+
// Permission is hereby granted, free of charge, to any person obtaining a copy
8+
// of this software and associated documentation files (the "Software"), to deal
9+
// in the Software without restriction, including without limitation the rights
10+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
// copies of the Software, and to permit persons to whom the Software is
12+
// furnished to do so, subject to the following conditions:
13+
//
14+
// The above copyright notice and this permission notice shall be included in all
15+
// copies or substantial portions of the Software.
16+
//
17+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23+
// SOFTWARE.
24+
//---------------------------------------------------------------------------//
25+
26+
#ifndef CRYPTO3_ACCUMULATORS_PUBKEY_HPP
27+
#define CRYPTO3_ACCUMULATORS_PUBKEY_HPP
28+
29+
#include <iterator>
30+
#include <type_traits>
31+
32+
#include <boost/parameter/value_type.hpp>
33+
34+
#include <boost/accumulators/framework/accumulator_base.hpp>
35+
#include <boost/accumulators/framework/extractor.hpp>
36+
#include <boost/accumulators/framework/depends_on.hpp>
37+
#include <boost/accumulators/framework/parameters/sample.hpp>
38+
39+
#include <nil/crypto3/pubkey/accumulators/parameters/iterator_last.hpp>
40+
41+
namespace nil {
42+
namespace crypto3 {
43+
namespace pubkey {
44+
namespace accumulators {
45+
namespace impl {
46+
template<typename ProcessingMode, typename = void>
47+
struct pubkey_impl;
48+
49+
template<typename ProcessingMode>
50+
struct pubkey_impl<ProcessingMode> : boost::accumulators::accumulator_base {
51+
protected:
52+
typedef ProcessingMode processing_mode_type;
53+
typedef typename processing_mode_type::internal_accumulator_type internal_accumulator_type;
54+
55+
public:
56+
typedef typename processing_mode_type::result_type result_type;
57+
58+
template<typename Args>
59+
pubkey_impl(const Args &args) :
60+
acc(processing_mode_type::init_accumulator(args[boost::accumulators::sample])) {
61+
}
62+
63+
template<typename Args>
64+
inline void operator()(const Args &args) {
65+
resolve_type(args[boost::accumulators::sample | nullptr],
66+
args[nil::crypto3::accumulators::iterator_last | nullptr]);
67+
}
68+
69+
inline result_type result(boost::accumulators::dont_care) const {
70+
return processing_mode_type::process(acc);
71+
}
72+
73+
protected:
74+
inline void resolve_type(std::nullptr_t, std::nullptr_t) {
75+
}
76+
77+
template<typename InputRange>
78+
inline void resolve_type(const InputRange &range, std::nullptr_t) {
79+
processing_mode_type::update(acc, range);
80+
}
81+
82+
template<typename InputIterator>
83+
inline void resolve_type(InputIterator first, InputIterator last) {
84+
processing_mode_type::update(acc, first, last);
85+
}
86+
87+
mutable internal_accumulator_type acc;
88+
};
89+
} // namespace impl
90+
91+
namespace tag {
92+
template<typename ProcessingMode>
93+
struct pubkey : boost::accumulators::depends_on<> {
94+
typedef ProcessingMode processing_mode_type;
95+
96+
/// INTERNAL ONLY
97+
///
98+
99+
typedef boost::mpl::always<accumulators::impl::pubkey_impl<processing_mode_type>> impl;
100+
};
101+
} // namespace tag
102+
103+
namespace extract {
104+
template<typename ProcessingMode, typename AccumulatorSet>
105+
typename boost::mpl::apply<AccumulatorSet, tag::pubkey<ProcessingMode>>::type::result_type
106+
pubkey(const AccumulatorSet &acc) {
107+
return boost::accumulators::extract_result<tag::pubkey<ProcessingMode>>(acc);
108+
}
109+
} // namespace extract
110+
} // namespace accumulators
111+
} // namespace pubkey
112+
} // namespace crypto3
113+
} // namespace nil
114+
115+
#endif // CRYPTO3_ACCUMULATORS_PUBKEY_HPP
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
//---------------------------------------------------------------------------//
2+
// Copyright (c) 2021 Mikhail Komarov <[email protected]>
3+
// Copyright (c) 2021 Ilias Khairullin <[email protected]>
4+
//
5+
// MIT License
6+
//
7+
// Permission is hereby granted, free of charge, to any person obtaining a copy
8+
// of this software and associated documentation files (the "Software"), to deal
9+
// in the Software without restriction, including without limitation the rights
10+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
// copies of the Software, and to permit persons to whom the Software is
12+
// furnished to do so, subject to the following conditions:
13+
//
14+
// The above copyright notice and this permission notice shall be included in all
15+
// copies or substantial portions of the Software.
16+
//
17+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23+
// SOFTWARE.
24+
//---------------------------------------------------------------------------//
25+
26+
#ifndef CRYPTO3_PUBKEY_DECRYPT_HPP
27+
#define CRYPTO3_PUBKEY_DECRYPT_HPP
28+
29+
#include <nil/crypto3/pubkey/algorithm/pubkey.hpp>
30+
31+
#include <nil/crypto3/pubkey/pubkey_value.hpp>
32+
#include <nil/crypto3/pubkey/pubkey_state.hpp>
33+
34+
#include <nil/crypto3/pubkey/modes/isomorphic.hpp>
35+
36+
#include <nil/crypto3/pubkey/operations/decrypt_op.hpp>
37+
38+
namespace nil {
39+
namespace crypto3 {
40+
namespace pubkey {
41+
template<typename Scheme>
42+
using decryption_init_params_type = typename decrypt_op<Scheme>::init_params_type;
43+
}
44+
template<typename Scheme, typename Mode = pubkey::modes::isomorphic<Scheme>,
45+
typename ProcessingMode = typename Mode::decryption_policy, typename InputIterator,
46+
typename PubkeyAccumulator = pubkey::pubkey_accumulator_set<ProcessingMode>,
47+
typename StreamSchemeImpl = pubkey::detail::value_pubkey_impl<PubkeyAccumulator>,
48+
typename SchemeImpl = pubkey::detail::range_pubkey_impl<StreamSchemeImpl>>
49+
SchemeImpl decrypt(InputIterator first, InputIterator last,
50+
const pubkey::decryption_init_params_type<Scheme> &init_params) {
51+
return SchemeImpl(first, last, PubkeyAccumulator(init_params));
52+
}
53+
54+
template<typename Scheme, typename Mode = pubkey::modes::isomorphic<Scheme>,
55+
typename ProcessingMode = typename Mode::decryption_policy, typename SinglePassRange,
56+
typename PubkeyAccumulator = pubkey::pubkey_accumulator_set<ProcessingMode>,
57+
typename StreamSchemeImpl = pubkey::detail::value_pubkey_impl<PubkeyAccumulator>,
58+
typename SchemeImpl = pubkey::detail::range_pubkey_impl<StreamSchemeImpl>>
59+
SchemeImpl decrypt(const SinglePassRange &range,
60+
const pubkey::decryption_init_params_type<Scheme> &init_params) {
61+
return SchemeImpl(range, PubkeyAccumulator(init_params));
62+
}
63+
64+
template<typename Scheme, typename Mode = pubkey::modes::isomorphic<Scheme>,
65+
typename ProcessingMode = typename Mode::decryption_policy, typename InputIterator,
66+
typename OutputAccumulator = pubkey::pubkey_accumulator_set<ProcessingMode>>
67+
typename std::enable_if<boost::accumulators::detail::is_accumulator_set<OutputAccumulator>::value,
68+
OutputAccumulator>::type &
69+
decrypt(InputIterator first, InputIterator last, OutputAccumulator &acc) {
70+
typedef pubkey::detail::ref_pubkey_impl<OutputAccumulator> StreamSchemeImpl;
71+
typedef pubkey::detail::range_pubkey_impl<StreamSchemeImpl> SchemeImpl;
72+
73+
return SchemeImpl(first, last, std::forward<OutputAccumulator>(acc));
74+
}
75+
76+
template<typename Scheme, typename Mode = pubkey::modes::isomorphic<Scheme>,
77+
typename ProcessingMode = typename Mode::decryption_policy, typename SinglePassRange,
78+
typename OutputAccumulator = pubkey::pubkey_accumulator_set<ProcessingMode>>
79+
typename std::enable_if<boost::accumulators::detail::is_accumulator_set<OutputAccumulator>::value,
80+
OutputAccumulator>::type &
81+
decrypt(const SinglePassRange &range, OutputAccumulator &acc) {
82+
typedef pubkey::detail::ref_pubkey_impl<OutputAccumulator> StreamSchemeImpl;
83+
typedef pubkey::detail::range_pubkey_impl<StreamSchemeImpl> SchemeImpl;
84+
85+
return SchemeImpl(range, std::forward<OutputAccumulator>(acc));
86+
}
87+
88+
template<typename Scheme, typename Mode = pubkey::modes::isomorphic<Scheme>,
89+
typename ProcessingMode = typename Mode::decryption_policy, typename InputIterator,
90+
typename OutputIterator>
91+
OutputIterator decrypt(InputIterator first, InputIterator last,
92+
const pubkey::decryption_init_params_type<Scheme> &init_params, OutputIterator out) {
93+
typedef pubkey::pubkey_accumulator_set<ProcessingMode> PubkeyAccumulator;
94+
95+
typedef pubkey::detail::value_pubkey_impl<PubkeyAccumulator> StreamSchemeImpl;
96+
typedef pubkey::detail::itr_pubkey_impl<StreamSchemeImpl, OutputIterator> SchemeImpl;
97+
98+
return SchemeImpl(first, last, std::move(out), PubkeyAccumulator(init_params));
99+
}
100+
101+
template<typename Scheme, typename Mode = pubkey::modes::isomorphic<Scheme>,
102+
typename ProcessingMode = typename Mode::decryption_policy, typename SinglePassRange,
103+
typename OutputIterator>
104+
OutputIterator decrypt(const SinglePassRange &range,
105+
const pubkey::decryption_init_params_type<Scheme> &init_params, OutputIterator out) {
106+
typedef pubkey::pubkey_accumulator_set<ProcessingMode> PubkeyAccumulator;
107+
108+
typedef pubkey::detail::value_pubkey_impl<PubkeyAccumulator> StreamSchemeImpl;
109+
typedef pubkey::detail::itr_pubkey_impl<StreamSchemeImpl, OutputIterator> SchemeImpl;
110+
111+
return SchemeImpl(range, std::move(out), PubkeyAccumulator(init_params));
112+
}
113+
} // namespace crypto3
114+
} // namespace nil
115+
116+
#endif // CRYPTO3_PUBKEY_DECRYPT_HPP
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
//---------------------------------------------------------------------------//
2+
// Copyright (c) 2021 Mikhail Komarov <[email protected]>
3+
// Copyright (c) 2021 Ilias Khairullin <[email protected]>
4+
//
5+
// MIT License
6+
//
7+
// Permission is hereby granted, free of charge, to any person obtaining a copy
8+
// of this software and associated documentation files (the "Software"), to deal
9+
// in the Software without restriction, including without limitation the rights
10+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
// copies of the Software, and to permit persons to whom the Software is
12+
// furnished to do so, subject to the following conditions:
13+
//
14+
// The above copyright notice and this permission notice shall be included in all
15+
// copies or substantial portions of the Software.
16+
//
17+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23+
// SOFTWARE.
24+
//---------------------------------------------------------------------------//
25+
26+
#ifndef CRYPTO3_PUBKEY_ENCRYPT_HPP
27+
#define CRYPTO3_PUBKEY_ENCRYPT_HPP
28+
29+
#include <nil/crypto3/pubkey/algorithm/pubkey.hpp>
30+
31+
#include <nil/crypto3/pubkey/pubkey_value.hpp>
32+
#include <nil/crypto3/pubkey/pubkey_state.hpp>
33+
34+
#include <nil/crypto3/pubkey/modes/isomorphic.hpp>
35+
36+
#include <nil/crypto3/pubkey/operations/encrypt_op.hpp>
37+
38+
namespace nil {
39+
namespace crypto3 {
40+
namespace pubkey {
41+
template<typename Scheme>
42+
using encryption_init_params_type = typename encrypt_op<Scheme>::init_params_type;
43+
}
44+
template<typename Scheme, typename Mode = pubkey::modes::isomorphic<Scheme>,
45+
typename ProcessingMode = typename Mode::encryption_policy, typename InputIterator,
46+
typename PubkeyAccumulator = pubkey::pubkey_accumulator_set<ProcessingMode>,
47+
typename StreamSchemeImpl = pubkey::detail::value_pubkey_impl<PubkeyAccumulator>,
48+
typename SchemeImpl = pubkey::detail::range_pubkey_impl<StreamSchemeImpl>>
49+
SchemeImpl encrypt(InputIterator first, InputIterator last,
50+
const pubkey::encryption_init_params_type<Scheme> &init_params) {
51+
return SchemeImpl(first, last, PubkeyAccumulator(init_params));
52+
}
53+
54+
template<typename Scheme, typename Mode = pubkey::modes::isomorphic<Scheme>,
55+
typename ProcessingMode = typename Mode::encryption_policy, typename SinglePassRange,
56+
typename PubkeyAccumulator = pubkey::pubkey_accumulator_set<ProcessingMode>,
57+
typename StreamSchemeImpl = pubkey::detail::value_pubkey_impl<PubkeyAccumulator>,
58+
typename SchemeImpl = pubkey::detail::range_pubkey_impl<StreamSchemeImpl>>
59+
SchemeImpl encrypt(const SinglePassRange &range,
60+
const pubkey::encryption_init_params_type<Scheme> &init_params) {
61+
return SchemeImpl(range, PubkeyAccumulator(init_params));
62+
}
63+
64+
template<typename Scheme, typename Mode = pubkey::modes::isomorphic<Scheme>,
65+
typename ProcessingMode = typename Mode::encryption_policy, typename InputIterator,
66+
typename OutputAccumulator = pubkey::pubkey_accumulator_set<ProcessingMode>>
67+
typename std::enable_if<boost::accumulators::detail::is_accumulator_set<OutputAccumulator>::value,
68+
OutputAccumulator>::type &
69+
encrypt(InputIterator first, InputIterator last, OutputAccumulator &acc) {
70+
typedef pubkey::detail::ref_pubkey_impl<OutputAccumulator> StreamSchemeImpl;
71+
typedef pubkey::detail::range_pubkey_impl<StreamSchemeImpl> SchemeImpl;
72+
73+
return SchemeImpl(first, last, std::forward<OutputAccumulator>(acc));
74+
}
75+
76+
template<typename Scheme, typename Mode = pubkey::modes::isomorphic<Scheme>,
77+
typename ProcessingMode = typename Mode::encryption_policy, typename SinglePassRange,
78+
typename OutputAccumulator = pubkey::pubkey_accumulator_set<ProcessingMode>>
79+
typename std::enable_if<boost::accumulators::detail::is_accumulator_set<OutputAccumulator>::value,
80+
OutputAccumulator>::type &
81+
encrypt(const SinglePassRange &range, OutputAccumulator &acc) {
82+
typedef pubkey::detail::ref_pubkey_impl<OutputAccumulator> StreamSchemeImpl;
83+
typedef pubkey::detail::range_pubkey_impl<StreamSchemeImpl> SchemeImpl;
84+
85+
return SchemeImpl(range, std::forward<OutputAccumulator>(acc));
86+
}
87+
88+
template<typename Scheme, typename Mode = pubkey::modes::isomorphic<Scheme>,
89+
typename ProcessingMode = typename Mode::encryption_policy, typename InputIterator,
90+
typename OutputIterator>
91+
OutputIterator encrypt(InputIterator first, InputIterator last,
92+
const pubkey::encryption_init_params_type<Scheme> &init_params, OutputIterator out) {
93+
typedef pubkey::pubkey_accumulator_set<ProcessingMode> PubkeyAccumulator;
94+
95+
typedef pubkey::detail::value_pubkey_impl<PubkeyAccumulator> StreamSchemeImpl;
96+
typedef pubkey::detail::itr_pubkey_impl<StreamSchemeImpl, OutputIterator> SchemeImpl;
97+
98+
return SchemeImpl(first, last, std::move(out), PubkeyAccumulator(init_params));
99+
}
100+
101+
template<typename Scheme, typename Mode = pubkey::modes::isomorphic<Scheme>,
102+
typename ProcessingMode = typename Mode::encryption_policy, typename SinglePassRange,
103+
typename OutputIterator>
104+
OutputIterator encrypt(const SinglePassRange &range,
105+
const pubkey::encryption_init_params_type<Scheme> &init_params, OutputIterator out) {
106+
typedef pubkey::pubkey_accumulator_set<ProcessingMode> PubkeyAccumulator;
107+
108+
typedef pubkey::detail::value_pubkey_impl<PubkeyAccumulator> StreamSchemeImpl;
109+
typedef pubkey::detail::itr_pubkey_impl<StreamSchemeImpl, OutputIterator> SchemeImpl;
110+
111+
return SchemeImpl(range, std::move(out), PubkeyAccumulator(init_params));
112+
}
113+
} // namespace crypto3
114+
} // namespace nil
115+
116+
#endif // CRYPTO3_PUBKEY_ENCRYPT_HPP

0 commit comments

Comments
 (0)