[go: nahoru, domu]

Skip to content

Commit

Permalink
helib namespace (homenc#217)
Browse files Browse the repository at this point in the history
* Introduced helib namespace to source files

* Forward-declared friend methods where needed

* Added vecToStr function

* Qualified much of the code

* Updated the copyright

* Added using namespace helib to legacy tests

* Qualified everything in the example program

* Removed unecessary externs in legacy test files

* Removed double namespace
  • Loading branch information
Jack L Crawford authored and patrick-schwarz committed Dec 6, 2019
1 parent af309df commit ca0648b
Show file tree
Hide file tree
Showing 141 changed files with 1,520 additions and 1,123 deletions.
16 changes: 8 additions & 8 deletions example_program/helib_example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ int main(int argc, char *argv[]) {

std::cout << "Initialising context object..." << std::endl;
// Intialise context
FHEcontext context(m, p, r);
helib::FHEcontext context(m, p, r);
// Modify the context, adding primes to the modulus chain
std::cout << "Building modulus chain..." << std::endl;
buildModChain(context, bits, c);
Expand All @@ -33,19 +33,19 @@ int main(int argc, char *argv[]) {
// Secret key management
std::cout << "Creating secret key..." << std::endl;
// Create a secret key associated with the context
FHESecKey secret_key(context);
helib::FHESecKey secret_key(context);
// Generate the secret key
secret_key.GenSecKey();
std::cout << "Generating key-switching matrices..." << std::endl;
// Compute key-switching matrices that we need
addSome1DMatrices(secret_key);
helib::addSome1DMatrices(secret_key);

// Public key management
// Set the secret key (upcast: FHESecKey is a subclass of FHEPubKey)
const FHEPubKey& public_key = secret_key;
const helib::FHEPubKey& public_key = secret_key;

// Get the EncryptedArray of the context
const EncryptedArray& ea = *(context.ea);
const helib::EncryptedArray& ea = *(context.ea);

// Get the number of slot (phi(m))
long nslots = ea.size();
Expand All @@ -58,10 +58,10 @@ int main(int argc, char *argv[]) {
ptxt[i] = i;
}
// Print the plaintext
std::cout << "Initial Ptxt: " << ptxt << std::endl;
std::cout << "Initial Ptxt: " << helib::vecToStr(ptxt) << std::endl;

// Create a ciphertext
Ctxt ctxt(public_key);
helib::Ctxt ctxt(public_key);
// Encrypt the plaintext using the public_key
ea.encrypt(ctxt, public_key, ptxt);

Expand All @@ -76,7 +76,7 @@ int main(int argc, char *argv[]) {
ea.decrypt(ctxt, secret_key, decrypted);

// Print the decrypted plaintext
std::cout << "Decrypted Ptxt: " << decrypted << std::endl;
std::cout << "Decrypted Ptxt: " << helib::vecToStr(decrypted) << std::endl;

return 0;
}
6 changes: 5 additions & 1 deletion src/ArgMap.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Copyright (C) 2012-2017 IBM Corp.
/* Copyright (C) 2012-2019 IBM Corp.
* This program is Licensed under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
Expand All @@ -15,6 +15,8 @@
#include <fstream>
#include "ArgMap.h"

namespace helib {

ArgMap& ArgMap::note(const std::string& s) {
docStream << "\t\t " << s << "\n";
return *this;
Expand Down Expand Up @@ -177,3 +179,5 @@ ArgMap& ArgMap::parse(const std::string& filepath) {

return *this;
}

}
6 changes: 5 additions & 1 deletion src/ArgMap.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Copyright (C) 2012-2017 IBM Corp.
/* Copyright (C) 2012-2019 IBM Corp.
* This program is Licensed under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
Expand Down Expand Up @@ -41,6 +41,8 @@
*
**/

namespace helib {

class ArgMap {
private:
/* ArgProcessor: virtual base class */
Expand Down Expand Up @@ -240,4 +242,6 @@ class ArgMap {
std::string doc() const;
};

}

#endif // ifndef HELIB_ARGMAP_H
6 changes: 5 additions & 1 deletion src/BenesNetwork.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Copyright (C) 2012-2017 IBM Corp.
/* Copyright (C) 2012-2019 IBM Corp.
* This program is Licensed under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
Expand All @@ -21,6 +21,8 @@
#include "NumbTh.h"
#include "permutations.h"

namespace helib {

static void
recursiveGeneralBenesInit(long n, long k, long d, long delta_j,
const Permut& perm, const Permut& iperm,
Expand Down Expand Up @@ -316,3 +318,5 @@ bool GeneralBenesNetwork::testNetwork(const Permut& perm) const
}
return true;
}

}
8 changes: 4 additions & 4 deletions src/CModulus.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Copyright (C) 2012-2017 IBM Corp.
/* Copyright (C) 2012-2019 IBM Corp.
* This program is Licensed under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
Expand Down Expand Up @@ -27,6 +27,8 @@
#include "CModulus.h"
#include "timing.h"

namespace helib {

// It is assumed that m,q,context, and root are already set. If root is set
// to zero, it will be computed by the compRoots() method. Then rInv is
// computed as the inverse of root.
Expand Down Expand Up @@ -561,6 +563,4 @@ NTL::fftRep& Cmodulus::getScratch_fftRep(long k)
return rep;
}




}
5 changes: 4 additions & 1 deletion src/CModulus.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Copyright (C) 2012-2017 IBM Corp.
/* Copyright (C) 2012-2019 IBM Corp.
* This program is Licensed under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
Expand All @@ -22,6 +22,8 @@
#include "bluestein.h"
#include "clonedPtr.h"

namespace helib {

/**
* @class Cmodulus
* @brief Provides FFT and iFFT routines modulo a single-precision prime
Expand Down Expand Up @@ -126,5 +128,6 @@ class Cmodulus {
static NTL::fftRep& getScratch_fftRep(long k);
};

}

#endif // ifndef HELIB_CMODULUS_H
6 changes: 5 additions & 1 deletion src/CtPtrs.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Copyright (C) 2012-2017 IBM Corp.
/* Copyright (C) 2012-2019 IBM Corp.
* This program is Licensed under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
Expand All @@ -20,6 +20,8 @@
#include "PtrVector.h"
#include "PtrMatrix.h"

namespace helib {

typedef PtrVector<Ctxt> CtPtrs;
typedef PtrVector_VecT<Ctxt> CtPtrs_VecCt; // CtPtrs_VecCt(NTL::Vec<Ctxt>)
typedef PtrVector_vectorT<Ctxt> CtPtrs_vectorCt;//CtPtrs_vectorCt(std::vector<Ctxt>)
Expand Down Expand Up @@ -82,4 +84,6 @@ inline Ctxt innerProduct(const CtPtrs& v1, const CtPtrs& v2)
return ret;
}

}

#endif // ifndef HELIB_CTPTRS_H
5 changes: 4 additions & 1 deletion src/Ctxt.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Copyright (C) 2012-2017 IBM Corp.
/* Copyright (C) 2012-2019 IBM Corp.
* This program is Licensed under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
Expand All @@ -24,6 +24,7 @@
#include "fhe_stats.h"
#include "powerful.h"

namespace helib {

extern int fhe_watcher;
static const double safety = 1*log(2.0); // 1 bits of safety
Expand Down Expand Up @@ -2039,3 +2040,5 @@ double Ctxt::rawModSwitch(std::vector<NTL::ZZX>& zzParts, long q) const
return scaledNoise;
// this is returned so that caller in recryption.cpp can check bounds
}

}
5 changes: 4 additions & 1 deletion src/Ctxt.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Copyright (C) 2012-2017 IBM Corp.
/* Copyright (C) 2012-2019 IBM Corp.
* This program is Licensed under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
Expand Down Expand Up @@ -57,6 +57,8 @@
#include <cfloat> // DBL_MAX
#include "DoubleCRT.h"

namespace helib {

class KeySwitch;
class FHEPubKey;
class FHESecKey;
Expand Down Expand Up @@ -734,5 +736,6 @@ inline void Ctxt::extractBits(std::vector<Ctxt>& bits, long nBits2extract)
void extendExtractDigits(std::vector<Ctxt>& digits, const Ctxt& c, long r, long e);
// implemented in extractDigits.cpp

}

#endif // ifndef HELIB_CTXT_H
21 changes: 12 additions & 9 deletions src/DoubleCRT.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Copyright (C) 2012-2017 IBM Corp.
/* Copyright (C) 2012-2019 IBM Corp.
* This program is Licensed under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
Expand Down Expand Up @@ -27,6 +27,8 @@
#include "DoubleCRT.h"
#include "FHEContext.h"

namespace helib {

// A threaded implementation of DoubleCRT operations

static
Expand Down Expand Up @@ -1080,15 +1082,15 @@ void DoubleCRT::randomize(const NTL::ZZ* seed)
double DoubleCRT::sampleSmall()
{
zzX poly;
double retval = ::sampleSmall(poly,context); // degree-(phi(m)-1) polynomial
double retval = ::helib::sampleSmall(poly,context); // degree-(phi(m)-1) polynomial
*this = poly; // convert to DoubleCRT
return retval;
}

double DoubleCRT::sampleSmallBounded()
{
zzX poly;
double retval = ::sampleSmallBounded(poly,context); // degree-(phi(m)-1) polynomial
double retval = ::helib::sampleSmallBounded(poly,context); // degree-(phi(m)-1) polynomial
*this = poly; // convert to DoubleCRT
return retval;
}
Expand All @@ -1097,7 +1099,7 @@ double DoubleCRT::sampleSmallBounded()
double DoubleCRT::sampleHWt(long Hwt)
{
zzX poly;
double retval = ::sampleHWt(poly,context,Hwt);
double retval = ::helib::sampleHWt(poly,context,Hwt);
*this = poly; // convert to DoubleCRT
return retval;
}
Expand All @@ -1106,7 +1108,7 @@ double DoubleCRT::sampleHWt(long Hwt)
double DoubleCRT::sampleHWtBounded(long Hwt)
{
zzX poly;
double retval = ::sampleHWtBounded(poly,context,Hwt);
double retval = ::helib::sampleHWtBounded(poly,context,Hwt);
*this = poly; // convert to DoubleCRT
return retval;
}
Expand All @@ -1116,7 +1118,7 @@ double DoubleCRT::sampleGaussian(double stdev)
{
if (stdev==0.0) stdev=to_double(context.stdev);
zzX poly;
double retval = ::sampleGaussian(poly, context, stdev);
double retval = ::helib::sampleGaussian(poly, context, stdev);
*this = poly; // convert to DoubleCRT
return retval;
}
Expand All @@ -1125,7 +1127,7 @@ double DoubleCRT::sampleGaussianBounded(double stdev)
{
if (stdev==0.0) stdev=to_double(context.stdev);
zzX poly;
double retval = ::sampleGaussianBounded(poly, context, stdev);
double retval = ::helib::sampleGaussianBounded(poly, context, stdev);
*this = poly; // convert to DoubleCRT
return retval;
}
Expand All @@ -1136,15 +1138,15 @@ double DoubleCRT::sampleGaussianBounded(double stdev)
double DoubleCRT::sampleUniform(long B)
{
zzX poly;
double retval = ::sampleUniform(poly, context, B);
double retval = ::helib::sampleUniform(poly, context, B);
*this = poly;
return retval;
}

NTL::xdouble DoubleCRT::sampleUniform(const NTL::ZZ& B)
{
NTL::ZZX poly;
NTL::xdouble retval = ::sampleUniform(poly, context, B);
NTL::xdouble retval = ::helib::sampleUniform(poly, context, B);
*this = poly;
return retval;
}
Expand Down Expand Up @@ -1268,3 +1270,4 @@ void DoubleCRT::read(std::istream& str)
}
}

}
6 changes: 5 additions & 1 deletion src/DoubleCRT.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Copyright (C) 2012-2017 IBM Corp.
/* Copyright (C) 2012-2019 IBM Corp.
* This program is Licensed under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
Expand All @@ -20,6 +20,8 @@
#include "IndexMap.h"
#include "timing.h"

namespace helib {

class FHEcontext;

/**
Expand Down Expand Up @@ -428,4 +430,6 @@ inline NTL::ZZX to_ZZX(const DoubleCRT &d) { NTL::ZZX p; d.toPoly(p); return p;
typedef std::shared_ptr<DoubleCRT> DCRTptr;
typedef std::shared_ptr<NTL::ZZX> ZZXptr;

}

#endif // #ifndef HELIB_DOUBLECRT_H
6 changes: 5 additions & 1 deletion src/EaCx.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Copyright (C) 2012-2017 IBM Corp.
/* Copyright (C) 2012-2019 IBM Corp.
* This program is Licensed under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
Expand All @@ -22,6 +22,8 @@
#include "norms.h"
#include "debugging.h"

namespace helib {

static constexpr cx_double the_imaginary_i = cx_double(0.0, 1.0);

void EncryptedArrayCx::decrypt(const Ctxt& ctxt,
Expand Down Expand Up @@ -259,3 +261,5 @@ void EncryptedArrayCx::buildLinPolyCoeffs(std::vector<zzX>& C,
encode(C[0], x, msize, precision);
encode(C[1], y, msize, precision);
}

}
5 changes: 4 additions & 1 deletion src/EncryptedArray.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Copyright (C) 2012-2017 IBM Corp.
/* Copyright (C) 2012-2019 IBM Corp.
* This program is Licensed under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
Expand All @@ -18,6 +18,8 @@
#include "clonedPtr.h"
#include "norms.h"

namespace helib {

EncryptedArrayBase* buildEncryptedArray(const FHEcontext& context,
const PAlgebraMod& alMod, const NTL::ZZX& G)
{
Expand Down Expand Up @@ -1135,3 +1137,4 @@ template class EncryptedArrayDerived<PA_zz_p>;
template class PlaintextArrayDerived<PA_GF2>;
template class PlaintextArrayDerived<PA_zz_p>;

}
Loading

0 comments on commit ca0648b

Please sign in to comment.