**prime numbers**have been my

*obsession*since my childhood and they are source of continue interest for me. Actually thanks to cryptography they are a relevant part of my everyday life.

One of the most important problem in cryptography since the discovery of RSA is

**factoring**.
The factoring problem consists of finding the prime numbers p and q given a large number , N = p x q.

Unless you are still convinced that factoring is an easy peasy problem, you should know that, while probably not NP-complete, factoring is indeed reaaally hard.

The faster known method for factoring is currently NFS (Number Field Sieve) and if you are interested in the topic I suggest you to read this beautiful article from the great Carl Pomerance titled "A Tale of Two Sieves" . But it is not what I wanted to talk about today, mainly because the complete algorithm and all its shades go well beyond my current knowledge.

Today instead I want to talk you about one of the IMHO too often underappreciated (but crucial) steps of NFS that goes under the name of

Unless you are still convinced that factoring is an easy peasy problem, you should know that, while probably not NP-complete, factoring is indeed reaaally hard.

The faster known method for factoring is currently NFS (Number Field Sieve) and if you are interested in the topic I suggest you to read this beautiful article from the great Carl Pomerance titled "A Tale of Two Sieves" . But it is not what I wanted to talk about today, mainly because the complete algorithm and all its shades go well beyond my current knowledge.

Today instead I want to talk you about one of the IMHO too often underappreciated (but crucial) steps of NFS that goes under the name of

**filtering**. I became more familiar about this topic while reading Topics in Computational Number Theory Inspired by Peter L. Montgomery where I discovered that the legendary Peter L. Montgomery played a big role in the concrete development of these techniques.++ (thanks @XorNinja for the link:)) pic.twitter.com/Y34locZQyB— Antonio Sanso (@asanso) February 23, 2018

One of the most intriguing part of

**filtering**is that at the first sight these methods look trivial (because well, they are) but my main point, that drove me to write this blog post, is that they are probably one of the most important part of the all NFS algorithm.
Giving you an example, look at "The filtering step of discrete logarithm and integer factorization algorithms" by Cyril Bouvier:

The current NFS record factorization is RSA-768, an integer of 768 bits (232 digits). At the beginning of the filtering step, the matrix had about 47 billion rows and 35 billion columns. After the first part of the filtering step, the matrix had about 2.5 billion rows and 1.7 billions columns. At the end of the filtering step, the matrix used in the linear algebra had about 193 million rows and columns.But let's go in order...So the filtering step reduced the size of the matrix by more than 99%!!!

## ..What is this filtering about?

All the modern factorization procedures consist basically of 3 steps:

- Relation Building
- Elimination
- GCD Computation

Filtering is part of step 2 (Elimination). The main goal of the filtering step is to reduce the size of a large sparse matrix over a finite field in order to being able to compute its

**kernel**(we will see that for the factorization problem what we actually need is the**left kernel**). We will see that filtering per see is a 4 steps process:- Removing duplicates
- Removing singletons
- Removing cliques
- Merging

**filtering**that is part of

**Elimination**so in order to showcase it, we borrow a simple

**Relation Building**example from "An Introduction to Mathematical Cryptography"

from "An Introduction to Mathematical Cryptography" |

Again, without going to much in details, the goal here is to find a subset of all these relations whose product is a square on each side of the equality. This is equivalent to: the sum of the exponents in all chosen relations must be even. If you wonder why, we are trying to leverage one of the simplest of the identities in all of the mathematics:

X^2 - Y^2 = (X+Y)(X-Y)

But let's not diverge from out today's topic and let's focus on filtering. The problem above can be translated in a linear algebra problem. If one sees the relations as rows of a matrix where each column corresponds to an ideal, the coefficients of the matrix are the exponents of the ideals in the relations. As we are looking for even exponents, one can consider the matrix over GF(2).

Finding a linear combination of relations such as every exponent is even is equivalent to

computing the left kernel of the matrix. So lets translate A to B = A'

As we mentioned before, the matrix that comes out from factoring problem is huge (in the order of billions of row/columns). Our goal is to minimize the size of the matrix before the kernel computation in order to make this operation possible. So let's filter

## Removing duplicates

The first step of filtering is extremely trivial. It is simply**. As weird at it sounds having those rows is inevitable though in the factorization context (because lattice sieving with many distinct special q-primes will produce identical relations).**

*about removing the duplicate rows in the matrix*## Removing singletons

The rule of removing singleton is as simple as the removing duplicates above:*. Such column*

**if there is a row in B that contains only a single entry that is non-zero module 2 them the column containing non-zero entry can not occur in a dependency****,**called

**singleton**, can be removed from B (together with the respective row). In order to give an example let's use a simpler matrix then B above called M

**removing singleton**filter:

So we removed the first row and column of M. But we are not done yet as you can see this removal generated a new singleton (the first column of the new matrix) so several passes are normally requited before all singleton are normally removed from M. Continuing until the very end may not be worth the effort though (we will see in the next post how to handle this part).

## Conclusion

**Filtering**is a really important step in Number Field Sieve and it is implemented also in important integer factorizatiion tools as

**CADO-NFS**,

**Msieve**and

**GGNFS**. In this blog post we covered the first to phases of

*filtering*as r

*emoving duplicates*and

*removing singleton*. Stay tuned for the last two steps

*removing cliques*and

*merging*coming in part II of the blog post series.

**For more crypto goodies, follow me on Twitter.**
## Comments