The secret key. More...

#include <keys.h>

Inheritance diagram for helib::SecKey:
helib::PubKey

Public Member Functions

 SecKey ()=delete
 
 ~SecKey () override=default
 
 SecKey (const Context &_context)
 
bool operator== (const SecKey &other) const
 
bool operator!= (const SecKey &other) const
 
void clear () override
 Clear all secret-key data. More...
 
long ImportSecKey (const DoubleCRT &sKey, double bound, long ptxtSpace=0, long maxDegKswitch=3)
 
long GenSecKey (long ptxtSpace=0, long maxDegKswitch=3)
 
void GenKeySWmatrix (long fromSPower, long fromXPower, long fromKeyIdx=0, long toKeyIdx=0, long ptxtSpace=0)
 
void Decrypt (NTL::ZZX &plaintxt, const Ctxt &ciphertxt) const
 
template<typename Scheme >
void Decrypt (Ptxt< Scheme > &plaintxt, const Ctxt &ciphertxt, OptLong prec=OptLong()) const
 Decrypt a ciphertext into a plaintext. More...
 
void Decrypt (NTL::ZZX &plaintxt, const Ctxt &ciphertxt, NTL::ZZX &f) const
 Debugging version, returns in f the polynomial before reduction modulo the ptxtSpace. More...
 
long skEncrypt (Ctxt &ctxt, const NTL::ZZX &ptxt, long ptxtSpace, long skIdx) const
 Symmetric encryption using the secret key. More...
 
long skEncrypt (Ctxt &ctxt, const zzX &ptxt, long ptxtSpace, long skIdx) const
 
long Encrypt (Ctxt &ciphertxt, const NTL::ZZX &plaintxt, long ptxtSpace=0) const override
 
long Encrypt (Ctxt &ciphertxt, const zzX &plaintxt, long ptxtSpace=0) const override
 
virtual void Encrypt (Ctxt &ctxt, const EncodedPtxt &eptxt) const override
 
virtual void Encrypt (Ctxt &ctxt, const EncodedPtxt_BGV &eptxt) const override
 
virtual void Encrypt (Ctxt &ctxt, const EncodedPtxt_CKKS &eptxt) const override
 
long genRecryptData ()
 Generate bootstrapping data if needed, returns index of key. More...
 
const DoubleCRTgetRecryptKey () const
 Getter method for the recryption key. More...
 
void writeTo (std::ostream &str) const
 Write out the SecKey object in binary format. More...
 
void writeToJSON (std::ostream &str) const
 Write out the secret key (SecKey) object to the output stream using JSON format. More...
 
JsonWrapper writeToJSON () const
 Write out the secret key (SecKey) object to a JsonWrapper. More...
 
void readJSON (std::istream &str)
 Read from the stream the serialized secret key (SecKey) object using JSON format. More...
 
void readJSON (const JsonWrapper &j)
 Read from the JsonWrapper the serialized secret key (SecKey) object. More...
 
std::ostream & writeSecKeyDerivedASCII (std::ostream &str) const
 
template<>
void Decrypt (Ptxt< BGV > &plaintxt, const Ctxt &ciphertxt, UNUSED OptLong prec) const
 
template<>
void Decrypt (Ptxt< CKKS > &plaintxt, const Ctxt &ciphertxt, OptLong prec) const
 
- Public Member Functions inherited from helib::PubKey
 PubKey ()=delete
 
 PubKey (const Context &_context)
 
 PubKey (const PubKey &other)
 Copy constructor. More...
 
virtual ~PubKey ()=default
 Default destructor. More...
 
bool operator== (const PubKey &other) const
 
bool operator!= (const PubKey &other) const
 
const ContextgetContext () const
 
long getPtxtSpace () const
 
bool keyExists (long keyID) const
 
double getSKeyBound (long keyID=0) const
 The size of the secret key. More...
 
bool isReachable (long k, long keyID=0) const
 Is it possible to re-linearize the automorphism X -> X^k See Section 3.2.2 in the design document (KeySwitchMap) More...
 
