certified_log.py

back to table · edit · history · where entries came from · files · download

3345 bytes, as of the version from 2026-11-30 16:30 (current). Recorded here, not run.

"""Exact modular square roots and bounded Iwasawa logarithms (Python 3).

No Sage/PARI logarithm or Gamma is used. All returns are residues modulo p**N.
For p>=5, k-v_p(k) >= k-floor(log_p(k)). For k>=2N the latter is
>=N, and it stays >=N: k-floor(log_p(k)) is nondecreasing. The series
therefore stops before 2N. Denominators are cancelled before reduction;
input modulus N+floor(log_p(2N))+2 covers every denominator loss.
"""
from math import isqrt,gcd

def vp(n,p):
    if n==0: return 10**9
    v=0
    while n%p==0: n//=p;v+=1
    return v

def floorlog(n,p):
    e=0
    while n>=p:n//=p;e+=1
    return e

def relative_digits(p,digits):
    n=0;mod=1
    while mod<10**digits:n+=1;mod*=p
    return n

def hensel(D,p,N,r0):
    assert 0<r0<p and (r0*r0-D)%p==0 and gcd(2*r0,p)==1
    r=r0;m=p
    for j in range(1,N):
        t=((D-r*r)//m*pow(2*r,-1,p))%p
        r+=m*t;m*=p
        assert (r*r-D)%m==0
    return r

def log_unit(u,p,N):
    """u must be given modulo p**(N+floorlog(2N,p)+2)."""
    M=N+floorlog(2*N,p)+2;mod=p**M;target=p**N
    assert u%p
    z=(pow(u,p-1,mod)-1)%mod
    assert z%p==0
    t=vp(z,p);acc=0;power=1
    for k in range(1,2*N):
        power=power*z%mod;e=vp(k,p);den=k//p**e
        assert power%p**e==0
        term=(power//p**e)*pow(den,-1,target)%target
        acc=(acc+(term if k%2 else -term))%target
    assert 2*N-floorlog(2*N,p)>=N
    return acc*pow(p-1,-1,target)%target

def unit_derivative(D,p,x,y,w,N,r0=None):
    r0=min(r for r in range(1,p) if (r*r-D)%p==0) if r0 is None else r0
    M=N+floorlog(2*N,p)+2;mod=p**M;r=hensel(D,p,M,r0)
    u=(x+y*r)*pow(2,-1,mod)%mod
    assert u%p
    v=log_unit(u,p,N)
    return 4*v*pow(w,-1,p**N)%p**N,{'root':str(r),'root_precision':M,'unit':str(u),'unit_modulus_exponent':M,'series_terms':2*N-1,'tail_lower_bound':2*N-floorlog(2*N,p)}

def jacobi(a,n):
    assert n>0 and n%2
    a%=n;s=1
    while a:
        while a%2==0:
            a//=2
            if n%8 in (3,5):s=-s
        a,n=n,a
        if a%4==3 and n%4==3:s=-s
        a%=n
    return s if n==1 else 0

def character(D,a):
    assert a>0
    e=vp(a,2);odd=a//2**e
    if e and D%2==0:return 0
    s=(-1 if D%8 in (3,5) else 1)**e
    return s*jacobi(D,odd)

def squarefree(n):
    return all(n%(k*k) for k in range(2,isqrt(n)+1))

def fundamental(D):
    return (D%4==1 and squarefree(-D)) or (D%4==0 and (D//4)%4 in (2,3) and squarefree(-D//4))

def pairs():
    primes=[p for p in range(5,44) if all(p%d for d in range(2,isqrt(p)+1))]
    return [(D,p) for D in range(-3,-44,-1) if fundamental(D) for p in primes if character(D,p)==1]

def reduced_forms(D):
    forms=[]
    for a in range(1,isqrt((-D)//3)+1):
        for b in range(-a,a+1):
            if (b*b-D)%(4*a):continue
            c=(b*b-D)//(4*a)
            if a>c or gcd(gcd(a,b),c)>1:continue
            if (abs(b)==a or a==c) and b<0:continue
            forms.append((a,b,c))
    return forms

def coefficients(D,p,h,r0):
    norm=p**h
    for ay in range(isqrt(4*norm//(-D))+1):
        for y in ([0] if ay==0 else [-ay,ay]):
            xx=4*norm+D*y*y
            x=isqrt(xx)
            if x*x!=xx:continue
            if D%4==1 and (x-y)%2:continue
            if D%4==0 and x%2:continue
            if (x+y*r0)%p==0:continue
            return x,y
    raise ValueError('No unit-oriented generator found')