TITLE: Determine $2^{\frac{p-1}{4}}\equiv 1\pmod p$ or $2^{\frac{p-1}{4}}\equiv -1\pmod p$ when $p\equiv 1 \pmod 8$
QUESTION [13 upvotes]: Let $p=8k+1\equiv 1\pmod 8$ be a prime, thus $2$ is a quadratic residue module $p$. Euler's criterion show that $$2^{\frac{p-1}{2}}\equiv 1 \pmod p.$$
So we must have 
$$2^{\frac{p-1}{4}}\equiv \delta(p) \pmod p$$
where $\delta(p)=\pm1$.
Now my question is how to determine $\delta(p)$. 
I calculated many examples. Statistics show that the value of $\delta(p)$ should be related with the parity of $k$ and $h(-p)$, the latter being the class number of $\mathbb{Q}(\sqrt{-p})$. I know little algebraic number theory and hope some experts could help me solve this problem. I am waiting for help, thank you very much!
EDIT
Now I have formulated my conjecture about $\delta(p)$, which states as follows.
Conjecture:
$$\delta (p)=(-1)^{\frac{h(-p)}{4}+k}$$
Any ideas to prove or counterexample to disprove this?

REPLY [9 votes]: Barrucand and Cohn (MR0249396, Note on primes of type $x^2+32y^2$, class number, and residuacity. J. Reine Angew. Math. 238, 1969, 67--70) have proved that for primes $p \equiv 1 \textrm{ mod } 8$, the condition $h(−4p) \equiv 0 \textrm{ mod } 8$ is equivalent to $−4$ being a $8$-th power mod $p$. This implies your observation because $(-4)^{\frac{p-1}{8}} = (-1)^k 2^{\frac{p-1}{4}}$. I learned this in Merel's article L'accouplement de Weil entre le sous-groupe cuspidal et le sous-groupe de Shimura de $J_0(p)$, J. Reine Angew. Math. 477 (1996), 71--115, where you can find other characterizations e.g. $p$ is of the form $x^2+32y^2$, cf. Théorème 3.
In the case $p \equiv 5 \textrm{ mod } 8$, the formula 1.5 in Williams and Currie's article mentioned by Henri Cohen, together with $(\frac{p-1}{2})!^2 \equiv -1 \textrm{ mod } p$ for $p \equiv 1 \textrm{ mod } 4$ (Wilson's theorem) implies your second observation.