liveBook · Manning

Primality test

As explained in Hacking Cryptography

A probabilistic primality test uses one or more selected bases or trials to determine whether a number is probably prime while allowing a small probability of error.

A probabilistic primality test filters large random candidates before asymmetric key generation. Random bytes become a bignum; composite candidates are discarded, and candidates that pass are used as probable primes. section 8.2.2

Exact factorization can test primality by checking divisors through √N, but that approach becomes impractical for numbers such as 1,024-bit candidates. Probabilistic tests instead sample bases a and check prime-number properties, including Fermat-style congruences such as a^(p−1) ≡ 1 mod p. section 8.2.2

The operational flow is:

  • Generate random bytes of the required length and interpret them as a candidate integer. section 8.2.2
  • Test the candidate with selected random bases or trials. section 8.2.2
  • Reject composites and repeat generation until a candidate passes for key generation. section 8.2.2
Figure 8.5 Prime numbers are generated by testing random numbers for primality.
figure

Trace candidate generation through the primality filter to asymmetric-key construction. section 8.2.2

Equation 8.3
\[\begin{aligned} \label{equation:fermats-little-theorem-1} \begin{aligned} a^p &= a \pmod{p} \\ a^p - a &= 0 \pmod{p} \\ (a)(a^{p-1} - 1) &= 0 \pmod{p} \\ \end{aligned} \end{aligned}\]

Inspect the modular relation underlying the Fermat-style primality check. section 8.2.2

How Hacking Cryptography covers it

How deep it goes
Explains factorization limits, modular checks, random trials, and the candidate-retry flow without implementing a specific test.
Examples
Uses random 1,024-bit candidates, divisibility through √N, and sampled bases a in modular tests.
What it assumes
Assumes modular arithmetic and bignum concepts while motivating the procedure from basic factorization.
What you can do after
Evaluate randomly generated large integers as candidate primes before using them in asymmetric key generation.
How it connects
Connects random number generation, factorization, Fermat's little theorem, and asymmetric encryption.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Hacking Cryptography. The section links point to the parts of the book it draws on.