Given a prime $P$, an integer $A$ $(0\leq A<P$), and a set of legal positions (encoded as a binary mask $\text{mask}$), is there an efficient algorithm to find a number $B$ that has the same modulus as $A$ and (when $B$ is represented in its binary form) has ones only in legal positions. In other words, $B$ satisfies the following two constraints:
- $A = B\mod P$
B & (~mask) == 0
A related problem that might be simpler is restricting the illegal positions (~mask) to be sparse. In this situation, a brute force search seems can finish in $O(2^k)$ steps where $k = \text{number of ones in (~mask)}$ by trying $A +P t$ for random $t$.
Some thoughts:
The problem seems can be reduced to solving a linear equation on a finite field with bound constraints if we consider the assignment of bits for $B$ in each continuous regions of ones in the $\text{mask}$ as a set of independent variables $v_i$ with bound constraints $0\leq v_i < 2^{s_i}$ where $s_i$ is the length of continuous ones.
For example, suppose the $\text{mask}$ is 1110011100111
; it is equivalently to solve the following linear equation $\mod P$:
$$A = v_1 + 2^5\times v_2 + 2^{10}\times v_3\mod P$$ with constraints: $$ 0\leq v_1, v_2, v_3 < 2^3$$
But I don't know if this problem can be solved efficiently.
(Edit: The original problem seems too hard without any restrictions. I am not sure if the following extra background makes this problem easier. We can assume that all legal positions are below $O(\log(P))$ instead of $O(P)$. For example, we may have $P\sim 2^{64}$, and the mask is about $128$ bits long.)