Skip to content

Commit

Permalink
Merge pull request #103 from fplll/extenum
Browse files Browse the repository at this point in the history
Support for external enumeration libraries using ctypes
  • Loading branch information
malb committed Nov 11, 2017
2 parents 7c343f3 + e1e9490 commit e15afa2
Show file tree
Hide file tree
Showing 15 changed files with 153 additions and 63 deletions.
2 changes: 1 addition & 1 deletion src/fpylll/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@
from .fplll.pruner import Pruning
from .fplll.sieve_gauss import GaussSieve
from .util import ReductionError
from .util import set_random_seed, set_precision, get_precision
from .util import FPLLL
__version__ = "0.3.0dev"
2 changes: 1 addition & 1 deletion src/fpylll/fplll/enumeration.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,7 @@ cdef class Enumeration:
Return sub-solutions computed in last enumeration call.
>>> from fpylll import *
>>> set_random_seed(1337)
>>> FPLLL.set_random_seed(1337)
>>> A = IntegerMatrix.random(80, "qary", bits=30, k=40)
>>> _ = LLL.reduction(A)
>>> M = GSO.Mat(A)
Expand Down
17 changes: 17 additions & 0 deletions src/fpylll/fplll/fplll.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ from libcpp.vector cimport vector
from libcpp.string cimport string
from libcpp.pair cimport pair
from libcpp cimport bool
from libcpp.functional cimport function


cdef extern from "<map>" namespace "std":
Expand Down Expand Up @@ -582,6 +583,22 @@ cdef extern from "fplll/enum/enumerate.h" namespace "fplll":

long get_nodes()

cdef extern from "fplll/enum/enumerate_ext.h" namespace "fplll":

ctypedef void extenum_cb_set_config (double *mu, size_t mudim, bool mutranspose, double *rdiag,
double *pruning)

ctypedef double extenum_cb_process_sol(double dist, double *sol);

ctypedef void extenum_cb_process_subsol(double dist, double *subsol, int offset);

ctypedef unsigned long extenum_fc_enumerate(int dim, enumf maxdist,
function[extenum_cb_set_config] cbfunc,
function[extenum_cb_process_sol] cbsol,
function[extenum_cb_process_subsol] cbsubsol,
bool dual, bool findsubsols)

void set_external_enumerator(function[extenum_fc_enumerate] extenum)


