CKKSCiphertext ScaleEstimator::encrypt()

in src/hit/api/evaluator/scaleestimator.cpp [43:75]


    CKKSCiphertext ScaleEstimator::encrypt(const vector<double> &coeffs, int level) {
        if (level < 0) {
            LOG_AND_THROW_STREAM("Explicit encryption level must be non-negative; got " << level);
        }
        update_plaintext_max_val(coeffs);

        if (coeffs.size() != context->num_slots()) {
            // bad things can happen if you don't plan for your input to be smaller than the ciphertext
            // This forces the caller to ensure that the input has the correct size or is at least appropriately padded
            LOG_AND_THROW_STREAM("You can only encrypt vectors which have exactly as many "
                                 << " coefficients as the number of plaintext slots: Expected " << context->num_slots()
                                 << " coefficients, but " << coeffs.size() << " were provided");
        }

        if (level == -1) {
            level = context->max_ciphertext_level();
        }

        double scale = pow(2, context->log_scale());
        // order of operations is very important: floating point arithmetic is not associative
        for (int i = context->max_ciphertext_level(); i > level; i--) {
            scale = (scale * scale) / static_cast<double>(context->get_qi(i));
        }

        CKKSCiphertext destination;
        destination.he_level_ = level;
        destination.scale_ = scale;
        destination.raw_pt = coeffs;
        destination.num_slots_ = context->num_slots();
        destination.initialized = true;

        return destination;
    }