def plain_sieve(): """ Run a a plain sieve in the full dimension directly. """ description = plain_sieve.__doc__ args, all_params = parse_args(description,) stats = run_all(plain_sieve_kernel, list(all_params.values()), lower_bound=args.lower_bound, upper_bound=args.upper_bound, step_size=args.step_size, trials=args.trials, workers=args.workers, seed=args.seed) inverse_all_params = OrderedDict([(v, k) for (k, v) in six.iteritems(all_params)]) inverse_all_params = OrderedDict([(v, k) for (k, v) in six.iteritems(all_params)]) stats = sanitize_params_names(stats, inverse_all_params) fmt = "{name:50s} :: n: {n:2d}, cputime {cputime:7.4f}s, walltime: {walltime:7.4f}s, |db|: 2^{avg_max:.2f}" profiles = print_stats(fmt, stats, ("cputime", "walltime", "avg_max"), extractf={"avg_max": lambda n, params, stat: db_stats(stat)[0]}) output_profiles(args.profile, profiles) if args.pickle: pickler.dump(stats, open("plain-sieve-%d-%d-%d-%d.sobj" % (args.lower_bound, args.upper_bound, args.step_size, args.trials), "wb"))
def asvp(n, params, threads): lower_bound = n # lowest lattice dimension to consider (inclusive) upper_bound = 0 # upper bound on lattice dimension to consider (exclusive) step_size = 2 # increment lattice dimension in these steps trials = 1 # number of experiments to run per dimension workers = 1 # number of parallel experiments to run seed = int.from_bytes(os.urandom(8), 'big') # randomness seed all_params = OrderedDict({f"'threads': {threads}, ": params}) stats = run_all(asvp_kernel, list(all_params.values()), lower_bound=lower_bound, upper_bound=upper_bound, step_size=step_size, trials=trials, workers=workers, seed=seed) inverse_all_params = OrderedDict([(v, k) for (k, v) in all_params.items()]) stats = sanitize_params_names(stats, inverse_all_params) fmt = "{name:20s} :: n: {n:2d}, cputime {cputime:7.4f}s, walltime: {walltime:7.4f}s, "\ "flast: {flast:3.2f}, |db|: 2^{avg_max:.2f}" print_stats(fmt, stats, ("cputime", "walltime", "flast", "avg_max"), extractf={"avg_max": lambda n, params, stat: db_stats(stat)[0]}) res = list(stats.values())[0][0].data['res'] return res
def bkz_tour(): """ Run bkz tours. .. note :: that by default no information is printed. To enable set ``--dummy-tracer False`` and ``--verbose``. """ description = bkz_tour.__doc__ args, all_params = parse_args( description, bkz__alg="pump_and_jump", bkz__blocksizes="40:51:2", bkz__pre_blocksize=39, bkz__tours=1, bkz__extra_dim4free=0, bkz__jump=1, bkz__dim4free_fun="default_dim4free_fun", slide__overlap=1, pump__down_sieve=True, challenge_seed=0, dummy_tracer=False, # set to control memory verbose=False) stats = run_all(bkz_kernel, list(all_params.values()), lower_bound=args.lower_bound, upper_bound=args.upper_bound, step_size=args.step_size, trials=args.trials, workers=args.workers, seed=args.seed) inverse_all_params = OrderedDict([(v, k) for (k, v) in six.iteritems(all_params)]) stats = sanitize_params_names(stats, inverse_all_params) fmt = "{name:50s} :: n: {n:2d}, cputime {cputime:7.4f}s, walltime: {walltime:7.4f}s, slope: {slope:1.5f}, |db|: 2^{avg_max:.2f}" profiles = print_stats(fmt, stats, ("cputime", "walltime", "slope", "avg_max"), extractf={ "avg_max": lambda n, params, stat: db_stats(stat)[0] }) output_profiles(args.profile, profiles) if args.pickle: pickler.dump( stats, open( "bkz-%d-%d-%d-%d.sobj" % (args.lower_bound, args.upper_bound, args.step_size, args.trials), "wb"))
def asvp(): """ Run a Workout until 1.05-approx-SVP on matrices with dimensions in ``range(lower_bound, upper_bound, step_size)``. """ description = asvp.__doc__ args, all_params = parse_args( description, load_matrix=None, verbose=True, challenge_seed=0, workout__dim4free_dec=3, ) stats = run_all( asvp_kernel, list(all_params.values()), lower_bound=args.lower_bound, upper_bound=args.upper_bound, step_size=args.step_size, trials=args.trials, workers=args.workers, seed=args.seed, ) inverse_all_params = OrderedDict([(v, k) for (k, v) in six.iteritems(all_params)]) stats = sanitize_params_names(stats, inverse_all_params) fmt = "{name:50s} :: n: {n:2d}, cputime {cputime:7.4f}s, walltime: {walltime:7.4f}s, flast: {flast:3.2f}, |db|: 2^{avg_max:.2f}" profiles = print_stats( fmt, stats, ("cputime", "walltime", "flast", "avg_max"), extractf={ "avg_max": lambda n, params, stat: db_stats(stat)[0] }, ) output_profiles(args.profile, profiles) if args.pickle: pickler.dump( stats, open( "svp-challenge-%d-%d-%d-%d.sobj" % (args.lower_bound, args.upper_bound, args.step_size, args.trials), "wb", ), )
def svp(): """ Run a progressive until exact-SVP is solved. The exact-SVP length must have been priorly determined using ./svp_exact_find_norm.py """ description = svp.__doc__ args, all_params = parse_args(description, challenge_seed=0, svp__alg="workout") stats = run_all( svp_kernel, list(all_params.values()), lower_bound=args.lower_bound, upper_bound=args.upper_bound, step_size=args.step_size, trials=args.trials, workers=args.workers, seed=args.seed, ) inverse_all_params = OrderedDict([(v, k) for (k, v) in six.iteritems(all_params)]) stats = sanitize_params_names(stats, inverse_all_params) fmt = "{name:50s} :: n: {n:2d}, cputime {cputime:7.4f}s, walltime: {walltime:7.4f}s, flast: {flast:3.2f}, |db|: 2^{avg_max:.2f}" profiles = print_stats( fmt, stats, ("cputime", "walltime", "flast", "avg_max"), extractf={ "avg_max": lambda n, params, stat: db_stats(stat)[0] }, ) output_profiles(args.profile, profiles) if args.pickle: pickler.dump( stats, open( "svp-exact-%d-%d-%d-%d.sobj" % (args.lower_bound, args.upper_bound, args.step_size, args.trials), "wb", ), )
def full_sieve(): """ Run a a full sieve (with some partial sieve as precomputation). """ description = full_sieve.__doc__ args, all_params = parse_args(description, challenge_seed=0) stats = run_all( full_sieve_kernel, list(all_params.values()), lower_bound=args.lower_bound, upper_bound=args.upper_bound, step_size=args.step_size, trials=args.trials, workers=args.workers, seed=args.seed, ) inverse_all_params = OrderedDict([(v, k) for (k, v) in six.iteritems(all_params)]) stats = sanitize_params_names(stats, inverse_all_params) fmt = "{name:50s} :: n: {n:2d}, cputime {cputime:7.4f}s, walltime: {walltime:7.4f}s, |db|: 2^{avg_max:.2f}" profiles = print_stats( fmt, stats, ("cputime", "walltime", "avg_max"), extractf={ "avg_max": lambda n, params, stat: db_stats(stat)[0] }, ) output_profiles(args.profile, profiles) if args.pickle: pickler.dump( stats, open( "full-sieve-%d-%d-%d-%d.sobj" % (args.lower_bound, args.upper_bound, args.step_size, args.trials), "wb", ), )
def hkz(): """ Attempt HKZ reduction. """ description = hkz.__doc__ args, all_params = parse_args(description, challenge_seed=0, pump__down_sieve=True, pump__down_stop=9999, saturation_ratio=.8, pump__prefer_left_insert=10, workout__dim4free_min=0, workout__dim4free_dec=15) stats = run_all(hkz_kernel, list(all_params.values()), lower_bound=args.lower_bound, upper_bound=args.upper_bound, step_size=args.step_size, trials=args.trials, workers=args.workers, seed=args.seed) inverse_all_params = OrderedDict([(v, k) for (k, v) in six.iteritems(all_params)]) stats = sanitize_params_names(stats, inverse_all_params) fmt = "{name:50s} :: n: {n:2d}, cputime {cputime:7.4f}s, walltime: {walltime:7.4f}s, |db|: 2^{avg_max:.2f}" profiles = print_stats(fmt, stats, ("cputime", "walltime", "avg_max"), extractf={ "avg_max": lambda n, params, stat: db_stats(stat)[0] }) output_profiles(args.profile, profiles) if args.pickle: pickler.dump( stats, open( "hkz-%d-%d-%d-%d.sobj" % (args.lower_bound, args.upper_bound, args.step_size, args.trials), "wb"))
def solve_svp(A, goal_r0=None, threads=4, **kwds): """ A G6K Exact-SVP Solver :param A: basis matrix (repr. in list) :param goal_r0: ... Quit when this is reached. If it's None, use the result of `find_norm` with `n_matches` = 3 !!! may take long time running `find_norm` !!! :param threads: ... (default: 4) :param keep_tmpfile: keep the reduced matrix (default: False) :param load_matrix: filename for temp matrix file :param verbose: ... (default: True) :param alg: algorithm used to solve svp, choosen in 'enum', 'duc18', 'workout'(default) :param debug: ... (default: False) """ n = len(A) keep_tmpfile = kwds.get('keep_tmpfile', False) load_matrix = kwds.get('load_matrix', f'svpchallenge-{n}.txt') verbose = kwds.get('verbose', True) alg = kwds.get('alg', 'workout') debug = kwds.get('debug', False) lower_bound = n # lowest lattice dimension to consider (inclusive) upper_bound = 0 # upper bound on lattice dimension to consider (exclusive) step_size = 2 # increment lattice dimension in these steps trials = 1 # number of experiments to run per dimension workers = 1 # number of parallel experiments to run seed = int.from_bytes(os.urandom(8), 'big') # randomness seed with open(load_matrix, 'w') as f: f.write(str_mat(A)) if goal_r0 is None: goal_r0 = find_norm(load_matrix, n, verbose=verbose, n_matches=3) if verbose: print(f"goal_r0 = {goal_r0}") params = SieverParams(load_matrix=load_matrix, threads=threads, goal_r0=goal_r0, verbose=verbose) params['svp/alg'] = alg all_params = OrderedDict({f"'threads': {threads}, ": params}) stats = run_all(svp_kernel, list(all_params.values()), lower_bound=lower_bound, upper_bound=upper_bound, step_size=step_size, trials=trials, workers=workers, seed=seed) inverse_all_params = OrderedDict([(v, k) for (k, v) in all_params.items()]) stats = sanitize_params_names(stats, inverse_all_params) fmt = "{name:20s} :: n: {n:2d}, cputime {cputime:7.4f}s, walltime: {walltime:7.4f}s, "\ "flast: {flast:3.2f}, |db|: 2^{avg_max:.2f}" print_stats(fmt, stats, ("cputime", "walltime", "flast", "avg_max"), extractf={ "avg_max": lambda n, params, stat: db_stats(stat)[0] }) res = list(stats.values())[0][0].data['res'] if debug: __import__('IPython').embed() if keep_tmpfile: with open(load_matrix, 'w') as f: f.write(str(res)) else: os.system(f'rm -f {load_matrix}') res = [[res[i, j] for j in range(res.ncols)] for i in range(res.nrows)] return res