# If you XOR a random input with a biased input, the output is random - why?

Ive seen in an answer to this question here :

https://stackoverflow.com/questions/5889238/why-is-xor-the-default-way-to-combine-hashes/5889288#5889288

that If you XOR a random input with a biased input, the output is random .

What is the reason for that ? Is there a formal proof ?

thx

• It is not possible to give a formal proof of an informal statement. Commented Jun 5, 2018 at 12:02

Let $X, Y \in \{0,1\}$ be random variables, with $\Pr[X = 0] = \Pr[X = 1] = 1/2$ and any distribution on $Y$ as long as $Y$ is independent of $X$ so that $$\Pr[Y = y \mathrel| X = x] = \Pr[Y = y]$$ for any fixed bits $x, y \in \{0,1\}$. What is the distribution on $X \oplus Y$? Let $b \in \{0,1\}$ be a fixed bit. Then

\begin{align*} \Pr[X \oplus Y = b] &= \Pr[X \oplus Y = b \mathrel| X = 0]\cdot\Pr[X = 0] \\ &\quad\quad + \Pr[X \oplus Y = b \mathrel| X = 1]\cdot\Pr[X = 1] \\ &= \Pr[0 \oplus Y = b \mathrel| X = 0]\cdot(1/2) \\ &\quad\quad + \Pr[1 \oplus Y = b \mathrel| X = 1]\cdot(1/2) \\ &= \Pr[Y = b \mathrel| X = 0]\cdot(1/2) \\ &\quad\quad + \Pr[Y = b \oplus 1 \mathrel| X = 1]\cdot(1/2) \\ &= \Pr[Y = b]\cdot(1/2) + \Pr[Y = b \oplus 1]\cdot(1/2) \\ &= (\Pr[Y = b] + \Pr[Y = b \oplus 1])\cdot(1/2) \\ &= 1/2, \end{align*}

the last equality arising because $b$ and $b \oplus 1$ are the only two possible values that $Y$ can take on, so $\Pr[Y = b] + \Pr[Y = b \oplus 1] = 1$.

Another way to see this (less formally, maybe helps the intuition): take a biased bit $m$, a random bit $p$, and produce $e=m\oplus p$.

$p$ is equiprobably 1 or 0; both happen with 50% probability. $e$ has thus 50% probability of being the inverse of $m$, 50% probability of being equal to $m$. So your guess of $m$, based on $e$, has 50% probability of being correct.

For a fixed biased input of $b$-bit, consider the function from random input to result of the XOR. That's a bijection from the set of $b$-bit strings to that same set. If follows that if random input is uniformly random on that set, then the output also is.

Therefore, if the biased input is constant or otherwise independent of random input assumed uniformly random, then the output also is uniformly random.

For a more formal proof, see Squeamish Ossifrage's answer.

• This of course implies that the biased input is independent of the random input, since if it is then you can assume the biased input to be fixed relative to the random distribution wlog. Nice. Commented Jun 5, 2018 at 7:05
{"found_math": true, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 1, "mathjax_display_tex": 1, "mathjax_asciimath": 0, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0, "url": "https://crypto.stackexchange.com/questions/59793/if-you-xor-a-random-input-with-a-biased-input-the-output-is-random-why/59803#59803", "fetch_time": 1718902147000000000, "content_mime_type": "text/html", "warc_filename": "crawl-data/CC-MAIN-2024-26/segments/1718198861957.99/warc/CC-MAIN-20240620141245-20240620171245-00817.warc.gz", "warc_record_offset": 165624605, "warc_record_length": 41116, "token_count": 842, "char_count": 2526, "score": 4.15625, "int_score": 4, "crawl": "CC-MAIN-2024-26", "snapshot_type": "latest", "language": "en", "language_score": 0.8242254257202148, "file_path": "/sailhome/yjruan/project/latent-lm/data/processed/finemath-4plus/finemath-4plus/train-00008-of-00064.parquet"}