Skip to content

Commit

Permalink
710 validate parameters for cryptocontext generation (openfheorg#736)
Browse files Browse the repository at this point in the history
* Validation for cryptocontext parameters

* Restored the default value for thresholdNumOfParties for BFV, prepended some math functions with std::

* Set firstModSize to 96 for NATIVEINT == 128

* Set firstModSize to 89 for NATIVEINT == 128, disabled/enabled some functions, expanded validation logic, corrected unit tests

* Added a new check to the cryptoparams validator

* Addressed the PR comment, fixed some unittests

---------

Co-authored-by: Dmitriy Suponitskiy <dsuponitskiy@dualitytech.com>
  • Loading branch information
dsuponitskiy and dsuponitskiy-duality authored Apr 18, 2024
1 parent 9dcda8a commit 2589b2c
Show file tree
Hide file tree
Showing 26 changed files with 898 additions and 593 deletions.
1 change: 0 additions & 1 deletion src/pke/examples/threshold-fhe-5p.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ void RunBFVrns() {
parameters.SetBatchSize(batchSize);
parameters.SetDigitSize(digitSize);
parameters.SetScalingModSize(dcrtBits);
parameters.SetThresholdNumOfParties(5);
parameters.SetMultiplicationTechnique(HPSPOVERQLEVELED);

CryptoContext<DCRTPoly> cc = GenCryptoContext(parameters);
Expand Down
7 changes: 3 additions & 4 deletions src/pke/include/constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ enum ProxyReEncryptionMode {
INDCPA,
FIXED_NOISE_HRA,
NOISE_FLOODING_HRA,
DIVIDE_AND_ROUND_HRA,
};
ProxyReEncryptionMode convertToProxyReEncryptionMode(const std::string& str);
ProxyReEncryptionMode convertToProxyReEncryptionMode(uint32_t num);
Expand Down Expand Up @@ -154,7 +153,7 @@ enum {
BASE_NUM_LEVELS_TO_DROP = 1,
};

enum NOISE_FLOODING {
enum NoiseFlooding {
// noise flooding distribution parameter for distributed decryption in threshold FHE
MP_SD = 1048576,
// noise flooding distribution parameter for fixed 20 bits noise multihop PRE
Expand All @@ -167,11 +166,11 @@ enum NOISE_FLOODING {
#else
MULTIPARTY_MOD_SIZE = MAX_MODULUS_SIZE,
#endif
}; // namespace NOISE_FLOODING
};

// Defining the level to which the input ciphertext is brought to before
// interactive multi-party bootstrapping
enum COMPRESSION_LEVEL {
enum COMPRESSION_LEVEL { // TODO (dsuponit): change it to camel case
// we don't support 0 or 1 compression levels
// do not change values here

Expand Down
44 changes: 44 additions & 0 deletions src/pke/include/scheme/bfvrns/gen-cryptocontext-bfvrns-params.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,50 @@ class CCParams<CryptoContextBFVRNS> : public Params {
explicit CCParams(const std::vector<std::string>& vals) : Params(vals) {}
CCParams(const CCParams& obj) = default;
CCParams(CCParams&& obj) = default;

//================================================================================================================
// DISABLE FUNCTIONS that are not applicable to BFVRNS
//================================================================================================================
template <typename T = bool>
void SetScalingTechnique(ScalingTechnique scalTech0) {
DISABLED_FOR_BFVRNS;
}
template <typename T = bool>
void SetFirstModSize(usint firstModSize0) {
DISABLED_FOR_BFVRNS;
}
template <typename T = bool>
void SetMultiHopModSize(usint multiHopModSize0) {
DISABLED_FOR_BFVRNS;
}
template <typename T = bool>
void SetExecutionMode(ExecutionMode executionMode0) {
DISABLED_FOR_BGVRNS;
}
template <typename T = bool>
void SetDecryptionNoiseMode(DecryptionNoiseMode decryptionNoiseMode0) {
DISABLED_FOR_BFVRNS;
}
template <typename T = bool>
void SetNoiseEstimate(double noiseEstimate0) {
DISABLED_FOR_BFVRNS;
}
template <typename T = bool>
void SetDesiredPrecision(double desiredPrecision0) {
DISABLED_FOR_BFVRNS;
}
template <typename T = bool>
void SetStatisticalSecurity(uint32_t statisticalSecurity0) {
DISABLED_FOR_BFVRNS;
}
template <typename T = bool>
void SetNumAdversarialQueries(uint32_t numAdversarialQueries0) {
DISABLED_FOR_BFVRNS;
}
template <typename T = bool>
void SetInteractiveBootCompressionLevel(COMPRESSION_LEVEL interactiveBootCompressionLevel0) {
DISABLED_FOR_BFVRNS;
}
};
//====================================================================================================================

Expand Down
2 changes: 2 additions & 0 deletions src/pke/include/scheme/bfvrns/gen-cryptocontext-bfvrns.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
#include "scheme/bfvrns/gen-cryptocontext-bfvrns-params.h"
#include "scheme/bfvrns/bfvrns-scheme.h"
#include "scheme/bfvrns/bfvrns-cryptoparameters.h"
#include "scheme/gen-cryptocontext-params-validation.h"
#include "cryptocontext-fwd.h"
#include "lattice/lat-hal.h"

Expand All @@ -58,6 +59,7 @@ class CryptoContextBFVRNS {
using CryptoParams = CryptoParametersBFVRNS;

static CryptoContext<Element> genCryptoContext(const CCParams<CryptoContextBFVRNS>& parameters) {
validateParametersForCryptocontext(parameters);
return genCryptoContextBFVRNSInternal<CryptoContextBFVRNS, Element>(parameters);
}
};
Expand Down
36 changes: 36 additions & 0 deletions src/pke/include/scheme/bgvrns/gen-cryptocontext-bgvrns-params.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,42 @@ class CCParams<CryptoContextBGVRNS> : public Params {
explicit CCParams(const std::vector<std::string>& vals) : Params(vals) {}
CCParams(const CCParams& obj) = default;
CCParams(CCParams&& obj) = default;

//================================================================================================================
// DISABLE FUNCTIONS that are not applicable to BGVRNS
//================================================================================================================
template <typename T = bool>
void SetScalingModSize(usint scalingModSize0) {
DISABLED_FOR_BGVRNS;
}
template <typename T = bool>
void SetEncryptionTechnique(EncryptionTechnique encryptionTechnique0) {
DISABLED_FOR_BGVRNS;
}
template <typename T = bool>
void SetMultiplicationTechnique(MultiplicationTechnique multiplicationTechnique0) {
DISABLED_FOR_BGVRNS;
}
template <typename T = bool>
void SetExecutionMode(ExecutionMode executionMode0) {
DISABLED_FOR_BGVRNS;
}
template <typename T = bool>
void SetDecryptionNoiseMode(DecryptionNoiseMode decryptionNoiseMode0) {
DISABLED_FOR_BGVRNS;
}
template <typename T = bool>
void SetNoiseEstimate(double noiseEstimate0) {
DISABLED_FOR_BGVRNS;
}
template <typename T = bool>
void SetDesiredPrecision(double desiredPrecision0) {
DISABLED_FOR_BGVRNS;
}
template <typename T = bool>
void SetInteractiveBootCompressionLevel(COMPRESSION_LEVEL interactiveBootCompressionLevel0) {
DISABLED_FOR_BGVRNS;
}
};
//====================================================================================================================

Expand Down
2 changes: 2 additions & 0 deletions src/pke/include/scheme/bgvrns/gen-cryptocontext-bgvrns.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
#include "scheme/bgvrns/gen-cryptocontext-bgvrns-params.h"
#include "scheme/bgvrns/bgvrns-scheme.h"
#include "scheme/bgvrns/bgvrns-cryptoparameters.h"
#include "scheme/gen-cryptocontext-params-validation.h"
#include "cryptocontext-fwd.h"
#include "lattice/lat-hal.h"

Expand All @@ -58,6 +59,7 @@ class CryptoContextBGVRNS {
using CryptoParams = CryptoParametersBGVRNS;

static CryptoContext<Element> genCryptoContext(const CCParams<CryptoContextBGVRNS>& parameters) {
validateParametersForCryptocontext(parameters);
return genCryptoContextBGVRNSInternal<CryptoContextBGVRNS, Element>(parameters);
}
};
Expand Down
36 changes: 36 additions & 0 deletions src/pke/include/scheme/ckksrns/gen-cryptocontext-ckksrns-params.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,42 @@ class CCParams<CryptoContextCKKSRNS> : public Params {
explicit CCParams(const std::vector<std::string>& vals) : Params(vals) {}
CCParams(const CCParams& obj) = default;
CCParams(CCParams&& obj) = default;

//================================================================================================================
// DISABLE FUNCTIONS that are not applicable to CKKSRNS
//================================================================================================================
template <typename T = bool>
void SetPlaintextModulus(PlaintextModulus ptModulus0) {
DISABLED_FOR_CKKSRNS;
}
template <typename T = bool>
void SetEvalAddCount(usint evalAddCount0) {
DISABLED_FOR_CKKSRNS;
}
template <typename T = bool>
void SetKeySwitchCount(usint keySwitchCount0) {
DISABLED_FOR_CKKSRNS;
}
template <typename T = bool>
void SetEncryptionTechnique(EncryptionTechnique encryptionTechnique0) {
DISABLED_FOR_CKKSRNS;
}
template <typename T = bool>
void SetMultiplicationTechnique(MultiplicationTechnique multiplicationTechnique0) {
DISABLED_FOR_CKKSRNS;
}
template <typename T = bool>
void SetMultiHopModSize(usint multiHopModSize0) {
DISABLED_FOR_CKKSRNS;
}
template <typename T = bool>
void SetMultipartyMode(MultipartyMode multipartyMode0) {
DISABLED_FOR_CKKSRNS;
}
template <typename T = bool>
void SetThresholdNumOfParties(uint32_t thresholdNumOfParties0) {
DISABLED_FOR_CKKSRNS;
}
};
//====================================================================================================================

Expand Down
2 changes: 2 additions & 0 deletions src/pke/include/scheme/ckksrns/gen-cryptocontext-ckksrns.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
#include "scheme/ckksrns/gen-cryptocontext-ckksrns-params.h"
#include "scheme/ckksrns/ckksrns-cryptoparameters.h"
#include "scheme/ckksrns/ckksrns-scheme.h"
#include "scheme/gen-cryptocontext-params-validation.h"
#include "cryptocontextfactory.h"

namespace lbcrypto {
Expand All @@ -55,6 +56,7 @@ class CryptoContextCKKSRNS {
using CryptoParams = CryptoParametersCKKSRNS;

static CryptoContext<Element> genCryptoContext(const CCParams<CryptoContextCKKSRNS>& parameters) {
validateParametersForCryptocontext(parameters);
return genCryptoContextCKKSRNSInternal<CryptoContextCKKSRNS, Element>(parameters);
}
};
Expand Down
26 changes: 11 additions & 15 deletions src/pke/include/scheme/gen-cryptocontext-params-defaults.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,17 @@ constexpr PlaintextModulus ptModulus = 0;
constexpr usint digitSize = 0;
constexpr float standardDeviation = 3.19;
constexpr SecretKeyDist secretKeyDist = UNIFORM_TERNARY;
constexpr int maxRelinSkDeg = 2;
constexpr uint32_t maxRelinSkDeg = 2;
constexpr KeySwitchTechnique ksTech = HYBRID;
// Backend-specific settings for CKKS
#if NATIVEINT == 128 && !defined(__EMSCRIPTEN__)
constexpr ScalingTechnique scalTech = FIXEDAUTO;
constexpr usint firstModSize = 105;
constexpr usint firstModSize = 89;
constexpr usint scalingModSize = 78;
#else
constexpr ScalingTechnique scalTech = FLEXIBLEAUTOEXT;
constexpr usint firstModSize = 60;
constexpr usint scalingModSize = 59;
constexpr usint scalingModSize = 50;
#endif
constexpr usint batchSize = 0;
constexpr uint32_t numLargeDigits = 0;
Expand Down Expand Up @@ -89,17 +89,13 @@ constexpr PlaintextModulus ptModulus = 0;
constexpr usint digitSize = 0;
constexpr float standardDeviation = 3.19;
constexpr SecretKeyDist secretKeyDist = UNIFORM_TERNARY;
constexpr int maxRelinSkDeg = 2;
constexpr uint32_t maxRelinSkDeg = 2;
constexpr KeySwitchTechnique ksTech = BV;
constexpr ScalingTechnique scalTech = NORESCALE;
#if defined(HAVE_INT128) || NATIVEINT != 64
constexpr usint firstModSize = 60;
#else
constexpr usint firstModSize = 57;
#endif
constexpr usint batchSize = 0;
constexpr uint32_t numLargeDigits = 0;
constexpr usint multiplicativeDepth = 1;
constexpr usint firstModSize = 0;
constexpr usint batchSize = 0;
constexpr uint32_t numLargeDigits = 0;
constexpr usint multiplicativeDepth = 1;
#if defined(HAVE_INT128) || NATIVEINT != 64
constexpr usint scalingModSize = 60;
#else
Expand All @@ -118,8 +114,8 @@ constexpr ExecutionMode executionMode = EXEC_EVALUATION;
constexpr DecryptionNoiseMode decryptionNoiseMode = FIXED_NOISE_DECRYPT;
constexpr double noiseEstimate = 0;
constexpr double desiredPrecision = 0;
constexpr uint32_t statisticalSecurity = 30;
constexpr uint32_t numAdversarialQueries = 1;
constexpr uint32_t statisticalSecurity = 0;
constexpr uint32_t numAdversarialQueries = 0;
constexpr uint32_t thresholdNumOfParties = 1;
constexpr COMPRESSION_LEVEL interactiveBootCompressionLevel = SLACK;
}; // namespace BFVRNS_SCHEME_DEFAULTS
Expand All @@ -130,7 +126,7 @@ constexpr PlaintextModulus ptModulus = 0;
constexpr usint digitSize = 0;
constexpr float standardDeviation = 3.19;
constexpr SecretKeyDist secretKeyDist = UNIFORM_TERNARY;
constexpr int maxRelinSkDeg = 2;
constexpr uint32_t maxRelinSkDeg = 2;
constexpr KeySwitchTechnique ksTech = HYBRID;
constexpr ScalingTechnique scalTech = FLEXIBLEAUTOEXT;
constexpr usint firstModSize = 0;
Expand Down
46 changes: 46 additions & 0 deletions src/pke/include/scheme/gen-cryptocontext-params-validation.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
//==================================================================================
// BSD 2-Clause License
//
// Copyright (c) 2014-2024, 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.
//==================================================================================
#ifndef __GEN_CRYPTOCONTEXT_PARAMS_VALIDATION_H__
#define __GEN_CRYPTOCONTEXT_PARAMS_VALIDATION_H__

#include "scheme/gen-cryptocontext-params.h"

namespace lbcrypto {
/**
* @brief Validate parameters for generating cryptocontext. Doesn't validate the parameters which set functions
* are disabled as they cannot be set by users
* @param parameters scheme-specific parameters to generate cryptocontext
*/
void validateParametersForCryptocontext(const Params& parameters);

} // namespace lbcrypto

#endif // __GEN_CRYPTOCONTEXT_PARAMS_VALIDATION_H__
Loading

0 comments on commit 2589b2c

Please sign in to comment.