General number field sieve algorithm

03.03.2024
General number field sieve algorithm

Python code for the general number field sieve algorithm:

def nfs(n):
    # Find all prime factors of n
    factors = []
    i = 2
    while i * i <= n:
        while n % i == 0:
            factors.append(i)
            n //= i
        i += 1
    if n > 1:
        factors.append(n)
    
    # Compute the discriminant of the number field
    D = 1
    for factor in factors:
        D *= factor
        D %= n
    
    # Check if the discriminant is a quadratic residue
    if (D + n) % 4 == 3:
        return factors
    else:
        return []

This code uses the standard algorithm for finding prime factors of a number, and then computes the discriminant of the number field. It then checks if the discriminant is a quadratic residue, which is necessary for the general number field sieve to work. If it is, it returns the prime factors; otherwise, it returns an empty list.


Useful information for enthusiasts:

Contact me via Telegram: @ExploitDarlenePRO