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

Commit 08861c0

Browse files
author
Ilias Khairullin
committed
More threshold scheme refactoring. #3
1 parent dbe87c1 commit 08861c0

File tree

5 files changed

+429
-298
lines changed

5 files changed

+429
-298
lines changed

include/nil/crypto3/pubkey/modes/algorithm/create_key.hpp

+59-74
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,13 @@
3434

3535
#include <boost/range/concepts.hpp>
3636

37+
#include <nil/crypto3/pubkey/type_traits.hpp>
38+
3739
#include <nil/crypto3/pubkey/secret_sharing/pedersen.hpp>
38-
// #include <nil/crypto3/pubkey/secret_sharing/weighted_shamir.hpp>
40+
#include <nil/crypto3/pubkey/secret_sharing/weighted_shamir.hpp>
3941

4042
#include <nil/crypto3/pubkey/keys/private_key.hpp>
43+
#include <nil/crypto3/pubkey/modes/part_public_key.hpp>
4144

4245
#include <nil/crypto3/pubkey/algorithm/deal_shares.hpp>
4346
#include <nil/crypto3/pubkey/algorithm/deal_share.hpp>
@@ -49,10 +52,9 @@ namespace nil {
4952
// CoeffIt - coefficients of polynomial
5053
//
5154
template<typename Scheme, typename CoeffIt,
52-
typename SecretSharingScheme = typename pubkey::private_key<Scheme>::sss_public_key_group_type>
55+
typename SecretSharingScheme = typename pubkey::public_key<Scheme>::sss_public_key_group_type>
5356
inline typename std::enable_if<
54-
std::is_same<pubkey::shamir_sss<typename SecretSharingScheme::group_type>, SecretSharingScheme>::value ||
55-
std::is_same<pubkey::feldman_sss<typename SecretSharingScheme::group_type>, SecretSharingScheme>::value,
57+
pubkey::is_shamir_sss<SecretSharingScheme>::value || pubkey::is_feldman_sss<SecretSharingScheme>::value,
5658
std::pair<pubkey::public_key<Scheme>, std::vector<pubkey::private_key<Scheme>>>>::type
5759
create_key(CoeffIt first, CoeffIt last, std::size_t n) {
5860
BOOST_CONCEPT_ASSERT((boost::InputIteratorConcept<CoeffIt>));
@@ -73,10 +75,9 @@ namespace nil {
7375
// Coeffs - coefficients of polynomial
7476
//
7577
template<typename Scheme, typename Coeffs,
76-
typename SecretSharingScheme = typename pubkey::private_key<Scheme>::sss_public_key_group_type>
78+
typename SecretSharingScheme = typename pubkey::public_key<Scheme>::sss_public_key_group_type>
7779
inline typename std::enable_if<
78-
std::is_same<pubkey::shamir_sss<typename SecretSharingScheme::group_type>, SecretSharingScheme>::value ||
79-
std::is_same<pubkey::feldman_sss<typename SecretSharingScheme::group_type>, SecretSharingScheme>::value,
80+
pubkey::is_shamir_sss<SecretSharingScheme>::value || pubkey::is_feldman_sss<SecretSharingScheme>::value,
8081
std::pair<pubkey::public_key<Scheme>, std::vector<pubkey::private_key<Scheme>>>>::type
8182
create_key(const Coeffs &r, std::size_t n) {
8283
BOOST_RANGE_CONCEPT_ASSERT((boost::SinglePassRangeConcept<const Coeffs>));
@@ -88,10 +89,9 @@ namespace nil {
8889
// PublicCoeffIt - public representation values of polynomial's coefficients
8990
//
9091
template<typename Scheme, typename PublicCoeffIt,
91-
typename SecretSharingScheme = typename pubkey::private_key<Scheme>::sss_public_key_group_type>
92-
inline typename std::enable_if<
93-
std::is_same<pubkey::feldman_sss<typename SecretSharingScheme::group_type>, SecretSharingScheme>::value,
94-
pubkey::private_key<Scheme>>::type
92+
typename SecretSharingScheme = typename pubkey::public_key<Scheme>::sss_public_key_group_type>
93+
inline typename std::enable_if<pubkey::is_feldman_sss<SecretSharingScheme>::value,
94+
pubkey::private_key<Scheme>>::type
9595
create_key(PublicCoeffIt first, PublicCoeffIt last, const pubkey::share_sss<SecretSharingScheme> &share,
9696
std::size_t n) {
9797
BOOST_CONCEPT_ASSERT((boost::InputIteratorConcept<PublicCoeffIt>));
@@ -104,10 +104,9 @@ namespace nil {
104104
// PublicCoeffs - public representation values of polynomial's coefficients
105105
//
106106
template<typename Scheme, typename PublicCoeffs,
107-
typename SecretSharingScheme = typename pubkey::private_key<Scheme>::sss_public_key_group_type>
108-
inline typename std::enable_if<
109-
std::is_same<pubkey::feldman_sss<typename SecretSharingScheme::group_type>, SecretSharingScheme>::value,
110-
pubkey::private_key<Scheme>>::type
107+
typename SecretSharingScheme = typename pubkey::public_key<Scheme>::sss_public_key_group_type>
108+
inline typename std::enable_if<pubkey::is_feldman_sss<SecretSharingScheme>::value,
109+
pubkey::private_key<Scheme>>::type
111110
create_key(const PublicCoeffs &r, pubkey::share_sss<SecretSharingScheme> &share, std::size_t n) {
112111
BOOST_RANGE_CONCEPT_ASSERT((boost::SinglePassRangeConcept<const PublicCoeffs>));
113112
return create_key<Scheme>(std::cbegin(r), std::cend(r), share, n);
@@ -118,10 +117,9 @@ namespace nil {
118117
// ShareIt - shares generated by other participants
119118
//
120119
template<typename Scheme, typename PublicCoeffsIt, typename ShareIt,
121-
typename SecretSharingScheme = typename pubkey::private_key<Scheme>::sss_public_key_group_type>
122-
inline typename std::enable_if<
123-
std::is_same<pubkey::pedersen_dkg<typename SecretSharingScheme::group_type>, SecretSharingScheme>::value,
124-
std::pair<pubkey::public_key<Scheme>, pubkey::private_key<Scheme>>>::type
120+
typename SecretSharingScheme = typename pubkey::public_key<Scheme>::sss_public_key_group_type>
121+
inline typename std::enable_if<pubkey::is_pedersen_dkg<SecretSharingScheme>::value,
122+
std::pair<pubkey::public_key<Scheme>, pubkey::private_key<Scheme>>>::type
125123
create_key(PublicCoeffsIt first1, PublicCoeffsIt last1, ShareIt first2, ShareIt last2, std::size_t n) {
126124
BOOST_CONCEPT_ASSERT((boost::InputIteratorConcept<PublicCoeffsIt>));
127125
BOOST_RANGE_CONCEPT_ASSERT(
@@ -156,10 +154,9 @@ namespace nil {
156154
// Shares - shares generated by other participants
157155
//
158156
template<typename Scheme, typename PublicCoeffsRange, typename Shares,
159-
typename SecretSharingScheme = typename pubkey::private_key<Scheme>::sss_public_key_group_type>
160-
inline typename std::enable_if<
161-
std::is_same<pubkey::pedersen_dkg<typename SecretSharingScheme::group_type>, SecretSharingScheme>::value,
162-
std::pair<pubkey::public_key<Scheme>, pubkey::private_key<Scheme>>>::type
157+
typename SecretSharingScheme = typename pubkey::public_key<Scheme>::sss_public_key_group_type>
158+
inline typename std::enable_if<pubkey::is_pedersen_dkg<SecretSharingScheme>::value,
159+
std::pair<pubkey::public_key<Scheme>, pubkey::private_key<Scheme>>>::type
163160
create_key(const PublicCoeffsRange &r, const Shares &shares, std::size_t n) {
164161
BOOST_RANGE_CONCEPT_ASSERT((boost::SinglePassRangeConcept<const PublicCoeffsRange>));
165162
BOOST_RANGE_CONCEPT_ASSERT(
@@ -170,57 +167,45 @@ namespace nil {
170167
return create_key<Scheme>(std::cbegin(r), std::cend(r), std::cbegin(shares), std::cend(shares), n);
171168
}
172169

173-
// //
174-
// // CoeffIt - coefficients of polynomial
175-
// // InputIterator2 - participants' weights
176-
// //
177-
// template<typename Scheme, typename CoeffIt, typename WeightsIterator,
178-
// typename SecretSharingScheme = typename pubkey::private_key<Scheme>::sss_public_key_group_type,
179-
// typename ValueType1 = typename std::iterator_traits<CoeffIt>::value_type,
180-
// typename ValueType2 = typename std::iterator_traits<WeightsIterator>::value_type,
181-
// typename SecretSharingScheme::template check_coeff_type<ValueType1> = true,
182-
// typename SecretSharingScheme::template check_weight_type<ValueType2> = true>
183-
// inline typename std::enable_if<
184-
// std::is_same<pubkey::weighted_shamir_sss<typename SecretSharingScheme::group_type>,
185-
// SecretSharingScheme>::value,
186-
// std::pair<pubkey::public_key<Scheme>, std::vector<pubkey::private_key<Scheme>>>>::type
187-
// create_key(CoeffIt first1, CoeffIt last1, WeightsIterator first2, WeightsIterator last2) {
188-
// BOOST_CONCEPT_ASSERT((boost::InputIteratorConcept<CoeffIt>));
189-
// BOOST_CONCEPT_ASSERT((boost::InputIteratorConcept<WeightsIterator>));
190-
//
191-
// using privkeys_type = std::vector<pubkey::private_key<Scheme>>;
192-
// using sss_no_key_ops_type = typename pubkey::private_key<Scheme>::sss_public_key_no_key_ops_type;
193-
//
194-
// typename sss_no_key_ops_type::shares_type shares = nil::crypto3::deal_shares<SecretSharingScheme>(
195-
// first1, last1, first2, last2, std::distance(first2, last2));
196-
// privkeys_type privkeys;
197-
// for (const auto &s : shares) {
198-
// privkeys.emplace_back(s, std::distance(first1, last1));
199-
// }
200-
// auto PK = pubkey::public_key<Scheme>(sss_no_key_ops_type::get_public_coeffs(first1, last1).front(),
201-
// std::distance(first2, last2));
202-
// return std::make_pair(PK, privkeys);
203-
// }
204-
//
205-
// //
206-
// // Coeffs - coefficients of polynomial
207-
// // WeightsRange - participants' weights
208-
// //
209-
// template<typename Scheme, typename Coeffs, typename WeightsRange,
210-
// typename SecretSharingScheme = typename pubkey::private_key<Scheme>::sss_public_key_group_type,
211-
// typename ValueType1 = typename std::iterator_traits<typename Coeffs::iterator>::value_type,
212-
// typename ValueType2 = typename std::iterator_traits<typename WeightsRange::iterator>::value_type,
213-
// typename SecretSharingScheme::template check_coeff_type<ValueType1> = true,
214-
// typename SecretSharingScheme::template check_weight_type<ValueType2> = true>
215-
// inline typename std::enable_if<
216-
// std::is_same<pubkey::weighted_shamir_sss<typename SecretSharingScheme::group_type>,
217-
// SecretSharingScheme>::value,
218-
// std::pair<pubkey::public_key<Scheme>, std::vector<pubkey::private_key<Scheme>>>>::type
219-
// create_key(const Coeffs &r1, const WeightsRange &r2) {
220-
// BOOST_RANGE_CONCEPT_ASSERT((boost::SinglePassRangeConcept<const Coeffs>));
221-
// BOOST_RANGE_CONCEPT_ASSERT((boost::SinglePassRangeConcept<const WeightsRange>));
222-
// return create_key<Scheme>(r1.begin(), r1.end(), r2.begin(), r2.end());
223-
// }
170+
//
171+
// CoeffIt - coefficients of polynomial
172+
// InputIterator2 - participants' weights
173+
//
174+
template<typename Scheme, typename CoeffIt,
175+
typename SecretSharingScheme = typename pubkey::public_key<Scheme>::sss_public_key_group_type>
176+
inline typename std::enable_if<
177+
pubkey::is_weighted_shamir_sss<SecretSharingScheme>::value,
178+
std::pair<pubkey::public_key<Scheme>, std::vector<pubkey::private_key<Scheme>>>>::type
179+
create_key(CoeffIt first1, CoeffIt last1, std::size_t n, const typename SecretSharingScheme::weights_type &weights) {
180+
BOOST_CONCEPT_ASSERT((boost::InputIteratorConcept<CoeffIt>));
181+
182+
using shares_dealing_mode = typename pubkey::modes::isomorphic<SecretSharingScheme>::template bind<
183+
pubkey::shares_dealing_policy<SecretSharingScheme>>::type;
184+
185+
typename shares_dealing_mode::result_type shares = nil::crypto3::deal_shares<SecretSharingScheme>(
186+
first1, last1, n, weights);
187+
std::vector<pubkey::private_key<Scheme>> privkeys;
188+
for (const auto &s : shares) {
189+
privkeys.emplace_back(s);
190+
}
191+
auto PK = pubkey::public_key<Scheme>(SecretSharingScheme::get_public_coeffs(first1, last1).front());
192+
return std::make_pair(PK, privkeys);
193+
}
194+
195+
//
196+
// Coeffs - coefficients of polynomial
197+
// WeightsRange - participants' weights
198+
//
199+
template<typename Scheme, typename Coeffs,
200+
typename SecretSharingScheme = typename pubkey::public_key<Scheme>::sss_public_key_group_type>
201+
inline typename std::enable_if<
202+
pubkey::is_weighted_shamir_sss<SecretSharingScheme>::value,
203+
std::pair<pubkey::public_key<Scheme>, std::vector<pubkey::private_key<Scheme>>>>::type
204+
create_key(const Coeffs &r1, std::size_t n, const typename SecretSharingScheme::weights_type &weights) {
205+
BOOST_RANGE_CONCEPT_ASSERT((boost::SinglePassRangeConcept<const Coeffs>));
206+
207+
return create_key<Scheme>(std::cbegin(r1), std::cend(r1), n, weights);
208+
}
224209
} // namespace crypto3
225210
} // namespace nil
226211

include/nil/crypto3/pubkey/modes/algorithm/part_verify.hpp

+11-11
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
#include <nil/crypto3/pubkey/pubkey_value.hpp>
3030
#include <nil/crypto3/pubkey/modes/pubkey_state.hpp>
3131

32-
#include <nil/crypto3/pubkey/keys/public_key.hpp>
32+
#include <nil/crypto3/pubkey/modes/part_public_key.hpp>
3333

3434
namespace nil {
3535
namespace crypto3 {
@@ -56,8 +56,8 @@ namespace nil {
5656
template<typename Mode, typename SinglePassRange, typename OutputIterator>
5757
OutputIterator
5858
part_verify(const SinglePassRange &rng,
59-
const typename pubkey::public_key<typename Mode::scheme_type>::part_signature_type &part_sig,
60-
const pubkey::public_key<typename Mode::scheme_type> &key, OutputIterator out) {
59+
const typename pubkey::part_public_key<typename Mode::scheme_type>::part_signature_type &part_sig,
60+
const pubkey::part_public_key<typename Mode::scheme_type> &key, OutputIterator out) {
6161

6262
typedef typename Mode::template bind<pubkey::part_verification_mode_policy<Mode>>::type ProcessingMode;
6363
typedef typename pubkey::part_verification_accumulator_set<ProcessingMode> ModeAccumulator;
@@ -88,8 +88,8 @@ namespace nil {
8888
template<typename Mode, typename InputIterator, typename OutputIterator>
8989
OutputIterator
9090
part_verify(InputIterator first, InputIterator last,
91-
const typename pubkey::public_key<typename Mode::scheme_type>::part_signature_type &part_sig,
92-
const pubkey::public_key<typename Mode::scheme_type> &key, OutputIterator out) {
91+
const typename pubkey::part_public_key<typename Mode::scheme_type>::part_signature_type &part_sig,
92+
const pubkey::part_public_key<typename Mode::scheme_type> &key, OutputIterator out) {
9393

9494
typedef typename Mode::template bind<pubkey::part_verification_mode_policy<Mode>>::type ProcessingMode;
9595
typedef typename pubkey::part_verification_accumulator_set<ProcessingMode> ModeAccumulator;
@@ -178,8 +178,8 @@ namespace nil {
178178
typename Mode::template bind<pubkey::part_verification_mode_policy<Mode>>::type>>
179179
pubkey::detail::range_pubkey_impl<pubkey::detail::value_pubkey_impl<ModeAccumulator>>
180180
part_verify(InputIterator first, InputIterator last,
181-
const typename pubkey::public_key<typename Mode::scheme_type>::part_signature_type &part_sig,
182-
const pubkey::public_key<typename Mode::scheme_type> &key) {
181+
const typename pubkey::part_public_key<typename Mode::scheme_type>::part_signature_type &part_sig,
182+
const pubkey::part_public_key<typename Mode::scheme_type> &key) {
183183

184184
typedef typename Mode::template bind<pubkey::part_verification_mode_policy<Mode>>::type ProcessingMode;
185185

@@ -208,8 +208,8 @@ namespace nil {
208208
typename Mode::template bind<pubkey::part_verification_mode_policy<Mode>>::type>>
209209
pubkey::detail::range_pubkey_impl<pubkey::detail::value_pubkey_impl<ModeAccumulator>>
210210
part_verify(const SinglePassRange &r,
211-
const typename pubkey::public_key<typename Mode::scheme_type>::part_signature_type &part_sig,
212-
const pubkey::public_key<typename Mode::scheme_type> &key) {
211+
const typename pubkey::part_public_key<typename Mode::scheme_type>::part_signature_type &part_sig,
212+
const pubkey::part_public_key<typename Mode::scheme_type> &key) {
213213

214214
typedef typename Mode::template bind<pubkey::part_verification_mode_policy<Mode>>::type ProcessingMode;
215215

@@ -240,8 +240,8 @@ namespace nil {
240240
// typename Mode::template bind<pubkey::part_verification_mode_policy<Mode>>::type>>
241241
// pubkey::detail::range_pubkey_impl<pubkey::detail::value_pubkey_impl<ModeAccumulator>>
242242
// part_verify(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2,
243-
// const typename pubkey::public_key<typename Mode::scheme_type>::part_signature_type &part_sig,
244-
// const pubkey::public_key<typename Mode::scheme_type> &key) {
243+
// const typename pubkey::part_public_key<typename Mode::scheme_type>::part_signature_type &part_sig,
244+
// const pubkey::part_public_key<typename Mode::scheme_type> &key) {
245245
//
246246
// typedef typename Mode::template bind<pubkey::part_verification_mode_policy<Mode>>::type ProcessingMode;
247247
//
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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_PART_PUBLIC_KEY_HPP
27+
#define CRYPTO3_PUBKEY_PART_PUBLIC_KEY_HPP
28+
29+
namespace nil {
30+
namespace crypto3 {
31+
namespace pubkey {
32+
/*!
33+
* @brief
34+
*
35+
* @ingroup pubkey_algorithms
36+
*
37+
* Public key - a key that can be published and used to verify the authenticity
38+
* of the signed document, as well as to prevent fraud on the part of the certifying
39+
* person in the form of his refusal to sign the document.
40+
*
41+
*/
42+
template<typename Scheme, typename = void>
43+
struct part_public_key;
44+
} // namespace pubkey
45+
} // namespace crypto3
46+
} // namespace nil
47+
48+
#endif // CRYPTO3_PUBKEY_PART_PUBLIC_KEY_HPP

0 commit comments

Comments
 (0)