Maintaining the HE scheme parameters. More...

#include <Context.h>

Classes

struct  BootStrapParams
 
struct  ModChainParams
 
struct  SerializableContent
 

Public Member Functions

long getM () const
 Getter method for the m used to create this context. More...
 
long getP () const
 Getter method for the p used to create this context. More...
 
long getPhiM () const
 Getter method for the phi(m) of the created context. More...
 
long getOrdP () const
 Getter method for the ord(p) of the created context. More...
 
long getNSlots () const
 Getter method for the number of plaintext slots of the created context. More...
 
double getScale () const
 Getter method for the scale. More...
 
NTL::xdouble getStdev () const
 Getter method for the standard deviation used.. More...
 
long getR () const
 Getter method for the default r value of the created context. More...
 
long getPPowR () const
 Getter method for the default p^r value of the created context. More...
 
long getPrecision () const
 Getter method for the precision value of the created CKKS context. More...
 
const PowerfulDCRTgetPowerfulConverter () const
 Get a powerful converter. More...
 
const std::shared_ptr< PolyModRing > & getSlotRing () const
 Get a slot ring. More...
 
const IndexSetgetSmallPrimes () const
 Getter method to the index set to the small primes. More...
 
const IndexSetgetCtxtPrimes () const
 Getter method to the index set to the ciphertext primes. More...
 
const IndexSetgetSpecialPrimes () const
 Getter method to the index set to the special primes. More...
 
const std::vector< IndexSet > & getDigits () const
 Getter method to the digits. More...
 
const IndexSetgetDigit (long i) const
 Getter method to get a single digit. More...
 
const ThinRecryptDatagetRcData () const
 Getter method for a recryption data object. More...
 
bool isCKKS () const
 Return whether this is a CKKS context or not Context. More...
 
long getHwt () const
 Getter method for the Hamming weight value. More...
 
long getE () const
 Getter method for the e parameter. More...
 
long getEPrime () const
 Getter method for the e prime parameter. More...
 
const PAlgebragetZMStar () const
 Get the underlying zMStar object. More...
 
const PAlgebraModgetAlMod () const
 Get the underlying AlMod object. More...
 
const EncryptedArraygetView () const
 Getter method returning the default view object of the created context. More...
 
const EncryptedArraygetEA () const
 Getter method returning the default EncryptedArray object of the created context. More...
 
const std::shared_ptr< const EncryptedArray > & shareEA () const
 Getter method returning the std::shared_ptr to default EncryptedArray object of the created context. More...
 
double noiseBoundForUniform (double magBound, long degBound) const
 
NTL::xdouble noiseBoundForUniform (NTL::xdouble magBound, long degBound) const
 
double noiseBoundForMod (long modulus, long degBound) const
 
double noiseBoundForGaussian (double sigma, long degBound) const
 
NTL::xdouble noiseBoundForGaussian (NTL::xdouble sigma, long degBound) const
 
double noiseBoundForSmall (double prob, long degBound) const
 
double noiseBoundForHWt (long hwt, UNUSED long degBound) const
 
double stdDevForRecryption () const
 Calculate the standard deviation for recryption. More...
 
double boundForRecryption () const
 Calculate the bound for recryption. More...
 
const ModuliSizesgetModSizeTable () const
 Get the helper table to map required modulo-sizes to primeSets. More...
 
void setModSizeTable ()
 Set the helper table to map required modulo-sizes to primeSets. More...
 
 ~Context ()=default
 Default destructor. More...
 
 Context ()=delete
 Deleted default constructor. More...
 
 Context (const Context &other)=delete
 Deleted copy constructor. More...
 
 Context (Context &&other)=delete
 Deleted move constructor. More...
 
Contextoperator= (const Context &other)=delete
 Deleted copy assignment. More...
 
Contextoperator= (Context &&other)=delete
 Deleted move assignment. More...
 
void enableBootStrapping (const NTL::Vec< long > &mvec, bool build_cache=false, bool alsoThick=true)
 Initialises the recryption data. More...
 
bool isBootstrappable () const
 Check if a Context is bootstrappable. More...
 
