コード例 #1
0
def load_matrix_file(filepath, randomize=False, seed=None):
    """
    Load matrix from file, LLL reduce (and randomize).

    :param filepath: Load matrix from this file
    :param randomize: Randomize the basis
    :param seed: Seed for randomization
    :returns: lattice basis and BKZ object

    """
    A = IntegerMatrix.from_file(filepath)
    A = LLL.reduction(A)
    A = IntegerMatrix.from_matrix(A, int_type="long")

    M = GSO.Mat(A, float_type="double", flags=GSO.ROW_EXPO)
    bkz = BKZReduction(M)

    if seed is not None:
        FPLLL.set_random_seed(seed)

    if randomize:
        bkz.randomize_block(0, A.nrows, density=A.ncols / 4)
        LLL.reduction(A)
        bkz = BKZReduction(A)

    LLL.reduction(A)
    bkz.lll_obj()  # to initialize bkz.M etc

    return A, bkz
コード例 #2
0
ファイル: chal.py プロジェクト: fasterBKZ/simulation
def load_svp_challenge(d, seed=0):
    """
    Load SVP Challenge matrix in dimension `d` and ``seed``

    :param d: dimension
    :param seed: random seed

    """
    filename = os.path.join("data", "svp-challenge", "%03d-%d.txt" % (d, seed))

    if os.path.isfile(filename) is False:
        import requests

        logging.debug("Did not find '{filename}', downloading ...".format(
            filename=filename))
        r = requests.post(
            "https://www.latticechallenge.org/svp-challenge/generator.php",
            data={
                "dimension": d,
                "seed": seed,
                "sent": "True"
            },
        )
        logging.debug("%s %s" % (r.status_code, r.reason))
        fn = open(filename, "w")
        fn.write(r.text)
        fn.close()

    A = IntegerMatrix.from_file(filename)
    LLL.reduction(A)
    return A
コード例 #3
0
def main_cleanbkz_mpi_master(filename, bs, cores):

    try:
        with open(filename, "rb") as f:
            mat = pickle.load(f)
            #print "len(mat)", len(mat)
            #if (len(mat) > 1):
            #   mat = mat[0]
        if isinstance(mat, IntegerMatrix):
            Ainput = mat
        else:
            Ainput = IntegerMatrix.from_matrix(mat)
    except:
        Ainput = IntegerMatrix.from_file(filename)

    Ainput_M = GSO.Mat(Ainput, float_type='double')
    Ainput_M.update_gso()
    r = [Ainput_M.get_r(i, i) for i in range(0, Ainput.nrows)]
    L_Ainput_M = LLL.Reduction(Ainput_M)
    L_Ainput_M()

    print r

    A = IntegerMatrix.from_matrix(L_Ainput_M.M.B, int_type="long")

    cleanbkz_mpi = CLEANBKZ_MPI(A, cores)
    #cleanbkz_mpi = BKZ2(A)
    cleanbkz_mpi.lll_obj()
    cleanbkz_mpi.M.update_gso()
    r = [
        log(cleanbkz_mpi.M.get_r(i, i)) for i in range(0, cleanbkz_mpi.A.nrows)
    ]
    print "# starting r "
    print r

    params = BKZ.Param(
        block_size=bs,
        max_loops=5000,
        min_success_probability=.01,
        flags=BKZ.BOUNDED_LLL,  #|BKZ.DUMP_GSO,
        dump_gso_filename="gso_output.file",
        strategies="default.json")
    cleanbkz_mpi(params=params, min_row=0)
    #print "# done. found sv", cleanbkz_mpi.M.B[0]

    # done send last end signal
    for i in range(1, size):
        comm.send(1, i, tag=999)  # *** changed to send

    cleanbkz_mpi.M.update_gso()
    r2 = [
        log(cleanbkz_mpi.M.get_r(i, i)) for i in range(0, cleanbkz_mpi.A.nrows)
    ]
    print cleanbkz_mpi.A[0]
    print "# ending r"
    print r2

    return
コード例 #4
0
def main_pruning(filename, bs, cores):

    try:
        with open(filename, "rb") as f:
            mat = pickle.load(f)
            #print "len(mat)", len(mat)
            #if (len(mat) > 1):
            #   mat = mat[0]
        if isinstance(mat, IntegerMatrix):
            Ainput = mat
        else:
            Ainput = IntegerMatrix.from_matrix(mat)
    except:
        Ainput = IntegerMatrix.from_file(filename)

    Ainput_M = GSO.Mat(Ainput, float_type='double')
    Ainput_M.update_gso()
    r = [Ainput_M.get_r(i, i) for i in range(0, Ainput.nrows)]
    L_Ainput_M = LLL.Reduction(Ainput_M)
    L_Ainput_M()
    #print r

    A = IntegerMatrix.from_matrix(L_Ainput_M.M.B, int_type="long")
    M = GSO.Mat(A, float_type="double")
    bkzobj = BKZ2(M)
    bkzobj.M.update_gso()
    block_size = bs
    r = [M.get_r(i, i) for i in range(0, block_size)]
    radius = r[0] * 0.99
    preproc_cost = 5000**(rank + 1)

    pr0 = Pruning.run(radius,
                      NPS[block_size] * preproc_cost, [r],
                      0.1,
                      metric="probability",
                      float_type="double",
                      flags=Pruning.GRADIENT | Pruning.NELDER_MEAD)

    print pr0.coefficients
    """
    pruning = prune(radius, NPS[block_size] * preproc_cost, [r], 0.01,
                        metric="probability", float_type="double",
                        flags=Pruning.GRADIENT|Pruning.NELDER_MEAD)
    cost = sum(pruning.detailed_cost) / NPS[block_size]
    print "# [rank %d] cost %.1f, precost %.1f " % (rank, cost, preproc_cost)
    """

    pr0_linear = pr0.LinearPruningParams(block_size, block_size - 2)
    print pr0_linear.coefficients

    return
