Last active
May 29, 2025 17:35
-
-
Save Hermann-SW/76e7cf8545c5e8b0332faeaad878e08f to your computer and use it in GitHub Desktop.
Kunerth's algorithm from 1878, for determining modular sqrt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
assert(b,s)=if(!(b), error(Str(s))); | |
m=eval(getenv("m")); | |
b=m.mod; | |
c=lift(m); | |
if(ispseudoprime(c),s=sqrt(Mod(-b,c)),issquare(Mod(b,-c),&s)); | |
V=r=lift(s);; | |
print("V=",r); | |
e=simplify(((c*z+r)^2+b)/c); | |
f=1; | |
w=polcoeff(e,0); | |
if(type(w)!="t_INT"||w<0,e=simplify(((c*z+r)^2-b)/c);f=-1;w=polcoeff(e,0)); | |
print("e=",e); | |
print("f=",f); | |
assert(issquare(w,&W)); | |
print("W=",W); | |
beta=-(V\W); | |
alpha=W*(V+W*beta); | |
print("alpha=",alpha); | |
print("beta=",beta); | |
xx=alpha^2*x^2+(2*alpha*beta-f*b)*x+(beta^2-c); | |
print("xx=",xx); | |
nfr=nfroots(,xx); | |
print("nfr=",nfr); | |
{ | |
foreach(nfr,X, | |
Y=Mod(alpha*X+beta,b); | |
if(lift(Y^2)==c, | |
print("X=",X); | |
print("Y=",Y); | |
print("Y^2=",Y^2))); | |
} | |
print("all asserts OK"); | |
write("/dev/stderr", "\nb=m.mod; c=lift(m); V=r=lift(\"sqrt\"(Mod(-b,c)));"); | |
write("/dev/stderr", "e=simplify(((c*z+r)^2±b)/c); f=±1; W=sqrt(polcoeff(e,0));"); | |
write("/dev/stderr", "beta=-(V\\W); alpha=W*(V+W*beta);"); | |
write("/dev/stderr", "xx=alpha^2*x^2+(2*alpha*beta-f*b)*x+(beta^2-c); nfr=nfroots(,xx);"); | |
write("/dev/stderr", "\n∀X∈nfr: Y=Mod(alpha*X+beta,b);"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for recovery this nice algorithm which is quite forgotten these days.
I've tried
and
123^2 = 861 (mod 1189)