Understanding Public Key Cryptography with Paint

(this post roughly corresponds to the narrative from part of a talk I’m preparing for S5 (approximately 16 years old) students, intended to portray a modern research area in an accessible manner. So I’d very much appreciate feedback in the comments!)

For centuries, cryptography – literally, `secret writing’ – has been used to securely send and receive messages. But although the sophistication of these systems increased, the core idea remained the same: combining a secret encryption rule with the plaintext message yields a ciphertext, from which the message is recovered by a corresponding decryption rule. Thus the secrecy of messages depended on preserving the secrecy of the cryptographic system (or at least certain parameters).

While this might be feasible for governments or armies, it leads to a fatal flaw when trying to communicate securely with a stranger, a task that underpins the millions of ecommerce transactions that take place every day, for instance. To share secrets, you must first share a secret, the particulars of the cryptographic system you wish to protect the message with. This presents a seemingly impossible hurdle: how can you share that first secret with a previously-uncontacted individual, if any instructions you give will also be available to your adversaries?

Public Key encryption is the solution to this problem; to get a feel for how this is achieved, we’ll consider a non-mathematical formulation in terms of mixing paint, before abstracting to the properties that make it work.

Secret Sharing with Paint

Suppose, then, that our protagonists, Alice and Bob, wish to share a secret: but all their communication is intercepted by an eavesdropper, Eve. How can Alice and Bob arrive at a colour without Eve also knowing it?

Alice and Bob are assumed to know a public base colour- and there’s no problem with Eve knowing this too. They then choose a private colour of their own, and combine some of that with the base colour to create a public mix. They can then send these mixtures to each other: Eve sees both public colours, but (since it’s a lot harder to unmix paint), has no idea what private colours were used to produce them.

Having received each others’ mix, Alice and Bob can then mix in their own private colour again, to produce a blend of three colours. But, each of them will have the same colour, since the order in which we mix paint is irrelevant. Eve, however, has no idea what this new mixture looks like.

The following table summarises who sees what, for a particular set of chosen private colours.

Useful Properties

What are the key ideas that make the example above work? and how can we mimic them mathematically?

Unmixing is necessary…

No combination of the colours Eve has seen will mix to give the fetching shade of mustard yellow that Alice and Bob know. Since they had to agree on the procedure, Eve would be able to recreate the desired shade if she knew either of the private colours, since then she could mix it with the corresponding public colour just like Alice and Bob. Unfortunately, the private colours are never disclosed, only their combination with the base colour. So Eve must analyse the public colours in the hope of extracting a private colour.

…but unmixing is hard!

Without an encyclopaedic knowledge of all combinations of paint, Eve cannot know what private colours have been used to generate the public ones. So her only apparent option is to keep trying candidates, mixing each of them with the base coat until she arrives at one of the public colours by sheer luck. This brute force approach will obviously take a very long time!

Fortunately, Alice and Bob don’t need to unmix.

For Alice and Bob, this is irrelevant- they’re only ever required to mix, which is much easier than unmixing. However, it’s vital that their two routes through the colours lead to the same result: that is, that Blue+Red+Green is the same as Blue+Green+Red.

Secret Sharing with Maths

This leads us in search of a ‘one-way function’: roughly speaking, a mathematical function with the property that it’s much more difficult to recover the inputs from the outputs (reverse the function) than it is to compute those outputs in the first place, thus satisfying the second property above. Moreover, we need a procedure by which Alice and Bob can make use of such functions to independently arrive at a mutual secret which cannot be obtained by Eve. To do so, we therefore require that the only way to deduce a shared secret is indeed to reverse the function (the first property). Finally, to make the whole thing work as described above, we require that two applications of the function can be performed in either order to give the same result. This is the third property, described mathematically as commutativity.

Unfortunately, no-one has been able to demonstrate a genuinely one-way function. Fortunately, there are a few candidates, for which even the best publically-known techniques for the reversal are painfully slow. But there remains a risk that someone, somewhere, will devise a smarter way to perform this mathematical unmixing, rendering the function useless for cryptographic application.

A candidate One-Way Function: Modular Exponentiation

For a number x, we say x is congruent to y modulo N (written x=y mod N) if y is the remainder after dividing x by N. This might seem a strange idea, but it’s something we do every day: a clock face works “mod 12”, so that if you add 6 to 7 you get 1, as 13 = 12*1 +1 = 1 mod 12.

So, for a fixed base g and modulus N (our equivalent of the base colour), we can compute the modular exponent of any x, defined as gx mod N (that is, multiply g by itselfx times, subtracting lots of N until the answer is at most N-1).

For instance, with a base of 2 and a modulus of 11, the modular exponent of x=6 is 26 mod 11. Working that out, we get 2*2*2*2*2*2 mod 11 = 64 mod 11 = 9 mod 11 since 64 = 5*11 +9.

The possibly surprising (but desired) result is that going backwards- that is, given y, finding x such that gx mod N =y mod N – is apparently hard for decently-sized N. This reversal is known as the Discrete Logarithm Problem, and generalises to some very abstract mathematical objects, known as finite groups, with varying difficulty.

For our example function, with N=21024+643 and assuming a computer capable of a billion tests per second, naively trying each possible private key in turn can take up to a staggering 3, 671743, 063000, 000000, 000000, 000000, 000000, 000000, 000000, 000000, 000000 years to match with a public key. This is significantly longer than the life of the universe so far – some 13700000000 years – and definitely longer than anyone is prepared to wait. Of course, there are smarter ways to try keys- and finding yet smarter ones for a given DLP is a very active area of research- but for such values of N none of the publically known ones fall within feasible time scales.

Further, we have the commutativity property we required: working out (ga)b is the same as (gb)a. So we should be able to share secret numbers via modular exponentiation just as we were able to share secret colours with paint mixing. This process is known as Diffie-Hellman Key Exchange.

Diffie-Hellman Key Exchange

  • Alice and Bob agree (in public) on a modulus N and a base g (the public base colour)
  • Each chooses a private key between 1 and N-1; a and b respectively (their private colour)
  • They each construct a public key by computing A=ga mod N and B=gb mod N (mixing private with base)
  • These can be safely exchanged, as it’s hard to get back a or b from the public keys A and B (unmixing hard)
  • Each then performs another modular exponentiation on the public key received:
    • Alice computes Ba mod N = (gb)a mod N = (gab) mod N = S mod N for some S between 0 and N-1.
    • Bob computes Ab mod N = (ga)b mod N = (gab) mod N = S mod N, the same S.

Hence (assuming N is chosen for the DLP to be sufficiently hard) Alice and Bob know a secret,S, which Eve does not.

Man-in-the-Middle Attacks

But is Diffie-Hellman truly secure? If we alter the ‘intruder power’, replacing our eavesdropper Eve with a more powerful character, the malicious Mallory, then we can construct a scenario in which Alice and Bob think they share a secret with each other, but instead share one with Mallory.

To achieve this, Mallory must be able to not just listen in on messages, but replace them with messages of his own. Given that power, he can pick a private key of his own, m, and generate the corresponding public key M, since the choice of g and N must be made in the clear.

Then, when Alice attempts to retrieve Bob’s public key B, Mallory instead supplies her with M, keeping A; and when Bob asks for Alice’s key A, revealing B, he is given M as well. Alice then computes S=Ma mod N, but Mallory has seen A and thus can compute S as Am mod N; Bob meanwhile computes T=Mb mod N which is also known to Mallory, being Bm mod N.

Thus, if Alice the tries to use a classical encryption system depending on the secrecy of S, then Mallory will be able to decode the ciphertext. Even more cleverly, he can re-encrypt it using T and forward it to Bob- who can decrypt it with the secret he thinks he shares with Alice, T. Thus no suspicion is raised, yet Mallory has also read the message, without ever having to reverse the one-way function.

Fortunately, this attack depends on near-total control of Alice and Bob’s communication. If Alice ever looks up Bob’s public key without Mallory intervening, then she’ll notice that it’s B, not M. Or, if she sends Bob a message encrypted with S that Mallory doesn’t intercept and repack, Bob will recieve a message that cannot be decrypted with the key he has, T: and knows that the Diffie-Hellman key exchange must have been compromised.

Conclusions

Identifying and preventing such attacks is part of cryptanalysis, and even with perfect cryptography, forms a vital part of designing secure communication systems. Since we don’t yet have a provably one-way function, cryptography itself remains an active field of mathematical research, drawing on a range of topics from both pure and applied areas to assess the difficulty of functions. Together, these two fields are known as cryptology; a subject which is becoming increasingly vital as computers and communication systems work themselves further into our everyday lives.

The Anatomy of a Maths Degree

Ever wondered what a mathematics degree involves? As another excuse to play with graphviz, and before I forget entirely, I decided to map out the contents of my undergraduate degree- a four year masters at the University of Bath, England. Here’s the result:

(clickthrough for legible, hi-res version)

The colours denote the year in which I took the course: green for year 1, blue for year 2, yellow for year 3, and red for year 4. The rating of the courses themselves is denoted by the course code- the first digit specifies the intended level of study. 1 is ‘certificate’, 2 is ‘intermediate’, 3 is ‘honours’ and 4 ‘masters’. The intention is that you collect at least 10 each at levels 3 and 4.

For a fuller description of what the courses involve, you can look up their course code in the unit handbook, available online via the university.

The Mathematics of Being Nice

Today I gave the first of this year’s postgraduate colloquia series, The Mathematics of Being Nice- cooperation in the Prisoner’s Dilemma. You can grab the slideshow as pdf here; since the potential audience was quite broad (and I’m no expert) I think it’s fairly light technically, and thus hopefully more accessible than many of my posts here. It’s essentially the talk version of my article for iSquared, so you should invest in a copy of that if you want something that’s more fleshed out!

iSquared Magazine

A while ago I received a request for articles for a new magazine, iSquared, which seeks to cover mathematical topics, particularly real-world applications, at a level accessible to those not active in the field. This is quite a challenge, but a vital one. My own article, on the Prisoner’s Dilemma in game theory, appears in the first edition, so you can judge how well I did by buying a copy! I suspect I went overly-technical, but it’s a long time since I was an A-level student- although I remember reading New Scientist then and I don’t think the content is any more demanding in iSquared. Certainly I feel any scientifically literate undergrad with an interest in maths would find it rewarding, and at £10 for the year, educators have little to lose by picking up a subscription for a college library. For those of you more deeply involved in the subject, why not consider writing an article? Whichever category you fall into, please give it your support!

First Year Presentation

Tomorrow I give my first year presentation, which determines whether I’m allowed to continue my studies. At 20-30 minutes, it’s a rather condensed version of my recent geometry club talk on the point counting problem, although this time I’m skipping more quickly through the fundamentals so that I can discuss some of the algorithms in depth, and taking a (hopefully) clearer route.

Both the report itself and the OHP slides version are available (pdf). Content covered: hyperelliptic curves, points, divisors, mumford polynomials and the Picard group/Jacobian; the discrete logarithm problem; explicit group law computation; characteristic polynomial of Frobenius and Weil theorems/interval; group-theoretic approaches; Schoof’s algorithm, SEA in genus 1, genus 2 hybrid algorithms.

On logic

As part of my postgraduate life here in Edinburgh, I’m expected to tutor a couple of dozen first (of four) year undergraduate Mathematics students. Thus I was involved in the marking of their group theory exam last Friday. As the questions were being assigned to markers, there seemed to be a reluctance to take the logic question, probably because students manage to tie themselves in hard-to-follow knots with this material. However, I spent some of my previous year at Bath tutoring on set theory and logic to Physicists, so was prepared for the worst, and I’d also spotted that the question was multiple choice, so the actual marking shouldn’t be too taxing, so I took responsibility for that one.

Still, 207 scripts later I was somewhat worried by the general standard. There were quite a few attempts which received full marks, but many more received zero, including elsewhere high-scoring candidates. Whilst the emphasis of a group theory exam should be group theory, the pressure of an exam and lack of access to notes will always dampen performance, and I probably received a few scripts that were pure guesswork, that’s still a disappointment. Whilst it pains me to say it, a student could become competent at first year undergraduate mathematics just by learning how to ‘turn the handle’ of appropriate bits of mathematical machinery, and as this is pretty much all that they do at school, they may find that university-level mathematics isn’t really the subject they thought it was. To make the leap from computation to understanding, it strikes me as vital to gain mastery of basic logical manipulation. Nor should this just be the domain of Mathematicians; if anything, a precise understanding of the structure of an argument (and its falsification) should be even more crucial to the Humanities.

So I’m going to reproduce the question here and then attempt to unravel it in a precise manner but without recourse to the more abstract approaches of formal logic, in the hope that everyone can follow. Here goes:

Consider the statements:

A. All people who can sing in tune are musical.

B. Some people who cannot sing in tune are musical.

C. Some unmusical people can sing in tune.

D. Some unmusical people cannot sing in tune.

E. No unmusical person can sing in tune.

Mark with the number 1 any of the statements B,C,D,E that are logically equivalent to statement A, with 2 any that are (logically equivalent to) the negation of A, with 3 any that are (logically equivalent to) the converse of A, with 4 any that are (logically equivalent to) the negation of the converse of A and with 0 any which are in none of the above categories.

Solution is behind the cut, so if you want to try this yourself without spoilers, now’s the time!

Continue reading ‘On logic’ »

Mathematical tourism

The Quaternions
The plaque marking the discovery of the quarternions, Dublin.

I’ve spent the last three days in Dublin, and whilst there I couldn’t resist taking a rare opportunity for mathematical tourism. Hamilton figured out the structure of the quarternions in “a flash of genius” during a morning walk along Dublin’s royal canal, carving them into the nearby Brougham bridge. Whilst the carving does not survive, there is a plaque at the spot to mark this discovery, and it was that I set off to see.

Getting there doesn’t seem too difficult, though: I got the 120 bus from Parnell Street (conveniently close to my hotel on Parnell Square) to Broombridge road. This is the opposite of advice found elsewhere online, but the suggested 20 route doesn’t seem to exist any more. If you have a map, it’s easy enough to follow the bus route, but as I didn’t I just asked the driver to give me a shout when we got there. Brougham is pronounced broom, so all you need to do is to find where Broombridge road crosses the canal and (it turns out) railway tracks: presumably taking the western suburban line would be an even easier way to get there, as the platform is next to the bridge. Of course, I went the wrong way along Broombridge road, which is easily detected by reaching the end without finding a bridge!

It’s been described as “the least visited tourist attraction in Dublin”, partly because the area isn’t particularly appealing today but presumably because there aren’t that many mathematical tourists!

It does seem a shame that the site – and Hamilton – are so overlooked compared to the attention that, say, local authors or artists get. But it’s better than nothing, as mathematical tourist attractions are few and far between, especially outside of universities. The bridges of Konigsberg are often suggested; I’d also be fascinated to tour Japan in search of sangaku. If you have other ideas, why not mention them in the comments?

The Carnival of Mathematics

Welcome to the sixth edition of the Carnival of Mathematics! I’ve certainly enjoyed looking through this fortnight’s submissions, and hope you will too.

Trivium Pursuit‘s series on when best to introduce formal arithmetic to children continues with this article, setting out their arguments for providing an informal experience of mathematics before the age of ten. I remember plodding through worksheets at that age, where “harder” simply meant “more tedious”, perfoming larger calculations with by-then familiar processes; it’d be interesting to know how my mathematical career would have been shaped by avoiding such drill in favour of the suggested approach. I suspect most mathematicians will also find it a very strange idea, assuming they also took to formal arithmetic at a very young age and thus find it hard to imagine struggling with such material.

Of course, the beauty of mathematics often lies not in getting an answer, but getting there in a clever way. Political Calculations presents criss-cross multiplication; an easier way to multiply two digit numbers in your head; whilst Let’s play math offers some guidelines for K-12 students to help organise their thoughts in problem solving. But over in the universe of discourse, Mark Dominus warns of the danger of over-thinking a problem where brute-force may suffice. This is particularly true when computers are thrown into the mix, as the time taken to devise and program an elegant solution may well exceed the runtime of the naive approach.

At the other extreme, though, there are computations which you wouldn’t survive to see the result of if you took the brute-force option. I’m reminded of this e2 writeup, which describes a particularly daft hello world program along with a curious question: if hardware performance continues to double with Moore’s law, how long should you wait before running such a program? After all, if you estimate a function will take 4 years to execute on your current machine, but one twice as fast will be available in 18 months, then you can save 6 months (and a load of electricity) by waiting a year and a half, obtaining the faster kit, and running your program for just two years. Conversely, if you want to guard against brute-force attacks for a given length of time, just how good does your encryption have to be?

Also on e2 (hey, if I’m hosting, I can plug my favourite site, right?), sam512 has been exploring very, very large numbers, inspired by this edition of xkcd: so far the clarkkkkson, hyper operator and linear array notation have been covered.

Many of this weeks submissions presented problems, of varying difficulty. As a warm-up, Sharp Brains‘ new puzzle master offers a brainteaser entitled ‘Party For Polyglots’. More demanding are two entries from MathNotations, aimed at calculus students: the first concerning properties of the ellipse, the second, more advanced post is on exploring infinite series. We close with the most amibitious material, the Unapolagetic Mathematician‘s examination of the knot colouring problem: for the background, see here; this week’s submission explains what’s going on. I feel that I had an unfair advantage since my flatmate is a knot-theorist; I’m often intrigued by the techniques applied to this field. Plus I rarely get to draw pictures during my own research!

Thanks to all who submitted content, and to those of you reading this! The next stop for the carnival is Nonoscience, on the 4th of May.

Formal Analysis of Cryptographic Protocols

(this post merges several previous ones.)

I attended an Informatics postgraduate course, Formal Analysis of Cryptographic Protocols during the second semester of 2006/07. The full notes from lecturer Sibylle Fröschle can be found here, but I also tried to make some more accessible summaries of my own, which subsequently found their way to E2.

I actually wrote a piece on Massey-Omura encryption before the course started, but it’s of a similar theme. The presentation is mostly in lay terms (featuring the usual cast of Alice, Bob, Eve and Mallory posting boxes to each other), but the mathematics of the three-pass system is covered towards the end. Specifically, I outline how factorisation isn’t a suitable technique but (assuming it’s as hard as everyone suspects) the discrete logarithm problem is.

My interest in this course came from a number of modules in formal logic I pursued as an undergraduate, and I’m often fascinated by ideas in this field. There are natural connections between cryptographic protocol analysis and string rewriting, and with enough freedom one can emulate general models of computation such as Turing machines by protocols. One particularly neat side effect of this is that it allows an elegant proof of the undecidability of the secrecy problem: namely, given a protocol and a secret message, is there a decision procedure to establish whether an intruder can learn the secret? By reducing to Post’s Correspondence Problem, it can be shown that the answer is no! Of course, this raises issues of intruder strength and ‘honest’ protocols; here’s the article.

Next up were a few protocols and attacks against them: Authentication sets notation and discusses one-way identity verification such as central locking for cars. The challenge-response protocol and the replay attack are included. Mutual authentication demonstrates the reflection attack on running challenge-response in both directions; three-pass mutual authentication is introduced to resolve this (at the price of some extra complexity).

Finally, I covered the type flaw attack; a particularly devious vulnerability that is invisible to many formal analytic techniques.