1 // @file nullscheme-impl.cpp - template instantiations and methods for the NULL
2 // scheme
3 // @author TPOC: contact@palisade-crypto.org
4 //
5 // @copyright Copyright (c) 2019, New Jersey Institute of Technology (NJIT)
6 // All rights reserved.
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions are met:
9 // 1. Redistributions of source code must retain the above copyright notice,
10 // this list of conditions and the following disclaimer.
11 // 2. Redistributions in binary form must reproduce the above copyright notice,
12 // this list of conditions and the following disclaimer in the documentation
13 // and/or other materials provided with the distribution. THIS SOFTWARE IS
14 // PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
15 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
16 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
17 // EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
18 // INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
21 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
23 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 
25 #include "cryptocontext.h"
26 #include "scheme/null/nullscheme.h"
27 
28 namespace lbcrypto {
29 
30 template <>
EvalMult(ConstCiphertext<Poly> ciphertext1,ConstCiphertext<Poly> ciphertext2) const31 Ciphertext<Poly> LPAlgorithmSHENull<Poly>::EvalMult(
32     ConstCiphertext<Poly> ciphertext1,
33     ConstCiphertext<Poly> ciphertext2) const {
34   Ciphertext<Poly> newCiphertext = ciphertext1->CloneEmpty();
35 
36   const Poly& c1 = ciphertext1->GetElement();
37   const Poly& c2 = ciphertext2->GetElement();
38 
39   const auto ptm = ciphertext1->GetCryptoParameters()->GetPlaintextModulus();
40 
41   Poly cResult = ElementNullSchemeMultiply(c1, c2, ptm);
42 
43   newCiphertext->SetElement(cResult);
44 
45   return newCiphertext;
46 }
47 
48 template <>
EvalMult(ConstCiphertext<Poly> ciphertext1,ConstPlaintext plaintext) const49 Ciphertext<Poly> LPAlgorithmSHENull<Poly>::EvalMult(
50     ConstCiphertext<Poly> ciphertext1, ConstPlaintext plaintext) const {
51   Ciphertext<Poly> newCiphertext = ciphertext1->CloneEmpty();
52 
53   const Poly& c1 = ciphertext1->GetElement();
54   const Poly& c2 = plaintext->GetElement<Poly>();
55 
56   const auto ptm = ciphertext1->GetCryptoParameters()->GetPlaintextModulus();
57 
58   Poly cResult = ElementNullSchemeMultiply(c1, c2, ptm);
59 
60   newCiphertext->SetElement(cResult);
61 
62   return newCiphertext;
63 }
64 
65 template class LPCryptoParametersNull<Poly>;
66 template class LPPublicKeyEncryptionSchemeNull<Poly>;
67 template class LPAlgorithmNull<Poly>;
68 template class LPAlgorithmParamsGenNull<Poly>;
69 template class LPAlgorithmSHENull<Poly>;
70 template class LPLeveledSHEAlgorithmNull<Poly>;
71 
72 template <>
EvalMult(ConstCiphertext<NativePoly> ciphertext1,ConstCiphertext<NativePoly> ciphertext2) const73 Ciphertext<NativePoly> LPAlgorithmSHENull<NativePoly>::EvalMult(
74     ConstCiphertext<NativePoly> ciphertext1,
75     ConstCiphertext<NativePoly> ciphertext2) const {
76   Ciphertext<NativePoly> newCiphertext = ciphertext1->CloneEmpty();
77 
78   const NativePoly& c1 = ciphertext1->GetElement();
79   const NativePoly& c2 = ciphertext2->GetElement();
80 
81   const auto ptm = ciphertext1->GetCryptoParameters()->GetPlaintextModulus();
82 
83   NativePoly cResult = ElementNullSchemeMultiply(c1, c2, ptm);
84 
85   newCiphertext->SetElement(cResult);
86 
87   return newCiphertext;
88 }
89 
90 template <>
EvalMult(ConstCiphertext<NativePoly> ciphertext1,ConstPlaintext plaintext) const91 Ciphertext<NativePoly> LPAlgorithmSHENull<NativePoly>::EvalMult(
92     ConstCiphertext<NativePoly> ciphertext1, ConstPlaintext plaintext) const {
93   Ciphertext<NativePoly> newCiphertext = ciphertext1->CloneEmpty();
94 
95   const NativePoly& c1 = ciphertext1->GetElement();
96   const NativePoly& c2 = plaintext->GetElement<NativePoly>();
97 
98   const auto ptm = ciphertext1->GetCryptoParameters()->GetPlaintextModulus();
99 
100   NativePoly cResult = ElementNullSchemeMultiply(c1, c2, ptm);
101 
102   newCiphertext->SetElement(cResult);
103 
104   return newCiphertext;
105 }
106 
107 template class LPCryptoParametersNull<NativePoly>;
108 template class LPPublicKeyEncryptionSchemeNull<NativePoly>;
109 template class LPAlgorithmNull<NativePoly>;
110 template class LPAlgorithmParamsGenNull<NativePoly>;
111 template class LPAlgorithmSHENull<NativePoly>;
112 template class LPLeveledSHEAlgorithmNull<NativePoly>;
113 
114 template <>
EvalMult(ConstCiphertext<DCRTPoly> ciphertext1,ConstCiphertext<DCRTPoly> ciphertext2) const115 Ciphertext<DCRTPoly> LPAlgorithmSHENull<DCRTPoly>::EvalMult(
116     ConstCiphertext<DCRTPoly> ciphertext1,
117     ConstCiphertext<DCRTPoly> ciphertext2) const {
118   Ciphertext<DCRTPoly> newCiphertext = ciphertext1->CloneEmpty();
119 
120   const DCRTPoly& c1 = ciphertext1->GetElement();
121   const DCRTPoly& c2 = ciphertext2->GetElement();
122 
123   const vector<PolyType>& c1e = c1.GetAllElements();
124   const vector<PolyType>& c2e = c2.GetAllElements();
125 
126   const auto ptm = ciphertext1->GetCryptoParameters()->GetPlaintextModulus();
127 
128   vector<PolyType> mResults;
129 
130   for (size_t i = 0; i < c1.GetNumOfElements(); i++) {
131     PolyType v = ElementNullSchemeMultiply(c1e.at(i), c2e.at(i), ptm);
132     mResults.push_back(std::move(v));
133   }
134 
135   DCRTPoly cResult(mResults);
136 
137   newCiphertext->SetElement(std::move(cResult));
138 
139   return newCiphertext;
140 }
141 
142 template <>
EvalMult(ConstCiphertext<DCRTPoly> ciphertext1,ConstPlaintext plaintext) const143 Ciphertext<DCRTPoly> LPAlgorithmSHENull<DCRTPoly>::EvalMult(
144     ConstCiphertext<DCRTPoly> ciphertext1, ConstPlaintext plaintext) const {
145   Ciphertext<DCRTPoly> newCiphertext = ciphertext1->CloneEmpty();
146 
147   const DCRTPoly& c1 = ciphertext1->GetElement();
148   const DCRTPoly& c2 = plaintext->GetElement<DCRTPoly>();
149 
150   const vector<PolyType>& c1e = c1.GetAllElements();
151   const vector<PolyType>& c2e = c2.GetAllElements();
152 
153   const auto ptm = ciphertext1->GetCryptoParameters()->GetPlaintextModulus();
154 
155   vector<PolyType> mResults;
156 
157   for (size_t i = 0; i < c1.GetNumOfElements(); i++) {
158     PolyType v = ElementNullSchemeMultiply(c1e.at(i), c2e.at(i), ptm);
159     mResults.push_back(std::move(v));
160   }
161 
162   DCRTPoly cResult(mResults);
163 
164   newCiphertext->SetElement(std::move(cResult));
165 
166   return newCiphertext;
167 }
168 
169 template class LPCryptoParametersNull<DCRTPoly>;
170 template class LPPublicKeyEncryptionSchemeNull<DCRTPoly>;
171 template class LPAlgorithmNull<DCRTPoly>;
172 template class LPAlgorithmParamsGenNull<DCRTPoly>;
173 template class LPAlgorithmSHENull<DCRTPoly>;
174 template class LPLeveledSHEAlgorithmNull<DCRTPoly>;
175 
176 }  // namespace lbcrypto
177