# FHE multiplication

**URL:** <https://openfhe.discourse.group/t/fhe-multiplication/1335>\
**Category:** FHE Questions\
**Created:** [June 10, 2024, 10:53am UTC](https://openfhe.discourse.group/t/fhe-multiplication/1335 "2024-06-10T10:53:03Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![sam](https://avatars.discourse-cdn.com/v4/letter/s/5f9b8f/32.png) [@sam](https://openfhe.discourse.group/u/sam)\
**Post date:** [June 10, 2024, 10:53am UTC](https://openfhe.discourse.group/t/fhe-multiplication/1335/1 "2024-06-10T10:53:03Z")

</div>

When i am trying to multiply ciphertext with constant(plaintext). i am getting the below error.

auto ciphertext\_shifted\_left = cryptoContext-\>GetScheme()-\>MultByInteger(ciphertext,constant);

terminate called after throwing an instance of ‘lbcrypto::OpenFHEException’  
what(): /home/pi2/openfhe-development/src/pke/include/schemebase/base-leveledshe.h:l.439:MultByInteger(): MultByInteger is not implemented for this scheme  
Aborted.

How to fix this.

---

<div class="post-metadata">

**Author:** ![Caesar](https://yyz1.discourse-cdn.com/flex031/user_avatar/openfhe.discourse.group/caesar/32/63_2.png) [@Caesar](https://openfhe.discourse.group/u/Caesar)\
**Post date:** [June 10, 2024, 7:34pm UTC](https://openfhe.discourse.group/t/fhe-multiplication/1335/2 "2024-06-10T19:34:40Z")

</div>

Which scheme are you using? This method is only supported by CKKS AFAIK.

---

<div class="post-metadata">

**Author:** ![sam](https://avatars.discourse-cdn.com/v4/letter/s/5f9b8f/32.png) [@sam](https://openfhe.discourse.group/u/sam)\
**Post date:** [June 10, 2024, 9:43pm UTC](https://openfhe.discourse.group/t/fhe-multiplication/1335/3 "2024-06-10T21:43:21Z")

</div>

I am using BFV scheme. Is there any other way to implement this in BFV scheme

---

<div class="post-metadata">

**Author:** ![Caesar](https://yyz1.discourse-cdn.com/flex031/user_avatar/openfhe.discourse.group/caesar/32/63_2.png) [@Caesar](https://openfhe.discourse.group/u/Caesar)\
**Post date:** [June 10, 2024, 10:07pm UTC](https://openfhe.discourse.group/t/fhe-multiplication/1335/4 "2024-06-10T22:07:22Z")

</div>

Say you want to multiply an encrypted vector of length n by a constant integer a.  
In BFV, you can create a plaintext object that encodes a vector of length n of values a, and then multiply your encrypted vector with this plaintext.

Here is a sample code.

```auto
std::vector<int64_t> v(n, a); // n-vec of a's
Plaintext pt = cryptoContext->MakePackedPlaintext(v); // encoding

auto out_ct = cryptoContext->EvalMult(in_ct, pt); // homomorphic multiplication by a constant plaintext (cheap operation in BFV)

```

---

<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 10, 2024, 10:49pm UTC](https://openfhe.discourse.group/t/fhe-multiplication/1335/5 "2024-06-10T22:49:28Z")

</div>

To complement Caesar’s answer, you can also write your own method for ciphertext-constant multiplication in BFV, along the lines below:

```auto
Ciphertext<DCRTPoly> MultByInteger(const ConstCiphertext<DCRTPoly>& ciphertext, const int64_t constant) {
    auto result = ciphertext->Clone();
    for (auto& c : result->GetElements())
        c *= constant;
    return result;
}

```

---

<div class="post-metadata">

**Author:** ![sam](https://avatars.discourse-cdn.com/v4/letter/s/5f9b8f/32.png) [@sam](https://openfhe.discourse.group/u/sam)\
**Post date:** [June 11, 2024, 5:58am UTC](https://openfhe.discourse.group/t/fhe-multiplication/1335/6 "2024-06-11T05:58:11Z")

</div>

Hi this works fine.  
Is there any option to do bitwise shift operation in ciphertext if our constant is multiple of 2. then we can directly do leftshift operation in ciphertext. will this operation be cheaper than multiplication

---

<div class="post-metadata">

**Author:** ![Caesar](https://yyz1.discourse-cdn.com/flex031/user_avatar/openfhe.discourse.group/caesar/32/63_2.png) [@Caesar](https://openfhe.discourse.group/u/Caesar)\
**Post date:** [June 11, 2024, 2:40pm UTC](https://openfhe.discourse.group/t/fhe-multiplication/1335/7 "2024-06-11T14:40:48Z")

</div>

I am not aware of an equivalent homomorphic arithmetic shift in BFV similar to the conventional arithmetic shift you described. Anyway, the computational complexity of the solution above is relatively small especially compared with ciphertext-ciphertext multiplication.
