01
Modular Arithmetic — Reduction Rules
Master Rule
(a op b) mod p = ((a mod p) op (b mod p)) mod p
// valid for op = + − × — reduce inputs before and output after
// valid for op = + − × — reduce inputs before and output after
| Operation | Rule | Key note |
|---|---|---|
| Addition | (a + b) mod p = ((a mod p) + (b mod p)) mod p | Result < 2p; at most one subtraction of p needed |
| Subtraction | (a − b) mod p = ((a mod p) − (b mod p) + p) mod p | +p prevents negatives; always add p before reducing |
| Multiplication | (a × b) mod p = ((a mod p) × (b mod p)) mod p | Reduce both first; product is at most (p−1)² |
| Exponentiation | ae mod p = (a mod p)e mod p | Reduce base first; reduce after every squaring |
| Division ⚠ | (a / b) mod p = (a mod p) × (b−1 mod p) mod p | No direct division — use modular inverse of b |
Modular Inverse — two methods
// Method 1: Fermat's little theorem (p must be prime)
b−1 ≡ b(p−2) (mod p)
// Method 2: Extended Euclidean — find k, j such that b·k + p·j = 1
// then k mod p is the inverse
b−1 ≡ b(p−2) (mod p)
// Method 2: Extended Euclidean — find k, j such that b·k + p·j = 1
// then k mod p is the inverse
Interactive — Modular Inverse Calculator
Interactive — Modular Arithmetic Calculator
02
Point Addition — P + Q
Curve
y² = x³ + ax + b (mod p)
Slope λ
λ = (yQ − yP) · (xQ − xP)−1 mod p
Result Point R = P + Q
xR = λ² − xP − xQ mod p
yR = λ(xP − xR) − yP mod p
yR = λ(xP − xR) − yP mod p
1
Compute Δy = yQ − yP mod p and Δx = xQ − xP mod p
2
Find (Δx)−1 mod p using Fermat:
(Δx)(p−2) mod p3
λ = Δy · (Δx)−1 mod p
4
xR = λ² − xP − xQ mod p
5
yR = λ(xP − xR) − yP mod p
Special cases: If P = Q → use point doubling. If xP = xQ but yP ≠ yQ → P + Q = 𝒪 (point at infinity, the identity). P + 𝒪 = P always.
03
Point Doubling — 2P
Tangent slope λ
λ = (3xP² + a) · (2yP)−1 mod p
Result Point R = 2P
xR = λ² − 2xP mod p
yR = λ(xP − xR) − yP mod p
yR = λ(xP − xR) − yP mod p
1
Numerator: 3xP² + a mod p
2
Denominator: 2yP mod p. Find its modular inverse.
3
λ = numerator · (denominator)−1 mod p
4
xR = λ² − 2xP mod p
5
yR = λ(xP − xR) − yP mod p
Special case: If yP = 0 → 2P = 𝒪 (tangent is vertical).
04
Interactive — λ & Point Calculator
Curve parameters
Point P
Point Q
05
Scalar Multiplication — nP (Double-and-Add)
Algorithm — Double-and-Add
// Write n in binary: n = bkbk−1...b1b0
1. R = P // start: leading 1-bit
2. For each remaining bit (left to right):
R = 2R // always double
if bit = 1: R = R + P // add only on 1-bits
3. Return R
1. R = P // start: leading 1-bit
2. For each remaining bit (left to right):
R = 2R // always double
if bit = 1: R = R + P // add only on 1-bits
3. Return R
Operation count
Doublings: ⌊log2(n)⌋
Additions: popcount(n) − 1
Total: much less than n − 1
Additions: popcount(n) − 1
Total: much less than n − 1
Analogy to fast exp
Double → Square (R²)
Add P → Multiply by b
Same binary scan, same logic
Add P → Multiply by b
Same binary scan, same logic
Interactive — Scalar Multiplication Calculator
06
Fast Exponentiation — Square-and-Multiply
Algorithm
R = b // start
bit=0 → R = R2 mod p
bit=1 → R = R2 · b mod p
bit=0 → R = R2 mod p
bit=1 → R = R2 · b mod p
Complexity
Naive: e − 1 multiplications
Fast: ~2 log2(e) operations
For e=2048: 4000 vs 10616
Fast: ~2 log2(e) operations
For e=2048: 4000 vs 10616
Interactive — Fast Exponentiation Calculator
07
Hasse's Theorem — Point Count Bounds
What does #E mean?
Notation
#E means "the number of points on the elliptic curve E" — it is the order (size) of the group formed by the curve's points.Specifically, #E counts every pair (x, y) with x, y in {0, 1, …, p−1} that satisfies y² ≡ x³ + ax + b (mod p), plus one extra for the point at infinity 𝒪, which acts as the identity element (the "zero") of the group.
So: #E = (number of (x,y) pairs on the curve) + 1
Why it matters
In elliptic curve cryptography, the security of operations like scalar multiplication nP depends on #E being large (so brute-force is infeasible) and having a large prime factor (to resist Pohlig–Hellman attacks). Hasse's theorem tells you, without counting every point, that #E is always close to p — so picking a large prime p guarantees a large and secure group.The Theorem
| #E − (p + 1) | ≤ 2√p
// Expanded into lower and upper bounds:
p + 1 − 2√p ≤ #E ≤ p + 1 + 2√p
// Perfect-square form (easier to remember):
(√p − 1)² ≤ #E ≤ (√p + 1)²
p + 1 − 2√p ≤ #E ≤ p + 1 + 2√p
// Perfect-square form (easier to remember):
(√p − 1)² ≤ #E ≤ (√p + 1)²
Why is the centre p + 1, not p?
Intuition — quadratic residues
For each of the p possible x-values, you compute S = x³ + ax + b mod p and ask: how many y satisfy y² ≡ S?— If S = 0 → exactly 1 solution (y = 0)
— If S is a quadratic residue (a perfect square mod p) → exactly 2 solutions (y and p−y)
— If S is a non-residue → 0 solutions
Exactly half of {1, …, p−1} are quadratic residues, so averaging over all x: each x contributes exactly 1 point on average. That gives p points from p x-values, plus the point at infinity 𝒪, totalling p + 1 as the expected centre.
Why the error is bounded by 2√p
Hasse (1930s, completed rigorously by Weil 1948) proved that the deviation t = #E − (p+1) can be written as t = α + ᾱ where α is a complex number with |α| = √p. Since |α + ᾱ| ≤ 2|α|, the error is at most 2√p. This is a deep result from algebraic geometry — it's not obvious, but the bound is tight: some curves actually reach #E = p + 1 ± 2√p.How to calculate bounds by hand
1
Take your prime p.
2
Compute √p (decimal approximation is fine — it doesn't need to be exact).
3
Multiply by 2 to get 2√p.
4
Lower bound = p + 1 − 2√p, then round up (ceiling) to the nearest integer — #E must be a whole number.
5
Upper bound = p + 1 + 2√p, then round down (floor).
Example — p = 17
√17 ≈ 4.123 → 2√17 ≈ 8.246
Lower: 18 − 8.246 ≈ 9.754 → ceil = 10
Upper: 18 + 8.246 ≈ 26.246 → floor = 26
Result: 10 ≤ #E ≤ 26
Lower: 18 − 8.246 ≈ 9.754 → ceil = 10
Upper: 18 + 8.246 ≈ 26.246 → floor = 26
Result: 10 ≤ #E ≤ 26
Example — p = 37
√37 ≈ 6.083 → 2√37 ≈ 12.166
Lower: 38 − 12.166 ≈ 25.834 → ceil = 26
Upper: 38 + 12.166 ≈ 50.166 → floor = 50
Result: 26 ≤ #E ≤ 50
Lower: 38 − 12.166 ≈ 25.834 → ceil = 26
Upper: 38 + 12.166 ≈ 50.166 → floor = 50
Result: 26 ≤ #E ≤ 50
Interactive — Hasse Bounds Calculator