SeComLib
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Macros Pages
secure_svm.h
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 SECURE_SVM_HEADER_GUARD
30 #define SECURE_SVM_HEADER_GUARD
31 
32 //include our headers
33 #include "utils/config.h"
34 #include "core/big_integer.h"
35 #include "core/paillier.h"
36 
37 //include 3rd party libraries
38 #include "libsvm/svm.h"
39 #include "boost/assign.hpp"
40 
41 //include C++ libraries
42 #include <iostream>
43 #include <cmath>
44 #include <stdexcept>
45 #include <fstream>
46 #include <vector>
47 #include <map>
48 
49 namespace SeComLib {
50 using namespace Core;
51 
52 namespace SecureRecommendations {
53  //forward-declare required classes
54  class Server;
55 
59  class SecureSvm {
60  public:
62  typedef std::vector<Paillier::Ciphertext> EncryptedVector;
63 
66 
68  enum KernelTypes {linear, homogeneousPolynomial, inhomogeneousPolynomial, inverseQuadraticRBF};
69 
71  SecureSvm (const std::string &directoryPath, const std::string &modelFile, const PaillierPublicKey &publicKey, const std::weak_ptr<const Server> &server);
72 
74  ~SecureSvm ();
75 
77  static KernelTypes GetKernel (const std::string &input);
78 
80  const std::string &GetUnsafeClasses () const;
81 
83  Paillier::Ciphertext Predict (const SecureSvm::EncryptedVector &x, const SecureSvm::EncryptedVector &xx = SecureSvm::nullVector, const SecureSvm::EncryptedVector &xSquared = SecureSvm::nullVector) const;
84 
85  private:
88 
90  std::weak_ptr<const Server> server;
91 
93  static const std::map<std::string, KernelTypes> kernelTypesMap;
94 
96  typedef std::vector<BigInteger> ModelVector;
97 
100 
102  std::string modelFileName;
103 
106  struct svm_model *model;
107 
110 
113  BigInteger svWeightScaling;
114 
116  unsigned short minimumAiDecimalDigits;
117 
120 
123 
126 
128  BigInteger gammaScaling;
129 
132 
134  std::vector<ModelVector> sMatrix;
135 
137  std::vector<ModelVector> twoGammaSMatrix;
138 
140  std::vector<ModelVector> minusTwoSMatrix;
141 
143  std::vector<ModelVector> twoGammaSquaredSSMatrix;
144 
146  std::vector<EncryptedVector> encryptedSSquaredMatrix;
147 
149  std::vector<BigInteger> aVector;
150 
153 
155  BigInteger scaledGamma;
156 
159 
162 
165 
168 
170  void preprocessData ();
171 
173  Paillier::Ciphertext linearKernel (const SecureSvm::EncryptedVector &x, const SecureSvm::ModelVector &s) const;
174 
176  Paillier::Ciphertext homogeneousPolynomialKernel (const SecureSvm::EncryptedVector &xx, const SecureSvm::ModelVector &twoGammaSquaredSS) const;
177 
179  Paillier::Ciphertext inhomogeneousPolynomialKernel (const SecureSvm::EncryptedVector &x, const SecureSvm::EncryptedVector &xx, const SecureSvm::ModelVector &twoGammaS, const SecureSvm::ModelVector &twoGammaSquaredSS) const;
180 
182  Paillier::Ciphertext computeInverseQuadraticRbfKernelDenominator (const SecureSvm::EncryptedVector &x, const SecureSvm::EncryptedVector &xSquared, const SecureSvm::ModelVector &minusTwoS, const SecureSvm::EncryptedVector &encryptedSSquared) const;
183 
185  SecureSvm (SecureSvm const &);
186 
188  SecureSvm operator= (SecureSvm const &);
189  };
190 }//namespace SecureRecommendations
191 }//namespace SeComLib
192 
193 #endif//SECURE_SVM_HEADER_GUARD
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
Paillier::Ciphertext encryptedZero
Contains [0], used for optimization purposes.
Definition: secure_svm.h:158
std::vector< ModelVector > sMatrix
Matrix which stores the scaled SVM model vectors.
Definition: secure_svm.h:134
Defines BigInteger.
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
Definition of class Config.
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
Implementation of the public-key Paillier Cryptosystem.
Definition: paillier.h:103
KernelTypes
Types of implemented kernels.
Definition: secure_svm.h:68
Secure Support Vector Machine algorithm.
Definition: secure_svm.h:59
std::string modelFileName
The name of the model file.
Definition: secure_svm.h:102
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
Definition of class Paillier.
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
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
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