Lambda function in LUT

Hi,
I am trying to understand the code here,

We are calculating x^3%8. In the following snippet,

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

I do not understand why the else statement is need. I would explain what I understand by the code so that one can figure out where I am wrong.
In the main code (code where this snippet belongs), we have p1=8. For i\ge p1/2 we have (i-p1)^3\equiv p1. Hence I think the code could be,

if (m < p1/2)
            return (m * m * m) % p1;
        else
            return ((m-p1)^3) % p1;

I would really appreciate, if someone could correct me.

Thanks very much.

In fact, I have also checked that the else part is never executed in the original source code.

It appears to me that the else-statement’s logic is to handle negative values of m. But you are right, it is an unreachable code and you can ignore it.

1 Like

Thank you very much for the info @Caesar I want to really understand the code. I would be very glad if you (or anyone could help me understand) - how EvalFunc is implemented (openfhe-development/src/binfhe/lib/binfhecontext.cpp at 7b8346f4eac27121543e36c17237b919e03ec058 · openfheorg/openfhe-development · GitHub).

I also came accross the NativeInteger. It is defined, if I understand correctly, for easing the modular operations. I wanted to see the code.

Thanks very much.

EvalFunc is described here https://eprint.iacr.org/2021/1337.pdf in Section 4. It’s useful to read the previous sections as well, to understand how the floor evaluation and sign evaluations work.

2 Likes

I was taking it on faith :rofl: