Ticket #3232: bkz.patch

File bkz.patch, 49.3 kB (added by malb, 5 months ago)

addresses rpw's review

  • a/c_lib/include/ntl_wrap.h

    old new  
    1717#include <NTL/GF2EX.h> 
    1818#include <NTL/mat_GF2E.h> 
    1919#include <NTL/HNF.h> 
     20#include <NTL/LLL.h> 
    2021#include <gmp.h> 
    2122using namespace NTL; 
    2223#endif 
  • a/sage/libs/ntl/decl.pxi

    old new  
    737737    ZZ_c* mat_ZZ_determinant(mat_ZZ_c* x, long deterministic) 
    738738    mat_ZZ_c* mat_ZZ_HNF(mat_ZZ_c* A, ZZ_c* D) 
    739739    ZZX_c* mat_ZZ_charpoly(mat_ZZ_c* A) 
    740     long mat_ZZ_LLL(ZZ_c **det, mat_ZZ_c *x, long a, long b, long verbose) 
    741     long mat_ZZ_LLL_U(ZZ_c **det, mat_ZZ_c *x, mat_ZZ_c *U, long a, long b, long verbose) 
     740 
     741    cdef long mat_ZZ_LLL(ZZ_c **det, mat_ZZ_c *x, long a, long b, long verbose) 
     742    cdef long mat_ZZ_LLL_U(ZZ_c **det, mat_ZZ_c *x, mat_ZZ_c *U, long a, long b, long verbose) 
     743 
     744    cdef long mat_ZZ_LLL_FP   "LLL_FP"(mat_ZZ_c B, double delta, int deep, int check , int verbose) 
     745    cdef long mat_ZZ_LLL_FP_U "LLL_FP"(mat_ZZ_c B, mat_ZZ_c U, double delta, int deep, int check , int verbose) 
     746    cdef long mat_ZZ_LLL_QP   "LLL_QP"(mat_ZZ_c B, double delta, int deep, int check , int verbose) 
     747    cdef long mat_ZZ_LLL_QP_U "LLL_QP"(mat_ZZ_c B, mat_ZZ_c U, double delta, int deep, int check , int verbose) 
     748    cdef long mat_ZZ_LLL_XD   "LLL_XD"(mat_ZZ_c B, double delta, int deep, int check , int verbose) 
     749    cdef long mat_ZZ_LLL_XD_U "LLL_XD"(mat_ZZ_c B, mat_ZZ_c U, double delta, int deep, int check , int verbose) 
     750    cdef long mat_ZZ_LLL_RR   "LLL_RR"(mat_ZZ_c B, double delta, int deep, int check , int verbose) 
     751    cdef long mat_ZZ_LLL_RR_U "LLL_RR"(mat_ZZ_c B, mat_ZZ_c U, double delta, int deep, int check , int verbose) 
     752 
     753    cdef long mat_ZZ_G_LLL_FP   "G_LLL_FP"(mat_ZZ_c B, double delta, int deep, int check , int verbose) 
     754    cdef long mat_ZZ_G_LLL_FP_U "G_LLL_FP"(mat_ZZ_c B, mat_ZZ_c U, double delta, int deep, int check , int verbose) 
     755    cdef long mat_ZZ_G_LLL_QP   "G_LLL_QP"(mat_ZZ_c B, double delta, int deep, int check , int verbose) 
     756    cdef long mat_ZZ_G_LLL_QP_U "G_LLL_QP"(mat_ZZ_c B, mat_ZZ_c U, double delta, int deep, int check , int verbose) 
     757    cdef long mat_ZZ_G_LLL_XD   "G_LLL_XD"(mat_ZZ_c B, double delta, int deep, int check , int verbose) 
     758    cdef long mat_ZZ_G_LLL_XD_U "G_LLL_XD"(mat_ZZ_c B, mat_ZZ_c U, double delta, int deep, int check , int verbose) 
     759    cdef long mat_ZZ_G_LLL_RR   "G_LLL_RR"(mat_ZZ_c B, double delta, int deep, int check , int verbose) 
     760    cdef long mat_ZZ_G_LLL_RR_U "G_LLL_RR"(mat_ZZ_c B, mat_ZZ_c U, double delta, int deep, int check , int verbose) 
     761 
     762    cdef long mat_ZZ_BKZ_FP     "BKZ_FP"(mat_ZZ_c B, double delta, long BlockSize, long prune, int check, long verbose) 
     763    cdef long mat_ZZ_BKZ_FP_U   "BKZ_FP"(mat_ZZ_c B, mat_ZZ_c U, double delta, long BlockSize, long prune, int check, long verbose) 
     764    cdef long mat_ZZ_BKZ_XD     "BKZ_XD"(mat_ZZ_c B, double delta, long BlockSize, long prune, int check, long verbose) 
     765    cdef long mat_ZZ_BKZ_XD_U   "BKZ_XD"(mat_ZZ_c B, mat_ZZ_c U, double delta, long BlockSize, long prune, int check, long verbose) 
     766    cdef long mat_ZZ_BKZ_QP     "BKZ_QP"(mat_ZZ_c B, double delta, long BlockSize, long prune, int check, long verbose) 
     767    cdef long mat_ZZ_BKZ_QP_U   "BKZ_QP"(mat_ZZ_c B, mat_ZZ_c U, double delta, long BlockSize, long prune, int check, long verbose) 
     768    cdef long mat_ZZ_BKZ_QP1    "BKZ_QP1"(mat_ZZ_c B, double delta, long BlockSize, long prune, int check, long verbose) 
     769    cdef long mat_ZZ_BKZ_QP1_U  "BKZ_QP1"(mat_ZZ_c B, mat_ZZ_c U, double delta, long BlockSize, long prune, int check, long verbose) 
     770    cdef long mat_ZZ_BKZ_RR     "BKZ_RR"(mat_ZZ_c B, double delta, long BlockSize, long prune, int check, long verbose) 
     771    cdef long mat_ZZ_BKZ_RR_U   "BKZ_RR"(mat_ZZ_c B, mat_ZZ_c U, double delta, long BlockSize, long prune, int check, long verbose) 
     772 
     773    cdef long mat_ZZ_G_BKZ_FP     "G_BKZ_FP"(mat_ZZ_c B, double delta, long BlockSize, long prune, int check, long verbose) 
     774    cdef long mat_ZZ_G_BKZ_FP_U   "G_BKZ_FP"(mat_ZZ_c B, mat_ZZ_c U, double delta, long BlockSize, long prune, int check, long verbose) 
     775    cdef long mat_ZZ_G_BKZ_XD     "G_BKZ_XD"(mat_ZZ_c B, double delta, long BlockSize, long prune, int check, long verbose) 
     776    cdef long mat_ZZ_G_BKZ_XD_U   "G_BKZ_XD"(mat_ZZ_c B, mat_ZZ_c U, double delta, long BlockSize, long prune, int check, long verbose) 
     777    cdef long mat_ZZ_G_BKZ_QP     "G_BKZ_QP"(mat_ZZ_c B, double delta, long BlockSize, long prune, int check, long verbose) 
     778    cdef long mat_ZZ_G_BKZ_QP_U   "G_BKZ_QP"(mat_ZZ_c B, mat_ZZ_c U, double delta, long BlockSize, long prune, int check, long verbose) 
     779    cdef long mat_ZZ_G_BKZ_QP1    "G_BKZ_QP1"(mat_ZZ_c B, double delta, long BlockSize, long prune, int check, long verbose) 
     780    cdef long mat_ZZ_G_BKZ_QP1_U  "G_BKZ_QP1"(mat_ZZ_c B, mat_ZZ_c U, double delta, long BlockSize, long prune, int check, long verbose) 
     781    cdef long mat_ZZ_G_BKZ_RR     "G_BKZ_RR"(mat_ZZ_c B, double delta, long BlockSize, long prune, int check, long verbose) 
     782    cdef long mat_ZZ_G_BKZ_RR_U   "G_BKZ_RR"(mat_ZZ_c B, mat_ZZ_c U, double delta, long BlockSize, long prune, int check, long verbose) 
    742783 
    743784    #### GF2_c 
    744785    ctypedef struct GF2_c "struct GF2": 
     
    947988cdef extern from "ZZ_pylong.h": 
    948989    object ZZ_get_pylong(ZZ_c z) 
    949990    int ZZ_set_pylong(ZZ_c z, object ll) 
     991 
     992 
  • a/sage/libs/ntl/ntl_mat_ZZ.pyx

    old new  
    1717include "../../ext/stdsage.pxi" 
    1818include 'misc.pxi' 
    1919include 'decl.pxi' 
    20  
    21 cdef extern from "NTL/LLL.h": 
    22     cdef long mat_ZZ_LLL_FP   "LLL_FP"(mat_ZZ_c B, double delta, int deep, int check , int verbose) 
    23     cdef long mat_ZZ_LLL_FP_U "LLL_FP"(mat_ZZ_c B, mat_ZZ_c U, double delta, int deep, int check , int verbose) 
    24     cdef long mat_ZZ_LLL_QP   "LLL_QP"(mat_ZZ_c B, double delta, int deep, int check , int verbose) 
    25     cdef long mat_ZZ_LLL_QP_U "LLL_QP"(mat_ZZ_c B, mat_ZZ_c U, double delta, int deep, int check , int verbose) 
    26     cdef long mat_ZZ_LLL_XD   "LLL_XD"(mat_ZZ_c B, double delta, int deep, int check , int verbose) 
    27     cdef long mat_ZZ_LLL_XD_U "LLL_XD"(mat_ZZ_c B, mat_ZZ_c U, double delta, int deep, int check , int verbose) 
    28     cdef long mat_ZZ_LLL_RR   "LLL_RR"(mat_ZZ_c B, double delta, int deep, int check , int verbose) 
    29     cdef long mat_ZZ_LLL_RR_U "LLL_RR"(mat_ZZ_c B, mat_ZZ_c U, double delta, int deep, int check , int verbose) 
    30  
    31     cdef long mat_ZZ_G_LLL_FP   "G_LLL_FP"(mat_ZZ_c B, double delta, int deep, int check , int verbose) 
    32     cdef long mat_ZZ_G_LLL_FP_U "G_LLL_FP"(mat_ZZ_c B, mat_ZZ_c U, double delta, int deep, int check , int verbose) 
    33     cdef long mat_ZZ_G_LLL_QP   "G_LLL_QP"(mat_ZZ_c B, double delta, int deep, int check , int verbose) 
    34     cdef long mat_ZZ_G_LLL_QP_U "G_LLL_QP"(mat_ZZ_c B, mat_ZZ_c U, double delta, int deep, int check , int verbose) 
    35     cdef long mat_ZZ_G_LLL_XD   "G_LLL_XD"(mat_ZZ_c B, double delta, int deep, int check , int verbose) 
    36     cdef long mat_ZZ_G_LLL_XD_U "G_LLL_XD"(mat_ZZ_c B, mat_ZZ_c U, double delta, int deep, int check , int verbose) 
    37     cdef long mat_ZZ_G_LLL_RR   "G_LLL_RR"(mat_ZZ_c B, double delta, int deep, int check , int verbose) 
    38     cdef long mat_ZZ_G_LLL_RR_U "G_LLL_RR"(mat_ZZ_c B, mat_ZZ_c U, double delta, int deep, int check , int verbose) 
    3920 
    4021 
    4122from sage.libs.ntl.ntl_ZZ cimport ntl_ZZ 
     
    441422        _sig_off 
    442423        return r 
    443424 
     425    def BKZ_FP(self, U=None, delta=0.99, BlockSize=10, prune=0, verbose=False): 
     426        r""" 
     427        All BKZ methods are equivalent to the LLL routines, 
     428        except that Block Korkin-Zolotarev reduction is applied. We 
     429        describe here only the differences in the calling syntax. 
     430 
     431        * The optional parameter "BlockSize" specifies the size of the 
     432        blocks in the reduction. High values yield shorter vectors, 
     433        but the running time increases exponentially with BlockSize. 
     434        BlockSize should be between 2 and the number of rows of B. 
     435 
     436        * The optional parameter "prune" can be set to any positive 
     437        number to invoke the Volume Heuristic from [Schnorr and 
     438        Horner, Eurocrypt '95].  This can significantly reduce the 
     439        running time, and hence allow much bigger block size, but the 
     440        quality of the reduction is of course not as good in general. 
     441        Higher values of prune mean better quality, and slower running 
     442        time.  When prune == 0, pruning is disabled.  Recommended 
     443        usage: for BlockSize >= 30, set 10 <= prune <= 15. 
     444 
     445        * The QP1 variant uses quad_float precision to compute 
     446        Gram-Schmidt, but uses double precision in the search phase 
     447        of the block reduction algorithm.  This seems adequate for 
     448        most purposes, and is faster than QP, which uses quad_float 
     449        precision uniformly throughout. 
     450 
     451        INPUT: 
     452            U -- optional permutation matrix (see LLL, default: None) 
     453            delta -- reduction parameter (default: 0.99) 
     454            BlockSize -- see above (default: 10) 
     455            prune -- see above (default: 0) 
     456            verbose -- print verbose output (default: False) 
     457 
     458        EXAMPLE: 
     459            sage: A = Matrix(ZZ,5,5,range(25)) 
     460            sage: a = A._ntl_() 
     461            sage: a.BKZ_FP(); a 
     462            2 
     463            [ 
     464            [0 0 0 0 0] 
     465            [0 0 0 0 0] 
     466            [0 0 0 0 0] 
     467            [0 1 2 3 4] 
     468            [5 3 1 -1 -3] 
     469            ] 
     470 
     471            sage: U = ntl.mat_ZZ(2,2) # note that the dimension doesn't matter 
     472            sage: r = a.BKZ_FP(U=U); U 
     473            [ 
     474            [0 1 0 0 0] 
     475            [1 0 0 0 0] 
     476            [0 0 1 0 0] 
     477            [0 0 0 1 0] 
     478            [0 0 0 0 1] 
     479            ] 
     480        """ 
     481        if U is None: 
     482            _sig_on 
     483            rank = mat_ZZ_BKZ_FP(self.x, float(delta), int(BlockSize), int(prune), 0, int(verbose)); 
     484            _sig_off 
     485            return rank 
     486        elif PY_TYPE_CHECK(U, ntl_mat_ZZ): 
     487            _sig_on 
     488            rank = mat_ZZ_BKZ_FP_U(self.x, (<ntl_mat_ZZ>U).x, float(delta), int(BlockSize), int(prune), 0, int(verbose)); 
     489            _sig_off 
     490            return rank 
     491        else: 
     492            raise TypeError, "parameter U has wrong type." 
     493 
     494    def BKZ_QP(self, U=None, delta=0.99, BlockSize=10, prune=0, verbose=False): 
     495        r""" 
     496        All BKZ methods are equivalent to the LLL routines, 
     497        except that Block Korkin-Zolotarev reduction is applied. We 
     498        describe here only the differences in the calling syntax. 
     499 
     500        * The optional parameter "BlockSize" specifies the size of the 
     501        blocks in the reduction. High values yield shorter vectors, 
     502        but the running time increases exponentially with BlockSize. 
     503        BlockSize should be between 2 and the number of rows of B. 
     504 
     505        * The optional parameter "prune" can be set to any positive 
     506        number to invoke the Volume Heuristic from [Schnorr and 
     507        Horner, Eurocrypt '95].  This can significantly reduce the 
     508        running time, and hence allow much bigger block size, but the 
     509        quality of the reduction is of course not as good in general. 
     510        Higher values of prune mean better quality, and slower running 
     511        time.  When prune == 0, pruning is disabled.  Recommended 
     512        usage: for BlockSize >= 30, set 10 <= prune <= 15. 
     513 
     514        * The QP1 variant uses quad_float precision to compute 
     515        Gram-Schmidt, but uses double precision in the search phase 
     516        of the block reduction algorithm.  This seems adequate for 
     517        most purposes, and is faster than QP, which uses quad_float 
     518        precision uniformly throughout. 
     519 
     520        INPUT: 
     521            U -- optional permutation matrix (see LLL, default: None) 
     522            delta -- reduction parameter (default: 0.99) 
     523            BlockSize -- see above (default: 10) 
     524            prune -- see above (default: 0) 
     525            verbose -- print verbose output (default: False) 
     526 
     527        EXAMPLE: 
     528            sage: A = Matrix(ZZ,5,5,range(25)) 
     529            sage: a = A._ntl_() 
     530            sage: a.BKZ_QP(); a 
     531            2 
     532            [ 
     533            [0 0 0 0 0] 
     534            [0 0 0 0 0] 
     535            [0 0 0 0 0] 
     536            [0 1 2 3 4] 
     537            [5 3 1 -1 -3] 
     538            ] 
     539 
     540            sage: U = ntl.mat_ZZ(2,2) # note that the dimension doesn't matter 
     541            sage: r = a.BKZ_QP(U=U); U 
     542            [ 
     543            [0 1 0 0 0] 
     544            [1 0 0 0 0] 
     545            [0 0 1 0 0] 
     546            [0 0 0 1 0] 
     547            [0 0 0 0 1] 
     548            ] 
     549        """ 
     550        if U is None: 
     551            _sig_on 
     552            rank = mat_ZZ_BKZ_QP(self.x, float(delta), int(BlockSize), int(prune), 0, int(verbose)); 
     553            _sig_off 
     554            return rank 
     555        elif PY_TYPE_CHECK(U, ntl_mat_ZZ): 
     556            _sig_on 
     557            rank = mat_ZZ_BKZ_QP_U(self.x, (<ntl_mat_ZZ>U).x, float(delta), int(BlockSize), int(prune), 0, int(verbose)); 
     558            _sig_off 
     559            return rank 
     560        else: 
     561            raise TypeError, "parameter U has wrong type." 
     562 
     563    def BKZ_QP1(self, U=None, delta=0.99, BlockSize=10, prune=0, verbose=False): 
     564        r""" 
     565        All BKZ methods are equivalent to the LLL routines, 
     566        except that Block Korkin-Zolotarev reduction is applied. We 
     567        describe here only the differences in the calling syntax. 
     568 
     569        * The optional parameter "BlockSize" specifies the size of the 
     570        blocks in the reduction. High values yield shorter vectors, 
     571        but the running time increases exponentially with BlockSize. 
     572        BlockSize should be between 2 and the number of rows of B. 
     573 
     574        * The optional parameter "prune" can be set to any positive 
     575        number to invoke the Volume Heuristic from [Schnorr and 
     576        Horner, Eurocrypt '95].  This can significantly reduce the 
     577        running time, and hence allow much bigger block size, but the 
     578        quality of the reduction is of course not as good in general. 
     579        Higher values of prune mean better quality, and slower running 
     580        time.  When prune == 0, pruning is disabled.  Recommended 
     581        usage: for BlockSize >= 30, set 10 <= prune <= 15. 
     582 
     583        * The QP1 variant uses quad_float precision to compute 
     584        Gram-Schmidt, but uses double precision in the search phase 
     585        of the block reduction algorithm.  This seems adequate for 
     586        most purposes, and is faster than QP, which uses quad_float 
     587        precision uniformly throughout. 
     588 
     589        INPUT: 
     590            U -- optional permutation matrix (see LLL, default: None) 
     591            delta -- reduction parameter (default: 0.99) 
     592            BlockSize -- see above (default: 10) 
     593            prune -- see above (default: 0) 
     594            verbose -- print verbose output (default: False) 
     595 
     596        EXAMPLE: 
     597            sage: A = Matrix(ZZ,5,5,range(25)) 
     598            sage: a = A._ntl_() 
     599            sage: a.BKZ_QP1(); a 
     600            2 
     601            [ 
     602            [0 0 0 0 0] 
     603            [0 0 0 0 0] 
     604            [0 0 0 0 0] 
     605            [0 1 2 3 4] 
     606            [5 3 1 -1 -3] 
     607            ] 
     608 
     609            sage: U = ntl.mat_ZZ(2,2) # note that the dimension doesn't matter 
     610            sage: r = a.BKZ_QP1(U=U); U 
     611            [ 
     612            [0 1 0 0 0] 
     613            [1 0 0 0 0] 
     614            [0 0 1 0 0] 
     615            [0 0 0 1 0] 
     616            [0 0 0 0 1] 
     617            ] 
     618        """ 
     619        if U is None: 
     620            _sig_on 
     621            rank = mat_ZZ_BKZ_QP1(self.x, float(delta), int(BlockSize), int(prune), 0, int(verbose)); 
     622            _sig_off 
     623            return rank 
     624        elif PY_TYPE_CHECK(U, ntl_mat_ZZ): 
     625            _sig_on 
     626            rank = mat_ZZ_BKZ_QP1_U(self.x, (<ntl_mat_ZZ>U).x, float(delta), int(BlockSize), int(prune), 0, int(verbose)); 
     627            _sig_off 
     628            return rank 
     629        else: 
     630            raise TypeError, "parameter U has wrong type." 
     631 
     632    def BKZ_XD(self, U=None, delta=0.99, BlockSize=10, prune=0, verbose=False): 
     633        r""" 
     634        All BKZ methods are equivalent to the LLL routines, 
     635        except that Block Korkin-Zolotarev reduction is applied. We 
     636        describe here only the differences in the calling syntax. 
     637 
     638        * The optional parameter "BlockSize" specifies the size of the 
     639        blocks in the reduction. High values yield shorter vectors, 
     640        but the running time increases exponentially with BlockSize. 
     641        BlockSize should be between 2 and the number of rows of B. 
     642 
     643        * The optional parameter "prune" can be set to any positive 
     644        number to invoke the Volume Heuristic from [Schnorr and 
     645        Horner, Eurocrypt '95].  This can significantly reduce the 
     646        running time, and hence allow much bigger block size, but the 
     647        quality of the reduction is of course not as good in general. 
     648        Higher values of prune mean better quality, and slower running 
     649        time.  When prune == 0, pruning is disabled.  Recommended 
     650        usage: for BlockSize >= 30, set 10 <= prune <= 15. 
     651 
     652        * The QP1 variant uses quad_float precision to compute 
     653        Gram-Schmidt, but uses double precision in the search phase 
     654        of the block reduction algorithm.  This seems adequate for 
     655        most purposes, and is faster than QP, which uses quad_float 
     656        precision uniformly throughout. 
     657 
     658        INPUT: 
     659            U -- optional permutation matrix (see LLL, default: None) 
     660            delta -- reduction parameter (default: 0.99) 
     661            BlockSize -- see above (default: 10) 
     662            prune -- see above (default: 0) 
     663            verbose -- print verbose output (default: False) 
     664 
     665        EXAMPLE: 
     666            sage: A = Matrix(ZZ,5,5,range(25)) 
     667            sage: a = A._ntl_() 
     668            sage: a.BKZ_XD(); a 
     669            2 
     670            [ 
     671            [0 0 0 0 0] 
     672            [0 0 0 0 0] 
     673            [0 0 0 0 0] 
     674            [0 1 2 3 4] 
     675            [5 3 1 -1 -3] 
     676            ] 
     677 
     678            sage: U = ntl.mat_ZZ(2,2) # note that the dimension doesn't matter 
     679            sage: r = a.BKZ_XD(U=U); U 
     680            [ 
     681            [0 1 0 0 0] 
     682            [1 0 0 0 0] 
     683            [0 0 1 0 0] 
     684            [0 0 0 1 0] 
     685            [0 0 0 0 1] 
     686            ] 
     687        """ 
     688        if U is None: 
     689            _sig_on 
     690            rank = mat_ZZ_BKZ_XD(self.x, float(delta), int(BlockSize), int(prune), 0, int(verbose)); 
     691            _sig_off 
     692            return rank 
     693        elif PY_TYPE_CHECK(U, ntl_mat_ZZ): 
     694            _sig_on 
     695            rank = mat_ZZ_BKZ_XD_U(self.x, (<ntl_mat_ZZ>U).x, float(delta), int(BlockSize), int(prune), 0, int(verbose)); 
     696            _sig_off 
     697            return rank 
     698        else: 
     699            raise TypeError, "parameter U has wrong type." 
     700 
     701    def BKZ_RR(self, U=None, delta=0.99, BlockSize=10, prune=0, verbose=False): 
     702        r""" 
     703        All BKZ methods are equivalent to the LLL routines, 
     704        except that Block Korkin-Zolotarev reduction is applied. We 
     705        describe here only the differences in the calling syntax. 
     706 
     707        * The optional parameter "BlockSize" specifies the size of the 
     708        blocks in the reduction. High values yield shorter vectors, 
     709        but the running time increases exponentially with BlockSize. 
     710        BlockSize should be between 2 and the number of rows of B. 
     711 
     712        * The optional parameter "prune" can be set to any positive 
     713        number to invoke the Volume Heuristic from [Schnorr and 
     714        Horner, Eurocrypt '95].  This can significantly reduce the 
     715        running time, and hence allow much bigger block size, but the 
     716        quality of the reduction is of course not as good in general. 
     717        Higher values of prune mean better quality, and slower running 
     718        time.  When prune == 0, pruning is disabled.  Recommended 
     719        usage: for BlockSize >= 30, set 10 <= prune <= 15. 
     720 
     721        * The QP1 variant uses quad_float precision to compute 
     722        Gram-Schmidt, but uses double precision in the search phase 
     723        of the block reduction algorithm.  This seems adequate for 
     724        most purposes, and is faster than QP, which uses quad_float 
     725        precision uniformly throughout. 
     726 
     727        INPUT: 
     728            U -- optional permutation matrix (see LLL, default: None) 
     729            delta -- reduction parameter (default: 0.99) 
     730            BlockSize -- see above (default: 10) 
     731            prune -- see above (default: 0) 
     732            verbose -- print verbose output (default: False) 
     733 
     734        EXAMPLE: 
     735            sage: A = Matrix(ZZ,5,5,range(25)) 
     736            sage: a = A._ntl_() 
     737            sage: a.BKZ_RR(); a 
     738            2 
     739            [ 
     740            [0 0 0 0 0] 
     741            [0 0 0 0 0] 
     742            [0 0 0 0 0] 
     743            [0 1 2 3 4] 
     744            [5 3 1 -1 -3] 
     745            ] 
     746 
     747            sage: U = ntl.mat_ZZ(2,2) # note that the dimension doesn't matter 
     748            sage: r = a.BKZ_RR(U=U); U 
     749            [ 
     750            [0 1 0 0 0] 
     751            [1 0 0 0 0] 
     752            [0 0 1 0 0] 
     753            [0 0 0 1 0] 
     754            [0 0 0 0 1] 
     755            ] 
     756        """ 
     757        if U is None: 
     758            _sig_on 
     759            rank = mat_ZZ_BKZ_RR(self.x, float(delta), int(BlockSize), int(prune), 0, int(verbose)); 
     760            _sig_off 
     761            return rank 
     762        elif PY_TYPE_CHECK(U, ntl_mat_ZZ): 
     763            _sig_on 
     764            rank = mat_ZZ_BKZ_RR_U(self.x, (<ntl_mat_ZZ>U).x, float(delta), int(BlockSize), int(prune), 0, int(verbose)); 
     765            _sig_off 
     766            return rank 
     767        else: 
     768            raise TypeError, "parameter U has wrong type." 
     769 
     770    def G_BKZ_FP(self, U=None, delta=0.99, BlockSize=10, prune=0, verbose=False): 
     771        r""" 
     772        All BKZ methods are equivalent to the LLL routines, 
     773        except that Block Korkin-Zolotarev reduction is applied. We 
     774        describe here only the differences in the calling syntax. 
     775 
     776        * The optional parameter "BlockSize" specifies the size of the 
     777        blocks in the reduction. High values yield shorter vectors, 
     778        but the running time increases exponentially with BlockSize. 
     779        BlockSize should be between 2 and the number of rows of B. 
     780 
     781        * The optional parameter "prune" can be set to any positive 
     782        number to invoke the Volume Heuristic from [Schnorr and 
     783        Horner, Eurocrypt '95].  This can significantly reduce the 
     784        running time, and hence allow much bigger block size, but the 
     785        quality of the reduction is of course not as good in general. 
     786        Higher values of prune mean better quality, and slower running 
     787        time.  When prune == 0, pruning is disabled.  Recommended 
     788        usage: for BlockSize >= 30, set 10 <= prune <= 15. 
     789 
     790        * The QP1 variant uses quad_float precision to compute 
     791        Gram-Schmidt, but uses double precision in the search phase 
     792        of the block reduction algorithm.  This seems adequate for 
     793        most purposes, and is faster than QP, which uses quad_float 
     794        precision uniformly throughout. 
     795 
     796        INPUT: 
     797            U -- optional permutation matrix (see LLL, default: None) 
     798            delta -- reduction parameter (default: 0.99) 
     799            BlockSize -- see above (default: 10) 
     800            prune -- see above (default: 0) 
     801            verbose -- print verbose output (default: False) 
     802 
     803        EXAMPLE: 
     804            sage: A = Matrix(ZZ,5,5,range(25)) 
     805            sage: a = A._ntl_() 
     806            sage: a.G_BKZ_FP(); a 
     807            2 
     808            [ 
     809            [0 0 0 0 0] 
     810            [0 0 0 0 0] 
     811            [0 0 0 0 0] 
     812            [0 1 2 3 4] 
     813            [5 3 1 -1 -3] 
     814            ] 
     815 
     816            sage: U = ntl.mat_ZZ(2,2) # note that the dimension doesn't matter 
     817            sage: r = a.G_BKZ_FP(U=U); U 
     818            [ 
     819            [0 1 0 0 0] 
     820            [1 0 0 0 0] 
     821            [0 0 1 0 0] 
     822            [0 0 0 1 0] 
     823            [0 0 0 0 1] 
     824            ] 
     825        """ 
     826        if U is None: 
     827            _sig_on 
     828            rank = mat_ZZ_G_BKZ_FP(self.x, float(delta), int(BlockSize), int(prune), 0, int(verbose)); 
     829            _sig_off 
     830            return rank 
     831        elif PY_TYPE_CHECK(U, ntl_mat_ZZ): 
     832            _sig_on 
     833            rank = mat_ZZ_G_BKZ_FP_U(self.x, (<ntl_mat_ZZ>U).x, float(delta), int(BlockSize), int(prune), 0, int(verbose)); 
     834            _sig_off 
     835            return rank 
     836        else: 
     837            raise TypeError, "parameter U has wrong type." 
     838 
     839    def G_BKZ_QP(self, U=None, delta=0.99, BlockSize=10, prune=0, verbose=False): 
     840        r""" 
     841        All BKZ methods are equivalent to the LLL routines, 
     842        except that Block Korkin-Zolotarev reduction is applied. We 
     843        describe here only the differences in the calling syntax. 
     844 
     845        * The optional parameter "BlockSize" specifies the size of the 
     846        blocks in the reduction. High values yield shorter vectors, 
     847        but the running time increases exponentially with BlockSize. 
     848        BlockSize should be between 2 and the number of rows of B. 
     849 
     850        * The optional parameter "prune" can be set to any positive 
     851        number to invoke the Volume Heuristic from [Schnorr and 
     852        Horner, Eurocrypt '95].  This can significantly reduce the 
     853        running time, and hence allow much bigger block size, but the 
     854        quality of the reduction is of course not as good in general. 
     855        Higher values of prune mean better quality, and slower running 
     856        time.  When prune == 0, pruning is disabled.  Recommended 
     857        usage: for BlockSize >= 30, set 10 <= prune <= 15. 
     858 
     859        * The QP1 variant uses quad_float precision to compute 
     860        Gram-Schmidt, but uses double precision in the search phase 
     861        of the block reduction algorithm.  This seems adequate for 
     862        most purposes, and is faster than QP, which uses quad_float 
     863        precision uniformly throughout. 
     864 
     865        INPUT: 
     866            U -- optional permutation matrix (see LLL, default: None) 
     867            delta -- reduction parameter (default: 0.99) 
     868            BlockSize -- see above (default: 10) 
     869            prune -- see above (default: 0) 
     870            verbose -- print verbose output (default: False) 
     871 
     872        EXAMPLE: 
     873            sage: A = Matrix(ZZ,5,5,range(25)) 
     874            sage: a = A._ntl_() 
     875            sage: a.G_BKZ_QP(); a 
     876            2 
     877            [ 
     878            [0 0 0 0 0] 
     879            [0 0 0 0 0] 
     880            [0 0 0 0 0] 
     881            [0 1 2 3 4] 
     882            [5 3 1 -1 -3] 
     883            ] 
     884 
     885            sage: U = ntl.mat_ZZ(2,2) # note that the dimension doesn't matter 
     886            sage: r = a.G_BKZ_QP(U=U); U 
     887            [ 
     888            [0 1 0 0 0] 
     889            [1 0 0 0 0] 
     890            [0 0 1 0 0] 
     891            [0 0 0 1 0] 
     892            [0 0 0 0 1] 
     893            ] 
     894        """ 
     895        if U is None: 
     896            _sig_on 
     897            rank = mat_ZZ_G_BKZ_QP(self.x, float(delta), int(BlockSize), int(prune), 0, int(verbose)); 
     898            _sig_off 
     899            return rank 
     900        elif PY_TYPE_CHECK(U, ntl_mat_ZZ): 
     901            _sig_on 
     902            rank = mat_ZZ_G_BKZ_QP_U(self.x, (<ntl_mat_ZZ>U).x, float(delta), int(BlockSize), int(prune), 0, int(verbose)); 
     903            _sig_off 
     904            return rank 
     905        else: 
     906            raise TypeError, "parameter U has wrong type." 
     907 
     908    def G_BKZ_QP1(self, U=None, delta=0.99, BlockSize=10, prune=0, verbose=False): 
     909        r""" 
     910        All BKZ methods are equivalent to the LLL routines, 
     911        except that Block Korkin-Zolotarev reduction is applied. We 
     912        describe here only the differences in the calling syntax. 
     913 
     914        * The optional parameter "BlockSize" specifies the size of the 
     915        blocks in the reduction. High values yield shorter vectors, 
     916        but the running time increases exponentially with BlockSize. 
     917        BlockSize should be between 2 and the number of rows of B. 
     918 
     919        * The optional parameter "prune" can be set to any positive 
     920        number to invoke the Volume Heuristic from [Schnorr and 
     921        Horner, Eurocrypt '95].  This can significantly reduce the 
     922        running time, and hence allow much bigger block size, but the 
     923        quality of the reduction is of course not as good in general. 
     924        Higher values of prune mean better quality, and slower running 
     925        time.  When prune == 0, pruning is disabled.  Recommended 
     926        usage: for BlockSize >= 30, set 10 <= prune <= 15. 
     927 
     928        * The QP1 variant uses quad_float precision to compute 
     929        Gram-Schmidt, but uses double precision in the search phase 
     930        of the block reduction algorithm.  This seems adequate for 
     931        most purposes, and is faster than QP, which uses quad_float 
     932        precision uniformly throughout. 
     933 
     934        INPUT: 
     935            U -- optional permutation matrix (see LLL, default: None) 
     936            delta -- reduction parameter (default: 0.99) 
     937            BlockSize -- see above (default: 10) 
     938            prune -- see above (default: 0) 
     939            verbose -- print verbose output (default: False) 
     940 
     941        EXAMPLE: 
     942            sage: A = Matrix(ZZ,5,5,range(25)) 
     943            sage: a = A._ntl_() 
     944            sage: a.G_BKZ_QP1(); a 
     945            2 
     946            [ 
     947            [0 0 0 0 0] 
     948            [0 0 0 0 0] 
     949            [0 0 0 0 0] 
     950            [0 1 2 3 4] 
     951            [5 3 1 -1 -3] 
     952            ] 
     953 
     954            sage: U = ntl.mat_ZZ(2,2) # note that the dimension doesn't matter 
     955            sage: r = a.G_BKZ_QP1(U=U); U 
     956            [ 
     957            [0 1 0 0 0] 
     958            [1 0 0 0 0] 
     959            [0 0 1 0 0] 
     960            [0 0 0 1 0] 
     961            [0 0 0 0 1] 
     962            ] 
     963        """ 
     964        if U is None: 
     965            _sig_on 
     966            rank = mat_ZZ_G_BKZ_QP1(self.x, float(delta), int(BlockSize), int(prune), 0, int(verbose)); 
     967            _sig_off 
     968            return rank 
     969        elif PY_TYPE_CHECK(U, ntl_mat_ZZ): 
     970            _sig_on 
     971            rank = mat_ZZ_G_BKZ_QP1_U(self.x, (<ntl_mat_ZZ>U).x, float(delta), int(BlockSize), int(prune), 0, int(verbose)); 
     972            _sig_off 
     973            return rank 
     974        else: 
     975            raise TypeError, "parameter U has wrong type." 
     976 
     977    def G_BKZ_XD(self, U=None, delta=0.99, BlockSize=10, prune=0, verbose=False): 
     978        r""" 
     979        All BKZ methods are equivalent to the LLL routines, 
     980        except that Block Korkin-Zolotarev reduction is applied. We 
     981        describe here only the differences in the calling syntax. 
     982 
     983        * The optional parameter "BlockSize" specifies the size of the 
     984        blocks in the reduction. High values yield shorter vectors, 
     985        but the running time increases exponentially with BlockSize. 
     986        BlockSize should be between 2 and the number of rows of B. 
     987 
     988        * The optional parameter "prune" can be set to any positive 
     989        number to invoke the Volume Heuristic from [Schnorr and 
     990        Horner, Eurocrypt '95].  This can significantly reduce the 
     991        running time, and hence allow much bigger block size, but the 
     992        quality of the reduction is of course not as good in general. 
     993        Higher values of prune mean better quality, and slower running 
     994        time.  When prune == 0, pruning is disabled.  Recommended 
     995        usage: for BlockSize >= 30, set 10 <= prune <= 15. 
     996 
     997        * The QP1 variant uses quad_float precision to compute 
     998        Gram-Schmidt, but uses double precision in the search phase 
     999        of the block reduction algorithm.  This seems adequate for 
     1000        most purposes, and is faster than QP, which uses quad_float 
     1001        precision uniformly throughout. 
     1002 
     1003        INPUT: 
     1004            U -- optional permutation matrix (see LLL, default: None) 
     1005            delta -- reduction parameter (default: 0.99) 
     1006            BlockSize -- see above (default: 10) 
     1007            prune -- see above (default: 0) 
     1008            verbose -- print verbose output (default: False) 
     1009 
     1010        EXAMPLE: 
     1011            sage: A = Matrix(ZZ,5,5,range(25)) 
     1012            sage: a = A._ntl_() 
     1013            sage: a.G_BKZ_XD(); a 
     1014            2 
     1015            [ 
     1016            [0 0 0 0 0] 
     1017            [0 0 0 0 0] 
     1018            [0 0 0 0 0] 
     1019            [0 1 2 3 4] 
     1020            [5 3 1 -1 -3] 
     1021            ] 
     1022 
     1023            sage: U = ntl.mat_ZZ(2,2) # note that the dimension doesn't matter 
     1024            sage: r = a.G_BKZ_XD(U=U); U 
     1025            [ 
     1026            [0 1 0 0 0] 
     1027            [1 0 0 0 0] 
     1028            [0 0 1 0 0] 
     1029            [0 0 0 1 0] 
     1030            [0 0 0 0 1] 
     1031            ] 
     1032        """ 
     1033        if U is None: 
     1034            _sig_on 
     1035            rank = mat_ZZ_G_BKZ_XD(self.x, float(delta), int(BlockSize), int(prune), 0, int(verbose)); 
     1036            _sig_off 
     1037            return rank 
     1038        elif PY_TYPE_CHECK(U, ntl_mat_ZZ): 
     1039            _sig_on 
     1040            rank = mat_ZZ_G_BKZ_XD_U(self.x, (<ntl_mat_ZZ>U).x, float(delta), int(BlockSize), int(prune), 0, int(verbose)); 
     1041            _sig_off 
     1042            return rank 
     1043        else: 
     1044            raise TypeError, "parameter U has wrong type." 
     1045 
     1046    def G_BKZ_RR(self, U=None, delta=0.99, BlockSize=10, prune=0, verbose=False): 
     1047        r""" 
     1048        All BKZ methods are equivalent to the LLL routines, 
     1049        except that Block Korkin-Zolotarev reduction is applied. We 
     1050        describe here only the differences in the calling syntax. 
     1051 
     1052        * The optional parameter "BlockSize" specifies the size of the 
     1053        blocks in the reduction. High values yield shorter vectors, 
     1054        but the running time increases exponentially with BlockSize. 
     1055        BlockSize should be between 2 and the number of rows of B. 
     1056 
     1057        * The optional parameter "prune" can be set to any positive 
     1058        number to invoke the Volume Heuristic from [Schnorr and 
     1059        Horner, Eurocrypt '95].  This can significantly reduce the 
     1060        running time, and hence allow much bigger block size, but the 
     1061        quality of the reduction is of course not as good in general. 
     1062        Higher values of prune mean better quality, and slower running 
     1063        time.  When prune == 0, pruning is disabled.  Recommended 
     1064        usage: for BlockSize >= 30, set 10 <= prune <= 15. 
     1065 
     1066        * The QP1 variant uses quad_float precision to compute 
     1067        Gram-Schmidt, but uses double precision in the search phase 
     1068        of the block reduction algorithm.  This seems adequate for 
     1069        most purposes, and is faster than QP, which uses quad_float 
     1070        precision uniformly throughout. 
     1071 
     1072        INPUT: 
     1073            U -- optional permutation matrix (see LLL, default: None) 
     1074            delta -- reduction parameter (default: 0.99) 
     1075            BlockSize -- see above (default: 10) 
     1076            prune -- see above (default: 0) 
     1077            verbose -- print verbose output (default: False) 
     1078 
     1079        EXAMPLE: 
     1080            sage: A = Matrix(ZZ,5,5,range(25)) 
     1081            sage: a = A._ntl_() 
     1082            sage: a.G_BKZ_RR(); a 
     1083            2 
     1084            [ 
     1085            [0 0 0 0 0] 
     1086            [0 0 0 0 0] 
     1087            [0 0 0 0 0] 
     1088            [0 1 2 3 4] 
     1089            [5 3 1 -1 -3] 
     1090            ] 
     1091 
     1092            sage: U = ntl.mat_ZZ(2,2) # note that the dimension doesn't matter 
     1093            sage: r = a.G_BKZ_RR(U=U); U 
     1094            [ 
     1095            [0 1 0 0 0] 
     1096            [1 0 0 0 0] 
     1097            [0 0 1 0 0] 
     1098            [0 0 0 1 0] 
     1099            [0 0 0 0 1] 
     1100            ] 
     1101        """ 
     1102        if U is None: 
     1103            _sig_on 
     1104            rank = mat_ZZ_G_BKZ_RR(self.x, float(delta), int(BlockSize), int(prune), 0, int(verbose)); 
     1105            _sig_off 
     1106            return rank 
     1107        elif PY_TYPE_CHECK(U, ntl_mat_ZZ): 
     1108            _sig_on 
     1109            rank = mat_ZZ_G_BKZ_RR_U(self.x, (<ntl_mat_ZZ>U).x, float(delta), int(BlockSize), int(prune), 0, int(verbose)); 
     1110            _sig_off 
     1111            return rank 
     1112        else: 
     1113            raise TypeError, "parameter U has wrong type." 
     1114 
    4441115    def LLL(self, a=3, b=4, return_U=False, verbose=False): 
    4451116        r""" 
    446         Performs LLL reduction of self (puts self in an LLL form). 
     1117        Performs LLL reduction of self (puts \code{self} in an LLL form). 
    4471118 
    448         self is an m x n matrix, viewed as m rows of n-vectors.  m may 
    449         be less than, equal to, or greater than n, and the rows need 
    450         not be linearly independent. self is transformed into an 
    451         LLL-reduced basis, and the return value is the rank r of self 
    452         so as det2 (see below).  The first m-r rows of self are zero. 
     1119        \code{self} is an $m x n$ matrix, viewed as $m$ rows of 
     1120        $n$-vectors.  $m$ may be less than, equal to, or greater than $n$, 
     1121        and the rows need not be linearly independent. self is 
     1122        transformed into an LLL-reduced basis, and the return value is 
     1123        the rank r of self so as det2 (see below).  The first $m-r$ rows 
     1124        of self are zero. 
    4531125 
    4541126        More specifically, elementary row transformations are 
    455         performed on self so that the non-zero rows of new-self form 
    456         an LLL-reduced basis for the lattice spanned by the rows of 
    457         old-self.  The default reduction parameter is $\delta=3/4$, 
    458         which means that the squared length of the first non-zero 
    459         basis vector is no more than $2^{r-1}$ times that of the 
    460         shortest vector in the lattice. 
     1127        performed on \code{self} so that the non-zero rows of 
     1128        new-\code{self} form an LLL-reduced basis for the lattice 
     1129        spanned by the rows of old-\code{self}.  The default reduction 
     1130        parameter is $\delta=3/4$, which means that the squared length 
     1131        of the first non-zero basis vector is no more than $2^{r-1}$ 
     1132        times that of the shortest vector in the lattice. 
    4611133 
    4621134        det2 is calculated as the \emph{square} of the determinant of 
    4631135        the lattice---note that sqrt(det2) is in general an integer 
     
    4651137 
    4661138        If return_U is True, a value U is returned which is the 
    4671139        transformation matrix, so that U is a unimodular m x m matrix 
    468         with U * old-self = new-self.  Note that the first m-r rows of 
    469         U form a basis (as a lattice) for the kernel of old-B. 
     1140        with U * old-\code{self} = new-\code{self}. Note that the 
     1141        first m-r rows of U form a basis (as a lattice) for the kernel 
     1142        of old-B. 
    4701143         
    4711144        The parameters a and b allow an arbitrary reduction parameter 
    4721145        $\delta=a/b$, where $1/4 < a/b \leq 1$, where a and b are positive 
     
    5181191            [0 -1 -7 5] 
    5191192            ] 
    5201193 
    521         WARNING: This method modifies self. So after applying this method your matrix 
    522         will be a vector of vectors. 
     1194        WARNING: This method modifies \code{self}. So after applying 
     1195        this method your matrix will be a vector of vectors. 
    5231196        """ 
    5241197        cdef ZZ_c *det2 
    5251198        cdef ntl_mat_ZZ U 
     
    5371210 
    5381211    def LLL_FP(self, delta=0.75 , return_U=False, verbose=False): 
    5391212        r""" 
    540         Performs approximate LLL reduction of self (puts self in an 
    541         LLL form) subject to the following conditions: 
     1213        Performs approximate LLL reduction of \code{self} (puts 
     1214        \code{self} in an LLL form) subject to the following 
     1215        conditions: 
    5421216 
    5431217        The precision is double. 
    5441218         
    5451219        The return value is the rank of B. 
    5461220 
    547         Classical Gramm-Schmidt Orthogonalization is used: 
     1221        Classical Gram-Schmidt Orthogonalization is used: 
    5481222 
    5491223        This choice uses classical methods for computing the 
    550         Gramm-Schmidt othogonalization.  It is fast but prone to 
     1224        Gram-Schmidt othogonalization.  It is fast but prone to 
    5511225        stability problems.  This strategy was first proposed by 
    5521226        Schnorr and Euchner [C. P. Schnorr and M. Euchner, 
    5531227        Proc. Fundamentals of Computation Theory, LNCS 529, pp. 68-85, 
     
    6041278            [0 -1 -7 5] 
    6051279            ] 
    6061280 
    607         WARNING: This method modifies self. So after applying this 
     1281        WARNING: This method modifies \code{self}. So after applying this 
    6081282        method your matrix will be a vector of vectors. 
    6091283        """ 
    6101284        cdef ntl_mat_ZZ U 
     
    6221296 
    6231297    def LLL_QP(self, delta, return_U=False, verbose=False): 
    6241298        r""" 
    625         Peforms the same reduction as self.LLL_FP using the same 
    626         calling conventions but with quad float precision. 
     1299        Peforms the same reduction as \code{self.LLL_FP} using the 
     1300        same calling conventions but with quad float precision. 
     1301 
     1302        EXAMPLE: 
     1303            sage: M=ntl.mat_ZZ(3,3,[1,2,3,4,5,6,7,8,9]) 
     1304            sage: M.LLL_QP(delta=0.75) 
     1305            2 
    6271306        """ 
    6281307        cdef ntl_mat_ZZ U 
    6291308        if return_U: 
     
    6401319 
    6411320    def LLL_XD(self, delta, return_U=False, verbose=False): 
    6421321        r""" 
    643         Peforms the same reduction as self.LLL_FP using the sam
    644         calling conventions but with extended exponent double 
     1322        Peforms the same reduction as \code{self.LLL_FP} using th
     1323        same calling conventions but with extended exponent double 
    6451324        precision. 
     1325 
     1326        EXAMPLE: 
     1327            sage: M=ntl.mat_ZZ(3,3,[1,2,3,4,5,6,7,8,9]) 
     1328            sage: M.LLL_XD(delta=0.75) 
     1329            2 
    6461330        """ 
    6471331        cdef ntl_mat_ZZ U 
    6481332        if return_U: 
     
    6581342            return rank 
    6591343 
    6601344    def LLL_RR(self, delta, return_U=False, verbose=False): 
    661         r""" 
    662         Peforms the same reduction as self.LLL_FP using the sam
    663         calling conventions but with arbitrary precision floating 
     1345        r"""  
     1346        Peforms the same reduction as \code{self.LLL_FP} using th
     1347        same calling conventions but with arbitrary precision floating 
    6641348        point numbers. 
     1349