Program Listing for File bgvrns-parametergeneration.h

↰ Return to documentation for file (pke/include/scheme/bgvrns/bgvrns-parametergeneration.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.
//==================================================================================

#ifndef SRC_PKE_INCLUDE_SCHEME_BGVRNS_BGVRNS_PARAMETERGENERATION_H_
#define SRC_PKE_INCLUDE_SCHEME_BGVRNS_BGVRNS_PARAMETERGENERATION_H_

#include <cstdint>
#include <memory>
#include <string>
#include <utility>
#include <vector>

#include "schemerns/rns-parametergeneration.h"

namespace lbcrypto {

struct BGVNoiseEstimates {
    double Berr;
    double Bkey;
    double expansionFactor;
    double freshEncryptionNoise;
    double keySwitchingNoise;
    double modSwitchingNoise;
    double noisePerLevel;

    BGVNoiseEstimates(double Berr0, double Bkey0, double expansionFactor0, double freshEncryptionNoise0,
                      double keySwitchingNoise0, double modSwitchingNoise0, double noisePerLevel0)
        : Berr(Berr0),
          Bkey(Bkey0),
          expansionFactor(expansionFactor0),
          freshEncryptionNoise(freshEncryptionNoise0),
          keySwitchingNoise(keySwitchingNoise0),
          modSwitchingNoise(modSwitchingNoise0),
          noisePerLevel(noisePerLevel0) {}
};

class ParameterGenerationBGVRNS : public ParameterGenerationRNS {
  public:
    virtual ~ParameterGenerationBGVRNS() {}

    bool ParamsGenBGVRNSInternal(std::shared_ptr<CryptoParametersBase<DCRTPoly>> cryptoParams, uint32_t evalAddCount,
                                 uint32_t keySwitchCount, uint32_t cyclOrder, uint32_t numPrimes, uint32_t firstModSize,
                                 uint32_t dcrtBits, uint32_t numPartQ, uint32_t numHops) const override;

    // SERIALIZATION

    template <class Archive>
    void save(Archive& ar, std::uint32_t const version) const {}

    template <class Archive>
    void load(Archive& ar, std::uint32_t const version) {}

    std::string SerializedObjectName() const {
        return "ParameterGenerationBGVRNS";
    }

  private:
    uint32_t computeRingDimension(const std::shared_ptr<CryptoParametersBase<DCRTPoly>>& cryptoParams, uint32_t qBound,
                                  uint32_t cyclOrder) const;

    BGVNoiseEstimates computeNoiseEstimates(const std::shared_ptr<CryptoParametersBase<DCRTPoly>>& cryptoParams,
                                            uint32_t ringDimension, uint32_t evalAddCount, uint32_t keySwitchCount,
                                            uint32_t auxTowers, uint32_t numPrimes) const;

    uint64_t getCyclicOrder(const uint32_t ringDimension, const int plainModulus,
                            const ScalingTechnique scalTech) const;

    std::pair<std::vector<NativeInteger>, uint32_t> computeModuli(
            const std::shared_ptr<CryptoParametersBase<DCRTPoly>>& cryptoParams, uint32_t ringDimension,
            uint32_t evalAddCount, uint32_t keySwitchCount, uint32_t auxTowers, uint32_t numPrimes) const;

    void InitializeFloodingDgg(const std::shared_ptr<CryptoParametersBase<DCRTPoly>>& cryptoParams, uint32_t numPrimes,
                               uint32_t ringDimension) const;
};

}  // namespace lbcrypto

#endif  // SRC_PKE_INCLUDE_SCHEME_BGVRNS_BGVRNS_PARAMETERGENERATION_H_