# Modular Arithmetic

<figure><img src="/files/ZlFxXabBELjxsItWjWrs" alt=""><figcaption></figcaption></figure>

**Modular arithmetic** is sometimes called **“clock arithmetic”** because numbers wrap around once they reach a certain value. For a clock, that value is **12**: after 12 o’clock comes 1 again.

In mathematics, this “wrap-around” value is called the **modulus**.

***

### Equivalence Classes

When we apply modular arithmetic to integers, numbers group together into **equivalence classes**.

* **Equivalence class** means: *all numbers in the group behave the same way under modular arithmetic*.
* Example: Saying *“all green cars are green”* is similar to saying *“all numbers in the class behave the same”*.

***

### Defining the Modulo Operation

We write the modulo operation like this:

**a ≡ r (mod m)**

This means:

* When we divide **a** by **m**, the remainder is **r**.
* Here, **a**, **r**, and **m** are integers, and **m > 0**.
* **m** is called the **modulus**.

***

### Examples

* **9 ≡ 4 (mod 5)** → 9 divided by 5 leaves remainder 4.
* **14 ≡ 4 (mod 5)** → 14 divided by 5 also leaves remainder 4.
* **–1 ≡ 4 (mod 5)** → –1 divided by 5 also corresponds to remainder 4.

So, the set \[ …, –1, 4, 9, 14, … ] forms **one equivalence class** under mod 5.

<figure><img src="/files/FLrkOi7qcO1up9HClIs0" alt=""><figcaption></figcaption></figure>

***

### The Pattern

If we apply **mod 5** to all integers, the results always fall into one of **five equivalence classes**:

* \[0], \[1], \[2], \[3], \[4]

More generally, for **mod n**:

* We get **n distinct equivalence classes**: \[0], \[1], \[2], …, \[n–1]

&#x20;

***

### Examples of Modular Arithmetic

| **Modulus (n)** | **Operation**                 | **Examples**                                  | **Equivalence Classes**      |
| --------------- | ----------------------------- | --------------------------------------------- | ---------------------------- |
| **mod 2**       | Remainder after dividing by 2 | 5 ≡ 1 (mod 2) → 5 ÷ 2 = 2 remainder 1         | \[0], \[1]                   |
| **mod 3**       | Remainder after dividing by 3 | 7 ≡ 1 (mod 3), 8 ≡ 2 (mod 3), 9 ≡ 0 (mod 3)   | \[0], \[1], \[2]             |
| **mod 5**       | Remainder after dividing by 5 | 9 ≡ 4 (mod 5), 14 ≡ 4 (mod 5), –1 ≡ 4 (mod 5) | \[0], \[1], \[2], \[3], \[4] |
| **mod 10**      | Last digit of the number      | 27 ≡ 7 (mod 10), 123 ≡ 3 (mod 10)             | \[0], \[1], \[2], …, \[9]    |

***

### Why It Matters in Cryptography

Modular arithmetic makes operations like **exponentiation with very large numbers** both **feasible and predictable**. This property is at the heart of many cryptographic algorithms, including those used in digital signatures.

***

### Key Takeaway

Modular arithmetic is a **system of remainders** that groups numbers into repeating classes. This simple idea makes it possible to handle very large computations efficiently, which is why it is so important in cryptography.


---

# Agent Instructions: 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:

```
GET https://hub.bsvblockchain.org/higher-learning/bsv-academy/bitcoin-primitives-digital-signatures/ecdsa-prerequisites/modular-arithmetic.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