void setKeySwitchMap (long keyId=0)
 Compute the reachability graph of key-switching matrices See Section 3.2.2 in the design document (KeySwitchMap) More...
 
long getKSStrategy (long dim) const
 get KS strategy for dimension dim dim == -1 is Frobenius More...
 
void setKSStrategy (long dim, int val)
 set KS strategy for dimension dim dim == -1 is Frobenius More...
 
long Encrypt (Ctxt &ciphertxt, const NTL::ZZX &plaintxt, long ptxtSpace, bool highNoise) const
 
long Encrypt (Ctxt &ciphertxt, const zzX &plaintxt, long ptxtSpace, bool highNoise) const
 
void CKKSencrypt (Ctxt &ciphertxt, const NTL::ZZX &plaintxt, double ptxtSize=1.0, double scaling=0.0) const
 
void CKKSencrypt (Ctxt &ciphertxt, const zzX &plaintxt, double ptxtSize=1.0, double scaling=0.0) const
 
template<typename Scheme >
void Encrypt (Ctxt &ciphertxt, const Ptxt< Scheme > &plaintxt) const
 Encrypts a plaintext into a ciphertext. More...
 
bool isCKKS () const
 
bool isBootstrappable () const
 
void reCrypt (Ctxt &ctxt) const
 
void thinReCrypt (Ctxt &ctxt) const
 
void writeTo (std::ostream &str) const
 Write out the PubKey object in binary format. More...
 
void writeToJSON (std::ostream &str) const
 Write out the public key (PubKey) object to the output stream using JSON format. More...
 
JsonWrapper writeToJSON () const
 Write out the public key (PubKey) object to a JsonWrapper. More...
 
void readJSON (std::istream &str)
 In-place read from the stream the serialized public key (PubKey) object using JSON format. More...
 
void readJSON (const JsonWrapper &j)
 In-place read from the JsonWrapper the serialized public key (PubKey) object. More...
 
void hackPtxtSpace (long p2r)
 
template<>
void Encrypt (Ctxt &ciphertxt, const Ptxt< BGV > &plaintxt) const
 
template<>
void Encrypt (Ctxt &ciphertxt, const Ptxt< CKKS > &plaintxt) const
 
const std::vector< KeySwitch > & keySWlist () const
 
const KeySwitchgetKeySWmatrix (const SKHandle &from, long toID=0) const
 Find a key-switching matrix by its indexes. If no such matrix exists it returns a dummy matrix with toKeyID==-1. More...
 
const KeySwitchgetKeySWmatrix (long fromSPower, long fromXPower, long fromID=0, long toID=0) const
 
bool haveKeySWmatrix (const SKHandle &from, long toID=0) const
 
bool haveKeySWmatrix (long fromSPower, long fromXPower, long fromID=0, long toID=0) const
 
const KeySwitchgetAnyKeySWmatrix (const SKHandle &from) const
 Is there a matrix from this key to any base key? More...
 
bool haveAnyKeySWmatrix (const SKHandle &from) const
 
const KeySwitchgetNextKSWmatrix (long fromXPower, long fromID=0) const
 Get the next matrix to use for multi-hop automorphism See Section 3.2.2 in the design document. More...
 

Static Public Member Functions

static SecKey readFrom (std::istream &str, const Context &context)
 Read from the stream the serialized SecKey object in binary format. More...
 
static SecKey readFromJSON (std::istream &str, const Context &context)
 Read from the stream the serialized secret key (SecKey) object using JSON format. More...
 
static SecKey readFromJSON (const JsonWrapper &j, const Context &context)
 Read from the JsonWrapper the serialized secret key (SecKey) object. More...
 
- Static Public Member Functions inherited from helib::PubKey
static PubKey readFrom (std::istream &str, const Context &context)
 Read from the stream the serialized PubKey object in binary format. More...
 
static PubKey readFromJSON (std::istream &str, const Context &context)
 Read from the stream the serialized public key (PubKey) object using JSON format. More...
 
