Score:1

FHE modular reduction in specific range

ws flag

I'm trying to implement a naive version of CKKS in Python. It was great until I start implementing the modulus.

For this kind of schemes, the modulus $q$ is in the range $(-q/2,q/2]$. How does this work?

In CKKS paper (I think BGV and others do the same) use something like this (a toy example): $c = m (mod$ $q)$. Where $c$ and $m$ are polynomials, so the coefficients of m are mod q. So c and m are congruent mod q.

So for each coefficient $n$ (if the range of mod is the typical $[0,1,...q-1]$), a way is to solve:

$n = m*q + R$

Where $m$ is the greatest integer that makes $n<m*q$ and $R$ is the remainder that solves the equation being this the same reminder that for c, so n is the result for the coefficient.

But if the modulus range is other like I say, how is this done? The intuition says me that is like wrapping the number in a ''clock'' that starts in -q/2+1 and goes up to q/2 before starting again. But I don't know how to apply this intuition for negative numbers that are smaller than -q/2.

For example which is the answer of -771 (mod 1280)

Score:-1
ng flag

If you first have some modulus function $x\bmod q$ that always returns a value in $[0, q)$, it is straightforward to switch this to your desired modulus function, for example one can define

$$x\bmod^{\pm} q := (x\bmod q) - \lfloor q/2\rfloor.$$

This is to say that to get a modulus function that returns a value in $[c, c+q)$ rather than $[0,q)$, it suffices to (manually) shift the result of calling the standard modulus function.

mmazz avatar
ws flag
This was my first attempt and didn't work.... So I have a bug in other please! At least I know that the mod was not the problem.
Mark avatar
ng flag
For some programming languages there is a difference between "modulus" and "remainder", usually with how they treat negative inputs. I would try to look into the documentation of your programming language for this, as it is likely the issue.
mmazz avatar
ws flag
Thats true. I think that Python %, implementes modulus, so thats its correct. Tell me if what I think its correct. The idea of this congruence in the new interval its that $c\equiv x\pmod q$, its to find witch $c \in (-q/2, q/2]$ has the same modulus that $m \bmod q$? For what I understand $q/2\bmod q=q/2$, but if at that I subtract $q/2$ will give me a wrong answer (for what I understand). What I'm missing?
Mark avatar
ng flag
If your issue is one of the rounding boundary, i.e. $(-q/2, q/2]$ vs $[-q/2, q/2)$, you could simply add/subtract 1 from the result as necessary.
poncho avatar
my flag
@mmazz: one way to implement $c \in (-q/2, q/2]$ (given a % operation that gives a result in $[0, q)$ is $c = ((x + (q/2-1)) \% q) - (q/2-1)$
fgrieu avatar
ng flag
@poncho: Your formula is off-by-one for odd integer $q>0$ in most languages where `q/2` in an integer. E.g. in C, for `int q=5, x=3, c=((x+(q/2-1))%q)-(q/2-1);` we get `c==3`. [Try it Online!](https://tio.run/##S9ZNT07@/185My85pzQlVcGmuCQlM18vw44rM69EITcxM0@jLD8zRVOhmosTJFJoa6qjUGFrrKOQbKuhUaGtUahvpGuoqalaqKkLZVtzcRYUAdWmaSippijpJIMEilJLSovyFAysuWr//wcA). Consider making an answer, because **the [current answer](https://crypto.stackexchange.com/revisions/102859/1) is wrong.**
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.