IndexSet fullPrimes () const
 Getter method that returns the handles of both the ctxtPrimes and specialPrimes associated with this Context. More...
 
IndexSet allPrimes () const
 Getter method that returns the handles of all primes associated with this Context. More...
 
IndexSet getCtxtPrimes (long nprimes) const
 Getter method that returns the first nprimes ctxtPrimes associated with this Context. More...
 
long BPL () const
 
bool operator== (const Context &other) const
 Equals operator between two Context objects. More...
 
bool operator!= (const Context &other) const
 Not equals operator between two Context objects. More...
 
long ithPrime (unsigned long i) const
 Getter method for the small prime of the modulus chain at index i as a long. More...
 
const CmodulusithModulus (unsigned long i) const
 Getter method for the small prime of the modulus chain at index i as a Cmodulus. More...
 
long numPrimes () const
 Return the total number of small primes in the modulus chain. More...
 
bool isZeroDivisor (const NTL::ZZ &num) const
 Check if a number is divisible by any of the primes in the modulus chain. More...
 
bool inChain (long p) const
 Check if value is already contained within the modulus chain. More...
 
void productOfPrimes (NTL::ZZ &p, const IndexSet &s) const
 Calculate the product of all primes in the given set. More...
 
NTL::ZZ productOfPrimes (const IndexSet &s) const
 
double logOfPrime (unsigned long i) const
 Calculate the natural logarithm of the ith prime of the modulus chain. More...
 
double logOfProduct (const IndexSet &s) const
 Calculate the natural logarithm of productOfPrimes(s) for a given set of primes s. More...
 
long bitSizeOfQ () const
 Calculate the size of the ciphertext modulus Q in bits. More...
 
double securityLevel () const
 An estimate for the security-level. This has a lower bound of 0. More...
 
void printout (std::ostream &out=std::cout) const
 Print out algebra and other important info. More...
 
void writeTo (std::ostream &str) const
 Write out the Context object in binary format. More...
 
void writeToJSON (std::ostream &str) const
 Write out the Context object to the output stream using JSON format. More...
 
JsonWrapper writeToJSON () const
 Write out the Context object to a JsonWrapper. More...
 
void clearModChain ()
 
void buildModChain (long nBits, long nDgts=3, bool willBeBootstrappable=false, long skHwt=0, long resolution=3, long bitsInSpecialPrimes=0)
 Build the modulus chain for given Context object. More...
 
void endBuildModChain ()
 

Static Public Member Functions

static Context readFrom (std::istream &str)
 Read from the stream the serialized Context object in binary format. More...
 
static ContextreadPtrFrom (std::istream &str)
 Read from the stream the serialized Context object in binary format. More...
 
static Context readFromJSON (std::istream &str)
 Read from the stream the serialized Context object using JSON format. More...
 
static Context readFromJSON (const JsonWrapper &j)
 Read from the JsonWrapper the serialized Context object. More...
 
static ContextreadPtrFromJSON (std::istream &str)
 Read from the JsonWrapper the serialized Context object. More...
 

Static Public Attributes

static constexpr std::string_view typeName = "Context"
 Class label to be added to JSON serialization as object type information. More...
 

Friends

template<typename SCHEME >
class ContextBuilder
 
std::ostream & operator<< (std::ostream &str, const Context &context)
 Write out all other data associated with a given Context object. More...
 
void readContextBase (std::istream &str, unsigned long &m, unsigned long &p, unsigned long &r, std::vector< long > &gens, std::vector< long > &ords)
 Read in the basic information m, p and r required to construct a Context object. More...
 
std::istream & operator>> (std::istream &str, Context &context)
 Read in all other data associated with a given Context object. More...
 

Detailed Description

Maintaining the HE scheme parameters.

Constructor & Destructor Documentation

◆ ~Context()

helib::Context::~Context ( )
default

Default destructor.

◆ Context() [1/3]

helib::Context::Context ( )
delete

Deleted default constructor.

◆ Context() [2/3]

helib::Context::Context ( const Context other)
delete

Deleted copy constructor.

Parameters
otherContext to copy.

◆ Context() [3/3]

helib::Context::Context ( Context &&  other)
delete

Deleted move constructor.

Parameters
otherContext to move.

Member Function Documentation

◆ allPrimes()

IndexSet helib::Context::allPrimes ( ) const
inline

Getter method that returns the handles of all primes associated with this Context.

Returns
IndexSet of handles to the ctxtPrimes, specialPrimes and smallPrimes.

◆ bitSizeOfQ()

long helib::Context::bitSizeOfQ ( ) const
inline

Calculate the size of the ciphertext modulus Q in bits.

Returns
The bit size of the ciphertext modulus Q = ctxtPrimes | specialPrimes.

◆ boundForRecryption()

double helib::Context::boundForRecryption ( ) const
inline

Calculate the bound for recryption.

Returns
The bound for recryption.

◆ BPL()

long helib::Context::BPL ( ) const
inline

◆ buildModChain()

void helib::Context::buildModChain ( long  nBits,
long  nDgts = 3,
bool  willBeBootstrappable = false,
long  skHwt = 0,
long  resolution = 3,
long  bitsInSpecialPrimes = 0 
)

Build the modulus chain for given Context object.

Parameters
nBitsTotal number of bits required for the modulus chain.
nDgtsNumber of digits/columns in the key-switching matrix. Default is 3.
willBeBoostrappableFlag for initializing bootstrapping data. Default is false.
skHwtThe Hamming weight of the secret key. Default is 0.
resolutionThe bit size of resolution of the modulus chain. Default is 3.
bitsInSpecialPrimesThe bit size of the special primes in the modulus chain. Default is 0.

◆ clearModChain()

void helib::Context::clearModChain ( )
inline

◆ enableBootStrapping()

void helib::Context::enableBootStrapping ( const NTL::Vec< long > &  mvec,
bool  build_cache = false,
bool  alsoThick = true 
)
inline

Initialises the recryption data.

Parameters
mvecA std::vector of unique prime factors of m.
build_cacheFlag for building a cache for improved efficiency. Default is false.
alsoThickFlag for initialising additional information needed for thick bootstrapping. Default is true.

◆ endBuildModChain()

void helib::Context::endBuildModChain ( )

◆ fullPrimes()

IndexSet helib::Context::fullPrimes ( ) const
inline

Getter method that returns the handles of both the ctxtPrimes and specialPrimes associated with this Context.

Returns
IndexSet of the handles to the ctxtPrimes and specialPrimes.

◆ getAlMod()

const PAlgebraMod& helib::Context::getAlMod ( ) const
inline

Get the underlying AlMod object.

Returns
A AlMod object.

◆ getCtxtPrimes() [1/2]

const IndexSet& helib::Context::getCtxtPrimes ( ) const
inline

Getter method to the index set to the ciphertext primes.

Returns
A const reference to the index set to the ciphertext primes.

◆ getCtxtPrimes() [2/2]

IndexSet helib::Context::getCtxtPrimes ( long  nprimes) const
inline

Getter method that returns the first nprimes ctxtPrimes associated with this Context.

Parameters
nprimesThe number of desired ctxtPrimes.
Returns
IndexSet of handles to the first nprimes ctxtPrimes.

◆ getDigit()

const IndexSet& helib::Context::getDigit ( long  i) const
inline

Getter method to get a single digit.

Parameters
iThe i the digit.
Returns
A const reference to an index set that representing the ith digit.

◆ getDigits()

const std::vector<IndexSet>& helib::Context::getDigits ( ) const
inline

Getter method to the digits.

Returns
A const reference to a std::vector of index sets that represent the digits.

◆ getE()

long helib::Context::getE ( ) const
inline

Getter method for the e parameter.

Returns
The e parameter.

◆ getEA()

const EncryptedArray& helib::Context::getEA ( ) const
inline

Getter method returning the default EncryptedArray object of the created context.

Returns
A reference to the EncryptedArray object.
Note
It is foreseen that this method will be eventually deprecated in favour of the alternative getView.

◆ getEPrime()

long helib::Context::getEPrime ( ) const
inline

Getter method for the e prime parameter.

Returns
The e prime parameter.

◆ getHwt()