static PubKey readFromJSON (const JsonWrapper &j, const Context &context)
 Read from the JsonWrapper the serialized public key (PubKey) object. More...
 
static long ePlusR (long p)
 

Static Public Attributes

static constexpr std::string_view typeName = "SecKey"
 Class label to be added to JSON serialization as object type information. More...
 
- Static Public Attributes inherited from helib::PubKey
static constexpr std::string_view typeName = "PubKey"
 Class label to be added to JSON serialization as object type information. More...
 

Friends

class KeySwitch
 
std::ostream & operator<< (std::ostream &str, const SecKey &sk)
 
std::istream & operator>> (std::istream &str, SecKey &sk)
 

Detailed Description

The secret key.

Constructor & Destructor Documentation

◆ SecKey() [1/2]

helib::SecKey::SecKey ( )
delete

◆ ~SecKey()

helib::SecKey::~SecKey ( )
overridedefault

◆ SecKey() [2/2]

helib::SecKey::SecKey ( const Context _context)
explicit

Member Function Documentation

◆ clear()

void helib::SecKey::clear ( )
overridevirtual

Clear all secret-key data.

Reimplemented from helib::PubKey.

◆ Decrypt() [1/5]

void helib::SecKey::Decrypt ( NTL::ZZX &  plaintxt,
const Ctxt ciphertxt 
) const

◆ Decrypt() [2/5]

void helib::SecKey::Decrypt ( NTL::ZZX &  plaintxt,
const Ctxt ciphertxt,
NTL::ZZX &  f 
) const

Debugging version, returns in f the polynomial before reduction modulo the ptxtSpace.

◆ Decrypt() [3/5]

template<>
void helib::SecKey::Decrypt ( Ptxt< BGV > &  plaintxt,
const Ctxt ciphertxt,
UNUSED OptLong  prec 
) const

◆ Decrypt() [4/5]

template<>
void helib::SecKey::Decrypt ( Ptxt< CKKS > &  plaintxt,
const Ctxt ciphertxt,
OptLong  prec 
) const

◆ Decrypt() [5/5]

template<typename Scheme >
void helib::SecKey::Decrypt ( Ptxt< Scheme > &  plaintxt,
const Ctxt ciphertxt,
OptLong  prec = OptLong() 
) const

Decrypt a ciphertext into a plaintext.

Template Parameters
SchemeEncryption scheme used (must be BGV or CKKS).
Parameters
plaintxtPlaintext into which to decrypt.
ciphertxtCiphertext to decrypt.
precCKKS precision to be used (must be defaulted if Scheme is BGV).

◆ Encrypt() [1/5]

long helib::SecKey::Encrypt ( Ctxt ciphertxt,
const NTL::ZZX &  plaintxt,
long  ptxtSpace = 0 
) const
overridevirtual

Reimplemented from helib::PubKey.

◆ Encrypt() [2/5]

long helib::SecKey::Encrypt ( Ctxt ciphertxt,
const zzX plaintxt,
long  ptxtSpace = 0 
) const
overridevirtual

Reimplemented from helib::PubKey.

◆ Encrypt() [3/5]

void helib::SecKey::Encrypt ( Ctxt ctxt,
const EncodedPtxt eptxt 
) const
overridevirtual

Reimplemented from helib::PubKey.

◆ Encrypt() [4/5]

void helib::SecKey::Encrypt ( Ctxt ctxt,
const EncodedPtxt_BGV eptxt 
) const
overridevirtual

Reimplemented from helib::PubKey.

◆ Encrypt() [5/5]

void helib::SecKey::Encrypt ( Ctxt ctxt,
const EncodedPtxt_CKKS eptxt 
) const
overridevirtual

Reimplemented from helib::PubKey.

◆ GenKeySWmatrix()

void helib::SecKey::GenKeySWmatrix ( long  fromSPower,
long  fromXPower,
long  fromKeyIdx = 0,
long  toKeyIdx = 0,
long  ptxtSpace = 0 
)

