Beispiel #1
0
def pollards_pminus1_wrapper(N):
    steps = Steps()
    primes_list = get_primes()
    unfactored, factors = [N], []

    while len(unfactored) > 0:
        n = unfactored.pop()
        if n in primes_list:
            factors.append(n)
        elif n == 1:
            continue
        else:
            steps.add_cuberoot(n)
            B = math.pow(n, 1.0 / 3.0)

            f1, f2, hsteps = pollards_pminus1(n, B)
            steps.append(hsteps)
            if f1 == None:  # p-1 wont work, trial division
                flist, tsteps, _ = trial_division(n)
                steps.append(tsteps)
                factors.extend(flist)
            else:
                unfactored.append(f1)
                unfactored.append(f2)

    return factors, steps, 0
Beispiel #2
0
def lehmans_var_of_fermat_prewrapper(N):
    steps = Steps()

    steps.add_cuberoot(N)
    s3n = int(math.ceil(N**(1.0 / 3.0)))

    factors, tsteps, num_unfactored = trial_division(N, s3n)
    steps.append(tsteps)

    unfactored = []
    if num_unfactored == 1:
        unfactored.append(factors[-1])
        factors = factors[:-1]
    return factors, steps, unfactored
Beispiel #3
0
def harts_one_line_prewrapper(N):
    steps = Steps()
    factors, unfactored = [], [N]

    isq, isq_steps = is_square(N)
    steps.append(isq_steps)
    if not isq:
        steps.add_cuberoot(N)
        when_to_stop = math.pow(N, 1.0 / 3.0)
        factors, tsteps, num_unfactored = trial_division(N, when_to_stop)
        steps.append(tsteps)
        unfactored = []
        if num_unfactored == 1:
            unfactored.append(factors[-1])
            factors = factors[:-1]

    return factors, steps, unfactored
Beispiel #4
0
def pollards_pminus1(N, B=None, steps=None):
    if steps == None:
        steps = Steps()
    if B == None:
        steps.add_cuberoot(N)
        B = math.pow(N, 1.0 / 3.0)

    primes_list = get_primes()
    a, i = 2, 0
    a_set = set([])
    while True:
        pi = primes_list[i]
        if pi > B:
            break

        steps.add_log(B)
        steps.add_log(pi)
        e = int(math.floor(math.log(B) / math.log(pi)))

        steps.add_exp(pi, e)
        f = pi**e

        steps.add_exp(a, f)
        steps.add_mod()
        a_hold = powering.power_mod(a, f, N)  #(a**f) % N

        if a_hold == 0:
            break
        a = a_hold
        a_set.add(a)
        i += 1

    for a in a_set:
        g, g_steps = gcd(a - 1, N)
        steps.append(g_steps)
        if 1 < g and g < N:
            return g, N / g, steps
    return None, None, steps