In this answer by fkraiem he proves a property that:
$a^{(p-1)/2} = 1$ if and only if $x$ is even
But this doesn't seem to work in my test with the secp256k1 Elliptic Curve. Here is my Python 2 code:
ECLib.py:
P = 2**256 - 2**32 - 2**9 - 2**8 - 2**7 - 2**6 - 2**4 - 1
N = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141
Gx = 55066263022277343669578718895168534326250603453777594175500187360389116729240
Gy = 32670510020758816978083085130507043184471273380659243275938904335757337482424
GPoint = (int(Gx),int(Gy))
Acurve = 0
def legendreSymbol(a,p=P): #Legendre Symbol Calculation
ls = pow(a, (p - 1) / 2, p)
if ls == p - 1: return -1
else: return ls
def modinv(a,n): # Extended Euclidean Algorithm
lm, hm = 1,0
low, high = a%n,n
while low > 1:
ratio = high/low
nm = hm - lm * ratio
new = high - low * ratio
hm = lm
high = low
lm = nm
low = new
return lm % n
def ECadd(xp,yp,xq=Gx,yq=Gy): # EC point addition
m = ((yq-yp) * modinv(xq-xp,P))%P
xr = (m*m-xp-xq)%P
yr = (m*(xp-xr)-yp)%P
return (xr,yr)
def ECdouble(xp,yp): # EC point doubling
LamNumer = 3*xp*xp+Acurve
LamDenom = 2*yp
Lam = (LamNumer * modinv(LamDenom,P)) % P
xr = (Lam*Lam-2*xp) % P
yr = (Lam*(xp-xr)-yp) % P
return (xr,yr)
Test.py:
import ECLib
Point = (ECLib.Gx, ECLib.Gy)
Deg = 1
print 'Degree of G \t\t\t Legendre Symbol'
for i in range(8):
Point = ECLib.ECdouble(Point[0], Point[1])
Deg = Deg * 2
print '\t' + str(Deg) + '\t\t\t\t' + str(ECLib.legendreSymbol(Point[0]))
Output:
Degree of G Legendre Symbol
2 1
4 -1
8 -1
16 1
32 -1
64 -1
128 1
256 1
As I am doubling, the power of G is always even, so Legendre Symbol should always be 1 but that is not the case. Why is that?