SeComLib
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Macros Pages
secure_svm.cpp
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 */
30 #include "secure_svm.h"
31 //avoid circular includes
32 #include "server.h"
33 
34 namespace SeComLib {
35 namespace SecureRecommendations {
40 
44  const std::map<std::string, SecureSvm::KernelTypes> SecureSvm::kernelTypesMap = boost::assign::map_list_of("linear", SecureSvm::linear)
45  ("homogeneous_poly", SecureSvm::homogeneousPolynomial)
46  ("inhomogeneous_poly", SecureSvm::inhomogeneousPolynomial)
47  ("rbf", SecureSvm::inverseQuadraticRBF);
48 
61  SecureSvm::SecureSvm (const std::string &directoryPath, const std::string &modelFile, const PaillierPublicKey &publicKey, const std::weak_ptr<const Server> &server) : cryptoProvider(publicKey), server(server), modelFileName(modelFile), model(NULL) {
62  //set configuration parameters
63  this->featureScalingFactor = BigInteger(10).Pow(Utils::Config::GetInstance().GetParameter<unsigned long>("SecureRecommendations.Svm.minimumFeatureDecimalDigits"));
64  this->minimumAiDecimalDigits = Utils::Config::GetInstance().GetParameter<unsigned short>("SecureRecommendations.Svm.minimumAiDecimalDigits");
65 
66  //set the kernel
67  this->kernel = SecureSvm::GetKernel(Utils::Config::GetInstance().GetParameter<std::string>("SecureRecommendations.kernel"));
68 
69  //set the safety block models specific parameters
70  this->safetyModelFilePrefix = Utils::Config::GetInstance().GetParameter<std::string>("SecureRecommendations.Server.SafetyBlock.modelFilePrefix");
71 
72  //parse the unsafe classes for the safety models
73  if (std::string::npos != this->modelFileName.find(this->safetyModelFilePrefix)) {
74  std::vector<unsigned short> unsafeClasses;
75  //iterate over each class (digit) in the file name
76  for (size_t i = this->safetyModelFilePrefix.size(); i < this->modelFileName.find("."); ++i) {
77  //the difference between any digit and '0' will yield the desired numeric value
78  unsafeClasses.emplace_back(this->modelFileName[i] - '0');
79  }
80 
81  std::sort(unsafeClasses.begin(), unsafeClasses.end());
82 
83  //convert the ordered unsafeClasses vector to a stringstream
84  std::stringstream unsafeClassesStream;
85  for (std::vector<unsigned short>::const_iterator unsafeClassesIterator = unsafeClasses.begin(); unsafeClassesIterator != unsafeClasses.end(); ++unsafeClassesIterator) {
86  unsafeClassesStream << *unsafeClassesIterator;
87  }
88  this->safetyModelUnsafeClasses = unsafeClassesStream.str();
89  }
90 
91  //get the inverse quadratic RBF kernel configuration parameters
92  if (SecureSvm::inverseQuadraticRBF == this->kernel) {
93  this->inverseQuadraticRbfKernelRelevantDigits = Utils::Config::GetInstance().GetParameter<unsigned short>("SecureRecommendations.Svm.inverseQuadraticRbfKernelRelevantDigits");
94 
95  unsigned long blindingFactorSize = Utils::Config::GetInstance().GetParameter<unsigned long>("SecureRecommendations.Server.blindingFactorSize");
96  this->blindingFactorScaling = BigInteger(2).Pow(blindingFactorSize + 1);//the maximum value of the blinding may have blindingFactorSize + 1 bits
97  }
98 
99  std::string modelFilePath = directoryPath + this->modelFileName;
100 
101  std::ifstream fileStream(modelFilePath);
102 
103  //test if the file exists and can be accessed
104  if (!fileStream.good()) {
105  throw std::runtime_error("Can't open the SVM model file.");
106  }
107 
109  if (SecureSvm::linear != this->kernel) {
110  //we must read the gamma parameter from the file as a string, to determine the scaling that needs to be applied to it, such that it ends up being > 0 and we don't loose precision
111  std::string line;
112  bool foundGamma = false;
113  while (std::getline(fileStream, line)) {
114  if (std::string::npos != line.find("gamma")) {
115  //we only scale gamma if it has a radix point
116  if (std::string::npos != line.find('.')) {
117  std::string gammaValueString = line.substr(line.find(' '));
118  std::stringstream gammaValueStream; gammaValueStream << gammaValueString;
119  double gammaValue; gammaValueStream >> gammaValue;
120 
121  //if gamma has decimals, then it *must* be a negative power of 2...
122  double exponent = std::log(gammaValue) / std::log(2.0);
123  if (exponent > 0) throw std::runtime_error("Unexpected gamma value detected.");
124 
125  //we want to compute the scaling such that gamma * scaling = 1
126  //first, we round the negative number towards the closest integer
127  this->gammaScaling = BigInteger(2).Pow(static_cast<unsigned long>(std::abs(std::ceil(exponent - 0.5))));
128  }
129  else {
130  //gamma is already an integer
131  this->gammaScaling = 1;
132  }
133 
134  foundGamma = true;
135  break;
136  }
137  }
138  //sanity check
139  if (!foundGamma)
140  throw std::runtime_error("Can't find gamma in the model file.");
141  }
142 
143  //close the file stream
144  fileStream.close();
145 
146  //use the libsvm library to parse the model file
147  this->model = svm_load_model(modelFilePath.c_str());
148 
149  if (NULL == this->model) {
150  throw std::runtime_error("Invalid model file.");
151  }
152 
153  this->preprocessData();
154 
155  std::cout << "Loaded " << modelFile << "; nSV: " << this->model->l << std::endl;
156  }
157 
162  svm_free_and_destroy_model(&this->model);
163  }
164 
170  SecureSvm::KernelTypes SecureSvm::GetKernel (const std::string &input) {
171  std::map<std::string, KernelTypes>::const_iterator iterator = SecureSvm::kernelTypesMap.find(input);
172 
173  //if the specified kernel name is not found
174  if (SecureSvm::kernelTypesMap.end() == iterator) {
176  throw std::runtime_error("Invalid kernel name received.");
177  }
178 
179  return iterator->second;
180  }
181 
185  const std::string &SecureSvm::GetUnsafeClasses () const {
186  return this->safetyModelUnsafeClasses;
187  }
188 
210  Paillier::Ciphertext output = this->encryptedZero;
211 
212  //stores the values for the inverse quadratic RBF kernel
213  SecureSvm::EncryptedVector inverseQuadraticRbfKernelValues;
214 
215  //iterate over the model rows (vectors)
216  for (size_t i = 0; i < this->aVector.size(); ++i) {
217  Paillier::Ciphertext encryptedKernelValue;
218 
219  //compute kernel
220  switch (this->kernel) {
221  case SecureSvm::linear:
222  encryptedKernelValue = this->linearKernel(x, this->sMatrix[i]);
223  break;
224  case SecureSvm::homogeneousPolynomial:
225  encryptedKernelValue = this->homogeneousPolynomialKernel(xx, this->twoGammaSquaredSSMatrix[i]);
226  break;
227  case SecureSvm::inhomogeneousPolynomial:
228  encryptedKernelValue = this->inhomogeneousPolynomialKernel(x, xx, this->twoGammaSMatrix[i], this->twoGammaSquaredSSMatrix[i]);
229  break;
230  case SecureSvm::inverseQuadraticRBF:
232  inverseQuadraticRbfKernelValues.emplace_back(this->computeInverseQuadraticRbfKernelDenominator(x, xSquared, this->minusTwoSMatrix[i], encryptedSSquaredMatrix[i]));
233  break;
234  default:
236  throw std::runtime_error("Invalid kernel type.");
237  }
238 
240 
241  //compute this separately for the inverse quadratic RBF kernel
242  if (SecureSvm::inverseQuadraticRBF != this->kernel) {
243  output = output + encryptedKernelValue * this->aVector[i];
244  }
245  }
246 
248  if (SecureSvm::inverseQuadraticRBF == this->kernel) {
250 
251  //debug
252  //std::cout << "numerator:" << this->inverseQuadraticRbfNumerator.ToString(10) << std::endl;
253 
254  //replace the denominator values with the actual kernel values
255  this->server.lock()->InteractiveSecureDivision(this->inverseQuadraticRbfNumerator, inverseQuadraticRbfKernelValues);
256 
257  for (size_t i = 0; i < this->aVector.size(); ++i) {
258  //debug
259  //this->server.lock()->DebugValue(inverseQuadraticRbfKernelValues[i]);
260  //std::cout << this->aVector[i].ToString(10).c_str() << std::endl;
261 
262  output = output + inverseQuadraticRbfKernelValues[i] * this->aVector[i];
263 
264  //debug
265  //this->server.lock()->DebugValue(this->cryptoProvider.HomomorphicMultiply(inverseQuadraticRbfKernelValues[i], this->aVector[i]));
266  //this->server.lock()->DebugValue(output);
267  }
268 
269  //debug
270  //this->server.lock()->DebugValue(output);
271  //this->server.lock()->DebugValue(this->encryptedB);
272  }
273 
274  output = output + this->encryptedB;
275 
276  //debug
277  //this->server.lock()->DebugValue(output);
278 
279  return output;
280  }
281 
290  if (this->model->label[0] < 0)
291  this->reversedSign = true;
292  else
293  this->reversedSign = false;
294 
296  //model->param.gamma is unitialized in the case of the linear kernel and it's not used in the formula
297  if (SecureSvm::linear != this->kernel) {
298  this->scaledGamma = BigInteger(this->model->param.gamma, this->gammaScaling);
299 
300  //debug
301  //std::cout << this->scaledGamma.ToString(10).c_str() << std::endl;
302  //std::cout << this->gammaScaling.ToString(10).c_str() << std::endl;
303  }
304 
306  this->svWeightScaling = BigInteger(10).Pow(static_cast<unsigned long>(this->minimumAiDecimalDigits));
307 
308  //std::cout << this->svWeightScaling.ToString(10).c_str() << std::endl;
309 
311  double minAbsAi = std::abs(this->model->sv_coef[0][0]);
312  for (unsigned int i = 1; i < static_cast<unsigned int>(this->model->l); ++i) {
313  if (minAbsAi > std::abs(this->model->sv_coef[0][i])) {
314  minAbsAi = std::abs(this->model->sv_coef[0][i]);
315  }
316  }
317  //compute the minimum between min(a_i) and b
318  double minAbsAiB = minAbsAi;
319  if (minAbsAi > std::abs(this->model->rho[0])) {
320  minAbsAiB = std::abs(this->model->rho[0]);
321  }
322 
323  //debug
324  //std::cout << minAbsAiB << std::endl;
325 
327  //iterate over the model rows (vectors)
328  for (unsigned int i = 0; i < static_cast<unsigned int>(this->model->l); ++i) {
330  //double aValue = this->reversedSign ? -this->model->sv_coef[0][i]: this->model->sv_coef[0][i];
331  double aValue = this->reversedSign ? -this->model->sv_coef[0][i] / minAbsAiB : this->model->sv_coef[0][i] / minAbsAiB;
332  this->aVector.emplace_back(BigInteger(aValue, this->svWeightScaling));
333 
334  //debug
335  /*
336  if (this->aVector.back() > this->cryptoProvider.GetMessageSpaceUpperBound() / 2) {
337  std::cout << "-" << (this->cryptoProvider.GetMessageSpaceUpperBound() - this->aVector.back()).ToString(10).c_str() << std::endl;
338  }
339  else
340  std::cout << this->aVector.back().ToString(10).c_str() << std::endl;
341  */
342  }
343 
345 
346  //construct the scaling for b (see the description of each kernel for the detailed formulas)
347  BigInteger bScaling(this->svWeightScaling);//apply the SVM parameter scaling (to compensate for the scaling applied to a_i)
348 
349  //compensate for scalings applied to s_i and x_i
350  switch (this->kernel) {
351  case SecureSvm::linear:
352  //compensate for s_i * x_i
353  bScaling *= this->featureScalingFactor;
354  bScaling *= this->featureScalingFactor;
355  break;
356  case SecureSvm::homogeneousPolynomial:
357  case SecureSvm::inhomogeneousPolynomial:
358  //compensate for (gamma s_i * x_i)^2 or (gamma s_i * x_i + 1)^2
359  bScaling *= this->gammaScaling;
360  bScaling *= this->gammaScaling;
361  bScaling *= this->featureScalingFactor;
362  bScaling *= this->featureScalingFactor;
363  bScaling *= this->featureScalingFactor;
364  bScaling *= this->featureScalingFactor;
365  break;
366  case SecureSvm::inverseQuadraticRBF:
367  //compensate for the digits preserved from the kernel value
368  bScaling *= BigInteger(10).Pow(static_cast<unsigned long>(this->inverseQuadraticRbfKernelRelevantDigits));
369  //compensate for the secure division blinding factor
370  bScaling *= this->blindingFactorScaling;
371  break;
372  default:
374  throw std::runtime_error("Invalid kernel type.");
375  }
376 
377  //remap negative values to the upper part of the crypto provider message space
378  //libsvm negates rho before adding it to the sum. We also need to check if the sign of the prediction needs to be inverted
379  //double bValue = this->reversedSign ? this->model->rho[0] : -(this->model->rho[0]);
380  double bValue = this->reversedSign ? this->model->rho[0] / minAbsAiB : -(this->model->rho[0] / minAbsAiB);
381  BigInteger scaledB = BigInteger(bValue, bScaling);
382 
383  //encrypt scaled b
384  this->encryptedB = this->cryptoProvider.EncryptInteger(scaledB);
385 
386  //debug
387  //std::cout this->svWeightScaling.ToString(10).c_str() << std::endl;
388  //this->server.lock()->DebugValue(this->encryptedB);
389 
391  BigInteger two(2);
392  BigInteger gammaSquared = this->scaledGamma.GetPow(2);
393  //iterate over the model rows (vectors)
394  for (unsigned int row = 0; row < static_cast<unsigned int>(this->model->l); ++row) {
397 
399  SecureSvm::ModelVector tempTwoGammaS;
400 
402  SecureSvm::ModelVector tempMinusTwoS;
403 
407  SecureSvm::ModelVector tempTwoGammaSquaredSS;
408 
410  SecureSvm::EncryptedVector tempEncryptedSSquared;
411 
412  //create an iterator for the model row (vector)
413  svm_node *iterator = this->model->SV[row];
414 
415  //collect all the weights for the current row (vector)
416  //libsvm implementation detail: iterator->index is set to -1 after the last attribute weight in the vector
417  //we assume that no attribute weights are omitted in the model file (the format specifies that 0 valued weights can be omitted)
418  while (-1 != iterator->index) {
419  //scale the current value and load it in a buffer variable
420  BigInteger tempSValue(iterator->value, this->featureScalingFactor);
421 
422  //we need to have values greater than 0 in order to ensure security
423  if (tempSValue == 0) {
424  tempSValue = 1;
425  }
426 
427  //debug
428  //std::cout << tempSValue.ToString(10) << std::endl;
429 
430  tempS.push_back(tempSValue);
431 
432  ++iterator;
433  }
434 
435  //s matrix is needed only for the linear kernel
436  if (SecureSvm::linear == this->kernel) {
437  //populate the s matrix
438  this->sMatrix.emplace_back(tempS);
439  }
440 
441  //compute auxiliary matrices for the polynomial and RBF kernels
442  if (SecureSvm::linear != this->kernel) {
443  for (size_t i = 0; i < tempS.size(); ++i) {
444  //twoGammaSquaredSS matrix is not required for the inverse quadratic RBF kernel
445  if (SecureSvm::inverseQuadraticRBF != this->kernel) {
446  for (size_t j = i; j < tempS.size(); ++j) {
448  if (i != j) {
449  tempTwoGammaSquaredSS.push_back(two * gammaSquared * tempS[i] * tempS[j]);
450  }
451  else {
452  tempTwoGammaSquaredSS.push_back(gammaSquared * tempS[i] * tempS[j]);
453  }
454  }
455  }
456 
459  if (SecureSvm::inhomogeneousPolynomial == this->kernel) {
460  //get local copy of the current scaled value
461  BigInteger tempSValue = tempS[i];
462 
463  //need to compensate for gamma^2 * x^2 * s^2
464  //we already have gammaScaling * featureScalingFactor, so we add the rest
465  tempSValue *= this->gammaScaling;
466  tempSValue *= this->featureScalingFactor;
467  tempSValue *= this->featureScalingFactor;
468  tempTwoGammaS.push_back(two * this->scaledGamma * tempSValue);
469  }
470 
471  //encrypted sSquared and minusTwoS matrices are required only for the inverse quadratic RBF kernel
472  if (SecureSvm::inverseQuadraticRBF == this->kernel) {
474  tempEncryptedSSquared.push_back(this->cryptoProvider.EncryptInteger(tempS[i].GetPow(2)));
475 
477  tempMinusTwoS.push_back(-two * tempS[i]);//use unary - operator
478  }
479  }
480 
481  //twoGammaSquaredSS matrix is not requred for the inverse quadratic RBF kernel
482  if (SecureSvm::inverseQuadraticRBF != this->kernel) {
483  this->twoGammaSquaredSSMatrix.emplace_back(tempTwoGammaSquaredSS);
484  }
485 
486  //twoGammaS matrix is required for the inhomogeneous polynomial kernel
487  if (SecureSvm::inhomogeneousPolynomial == this->kernel) {
488  this->twoGammaSMatrix.emplace_back(tempTwoGammaS);
489  }
490 
491  //minusTwoS and encryptedSSquared matrices are required for the inverse quadratic RBF kernel
492  if (SecureSvm::inverseQuadraticRBF == this->kernel) {
493  this->minusTwoSMatrix.emplace_back(tempMinusTwoS);
494 
495  this->encryptedSSquaredMatrix.emplace_back(tempEncryptedSSquared);
496  }
497  }
498  }//model rows
499 
501  if (SecureSvm::inhomogeneousPolynomial == this->kernel) {
502  BigInteger one(1);
503  //need to compensate for gamma^2 * x^2 * s^2
504  one *= this->gammaScaling;
505  one *= this->gammaScaling;
506  one *= this->featureScalingFactor;
507  one *= this->featureScalingFactor;
508  one *= this->featureScalingFactor;
509  one *= this->featureScalingFactor;
511  }
512 
513 
514  if (SecureSvm::inverseQuadraticRBF == this->kernel) {
516  BigInteger one(1);
517  //need to compensate for gamma * (x - s)^2
518  one *= this->gammaScaling;
519  one *= this->featureScalingFactor;
520  one *= this->featureScalingFactor;
522 
524  this->inverseQuadraticRbfNumerator = BigInteger(1);
525  //compensate for the random blinding
527  //compensate for gamma * (x - s)^2
531  //preserve the required number of digits after performing division
532  this->inverseQuadraticRbfNumerator *= BigInteger(10).Pow(static_cast<unsigned long>(inverseQuadraticRbfKernelRelevantDigits));
533  }
534 
537  }
538 
552  Paillier::Ciphertext output = this->encryptedZero;
553 
554  for (size_t i = 0; i < s.size(); ++i) {
555  //debug
556  /*
557  this->server.lock()->DebugValue(x[i]);
558  std::cout << s[i].ToString(10) << std::endl;
559  */
560  output = output + x[i] * s[i];
561  }
562 
563  return output;
564  }
565 
581  Paillier::Ciphertext output = this->encryptedZero;
582 
583  for (size_t i = 0; i < xx.size(); ++i) {
584  output = output + xx[i] * twoGammaSquaredSS[i];
585  }
586 
587  #if 0
588  /* previous algorithm with complex index logic. Now, we just do sum(2*(xi xj)(si sj)) if i != j and sum((xi xj)(si sj)) if i==j in the encrypted domain */
589 
590  for (size_t i = 0; i < s.size(); ++i) {
591  for (size_t j = 0; j < s.size(); ++j) {
592  //the x_ix_j matrix is unraveled into a vector, xx, so we need to compute the apropriate index
593  //basically, we need to subtract sum(i - 1) = (i - 1) * i / 2 at each step to determine the index in the vector
594  size_t index;
595  if (i <= j) {
596  //the upper triangle of the x_ix_j matrix
597  index = i * (s.size() - 1) - (i - 1) * i / 2 + j;
598  }
599  else {
600  //the lower triangle of the x_ix_j matrix is symmetric to the upper one, so we remap it there
601  index = j * (s.size() - 1) - (j - 1) * j / 2 + i;
602 
603  }
604 
605  //std::cout << "i: " << i << "; j: " << j << "; index: " << index << std::endl;
606 
607  output = output + xx[index] * ss[index];
608  }
609  }
610  #endif
611 
612  return output;
613  }
614 
631  //leverage the homogeneousPolynomialKernel and linearKernel implementations to simplify the formula
632  return this->homogeneousPolynomialKernel(xx, twoGammaSquaredSS) + this->linearKernel(x, twoGammaS) + this->encryptedScaledOne;
633  }
634 
658  Paillier::Ciphertext encryptedDenominator = this->encryptedZero;
659 
660  for (size_t i = 0; i < x.size(); ++i) {
661  encryptedDenominator = encryptedDenominator + xSquared[i] + x[i] * minusTwoS[i] + encryptedSSquared[i];
662 
663  }
664 
666  //multiply (d, gamma), since gamma is the unencrypted value...
667  encryptedDenominator = this->encryptedScaledOne + encryptedDenominator * this->scaledGamma;
668 
669  return encryptedDenominator;
670  }
671 
672 }//namespace SecureRecommendations
673 }//namespace SeComLib
std::vector< Paillier::Ciphertext > EncryptedVector
Define a vector template specialization for vectors of encrypted data.
Definition: secure_svm.h:62
Paillier cryptoProvider
The crypto provider.
Definition: secure_svm.h:87
unsigned short minimumAiDecimalDigits
The minimum number of relevant decimal digits that should preserve.
Definition: secure_svm.h:116
BigInteger inverseQuadraticRbfNumerator
Scaled 1 - the inverse quadratic RBF kernel numerator.
Definition: secure_svm.h:122
Ciphertext GetEncryptedZero(const bool randomized=true) const
Returns [0].
Paillier::Ciphertext encryptedZero
Contains [0], used for optimization purposes.
Definition: secure_svm.h:158
Paillier::Ciphertext linearKernel(const SecureSvm::EncryptedVector &x, const SecureSvm::ModelVector &s) const
Computes the linear kernel on encrypted data.
Definition: secure_svm.cpp:550
static Config & GetInstance()
Returns a reference to the singleton.
Definition: config.cpp:48
std::vector< ModelVector > sMatrix
Matrix which stores the scaled SVM model vectors.
Definition: secure_svm.h:134
const std::string & GetUnsafeClasses() const
Returns the unsafe classes of safety block SVMs.
Definition: secure_svm.cpp:185
BigInteger scaledGamma
Stores the scaled gamma SVM parameter (1/(number of attributes) by default)
Definition: secure_svm.h:155
static const std::map< std::string, KernelTypes > kernelTypesMap
Map kernel names to the kernelTypes enum.
Definition: secure_svm.h:93
std::vector< BigInteger > aVector
Vector which stores the scaled SVM parameters.
Definition: secure_svm.h:149
SecureSvm::KernelTypes kernel
The SVM kernel type.
Definition: secure_svm.h:99
virtual T_Ciphertext EncryptInteger(const BigInteger &plaintext) const
Encrypt an integer and apply randomization.
Definition of class SecureSvm.
BigInteger gammaScaling
The scaling applied to the SVM gamma parameter.
Definition: secure_svm.h:128
std::string safetyModelFilePrefix
The prefix of the safety block model files.
Definition: secure_svm.h:164
static KernelTypes GetKernel(const std::string &input)
Converts the input string to the proper kernel.
Definition: secure_svm.cpp:170
Paillier::Ciphertext inhomogeneousPolynomialKernel(const SecureSvm::EncryptedVector &x, const SecureSvm::EncryptedVector &xx, const SecureSvm::ModelVector &twoGammaS, const SecureSvm::ModelVector &twoGammaSquaredSS) const
Computes the second degree polynomial kernel on encrypted data.
Definition: secure_svm.cpp:630
KernelTypes
Types of implemented kernels.
Definition: secure_svm.h:68
Paillier::Ciphertext homogeneousPolynomialKernel(const SecureSvm::EncryptedVector &xx, const SecureSvm::ModelVector &twoGammaSquaredSS) const
Computes the second degree polynomial kernel on encrypted data.
Definition: secure_svm.cpp:579
std::string modelFileName
The name of the model file.
Definition: secure_svm.h:102
Paillier::Ciphertext computeInverseQuadraticRbfKernelDenominator(const SecureSvm::EncryptedVector &x, const SecureSvm::EncryptedVector &xSquared, const SecureSvm::ModelVector &minusTwoS, const SecureSvm::EncryptedVector &encryptedSSquared) const
Computes the inverse quadratic RBF kernel d values on encrypted data.
Definition: secure_svm.cpp:657
Paillier::Ciphertext Predict(const SecureSvm::EncryptedVector &x, const SecureSvm::EncryptedVector &xx=SecureSvm::nullVector, const SecureSvm::EncryptedVector &xSquared=SecureSvm::nullVector) const
Computes the prediction for a given set of data.
Definition: secure_svm.cpp:208
std::vector< EncryptedVector > encryptedSSquaredMatrix
Matrix which stores the encrypted scaled SVM model vectors.
Definition: secure_svm.h:146
BigInteger featureScalingFactor
The scaling applied to the test and model vectors and .
Definition: secure_svm.h:109
unsigned short inverseQuadraticRbfKernelRelevantDigits
The number of digits preserved in the inverse quadratic RBF kernel value after performing the divisio...
Definition: secure_svm.h:119
T GetParameter(const std::string &parameter) const
Template method which returns the value of the specified configuration parameter. ...
Definition: config.hpp:41
std::weak_ptr< const Server > server
A reference to the server - required for interactive protocol requests with the client (secure divisi...
Definition: secure_svm.h:90
std::vector< ModelVector > twoGammaSquaredSSMatrix
Matrix containing scaled vector product combinations, , stored as an unraveled upper triangular matri...
Definition: secure_svm.h:143
std::vector< ModelVector > minusTwoSMatrix
Matrix which stores the scaled vectors.
Definition: secure_svm.h:140
Paillier::Ciphertext encryptedScaledOne
[scaled 1], required when computing the inhomogeneous kernel. Precompute it for optimization purposes...
Definition: secure_svm.h:161
std::vector< ModelVector > twoGammaSMatrix
Matrix which stores the scaled vectors.
Definition: secure_svm.h:137
BigInteger blindingFactorScaling
The scaling that needs to be applied to the b parameter in order to compensate for the blinding facto...
Definition: secure_svm.h:125
SecureSvm(const std::string &directoryPath, const std::string &modelFile, const PaillierPublicKey &publicKey, const std::weak_ptr< const Server > &server)
Constructor.
Definition: secure_svm.cpp:61
std::string safetyModelUnsafeClasses
The unsafe classes of the safety block model.
Definition: secure_svm.h:167
bool reversedSign
During training, libsvm uses an internal flag to denote the sign of the first label it encounters...
Definition: secure_svm.h:131
static const EncryptedVector nullVector
Use this to pass a NULL vector to the Predict method;.
Definition: secure_svm.h:65
Definition of class Server.
Paillier::Ciphertext encryptedB
Stores the scaled and encrypted SVM parameter (-rho in libsvm)
Definition: secure_svm.h:152
std::vector< BigInteger > ModelVector
Define an std::vector template specialization for rows of model weights.
Definition: secure_svm.h:96
The public key container structure for the Paillier cryptosystem.
Definition: paillier.h:49
void preprocessData()
Performs scalings and encryptions.
Definition: secure_svm.cpp:287