- Learn
- Mathematics
- University Math
- Discrete Mathematics
- Number Theory and Cryptography
Number Theory and Cryptography
IntermediateLesson 6 of 10 in Discrete Mathematics Course
What you will learn
- State the Chinese remainder theorem, build the solution of a system whose moduli are pairwise coprime, and solve systems step by step, including systems whose moduli share factors.
- State the Chinese remainder theorem and build its solutionSay exactly what the theorem guarantees, construct the solution from modular inverses, and see why the solution is unique modulo the product of the moduli.
- Solve systems step by step, including shared factorsSolve systems by back substitution, handle congruences with coefficients, decide when moduli that share a factor still allow a solution, and set up word problems.
- State the Chinese remainder theorem and build its solution
- Evaluate very large powers modulo n quickly, use Fermat's little theorem to simplify powers and find inverses modulo a prime, and use it to test whether a number is prime.
- Compute large powers with repeated squaringUse the binary expansion of the exponent to compute a power modulo n with few multiplications, and count how much work this saves.
- Apply Fermat's little theoremState and prove Fermat's little theorem, use it to reduce exponents and find inverses modulo a prime, and avoid the usual misapplications.
- Test numbers for primality with Fermat's theoremUse the Fermat test to prove that a number is composite, explain why passing the test does not prove primality, and recognize pseudoprimes and Carmichael numbers.
- Compute large powers with repeated squaring
- Compute Euler's totient function, use Euler's theorem to simplify powers and find inverses for any modulus, and combine it with the Chinese remainder theorem and repeated squaring.
- Compute Euler's totient functionCount the residues coprime to n, compute the totient from a prime factorization, and see why the totient of an RSA modulus is as secret as its factors.
- Prove and use Euler's theoremState and prove Euler's theorem, use it to reduce exponents and find inverses modulo any n, and know exactly when it applies.
- Combine the theorems to evaluate powersChoose and chain the right tools, from splitting the modulus with the Chinese remainder theorem to reducing exponents and repeated squaring, to evaluate powers by hand and check the result.
- Compute Euler's totient function
- Explain what makes public-key cryptography possible, generate an RSA key pair, encrypt and decrypt messages, prove that decryption works, and sign and verify messages, including why textbook RSA is not used unprotected.
- Explain the idea of public-key cryptographyContrast secret-key and public-key cryptography, explain one-way and trapdoor functions, and describe what the security of RSA rests on.
- Generate an RSA key pairChoose the primes, compute the modulus and the totient, choose a valid public exponent and compute the private exponent with the extended Euclidean algorithm.
- Encrypt, decrypt and prove that RSA worksEncrypt and decrypt with RSA, prove with Euler's and Fermat's theorems that decryption recovers every message, speed up decryption with the Chinese remainder theorem, and explain the limits of textbook RSA.
- Sign and verify messages with RSACreate and check RSA signatures, explain why practical signatures sign a hash, and show the forgeries that unprotected textbook signatures allow.
- Explain the idea of public-key cryptography
- Find orders and primitive roots, explain the discrete logarithm problem, and carry out and analyze the Diffie–Hellman key exchange.
- Find orders, primitive roots and discrete logarithmsCompute the order of a number modulo n, recognize and test primitive roots modulo a prime, and explain why discrete logarithms are hard to compute.
- Run and analyze the Diffie–Hellman key exchangeCarry out the Diffie–Hellman key exchange, explain what an eavesdropper sees and must solve, and recognize its limits and its relatives such as ElGamal encryption.
- Find orders, primitive roots and discrete logarithms
Number Theory and Cryptography study guide
1Solve systems of congruences with the Chinese remainder theorem
Several remainder conditions can describe the same integer. The Chinese remainder theorem tells you when they fit together and how much information they give. You will construct a solution using inverses, prove its uniqueness, and solve systems by substitution. Shared factors need a compatibility check and change the least period of the solution class. These methods later let you split a large modular computation into smaller parts.
1.1 State the Chinese remainder theorem and build its solution
A list of remainders can identify one integer within a fixed range. The Chinese remainder theorem, or CRT, tells you when every list is possible and gives a formula for the integer.
What the theorem guarantees
Let the moduli be pairwise coprime: every pair has greatest common divisor 1. Put . For any remainders , the system has exactly one solution modulo . Thus one solution lies in , and every solution is obtained by adding an integer multiple of .
Pairwise is stronger than having no factor common to the whole list. The moduli 6, 10 and 15 have overall gcd 1, but each pair shares a factor. For instance, a number congruent to 0 modulo 6 is even and cannot also be congruent to 1 modulo 10.
Build one term for each remainder
For each modulus, define and find . The inverse exists because the other moduli are all coprime to . Then
solves the system. To see why, reduce the sum modulo one chosen modulus . Every other term contains and vanishes. The remaining term is . Each term therefore supplies its own remainder without changing the others.
Work through a system
Solve , and . Here and the partial products are 35, 21 and 15. Their inverses modulo 3, 5 and 7 are 2, 1 and 1, respectively: , and . The construction gives
Check every remainder: . The complete answer is for any integer . It includes ; the number 23 alone does not describe the full solution set.
Why there is only one class
If and both solve the system, every divides . Since the moduli are pairwise coprime, their product divides that difference. Hence . There are also exactly lists of possible remainders and residues modulo , so the correspondence matches them one-to-one.
This correspondence preserves addition and multiplication. Modulo 105, 23 corresponds to and 47 to . Add component by component and reduce to get , the remainders of 70. This lets a large calculation be split into smaller ones and reconstructed afterward.
With shared factors the product need not be the least period. The conditions modulo 6, 10 and 15 together give . Writing only would omit valid solutions such as 31 and 61.
Key Takeaways
- Pairwise coprime moduli give one solution class modulo their product.
- Each CRT construction term survives modulo its own modulus and vanishes modulo every other one.
- Two solutions differ by a multiple of every modulus, hence by a multiple of their product.
- Give the full answer as a congruence class, including its modulus.
- Addition and multiplication can be carried out on the separate remainders.
1.2 Solve systems step by step, including shared factors
Back substitution builds the solution one congruence at a time. It also handles shared factors, provided you check that the conditions agree on those factors.
Turn a congruence into an equation
Solve , and . The first condition means for an integer . Substitute into the second:
Since , multiply by 7 to get . Write and substitute back: . This describes every solution to the first two conditions, not just the representative 23.
Now impose the last condition: gives , so . Therefore . Check: , and .
Remove coefficients before combining
A condition such as first becomes , since and . If , then is solvable exactly when . When it is, divide the coefficient, right side and modulus by before finding an inverse. Dividing only the coefficient and right side would preserve the wrong modulus.
Shared factors impose a compatibility check
For and , a solution exists exactly when . Both remainders must describe the same residue modulo the common divisor. When they do, the full answer is one class modulo .
For and , the gcd is 2 and the difference is 2, so the conditions agree. Set . Then becomes , giving and . In contrast, and require opposite parities and have no solution.
Split into prime powers when useful
A condition modulo 24 can be split into conditions modulo 8 and 3. Conditions on powers of the same prime agree exactly when their remainders agree modulo the smaller power. Thus conflicts with . If two such conditions agree, keep the larger prime-power modulus; it already includes the smaller condition.
Translate a counting problem
“Fewer than 100 chairs leave 3 over in rows of 7 and 2 over in rows of 11.” Write and . Testing gives 24. The class is ; among positive counts below 100 only 24 fits. The bound selects a particular member of the class after the congruences have been solved.
Why the gcd test is sufficient
Substitution gives . Put . If , divide to obtain . Now and are coprime, so an inverse exists and determines one class of parameters modulo . Each increase of in the parameter increases by . This proves both existence and the claimed least period.
For compatible powers of the same prime, the larger condition already implies the smaller one. For example, implies . Retaining the modulus-8 condition keeps exactly the integers that satisfy both; it does not discard an independent restriction.
Key Takeaways
- Keep the integer parameter when substituting a solution into the next congruence.
- Normalize coefficient congruences using the gcd and a modular inverse.
- Shared-factor systems are solvable exactly when the remainders agree modulo the gcd.
- A consistent two-modulus system repeats with least positive period equal to the lcm.
- Compatible conditions on powers of one prime reduce to the larger-modulus condition.
- Apply bounds from a counting problem after finding its solution class.
2Compute modular powers and use Fermat's little theorem
Cryptographic calculations involve powers whose ordinary values are far too large to write down. Repeated squaring keeps their residues small and uses the binary exponent to limit the work. Fermat’s little theorem supplies an exponent period for prime moduli and explains a simple primality test. You will connect the algorithm to its proof and distinguish a conclusive compositeness witness from a number that merely passes a test.
2.1 Compute large powers with repeated squaring
A modular power can be computed without ever forming the huge ordinary power. Repeated squaring combines two facts: products can be reduced at every step, and every nonnegative integer exponent has a binary expansion.
Build powers whose exponents double
To compute , first reduce the base. Square it to obtain modulo , square again for , then continue with . Each squaring doubles the exponent; it does not add 2 to it. Reduce after each multiplication so the factors stay below and the unreduced product is at most .
For , write . The needed table is:
| Exponent | Residue modulo 391 |
|---|---|
| 1 | 5 |
| 2 | 25 |
| 4 | 234 |
| 8 | 16 |
| 16 | 256 |
| 32 | 239 |
| 64 | 35 |
For example, , and . Every row is obtained from the preceding row.
Select the 1-bits
Only the exponents 1, 4, 16, 32 and 64 occur in the binary sum. Multiply their residues in stages:
Thus . Multiplying all rows would use the exponent , not 117.
A right-to-left implementation starts with result 1 and power . Read each bit from the right: multiply the current power into the result for a 1-bit, then square the power before moving to the next bit. Skip the final unnecessary squaring. The first multiplication into 1 can be a copy. If the exponent is zero, the result stays 1, reduced modulo .
Read the bits from the other direction
For a positive exponent, start with the base to account for the leading 1-bit. For every remaining bit, square the running result; for a 1-bit, also multiply by the base. Appending a binary digit changes an exponent to or , which explains the two operations. With bits 1110101, the running residues are .
Count the work and keep the exponent straight
If a positive exponent has bits and 1-bits, these versions use squarings and extra multiplications. For 117, that is operations instead of 116 successive multiplications by 5. A 2048-bit exponent needs at most 4094 operations of this kind.
Reducing the exponent modulo the modulus is not a valid general shortcut. Modulo 3, , whereas replacing 5 by gives . A valid exponent period comes from a theorem or a known cycle of powers. Small negative residues can shorten arithmetic: immediately gives .
Key Takeaways
- Reduce the base and each intermediate product modulo the modulus.
- Binary 1-bits select which squared powers to multiply.
- Appending a binary bit means squaring, then multiplying by the base if the bit is 1.
- For a positive exponent, the optimized cost is squarings plus further multiplications.
- The modulus of a residue is not generally a valid period for its exponents.
2.2 Apply Fermat's little theorem
Fermat’s little theorem provides a period for powers modulo a prime. Its hypotheses determine which form you can use and which factors you may cancel in its proof.
State the two forms
If is prime and , then . For every integer , including multiples of , the equivalent form is . When , the second form reads ; the first form would incorrectly claim .
Why multiplication rearranges the residues
Take the nonzero residues and multiply each by . None becomes zero, since divides neither factor. No two become equal: allows cancellation of and gives . Within the chosen range, that forces . So the products are the same nonzero residues in another order.
For and , the new list is . Multiply the whole list in either order to obtain
Every factor of is nonzero modulo the prime, so the product has an inverse. Cancel that product to get the theorem. The cancellation is justified by coprimality; modular arithmetic does not permit cancellation of every nonzero integer for every modulus.
Shorten an exponent
Write . Then . For , use . Hence . Reducing 100 modulo 13 instead would give exponent 9 and the wrong residue 5.
For , the relevant period is 10. Since , the result is . If the remainder exponent is zero, the answer is 1 because the original base is coprime to the prime.
Find an inverse
For , the identity gives . Thus . Check by multiplication: . The extended Euclidean algorithm gives the same answer and also works for composite moduli whenever the gcd is 1.
Check the modulus and the cycle
Primality is essential: using the first form with modulus 4 would claim , but the actual remainder is 3. For a composite modulus, Euler’s theorem gives the corresponding result with a different exponent.
The theorem supplies a working period, not necessarily the shortest one. Modulo 11, even though . The smallest positive exponent returning a coprime base to 1 is called its order. This smaller cycle later helps test bases for Diffie–Hellman.
Key Takeaways
- For a prime and , Fermat gives .
- The form also covers multiples of .
- Multiplication by an invertible residue permutes the nonzero residues modulo a prime.
- Reduce exponents modulo , and obtain inverses from exponent .
- A working exponent period can exceed the order of the base.
2.3 Test numbers for primality with Fermat's theorem
Fermat’s theorem gives a one-sided test for primality. A failed equality settles the question; a successful equality leaves open the possibility that a composite number happens to pass.
Test a candidate
For an odd integer , choose and compute by repeated squaring. If the result differs from 1, is composite and is a Fermat witness. Every prime would satisfy the equality for such a base, so failure rules out primality.
For , the computation gives . Since 64 differs from 1, 91 is composite. This proves compositeness without requiring the factors to be found. A witness generally supplies a contradiction to primality, not a factorization.
If a gcd calculation first gives , it already supplies a factor and proves compositeness. Exponentiation is then unnecessary.
A pass can be misleading
A number that passes is a probable prime to that base. A composite number that passes is a Fermat pseudoprime to that base. The base is part of the description: passing for one base need not mean passing for another.
For example, is composite, yet . It is the smallest base-2 pseudoprime. Base 3 exposes it because . One failed test outweighs any number of successful tests: a prime cannot fail even one valid test.
Carmichael numbers fool every coprime base
A Carmichael number is a composite integer satisfying for every base coprime to it. The smallest is ; 1105 and 1729 are further examples. Alford, Granville and Pomerance proved in 1994 that infinitely many exist.
Korselt’s criterion characterizes them: a composite integer is Carmichael exactly when it is squarefree and for every prime divisor . For 561, the prime factors are distinct and 560 is divisible by 2, 10 and 16. Both parts matter; the divisibility conditions alone cannot replace squarefreeness.
Bases sharing a factor still expose 561. For instance, , and already explains its compositeness. But repeatedly selecting coprime bases will never expose a Carmichael number. When its prime factors are large, a random base is unlikely to hit one of them.
Why stronger tests are useful
RSA key generation needs large primes, so software tests candidates with stronger methods such as Miller–Rabin. The prime number theorem estimates the fraction of primes near as . Near this is about one in 710 integers, or one in 355 odd integers. Selecting odd candidates saves work, but their primality still needs testing. The Fermat test explains the distinction between evidence that a number may be prime and a certificate that it is composite.
Key Takeaways
- A Fermat witness proves compositeness without necessarily finding a factor.
- A successful Fermat test gives a probable prime to one base.
- A pseudoprime is composite despite passing the test for its specified base.
- Carmichael numbers pass for every coprime base.
- Korselt’s criterion requires both squarefreeness and divisibility by every prime-factor period.
- Stronger primality tests avoid the Fermat test’s Carmichael weakness.
3Work with Euler's totient function and Euler's theorem
Euler’s totient counts which residues can be inverted. Its prime-factor formula leads to an exponent theorem that works for composite moduli as well as primes. You will prove that theorem by rearranging coprime residues, then combine it with repeated squaring and CRT. The important choice is local: a base may be invertible modulo one factor and divisible by another, so each part of a calculation may need a different argument.
3.1 Compute Euler's totient function
Euler’s totient function counts the residues that have modular inverses. Its value is easy to compute from a prime factorization, and for an RSA modulus that connection also explains why the totient must stay secret.
Count the coprime integers
For a positive integer , counts the integers with and . For 12, those integers are , so . They form a reduced residue system modulo 12. The endpoint 12 is not counted because it shares a factor with itself. The special value follows because .
For every the totient is even. Each coprime residue pairs with , also coprime to . No such residue pairs with itself: if and , then is not coprime to .
Start with primes and prime powers
If is prime, every integer from 1 to is coprime to it, so . For , the excluded integers are exactly the multiples of . There are of these among the first integers, giving
Thus , and . The tempting formula is wrong: it would give 4 for instead of 6.
Combine distinct prime factors
When , the CRT matches each residue modulo to a unique pair of residues modulo and . A residue is coprime to exactly when both components are coprime to their respective moduli. Therefore .
Combining the prime-power formulas gives
where the product takes each distinct prime divisor once. For , this gives . Similarly, and . Multiplicativity requires coprime factors: , but .
Recover RSA factors from the totient
For distinct primes and , . Hence . Knowing the sum and product determines the factors as the roots of
With and , the sum is 114. The discriminant is , so gives 61 and 53. Conversely, those factors immediately give the totient. For a modulus known to be a product of two distinct primes, recovering its exact totient and factoring it can therefore be reduced to each other.
Key Takeaways
- The totient counts residues coprime to the modulus.
- For prime powers, subtract the multiples of the prime.
- Totients multiply over coprime factors; apply one product-formula factor per distinct prime.
- For distinct primes, .
- The exact totient of a two-prime RSA modulus reveals its prime factors.
3.2 Prove and use Euler's theorem
Euler’s theorem extends Fermat’s little theorem to composite moduli. The key requirement is that the base have an inverse modulo the modulus.
State the theorem
If , then . For prime , the value recovers Fermat’s theorem. For composite moduli the totient counts the invertible residues that replace the prime case’s full list of nonzero residues.
Prove it by rearranging a reduced residue system
Let represent all residues coprime to . The products remain coprime to . They are also distinct modulo : an equality allows cancellation of because its inverse exists. Thus multiplication by rearranges the reduced residue system.
Put . Multiplying all the original and rearranged residues gives . Every factor of is coprime to , so can be cancelled. This gives . Using all nonzero residues for a composite modulus would break this proof because their product may lack an inverse.
Reduce powers and find inverses
Write . Euler gives . The last digit of is found modulo 10: and , so . For the last two digits of , use . The exponent reduces to zero, giving residue 1; two-digit formatting makes the answer “01.”
An inverse follows from . Modulo 20, , so . Since and , . Check . Euclid can find the same inverse without knowing the totient.
Respect the base condition
For , the gcd is 2 and , not 1. A zero remainder exponent would therefore give a false answer. Repeated squaring still works for this base; splitting the modulus may also help.
The totient need not be the smallest valid period. Every odd number squares to 1 modulo 8, even though . The order is the smallest positive exponent returning a chosen coprime base to 1, and divides the totient.
Work from the top of a power tower
For the last digit of , the outer exponent is . It needs reduction modulo 4, not modulo 10. Since , . Thus . The modulus used to shorten an exponent can itself be part of a second modular calculation.
Key Takeaways
- Euler’s theorem requires .
- Multiplication by a coprime base permutes a reduced residue system.
- Use as an exponent period and as an inverse.
- A valid period may be larger than the base’s order.
- In a power tower, reduce the upper exponent using the period of the outer base.
3.3 Combine the theorems to evaluate powers
Large modular powers often become short calculations when you choose a different method for each factor of the modulus. The CRT joins the results after Fermat, Euler or direct powers have handled the smaller pieces.
Choose the route for each factor
Start by checking the gcd of the base and the modulus. If a factorization is available, split the modulus into pairwise coprime factors, usually its prime powers. Check coprimality separately for each piece. Use Fermat for a prime piece and Euler for a composite piece when the base is invertible there. Otherwise, compute the power directly or look for divisibility that makes its remainder zero. Finish with repeated squaring and reconstruct the residue by CRT.
The exponent period belongs to the particular modulus and base. A reduction valid for one piece need not be valid for another. Recombination also requires the moduli, not just the list of remainders.
Recompute a power in two smaller systems
For , use . Modulo 17, Fermat shortens the exponent to , giving . Modulo 23, the exponent becomes , giving .
Now solve and . Write , so . Since , . Hence and the result is . Its remainders are indeed 14 and 17, matching the independent repeated-squaring calculation.
Split three prime factors
For , factor . Fermat gives exponents 4 modulo 6, 0 modulo 10 and 4 modulo 12. The power therefore has remainders 2 modulo 7, 1 modulo 11 and 3 modulo 13.
Combine the first two conditions by setting . Then , so and . The final condition gives , hence . This produces . Check , and .
Working directly modulo 1001, Euler would shorten 1000 only to . Splitting makes the arithmetic use bases below 13 and exponent remainders below 12.
Let a shared factor guide the calculation
For , Euler cannot be used with the whole modulus because . Modulo 3 the power is zero; modulo 5 the base is 1, so the power is 1. The system , gives .
Adding the component remainders would give 1 and fail modulo 3. CRT reconstruction is a solving step, not an addition rule. In RSA, the same split into prime factors both speeds decryption and supplies a correctness proof for messages that share a factor with the modulus.
Show the local arithmetic
In the calculation modulo 17, and , so . Modulo 23, and ; hence . The exponent reductions leave short powers, and the squaring method evaluates those powers without large integers.
A prime-power component needs its own divisibility check. A base divisible by 2 is zero modulo 2, but its square need not be zero modulo 8: . One more factor gives . If Euler is unavailable in such a component, direct squaring tracks when divisibility becomes sufficient; declaring every positive power zero would be incorrect.
Key Takeaways
- Check coprimality separately in every factor modulus.
- Shorten an exponent with the theorem valid for that part, then use repeated squaring.
- Solve a CRT system to recombine the remainders.
- A shared factor can make one component zero while another uses a power theorem.
4Explain and carry out RSA encryption and signatures
RSA turns modular exponentiation into a public operation with a private inverse. You will follow a complete key pair from its prime factors to encryption, decryption and signatures, and prove why the inverse works for every message residue. The same arithmetic also exposes weaknesses in the bare formulas. Understanding those weaknesses explains why practical encryption and signing add carefully designed encoding and padding.
4.1 Explain the idea of public-key cryptography
Public-key cryptography separates an operation anyone may perform from one that requires secret information. Number theory supplies functions that are fast in the public direction and believed hard to reverse without a trapdoor.
Two ways to distribute keys
In symmetric cryptography, the same secret key encrypts and decrypts. A simple shift cipher illustrates the roles: with letters numbered 0 through 25, and . Both parties need the shift . Agreeing on a secret with someone over a channel others can read is the key distribution problem.
In public-key cryptography, each person publishes a public key and keeps a private key secret. A sender encrypts with the recipient’s public key; the recipient decrypts with the corresponding private key. A signature has different roles: its maker signs with a private key and others verify with that maker’s public key. Publishing one member of the pair must leave the private operation infeasible for an outsider.
Easy forward, hard backward
A one-way function is easy to compute but computationally hard to invert. A trapdoor function has secret information that makes inversion efficient. In RSA, the public calculation is . Repeated squaring makes it efficient. The private exponent makes the inverse operation efficient.
Multiplying two large primes is fast; recovering them from their product is much harder with known classical algorithms. For RSA, factoring gives and then . Conversely, a valid private exponent lets the modulus be factored efficiently. Computing the private key and factoring are therefore closely linked. Whether solving an arbitrary RSA ciphertext without recovering the key is equally hard remains an open mathematical question.
Size and computational assumptions
NIST SP 800-57 Part 1 Revision 5 estimates about 112 bits of security for a 2048-bit RSA modulus and about 128 bits for 3072 bits. A 2048-bit integer has 617 decimal digits. A larger modulus does not give the same number of security bits: attacks exploit number-theoretic structure, not just a search over all integers. The small keys used for hand calculations can be factored immediately.
The best known general classical factoring method is the number field sieve, with super-polynomial running time. Hardness has not been proved. Shor’s algorithm would factor integers and find discrete logarithms efficiently on a sufficiently large, error-corrected quantum computer. NIST published its first post-quantum standards, FIPS 203, 204 and 205, in August 2024, using different mathematical foundations.
From a message to an integer
RSA operates on . A hand encoding may use through , so “HI” becomes 0708, the integer 708. Such a block must fit below the modulus. In practical encryption, public-key methods usually protect a short random symmetric key, which then protects the longer data.
Diffie and Hellman published public-key ideas and their exchange in 1976. Rivest, Shamir and Adleman described RSA at MIT in 1977. Cocks had found an equivalent system at GCHQ in 1973; that work became public in 1997.
Key Takeaways
- Symmetric encryption requires a shared secret; public-key encryption separates public and private keys.
- Encrypt with the recipient’s public key and verify with the signer’s public key.
- RSA is easy to compute using repeated squaring and easy to invert with its trapdoor.
- Factoring an RSA modulus reveals the private key; a valid private exponent also permits factoring.
- RSA’s security rests on computational assumptions, with different classical and quantum attack models.
- An RSA message representative is an integer below its modulus.
4.2 Generate an RSA key pair
An RSA key pair is built so that encryption followed by decryption raises a message to an exponent that acts like 1. The crucial inverse is taken modulo a period of powers, not modulo the message modulus.
Build the modulus and choose the public exponent
Choose distinct primes . Compute and . Select with and . Then find satisfying .
For , the modulus is and the totient is . Choose . Its gcd with 3120 is 1, so it has an inverse. Choosing would fail because 3 divides 3120. The exponent need not be prime; the requirement is coprimality with the totient.
Compute the private exponent
Apply Euclid to 3120 and 17:
Work backward:
Thus the inverse of 17 is . Choose the positive representative . Check . This last equality is the property needed by the correctness proof.
Computing would answer a different inverse problem. The modulus 3233 controls message residues; the totient 3120 controls the exponent relation used here.
Decide what is public
Publish . Keep secret, along with and . Any one of the prime factors reveals the other by division. The exact totient reveals their sum and thus both factors. Keeping only secret while publishing the factorization would therefore leave the key exposed.
Distinct primes matter. If , the modulus is a square whose prime factor is recovered by taking its integer square root. Its totient is , so the two-distinct-prime formula would also be wrong.
Alternative exponent periods
RSA can instead use . Here . The inverse of 17 modulo 780 is 413 because . Both 413 and 2753 work; they are congruent modulo 780. Their exponent products are 1 modulo both prime-factor periods, which is enough for correctness.
A common public exponent is . Its binary representation has only two 1-bits, allowing 16 squarings and one extra multiplication. Coprimality must still be checked for the selected primes. In that formulation, a valid public exponent can exceed the totient; the small-range convention above selects a convenient representative for hand calculations. Large keys use randomly selected primes found by strong primality tests, with each prime about half the modulus’s bit length.
Key Takeaways
- Choose two distinct primes and compute their product and totient.
- The public exponent must be coprime to the exponent modulus.
- Use extended Euclid to compute .
- Publish and protect the private exponent, factors and totient.
- An inverse modulo also gives a working RSA private exponent.
4.3 Encrypt, decrypt and prove that RSA works
RSA encryption and decryption are modular powers. The correctness proof has two parts: Euler handles messages coprime to the modulus, and Fermat plus CRT covers every remaining message.
Encrypt and decrypt
For the recipient’s public key , encode the message as and compute . The recipient computes . With and , the ciphertext is ; decrypting gives . Both computations use repeated squaring. The 12-bit exponent 2753 needs 11 squarings and 4 further multiplications in the optimized method.
The range matters because decryption returns a residue. Encrypting the integer 3300 with this modulus and decrypting returns . It cannot distinguish two integers in the same congruence class.
First prove the coprime case
Write . If , Euler gives
The coprimality condition belongs to Euler’s theorem. It cannot be dropped just because a message is within range.
Cover messages sharing a factor
Work separately modulo and . If , then both and are zero modulo . If , use Fermat and to obtain . Repeat for . The two residues match those of , so the CRT gives equality modulo .
This proof covers for the running key, even though the gcd with 3233 is 61. Zero is also recovered by the same divisibility case. For a private exponent chosen modulo , the proof works because both and divide .
Use CRT to shorten private computations
For the ciphertext 2790, compute and . The component messages are and . Recombine and . Writing gives , so and . At large sizes, doing both powers with roughly half-sized numbers and exponents commonly gives about a fourfold speedup.
Understand what the bare formula reveals
Textbook RSA is deterministic: encrypting the same message twice gives the same ciphertext. Anyone can encrypt guesses with the public key and compare them. Also, 0 and 1 encrypt to themselves. If , reduction never occurs: for , a message 10 gives ciphertext 1000, exposing 10 by an ordinary cube root. Practical RSA encryption applies randomized padding, such as OAEP, before the public operation.
Follow the encryption power
For exponent , repeated squaring of 65 modulo 3233 gives , , and . Multiply the selected entries: . This produces the ciphertext 2790 using four squarings and one further multiplication.
The private exponent has a less sparse binary expansion: . Its largest selected power needs eleven squarings to reach exponent 2048; combining the five selected residues needs four more multiplications. The public and private operations follow the same algorithm, but the bit patterns explain their different costs. Re-encrypting a recovered representative with the public exponent checks the private computation without repeating its longer exponentiation.
Key Takeaways
- Encrypt with and decrypt with .
- The input representative must satisfy .
- Euler proves correctness for coprime messages; Fermat and CRT prove it for every message.
- CRT decryption computes separate powers modulo the private primes and recombines them.
- Determinism and small unpadded messages expose weaknesses in textbook RSA.
- Randomized padding is part of practical RSA encryption.
4.4 Sign and verify messages with RSA
A digital signature supports checks on message origin and integrity. It answers a different question from encryption: who authorized this message, and has it changed?
Sign with the private key, verify with the public key
In textbook RSA, a signature on a representative is . A verifier computes and checks whether the result equals . The correctness argument is the same identity used for decryption.
With , signing gives . The public check gives . Changing the message to 101 makes this check fail: the signature still verifies against 100.
The public key used for verification belongs to the signer. A verifier’s own key says nothing about a signature made with somebody else’s private key. Connecting a public key to its claimed owner requires authentication; a valid mathematical check establishes a relationship to that key.
A signature leaves the message public
Anyone with the public key can verify, so a signature supplies no confidentiality. Calling signing “private-key encryption” obscures this purpose and the different encodings used by practical encryption and signature schemes. If Alice wants a signed message to remain private to Bob, her signing key and Bob’s encryption key play separate roles. Bob decrypts with his private key and verifies using Alice’s public key.
Signatures also provide evidence that can support nonrepudiation: unlike a shared-secret check, public verification does not give every verifier the ability to sign. Attribution still depends on protecting the private key and establishing whose key it is.
Why multiplication permits a forgery
The bare RSA operation preserves multiplication. Given textbook signatures and modulo , their product satisfies
For the running key, signatures on 2 and 3 are 1027 and 1857. Their product modulo 3233 is 2902, a valid signature on 6. Someone can construct it without knowing . The forged message is a product of signed representatives; it need not be a message the signer ever approved.
There is another direct forgery. Choose a number first, then set . The pair passes the bare verification equation by construction. Choosing gives for this key. It does not allow the attacker to choose any desired message, but it demonstrates that passing the raw equation alone is insufficient.
Hash and encode before signing
Practical signatures first hash the message to a fixed-length fingerprint, then apply a signature encoding and padding before the private operation. Verification computes the corresponding hash and checks the encoding. The hash handles messages of arbitrary length; the full signature scheme blocks these algebraic forgeries. A product of two encoded representatives is not generally the required encoding of the hash of the desired message.
Signatures can authenticate the public values in a Diffie–Hellman exchange. Each party can then check who supplied the value before treating the resulting shared key as belonging to that party.
Key Takeaways
- Textbook RSA signs with and verifies with the signer’s public exponent .
- A signature checks origin and integrity; it does not hide the message.
- Multiplying textbook signatures yields a signature on the product of their message representatives.
- Choosing a signature first creates another raw-equation forgery.
- Practical signatures hash and encode the message before the RSA operation.
- Authentication connects a verification key to the person or service it identifies.
5Agree on a shared secret key with Diffie–Hellman
Diffie–Hellman builds a shared secret from public modular powers. To understand its choice of base, you first need the cycle length of a residue and the meaning of a discrete logarithm. You will follow both parties’ computations, identify what an eavesdropper would need to solve, and see why authentication is also necessary. ElGamal then applies the same shared-power idea to mask and recover a message.
5.1 Find orders, primitive roots and discrete logarithms
Powers of a coprime residue cycle back to 1. The cycle length explains which bases reach every nonzero residue and why recovering an exponent is a different problem from computing a power.
Find the order
For , the order is the smallest positive integer with . Euler guarantees that such an exponent exists. Modulo 11, the powers of 3 are , so the order is 5. The powers of 2 are , giving order 10.
If , divide by the order: with . Then . A positive remainder would contradict the minimality of , so . Thus the order divides every exponent returning the base to 1. In particular, , and for a prime modulus .
Recognize a primitive root
A primitive root modulo a prime has order . Its powers reach all nonzero residues exactly once before repeating. The base 2 is a primitive root modulo 11; the base 3 is not. Every prime has primitive roots, and there are of them. Modulo 11 they are 2, 6, 7 and 8, a total of .
To test a candidate , factor . For each distinct prime divisor , check
If the order were a proper divisor of , it would divide at least one of these smaller exponents. Passing every check therefore forces full order. Zero must be excluded first: its powers are never 1, but it has no multiplicative order.
For , the relevant prime divisors of 22 are 2 and 11. Since and , neither check gives 1. Hence 5 is a primitive root modulo 23.
Reverse a modular power
For a primitive root modulo and nonzero , the discrete logarithm is the unique exponent with . Modulo 11, because . Exponents are determined modulo the base’s order; the range selects one representative.
These logarithms are not obtained by ordinary real logarithms. Modulo 11, the discrete logs to base 2 of 8, 9 and 10 are 3, 6 and 5. Increasing the target by 1 does not produce a small predictable change in its discrete log.
Repeated squaring computes quickly even for large exponents. Recovering from the result is hard for suitably chosen large parameters with known classical algorithms. Trying every exponent is feasible for 11 or 23, but not for a well-chosen cryptographic modulus. This computational difference underlies Diffie–Hellman.
Why the primitive-root tests are enough
Write and suppose the order is a proper divisor of . The integer has a prime divisor . Therefore and . At least one test must fail for every proper order. Conversely, a result of 1 at any tested exponent proves that the order is smaller than .
A discrete-logarithm range removes a different ambiguity. If with , cancellation gives . Thus the order divides . For a primitive root, two different exponents in cannot differ by , giving uniqueness there. In particular, the target residue 1 has representative logarithm 0, while exponent names the same power outside that range.
Key Takeaways
- The order is the smallest positive exponent returning a coprime base to 1.
- The order divides every return exponent, including the totient.
- A primitive root modulo a prime has order .
- Test nonzero primitive-root candidates using the distinct prime divisors of .
- A discrete logarithm recovers an exponent from a modular power.
5.2 Run and analyze the Diffie–Hellman key exchange
Diffie–Hellman key exchange lets two parties derive the same secret from values exchanged publicly. It agrees on a key; it does not itself encrypt their message.
Exchange powers, keep exponents secret
Alice and Bob publicly choose a prime and a base of large order. Alice chooses a secret and sends . Bob chooses a secret and sends . Alice computes ; Bob computes . The values agree because
Neither secret exponent is sent. The shared number is used to derive a key for symmetric encryption.
For , Alice sends and Bob sends . Their calculations give and . An independent check uses , so .
State the outsider’s problem precisely
Eve sees . Computing from these is the Diffie–Hellman problem. Finding a discrete logarithm, such as recovering from , is sufficient: Eve could then compute . This is a sufficient attack route, not an equivalence proof between the two problems.
Multiplying the public values gives , not . In the example, , different from the shared value 2. With these small parameters Eve can simply enumerate powers; large, well-chosen parameters make that route infeasible with known classical methods.
Choose a base with enough possible powers
A large prime alone is insufficient. Modulo 23, the base has order 2. Its powers, and therefore possible shared values, are limited to 1 and 22. Cryptographic choices use large-order bases, commonly of large prime order. Elliptic-curve exchanges such as X25519 use a different representation to obtain comparable classical security with shorter keys. Shor’s algorithm threatens both classic RSA and discrete-logarithm exchanges on a sufficiently large quantum computer.
Authenticate the exchanged values
An active attacker can replace Alice’s and Bob’s public values and run a separate exchange with each. The attacker then knows both resulting keys and relays messages between them. This man-in-the-middle attack does not require solving the passive eavesdropper’s number-theory problem. Authentication, such as signatures on the exchange values tied to the parties, prevents undetected replacement.
Use the same idea for ElGamal encryption
Bob publishes . Alice chooses a fresh secret and sends and . Bob computes and removes that factor using its inverse.
For , , and . Bob gets ; its inverse is 14, and recovers the message. The fresh exponent supplies the shared masking factor for this encryption.
Key Takeaways
- The exchanged values are public powers; the exponents stay secret.
- Both parties reach by raising the received value to their own exponent.
- Computing a discrete logarithm is sufficient to break the classic exchange.
- A small-order base restricts the possible keys even when the prime is large.
- Authentication prevents an attacker from substituting exchange values.
- ElGamal masks a message with a shared power and removes it with a modular inverse.
Number Theory and Cryptography examples
1Combine remainder conditions
13 steps
- 1IntroductionOne integer, several remainders
A remainder condition describes infinitely many integers. Combining conditions can identify one class with a larger period. For pairwise coprime moduli, the Chinese remainder theorem guarantees a solution for every list of remainders.
We will solve one system with moduli 3, 5 and 7, then change to shared-factor moduli to see when a system can fail. Each final answer needs both a representative and its period.
- 2ExplanationCheck every pair
Take , and . The moduli are pairwise coprime, and their product is .
Checking only the gcd of the whole list would be insufficient. For example, 6, 10 and 15 have overall gcd 1, yet every pair shares a factor. The product construction needs inverses for each component, which is why the pairwise condition matters.
- 3ExampleMake three independent contributions
Divide 105 by each modulus to obtain the partial products 35, 21 and 15. Find their inverses in their own component moduli: , and .
Multiplying a partial product by its inverse makes it 1 in its own component. It is already 0 in every other component because it contains those other moduli as factors.
- 4ExampleWeight and add the contributions
Attach the requested remainders to their contributions:
Reduce modulo 105 to get . Check 23 directly: its remainders modulo 3, 5 and 7 are 2, 3 and 2. The integer 233 also works; reducing it selects the representative between 0 and 104, without discarding the other solutions.
- 5Key InsightExplain the unique class
Suppose two integers satisfy the three conditions. Their difference is divisible by 3, 5 and 7. Since these factors are pairwise coprime, the difference is divisible by 105.
That proves uniqueness modulo 105. It does not say only one integer works: the full set is for integer .
- 6ChallengeReconstruct a new pair
Solve and . Give the whole class.
---reveal---
Write . Then gives , so . The moduli are coprime, so 35 is the period. Reporting only 32 would leave out all other members of the solution class.
- 7ExampleCarry the parameter forward
Now solve , and by substitution. Write .
The second condition gives . Multiplying by inverse 7 gives . Keep its parameter: , so . Setting permanently would lose the freedom needed to meet the third condition.
- 8ExampleImpose the final remainder
Substitute into the condition modulo 5. It becomes , giving .
Thus . The modulus has grown from 4 to 36 and finally to 180 as each coprime condition is added. Reducing 167 modulo the three original moduli gives 3, 5 and 2.
- 9ExplanationShared factors must agree
For and , the gcd must divide . This says the two conditions must agree on the part of their moduli that they share.
For remainders 3 modulo 6 and 5 modulo 8, the gcd is 2 and both remainders are odd. The conditions are compatible. Their solution class has period , not the product 48.
- 10ExampleDivide the whole congruence
Set in the shared-factor example. The second condition gives . Divide coefficient, right side and modulus by 2:
The inverse of 3 modulo 4 is 3, so and . Dividing without reducing the modulus would describe a different, overly restrictive condition.
- 11ChallengeDecide whether a system is possible
Can an integer satisfy and ? Explain before trying a construction.
---reveal---
No. The gcd is 4, but it does not divide . The first condition implies remainder 2 modulo 4; the second implies remainder 1. No integer has both remainders. An overall search or modular inverse calculation cannot remove that conflict.
- 12ChallengeSolve a bounded counting problem
A positive collection has fewer than 60 items. Grouping by 4 leaves 1 over; grouping by 9 leaves 7 over. How many items are there?
---reveal---
Write . Then gives , so . The only positive member below 60 is 25; the next is 61. The congruences determine the class, and the bound selects the count.
- 13RecapCheck both the class and its period
For coprime moduli, CRT constructs a solution and proves uniqueness modulo the product. Substitution reaches the same class by preserving an integer parameter through each condition.
Shared factors require agreement modulo the gcd and give an lcm period. Before accepting an answer, reduce its representative in every original modulus and check that the stated period preserves all the conditions. Apply any interval restriction to the completed class.
2Compute powers and interpret Fermat tests
14 steps
Save this lesson to follow every Walkthrough step by step.
- 1IntroductionKeep the power small
- 2ExampleUse the binary exponent
- 3ExampleComplete the squaring chain
- 4ExampleMultiply the selected entries
- 5Key InsightCount operations from bits
- 6ChallengeApply the method independently
- 7ExplanationA prime gives a return exponent
- 8ExampleShorten a power and obtain an inverse
- 9ChallengeCheck the conditions before reducing
- 10ExplanationTurn a failure into a certificate
- 11ExplanationA pass leaves a gap
- 12ChallengeInterpret two test reports
- 13ChallengeConnect the theorem to the test
- 14RecapSeparate calculation from conclusion
3Count invertible residues and combine power theorems
12 steps
Save this lesson to follow every Walkthrough step by step.
- 1IntroductionCount before using a power theorem
- 2ExplanationRemove multiples of the prime
- 3ExampleMultiply across coprime factors
- 4ChallengeCalculate a totient from its factors
- 5ExplanationRearrange only the invertible residues
- 6ExampleUse the return exponent
- 7ChallengeReject an invalid shortcut
- 8ExampleSplit a power into prime moduli
- 9ExampleJoin the component results
- 10ChallengeHandle a noncoprime base locally
- 11ChallengeChoose both exponent moduli
- 12RecapKeep each condition with its theorem
4Build an RSA key and test what it guarantees
17 steps
Save this lesson to follow every Walkthrough step by step.
- 1IntroductionSeparate public and private operations
- 2ExplanationA public operation needs a trapdoor
- 3ChallengeChoose the recipient’s key
- 4ExampleForm the modulus and totient
- 5ExampleFind the inverse exponent
- 6ChallengeCheck a proposed smaller key
- 7ExampleEncrypt and reverse a representative
- 8ExplanationProve recovery for coprime messages
- 9Key InsightProve recovery for every residue
- 10ExampleDecrypt in smaller components
- 11ChallengeFind the missing proof case
- 12ExampleFind what a small ciphertext reveals
- 13ExplanationUse the signer’s public key
- 14ExampleMultiply signatures to expose a forgery
- 15ChallengeRecognize a pair created backward
- 16ChallengeAssign four key operations
- 17RecapDistinguish the guarantees
5Agree on a secret through public powers
14 steps
Save this lesson to follow every Walkthrough step by step.
- 1IntroductionCombine secrets without transmitting them
- 2ExplanationFind the first return to one
- 3ExampleTest for the full cycle
- 4ExplanationRecover an exponent in a small system
- 5ChallengeCheck an order and a root
- 6ExampleSend public powers
- 7ExampleReach the same shared value
- 8ExplanationState Eve’s task
- 9ChallengeCompute a different exchange
- 10ConnectionPrevent substituted exchange values
- 11ExampleBuild an ElGamal mask
- 12ExampleRemove the shared factor
- 13ChallengeDistinguish two ways an exchange can fail
- 14RecapMatch each tool to its purpose
Number Theory and Cryptography practice questions
Set 1: Remainders, powers and key roles10 Practice Questions
Question 1 of 10
Show answerHide answer
Question 2 of 10
Show answerHide answer
Question 3 of 10
Show answerHide answer
Question 4 of 10
Show answerHide answer
Question 5 of 10
Show answerHide answer
Question 6 of 10
Show answerHide answer
Question 7 of 10
Show answerHide answer
Question 8 of 10
Show answerHide answer
Question 9 of 10
Show answerHide answer
Question 10 of 10
Show answerHide answer
Set 2: Apply the theorems to public-key arithmetic10 Practice Questions
Save this lesson to see every answer and practice every set.
Question 1 of 10
Show answerHide answer
Save this lesson to see this answer.
Question 2 of 10
Show answerHide answer
Save this lesson to see this answer.
Question 3 of 10
Show answerHide answer
Save this lesson to see this answer.
Question 4 of 10
Show answerHide answer
Save this lesson to see this answer.
Question 5 of 10
Show answerHide answer
Save this lesson to see this answer.
Question 6 of 10
Show answerHide answer
Save this lesson to see this answer.
Question 7 of 10
Show answerHide answer
Save this lesson to see this answer.
Question 8 of 10
Show answerHide answer
Save this lesson to see this answer.
Question 9 of 10
Show answerHide answer
Save this lesson to see this answer.
Question 10 of 10
Show answerHide answer
Save this lesson to see this answer.
Set 3: Check boundaries, proofs and attacks10 Practice Questions
Save this lesson to see every answer and practice every set.
Question 1 of 10
Show answerHide answer
Save this lesson to see this answer.
Question 2 of 10
Show answerHide answer
Save this lesson to see this answer.
Question 3 of 10
Show answerHide answer
Save this lesson to see this answer.
Question 4 of 10
Show answerHide answer
Save this lesson to see this answer.
Question 5 of 10
Show answerHide answer
Save this lesson to see this answer.
Question 6 of 10
Show answerHide answer
Save this lesson to see this answer.
Question 7 of 10
Show answerHide answer
Save this lesson to see this answer.
Question 8 of 10
Show answerHide answer
Save this lesson to see this answer.
Question 9 of 10
Show answerHide answer
Save this lesson to see this answer.
Question 10 of 10
Show answerHide answer
Save this lesson to see this answer.
Set 4: Combine computations with justification10 Practice Questions
Save this lesson to see every answer and practice every set.
Question 1 of 10
Show answerHide answer
Save this lesson to see this answer.
Question 2 of 10
Show answerHide answer
Save this lesson to see this answer.
Question 3 of 10
Show answerHide answer
Save this lesson to see this answer.
Question 4 of 10
Show answerHide answer
Save this lesson to see this answer.
Question 5 of 10
Show answerHide answer
Save this lesson to see this answer.
Question 6 of 10
Show answerHide answer
Save this lesson to see this answer.
Question 7 of 10
Show answerHide answer
Save this lesson to see this answer.
Question 8 of 10
Show answerHide answer
Save this lesson to see this answer.
Question 9 of 10
Show answerHide answer
Save this lesson to see this answer.
Question 10 of 10
Show answerHide answer
Save this lesson to see this answer.
Set 5: Solve new systems and diagnose failures10 Practice Questions
Save this lesson to see every answer and practice every set.
Question 1 of 10
Show answerHide answer
Save this lesson to see this answer.
Question 2 of 10
Show answerHide answer
Save this lesson to see this answer.
Question 3 of 10
Show answerHide answer
Save this lesson to see this answer.
Question 4 of 10
Show answerHide answer
Save this lesson to see this answer.
Question 5 of 10
Show answerHide answer
Save this lesson to see this answer.
Question 6 of 10
Show answerHide answer
Save this lesson to see this answer.
Question 7 of 10
Show answerHide answer
Save this lesson to see this answer.
Question 8 of 10
Show answerHide answer
Save this lesson to see this answer.
Question 9 of 10
Show answerHide answer
Save this lesson to see this answer.
Question 10 of 10
Show answerHide answer
Save this lesson to see this answer.
Stuck on your own question? Homework Help
Key Terms
A
Authentication
B
Back substitution
Binary expansion
C
Carmichael function
Carmichael number
Chinese remainder theorem
Ciphertext
Compatibility condition
Component congruence
Coprimality
CRT decryption
CRT reconstruction
Cryptographic hash
D
Deterministic encryption
Diffie–Hellman exchange
Diffie–Hellman problem
Digital signature
Discrete logarithm
Discriminant
E
ElGamal encryption
Euler’s theorem
Euler’s totient
Exponent period
Exponent reduction
F
Fermat pseudoprime
Fermat test
Fermat witness
Fermat’s little theorem
K
Key distribution problem
Key generation
Korselt’s criterion
L
Least common multiple
Least nonnegative residue
Left-to-right exponentiation
Linear congruence
M
Man-in-the-middle attack
Message representative
Modular cancellation
Modular exponentiation
Multiplicative function
Multiplicative order
N
Nonrepudiation
O
One-way function
P
Pairwise coprime moduli
Partial product
Power tower
Prime power
Prime-power decomposition
Primitive root
Primitive-root test
Private exponent
Probable prime
Public exponent
Public-key cryptography
R
Randomized padding
Reduced residue system
Repeated squaring
Residue permutation
RSA modulus
S
Shared secret
Signature forgery
Signature verification
Simultaneous congruences
Solution class
Squarefree integer
Squaring
Symmetric cryptography
T
Totient product formula
Trapdoor function
About this lesson
Make your own Study Materials
Turn your notes, slides and documents into Study Guides, Practice Questions, Flashcards and 6 more tools.