2

With https://en.m.wikipedia.org/wiki/Residue_number_system I can convert numbers to another form in some basis of coprime numbers.

Taking the basis $5,7$ then the decompositions of the numbers $2$ and $6$ are

$$2 = (2 \mbox{ mod }5, 2 \mbox{ mod }7) = (2,2)$$ $$6 = (6 \mbox{ mod }5, 6 \mbox{ mod }7) = (1,6)$$

Then addition is done coefficient-wise: $2 + 6 = (2,2) + (1,6) = (3,1) = 8$, doing modulo each basis element and multiplication can be done coefficient-wise as well.

But what if I have a number in RNS form, like $(x_0,x_1,\cdots, x_{n-1})$ and I want the RNS form of each bit of its binary representation?

For example:

$6$ is $(2,2)$, also $6$ is in binary $110$

I want to turn $(2,2)$ directly into

representation_in_rns_of(1), representation_in_rns_of(1), representation_in_rns_of(0)

without ever converting $(2,2)$ to $6$ and then $6$ to 110 binary and then binary to a bunch of RNS values.

Is there a fast way to do it?

The residue number system loses the property of having order, so I don't know if it can be done.

Alex Ravsky
  • 106,166
Poperton
  • 6,594

1 Answers1

1

Let $k$ be any natural number, $[k]=\{1,2,\dots,k\}$, $(m_i)_{i\in [k]}$ be any indexed family of pairwise coprime moduli of a residue numeral system, $x$ be any integer, and $(x_i)_{i\in [k]}$ be the representation of $x$ in the residue numeral system based on $(m_i)_{i\in [k]}$. In order to recover $x$ uniquely from its representation we assume that $0\le x<M$, where $M=m_1m_2\cdots m_k$. The question is to recover the binary representation $x=\sum_{i=0}^n a_i2^i$ of $x$.

First we assume that all moduli $m_i$ are odd. We shall proceed similarly to construction of the usual binary representation. Namely, first we recognize is $x$ even or odd. The digit $a_0$ equals $0$ in the first case and $1$ in the second case. Next we replace $x$ by $\lfloor x/2\rfloor$. Then we recognize is new $x$ even or odd. The digit $a_1$ equals $0$ in the first case and $1$ in the second case, and so forth.

Now we provide the algorithms for our construction.

Parity recognition. We do the following preprocessing first. Let $i,j\in [k]$ be any different numbers. Since $m_i$ and $m_j$ are coprime, there exist integers $m^i_j$ and $m^j_i$ such that $m^i_j m_i+m^j_i m_j=1$ (you can find them using Euclidean algorithm, see the answers to this question for the details. We can replace $m^i_j$ (resp. $m^j_i$) by its residue modulo $m_j$ (resp. $m_i$).

Pick any $j\in [k]$ such that $x_j=\min\{x_i: i\in [k]\}$. To recognize the parity of $x$ it suffices to recognize the parity of $x'_j=x-x_j$. Since $m_j$ is odd, the parity of $x'_j$ is the same as the parity of $x'_j/m_j$. The latter is the unique number $x'$ in the range from $0$ to $M/m_j-1$ such that $m_jx'\equiv x_i\pmod{m_i}$ for each $i\in [k]\setminus \{j\}$. Therefore $x'$ has the representation $(x'_i)_{i\in [k]\setminus \{j\}}$ in the residue system based on the indexed family $(m_i)_{i\in [k]\setminus \{j\}}$, where for each $i\in [k]\setminus \{j\}$, $x'_i$ is the residue of the division of $x_i\cdot m^j_i$ by $m_i$. And so forth.

Division by $2$. Let $x$ be an even number and $(x_1,x_2,\dots,x_k)$ be the representation of $x$ in the residue numeral system based on $(m_i)_{i\in [k]}$. For each $i\in [k]$ let $m'_i=(m_i+1)/2$. It is easy to see that the number $x/2$ has the representation $(x'_i)_{i\in [k]}$ in this residue numeral system, where for each $i\in [k]$, $x'_i$ is the residue of the division of $x_i\cdot m'_i$ by $m_i$.

Suppose now that some $m_j$ is even, that is $m_j=m'_j m_0$, where $m'_j$ is odd and $m_0$ is a power of $2$, say $m_0=2^\ell$ for some natural $\ell$. Then we modify a bit the base of the residue numeral system to deal with. Namely, we add to it $m_0$ and replace $m_j$ by $m'_j$. Then in the representation of $x$ we add $x_0$ equal to the residue of the division of $x_j$ my $m_0$ and then replace $x_j$ by its residue of the division by $m'_j$. Then the lowest $\ell$ bits of the binary representation of $x$ are those of the binary representation of $x_0$.

To find the remaining bits we shall deal with the representation $(x'_i)_{i\in [k]}$ of $x'=(x-x_0)/2^\ell$ in the residue numeral system based on $(m_i)_{i\in [k]}$ (recall that we replaced $m_j$, so it is odd now) as above.

Now for each $i\in [k]$ find an integer $m^0_i$ such that $m^0_i m_0\equiv 1\pmod{m_i}$. This can be done similarly to the above or by $\ell$ consecutive divisions modulo $m$: that is, we construct a sequence $b_0,\dots,b_{\ell}$ of nonnegative integers such that $b_0=1$, and for each natural $t\le \ell$ we have $b_t=b_{t-1}/2$, if $b_{t-1}$ is even and $b_t=(b_{t-1}+m_i)/2$, if $b_{t-1}$ is odd. Then let $m^0_i=b_\ell$ and $x'_i$ be the residue of the division of $x_i\cdot m^0_i$ by $m_i$.

Alex Ravsky
  • 106,166