Program Listing for File binfhecontext.h
↰ Return to documentation for file (binfhe/include/binfhecontext.h)
//==================================================================================
// BSD 2-Clause License
//
// Copyright (c) 2014-2022, NJIT, Duality Technologies Inc. and other contributors
//
// All rights reserved.
//
// Author TPOC: contact@openfhe.org
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this
// list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//==================================================================================
/*
Header file for BinFHEContext class, which is used for Boolean circuit FHE schemes
*/
#ifndef BINFHE_BINFHECONTEXT_H
#define BINFHE_BINFHECONTEXT_H
#include "binfhe-base-scheme.h"
#include "lattice/stdlatticeparms.h"
#include "utils/serializable.h"
#include <map>
#include <memory>
#include <string>
#include <vector>
namespace lbcrypto {
struct BinFHEContextParams {
// for intermediate prime, modulus for RingGSW / RLWE used in bootstrapping
uint32_t numberBits;
uint32_t cyclOrder;
// for LWE crypto parameters
uint32_t latticeParam;
// modulus for additive LWE
uint32_t mod;
// modulus for key switching; if it is zero, then it is replaced with intermediate prime for LWE crypto parameters
uint32_t modKS;
// base for key switching
uint32_t baseKS;
// for Ring GSW + LWE parameters
uint32_t gadgetBase; // gadget base used in the bootstrapping
uint32_t baseRK; // base for the refreshing key
// number of Automorphism keys for LMKCDEY (> 0)
uint32_t numAutoKeys;
// for key distribution
SecretKeyDist keyDist;
double stdDev;
};
class BinFHEContext : public Serializable {
public:
BinFHEContext() = default;
void GenerateBinFHEContext(uint32_t n, uint32_t N, NativeInteger q, NativeInteger Q, double std,
uint32_t baseKS, uint32_t baseG, uint32_t baseR, SecretKeyDist keyDist = UNIFORM_TERNARY,
BINFHE_METHOD method = GINX, uint32_t numAutoKeys = 10);
void GenerateBinFHEContext(BINFHE_PARAMSET set, bool arbFunc, uint32_t logQ = 11, uint32_t N = 0,
BINFHE_METHOD method = GINX, bool timeOptimization = false);
void GenerateBinFHEContext(BINFHE_PARAMSET set, BINFHE_METHOD method = GINX);
void GenerateBinFHEContext(const BinFHEContextParams& params, BINFHE_METHOD method = GINX);
const RingGSWACCKey& GetRefreshKey() const {
return m_BTKey.BSkey;
}
const LWESwitchingKey& GetSwitchKey() const {
return m_BTKey.KSkey;
}
const LWEPublicKey& GetPublicKey() const {
return m_BTKey.Pkey;
}
const std::shared_ptr<std::map<uint32_t, RingGSWBTKey>> GetBTKeyMap() const {
return std::make_shared<std::map<uint32_t, RingGSWBTKey>>(m_BTKey_map);
}
LWEPrivateKey KeyGen() const;
LWEKeyPair KeyGenPair() const;
LWEPublicKey PubKeyGen(ConstLWEPrivateKey& sk) const;
LWEPrivateKey KeyGenN() const;
LWECiphertext Encrypt(ConstLWEPrivateKey& sk, LWEPlaintext m, BINFHE_OUTPUT output = SMALL_DIM,
LWEPlaintextModulus p = 4, NativeInteger mod = 0) const;
LWECiphertext Encrypt(ConstLWEPublicKey& pk, LWEPlaintext m, BINFHE_OUTPUT output = SMALL_DIM,
LWEPlaintextModulus p = 4, NativeInteger mod = 0) const;
LWECiphertext SwitchCTtoqn(ConstLWESwitchingKey& ksk, ConstLWECiphertext& ct) const;
void Decrypt(ConstLWEPrivateKey& sk, ConstLWECiphertext& ct, LWEPlaintext* result, LWEPlaintextModulus p = 4) const;
LWESwitchingKey KeySwitchGen(ConstLWEPrivateKey& sk, ConstLWEPrivateKey& skN) const;
void BTKeyGen(ConstLWEPrivateKey& sk, KEYGEN_MODE keygenMode = SYM_ENCRYPT);
void BTKeyLoad(const RingGSWBTKey& key) {
m_BTKey = key;
}
void BTKeyMapLoadSingleElement(uint32_t baseG, const RingGSWBTKey& key) {
m_BTKey_map[baseG] = key;
}
void ClearBTKeys() {
m_BTKey.BSkey.reset();
m_BTKey.KSkey.reset();
m_BTKey.Pkey.reset();
m_BTKey_map.clear();
}
LWECiphertext EvalBinGate(BINGATE gate, ConstLWECiphertext& ct1, ConstLWECiphertext& ct2,
bool extended = false) const;
LWECiphertext EvalBinGate(BINGATE gate, const std::vector<LWECiphertext>& ctvector, bool extended = false) const;
LWECiphertext Bootstrap(ConstLWECiphertext& ct, bool extended = false) const;
LWECiphertext EvalFunc(ConstLWECiphertext& ct, const std::vector<NativeInteger>& LUT) const;
std::vector<NativeInteger> GenerateLUTviaFunction(NativeInteger (*f)(NativeInteger m, NativeInteger p),
NativeInteger p);
LWECiphertext EvalFloor(ConstLWECiphertext& ct, uint32_t roundbits = 0) const;
LWECiphertext EvalSign(ConstLWECiphertext& ct, bool schemeSwitch = false);
std::vector<LWECiphertext> EvalDecomp(ConstLWECiphertext& ct);
LWECiphertext EvalNOT(ConstLWECiphertext& ct) const;
LWECiphertext EvalConstant(bool value) const;
const std::shared_ptr<BinFHECryptoParams>& GetParams() {
return m_params;
}
const std::shared_ptr<LWEEncryptionScheme>& GetLWEScheme() {
return m_LWEscheme;
}
const std::shared_ptr<BinFHEScheme>& GetBinFHEScheme() {
return m_binfhescheme;
}
template <class Archive>
void save(Archive& ar, std::uint32_t const version) const {
ar(::cereal::make_nvp("params", m_params));
}
template <class Archive>
void load(Archive& ar, std::uint32_t const version) {
if (version > SerializedVersion()) {
OPENFHE_THROW("serialized object version " + std::to_string(version) +
" is from a later version of the library");
}
ar(::cereal::make_nvp("params", m_params));
m_binfhescheme = std::make_shared<BinFHEScheme>(m_params->GetRingGSWParams()->GetMethod());
}
std::string SerializedObjectName() const override {
return "BinFHEContext";
}
static uint32_t SerializedVersion() {
return 1;
}
NativeInteger GetMaxPlaintextSpace() const {
// Under our parameter choices, beta = 128 is enough, and therefore plaintext = q/2beta
return m_params->GetLWEParams()->Getq() / (this->GetBeta() << 1);
}
constexpr NativeInteger GetBeta() const {
return NativeInteger(128);
}
private:
// Shared pointer to Ring GSW + LWE parameters
std::shared_ptr<BinFHECryptoParams> m_params{nullptr};
// Shared pointer to the underlying additive LWE scheme
std::shared_ptr<LWEEncryptionScheme> m_LWEscheme{std::make_shared<LWEEncryptionScheme>()};
// Shared pointer to the underlying RingGSW/RLWE scheme
std::shared_ptr<BinFHEScheme> m_binfhescheme{nullptr};
// Struct containing the bootstrapping keys
RingGSWBTKey m_BTKey = {0};
std::map<uint32_t, RingGSWBTKey> m_BTKey_map;
// Whether to optimize time for sign eval
bool m_timeOptimization{false};
};
} // namespace lbcrypto
#endif