long helib::Context::getHwt ( ) const
inline

Getter method for the Hamming weight value.

Returns
The Hamming weight value.

◆ getM()

long helib::Context::getM ( ) const
inline

Getter method for the m used to create this context.

Returns
The cyclotomic index m.

◆ getModSizeTable()

const ModuliSizes& helib::Context::getModSizeTable ( ) const
inline

Get the helper table to map required modulo-sizes to primeSets.

Returns
The table as ModuliSizes type.

◆ getNSlots()

long helib::Context::getNSlots ( ) const
inline

Getter method for the number of plaintext slots of the created context.

Returns
The number of plaintext slots phi(m)/ord(p).

◆ getOrdP()

long helib::Context::getOrdP ( ) const
inline

Getter method for the ord(p) of the created context.

Returns
The order of p in (Z/mZ)^*.

◆ getP()

long helib::Context::getP ( ) const
inline

Getter method for the p used to create this context.

Returns
The plaintext modulus p.

◆ getPhiM()

long helib::Context::getPhiM ( ) const
inline

Getter method for the phi(m) of the created context.

Returns
The degree of the cyclotomic polynomial Phi_m(X).

◆ getPowerfulConverter()

const PowerfulDCRT& helib::Context::getPowerfulConverter ( ) const
inline

Get a powerful converter.

Returns
A powerful converter.

◆ getPPowR()

long helib::Context::getPPowR ( ) const
inline

Getter method for the default p^r value of the created context.

Returns
The raised plaintext modulus p^r.
Note
This value is not invariant: it is possible to work "view" objects that use different PAlgebra objects.

◆ getPrecision()

long helib::Context::getPrecision ( ) const
inline

Getter method for the precision value of the created CKKS context.

Returns
The bit precision value.
Note
This value is not invariant: it is possible to work "view" objects that use different PAlgebra objects.

◆ getR()

long helib::Context::getR ( ) const
inline

Getter method for the default r value of the created context.

Returns
The r value representing the Hensel lifting for BGV or the bit precision for CKKS.
Note
This value is not invariant: it is possible to work "view" objects that use different PAlgebra objects.

◆ getRcData()

const ThinRecryptData& helib::Context::getRcData ( ) const
inline

Getter method for a recryption data object.

Returns
A const reference to the recryption data object.

◆ getScale()

double helib::Context::getScale ( ) const
inline

Getter method for the scale.

Returns
the scale as a double.

◆ getSlotRing()

const std::shared_ptr<PolyModRing>& helib::Context::getSlotRing ( ) const
inline

Get a slot ring.

Returns
A reference to a std::shared pointer pointing to a slotRing.

◆ getSmallPrimes()

const IndexSet& helib::Context::getSmallPrimes ( ) const
inline

Getter method to the index set to the small primes.

Returns
A const reference to the index set to the small primes.

◆ getSpecialPrimes()

const IndexSet& helib::Context::getSpecialPrimes ( ) const
inline

Getter method to the index set to the special primes.

Returns
A const reference to the index set to the special primes.

◆ getStdev()

NTL::xdouble helib::Context::getStdev ( ) const
inline

Getter method for the standard deviation used..

Returns
the standard deviation as an NTL::xdouble.

◆ getView()

const EncryptedArray& helib::Context::getView ( ) const
inline

Getter method returning the default view object of the created context.

Returns
A reference to the view object.

◆ getZMStar()

const PAlgebra& helib::Context::getZMStar ( ) const
inline

Get the underlying zMStar object.

Returns
A zMStar object.

◆ inChain()

bool helib::Context::inChain ( long  p) const
inline

Check if value is already contained within the modulus chain.

Parameters
pThe number to check.
Returns
true if p is already contained within the modulus chain, false otherwise.

◆ isBootstrappable()

bool helib::Context::isBootstrappable ( ) const
inline

Check if a Context is bootstrappable.

Returns
true if recryption data is found, false otherwise.

◆ isCKKS()

bool helib::Context::isCKKS ( ) const
inline

Return whether this is a CKKS context or not Context.

Returns
A bool, true if the Context object uses CKKS scheme false otherwise.
Note
We assume false return to be BGV scheme.

