# EvalMultMutable and EvalMultMutableInPlace produces wrong results for BFVRNS

**URL:** <https://openfhe.discourse.group/t/evalmultmutable-and-evalmultmutableinplace-produces-wrong-results-for-bfvrns/1724>\
**Category:** Library Questions\
**Tags:** bugs\
**Created:** [November 15, 2024, 10:46am UTC](https://openfhe.discourse.group/t/evalmultmutable-and-evalmultmutableinplace-produces-wrong-results-for-bfvrns/1724 "2024-11-15T10:46:51Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![g\_tasop](https://avatars.discourse-cdn.com/v4/letter/g/258eb7/32.png) [@g\_tasop](https://openfhe.discourse.group/u/g_tasop)\
**Post date:** [November 15, 2024, 10:46am UTC](https://openfhe.discourse.group/t/evalmultmutable-and-evalmultmutableinplace-produces-wrong-results-for-bfvrns/1724/1 "2024-11-15T10:46:51Z")

</div>

Hi, I am using the `openfhe-python` wrapper and I noticed that when using `EvalMultMutable()` with BFVRNS the produced result is _not correct_. I played around a bit and found out that with BGVRNS the function works properly. Additionally, the result stays the same across multiple calls of the function in the same script, but changes when you run the script again.

In the same time I found out that `EvalMultMutableInPlace()` does not work for either BFVRNS or BGVRNS. Any insight?

Currently I have only tested it in `openfhe-python`. I am using `python-openfhe` version 0.8.1 with `openfhe` version 1.2.0 on Ubuntu 22.04 with `gcc` version 11.4.0.

I include an example below to reproduce this behavior.

```python
from openfhe import *

# Set CryptoContext and FHE parameters
parameters = CCParamsBFVRNS()

depth = 1
plainmod = 786433
parameters.SetPlaintextModulus(plainmod)
parameters.SetMultiplicativeDepth(depth)
parameters.SetSecurityLevel(HEStd_128_classic)

crypto_context = GenCryptoContext(parameters)

crypto_context.Enable(PKESchemeFeature.PKE)
crypto_context.Enable(PKESchemeFeature.KEYSWITCH)
crypto_context.Enable(PKESchemeFeature.LEVELEDSHE)
crypto_context.Enable(ADVANCEDSHE)

# Generate a public/private key pair
key_pair = crypto_context.KeyGen()

# Generate the relinearization key
crypto_context.EvalMultKeyGen(key_pair.secretKey)

a = [5]
b = [8]

packed_a = crypto_context.MakePackedPlaintext(a)
packed_b = crypto_context.MakePackedPlaintext(b)

enc_a = crypto_context.Encrypt(key_pair.publicKey, packed_a)
enc_b = crypto_context.Encrypt(key_pair.publicKey, packed_b)

print("a: ", a)
print("b: ", b)

print(f"EvalMultMutable() with a and b with BFV...")

enc_result = []

for i in range(2):
    
    enc_result.append(crypto_context.EvalMultMutable(enc_a, enc_b))

    # Decrypt, set length and unpack
    decrypted_result = crypto_context.Decrypt(enc_result[i], key_pair.secretKey)
    decrypted_result.SetLength(1)
    dec = decrypted_result.GetPackedValue()
    print ("a * b: ", dec)

print(f"EvalMultMutableInPlace() with a and b with BFV...")

enc_result = []

for i in range(2):
    
    crypto_context.EvalMultMutableInPlace(enc_a, enc_b)
    enc_result.append(enc_a)

    # Decrypt, set length and unpack
    decrypted_result = crypto_context.Decrypt(enc_result[i], key_pair.secretKey)
    decrypted_result.SetLength(1)
    dec = decrypted_result.GetPackedValue()
    print ("a * b: ", dec)

# Set CryptoContext and FHE parameters
parameters = CCParamsBGVRNS()

depth = 1
plainmod = 786433
parameters.SetPlaintextModulus(plainmod)
parameters.SetMultiplicativeDepth(depth)
parameters.SetSecurityLevel(HEStd_128_classic)

crypto_context = GenCryptoContext(parameters)

crypto_context.Enable(PKESchemeFeature.PKE)
crypto_context.Enable(PKESchemeFeature.KEYSWITCH)
crypto_context.Enable(PKESchemeFeature.LEVELEDSHE)
crypto_context.Enable(ADVANCEDSHE)

# Generate a public/private key pair
key_pair = crypto_context.KeyGen()

# Generate the relinearization key
crypto_context.EvalMultKeyGen(key_pair.secretKey)

packed_a = crypto_context.MakePackedPlaintext(a)
packed_b = crypto_context.MakePackedPlaintext(b)

enc_a = crypto_context.Encrypt(key_pair.publicKey, packed_a)
enc_b = crypto_context.Encrypt(key_pair.publicKey, packed_b)

print(f"EvalMultMutable() with a and b with BGV...")

enc_result = []

for i in range(2):
    
    enc_result.append(crypto_context.EvalMultMutable(enc_a, enc_b))

    # Decrypt, set length and unpack
    decrypted_result = crypto_context.Decrypt(enc_result[i], key_pair.secretKey)
    decrypted_result.SetLength(1)
    dec = decrypted_result.GetPackedValue()
    print ("a * b: ", dec)

print(f"EvalMultMutableInPlace() with a and b with BGV...")

enc_result = []

for i in range(2):
    
    crypto_context.EvalMultMutableInPlace(enc_a, enc_b)
    enc_result.append(enc_a)

    # Decrypt, set length and unpack
    decrypted_result = crypto_context.Decrypt(enc_result[i], key_pair.secretKey)
    decrypted_result.SetLength(1)
    dec = decrypted_result.GetPackedValue()
    print ("a * b: ", dec)

```

and this is a sample output:

```plaintext
a: [5]
b: [8]
EvalMultMutable() with a and b with BFV...
a * b: [77167]
a * b: [77167]
EvalMultMutableInPlace() with a and b with BFV...
a * b: [5]
a * b: [5]
EvalMultMutable() with a and b with BGV...
a * b: [40]
a * b: [40]
EvalMultMutableInPlace() with a and b with BGV...
a * b: [5]
a * b: [5]

```

---

<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:** [November 15, 2024, 10:16pm UTC](https://openfhe.discourse.group/t/evalmultmutable-and-evalmultmutableinplace-produces-wrong-results-for-bfvrns/1724/2 "2024-11-15T22:16:49Z")

</div>

Thanks. It looks like the problem is on the C++ side. We rarely use these functions (externally). I’ve created an issue to look into this bug further: [Fix a bug with EvalMultMutable\* · Issue #906 · openfheorg/openfhe-development · GitHub](https://github.com/openfheorg/openfhe-development/issues/906)

---

<div class="post-metadata">

**Author:** ![dsuponitskiy-duality](https://avatars.discourse-cdn.com/v4/letter/d/c68b51/32.png) [@dsuponitskiy-duality](https://openfhe.discourse.group/u/dsuponitskiy-duality)\
**Post date:** [January 30, 2025, 8:28pm UTC](https://openfhe.discourse.group/t/evalmultmutable-and-evalmultmutableinplace-produces-wrong-results-for-bfvrns/1724/3 "2025-01-30T20:28:26Z")

</div>

@g_tasop Please set depth to 2 in order to call EvalMultMutable twice in your code. The rest will be fixed by code changes made for [the open issue.](https://github.com/openfheorg/openfhe-development/issues/906)
