Prime Numbers in CS


Prime Numbers in CS

Maria
Welcome back to Computer Files! Last time, we were diving into mega-favorite numbers, which was fascinating. I remember you mentioned prime numbers and their connection to computer science. It sounded like there was a lot more to uncover there.

Michael
That's right, Maria! I'm glad you brought it up. Prime numbers are incredibly interesting in computer science, and their applications are far-reaching, especially in keeping our digital world secure. It's a topic I'm really keen to discuss today.

Maria
I'm looking forward to it! So, what makes prime numbers so special in this context? I recall you mentioning that it's not always easy to tell if a number is prime, but finding its prime factors is even harder. Can you elaborate on that?

Michael
Exactly. Take 91, for example. It's not immediately obvious if it's prime. But once you know it's 7 multiplied by 13, you can easily verify it's not. The trick is, while verifying a prime is relatively quick, actually finding those prime factors for very large numbers is computationally intensive.

Maria
So, for small numbers like 91, a computer can find 7 and 13 quickly, but for much larger numbers, it becomes an incredibly difficult problem, even for powerful computers. Is that why it's so valuable in computer science, particularly in cryptography?

Michael
Precisely. That difficulty is the cornerstone of its application in cryptography. If I want to communicate secretly with you, this 'hard-to-factor' property allows us to establish secure connections. It's truly ingenious how this mathematical quirk underpins so much.

Maria
That's fascinating. You mentioned cryptography. Is this where algorithms like Diffie-Hellman and RSA come into play? I've heard those names before, especially RSA, which seems to be everywhere, securing our internet connections.

Michael
You're spot on! Diffie-Hellman is one example, but RSA, named after Rivest, Shamir, and Adelman, is a major player. It's heavily based on prime numbers and is used almost every time we connect to the internet, particularly with HTTPS connections. It's crucial for online security.

Maria
So, RSA is a form of public-key cryptography, which I understand involves a public key for encryption and a private key for decryption. And it's one of the main technologies powering HTTPS, alongside elliptic curve cryptography, right?

Michael
That's right. HTTPS, which ensures secure communication over the internet, relies heavily on this. RSA is the older, more established algorithm, and it's built entirely on the principles of prime numbers, which is why we're discussing them today. It's a testament to the power of mathematics.

Maria
That's a fantastic real-world link. Can you show us how this works in practice? Like, if I'm looking at a website, how can I see RSA in action, even if I don't fully understand the underlying math just yet?

Michael
Absolutely. If you go to a secure website, like our university's, you'll see a padlock in the top left. If you click on that padlock, then 'More Information,' and 'View Certificate,' you'll see details like the algorithm being RSA and a huge 'modulus' number.

Maria
Wow, so that padlock symbol isn't just for show! And that modulus number, you mentioned it's a massive 600-digit decimal number. Is that number itself prime, or is it something else?

Michael
It's not prime itself, but here's the kicker: it's the product of two incredibly large prime numbers, each around 300 digits long. The security of RSA hinges on the fact that nobody knows what those two original prime numbers are, only their product.

Maria
So, if someone could factor that enormous 600-digit number back into its two 300-digit prime components, they could essentially break the encryption? That's quite a challenge, to say the least.

Michael
Indeed. If you could do that, you'd not only get a fully funded PhD and a guaranteed spot in the newspapers, but you'd also be able to hack the University of Nottingham's website, and countless others. It’s an almost impossible task with current computing power.

Maria
That really highlights the security aspect. But how does this 'difficult to factor' idea actually form the basis of a cryptographic protocol? We need a system, not just a difficult problem. Is this where modular arithmetic comes in?

Michael
Exactly. To understand how it works, we need to talk about modular arithmetic. It sounds complex, but we use it every day with an analog clock. If it's 15 hours past noon, we know it's 3 o'clock, because we're working with a 12-hour modulus.

Maria
So, it's about remainders after division, like 15 mod 12 equals 3. And you can even do negative numbers, like minus one hour is 11 on the clock. That's a great way to simplify it.

Michael
Right. Now, imagine a 91-hour clock, because 91 is the product of our two primes, 7 and 13. Let's pick a 'magic number' between 0 and 90, say 18. I then instruct you to raise 18 to the fifth power on this 91-hour clock.

Maria
Okay, so 18 to the power of 5, but every time the number exceeds 90, you take the remainder when divided by 91. That's a bit of a mental workout! What would the result be?

Michael
For a human, it's tough, but the answer would be 44. Now, here's the 'magic' part: if I take that 44 and raise it to the 29th power on the same 91-hour clock, what number do you think we'll end up with?

Maria
Raising 44 to the 29th power? That sounds even more complex! But if this is about reversing the operation, like an inverse, would we end up back at 18, our original number?

Michael
Precisely! We end up back at 18. So, 29 is a 'magical' number that reverses the operation of taking a number to the power of 5 on this particular 91-hour clock. It's a special property of these numbers and the modulus.

Maria
That's quite an elegant trick! So, if 5 is your 'encryption' exponent, then 29 is your 'decryption' exponent. And these numbers are specific to the 'clock' you're using. So, there are three main questions: where does 29 come from, how does it work, and why is it secure?

Michael
Exactly. Let's tackle the first question: where does 29 come from? Remember our 91-hour clock, derived from 7 times 13. Now, we construct a different clock using the numbers one less than our primes: 6 (from 7-1) and 12 (from 13-1).

Maria
So, 6 multiplied by 12 gives us 72. So we're working on a 72-hour clock now. And then we pick a prime number that doesn't divide 72, like 5, which we used as our encryption value, 'e'.

Michael
That's right. Five is our 'e'. Now, for the magic number 29, which we call 'd', we need to find a value such that 5 multiplied by 'd' is equal to 1 on our 72-hour clock. This is where the Extended Euclidean Algorithm comes in, which computers can find quickly.

Maria
And the algorithm gives us 29! So 5 times 29 is 145. And 145 on a 72-hour clock means 145 divided by 72 leaves a remainder of 1, because 72 times 2 is 144. So 5 times 29 is indeed 1 mod 72.

Michael
That's exactly it. So that's where our magical 29 comes from. Now, for the second question: how does this actually work to reverse the operation? The key lies in that relationship: e times d minus 1 is a multiple of 72.

Maria
So, since e times d minus 1 is a multiple of 72, it also means it's a multiple of 6 (7 minus 1) and 12 (13 minus 1). This is where the properties of modular arithmetic and prime numbers really converge, right?

Michael
Precisely. Because it's a multiple of both (prime minus 1) values, we can apply Fermat's Little Theorem. This theorem, discovered by Fermat hundreds of years ago, without any thought of cryptography, is crucial here.

Maria
So, Fermat's Little Theorem allows us to deduce that a number 'm' raised to the power of (e times d minus 1) simplifies to 1 on our clock. And with that little 'm' left over, we get 'm' back. It's an elegant mathematical cancellation!

Michael
It is quite elegant. It’s definitely complex to follow, but all these pieces fit together perfectly, cancelling out in a way that brings us back to our original number. It's a beautiful demonstration of how abstract mathematics finds practical application.

Maria
That brings us to the final, and perhaps most important, question: is it secure? We know how it works and where the numbers come from, but how is it that the University of Nottingham can calculate these secret numbers, but a malicious third party cannot?

Michael
The security stems from the difficulty of prime factorization. Remember we found 72 by taking (7-1) and (13-1) and multiplying them. If you don't know the original prime factors of 91, you can't construct that 72-hour clock.

Maria
Ah, I see! The university knows the two large prime numbers they used to create the 600-digit modulus. They multiply them, which is easy, and then calculate 'e' and 'd' based on those primes. But they then discard the primes themselves.

Michael
Exactly. The university knows 'p' and 'q' (the two large prime numbers), calculates 'n' (their product), 'e', and 'd'. They then publish 'n' and 'e' but keep 'd' secret and discard 'p' and 'q'. An attacker would only see 'n' and 'e'.

Maria
So, when I look at the university's certificate, I see the huge modulus (n) and the public exponent (e), which was like our '5'. But the secret decryption exponent (d), our '29', is hidden. And to find 'd', you'd need 'p' and 'q', which means factoring 'n'.

Michael
That's it. The number 'e' is typically small, like 65,537 in the university example, which is 2^16 + 1. But 'd' is a massive 300-digit number. Brute-forcing 'd' by trying every possibility is computationally impossible within any reasonable timeframe.

Ask anything
We've read this whole study set, so questions about it get the best answers.