# How can we determine if two LWE ciphertexts are equal?

**URL:** <https://openfhe.discourse.group/t/how-can-we-determine-if-two-lwe-ciphertexts-are-equal/1291>\
**Category:** Library Questions\
**Created:** [May 23, 2024, 9:39am UTC](https://openfhe.discourse.group/t/how-can-we-determine-if-two-lwe-ciphertexts-are-equal/1291 "2024-05-23T09:39:53Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![capibala](https://avatars.discourse-cdn.com/v4/letter/c/b5ac83/32.png) [@capibala](https://openfhe.discourse.group/u/capibala)\
**Post date:** [May 23, 2024, 9:39am UTC](https://openfhe.discourse.group/t/how-can-we-determine-if-two-lwe-ciphertexts-are-equal/1291/1 "2024-05-23T09:39:53Z")

</div>

There are ways of determining greater than or less in the example but how do we determine equality when we introduce random error

---

<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:** [May 23, 2024, 6:29pm UTC](https://openfhe.discourse.group/t/how-can-we-determine-if-two-lwe-ciphertexts-are-equal/1291/2 "2024-05-23T18:29:54Z")

</div>

If you write a circuit/look-up table only using LWE ciphertexts in an exact scheme such as FHEW, you can check equality. If you want to achieve this through an intermediate conversion through an approximate scheme such as CKKS, then you can only achieve equality up to some precision, because by definition CKKS has approximation errors. In the latter case, you could compute |x - y| \<= eps instead of x==y.

---

<div class="post-metadata">

**Author:** ![capibala](https://avatars.discourse-cdn.com/v4/letter/c/b5ac83/32.png) [@capibala](https://openfhe.discourse.group/u/capibala)\
**Post date:** [May 24, 2024, 6:28am UTC](https://openfhe.discourse.group/t/how-can-we-determine-if-two-lwe-ciphertexts-are-equal/1291/4 "2024-05-24T06:28:31Z")

</div>

If I use TFHE, can I calculate the difference between two values and then use that to create a LUT?  
Because the function parameter type is NativeInteger, I’m not sure if can determine it as 0.  
m = x-y;  
auto fp = (NativeInteger m, NativeInteger p1) → NativeInteger {  
if (m == 0)  
return 1;  
else  
return 0;  
};

```
// Generate LUT from function f(x)
auto lut = cc.GenerateLUTviaFunction(fp, p);

```

---

<div class="post-metadata">

**Author:** ![capibala](https://avatars.discourse-cdn.com/v4/letter/c/b5ac83/32.png) [@capibala](https://openfhe.discourse.group/u/capibala)\
**Post date:** [May 24, 2024, 6:30am UTC](https://openfhe.discourse.group/t/how-can-we-determine-if-two-lwe-ciphertexts-are-equal/1291/5 "2024-05-24T06:30:19Z")

</div>

The problem is that it doesn’t work when the difference is very large.

```auto
int p = cc.GetMaxPlaintextSpace().ConvertToInt(); // Obtain the maximum plaintext space

    // Initialize Function f(x) = x^3 % p
    auto fp = [](NativeInteger m, NativeInteger p1) -> NativeInteger {
        if (m == 0)
            return 1;
        else
            return 0;
    };

    // Generate LUT from function f(x)
    auto lut = cc.GenerateLUTviaFunction(fp, p);

    auto ct1 = cc.Encrypt(sk, 1024, FRESH, p);
    auto ct2 = cc.Encrypt(sk, 0, FRESH, p);

    cc.GetLWEScheme()->EvalSubEq(ct1, ct2);

    auto ct_cube = cc.EvalFunc(ct1, lut);

    LWEPlaintext result;

    cc.Decrypt(sk, ct_cube, &result, 2);

    std::cout << "Evaluated = " << result << std::endl;

```

---

<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:** [May 28, 2024, 2:48pm UTC](https://openfhe.discourse.group/t/how-can-we-determine-if-two-lwe-ciphertexts-are-equal/1291/6 "2024-05-28T14:48:40Z")

</div>

It works correctly with cc.Decrypt(sk, ct\_cube, &result, p). Also recall that you are computing x - y % p, so if for instance, p = 8, then the expected result is 1 for x = 1024 and y = 0.

---

<div class="post-metadata">

**Author:** ![capibala](https://avatars.discourse-cdn.com/v4/letter/c/b5ac83/32.png) [@capibala](https://openfhe.discourse.group/u/capibala)\
**Post date:** [June 4, 2024, 2:21am UTC](https://openfhe.discourse.group/t/how-can-we-determine-if-two-lwe-ciphertexts-are-equal/1291/7 "2024-06-04T02:21:05Z")

</div>

Is the value of p modifiable? The documentation on GitHub says that it can be supported up to 8 using the LUT method.

---

<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 4, 2024, 7:56pm UTC](https://openfhe.discourse.group/t/how-can-we-determine-if-two-lwe-ciphertexts-are-equal/1291/8 "2024-06-04T19:56:42Z")

</div>

It is the same answer as in your previous question [Is there a way to make the plaintext modulus greater than 8 in GenerateLUTviaFunction?](https://openfhe.discourse.group/t/is-there-a-way-to-make-the-plaintext-modulus-greater-than-8-in-generatelutviafunction/1253).

---

<div class="post-metadata">

**Author:** ![capibala](https://avatars.discourse-cdn.com/v4/letter/c/b5ac83/32.png) [@capibala](https://openfhe.discourse.group/u/capibala)\
**Post date:** [July 10, 2024, 3:15am UTC](https://openfhe.discourse.group/t/how-can-we-determine-if-two-lwe-ciphertexts-are-equal/1291/9 "2024-07-10T03:15:48Z")

</div>

I apologize for taking so long, but I still have a question.

If judged by the form of the circuit, it is required that each bit of the plaintext be the same, is the same operation required for the ciphertext as well? How can the XNOR operation be directly performed on two LWECipertexts, or is some function required to extract the bits one by one?

---

<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:** [July 11, 2024, 9:03am UTC](https://openfhe.discourse.group/t/how-can-we-determine-if-two-lwe-ciphertexts-are-equal/1291/10 "2024-07-11T09:03:03Z")

</div>

If you have several ciphertexts encoding the bits of the input, then you should apply the XNOR (or whatever binary gate you have in your circuit) on each ciphertext. If your ciphertext encodes a the whole message, then you only apply the LUT on that ciphertext. Finally, if your message is broken up in pieces up to p, then you write your circuit in terms of LUTs accordingly, and apply them on the respective ciphertexts.