◆ isZeroDivisor()

bool helib::Context::isZeroDivisor ( const NTL::ZZ &  num) const
inline

Check if a number is divisible by any of the primes in the modulus chain.

Parameters
numThe number to check.
Returns
true if the modulus chain contains at least one divisor of num, false otherwise.

◆ ithModulus()

const Cmodulus& helib::Context::ithModulus ( unsigned long  i) const
inline

Getter method for the small prime of the modulus chain at index i as a Cmodulus.

Parameters
iIndex of the desired small prime.
Returns
Reference to the small prime modulus at index i.

◆ ithPrime()

long helib::Context::ithPrime ( unsigned long  i) const
inline

Getter method for the small prime of the modulus chain at index i as a long.

Parameters
iIndex of the desired small prime.
Returns
The small prime of the modulus chain at index i.

◆ logOfPrime()

double helib::Context::logOfPrime ( unsigned long  i) const
inline

Calculate the natural logarithm of the ith prime of the modulus chain.

Parameters
iIndex of the desired prime.
Returns
The natural logarithm of the ith prime of the modulus chain.

◆ logOfProduct()

double helib::Context::logOfProduct ( const IndexSet s) const
inline

Calculate the natural logarithm of productOfPrimes(s) for a given set of primes s.

Parameters
sThe set of input primes.
Returns
The natural logarithm of the product of the input primes.

◆ noiseBoundForGaussian() [1/2]

double helib::Context::noiseBoundForGaussian ( double  sigma,
long  degBound 
) const
inline

◆ noiseBoundForGaussian() [2/2]

NTL::xdouble helib::Context::noiseBoundForGaussian ( NTL::xdouble  sigma,
long  degBound 
) const
inline

◆ noiseBoundForHWt()

double helib::Context::noiseBoundForHWt ( long  hwt,
UNUSED long  degBound 
) const
inline

◆ noiseBoundForMod()

double helib::Context::noiseBoundForMod ( long  modulus,
long  degBound 
) const
inline

◆ noiseBoundForSmall()

double helib::Context::noiseBoundForSmall ( double  prob,
long  degBound 
) const
inline

◆ noiseBoundForUniform() [1/2]

double helib::Context::noiseBoundForUniform ( double  magBound,
long  degBound 
) const
inline

◆ noiseBoundForUniform() [2/2]

NTL::xdouble helib::Context::noiseBoundForUniform ( NTL::xdouble  magBound,
long  degBound 
) const
inline

◆ numPrimes()

long helib::Context::numPrimes ( ) const
inline

Return the total number of small primes in the modulus chain.

Returns
The total number of small primes in the modulus chain.

◆ operator!=()

bool helib::Context::operator!= ( const Context other) const
inline

Not equals operator between two Context objects.

Parameters
otherContext to compare to.
Returns
true if differ, false otherwise.

◆ operator=() [1/2]

Context& helib::Context::operator= ( const Context other)
delete

Deleted copy assignment.

Parameters
otherContext to copy.

◆ operator=() [2/2]

Context& helib::Context::operator= ( Context &&  other)
delete

Deleted move assignment.

Parameters
otherContext to move.

◆ operator==()

bool helib::Context::operator== ( const Context other) const

Equals operator between two Context objects.

Parameters
otherContext to compare to.
Returns
true if identical, false otherwise.

◆ printout()

void helib::Context::printout ( std::ostream &  out = std::cout) const

Print out algebra and other important info.

Parameters
outOutput std::ostream.

◆ productOfPrimes() [1/2]

NTL::ZZ helib::Context::productOfPrimes ( const IndexSet s) const
inline

◆ productOfPrimes() [2/2]

void helib::Context::productOfPrimes ( NTL::ZZ &  p,
const IndexSet s 
) const

Calculate the product of all primes in the given set.

Parameters
pThe product of the input primes.
sThe set of input primes to the product.

◆ readFrom()

Context helib::Context::readFrom ( std::istream &  str)
static

Read from the stream the serialized Context object in binary format.

Parameters
strInput std::istream.
Returns
The deserialized Context object.

◆ readFromJSON() [1/2]

