TITLE: Conjectured primality test
QUESTION [31 upvotes]: Can you provide a proof or a counterexample for the following claim :

Let $n$ be a natural number , $n>2$ and $n \neq 9$ . Then $n$ is prime if and only if
$\displaystyle\sum_{k=1}^{n-1}\left(2^k-1\right)^{n-1} \equiv n \pmod{2^n-1}$

You can run this test here .
GUI application that implements this algorithm can be found here .
Android app that implements this algorithm can be found here .
I was searching for a counterexample using the following two PARI/GP programs :
CE1(n1,n2)=
{
forcomposite(n=n1,n2,
if((Mod(sum(k=1,n-1,(2^k-1)^(n-1)),2^n-1)==n),print("n="n))) 
}


CE2(n1,n2)=
{
forprime(n=n1,n2,
if(!(Mod(sum(k=1,n-1,(2^k-1)^(n-1)),2^n-1)==n),print("n="n))) 
}

Remark
More generally we can formulate the following criterion :

Let $b$ and $n$ be a natural numbers , $b\geq 2$ , $n>2$ and $n \neq 9$ . Then $n$ is prime if and only if
$\displaystyle\sum_{k=1}^{n-1}\left(b^k-1\right)^{n-1} \equiv n \pmod{\frac{b^n-1}{b-1}}$

EDIT
Improved PARI/GP programs for counterexample search :
CE1(b,n1,n2)=
{
forcomposite(n=n1,n2,
s=sum(k=1,n-1,lift(Mod(b^k-1,(b^n-1)/(b-1))^(n-1)));
if((Mod(s,(b^n-1)/(b-1))==n),print("n="n)))
}


CE2(b,n1,n2)=
{
forprime(n=n1,n2,
s=sum(k=1,n-1,lift(Mod(b^k-1,(b^n-1)/(b-1))^(n-1)));
if(!(Mod(s,(b^n-1)/(b-1))==n),print("n="n)))
}

REPLY [5 votes]: Just a remark which is a little long for a comment:
If we forget about the two and also about the generalization to other numbers for a second and look at polynomials instead, i.e. consider the polynomial
$$p_n(x) := \sum_{k=1}^{n-1} (x^k-1)^{n-1}$$
and then reduce this modulo $x^n-1$, the reduced polynomials for $p_n$ when $n$ is prime have a very nice form, namely:
$$p_n(x) \equiv n - \sum_{i=0}^{n-1} x^i \mod{x^n-1}.$$
Now it is clear that if we input $2$ (or $b$), the sum will exactly be $2^n-1$ resp $\frac{b^n-1}{b-1}$, so we get $n$ as claimed.
This is, unfortunately, not yet a proof for your claim, but it suggests that there is an even stronger structure behind it, that goes beyond natural numbers. Note that the polynomial $p_9$ does not have this nice structure, meaning that this strange case could be excluded when viewing it in the polynomial world.