Last active
November 26, 2023 11: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 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);"); |
Bill Allombert made me aware that I can use nfroots(,P) to find the rational roots of P.
That simplified my code to determine the roots "by hand" significantly.
Diff for revisions 5 and 6 shows the changes, "nfroots()" made "factor()" call for xx unnecessary, and simplified code with foreach().
New variable nfr containing results of nfroots() call is printed as well:
$ m="Mod(60,85)" gp -q < Kunerth.gp
V=25
e=60*z^2 + 50*z + 9
f=-1
W=3
alpha=3
beta=-8
xx=9*x^2 + 37*x + 4
nfr=[-4, -1/9]~
X=-4
Y=Mod(65, 85)
Y^2=Mod(60, 85)
X=-1/9
Y=Mod(20, 85)
Y^2=Mod(60, 85)
all asserts OK
b=m.mod; c=lift(m); V=r=lift("sqrt"(Mod(-b,c)));
e=simplify(((c*z+r)^2±b)/c); f=±1; W=sqrt(polcoeff(e,0));
beta=-(V\W); alpha=W*(V+W*beta);
xx=alpha^2*x^2+(2*alpha*beta-f*b)*x+(beta^2-c); nfr=nfroots(,xx);
∀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
Here is a new example calculation.
f=-1 is forced for getting a t_INT constant term of e, by subtracting b in simplify() instead of adding.
Modified f value influences computation of xx:
Constant term of e was square 9, 5*17-60=25 was square as well.
Here is another square difference resulting in square constant of e and results: