4. Find all functions $f: \mathbf{R} \rightarrow \mathbf{R}$, such that for all real numbers $x, y$, the following holds:
$$
f(x+y)+f(x) f(y)=f(x y)+2 x y+1 .
$$
4. $f(x)=2 x-1, f(x)=-x-1, f(x)=x^{2}-1$.

In the original equation, let $y=1$, and let $a=1-f(1)$, then we have $f(x+1)=a f(x)+2 x+1$.
In the original equation, change $y$ to $y+1$, we get
$$
\begin{array}{l}
f(x+y+1)+f(x) f(y+1) \\
=f(x(y+1))+2 x(y+1)+1 .
\end{array}
$$

Substitute $f(x+y+1)=a f(x+y)+2(x+y)+1$,
$$
f(y+1)=a f(y)+2 y+1
$$

into the equation, we get
$$
\begin{array}{l}
a(f(x+y)+f(x) f(y))+(2 y+1)(1+f(x)) \\
=f(x(y+1))+2 x y+1 .
\end{array}
$$

Using the original equation again, we get
$$
\begin{array}{l}
a(f(x y)+2 x y+1)+(2 y+1)(1+f(x)) \\
=f(x(y+1))+2 x y+1 .
\end{array}
$$

Let $x=2 t, y=-\frac{1}{2}$, then we have
$$
a(f(-t)-2 t+1)=f(t)-2 t+1 .
$$

Replace $t$ with $-t$ in the above equation, we get
$$
a(f(t)+2 t+1)=f(-t)+2 t+1 \text {. }
$$

Eliminating $f(-t)$, we get
$$
\left(1-a^{2}\right) f(t)=2(1-a)^{2} t+a^{2}-1,
$$

where $a \neq-1$, otherwise for all $t$, we have $8 t=0$, which is a contradiction.
If $a \neq 1$, then $1-a^{2} \neq 0$. Therefore,
$$
f(t)=2\left(\frac{1-a}{1+a}\right) t-1 \text {. }
$$

Let $t=1$, we get $1-a=2\left(\frac{1-a}{1+a}\right)-1$.
Thus, $a=0$ or $a=3$.
When $a=0$, we get $f(t)=2 t-1$;
When $a=3$, we get $f(t)=-t-1$.
If $a=1$, from equation (1) we get $f(t)=f(-t)$.
In the original equation, set $y=x$ and $y=-x$ respectively, we get
$$
\begin{array}{l}
f(2 x)+f^{2}(x)=f\left(x^{2}\right)+2 x^{2}+1, \\
f(0)+f^{2}(x)=f\left(x^{2}\right)-2 x^{2}+1 .
\end{array}
$$

Subtracting the two equations, we get $f(2 x)=4 x^{2}+f(0)$.
In $f(x+1)=a f(x)+2 x+1$, let $x=0$, we get $f(1)=a f(0)+1$.
Since $a=1, f(1)=1-a=0$, so, $f(0)=-1$.
Thus, $f(2 x)=4 x^{2}-1$.
Therefore, for all $x \in \mathbf{R}$, we have $f(x)=x^{2}-1$.