Safe Prime Calculator

Primes

is 47 a safe prime?Yes - it is a safe prime
is the number itself prime?Yes
(p - 1) / 223 - prime
next safe prime after it59
How this was calculated
  1. A safe prime is a prime p where (p - 1) / 2 is also prime; that half-value is called a Sophie Germain prime.
  2. First check: 47 itself is prime.
  3. Second check: (p - 1) / 2 = 23, which is prime.
  4. Both checks pass, so the number is a safe prime - the kind used to build hard discrete-logarithm groups in cryptography.

A safe prime is a prime p whose half-neighbour is prime as well: (p - 1) / 2 must also be prime. The prime 47 qualifies, because (47 - 1) / 2 = 23 and 23 is prime. The prime 41 does not, because (41 - 1) / 2 = 20, which is not prime at all. Two primality checks, and a prime can pass one and fail the other.

The other half of the pair has its own name: if p is a safe prime, then q = (p - 1) / 2 is a Sophie Germain prime, a prime q with 2q + 1 also prime. The two families are the same list read from opposite ends - every safe prime is 2q + 1 for a Sophie Germain prime q.

Safe primes are working tools in cryptography, not just definitions. This calculator tests a single number with both checks and shows the next safe prime after it, or lists every safe prime up to a limit - there are 25 of them up to 1000.

Worked examples

Is 47 a safe prime?

(0 - test a single number · 47)

is 47 a safe prime?Yes - it is a safe prime
is the number itself prime?Yes
(p - 1) / 223 - prime
next safe prime after it59

Is 41 a safe prime?

(0 - test a single number · 41)

is 41 a safe prime?No - it is not a safe prime
is the number itself prime?Yes
(p - 1) / 220 - not prime
next safe prime after it47

Safe primes up to 100

(1 - list up to a limit · 100)

safe primes up to 1005, 7, 11, 23, 47, 59, 83
how many there are7
largest of them83
its Sophie Germain half (p - 1) / 241

What a safe prime is

Start with an odd prime p. Halving p - 1 gives an integer q = (p - 1) / 2. If q is also prime, p is a safe prime and q is its Sophie Germain partner. The smallest example is p = 5, with q = 2. Then come 7 (half 3), 11 (half 5), 23 (half 11) and 47 (half 23) - notice the chain hiding there: 5 is the half of 11, 11 is the half of 23, and 23 is the half of 47, a Cunningham chain in action.

Most primes fail the test. Of the 168 primes below 1000, only 25 are safe. The failures split into kinds: a prime like 41 whose half is composite in an uninteresting way, and primes like 13 whose half 6 is composite, or 19 whose half 9 is a perfect square. Passing requires the half to survive its own primality test, and primality gets rarer as numbers grow - so safe primes, needing two hits at once, thin out roughly like 2C x / (ln x)^2, with C the twin-prime constant.

  • p safe means p prime and (p - 1) / 2 prime
  • The half (p - 1) / 2 is called a Sophie Germain prime
  • Smallest safe prime: 5, with half 2
  • Only 25 safe primes below 1000, out of 168 primes

Testing a number

The test mode runs the two checks in order and reports both, so a failure says where it failed. For 47: first check, 47 is prime; second check, (47 - 1) / 2 = 23 is prime; verdict, safe prime. For 41: the first check passes but the second gives 20, not prime, so 41 is an ordinary prime. For a composite input the first check fails immediately and the calculator says so, showing the factorisation context through the next-safe-prime pointer instead.

That pointer is practical: after any input the calculator reports the next safe prime above it, found by scanning odd candidates and running both checks on each. After 41 it points to 47; after 47, to 59. Cryptographic code that needs a safe prime of a given size does essentially this search at much larger scales, which is why generating large safe primes is noticeably slower than generating ordinary primes - most candidates die on the second check.

  • Check one: is n prime?
  • Check two: is (n - 1) / 2 prime?
  • 47 passes both; 41 passes only the first
  • The next safe prime after 41 is 47; after 47, it is 59

Listing safe primes

List mode sweeps every odd number up to the limit and keeps those passing both checks. Up to 100 there are 7 safe primes: 5, 7, 11, 23, 47, 59 and 83. Up to 200 there are ten, adding 107, 167 and 179. Up to 1000 the count reaches 25.

The calculator reports the count, the largest in range and its Sophie Germain half, so the pairing between the two families is visible in the output itself: the largest safe prime up to 1000 is 983, and its half is 491, itself prime - the largest Sophie Germain prime in that range.

  • Up to 100: 5, 7, 11, 23, 47, 59, 83
  • Up to 200: ten, adding 107, 167, 179
  • Up to 1000: 25 safe primes, largest 983 with half 491
  • Each list entry pairs with a Sophie Germain prime

Why cryptography uses them

