# BFV Ciphertext multiplication with default parameter setting

**URL:** <https://openfhe.discourse.group/t/bfv-ciphertext-multiplication-with-default-parameter-setting/1364>\
**Category:** Library Questions\
**Tags:** questions, openfhe-help\
**Created:** [June 18, 2024, 1:58pm UTC](https://openfhe.discourse.group/t/bfv-ciphertext-multiplication-with-default-parameter-setting/1364 "2024-06-18T13:58:11Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Atwo](https://avatars.discourse-cdn.com/v4/letter/a/e99b99/32.png) [@Atwo](https://openfhe.discourse.group/u/Atwo)\
**Post date:** [June 18, 2024, 1:58pm UTC](https://openfhe.discourse.group/t/bfv-ciphertext-multiplication-with-default-parameter-setting/1364/1 "2024-06-18T13:58:12Z")

</div>

Hi everyone,

I am using the default parameter setting for multiplying BFV ciphertexts.  
I am implementing the same code at [openfhe-development/src/pke/examples/simple-integers.cpp at main · openfheorg/openfhe-development · GitHub](https://github.com/openfheorg/openfhe-development/blob/main/src/pke/examples/simple-integers.cpp)  
I changed the multiplicative depth from 2 to 3 and by binary tree multiplication it should multiply 8 ciphertext-ciphertext multiplication but i can only multiply 4 ciphertext-ciphertext multiplication.  
What is the default ciphertext-ciphertext multiplication in BFV encryption mechanism.

Thanks in advance!.

---

<div class="post-metadata">

**Author:** ![andreea.alexandru](https://yyz1.discourse-cdn.com/flex031/user_avatar/openfhe.discourse.group/andreea.alexandru/32/378_2.png) [@andreea.alexandru](https://openfhe.discourse.group/u/andreea.alexandru)\
**Post date:** [June 18, 2024, 5:49pm UTC](https://openfhe.discourse.group/t/bfv-ciphertext-multiplication-with-default-parameter-setting/1364/2 "2024-06-18T17:49:55Z")

</div>

How are you performing the multiplication? Is it like this

````auto
    auto ciphertextMul12 = cryptoContext->EvalMult(ciphertext1, ciphertext2);
    auto ciphertextMul34 = cryptoContext->EvalMult(ciphertext3, ciphertext4);
    auto ciphertextMul56 = cryptoContext->EvalMult(ciphertext5, ciphertext6);
    auto ciphertextMul78 = cryptoContext->EvalMult(ciphertext7, ciphertext8);
    auto ciphertextMul1234 = cryptoContext->EvalMult(ciphertextMul12, ciphertextMul34);
    auto ciphertextMul5678 = cryptoContext->EvalMult(ciphertextMul56, ciphertextMul78);
    auto ciphertextMultResult = cryptoContext->EvalMult(ciphertextMul1234, ciphertextMul5678);```

Please post a minimum working example such that we identify the issue.
````

---

<div class="post-metadata">

**Author:** ![Atwo](https://avatars.discourse-cdn.com/v4/letter/a/e99b99/32.png) [@Atwo](https://openfhe.discourse.group/u/Atwo)\
**Post date:** [June 18, 2024, 6:08pm UTC](https://openfhe.discourse.group/t/bfv-ciphertext-multiplication-with-default-parameter-setting/1364/3 "2024-06-18T18:08:43Z")

</div>

Thanks for your response .  
Below is the snippet of my code. when i giving total plaintext as 4 then it works correctly but on giving higher vaule it gives incorrect result.

std::vector plaintexts(numPlaintexts);  
std::vector\<Ciphertext\> ciphertexts(numPlaintexts);

```
    for (int i = 0; i < numPlaintexts; ++i) {
        plaintexts[i] = cryptoContext->MakePackedPlaintext(vectors[i]);
        ciphertexts[i] = cryptoContext->Encrypt(keyPair.publicKey, plaintexts[i]);
    }

    // Sample Program: Step 4: Evaluation

    // Homomorphic multiplications
    auto ciphertextMul = ciphertexts[0];
    for (int i = 1; i < numPlaintexts; ++i) {
        ciphertextMul = cryptoContext->EvalMult(ciphertextMul, ciphertexts[i]);
    }

```

---

<div class="post-metadata">

**Author:** ![andreea.alexandru](https://yyz1.discourse-cdn.com/flex031/user_avatar/openfhe.discourse.group/andreea.alexandru/32/378_2.png) [@andreea.alexandru](https://openfhe.discourse.group/u/andreea.alexandru)\
**Post date:** [June 18, 2024, 6:28pm UTC](https://openfhe.discourse.group/t/bfv-ciphertext-multiplication-with-default-parameter-setting/1364/4 "2024-06-18T18:28:24Z")

</div>

The way you are doing the multiplication requires a multiplicative depth of numPlaintexts - 1. To achieve a smaller multiplicative depth, you have to multiply ciphertexts in a binary tree shape, the way I showed in my previous post.

---

<div class="post-metadata">

**Author:** ![ypolyakov](https://yyz1.discourse-cdn.com/flex031/user_avatar/openfhe.discourse.group/ypolyakov/32/47_2.png) [@ypolyakov](https://openfhe.discourse.group/u/ypolyakov)\
**Post date:** [June 18, 2024, 11:37pm UTC](https://openfhe.discourse.group/t/bfv-ciphertext-multiplication-with-default-parameter-setting/1364/5 "2024-06-18T23:37:02Z")

</div>

Note that OpenFHE API includes an EvalMultMany operation ([Template Class CryptoContextImpl — OpenFHE documentation](https://openfhe-development.readthedocs.io/en/latest/api/classlbcrypto_1_1CryptoContextImpl.html?highlight=EvalMultMany#_CPPv4NK8lbcrypto17CryptoContextImpl12EvalMultManyERKNSt6vectorI10CiphertextI7ElementEEE)) that multiplies ciphertexts in a vector using the binary tree method.
