Skip to content

Commit

Permalink
Trac #30530: some flake8 cleanup in elliptic curve
Browse files Browse the repository at this point in the history
mainly about E701, E702 : one command per line

URL: https://trac.sagemath.org/30530
Reported by: chapoton
Ticket author(s): Frédéric Chapoton
Reviewer(s): John Cremona
  • Loading branch information
Release Manager committed Nov 7, 2020
2 parents 6d28b20 + 98fc506 commit c71679a
Show file tree
Hide file tree
Showing 13 changed files with 200 additions and 120 deletions.
39 changes: 26 additions & 13 deletions src/sage/schemes/elliptic_curves/BSD.py
Expand Up @@ -219,7 +219,8 @@ def heegner_index_work(E):
except RuntimeError as err:
if err.args[0][-33:] == 'Generators not provably computed.':
dsl += 1
else: raise RuntimeError(err)
else:
raise RuntimeError(err)
J = I.is_int()
if J[0] and J[1] > 0:
I = J[1]
Expand Down Expand Up @@ -466,7 +467,8 @@ def prove_BSD(E, verbosity=0, two_desc='mwrank', proof=None, secs_hi=5,
raise NotImplementedError()
rank_lower_bd, rank_upper_bd, sha2_lower_bd, sha2_upper_bd, gens = M
assert sha2_lower_bd <= sha2_upper_bd
if gens is not None: gens = BSD.curve.saturation(gens)[0]
if gens is not None:
gens = BSD.curve.saturation(gens)[0]
if rank_lower_bd > rank_upper_bd:
raise RuntimeError("Apparent contradiction: %d <= rank <= %d." % (rank_lower_bd, rank_upper_bd))
BSD.two_selmer_rank = rank_upper_bd + sha2_lower_bd + BSD.two_tor_rk
Expand Down Expand Up @@ -520,7 +522,8 @@ def prove_BSD(E, verbosity=0, two_desc='mwrank', proof=None, secs_hi=5,
max_height = max(13,BSD.curve.quadratic_twist(D).CPS_height_bound())
heegner_primes = -1
while heegner_primes == -1:
if max_height > 21: break
if max_height > 21:
break
heegner_primes, _, exact = BSD.curve.heegner_index_bound(D, max_height=max_height)
max_height += 1
if isinstance(heegner_primes, list):
Expand Down Expand Up @@ -600,12 +603,14 @@ def prove_BSD(E, verbosity=0, two_desc='mwrank', proof=None, secs_hi=5,
if 2 not in BSD.primes:
if not s:
s = '2'
else: s = '2, '+s
else:
s = '2, ' + s
print('True for p not in {' + s + '} by Kolyvagin.')
BSD.proof['finite'] = copy(BSD.primes)
primes_to_remove = []
for p in BSD.primes:
if p == 2: continue
if p == 2:
continue
if galrep.is_surjective(p) and not BSD.curve.has_additive_reduction(p):
if BSD.curve.has_nonsplit_multiplicative_reduction(p):
if BSD.rank > 0:
Expand Down Expand Up @@ -637,7 +642,8 @@ def prove_BSD(E, verbosity=0, two_desc='mwrank', proof=None, secs_hi=5,
BSD.primes.remove(p)
kolyvagin_primes = []
for p in BSD.primes:
if p == 2: continue
if p == 2:
continue
if galrep.is_surjective(p):
kolyvagin_primes.append(p)
for p in kolyvagin_primes:
Expand All @@ -647,7 +653,8 @@ def prove_BSD(E, verbosity=0, two_desc='mwrank', proof=None, secs_hi=5,

# Cha's hypothesis
for p in BSD.primes:
if p == 2: continue
if p == 2:
continue
if D_K%p != 0 and BSD.N%(p**2) != 0 and galrep.is_irreducible(p):
if verbosity > 0:
print('Kolyvagin\'s bound for p = %d applies by Cha.' % p)
Expand Down Expand Up @@ -699,7 +706,8 @@ def prove_BSD(E, verbosity=0, two_desc='mwrank', proof=None, secs_hi=5,
# apply Kolyvagin's bound
primes_to_remove = []
for p in kolyvagin_primes:
if p == 2: continue
if p == 2:
continue
if p not in heegner_primes:
ord_p_bound = 0
elif heegner_index is not None: # p must divide heegner_index
Expand Down Expand Up @@ -749,7 +757,8 @@ def prove_BSD(E, verbosity=0, two_desc='mwrank', proof=None, secs_hi=5,
kato_primes = BSD.Sha.bound_kato()
primes_to_remove = []
for p in BSD.primes:
if p == 2: continue
if p == 2:
continue
if p not in kato_primes:
if verbosity > 0:
print('Kato further implies that #Sha[%d] is trivial.' % p)
Expand Down Expand Up @@ -779,7 +788,8 @@ def prove_BSD(E, verbosity=0, two_desc='mwrank', proof=None, secs_hi=5,
primes_to_remove = []
if BSD.N.is_prime():
for p in BSD.primes:
if p == 2: continue
if p == 2:
continue
if galrep.is_reducible(p):
primes_to_remove.append(p)
if verbosity > 0:
Expand All @@ -800,7 +810,8 @@ def prove_BSD(E, verbosity=0, two_desc='mwrank', proof=None, secs_hi=5,
for D in BSD.heegner_index_upper_bound:
M = BSD.heegner_index_upper_bound[D]
for p in kolyvagin_primes:
if p not in BSD.primes or p == 3: continue
if p not in BSD.primes or p == 3:
continue
if verbosity > 0:
print(' p = %d: Trying harder for Heegner index' % p)
obt = 0
Expand Down Expand Up @@ -846,9 +857,11 @@ def prove_BSD(E, verbosity=0, two_desc='mwrank', proof=None, secs_hi=5,
BSD.primes.remove(p)
break
for p in kolyvagin_primes:
if p not in BSD.primes or p == 3: continue
if p not in BSD.primes or p == 3:
continue
for D in BSD.curve.heegner_discriminants_list(4):
if D in BSD.heegner_index_upper_bound: continue
if D in BSD.heegner_index_upper_bound:
continue
print(' discriminant', D)
if verbosity > 0:
print('p = %d: Trying discriminant = %d for Heegner index' % (p, D))
Expand Down
83 changes: 55 additions & 28 deletions src/sage/schemes/elliptic_curves/cardinality.py
Expand Up @@ -138,9 +138,11 @@ def _cardinality_with_j_invariant_1728(self):
n = (d+1)//2
t = 2**n
n = n%4
if n==0 or n==1: t=-t
E=EllipticCurve(k,[0,0,1,1,1])
if self.is_isomorphic(E): t=-t
if n == 0 or n == 1:
t = -t
E = EllipticCurve(k, [0, 0, 1, 1, 1])
if self.is_isomorphic(E):
t = -t
else:
# The 7 classes are represented by E1=[0,0,1,0,0],
# E2=[0,0,1,0,b], E3=[0,0,1,b,0], E4=[0,0,a,0,0],
Expand All @@ -157,7 +159,8 @@ def _cardinality_with_j_invariant_1728(self):
if discube:
a = k.gen()
b = a
while b.trace()==0: b*=a
while b.trace() == 0:
b *= a
if self.is_isomorphic(EllipticCurve(k,[0,0,1,b,0])):
t = 0
else:
Expand All @@ -184,15 +187,18 @@ def _cardinality_with_j_invariant_1728(self):
t = 0
else:
u = delta.sqrt()
if not u.is_square(): u=-u
if not u.is_square():
u = -u
tr = ((self.a3()**2+self.a6())/u).trace()
if tr==0:
t = 0
else:
d2 = (d+1)//2
t = 3**d2
if d2%2==1: t = -t
if tr==-1: t = -t
if d2%2 == 1:
t = -t
if tr == -1:
t = -t
else:
# The 6 classes are represented by: [0,0,0,1,0],
# [0,0,0,1,i*a]; [0,0,0,g,0], [0,0,0,g^3,0];
Expand Down Expand Up @@ -227,9 +233,12 @@ def _cardinality_with_j_invariant_1728(self):
else:
t = (-p)**(d//2)
w = (self.c4()/k(48))**((q-1)//4)
if w==1: t = 2*t
elif w==-1: t = -2*t
else: t = 0
if w == 1:
t = 2*t
elif w == -1:
t = -2*t
else:
t = 0

# p=1 (mod 4). First find Frobenius pi=a+b*i for [0,0,0,-1,0] over GF(p):
# N(pi)=p and N(pi-1)=0 (mod 8).
Expand Down Expand Up @@ -313,9 +322,12 @@ def _cardinality_with_j_invariant_0(self):
else:
t = (-p)**(d//2)
w = (self.c6()/k(-864))**((q-1)//6)
if w==1: t = 2*t
elif w==-1: t = -2*t
elif w**3==1: t = -t
if w == 1:
t = 2*t
elif w == -1:
t = -2*t
elif w**3 == 1:
t = -t

# p=1 (mod 6). First find Frobenius pi=a+b*w for [0,0,0,0,1] over GF(p):
# N(pi)=p and N(pi-1)=0 (mod 12).
Expand All @@ -324,7 +336,8 @@ def _cardinality_with_j_invariant_0(self):
R = ZZ.extension(x**2-x+1,'zeta6')
zeta6 = R.gen(1)
pi = R.fraction_field().factor(p)[0][0].gens_reduced()[0]
while (pi-1).norm()%12 !=0: pi*=zeta6
while (pi - 1).norm() % 12:
pi *= zeta6
a,b = pi.list()
z = k(-b)/k(a) # a *specific* 6th root of unity in k

Expand All @@ -338,12 +351,18 @@ def _cardinality_with_j_invariant_0(self):
# Compute appropriate sextic twist:
w = (self.c6()/k(-864))**((q-1)//6)

if w==1: t = 2*a+b # = Trace(pi)
elif w==-1: t = -2*a-b # = Trace(-pi)
elif w==z: t = a-b # = Trace(pi*zeta6)
elif w==z**2: t = -a-2*b # = Trace(pi*zeta6**2)
elif w==z**4: t = b-a # = Trace(pi*zeta6**4)
elif w==z**5: t = a+2*b # = Trace(pi*zeta6**5)
if w == 1:
t = 2*a+b # = Trace(pi)
elif w == -1:
t = -2*a-b # = Trace(-pi)
elif w == z:
t = a-b # = Trace(pi*zeta6)
elif w == z**2:
t = -a-2*b # = Trace(pi*zeta6**2)
elif w == z**4:
t = b-a # = Trace(pi*zeta6**4)
elif w == z**5:
t = a+2*b # = Trace(pi*zeta6**5)

return Integer(q + 1 - t)

Expand Down Expand Up @@ -449,9 +468,11 @@ def cardinality_bsgs(self, verbose=False):
kmax = ((B-a)/M).floor()
if kmin==kmax:
self._order = q1-a-kmin*M
if verbose: print("no random points were needed")
if verbose:
print("no random points were needed")
return self._order
if verbose: print("(2,3,5)-torsion subgroup gives M=", M)
if verbose:
print("(2,3,5)-torsion subgroup gives M=", M)

# N1, N2 are divisors of the orders of E1, E2 separately,
# which are used to speed up the computation of the orders of
Expand All @@ -463,7 +484,8 @@ def cardinality_bsgs(self, verbose=False):
# Hasse bounds and the fact that we know that the group
# order is a multiple of N1:
n = order_from_bounds(E1.random_point(),bounds,N1,operation='+')
if verbose: print("New point on E has order ", n)
if verbose:
print("New point on E has order ", n)
# update N1 and M
N1 = N1.lcm(n)
g,u,v = M.xgcd(n) # g==u*M+v*n
Expand All @@ -472,19 +494,22 @@ def cardinality_bsgs(self, verbose=False):
a = (a*v*n+q1*u*M)//g
M *= (n//g) # = lcm(M,n)
a = a%M
if verbose: print("(a,M)=", (a, M))
if verbose:
print("(a,M)=", (a, M))
kmin = ((-B-a)/M).ceil()
kmax = ((B-a)/M).floor()
if kmin==kmax:
self._order = q1-a-kmin*M
return self._order
if verbose: print("number of possibilities is now ",kmax-kmin+1)
if verbose:
print("number of possibilities is now ",kmax-kmin+1)

# Get a random point on E2 and find its order, using the
# Hasse bounds and the fact that we know that the group
# order is a multiple of N2:
n = order_from_bounds(E2.random_point(),bounds,N2,operation='+')
if verbose: print("New point on E' has order ", n)
if verbose:
print("New point on E' has order ", n)
# update N2 and M
N2 = N2.lcm(n)
g,u,v = M.xgcd(n) # g==u*M+v*n
Expand All @@ -493,13 +518,15 @@ def cardinality_bsgs(self, verbose=False):
a = (a*v*n-q1*u*M)//g
M *= (n//g) # = lcm(M,n)
a = a%M
if verbose: print("(a,M)=", (a, M))
if verbose:
print("(a,M)=", (a, M))
kmin = ((-B-a)/M).ceil()
kmax = ((B-a)/M).floor()
if kmin==kmax:
self._order = q1-a-kmin*M
return self._order
if verbose: print("number of possibilities is now ",kmax-kmin+1)
if verbose:
print("number of possibilities is now ", kmax - kmin + 1)


def _cardinality_subfield(self, jpol):
Expand Down
7 changes: 5 additions & 2 deletions src/sage/schemes/elliptic_curves/cm.py
Expand Up @@ -495,11 +495,14 @@ def discriminants_with_bounded_class_number(hmax, B=None, proof=None):

# This lower bound gets used in an inner loop below.
from math import log

def lb(f):
"""Lower bound on euler_phi."""
# 1.79 > e^gamma = 1.7810724...
if f <= 1: return 0 # don't do log(log(1)) = log(0)
return f/(1.79*log(log(f)) + 3.0/log(log(f)))
if f <= 1:
return 0 # don't do log(log(1)) = log(0)
llf = log(log(f))
return f/(1.79*llf + 3.0/llf)

for D in range(-B, -2):
D = Integer(D)
Expand Down

0 comments on commit c71679a

Please sign in to comment.