Diffie-Hellman key exchange works in the multiplicative group modulo p, which has p - 1 elements. If p is a safe prime, p - 1 = 2q with q prime, so the group has a subgroup of prime order q - enormous, and with no small subgroups to exploit. Small subgroups are an attack surface: an adversary who can confine a shared secret to a small subgroup can brute-force it. Safe primes remove almost all of that surface in one stroke, leaving only the subgroups of order 1, 2, q and 2q.

That is why the standard Diffie-Hellman groups in protocols such as TLS and SSH are built on safe primes, and why ElGamal encryption is usually presented over a safe-prime modulus. The cost is generation time: a 2048-bit safe prime requires testing many candidates twice over. The test on this page is the same two-check procedure, scaled down to numbers you can inspect.

  • Modulo a safe prime, the group order is 2q with q prime
  • A prime-order subgroup of size q blocks small-subgroup attacks
  • Standard TLS and SSH Diffie-Hellman groups use safe primes
  • Generation is slower: every candidate faces two primality tests

Cunningham chains and related structures

Safe primes rarely travel alone. A Cunningham chain is a run of primes where each is twice the previous plus one: 2, 5, 11, 23, 47 - read backwards, each term is the safe prime built on the one before, since 5 = 2 x 2 + 1, 11 = 2 x 5 + 1, 23 = 2 x 11 + 1 and 47 = 2 x 23 + 1. Every interior member of such a chain is simultaneously a Sophie Germain prime (looking down) and a safe prime (looking up). The chain through 47 is visible inside this calculator's list up to 200.

Chains matter beyond their beauty. In cryptography, primes p where (p - 1) / 2 is prime are wanted; in some protocols, primes where (p + 1) / 2 is prime (the mirror condition, tied to factorisation difficulty for p - 1 versus p + 1 methods) are wanted too, and Cunningham chains supply both patterns at once. The longest known chains run to a dozen or more primes of hundreds of digits, found by distributed searches.

Safe primes also anchor a standard construction: given a safe prime p = 2q + 1, the quadratic residues modulo p form a cyclic subgroup of prime order q, the cleanest possible setting for Diffie-Hellman and Schnorr-style protocols. The generator choices and subgroup checks in real standards documents are, at root, bookkeeping around the two primality checks this page performs - which is why a humble two-check calculator is a fair miniature of production key generation.

  • Chain: 2, 5, 11, 23, 47 - each twice the last plus one
  • Interior chain members are both safe and Sophie Germain primes
  • Quadratic residues mod p form a group of prime order q
  • Real DH standards are built on exactly this structure

Generating safe primes at scale

Production key generation runs this page's two checks at sizes where naivety fails. A 2048-bit safe prime begins as a random odd candidate q; the generator tests q for primality, forms p = 2q + 1, and tests p - most candidates die at one gate or the other, and the search expects to try on the order of a million candidates before both gates pass. Sieving tricks pre-filter candidates whose q or 2q + 1 has small factors, discarding the doomed majority before any expensive Miller-Rabin work, exactly as the small-factor logic works elsewhere on this site.

The cost is why safe primes are generated once and reused: the standard Diffie-Hellman groups published in RFCs are single, fixed safe primes with vetted generators, shared by millions of connections. Reuse is safe because the hardness lies in the discrete logarithm problem, not in the secrecy of p. The calculator's next-safe-prime pointer is the same search in miniature - scan, test twice, report the survivor.

  • Two gates: q prime and 2q + 1 prime
  • Sieving pre-filters candidates with small factors
  • Standard groups reuse one published safe prime
  • Hardness is the discrete log, not secrecy of p

FAQs

What is a safe prime?

A prime p where (p - 1) / 2 is also prime. 47 qualifies because its half, 23, is prime.

Is 41 a safe prime?

No. 41 is prime, but (41 - 1) / 2 = 20 is not prime, so 41 fails the second check.

How many safe primes are there up to 1000?

25. They thin out quickly because each one needs two primality hits at once.

What is the smallest safe prime?

5, because (5 - 1) / 2 = 2 is prime.

What is a Sophie Germain prime?

The half of a safe prime: a prime q such that 2q + 1 is also prime. The safe primes and Sophie Germain primes are the same pairs read from opposite ends.

Why do cryptographers want safe primes?

Modulo a safe prime p = 2q + 1, the multiplicative group has a subgroup of large prime order q and almost no small subgroups, which blocks small-subgroup attacks on Diffie-Hellman.

Sources

Related calculators

Chen Prime Calculator
Check whether a prime is a Chen prime - p where p + 2 is prime or semiprime - or list Chen primes up to a limit.
Circular Prime Calculator
Test whether a number is a circular prime - prime under every cyclic rotation of its digits - listing all rotations and the next circular prime.
Cousin Prime Calculator
Count cousin prime pairs - primes differing by 4 - up to a limit by sieve, with the largest pair in range and the twin-prime count at the same limit for comparison.