コード例 #5
0
def main_pbkz_mpi_master (filename, bs, cores):

    try:
        with open(filename, "rb") as f:
            mat, succe = pickle.load(f)
        #Ainput = IntegerMatrix.from_matrix(mat)
        Ainput = mat
        print " success here"
    except:
        print " failed here"        
        Ainput = IntegerMatrix.from_file(filename)

    print Ainput
    
    return
コード例 #6
0
def load_challenge_and_randomize(n):
    A_pre = IntegerMatrix.from_file("svpchallenge/svpchallengedim%dseed0.txt" %
                                    n)
    LLL.reduction(A_pre)
    A = IntegerMatrix.from_matrix(A_pre, int_type="long")
    params = fplll_bkz.Param(block_size=n,
                             max_loops=1,
                             strategies=fplll_bkz.DEFAULT_STRATEGY,
                             flags=fplll_bkz.GH_BND,
                             min_success_probability=.8)
    bkz = BKZReduction(A)
    bkz.lll_obj()
    bkz.randomize_block(0, n, density=n / 4)
    bkz.lll_obj()
    return A, bkz
コード例 #7
0
def svpchallenge_test ():
    dim = 60
    A_pre = IntegerMatrix.from_file("svpchallenge/svpchallengedim%dseed0.txt"%dim)
    print "# input dim: ", dim
    print "# nrows: ", A_pre.nrows
    ASVP_START = time()
    LLL.reduction(A_pre)
    A = IntegerMatrix.from_matrix(A_pre, int_type="long")
    bkz = BKZReduction(A)
    bkz.lll_obj()
    r = [bkz.M.get_r(i, i) for i in range(dim)]
    goal = (1.05)**2 * gaussian_heuristic(r)
    params = fplll_bkz.Param(block_size=20, max_loops=1,
                                 min_success_probability=.01)
    bkz(params=params)
    print " done BKZ yes"
コード例 #8
0
def svpchallenge_par3(bs_diff=10, cores=2, start_dim=80, end_dim=80+2, BS_RANDOM_RANGE = 10):
    for dim in range(start_dim, start_dim+2, 2):
        A_pre = IntegerMatrix.from_file("svpchallenge/svpchallengedim%dseed0.txt"%dim)
        print "# input dim: ", dim
        print "# nrows: ", A_pre.nrows
        ASVP_START = time()
        LLL.reduction(A_pre)
        A = IntegerMatrix.from_matrix(A_pre, int_type="long")
        bkz = BKZReduction(A)
        bkz.lll_obj()
        r = [bkz.M.get_r(i, i) for i in range(dim)]
        goal = (1.05)**2 * gaussian_heuristic(r)
        bs_ulim = dim - bs_diff
        interacting_parrallel_asvp(A, bs_ulim, goal, cores, BS_RANDOM_RANGE)
        ASVP_TIME = time() - ASVP_START

        print ("\nSUMMARY", {"input dim": dim, "bs_range": (bs_ulim - BS_RANDOM_RANGE, bs_ulim), "time": ASVP_TIME})
コード例 #9
0
def mpi_svpchallenge_par3(bs_diff=10,
                          cores=2,
                          start_dim=80,
                          end_dim=80 + 2,
                          BS_RANDOM_RANGE=10):
    dim = start_dim
    A_pre = IntegerMatrix.from_file(
        "/home/shi/suite/sb_fpylll/bench/svpchallenge/svpchallengedim%dseed0.txt"
        % dim)
    if (rank == 0):
        print "# input dim: ", dim
        print "# nrows: ", A_pre.nrows
    ASVP_START = time()
    LLL.reduction(A_pre)
    A = IntegerMatrix.from_matrix(A_pre, int_type="long")
    bkz = BKZReduction(A)
    bkz.lll_obj()
    r = [bkz.M.get_r(i, i) for i in range(dim)]
    goal = (1.05)**2 * gaussian_heuristic(r)
    bs_ulim = dim - bs_diff
    mpi_interacting_parrallel_asvp(A, bs_ulim, goal, cores, BS_RANDOM_RANGE)
    ASVP_TIME = time() - ASVP_START

    # done send signal
    comm.send(99, dest=0, tag=0)
    comm.send(rank, dest=0, tag=1)

    if (rank == 0):
        print(
            "\nSUMMARY", {
                "input dim": dim,
                "bs_range": (bs_ulim - BS_RANDOM_RANGE, bs_ulim),
                "time": ASVP_TIME
            })

    return
コード例 #10
0
        print_basis_stats(bkz.M, n)

        enum_trial(bkz, BKZ_TIME, gh_factor=gh_factor)
        print
        r = [bkz.M.get_r(i, i) for i in range(n)]
        trials += 1

    print "Finished !"
    print_basis_stats(bkz.M, n)
    return trials


START = time()
for dim in range(start_dim, 130, 2):
    for r in range(repeat):
        A_pre = IntegerMatrix.from_file(
            "svpchallenge/svpchallengedim%dseed0.txt" % dim)
        print "---------------------", A_pre.nrows
        ASVP_START = time()
        LLL.reduction(A_pre)

        # bs = int(floor(dim * bs_rat))

        A = IntegerMatrix.from_matrix(A_pre, int_type="long")

        trials = asvp(A, bs, gh_factor=(1.05**2))
        ASVP_TIME = time() - ASVP_START

        print "\n\n Challenge %d Solved" % dim
        print A[0]
        print "SUMMARY", {
            "dim": dim,