HElib  1.0
Implementing Homomorphic Encryption
 All Classes Files Functions Variables Friends Pages
FHEContext.h
Go to the documentation of this file.
1 /* Copyright (C) 2012,2013 IBM Corp.
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 2 of the License, or
5  * (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10  * See the GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License along
13  * with this program; if not, write to the Free Software Foundation, Inc.,
14  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15  */
16 #ifndef _FHEcontext_H_
17 #define _FHEcontext_H_
18 
22 #include <NTL/xdouble.h>
23 #include "PAlgebra.h"
24 #include "CModulus.h"
25 #include "IndexSet.h"
26 
39 long FindM(long k, long L, long c, long p, long d, long s, long chosen_m, bool verbose=false);
40 
45 class FHEcontext {
46  vector<Cmodulus> moduli; // Cmodulus objects for the different primes
47  // This is private since the implementation assumes that the list of
48  // primes only grows and no prime is ever modified or removed.
49 
50 public:
51  // FHEContext is meant for convenience, not encapsulation: Most data
52  // members are public and can be initialized by the application program.
53 
56 
59 
61  xdouble stdev;
62 
73 
79 
96  vector<IndexSet> digits; // digits of ctxt/columns of key-switching matrix
97 
98  // Constructors must ensure that alMod points to zMStar
99 
100  // constructor
101  FHEcontext(unsigned m, unsigned p, unsigned r): zMStar(m, p), alMod(zMStar, r)
102  { stdev=3.2; }
103 
104  bool operator==(const FHEcontext& other) const;
105  bool operator!=(const FHEcontext& other) const { return !(*this==other); }
106 
108  long ithPrime(unsigned i) const
109  { return (i<moduli.size())? moduli[i].getQ() :0; }
110 
112  const Cmodulus& ithModulus(unsigned i) const { return moduli[i]; }
113 
115  long numPrimes() const { return moduli.size(); }
116 
118  bool isZeroDivisor(const ZZ& num) const {
119  for (unsigned i=0; i<moduli.size(); i++)
120  if (divide(num,moduli[i].getQ())) return true;
121  return false;
122  }
123 
125  bool inChain(long p) const {
126  for (unsigned i=0; i<moduli.size(); i++)
127  if (p==moduli[i].getQ()) return true;
128  return false;
129  }
130 
133  void productOfPrimes(ZZ& p, const IndexSet& s) const;
134  ZZ productOfPrimes(const IndexSet& s) const {
135  ZZ p;
136  productOfPrimes(p,s);
137  return p;
138  }
140 
141  // FIXME: run-time error when ithPrime(i) returns 0
143  double logOfPrime(unsigned i) const { return log(ithPrime(i)); }
144 
146  double logOfProduct(const IndexSet& s) const {
147  if (s.last() >= numPrimes())
148  Error("FHEContext::logOfProduct: IndexSet has too many rows");
149 
150  double ans = 0.0;
151  for (long i = s.first(); i <= s.last(); i = s.next(i))
152  ans += logOfPrime(i);
153  return ans;
154  }
155 
157  void AddPrime(long p, bool special);
158 
159 
161 
194 
195  friend void writeContextBase(ostream& str, const FHEcontext& context);
196 
198  friend ostream& operator<< (ostream &str, const FHEcontext& context);
199 
201  friend void readContextBase(istream& str, unsigned& m, unsigned& p, unsigned& r);
202 
204  friend istream& operator>> (istream &str, FHEcontext& context);
206 };
207 
209 void writeContextBase(ostream& s, const FHEcontext& context);
211 void readContextBase(istream& s, unsigned& m, unsigned& p, unsigned& r);
212 
213 // VJS: compiler seems to need these declarations out here...wtf...
214 
216 
217 
220 double AddPrimesBySize(FHEcontext& context, double totalSize,
221  bool special=false);
222 
225 double AddPrimesByNumber(FHEcontext& context, long nPrimes,
226  long startAt=1,
227  bool special=false);
228 
230 void buildModChain(FHEcontext &context, long nLvls, long c=3);
232 extern FHEcontext* activeContext; // Points to the "current" context
233 #endif // ifndef _FHEcontext_H_