Generate a key-switching matrix and store it in the public key. The i'th column of the matrix encrypts fromKey*B1*B2*...*B{i-1}*Q under toKey, relative to the largest modulus (i.e., all primes) and plaintext space p. Q is the product of special primes, and the Bi's are the products of primes in the i'th digit. The plaintext space defaults to 2^r, as defined by context.mod2r.

◆ genRecryptData()

long helib::SecKey::genRecryptData ( )

Generate bootstrapping data if needed, returns index of key.

◆ GenSecKey()

long helib::SecKey::GenSecKey ( long  ptxtSpace = 0,
long  maxDegKswitch = 3 
)

Key generation: This procedure generates a single secret key, pushes it onto the sKeys list using ImportSecKey from above.

◆ getRecryptKey()

const DoubleCRT& helib::SecKey::getRecryptKey ( ) const
inline

Getter method for the recryption key.

Returns
A const reference to the recryption key.

◆ ImportSecKey()

long helib::SecKey::ImportSecKey ( const DoubleCRT sKey,
double  bound,
long  ptxtSpace = 0,
long  maxDegKswitch = 3 
)

We allow the calling application to choose a secret-key polynomial by itself, then insert it into the SecKey object, getting the index of that secret key in the sKeys list. If this is the first secret-key for this object then the procedure below also generates a corresponding public encryption key. It is assumed that the context already contains all parameters.

◆ operator!=()

bool helib::SecKey::operator!= ( const SecKey other) const

◆ operator==()

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

◆ readFrom()

SecKey helib::SecKey::readFrom ( std::istream &  str,
const Context context 
)
static

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

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

◆ readFromJSON() [1/2]

SecKey helib::SecKey::readFromJSON ( const JsonWrapper j,
const Context context 
)
static

Read from the JsonWrapper the serialized secret key (SecKey) object.

Parameters
jThe JsonWrapper containing the serialized SecKey object.
contextThe Context to be used.
Returns
The deserialized SecKey object.

◆ readFromJSON() [2/2]

SecKey helib::SecKey::readFromJSON ( std::istream &  str,
const Context context 
)
static

Read from the stream the serialized secret key (SecKey) object using JSON format.

Parameters
strInput std::istream.
contextThe Context to be used.
Returns
The deserialized SecKey object.

◆ readJSON() [1/2]

void helib::SecKey::readJSON ( const JsonWrapper j)

Read from the JsonWrapper the serialized secret key (SecKey) object.

Parameters
jThe JsonWrapper containing the serialized SecKey object.

◆ readJSON() [2/2]

void helib::SecKey::readJSON ( std::istream &  str)

Read from the stream the serialized secret key (SecKey) object using JSON format.

Parameters
strInput std::istream.

◆ skEncrypt() [1/2]

long helib::SecKey::skEncrypt ( Ctxt ctxt,
const NTL::ZZX &  ptxt,
long  ptxtSpace,
long  skIdx 
) const

Symmetric encryption using the secret key.

◆ skEncrypt() [2/2]

long helib::SecKey::skEncrypt ( Ctxt ctxt,
const zzX ptxt,
long  ptxtSpace,
long  skIdx 
) const

◆ writeSecKeyDerivedASCII()

std::ostream & helib::SecKey::writeSecKeyDerivedASCII ( std::ostream &  str) const

◆ writeTo()

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

Write out the SecKey object in binary format.

Parameters
strOutput std::ostream.

◆ writeToJSON() [1/2]

JsonWrapper helib::SecKey::writeToJSON ( ) const

Write out the secret key (SecKey) object to a JsonWrapper.

Returns
The JsonWrapper.

◆ writeToJSON() [2/2]

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

Write out the secret key (SecKey) object to the output stream using JSON format.

Parameters
strOutput std::ostream.

Friends And Related Function Documentation

◆ KeySwitch

friend class KeySwitch
friend

◆ operator<<

std::ostream& operator<< ( std::ostream &  str,
const SecKey sk 
)
friend

◆ operator>>

std::istream& operator>> ( std::istream &  str,
SecKey sk 
)
friend

Member Data Documentation

◆ typeName

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

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