Context helib::Context::readFromJSON ( const JsonWrapper j)
static

Read from the JsonWrapper the serialized Context object.

Parameters
jThe JsonWrapper containing the serialized Context object.
Returns
The deserialized Context object.

◆ readFromJSON() [2/2]

Context helib::Context::readFromJSON ( std::istream &  str)
static

Read from the stream the serialized Context object using JSON format.

Parameters
strInput std::istream.
Returns
The deserialized Context object.

◆ readPtrFrom()

Context * helib::Context::readPtrFrom ( std::istream &  str)
static

Read from the stream the serialized Context object in binary format.

Parameters
strInput std::istream.
Returns
Raw pointer to the deserialized Context object.

◆ readPtrFromJSON()

Context * helib::Context::readPtrFromJSON ( std::istream &  str)
static

Read from the JsonWrapper the serialized Context object.

Parameters
jThe JsonWrapper containing the serialized Context object.
Returns
Raw pointer to the deserialized Context object.

◆ securityLevel()

double helib::Context::securityLevel ( ) const
inline

An estimate for the security-level. This has a lower bound of 0.

Parameters
hwtThe Hamming weight of the secret key.
Note
This function uses experimental affine approximations to the lwe-estimator from https://bitbucket.org/malb/lwe-estimator/raw/HEAD/estimator.py, from Aug-2020 (see script in misc/estimator/lwe-estimator.sage).

Let s=3.2 if m is a power of two, or s=3.2*sqrt(m) otherwise. For the estimator we use alpha=s/q (so log2AlphaInv = log_2(q/s)), and n=phi(m).

◆ setModSizeTable()

void helib::Context::setModSizeTable ( )
inline

Set the helper table to map required modulo-sizes to primeSets.

◆ shareEA()

const std::shared_ptr<const EncryptedArray>& helib::Context::shareEA ( ) const
inline

Getter method returning the std::shared_ptr to default EncryptedArray object of the created context.

Returns
A reference to std::shared_ptr to the EncryptedArray object.

◆ stdDevForRecryption()

double helib::Context::stdDevForRecryption ( ) const
inline

Calculate the standard deviation for recryption.

Returns
The standard deviation for recryption.

◆ writeTo()

void helib::Context::writeTo ( std::ostream &  str) const

Write out the Context object in binary format.

Parameters
strOutput std::ostream.

◆ writeToJSON() [1/2]

JsonWrapper helib::Context::writeToJSON ( ) const

Write out the Context object to a JsonWrapper.

Returns
The JsonWrapper.

◆ writeToJSON() [2/2]

void helib::Context::writeToJSON ( std::ostream &  str) const

Write out the Context object to the output stream using JSON format.

Parameters
strOutput std::ostream.

Friends And Related Function Documentation

◆ ContextBuilder

template<typename SCHEME >
friend class ContextBuilder
friend

◆ operator<<

std::ostream& operator<< ( std::ostream &  str,
const Context context 
)
friend

Write out all other data associated with a given Context object.

Parameters
strOutput std::ostream.
contextThe Context to write.
Returns
Input std::ostream post writing.

◆ operator>>

std::istream& operator>> ( std::istream &  str,
Context context 
)
friend

Read in all other data associated with a given Context object.

Parameters
strInput std::istream.
contextDestination Context object.
Returns
Input std::istream post reading.

◆ readContextBase

void readContextBase ( std::istream &  str,
unsigned long &  m,
unsigned long &  p,
unsigned long &  r,
std::vector< long > &  gens,
std::vector< long > &  ords 
)
friend

Read in the basic information m, p and r required to construct a Context object.

Parameters
strInput std::istream.
mDestination of the index of the cyclotomic polynomial.
pDestination of the plaintext modulus.
rDestination of BGV: The Hensel lifting parameter. CKKS: The bit precision.
gensDestination of the generators of (Z/mZ)^* (other than p).
ordsDestination of the orders of each of the generators of (Z/mZ)^*.

Member Data Documentation

◆ typeName

constexpr std::string_view helib::Context::typeName = "Context"
staticconstexpr

Class label to be added to JSON serialization as object type information.