Multiple Evaluations of EvalChebyshevFunction in CKKS ciphertext

I have a CKKs ciphertext in OpenFHE and I have carried out a handful of operations on it.
I am trying to evaluate a EvalChebyshevFunction function for the second time but I am getting the following error:

dcrtpoly-impl.h:l.695:DropLastElement(): DropLastElement: Removing last element of DCRTPoly renders it invalid.

I understand that this error comes from a large noise budget.

I have done

auto boots_second_cph = context->EvalBootstrap(cph, 2);
before trying to evaluate again. The function I am trying to evaluate with EvalChebyshevFunction is a simple greater than function which I believe takes 5 multiplicative levels to evaluate.

Trying to evaluate the boots_second_cph still give the error above. How can I bootstrap more efficiently or fix this issue if it is not a bootstrapping issue?

My context is with the following parameters:


    CCParams<CryptoContextCKKSRNS> parameters;
    num_slots = 1 << 12;
    parameters.SetSecretKeyDist(SPARSE_TERNARY);
    parameters.SetSecurityLevel(lbcrypto::HEStd_NotSet);
    parameters.SetNumLargeDigits(3);
    parameters.SetRingDim(1 << 14);
    parameters.SetBatchSize(num_slots);
    level_budget = {4, 4};

    ScalingTechnique rescaleTech = FLEXIBLEAUTO;

    int dcrtBits               = 47;
    int firstMod               = 52;

    parameters.SetScalingModSize(dcrtBits);
    parameters.SetScalingTechnique(rescaleTech);
    parameters.SetFirstModSize(firstMod);

    uint32_t approxBootstrapDepth = 2 + 2;

    uint32_t levelsUsedBeforeBootstrap = 4;
    circuit_depth = levelsUsedBeforeBootstrap + FHECKKSRNS::GetBootstrapDepth(approxBootstrapDepth, level_budget, SPARSE_TERNARY);
    
    parameters.SetMultiplicativeDepth(circuit_depth);
    context = GenCryptoContext(parameters);
    
    context->Enable(PKE);
    context->Enable(KEYSWITCH);
    context->Enable(LEVELEDSHE);
    context->Enable(ADVANCEDSHE);
    context->Enable(FHE);

    keyPair = context->KeyGen();
    context->EvalMultKeyGen(keyPair.secretKey);

    std::vector<uint32_t> levelBudget = {2, 2};
    std::vector<uint32_t> bsgsDim     = {0, 0};
    usint numSlots = context->GetRingDimension() / 2;
    usint halfnumSlots = numSlots/2;
    context->EvalBootstrapSetup(levelBudget, bsgsDim, numSlots);
    context->EvalBootstrapKeyGen(keyPair.secretKey, numSlots);

    context->EvalBootstrapSetup(levelBudget, bsgsDim, halfnumSlots);
    context->EvalBootstrapKeyGen(keyPair.secretKey, halfnumSlots);

Your code is a bit confusing. You compute the depth using a different level budget than actually supplied in EvalBootstrapSetup. Also, the GetBootstrapDepth you are using will be deprecated. It is better to set your depth as such:
depth = levelsAvailableAfterBootstrap + FHECKKSRNS::GetBootstrapDepth(levelBudget, secretKeyDist);
The levelsAvailableAfterBootstrap should be large enough to be able to support your computations. So in your case, it should be at least 5.

Thank you . I figured it out and yes you are right.