HElib: src/CModulus.h Source File

HElib 1.0 Implementing Homomorphic Encryption

- Main Page
- Classes
- Files

- File List
- File Members

All Classes Files Functions Variables Friends Pages
CModulus.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 _CModulus_H_
17 #define _CModulus_H_
18
27 #include "PAlgebra.h"
28 #include "bluestein.h"
29 #include "cloned_ptr.h"
30 //NTL_CLIENT
31
36 class CMOD_zz_p {
37 public:
38 typedef long zz;
39 typedef zz_p zp;
40 typedef zz_pX zpx;
41 typedef vec_long zzv;
42 typedef fftRep fftrep;
43 typedef zz_pContext zpContext;
44 typedef zz_pBak zpBak;
45 typedef zz_pXModulus zpxModulus;
46
47 };
48
53 class CMOD_ZZ_p {
54 public:
55 typedef ZZ zz;
56 typedef ZZ_p zp;
57 typedef ZZ_pX zpx;
58 typedef vec_ZZ zzv;
59 typedef FFTRep fftrep;
60 typedef ZZ_pContext zpContext ;
61 typedef ZZ_pBak zpBak;
62 typedef ZZ_pXModulus zpxModulus;
63 };
64
65 #define INJECT_TYPE(type,subtype) typedef typename type::subtype subtype
66
67
88 template <class type>
89 class Cmod {
90 INJECT_TYPE(type,zz);
91 INJECT_TYPE(type,zp);
92 INJECT_TYPE(type,zpx);
93 INJECT_TYPE(type,zzv);
94 INJECT_TYPE(type,fftrep);
95 INJECT_TYPE(type,zpContext);
96 INJECT_TYPE(type,zpBak);
97 INJECT_TYPE(type,zpxModulus);
98
99 zz q; // the modulus
100 zpContext context; // NTL's tables for this modulus
101
102 const PAlgebra* zMStar; // points to the Zm* structure, m is FFT size
103
104 zz m_inv; // m^{-1} mod q
105
106 zz root; // 2m-th root of unity modulo q
107 zz rInv; // root^{-1} mod q
108
109 zpx* powers; // tables for forward FFT
110 mutable Vec<mulmod_precon_t> powers_aux;
111 fftrep* Rb;
112 mutable fftrep_aux Rb_aux;
113 fftrep* Ra;
114
115 zpx* ipowers; // tables for backward FFT
116 mutable Vec<mulmod_precon_t> ipowers_aux;
117 fftrep* iRb;
118 mutable fftrep_aux iRb_aux;
119
120 zpxModulus* phimx; // PhimX modulo q, for faster division w/ remainder
121 zpx* scratch; // temporary space, to satisfy NTL's rules
122
123 // Allocate memory and compute roots
124 void privateInit(const PAlgebra&, const zz& rt);
125
126 void freeSpace()
127 {
128 if (powers!=NULL) { delete powers; powers=NULL; }
129 if (Rb!=NULL) { delete Rb; Rb=NULL; }
130 if (Ra!=NULL) { delete Ra; Ra=NULL; }
131 if (ipowers!=NULL) { delete ipowers; ipowers=NULL; }
132 if (iRb!=NULL) { delete iRb; iRb = NULL; }
133 if (phimx!=NULL) { delete phimx; phimx = NULL; }
134 if (scratch!=NULL) { delete scratch; scratch = NULL; }
135 }
136
137 public:
138
139 // Destructor and constructors
140
141 ~Cmod() { freeSpace(); } // destructor
142
143 // Default constructor
144 Cmod(): zMStar(NULL), powers(NULL), Rb(NULL), Ra(NULL), ipowers(NULL), iRb(NULL),
145 phimx(NULL), scratch(NULL) {}
146
147 Cmod(const Cmod &other):
148 zMStar(NULL),powers(NULL),Rb(NULL),Ra(NULL),ipowers(NULL),iRb(NULL),phimx(NULL),scratch(NULL)
149 { *this = other; }
150
151 // Specify m and q, and optionally also the root
152 Cmod(const PAlgebra &zms, const zz &qq, const zz &rt);
153
154 // Copy operator
155 Cmod& operator=(const Cmod &other);
156
157 // utility methods
158
159 const PAlgebra &getZMStar() const { return *zMStar; }
160 unsigned getM() const { return zMStar->getM(); }
161 unsigned getPhiM() const { return zMStar->getPhiM(); }
162 const zz& getQ() const { return q; }
163 const zz& getRoot() const { return root; }
164 const zpxModulus& getPhimX() const { return *phimx; }
165 zpx& getScratch() const { return *scratch; }
166
168 void restoreModulus() const {context.restore();}
169
170 // FFT routines
171
172 // sets zp context internally
173 void FFT(zzv &y, const ZZX& x) const; // y = FFT(x)
174
175 // expects zp context to be set externally
176 void iFFT(zpx &x, const zzv& y) const; // x = FFT^{-1}(y)
177 };
178
179 typedef Cmod<CMOD_zz_p> Cmodulus;
180 typedef Cmod<CMOD_ZZ_p> CModulus;
181
182 #endif // ifdef _CModulus_H_