(BMO 2009)

Find all functions $f: \mathbb{N}^{*} \rightarrow \mathbb{N}^{*}$ such that

$$
f\left(f^{2}(m)+2 f^{2}(n)\right)=m^{2}+2 n^{2}
$$

for all positive integers $m, n$.
It is clear that $f$ is injective. The identity $(x+3)^{2}+2 x^{2}=(x-1)^{2}+2(x+2)^{2}$ allows us to show that for any integer $n \geqslant 2$,

$$
f(n+3)^{2}+2 f(n)^{2}=f(n-1)^{2}+2 f(n+2)^{2}
$$

Let $u_{n}=f(n)^{2}$. We solve the recurrence relation $u_{n+3}-2 u_{n+2}+2 u_{n}-u_{n-1}=0$: the characteristic polynomial is $X^{3}-2 X^{2}+2 X-1=(X-1)^{3}(X+1)$. We obtain $u_{n}=a n^{2}+b n+c+d(-1)^{n}$. Since, moreover, $2 u_{1}+u_{5}=3 u_{3}\left(\text{because } 2 \times 1^{2}+5^{2}=3^{2}+2 \times 3^{2}\right)$, we get $b=0$.

Let's prove that $c=d=0$: For any even $n$, $u_{n}=a n^{2}+c+d$. Since $u_{n}=f(n)^{2}$, $a n^{2}+c+d$ is a perfect square, and so is $4\left(a n^{2}+c+d\right)$. Since $2 n$ is even, $u_{2 n}=4 a n^{2}+c+d$ and it is also a perfect square. Therefore, for all $n \geqslant 1, 3(c+d)$ is the difference of two perfect squares of integers $\geqslant n$. If they are distinct, their difference is at least $(n+1)^{2}-n^{2}=2 n+1$ (take the two smallest consecutive squares), which exceeds $3(c+d)$ for large enough $n$, a contradiction. Therefore, $u_{2 n}=4 u_{n}$ and $c+d=0$. Thus, for any even $n$, $u_{n}=a n^{2}$ is a perfect square, so $a$ itself is a perfect square. For odd $m$, $u_{m}=a m^{2}+c-d$. Similarly, since $a m^{2}$ is a perfect square, $c-d$ is the difference of two squares of integers $\geqslant m$, and this for all $m$, which implies $c-d=0$ as before. Since $c+d=0$, we get $c=d=0$.

Thus, we have $f(n)=\sqrt{a} n$ for all integers $n$, with $a$ being a perfect square, in other words, $f$ is linear. By substituting this into the initial equation, we find that the slope can only be 1 (recall that $f$ is positive, so this coefficient is itself positive). Therefore, $f$ is the identity. Conversely, the identity is a solution to the problem.