Skip to content

Commit a0b8d2e

Browse files
author
Shai Halevi
committed
enable specifying m in Test_IO
1 parent 9edb535 commit a0b8d2e

File tree

1 file changed

+18
-84
lines changed

1 file changed

+18
-84
lines changed

src/Test_IO.cpp

Lines changed: 18 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -49,26 +49,29 @@ int main(int argc, char *argv[])
4949
long c = 2;
5050
long w = 64;
5151
long L = 5;
52+
long mm=0;
5253
amap.arg("p", p, "plaintext base");
5354
amap.arg("r", r, "lifting");
5455
amap.arg("c", c, "number of columns in the key-switching matrices");
56+
amap.arg("m", mm, "cyclotomic index","{31,127,1023}");
5557
amap.parse(argc, argv);
5658

5759
long ptxtSpace = power_long(p,r);
60+
long numTests = (mm==0)? N_TESTS : 1;
5861

59-
FHEcontext* contexts[N_TESTS];
60-
FHESecKey* sKeys[N_TESTS];
61-
Ctxt* ctxts[N_TESTS];
62-
EncryptedArray* eas[N_TESTS];
63-
vector<ZZX> ptxts[N_TESTS];
62+
std::unique_ptr<FHEcontext> contexts[numTests];
63+
std::unique_ptr<FHESecKey> sKeys[numTests];
64+
std::unique_ptr<Ctxt> ctxts[numTests];
65+
std::unique_ptr<EncryptedArray> eas[numTests];
66+
vector<ZZX> ptxts[numTests];
6467

6568
// first loop: generate stuff and write it to cout
6669

6770
// open file for writing
6871
{fstream keyFile("iotest.txt", fstream::out|fstream::trunc);
6972
assert(keyFile.is_open());
70-
for (long i=0; i<N_TESTS; i++) {
71-
long m = ms[i][1];
73+
for (long i=0; i<numTests; i++) {
74+
long m = (mm==0)? ms[i][1] : mm;
7275

7376
cout << "Testing IO: m="<<m<<", p^r="<<p<<"^"<<r<<endl;
7477

@@ -79,25 +82,25 @@ int main(int argc, char *argv[])
7982
vector<long> ords(2);
8083
ords[0] = ms[i][8]; ords[1] = ms[i][9];
8184

82-
if (gens[0]>0)
83-
contexts[i] = new FHEcontext(m, p, r, gens, ords);
85+
if (mm==0 && gens[0]>0)
86+
contexts[i].reset(new FHEcontext(m, p, r, gens, ords));
8487
else
85-
contexts[i] = new FHEcontext(m, p, r);
88+
contexts[i].reset(new FHEcontext(m, p, r));
8689
contexts[i]->zMStar.printout();
8790

8891
buildModChain(*contexts[i], L, c); // Set the modulus chain
89-
if (i==N_TESTS-1) contexts[i]->makeBootstrappable(mvec);
92+
if (mm==0 && m==1023) contexts[i]->makeBootstrappable(mvec);
9093

9194
// Output the FHEcontext to file
9295
writeContextBase(keyFile, *contexts[i]);
9396
writeContextBase(cout, *contexts[i]);
9497
keyFile << *contexts[i] << endl;
9598

96-
sKeys[i] = new FHESecKey(*contexts[i]);
99+
sKeys[i].reset(new FHESecKey(*contexts[i]));
97100
const FHEPubKey& publicKey = *sKeys[i];
98101
sKeys[i]->GenSecKey(w,ptxtSpace); // A Hamming-weight-w secret key
99102
addSome1DMatrices(*sKeys[i]);// compute key-switching matrices that we need
100-
eas[i] = new EncryptedArray(*contexts[i]);
103+
eas[i].reset(new EncryptedArray(*contexts[i]));
101104

102105
long nslots = eas[i]->size();
103106

@@ -111,7 +114,7 @@ int main(int argc, char *argv[])
111114
ZZX poly = RandPoly(0,to_ZZ(p2r)); // choose a random constant polynomial
112115
eas[i]->decode(ptxts[i], poly);
113116

114-
ctxts[i] = new Ctxt(publicKey);
117+
ctxts[i].reset(new Ctxt(publicKey));
115118
eas[i]->encrypt(*ctxts[i], publicKey, ptxts[i]);
116119
eas[i]->decrypt(*ctxts[i], *sKeys[i], b);
117120
assert(ptxts[i].size() == b.size());
@@ -137,7 +140,7 @@ int main(int argc, char *argv[])
137140

138141
// open file for read
139142
{fstream keyFile("iotest.txt", fstream::in);
140-
for (long i=0; i<N_TESTS; i++) {
143+
for (long i=0; i<numTests; i++) {
141144

142145
// Read context from file
143146
unsigned long m1, p1, r1;
@@ -252,72 +255,3 @@ int main(int argc, char *argv[])
252255
}}
253256
unlink("iotest.txt"); // clean up before exiting
254257
}
255-
256-
#if 0
257-
/************************ OLD CODE ************************/
258-
#include <cstdlib>
259-
#include <string>
260-
#include <sstream>
261-
#include <fstream>
262-
#include <iostream>
263-
#include "FHE.h"
264-
#include "EncryptedArray.h"
265-
// #include "parameters.h" //this has the function get_m_c
266-
int main()
267-
{
268-
string * tmp_ptr;
269-
long LLL, DDD, KKK ;
270-
long m;
271-
long ccc;
272-
FHEcontext * context_ptr;
273-
FHESecKey * fhekey_ptr;
274-
EncryptedArray * ea_ptr;
275-
276-
LLL = 682; DDD=12; KKK=80;
277-
/*
278-
pair<long, long> m_c = get_m_c(LLL, DDD, KKK);
279-
m = m_c.first;
280-
ccc = m_c.second;
281-
*/
282-
m = 15709;
283-
ccc = 3;
284-
context_ptr = new FHEcontext(m, 2, 1);
285-
buildModChain(*context_ptr, DDD, ccc);
286-
fhekey_ptr = new FHESecKey(*context_ptr);
287-
fhekey_ptr->clear();
288-
fhekey_ptr->GenSecKey(64,2);
289-
addSome1DMatrices(*fhekey_ptr);
290-
const FHEPubKey & pub_key = *fhekey_ptr;
291-
ZZX G;
292-
G = ZZX(1,1);
293-
ea_ptr = new EncryptedArray(*context_ptr, G);
294-
// Test I/O, write context and public key, then try to read them back
295-
cout << "KEY\n";
296-
cout <<"L= " <<LLL<< " D= " << DDD<< " K= " << KKK << endl<< flush;
297-
{
298-
stringstream s1;
299-
writeContextBase(s1, *context_ptr);
300-
s1 << *context_ptr;
301-
string s2 = s1.str();
302-
cout << s2 << endl; // output context also to external cout
303-
// Read back context from input stream (s3)
304-
unsigned long m1, p1, r1;
305-
stringstream s3(s2);
306-
readContextBase(s3, m1, p1, r1);
307-
FHEcontext c1(m1, p1, r1);
308-
s3 >> c1;
309-
assert(c1 == *context_ptr);
310-
}
311-
{
312-
stringstream s1;
313-
s1 << pub_key;
314-
string s2 = s1.str();
315-
cout << s2 <<"\nENDKEY" << endl; // output public key also to external cout
316-
// Read back cpublic key from input stream (s3)
317-
stringstream s3(s2);
318-
FHEPubKey pk1(*context_ptr);
319-
s3 >> pk1;
320-
assert(pk1 == pub_key);
321-
}
322-
}
323-
#endif

0 commit comments

Comments
 (0)