# SVP
Expand Down
18 changes: 9 additions & 9 deletions src/fpylll/fplll/gso.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -265,12 +265,12 @@ cdef class MatGSO:
@property
def float_type(self):
"""
>>> from fpylll import IntegerMatrix, GSO, set_precision
>>> from fpylll import IntegerMatrix, GSO, FPLLL
>>> A = IntegerMatrix(10, 10)
>>> M = GSO.Mat(A)
>>> M.float_type
'double'
>>> set_precision(100)
>>> FPLLL.set_precision(100)
53
>>> M = GSO.Mat(A, float_type='mpfr')
>>> M.float_type
Expand Down Expand Up @@ -321,7 +321,7 @@ cdef class MatGSO:
"""
Number of rows of ``B`` (dimension of the lattice).
>>> from fpylll import IntegerMatrix, GSO, set_precision
>>> from fpylll import IntegerMatrix, GSO, FPLLL
>>> A = IntegerMatrix(11, 11)
>>> M = GSO.Mat(A)
>>> M.d
Expand Down Expand Up @@ -365,7 +365,7 @@ cdef class MatGSO:
"""
Exact computation of dot products.
>>> from fpylll import IntegerMatrix, GSO, set_precision
>>> from fpylll import IntegerMatrix, GSO, FPLLL
>>> A = IntegerMatrix(11, 11)
>>> M = GSO.Mat(A)
>>> M.int_gram_enabled
Expand Down Expand Up @@ -413,7 +413,7 @@ cdef class MatGSO:
"""
Normalization of each row of b by a power of 2.
>>> from fpylll import IntegerMatrix, GSO, set_precision
>>> from fpylll import IntegerMatrix, GSO, FPLLL
>>> A = IntegerMatrix(11, 11)
>>> M = GSO.Mat(A)
>>> M.row_expo_enabled
Expand Down Expand Up @@ -461,7 +461,7 @@ cdef class MatGSO:
"""
Computation of the transform matrix.
>>> from fpylll import IntegerMatrix, GSO, set_precision
>>> from fpylll import IntegerMatrix, GSO, FPLLL
>>> A = IntegerMatrix(11, 11)
>>> M = GSO.Mat(A)
>>> M.transform_enabled
Expand Down Expand Up @@ -511,7 +511,7 @@ cdef class MatGSO:
"""
Computation of the inverse transform matrix (transposed).
>>> from fpylll import IntegerMatrix, GSO, set_precision
>>> from fpylll import IntegerMatrix, GSO, FPLLL
>>> A = IntegerMatrix(11, 11)
>>> M = GSO.Mat(A)
>>> M.inverse_transform_enabled
Expand Down Expand Up @@ -561,7 +561,7 @@ cdef class MatGSO:
"""
Changes the behaviour of ``row_addmul``, see its documentation.
>>> from fpylll import IntegerMatrix, GSO, set_precision
>>> from fpylll import IntegerMatrix, GSO, FPLLL
>>> A = IntegerMatrix(11, 11)
>>> M = GSO.Mat(A)
>>> M.row_op_force_long
Expand Down Expand Up @@ -1194,7 +1194,7 @@ cdef class MatGSO:
Example::
>>> from fpylll import *
>>> set_random_seed(42)
>>> FPLLL.set_random_seed(42)
>>> A = IntegerMatrix(6, 6)
>>> A.randomize("ntrulike", bits=6, q=31)
>>> print(A)
Expand Down
14 changes: 7 additions & 7 deletions src/fpylll/fplll/integer_matrix.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -457,8 +457,8 @@ cdef class IntegerMatrix:
Examples::
>>> from fpylll import set_random_seed
>>> set_random_seed(1337)
>>> from fpylll import FPLLL
>>> FPLLL.set_random_seed(1337)
>>> print(IntegerMatrix.random(10, "intrel", bits=100))
[ 256463166861109549799341521970 1 0 0 0 0 0 0 0 0 0 ]
Expand Down Expand Up @@ -702,7 +702,7 @@ cdef class IntegerMatrix:
Example::
>>> from fpylll import set_random_seed
>>> from fpylll import FPLLL
>>> z = [[0 for _ in range(10)] for _ in range(10)]
>>> A = IntegerMatrix.random(10, "qary", q=127, k=5)
>>> _ = A.to_matrix(z)
Expand Down Expand Up @@ -1343,8 +1343,8 @@ cdef class IntegerMatrix:
:param IntegerMatrix B: n × k integer matrix B
:returns: m × k integer matrix C = A × B
>>> from fpylll import set_random_seed
>>> set_random_seed(1337)
>>> from fpylll import FPLLL
>>> FPLLL.set_random_seed(1337)
>>> A = IntegerMatrix(2, 2)
>>> A.randomize("uniform", bits=2)
>>> print(A)
Expand Down Expand Up @@ -1526,9 +1526,9 @@ cdef class IntegerMatrix:
We illustrate the calling conventions of this function using a 10 x 10 matrix::
>>> from fpylll import IntegerMatrix, set_random_seed
>>> from fpylll import IntegerMatrix, FPLLL
>>> A = IntegerMatrix(10, 10)
>>> set_random_seed(1337)
>>> FPLLL.set_random_seed(1337)
>>> A.randomize("ntrulike", bits=22, q=4194319)
>>> print(A)
[ 1 0 0 0 0 3021421 752690 1522220 2972677 119630 ]
Expand Down
32 changes: 16 additions & 16 deletions src/fpylll/fplll/pruner.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Pruner
.. moduleauthor:: Martin R. Albrecht <martinralbrecht+fpylll@googlemail.com>
>>> from fpylll import *
>>> set_random_seed(1337)
>>> FPLLL.set_random_seed(1337)
>>> A = [IntegerMatrix.random(10, "qary", bits=10, k=5) for _ in range(20)]
>>> M = [GSO.Mat(a) for a in A]
>>> _ = [LLL.Reduction(m)() for m in M]
Expand Down Expand Up @@ -41,7 +41,7 @@ from fplll cimport svp_probability as svp_probability_c
from fplll cimport PRUNER_CVP, PRUNER_START_FROM_INPUT, PRUNER_GRADIENT, PRUNER_NELDER_MEAD, PRUNER_VERBOSE


from fpylll.util import adjust_radius_to_gh_bound, precision, get_precision
from fpylll.util import adjust_radius_to_gh_bound, precision, FPLLL
from fpylll.util cimport check_float_type, check_precision, check_pruner_metric

IF HAVE_LONG_DOUBLE:
Expand Down Expand Up @@ -238,8 +238,8 @@ cdef class Pruner:
:param flags: flags
:param float_type: floating point type to use
>>> from fpylll import IntegerMatrix, GSO, LLL, Pruning, set_random_seed
>>> set_random_seed(1337)
>>> from fpylll import IntegerMatrix, GSO, LLL, Pruning, FPLLL
>>> FPLLL.set_random_seed(1337)
>>> A = IntegerMatrix.random(40, "qary", bits=20, k=20)
>>> _ = LLL.reduction(A)
>>> M = GSO.Mat(A)
Expand Down Expand Up @@ -349,8 +349,8 @@ cdef class Pruner:
"""
Optimize pruning coefficients.
>>> from fpylll import IntegerMatrix, GSO, LLL, Pruning, set_random_seed
>>> set_random_seed(1337)
>>> from fpylll import IntegerMatrix, GSO, LLL, Pruning, FPLLL
>>> FPLLL.set_random_seed(1337)
>>> A = IntegerMatrix.random(40, "qary", bits=20, k=20)
>>> _ = LLL.reduction(A)
>>> M = GSO.Mat(A)
Expand Down Expand Up @@ -416,8 +416,8 @@ cdef class Pruner:
"""
Compute the cost of a single enumeration
>>> from fpylll import IntegerMatrix, GSO, LLL, Pruning, set_random_seed
>>> set_random_seed(1337)
>>> from fpylll import IntegerMatrix, GSO, LLL, Pruning, FPLLL
>>> FPLLL.set_random_seed(1337)
>>> A = IntegerMatrix.random(40, "qary", bits=20, k=20)
>>> _ = LLL.reduction(A)
>>> M = GSO.Mat(A)
Expand Down Expand Up @@ -492,8 +492,8 @@ cdef class Pruner:
Compute the cost of r enumeration and (r-1) preprocessing, where r is the required number of
retrials to reach target
>>> from fpylll import IntegerMatrix, GSO, LLL, Pruning, set_random_seed
>>> set_random_seed(1337)
>>> from fpylll import IntegerMatrix, GSO, LLL, Pruning, FPLLL
>>> FPLLL.set_random_seed(1337)
>>> A = IntegerMatrix.random(40, "qary", bits=20, k=20)
>>> _ = LLL.reduction(A)
>>> M = GSO.Mat(A)
Expand Down Expand Up @@ -554,8 +554,8 @@ cdef class Pruner:
"""
Compute the success probability of expected number of solutions of a single enumeration.
>>> from fpylll import IntegerMatrix, GSO, LLL, Pruning, set_random_seed
>>> set_random_seed(1337)
>>> from fpylll import IntegerMatrix, GSO, LLL, Pruning, FPLLL
>>> FPLLL.set_random_seed(1337)
>>> A = IntegerMatrix.random(40, "qary", bits=20, k=20)
>>> _ = LLL.reduction(A)
>>> M = GSO.Mat(A)
Expand Down Expand Up @@ -623,15 +623,15 @@ def prune(double enumeration_radius, double preproc_cost, gso_r, double target,
:param pruning: write output here, pass ``None`` for creating a new one
:param float_type: floating point type to use
>>> from fpylll import IntegerMatrix, LLL, GSO, get_precision, set_precision
>>> from fpylll import IntegerMatrix, LLL, GSO, FPLLL
>>> from fpylll.numpy import dump_r
>>> from fpylll import set_random_seed
>>> from fpylll import FPLLL
>>> from fpylll import Pruning
>>> set_random_seed(1337)
>>> FPLLL.set_random_seed(1337)
>>> A = IntegerMatrix.random(20, "qary", bits=20, k=10)
>>> M = GSO.Mat(A)
>>> LLL.Reduction(M)()
>>> _ = set_precision(53)
>>> _ = FPLLL.set_precision(53)
>>> R = [M.get_r(i,i) for i in range(0, 20)]
>>> pr0 = Pruning.run(R[0], 2**20, [R], 0.5, float_type="double")
>>> pr1 = Pruning.run(R[0], 2**20, [R], 0.5, float_type="long double")
Expand Down
4 changes: 2 additions & 2 deletions src/fpylll/fplll/sieve_gauss.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ ACM-SIAM.
Example::
>>> from fpylll import IntegerMatrix, GaussSieve, SVP, LLL, set_random_seed
>>> set_random_seed(1337)
>>> from fpylll import IntegerMatrix, GaussSieve, SVP, LLL, FPLLL
>>> FPLLL.set_random_seed(1337)
>>> A = IntegerMatrix.random(20, "qary", k=10, q=127); A = LLL.reduction(A)
>>> w = SVP.shortest_vector(A)
>>> v = GaussSieve(A, algorithm=2)()
Expand Down
2 changes: 1 addition & 1 deletion src/fpylll/fplll/svpcvp.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ def closest_vector(IntegerMatrix B, target, int flags=CVP_DEFAULT):
:rtype tuple:
>>> from fpylll import *
>>> set_random_seed(42)
>>> FPLLL.set_random_seed(42)
>>> A = IntegerMatrix.random(5, 'uniform', bits=10)
>>> lll = LLL.reduction(A)
>>> t = (94, -42, 123, 512, -1337)
Expand Down
4 changes: 2 additions & 2 deletions src/fpylll/tools/quality.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ def basis_quality(M):
:example:
>>> from fpylll import IntegerMatrix, GSO, LLL, set_random_seed
>>> set_random_seed(1337)
>>> from fpylll import IntegerMatrix, GSO, LLL, FPLLL
>>> FPLLL.set_random_seed(1337)
>>> A = IntegerMatrix.random(100, "qary", bits=30, k=50)
>>> _ = LLL.reduction(A)
>>> M = GSO.Mat(A); _ = M.update_gso()
Expand Down

0 comments on commit e15afa2

Please sign in to comment.