This theorem is
The number $N=2^n\cdot k+1$ with $k<2^n$ is prime if and only if there exists $a$ with $a^{(N-1)/2}\equiv -1\mod N$
This proof is
$\Longrightarrow$ : If $N$ is prime, let $a$ be a primitive root of $N$.
Fermat's little theorem gives $a^{N-1}\equiv 1\mod N$ , hence $a^{(N-1)/2}\equiv \pm1\mod N$.
But since $a$ is a primitive root , we cannot have $a^{(N-1)/2}\equiv 1\mod N$
$\Longleftarrow$ : Now suppose, there exists $a$ with $a^{(N-1)/2}\equiv -1\mod N$.
Let $s$ be the order of $a$ modulo $N$, in other words the smallest positive integer with $a^s\equiv 1\mod N$.
Since $a^{N-1}\equiv 1\mod N$ , we have $s\mid N-1$
If we write $s=2^m\cdot p$ with odd $p$ , we cannot have $m<n$ because then we would have $a^{(N-1)/2}\equiv 1\mod N$ because $s$ would divide $(N-1)/2$.
Hence $s$ is at least $2^n$.
If $q$ is a prime factor of $N$, we have $a^{q-1}\equiv 1\mod q$ because $a$ and $q$ must be coprime, and $s$ must be the order of $a$ modulo $q$, otherwise $s$ would not be the order of $a$ modulo $N$.
Hence, we have $s|q-1$. So, $q>s$.
Since $s$ is at least $2^n$ and $k<2^n$, we can conclude that every prime factor $q$ of $N$ must exceed $\sqrt{N}$ and this implies that $N$ must be prime.
I'm trying to understand this step:
If $q$ is a prime factor of $N$, we have $a^{q-1}\equiv 1\mod q$ because $a$ and $q$ must be coprime, and $s$ must be the order of $a$ modulo $q$, otherwise $s$ would not be the order of $a$ modulo $N$.
I know that $a^{q-1}\equiv 1\pmod q$ is because "$a$ and $q$ are coprime" (since $a$ and $N$ are coprime).
But I don't know how "$s$ must be the order of $a$ modulo $q$" is deduced from "otherwise $s$ would not be the order of $a$ modulo $N$".
I think
$a^s\equiv1\pmod N⇒a^s\equiv1\pmod q⇒s$ is a multiple of the order of $a$ modulo $q$
which doesn't imply that $s$ is equal to the order of $a$ modulo $q$.
E.g. $5$ is a prime factor of $35$.
The order of $2$ modulo $35$ is $12$,
the order of $2$ modulo $5$ is $4$, and $12\ne4$.