SeComLib
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Macros Pages
ciphertext_base.hpp
Go to the documentation of this file.
1 /*
2 SeComLib
3 Copyright 2012-2013 TU Delft, Information Security & Privacy Lab (http://isplab.tudelft.nl/)
4 
5 Contributors:
6 Inald Lagendijk (R.L.Lagendijk@TUDelft.nl)
7 Mihai Todor (todormihai@gmail.com)
8 Thijs Veugen (P.J.M.Veugen@tudelft.nl)
9 Zekeriya Erkin (z.erkin@tudelft.nl)
10 
11 Licensed under the Apache License, Version 2.0 (the "License");
12 you may not use this file except in compliance with the License.
13 You may obtain a copy of the License at
14 
15 http://www.apache.org/licenses/LICENSE-2.0
16 
17 Unless required by applicable law or agreed to in writing, software
18 distributed under the License is distributed on an "AS IS" BASIS,
19 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 See the License for the specific language governing permissions and
21 limitations under the License.
22 */
29 #ifndef CIPHERTEXT_BASE_IMPLEMENTATION_GUARD
30 #define CIPHERTEXT_BASE_IMPLEMENTATION_GUARD
31 
32 namespace SeComLib {
33 namespace Core {
37  template <typename T_CiphertextImpl>
39  }
40 
45  template <typename T_CiphertextImpl>
46  CiphertextBase<T_CiphertextImpl>::CiphertextBase (const std::shared_ptr<BigInteger> &encryptionModulus) : encryptionModulus(encryptionModulus) {
47  }
48 
54  template <typename T_CiphertextImpl>
55  CiphertextBase<T_CiphertextImpl>::CiphertextBase (const BigInteger &data, const std::shared_ptr<BigInteger> &encryptionModulus) : data(data), encryptionModulus(encryptionModulus) {
56  }
57 
62  template <typename T_CiphertextImpl>
64  if (!this->encryptionModulus) {
65  throw std::runtime_error("This operation requires the encryption modulus.");
66  }
67 
68  T_CiphertextImpl output(this->data.GetInverseModN(*this->encryptionModulus), this->encryptionModulus);
69 
70  return output;
71  }
72 
78  template <typename T_CiphertextImpl>
79  T_CiphertextImpl CiphertextBase<T_CiphertextImpl>::operator+ (const T_CiphertextImpl &input) const {
80  if (!this->encryptionModulus) {
81  throw std::runtime_error("This operation requires the encryption modulus.");
82  }
83 
84  T_CiphertextImpl output((this->data * input.data) % (*this->encryptionModulus), this->encryptionModulus);
85 
86  return output;
87  }
88 
94  template <typename T_CiphertextImpl>
95  T_CiphertextImpl CiphertextBase<T_CiphertextImpl>::operator- (const T_CiphertextImpl &input) const {
96  if (!this->encryptionModulus) {
97  throw std::runtime_error("This operation requires the encryption modulus.");
98  }
99 
100  T_CiphertextImpl output((this->data * input.data.GetInverseModN(*this->encryptionModulus)) % (*this->encryptionModulus), this->encryptionModulus);
101 
102  return output;
103  }
104 
115  template <typename T_CiphertextImpl>
116  template <typename T_DataType>
117  T_CiphertextImpl CiphertextBase<T_CiphertextImpl>::operator* (const T_DataType &input) const {
118  if (!this->encryptionModulus) {
119  throw std::runtime_error("This operation requires the encryption modulus.");
120  }
121 
122  if (input == 0) {
123  throw std::runtime_error("The plaintext term should not be 0.");
124  }
125 
126  T_CiphertextImpl output(this->data.GetPowModN(input, *this->encryptionModulus), this->encryptionModulus);
127 
128  return output;
129  }
130 }//namespace Core
131 }//namespace SeComLib
132 
133 #endif//CIPHERTEXT_BASE_IMPLEMENTATION_GUARD
T_CiphertextImpl operator-() const
Homomorphic negation unary operator.
T_CiphertextImpl operator+(const T_CiphertextImpl &input) const
Homomorphic addition binary operator.
T_CiphertextImpl operator*(const T_DataType &input) const
Homomorphic multiplication binary operator.
CiphertextBase()
Default constructor.