For Diffie-Hellman, why is a g value of p−1 not a suitable choice? I am currently learning about the Diffie-Hellman key exchange. I understand that for a $g$ of $1$, the resulting key would always end up as $1$ which would obviously not be secure.

I read that the $p\! -\! 1$ value for $g$ is not secure either, but there is no explanation as to why - which is my question. I guess it has to do with $g$ being a divisor of $p\! -\! 1$ but I am not sure.
If $g=p-1$ then $g\equiv -1\pmod p$ that is $g$ is effectively the same as $-1$ which has the obvious drawback that $g^x$ can only ever be $+1$ or $p-1$ which is easily brute-forcable.



Here's a short proof sketch of the above exponentiation statement:

\begin{align}
 (p-1)^2\bmod p  &amp;= (p^2-2p+1)\bmod p \\
  &amp;=(p^2\bmod p)-(2p\bmod p)+(1\bmod p)\\ 
  &amp;= 0-0+1\\
\end{align}

So when you have $(p-1)^x\bmod p$ this is the same as $(p-1)^{2y}\cdot(p-1)^{z}\bmod p$ for $x=2y+z$ and $z\in \{0,1\}$ which is $((p-1)^2)^y\cdot (p-1)^{z}\bmod p=1^y\cdot (p-1)^{z}$ which is either $p-1$ or $1$.