> For the complete documentation index, see [llms.txt](https://ir0nstone.gitbook.io/notes/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://ir0nstone.gitbook.io/notes/cryptography/overview/choice-of-primes/fermat-factorisation.md).

# Fermat Factorisation

If $$p$$ and $$q$$ are numerically close, we can use [Fermat Factorisation](https://en.wikipedia.org/wiki/Fermat's_factorization_method).

During Fermat Factorisation, we hope to find $$a$$ and $$b$$ such that

$$
a^2 - b^2 = N
$$

Because that then means we can factorise the left-hand expression into

$$
(a+b)(a-b)=N
$$

As thus we get the two factors of $$N$$ as $$(a+b)$$ and $$(a-b)$$.

The reason we use this when $$p$$ and $q$ are numerically close is because the closer they are to each other the closer they are to $$\sqrt{N}$$. If we say $$a=\sqrt{N}$$ rounded up to the nearest number, we can calculate $$b^2 = a^2-N$$ (as rearranged from before) until $$b$$ is a whole number, after which we've solved the equation.

An example of this attack can be found in [this writeup](https://www.notion.so/Factorize-b96056dc70f54cc7b42b32f8984cb7cf), which may make it a bit clearer.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://ir0nstone.gitbook.io/notes/cryptography/overview/choice-of-primes/fermat-factorisation.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
