default(parisize,"44M"); \\ overflows 42M
count = 0;

nfisprobisom(P,Q, n = 50)=
{
  forprime (p = 2,oo,
    my(FP = factormod(P,p,1)); if (vecmax(FP[,2]) > 1, next());
    my(FQ = factormod(Q,p,1)); if (vecmax(FQ[,2]) > 1, next());
    if(FP[,1] != FQ[,1], return(0));
    n--; if(!n,return(1)));
}
check(K,f,sol,flag, expo = 32)=
{
  if (!sol || poldegree(f) != poldegree(sol), return(0));
  if (exponent(f) > exponent(sol) + expo,
      error(exponent(f)," > ",exponent(sol)," + ",expo));
  if (flag != 2, sol = rnfequation(K,sol); f = rnfequation(K,f));
  nfisprobisom(sol,f);
}
checkvec(K,f,~sol,flag, expo = 32)=
{
  for (j=1,#sol, if (check(K,f,sol[j],flag,expo), sol[j]=0; return(1)));
  return(0);
}
do(t, flag = 0, sol = 0, expo = 32) =
{ my(f = 1, H = 0);
  if (type(t) == "t_VEC",
      if (#t == 3, [t,f,H] = t, [t,f] = t));
  my(K = bnfinit(t), T);
  setrand(1); print(count++);
  T = liftpol(bnrclassfield(bnrinit(K,f), H, flag));
  if (!sol, return(T));
  if (type(T) != "t_VEC", return(check(K,T,sol,flag, expo)));
  [checkvec(K,t,~sol,flag, expo) | t<-T];
}

do(y^2+6) \\1
do(y^2+30)
do(y^2-y+10)
do(y^2-y+22)
s=x^6+(2*y-4)*x^5+(-2*y-49)*x^4+(-40*y+38)*x^3+(-22*y+456)*x^2+(168*y+288)*x+(180*y-611);
do(y^2-y+22,1,s) \\5

s=[x^8+(-2*y+2)*x^7+(-y-36)*x^6+(12*y-16)*x^5+(3*y+21)*x^4+(6*y+16)*x^3+(-4*y+12)*x^2+(2*y-18)*x+(2*y-7)];
do(y^2-y+24,,s)
s=[x^2+(-y-2),x^5+(101464750*y+192858990)*x^3+(-108719876266494185*y+152155420535969655)*x^2+(2694338662186141731750*y-14807033089517485684890)*x+(18862914098615255705031752813*y-410373474000278666208453327219)];
do(y^2-y+30,,s)
s=x^10+(-2*y-4)*x^9+(4*y-50)*x^8+(40*y-36)*x^7+(56*y+653)*x^6+(-242*y+1802)*x^5+(-818*y-408)*x^4+(-692*y-8486)*x^3+(1302*y-11876)*x^2+(3188*y-1340)*x+(2420*y+7577);
do(y^2-y+30,1,s)
s=[x^9-24*x^7+(-2*y+1)*x^6+495*x^5+(12*y-6)*x^4-30*x^3+(-18*y+9)*x^2+18*x+(2*y-1)];
do(y^2-y+50,,s)
s=[x^2+3,x^2+7,x^3-3*x+9];
do(y^2-y+58,,s) \\10

s=x^12-2*x^11+(-4*y-5)*x^10+(8*y-28)*x^9+(9*y-308)*x^8+(100*y+482)*x^7+(-132*y+429)*x^6+(1082*y-294)*x^5+(-1617*y+15083)*x^4+(-1770*y+18798)*x^3+(-2764*y-60684)*x^2+(15996*y-16436)*x+(-2045*y-11529);
do(y^2-y+58,1,s)

s=x^16-6*x^15+27*x^14-78*x^13+(4*y+142)*x^12+240*x^11+(-36*y+297)*x^10+(-48*y-564)*x^9-3143*x^8+(96*y-1224)*x^7+(144*y+1044)*x^6+1920*x^5+(-64*y+2336)*x^4-2496*x^3+1728*x^2-768*x+256;
do(y^2-y+595,1,s)

s=[x^3+15*x+(10*y-5),x^8+(-2*y+5)*x^6+(2*y-6)*x^5+(-5*y-75)*x^4+(-54*y-258)*x^3+(185*y-711)*x^2+(-118*y-66)*x+(72*y+305)];
do(y^2-y+174,,s)
do(y^2-y+1007)
do(y^2+105) \\15

s=x^27-13*x^26+87*x^25+(2*y-480)*x^24+(-17*y+2492)*x^23+(71*y-11474)*x^22+(-301*y+44475)*x^21+(1432*y-154355)*x^20+(-5878*y+491504)*x^19+(19312*y-1346275)*x^18+(-60553*y+3140529)*x^17+(168655*y-6741496)*x^16+(-371696*y+13501864)*x^15+(678767*y-24741734)*x^14+(-1082635*y+46288449)*x^13+(1183374*y-97233723)*x^12+(529638*y+197106251)*x^11+(-6132942*y-318835894)*x^10+(15301717*y+368696308)*x^9+(-23543559*y-267144050)*x^8+(25723793*y+63312848)*x^7+(-21031976*y+89347006)*x^6+(13238860*y-108009320)*x^5+(-6610848*y+54605464)*x^4+(2720928*y-12390400)*x^3+(-908928*y-1409280)*x^2+(203264*y+2308608)*x+(-20480*y-770048);
do(y^2-y+825,1,s)
s=[x^16+6*x^15+7*x^14+(2*y-78)*x^13+(9*y-318)*x^12+(2*y-110)*x^11+(-119*y+1388)*x^10+(-338*y+1248)*x^9+(116*y-4804)*x^8+(1844*y+328)*x^7+(1471*y+29005)*x^6+(-3194*y+12792)*x^5+(-2580*y-54159)*x^4+(4304*y-39034)*x^3+(2303*y+33060)*x^2+(-2220*y+9738)*x+(-540*y-8965)];
do(y^2-y+102,,s)
s=[x^3+78*x-592,x^3-39*x+310*y,x^3+6*x+4*y,x^3+78*x-1207168,x^4-4*y*x^3-5853*x^2+3916*y*x+959512];
do(y^2+974,,s)
do([y,[15,[1]]])

s=[x^3-291*x+1843,x^32+388*x^30+35890*x^28+1341995*x^26+26505541*x^24+312065587*x^22+2327967602*x^20+11357699251*x^18+36771136457*x^16+79108818025*x^14+111890072979*x^12+101706019796*x^10+57316093902*x^8+18924774302*x^6+3292770148*x^4+231052836*x^2+360937];
do([y,[97,[1]]],,s) \\20

do(y^3-y^2+3*y+6,,[x^4 + (y^2 - 2)*x^2 + (-y - 1)])
s=[x^2+(-28*y^2+36*y-25),x^4+(y^2+8*y-10)*x^2+(27*y^2-37*y+16)];
do([y^3-y^2+1,[6545,1333,2018;0,1,0;0,0,1]],,s)

\\do(y^2-y+33515) \\~4 seconds
\\do(y^2-y+246) \\~9 seconds

\\Bill's bug (internal rnfkummer needs optimal conductor)
s=[x^4+(1/32*a^2-1/8*a+53/8)*x^2-1];
do([a^4+392*a^2+64*a+37456,[31,8,5,29;0,1,0,0;0,0,1,0;0,0,0,1],Mat(4)],,s) \\23
s=[x^2+7,x^2+19,x^3-21*x+7,x^9-8436*x^7-26087*x^6+130131*x^5+282036*x^4-551760*x^3-293265*x^2+703836*x-241129];
do([y,[19*7,[1]]],,s)

print("sanitize");
do([a^4+392*a^2+64*a+37456,[31,8,5,29;0,1,0,0;0,0,1,0;0,0,0,1],Mat(3)]) \\25
bnrclassfield(bnfinit(y^2+6),Mat(2))

print("hard DLs (#2228)");
p = 1624384359015881850161120870813;
bnf = bnfinit('y); bnr = bnrinit(bnf,p);
L = bnrclassfield(bnr,2);
[#L,poldegree(L[1]),nfdisc(L[1])]
p = 1519276421662273577;
bnr = bnrinit(bnf,p);
L = bnrclassfield(bnr,4);
[#L,poldegree(L[1]),nfdisc(L[1])]

bnf=bnfinit('y^2-5); p=1594287814679644276013; bnr=bnrinit(bnf,p,,2);
bnrclassfield(bnr,2)

\\large compositum
do([y,[vecprod(primes(8)),[1]], 2],1)

\\flag 2
do(y^2+6,2)
s=x^12-6*x^11-4*x^10+162*x^9-111*x^8-2160*x^7+3880*x^6+21270*x^5-33936*x^4-161652*x^3+116306*x^2+927816*x+976141;
do(y^2-y+22,2,s)
s=x^24-4*x^23-10*x^22-20*x^21+462*x^20-1082*x^19+2961*x^18-31722*x^17+247117*x^16-882980*x^15+1842308*x^14-1544496*x^13+3211657*x^12-39314820*x^11+152409044*x^10-232536070*x^9+320758771*x^8+178913634*x^7+1263026049*x^6-5148649720*x^5+744537336*x^4-4090769680*x^3+17058701456*x^2-3566396096*x+399052096;
do(y^2-y+58,2,s)
do(y^2-y+10,2) \\30
do(y^2-y+24,2)
s=x^18-48*x^16+1566*x^14-23621*x^12+244113*x^10-19818*x^8-3170*x^6+17427*x^4-3258*x^2+199;
do(y^2-y+50,2,s,64)

\\#2371
bnf=bnfinit(y^6-y^5-285*y^4+6485*y^3+42484*y^2-2039004*y+12574320,1);
cond = idealfactorback(bnf,idealprimedec(bnf,53));
sol= 310814079206400*x^8+(-2486512633651200*y-621628158412800)*x^7+(66887916480*y^5+818204587200*y^4-14257492603200*y^3+8951914683835200*y^2+16487050492725120*y-89368170197644800)*x^6+(-5644994604480*y^5-32924404497600*y^4-16226414938641600*y^3-70063716497544000*y^2-321020635715176320*y+5190839147637158400)*x^5+(113452713491040*y^5+22942648879365600*y^4+69195975272978400*y^3+317297566469268000*y^2+17224950803810016960*y-191456383385131603200)*x^4+(-19442722659212160*y^5-130893599494627200*y^4+546316792739625600*y^3-17485006969648310400*y^2-98888467973396048640*y+2356534918683614592000)*x^3+(109122986233578240*y^5+2617790380992864000*y^4-54740124198804844800*y^3-324358882573563129600*y^2+19373008639762348623360*y-133533794658507646233600)*x^2+(-869940047122534080*y^5+3668917763131790400*y^4+356477231016250344000*y^3-4048958829765766593600*y^2-46696295095177525902720*y+558997264698690246105600)*x+(131100352706584800*y^5-17248825388454660000*y^4-335591938853448631200*y^3+3860394375558818512800*y^2+164739391281530106148800*y-1627160628948961718304000);
T = lift(bnrclassfield(bnrinit(bnf,cond,,2),,1));
check(bnf,T,~sol);

\\bug: correct absolute field, wrong extension
print("correct absolute, incorrect relative extension");
setrand(1);
bnf = bnfinit(y^2+7);
dec = idealprimedec(bnf,37);
bnr = bnrinit(bnf,dec[1]);
pol1 = bnrclassfield(bnr,9)[1];
s=x^9+(6*y+9)*x^7+(-y-20)*x^6+(45/2*y-99/2)*x^5+(-42*y+48)*x^4+(131/2*y-303/2)*x^3+(-36*y+279)*x^2+(-216*y-213)*x+(-86*y+1055);
check(bnf,pol1,s,0)
idealval(bnf,rnfdisc(bnf,pol1)[1],dec[1])

bnr = bnrinit(bnf,dec[2]);
pol2 = bnrclassfield(bnr,9)[1];
s=x^9+(-6*y+9)*x^7+(y-20)*x^6+(-45/2*y-99/2)*x^5+(42*y+48)*x^4+(-131/2*y-303/2)*x^3+(36*y+279)*x^2+(216*y-213)*x+(86*y+1055);
check(bnf,pol2,s,0)
idealval(bnf,rnfdisc(bnf,pol1)[1],dec[1])

\\bug: corrupted bnr
bnf = bnfinit(y^3-y^2+1);
bnr = bnrinit(bnf,[6545,1333,2018;0,1,0;0,0,1]);
bnrclassfield(bnr,[2,0;0,2])
bnrclassfield(bnr,[2,1;0,1])

print("segfault bug with character input");
poldegree(bnrclassfield(bnfinit(a^2+23),[1],2))==6

print("bug: relative polynomial instead of absolute");
bnf = bnfinit(y^3 - y^2 - 2*y + 1,1);
bnr = bnrinit(bnf,[Mat([idealprimedec(bnf,2)[1],3]),[1,1,1]]);
poldegree(bnrclassfield(bnr,[2, 0, 0, 0; 0, 2, 1, 0; 0, 0, 1, 0; 0, 0, 0, 1],2))

\\#2146
bnf=bnfinit(y^3-7*y-7,1);
bnr=bnrinit(bnf,[[13729,0,0;0,13729,0;0,0,13729],[0,0,0]]);
bnrclassfield(bnr,4,1);

print("examples from doc");
bnf = bnfinit(y^3+14*y-1); bnf.cyc
pol = bnrclassfield(bnf,,1) \\Hilbert class field
rnfdisc(bnf,pol)[1]
bnr = bnrinit(bnf,3*5*7); bnr.cyc
bnrclassfield(bnr,2) \\maximal 2-elementary subextension

R=bnrinit(bnfinit(t^2+115),7); f=bnrclassfield(R,[1,0;0,3]);
s=x^3+84*x-91399*t;
check(R,f[1],s,0)

print("tough discrete log");
p=406969676467377719018888256331;
bnr=bnrinit(bnfinit(a^2+1),p^2);
bnrclassfield(bnr,3)

print("tough factorization for structure of k(pr)^*");
setrand(1);p = randomprime(10^200,3);
bnrclassfield(bnrinit(bnfinit(y), p,, 3), 3)

print("tough conductor factorization");
setrand(1);
p=randomprime(2^200,7);
q=randomprime(2^200,7); bnf=bnfinit(a^2+a+2);
M=matconcat([idealfactor(bnf,p),idealfactor(bnf,q)]~);
bnr=bnrinit(bnf,M,,7); bnrclassfield(bnr)

print("vector of subgroups"); \\ avoid 32-bit differences
RED(red, a)=if (type(a)=="t_POL", red(a), apply(x->RED(red,x),a));
setrand(1);bnr=bnrinit(bnfinit(y^2+3299), 3,, 3);
L=subgrouplist(bnr, [3], 1);
setrand(1);RED(x->rnfpolredabs(bnr,x), bnrclassfield(bnr, L, 0))
setrand(1);RED(x->rnfpolredabs(bnr,x), bnrclassfield(bnr, L, 1))
setrand(1);RED(polredabs, bnrclassfield(bnr, L, 2))
bnrclassfield(bnr,[])
deg(a)=poldegree(if (type(a)=="t_POL", a, a[1]));
filter(v)=[if (cmp(a,b=(-1)^deg(a)*subst(a,x,-x)) < 0,b,a)| a<-v];
filter(bnrclassfield(bnfinit(y^2+31),[Mat(3)]))

\\ bnf.Ge contains t_FRAC
setrand(1);bnf=bnfinit(y^4+394*y^2+197);bnr=bnrinit(bnf,1,,5);f=bnrclassfield(bnr);
s=x^5+(-2349980/7*y^2-925002340/7)*x^3+(49550643842275363355/7*y^2+19498113719555868395685/7)*x^2+(1746603558382375785229605/14*y^2+321242974266101824080279155/14)*x+(-5046922430979180236843378133620866307/14*y^2-1986375600660391162873839261584212464823/14);
check(bnr,f[1],s,,147)

\\ test rnfkummersimple for ell > 2
setrand(1);bnrclassfield(bnfinit(y^4 - y^3 - 5*y^2 - 6*y + 36))

\\#2325
bnf = bnfinit(a^3-2);
bnrclassfield(bnf)
bnrclassfield(bnf,,1)
bnrclassfield(bnf,,2)
bnrclassfield(bnf,[[],[]])
bnrclassfield(bnf,[[],[]],1)
bnrclassfield(bnf,[[],[]],2)

K=bnrinit(bnfinit(y^4-y^3+16*y^2-16*y+61),[89,9,52,85;0,1,0,0;0,0,1,0;0,0,0,1]);
bnrclassfield(K,5)

\\ #2280
setrand(1);bnf=bnfinit(quadpoly(-2923,a)); R=bnrclassfield(bnf,3);
if(!check(bnf,R[1],x^3 + 48*x + (26*a - 13)),error("quadpoly(-2923),3"));

print("bad inputs");
bnrclassfield(y^2+6,Mat(2))
K = bnfinit(y^2+6);
bnr=bnrinit(K,2^66);m=matdiagonal(bnr.cyc);
bnrclassfield(bnr,m)
bnrclassfield(bnr,[m])
bnrclassfield(K,Mat(1/2))
bnrclassfield(K,1/2)
bnrclassfield(K,matid(2))
bnrclassfield(K,Mat(2),3)
bnrclassfield(K,Mat(2),-1)
bnrclassfield(vector(6),Mat(2))
bnrclassfield(bnrinit(bnfinit(y,1),[73786976294838212093,[1]]))
bnrclassfield(bnfinit(x));
