elgamal.hpp
Go to the documentation of this file.
1 //---------------------------------------------------------------------------//
2 // Copyright (c) 2018-2020 Mikhail Komarov <nemo@nil.foundation>
3 //
4 // MIT License
5 //
6 // Permission is hereby granted, free of charge, to any person obtaining a copy
7 // of this software and associated documentation files (the "Software"), to deal
8 // in the Software without restriction, including without limitation the rights
9 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 // copies of the Software, and to permit persons to whom the Software is
11 // furnished to do so, subject to the following conditions:
12 //
13 // The above copyright notice and this permission notice shall be included in all
14 // copies or substantial portions of the Software.
15 //
16 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 // SOFTWARE.
23 //---------------------------------------------------------------------------//
24 
25 #ifndef CRYPTO3_PUBKEY_ELGAMAL_HPP
26 #define CRYPTO3_PUBKEY_ELGAMAL_HPP
27 
28 #include <nil/crypto3/pubkey/dl_algorithm.hpp>
29 
30 namespace nil {
31  namespace crypto3 {
32  namespace pubkey {
33  template<typename FieldType>
35  typedef FieldType field_type;
36 
37  typedef typename field_type::value_type value_type;
38 
39  constexpr static const std::size_t key_bits = field_type::modulus_bits;
40  typedef typename field_type::modulus_type key_type;
41 
42  constexpr static const std::size_t key_schedule_bits = field_type::modulus_bits;
43  typedef typename field_type::modulus_type key_schedule_type;
44 
45  constexpr static const std::size_t signature_bits = field_type::modulus_bits * 2;
46  typedef std::tuple<value_type, value_type> signature_type;
47 
48  inline static bool encrypt(const signature_type &val, const key_schedule_type &key) {
49  number<Backend, ExpressionTemplates> m(msg, msg_len);
50 
51  if (m >= m_group.get_p()) {
52  throw std::invalid_argument("ElGamal encryption: Input is too large");
53  }
54 
55  const size_t k_bits = m_group.exponent_bits();
56  const number<Backend, ExpressionTemplates> k(rng, k_bits);
57 
58  const number<Backend, ExpressionTemplates> a = m_group.power_g_p(k);
59  const number<Backend, ExpressionTemplates> b = m_group.multiply_mod_p(m, m_powermod_y_p(k));
60 
61  return number<Backend, ExpressionTemplates>::encode_fixed_length_int_pair(a, b, m_group.p_bytes());
62  }
63  };
64 
65  template<typename FieldType>
67  typedef FieldType field_type;
68 
69  typedef typename field_type::number_type number_type;
70  typedef typename field_type::value_type value_type;
71 
72  constexpr static const std::size_t key_bits = field_type::modulus_bits;
73  typedef typename field_type::modulus_type key_type;
74 
75  constexpr static const std::size_t key_schedule_bits = field_type::modulus_bits;
76  typedef typename field_type::modulus_type key_schedule_type;
77 
78  constexpr static const std::size_t signature_bits = field_type::modulus_bits * 2;
79  typedef std::tuple<value_type, value_type> signature_type;
80 
81  inline static bool decrypt(signature_type &res, const number_type &val, const key_schedule_type &key) {
82  const dl_group m_group;
83  fixed_exponent_power_mod m_powermod_x_p;
84  blinder m_blinder;
85  //---------
86  m_group(key.get_group()), m_powermod_x_p(key.get_x(), m_group.get_p()),
87  m_blinder(
88  m_group.p(), rng, [](const number<Backend, ExpressionTemplates> &k) { return k; },
89  [this](const number<Backend, ExpressionTemplates> &k) { return m_powermod_x_p(k); }) {
90  }
91  //---------
92  m_y = m_group.power_g_p(m_x);
93 
94  const size_t p_bytes = m_group.p_bytes();
95 
96  if (msg_len != 2 * p_bytes) {
97  throw std::invalid_argument("ElGamal decryption: Invalid message");
98  }
99 
100  number<Backend, ExpressionTemplates> a(msg, p_bytes);
101  const number<Backend, ExpressionTemplates> b(msg + p_bytes, p_bytes);
102 
103  if (a >= m_group.p() || b >= m_group.get_p()) {
104  throw std::invalid_argument("ElGamal decryption: Invalid message");
105  }
106 
107  a = m_blinder.blind(a);
108 
109  const number<Backend, ExpressionTemplates> r =
110  m_group.multiply_mod_p(m_group.inverse_mod_p(m_powermod_x_p(a)), b);
111 
112  return number<Backend, ExpressionTemplates>::encode_1363(m_blinder.unblind(r), p_bytes);
113  }
114  };
115 
116  template<typename FieldType>
117  struct el_gamal {
118  typedef FieldType field_type;
119 
122  };
123  } // namespace pubkey
124  } // namespace crypto3
125 } // namespace nil
126 
127 #endif
boost::mpl::apply< AccumulatorSet, tag::pubkey< ProcessingMode > >::type::result_type pubkey(const AccumulatorSet &acc)
Definition: accumulators/pubkey.hpp:106
Definition: pair.hpp:31
constexpr static const std::size_t key_schedule_bits
Definition: elgamal.hpp:75
field_type::value_type value_type
Definition: elgamal.hpp:70
field_type::modulus_type key_schedule_type
Definition: elgamal.hpp:76
constexpr static const std::size_t key_bits
Definition: elgamal.hpp:72
std::tuple< value_type, value_type > signature_type
Definition: elgamal.hpp:79
field_type::number_type number_type
Definition: elgamal.hpp:69
FieldType field_type
Definition: elgamal.hpp:67
static bool decrypt(signature_type &res, const number_type &val, const key_schedule_type &key)
Definition: elgamal.hpp:81
constexpr static const std::size_t signature_bits
Definition: elgamal.hpp:78
field_type::modulus_type key_type
Definition: elgamal.hpp:73
field_type::modulus_type key_schedule_type
Definition: elgamal.hpp:43
constexpr static const std::size_t key_schedule_bits
Definition: elgamal.hpp:42
field_type::value_type value_type
Definition: elgamal.hpp:37
constexpr static const std::size_t key_bits
Definition: elgamal.hpp:39
FieldType field_type
Definition: elgamal.hpp:35
field_type::modulus_type key_type
Definition: elgamal.hpp:40
std::tuple< value_type, value_type > signature_type
Definition: elgamal.hpp:46
constexpr static const std::size_t signature_bits
Definition: elgamal.hpp:45
static bool encrypt(const signature_type &val, const key_schedule_type &key)
Definition: elgamal.hpp:48
Definition: elgamal.hpp:117
el_gamal_private_key< field_type > private_key_type
Definition: elgamal.hpp:121
FieldType field_type
Definition: elgamal.hpp:118
el_gamal_public_key< field_type > public_key_type
Definition: elgamal.hpp:120