Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-11-07 10:15:33

0001 /*
0002     Copyright (C) 2007 William Hart
0003     Copyright (C) 2011 Sebastian Pancratz
0004     Copyright (C) 2013 Mike Hansen
0005 
0006     This file is part of FLINT.
0007 
0008     FLINT is free software: you can redistribute it and/or modify it under
0009     the terms of the GNU Lesser General Public License (LGPL) as published
0010     by the Free Software Foundation; either version 3 of the License, or
0011     (at your option) any later version.  See <https://www.gnu.org/licenses/>.
0012 */
0013 
0014 #ifndef FLINT_NTL_INT_H
0015 #define FLINT_NTL_INT_H
0016 
0017 #include <gmp.h>
0018 #include <NTL/ZZX.h>
0019 #include <NTL/ZZ_pXFactoring.h>
0020 #include <NTL/ZZ_pEX.h>
0021 #include <NTL/lzz_pXFactoring.h>
0022 #include <NTL/lzz_pEX.h>
0023 
0024 #include "fmpz.h"
0025 #include "fmpz_poly.h"
0026 #include "fmpz_mod_poly.h"
0027 #include "fq.h"
0028 #include "fq_poly.h"
0029 
0030 NTL_CLIENT
0031 
0032 /*
0033    Converts an NTL ZZ to an fmpz_t.
0034 
0035    Assumes the fmpz_t has already been allocated to have sufficient space.
0036 */
0037 inline void fmpz_set_ZZ(fmpz_t rop, const ZZ& op)
0038 {
0039     const _ntl_gbigint x = op.rep;
0040 
0041     if (!x)
0042         fmpz_zero(rop);
0043     else
0044     {
0045         const slong lw = op.size();
0046         const ulong *xp = ((ulong *) (((slong *) (x)) + 2));
0047 
0048         if (lw == 0)
0049             fmpz_zero(rop);
0050         else if (lw == 1)
0051             fmpz_set_ui(rop, xp[0]);
0052         else
0053         {
0054             mpz_ptr mf = _fmpz_promote(rop);
0055 
0056             mpz_import(mf, lw, -1, sizeof(ulong), 0, 0, xp);
0057         }
0058 
0059         if (op < WORD(0))
0060             fmpz_neg(rop, rop);
0061     }
0062 }
0063 
0064 /*
0065    Converts an fmpz_t to an NTL ZZ. Allocation is automatically handled.
0066  */
0067 inline void fmpz_get_ZZ(NTL_NNS ZZ& rop, const fmpz_t op)
0068 {
0069    ulong *xp;
0070    _ntl_gbigint *x = &rop.rep;
0071    slong lw = fmpz_size(op);
0072    fmpz c = *op;
0073 
0074    if (lw == 0)
0075    {
0076       if (*x) ((slong *)(*x))[1] = 0;  // size
0077       return;
0078    }
0079 
0080    _ntl_gsetlength(x, lw);
0081    xp = ((ulong *) (((slong *) (*x)) + 2));  // data
0082 
0083    if (COEFF_IS_MPZ(c))
0084    {
0085       mpz_ptr m = COEFF_TO_PTR(c);
0086       mpn_copyi(xp, m->_mp_d, lw);
0087    } else
0088    {
0089       if (c < WORD(0))
0090          xp[0] = -c;
0091       else
0092          xp[0] = c;
0093    }
0094 
0095    if (fmpz_sgn(op) < 0) ((slong *)(*x))[1] = -lw;
0096    else ((slong *)(*x))[1] = lw;
0097 }
0098 
0099 /*
0100    Converts an NTL ZZ_p to an fmpz_t.
0101 
0102    Assumes the fmpz_t has already been allocated to have sufficient space.
0103 */
0104 inline void fmpz_set_ZZ_p(fmpz_t rop, const NTL_NNS ZZ_p& op)
0105 {
0106     fmpz_set_ZZ(rop, rep(op));
0107 }
0108 
0109 /*
0110    Converts an fmpz_t to an NTL ZZ_p. Allocation is automatically handled.
0111  */
0112 inline void fmpz_get_ZZ_p(NTL_NNS ZZ_p& rop, const fmpz_t op)
0113 {
0114     ZZ a;
0115     fmpz_get_ZZ(a, op);
0116     conv(rop, a);
0117 }
0118 
0119 /*
0120    Converts an NTL zz_p to an fmpz_t.
0121 */
0122 inline void fmpz_set_zz_p(fmpz_t rop, const NTL_NNS zz_p& op)
0123 {
0124     fmpz_set_si(rop, rep(op));
0125 }
0126 
0127 /*
0128    Converts an fmpz_t to an NTL zz_p.
0129  */
0130 void fmpz_get_zz_p(NTL_NNS zz_p& rop, const fmpz_t op)
0131 {
0132     conv(rop, fmpz_get_si(op));
0133 }
0134 
0135 /*
0136   Converts an fmpz_poly_t to an NTL ZZX.
0137 */
0138 inline void fmpz_poly_get_ZZX(NTL_NNS ZZX& rop, const fmpz_poly_t op)
0139 {
0140     const slong len = op->length;
0141 
0142     if (len == 0)
0143     {
0144         rop = 0;
0145     }
0146     else
0147     {
0148         slong i;
0149         ZZ *ap;
0150 
0151         rop.rep.SetLength(len);
0152 
0153         for (i = 0, ap = rop.rep.elts(); i < len; i++, ap++)
0154         {
0155             fmpz_get_ZZ(*ap, op->coeffs + i);
0156         }
0157     }
0158 }
0159 
0160 /*
0161   Converts an NTL ZZX to an fmpz_poly_t.
0162 */
0163 inline void fmpz_poly_set_ZZX(fmpz_poly_t rop, const NTL_NNS ZZX& op)
0164 {
0165     const slong len = deg(op) + 1;
0166 
0167     if (len == 0)
0168     {
0169         fmpz_poly_zero(rop);
0170     }
0171     else
0172     {
0173         slong i;
0174         const ZZ *ap;
0175 
0176         fmpz_poly_fit_length(rop, len);
0177         _fmpz_poly_set_length(rop, len);
0178 
0179         for (i = 0, ap = op.rep.elts(); i < len; i++, ap++)
0180         {
0181             fmpz_set_ZZ(rop->coeffs + i, *ap);
0182         }
0183     }
0184 }
0185 /*
0186   Converts an fmpz_mod_poly_t to an NTL ZZ_pX.
0187 */
0188 inline void fmpz_mod_poly_get_ZZ_pX(NTL_NNS ZZ_pX& rop,
0189                              const fmpz_mod_poly_t op, const fmpz_mod_ctx_t ctx)
0190 {
0191     const slong len = op->length;
0192 
0193     if (len == 0)
0194     {
0195         rop = 0;
0196     }
0197     else
0198     {
0199         slong i;
0200         ZZ_p *ap;
0201 
0202         rop.rep.SetLength(len);
0203 
0204         for (i = 0, ap = rop.rep.elts(); i < len; i++, ap++)
0205         {
0206             fmpz_get_ZZ_p(*ap, op->coeffs + i);
0207         }
0208     }
0209 }
0210 
0211 /*
0212   Converts an NTL ZZ_pX to an fmpz_poly_t.
0213 */
0214 inline void fmpz_mod_poly_set_ZZ_pX(fmpz_mod_poly_t rop,
0215                               const NTL_NNS ZZ_pX& op, const fmpz_mod_ctx_t ctx)
0216 {
0217     const slong len = deg(op) + 1;
0218 
0219     if (len == 0)
0220     {
0221         fmpz_mod_poly_zero(rop, ctx);
0222     }
0223     else
0224     {
0225         slong i;
0226         const ZZ_p *ap;
0227 
0228         fmpz_mod_poly_fit_length(rop, len, ctx);
0229         _fmpz_mod_poly_set_length(rop, len);
0230 
0231         for (i = 0, ap = op.rep.elts(); i < len; i++, ap++)
0232         {
0233             fmpz_set_ZZ_p(rop->coeffs + i, *ap);
0234         }
0235     }
0236 }
0237 
0238 /*
0239   Converts an fq_t to an NTL ZZ_pE.
0240 */
0241 inline void fq_get_ZZ_pE(NTL_NNS ZZ_pE& rop, const fq_t op, const fq_ctx_t ctx)
0242 {
0243     ZZ_pX p;
0244 
0245     const slong len = op->length;
0246 
0247     if (len == 0)
0248     {
0249         rop = 0;
0250     }
0251     else
0252     {
0253         slong i;
0254         ZZ_p *ap;
0255 
0256         p.rep.SetLength(len);
0257 
0258         for (i = 0, ap = p.rep.elts(); i < len; i++, ap++)
0259         {
0260             fmpz_get_ZZ_p(*ap, op->coeffs + i);
0261         }
0262         conv(rop, p);
0263     }
0264 }
0265 
0266 /*
0267   Converts an NTL ZZ_pE to an fq_t.
0268 */
0269 inline void fq_set_ZZ_pE(fq_t rop, const NTL_NNS ZZ_pE& op, const fq_ctx_t ctx)
0270 {
0271     const slong len = deg(rep(op)) + 1;
0272 
0273     if (len == 0)
0274     {
0275         fq_zero(rop, ctx);
0276     }
0277     else
0278     {
0279         slong i;
0280         const ZZ_p *ap;
0281 
0282         fmpz_poly_fit_length(rop, len);
0283         _fmpz_poly_set_length(rop, len);
0284 
0285         for (i = 0, ap = rep(op).rep.elts(); i < len; i++, ap++)
0286         {
0287             fmpz_set_ZZ_p(rop->coeffs + i, *ap);
0288         }
0289         _fmpz_poly_normalise(rop);
0290     }
0291 }
0292 
0293 /*
0294   Converts an fq_poly_t to an NTL ZZ_pEX.
0295 */
0296 inline void fq_poly_get_ZZ_pEX(NTL_NNS ZZ_pEX& rop, const fq_poly_t op,
0297                                                              const fq_ctx_t ctx)
0298 {
0299     const slong len = op->length;
0300 
0301     if (len == 0)
0302     {
0303         rop = 0;
0304     }
0305     else
0306     {
0307         slong i;
0308         ZZ_pE *ap;
0309 
0310         rop.rep.SetLength(len);
0311 
0312         for (i = 0, ap = rop.rep.elts(); i < len; i++, ap++)
0313         {
0314             fq_get_ZZ_pE(*ap, op->coeffs + i, ctx);
0315         }
0316     }
0317 }
0318 
0319 /*
0320   Converts an NTL ZZ_pEX to an fq_poly_t.
0321 */
0322 inline void fq_poly_set_ZZ_pEX(fq_poly_t rop, const NTL_NNS ZZ_pEX& op,
0323                                                              const fq_ctx_t ctx)
0324 {
0325     const slong len = deg(op) + 1;
0326 
0327     if (len == 0)
0328     {
0329         fq_poly_zero(rop, ctx);
0330     }
0331     else
0332     {
0333         slong i;
0334         const ZZ_pE *ap;
0335 
0336         fq_poly_fit_length(rop, len, ctx);
0337         _fq_poly_set_length(rop, len, ctx);
0338 
0339         for (i = 0, ap = op.rep.elts(); i < len; i++, ap++)
0340         {
0341             fq_set_ZZ_pE(rop->coeffs + i, *ap, ctx);
0342         }
0343         _fq_poly_normalise(rop, ctx);
0344     }
0345 }
0346 
0347 /*
0348   Converts an fmpz_mod_poly_t to an NTL zz_pX.
0349 */
0350 inline void fmpz_mod_poly_get_zz_pX(NTL_NNS zz_pX& rop,
0351                              const fmpz_mod_poly_t op, const fmpz_mod_ctx_t ctx)
0352 {
0353     const slong len = op->length;
0354 
0355     if (len == 0)
0356     {
0357         rop = 0;
0358     }
0359     else
0360     {
0361         slong i;
0362         zz_p *ap;
0363 
0364         rop.rep.SetLength(len);
0365 
0366         for (i = 0, ap = rop.rep.elts(); i < len; i++, ap++)
0367         {
0368             fmpz_get_zz_p(*ap, op->coeffs + i);
0369         }
0370     }
0371 }
0372 
0373 /*
0374   Converts an NTL zz_pX to an fmpz_poly_t.
0375 */
0376 inline void fmpz_mod_poly_set_zz_pX(fmpz_mod_poly_t rop,
0377                               const NTL_NNS zz_pX& op, const fmpz_mod_ctx_t ctx)
0378 {
0379     const slong len = deg(op) + 1;
0380 
0381     if (len == 0)
0382     {
0383         fmpz_mod_poly_zero(rop, ctx);
0384     }
0385     else
0386     {
0387         slong i;
0388         const zz_p *ap;
0389 
0390         fmpz_mod_poly_fit_length(rop, len, ctx);
0391         _fmpz_mod_poly_set_length(rop, len);
0392 
0393         for (i = 0, ap = op.rep.elts(); i < len; i++, ap++)
0394         {
0395             fmpz_set_zz_p(rop->coeffs + i, *ap);
0396         }
0397     }
0398 }
0399 
0400 /*
0401   Converts an fq_t to an NTL zz_pE.
0402 */
0403 inline void fq_get_zz_pE(NTL_NNS zz_pE& rop, const fq_t op, const fq_ctx_t ctx)
0404 {
0405     zz_pX p;
0406 
0407     const slong len = op->length;
0408 
0409     if (len == 0)
0410     {
0411         rop = 0;
0412     }
0413     else
0414     {
0415         slong i;
0416         zz_p *ap;
0417 
0418         p.rep.SetLength(len);
0419 
0420         for (i = 0, ap = p.rep.elts(); i < len; i++, ap++)
0421         {
0422             fmpz_get_zz_p(*ap, op->coeffs + i);
0423         }
0424         conv(rop, p);
0425     }
0426 }
0427 
0428 /*
0429   Converts an NTL zz_pE to an fq_t.
0430 */
0431 inline void fq_set_zz_pE(fq_t rop, const NTL_NNS zz_pE& op, const fq_ctx_t ctx)
0432 {
0433     const slong len = deg(rep(op)) + 1;
0434 
0435     if (len == 0)
0436     {
0437         fq_zero(rop, ctx);
0438     }
0439     else
0440     {
0441         slong i;
0442         const zz_p *ap;
0443 
0444         fmpz_poly_fit_length(rop, len);
0445         _fmpz_poly_set_length(rop, len);
0446 
0447         for (i = 0, ap = rep(op).rep.elts(); i < len; i++, ap++)
0448         {
0449             fmpz_set_zz_p(rop->coeffs + i, *ap);
0450         }
0451         _fmpz_poly_normalise(rop);
0452     }
0453 }
0454 
0455 /*
0456   Converts an fq_poly_t to an NTL zz_pEX.
0457 */
0458 inline void fq_poly_get_zz_pEX(NTL_NNS zz_pEX& rop, const fq_poly_t op,
0459                                                              const fq_ctx_t ctx)
0460 {
0461     const slong len = op->length;
0462 
0463     if (len == 0)
0464     {
0465         rop = 0;
0466     }
0467     else
0468     {
0469         slong i;
0470         zz_pE *ap;
0471 
0472         rop.rep.SetLength(len);
0473 
0474         for (i = 0, ap = rop.rep.elts(); i < len; i++, ap++)
0475         {
0476             fq_get_zz_pE(*ap, op->coeffs + i, ctx);
0477         }
0478     }
0479 }
0480 
0481 /*
0482   Converts an NTL zz_pEX to an fq_poly_t.
0483 */
0484 inline void fq_poly_set_zz_pEX(fq_poly_t rop, const NTL_NNS zz_pEX& op,
0485                                                              const fq_ctx_t ctx)
0486 {
0487     const slong len = deg(op) + 1;
0488 
0489     if (len == 0)
0490     {
0491         fq_poly_zero(rop, ctx);
0492     }
0493     else
0494     {
0495         slong i;
0496         const zz_pE *ap;
0497 
0498         fq_poly_fit_length(rop, len, ctx);
0499         _fq_poly_set_length(rop, len, ctx);
0500 
0501         for (i = 0, ap = op.rep.elts(); i < len; i++, ap++)
0502         {
0503             fq_set_zz_pE(rop->coeffs + i, *ap, ctx);
0504         }
0505         _fq_poly_normalise(rop, ctx);
0506     }
0507 }
0508 
0509 #endif