Skip to content

Commit

Permalink
checkpoint for initial testing
Browse files Browse the repository at this point in the history
  • Loading branch information
pascoec committed Nov 28, 2023
1 parent 47fe72e commit ee61783
Show file tree
Hide file tree
Showing 27 changed files with 358 additions and 439 deletions.
8 changes: 4 additions & 4 deletions benchmark/src/VectorMath.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ static void add_BigVec(const V& a, const V& b) {
template <typename V>
static void BM_BigVec_Add(benchmark::State& state) {
auto p = state.range(0);
auto q = GetMaxPrime<typename V::Integer>(MAX_MODULUS_SIZE, p);
auto q = LastPrime<typename V::Integer>(MAX_MODULUS_SIZE, p);
V a = DiscreteUniformGeneratorImpl<V>().GenerateVector(p, q);
V b = DiscreteUniformGeneratorImpl<V>().GenerateVector(p, q);
while (state.KeepRunning()) {
Expand All @@ -69,7 +69,7 @@ static void addeq_BigVec(V& a, const V& b) {
template <typename V>
static void BM_BigVec_Addeq(benchmark::State& state) {
auto p = state.range(0);
auto q = GetMaxPrime<typename V::Integer>(MAX_MODULUS_SIZE, p);
auto q = LastPrime<typename V::Integer>(MAX_MODULUS_SIZE, p);
V a = DiscreteUniformGeneratorImpl<V>().GenerateVector(p, q);
V b = DiscreteUniformGeneratorImpl<V>().GenerateVector(p, q);
while (state.KeepRunning()) {
Expand All @@ -85,7 +85,7 @@ static void mult_BigVec(const V& a, const V& b) {
template <typename V>
static void BM_BigVec_Mult(benchmark::State& state) {
auto p = state.range(0);
auto q = GetMaxPrime<typename V::Integer>(MAX_MODULUS_SIZE, p);
auto q = LastPrime<typename V::Integer>(MAX_MODULUS_SIZE, p);
V a = DiscreteUniformGeneratorImpl<V>().GenerateVector(p, q);
V b = DiscreteUniformGeneratorImpl<V>().GenerateVector(p, q);
while (state.KeepRunning()) {
Expand All @@ -101,7 +101,7 @@ static void multeq_BigVec(V& a, const V& b) {
template <typename V>
static void BM_BigVec_Multeq(benchmark::State& state) {
auto p = state.range(0);
auto q = GetMaxPrime<typename V::Integer>(MAX_MODULUS_SIZE, p);
auto q = LastPrime<typename V::Integer>(MAX_MODULUS_SIZE, p);
V a = DiscreteUniformGeneratorImpl<V>().GenerateVector(p, q);
V b = DiscreteUniformGeneratorImpl<V>().GenerateVector(p, q);
while (state.KeepRunning()) {
Expand Down
14 changes: 6 additions & 8 deletions src/binfhe/include/lwe-cryptoparameters.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ namespace lbcrypto {
*/
class LWECryptoParams : public Serializable {
public:
// NativeInteger m_qKS = 1<<20; //PreviousPrime<NativeInteger>(FirstPrime<NativeInteger>(26, 2048), 2048);
LWECryptoParams() = default;

/**
Expand All @@ -68,19 +67,18 @@ class LWECryptoParams : public Serializable {
const NativeInteger& q_KS, double std, uint32_t baseKS,
SecretKeyDist keyDist = UNIFORM_TERNARY)
: m_q(q), m_Q(Q), m_qKS(q_KS), m_n(n), m_N(N), m_baseKS(baseKS), m_keyDist(keyDist) {
if(!m_n)
if (!m_n)
OPENFHE_THROW(config_error, "m_n (lattice parameter) can not be zero");
if(!m_N)
if (!m_N)
OPENFHE_THROW(config_error, "m_N (ring dimension) can not be zero");
if(!m_q)
if (!m_q)
OPENFHE_THROW(config_error, "m_q (modulus for additive LWE) can not be zero");
if(!m_Q)
if (!m_Q)
OPENFHE_THROW(config_error, "m_Q (modulus for RingGSW/RLWE) can not be zero");
if(!q_KS)
if (!q_KS)
OPENFHE_THROW(config_error, "q_KS (modulus for key switching) can not be zero");
if(!m_baseKS)
if (!m_baseKS)
OPENFHE_THROW(config_error, "m_baseKS (the base used for key switching) can not be zero");

if (m_Q.GetMSB() > MAX_MODULUS_SIZE)
OPENFHE_THROW(config_error, "Q.GetMSB() > MAX_MODULUS_SIZE");
m_dgg.SetStd(std);
Expand Down
8 changes: 3 additions & 5 deletions src/binfhe/lib/binfhecontext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ void BinFHEContext::GenerateBinFHEContext(BINFHE_PARAMSET set, bool arbFunc, uin
ringDim = N;
}
// find prime Q for NTT
NativeInteger Q = PreviousPrime<NativeInteger>(FirstPrime<NativeInteger>(logQprime, 2 * ringDim), 2 * ringDim);
NativeInteger Q = LastPrime<NativeInteger>(logQprime, 2 * ringDim);
// q = 2*ringDim by default for maximum plaintext space, if needed for arbitrary function evaluation, q = ringDim
uint32_t q = arbFunc ? ringDim : 2 * ringDim;

Expand Down Expand Up @@ -153,8 +153,7 @@ void BinFHEContext::GenerateBinFHEContext(BINFHE_PARAMSET set, BINFHE_METHOD met

BinFHEContextParams params = search->second;
// intermediate prime
NativeInteger Q(
PreviousPrime<NativeInteger>(FirstPrime<NativeInteger>(params.numberBits, params.cyclOrder), params.cyclOrder));
NativeInteger Q(LastPrime<NativeInteger>(params.numberBits, params.cyclOrder));

usint ringDim = params.cyclOrder / 2;
auto lweparams = (PRIME == params.modKS) ?
Expand All @@ -173,8 +172,7 @@ void BinFHEContext::GenerateBinFHEContext(BINFHE_PARAMSET set, BINFHE_METHOD met
void BinFHEContext::GenerateBinFHEContext(const BinFHEContextParams& params, BINFHE_METHOD method) {
enum { PRIME = 0 }; // value for modKS if you want to use the intermediate prime for modulus for key switching
// intermediate prime
NativeInteger Q(
PreviousPrime<NativeInteger>(FirstPrime<NativeInteger>(params.numberBits, params.cyclOrder), params.cyclOrder));
NativeInteger Q(LastPrime<NativeInteger>(params.numberBits, params.cyclOrder));

usint ringDim = params.cyclOrder / 2;

Expand Down
19 changes: 11 additions & 8 deletions src/core/include/lattice/hal/default/ildcrtparams.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,9 @@ class ILDCRTParams final : public ElemParams<IntType> {
if (corder == 0)
return;

auto q{FirstPrime<NativeInteger>(MAX_MODULUS_SIZE, corder)};
auto q{LastPrime<NativeInteger>(MAX_MODULUS_SIZE, corder)};
m_params.reserve(32);
m_params.push_back(std::make_shared<ILNativeParams>(corder, (q = PreviousPrime(q, corder))));
m_params.push_back(std::make_shared<ILNativeParams>(corder, q));

IntType compositeModulus(1);
while ((compositeModulus *= IntType(q.template ConvertToInt<BasicInteger>())) < modulus)
Expand All @@ -92,7 +92,7 @@ class ILDCRTParams final : public ElemParams<IntType> {

/**
* @brief Constructor with basic parameter set.
* q is selected as FirstPrime(bits, order)
* q is selected as LastPrime(bits, order)
* @param corder the order of the ciphertext.
* @param depth is the size of the tower.
* @param bits is the number of bits of each tower's moduli.
Expand All @@ -104,9 +104,9 @@ class ILDCRTParams final : public ElemParams<IntType> {
if (bits > MAX_MODULUS_SIZE)
OPENFHE_THROW(config_error, "Invalid bits for ILDCRTParams");

auto q{FirstPrime<NativeInteger>(bits, corder)};
auto q{LastPrime<NativeInteger>(bits, corder)};
m_params.reserve(depth);
m_params.push_back(std::make_shared<ILNativeParams>(corder, (q = PreviousPrime(q, corder))));
m_params.push_back(std::make_shared<ILNativeParams>(corder, q));

IntType compositeModulus(q.template ConvertToInt<BasicInteger>());
for (uint32_t _ = 1; _ < depth; ++_) {
Expand Down Expand Up @@ -283,7 +283,8 @@ class ILDCRTParams final : public ElemParams<IntType> {
*/

void PopLastParam() {
ElemParams<IntType>::m_ciphertextModulus /= IntType(m_params.back()->GetModulus().template ConvertToInt<BasicInteger>());
ElemParams<IntType>::m_ciphertextModulus /=
IntType(m_params.back()->GetModulus().template ConvertToInt<BasicInteger>());
m_params.pop_back();
}

Expand All @@ -292,7 +293,8 @@ class ILDCRTParams final : public ElemParams<IntType> {
*
*/
void PopFirstParam() {
ElemParams<IntType>::m_ciphertextModulus /= IntType(m_params[0]->GetModulus().template ConvertToInt<BasicInteger>());
ElemParams<IntType>::m_ciphertextModulus /=
IntType(m_params[0]->GetModulus().template ConvertToInt<BasicInteger>());
m_params.erase(m_params.begin());
}

Expand Down Expand Up @@ -329,7 +331,8 @@ class ILDCRTParams final : public ElemParams<IntType> {
void RecalculateModulus() {
ElemParams<IntType>::m_ciphertextModulus = 1;
for (size_t i = 0; i < m_params.size(); ++i)
ElemParams<IntType>::m_ciphertextModulus *= IntType(m_params[i]->GetModulus().template ConvertToInt<BasicInteger>());
ElemParams<IntType>::m_ciphertextModulus *=
IntType(m_params[i]->GetModulus().template ConvertToInt<BasicInteger>());
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/core/include/lattice/hal/default/ilparams.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class ILParamsImpl final : public ElemParams<IntType> {
* @return
*/
explicit ILParamsImpl(uint32_t order, uint32_t bits = MAX_MODULUS_SIZE)
: ILParamsImpl<IntType>(order, GetMaxPrime<IntType>(bits, order)) {}
: ILParamsImpl<IntType>(order, LastPrime<IntType>(bits, order)) {}

explicit ILParamsImpl(uint32_t order, const IntType& modulus)
: ElemParams<IntType>(order, modulus, RootOfUnity<IntType>(order, modulus)) {}
Expand Down
Loading

0 comments on commit ee61783

Please sign in to comment.