Applications of MCMC for Cryptography and Optimization | by Rahul Agarwal | Towards Data Science

MCMC is a pretty hard topic to wrap your head around but examples do help a lot.
Last time I wrote an article explaining MCMC methods intuitively. In that article, I showed how MCMC chains could be used to simulate from a random variable whose distribution is partially known i.e. we don’t know the normalizing constant.
I also told how MCMC can be used to Solve problems with a large state space. But didn’t give an example.
I will provide some real-world use cases in this post.
If you don’t really appreciate MCMC till now, I hope I will be able to pique your interest by the end of this blog post.
This post is about understanding MCMC Methods with the help of some Computer Science problems.
I love Cryptography.
Solving puzzles has always been a hobby of mine and this example is something that helped me better grasp a lot of ideas about MCMC.
This problem has also got a great pedigree as this method was suggested by Persi Diaconis- The Mathemagician.
So the problem is:
Someone comes to you with the below text. This text looks like gibberish but actually, this is a secret text that could save millions of lives. He also tells us that it is decrypted using a Substitution Cipher.
XZ STAVRK HXVR MYAZ OAKZM JKSSO SO MYR OKRR XDP JKSJRK XBMASD SO YAZ TWDHZ MYR JXMBYNSKF BSVRKTRM NYABY NXZ BXKRTRZZTQ OTWDH SVRK MYR AKSD ERPZMRXP KWZMTRP MYR JXTR OXBR SO X QSWDH NSIXD NXZ KXAZRP ORRETQ OKSI MYR JATTSN XDP X OXADM VSABR AIJRKORBMTQ XKMABWTXMRP MYR NSKPZ TRM IR ZRR MYR BYATP XDP PAR MYR ZWKHRSD YXP ERRD ZAMMADH NAMY YAZ OXBR MWKDRP MSNXKPZ MYR OAKR HAVADH MYR JXTIZ SO YAZ YXDPZ X NXKI XDP X KWE XTMRKDXMRTQ XZ MYR QSWDH NSIXD ZJSFR YR KSZR XDP XPVXDBADH MS MYR ERP Z YRXP ZXAP NAMY ISKR FADPDRZZ MYXD IAHYM YXVR ERRD RGJRBMRP SO YAI
Could you decrypt it?
Let us apply our newly found powers of MCMC.
A note about Substitution Cipher: A substitution cipher is when we substitute a letter by another. For example, A becomes I, B becomes C, etc. in the original text.
To create this example, this data has actually been taken from Oliver Twist. We scrambled the data using a random encryption key, which we forgot after encrypting and we would like to decrypt this encrypted text using MCMC Chains. The real decryption key actually is
“ICZNBKXGMPRQTWFDYEOLJVUAHS”
So let us think about this problem for a little bit.
The decryption key could be any 26 letter string with all alphabets appearing exactly once. How many string permutations are there like that? That number would come out to be 26! ~1026 permutations.
That is a pretty large number and thus this problem has a large state space(Essentially a large number of permutations that we might need to parse).
If we go for using a brute force approach maybe our grandchildren or maybe their grandchildren will get the solution. So what could we do?
MCMC Anyone?
We will devise a chain whose states theoretically could be any of these permutations. Then we will:
Start by picking up a random current state.Create a proposal for a new state by swapping two random letters in the current state.Use a Scoring Function which calculates the score of the current state Score_C and the proposed State Score_P.If the score of the proposed state is more than the current state, Move to Proposed State.Else flip a coin which has a probability of Heads Score_P/Score_C. If it comes heads move to the proposed State.Repeat from 2nd.
Start by picking up a random current state.
Create a proposal for a new state by swapping two random letters in the current state.
Use a Scoring Function which calculates the score of the current state Score_C and the proposed State Score_P.
If the score of the proposed state is more than the current state, Move to Proposed State.
Else flip a coin which has a probability of Heads Score_P/Score_C. If it comes heads move to the proposed State.
Repeat from 2nd.
If we get lucky we may reach a steady state where the chain has the stationary distribution of the needed states and the state that the chain is at could be used as a solution.
The Main thing we need to worry about here is the Scoring Function. How do we calculate if a proposal is a good proposal?
Remember the hill example from my last post?
We want to move to the highest hill and stay there for a long time. We should use a scoring function for each state(Decryption key) which assigns a positive score to each decryption key.
This score intuitively should be more if the encrypted text looks more like actual English if decrypted using this decryption key.
So how can we quantify such a function?
English has a particular structure to it. We assume that the number of times a certain pair of alphabets occur together may follow some particular pattern. Thus “TH” is more probable of occurring then “ZF”.
We will check a long text and calculate these statistics.
We will see how many times one alphabet comes after another in a legitimate long text like War and Peace. The choice of text here is not that important.
For example, we want to find out how many times does ‘BA’ appears in the text or how many times ‘TH’ occurs in the text.
For each pair of characters β1 and β2 (e.g. β1 = T and β2 =H), we let R( β1,β2) record the number of times that specific pair(e.g. “TH”) appears consecutively in the reference text.
Similarly, for a decryption key x, we let Fx(β1,β2) record the number of times that pair appears when the ciphertext is decrypted using the decryption key x.
We then Score a particular decryption key x using:
This function can be thought of as multiplying, for each consecutive pair of letters in the decrypted text, the number of times that pair occurred in the reference text.
Intuitively, the score function is higher when the pair frequencies in the decrypted text most closely match those of the reference text, and the decryption key is thus most likely to be correct.
To make our life easier with calculations, we will calculate log(Score(x))
Let us start working through the problem step by step.
First of all, let us encrypt some text to create our example. In this cruel world, we have to do everything ourselves.
We might want to start by creating some statistics for our scoring function and the scoring function itself.
We also would like to have a function to create proposal states and random coin toss.
Finally, Here is our MCMC Function which runs using Metropolis-Hastings Algorithm.
Let us now run the whole code using all the functions above.
Output:
This chain converges around the 2000th iteration and we are able to unscramble the code.
That’s awesome!
Now as you see the MCMC Key found is not exactly the encryption key. So the solution is not a deterministic one, but we can see that it does not actually decrease any of the value that the MCMC Methods provide.
Now let us help Bilbo.
So you have Bilbo Baggins who goes to Smaug’s Lair.
He finds M treasures.
Each treasure has some Weight and some Gold value.
But the problem is that Bilbo cannot really take all of that.
He could only carry a certain Maximum Weight.
But being a smart hobbit, he wants to Maximize the value of the treasures he takes.
Given the values for weights and value of the treasures and the maximum weight that Bilbo could carry, could you find a good solution?
This is known as the Knapsack Problem in Computer Science.
So in this problem, we have a 1xM array of Weight Values W, Gold Values G, and a value for the maximum weight w_MAX that Bilbo can carry.
We want to find out a 1xM array X of 1’s and 0’s, which holds whether Bilbo carries a particular treasure or not.
This array needs to follow the constraint:
WX < w_MAX
i.e. the total weight Bilbo Carries is less than Max Weight.
and we want to maximize
GX
i.e. the total Gold value for a particular state X.
Now as earlier this problem also has a large state space. If we try doing it by Brute force, the number of permutations we need to take is 2^M. We can see it could get quite big.
As previously this is where we can use MCMC methods.
So lets first discuss as to how we will create a proposal from a previous state.
Pick a random index from the state and toggle the index value.Check if we satisfy our constraint. If yes this state is the proposal state.Else pick up another random index and repeat.
Pick a random index from the state and toggle the index value.
Check if we satisfy our constraint. If yes this state is the proposal state.
Else pick up another random index and repeat.
We also need to think about the Scoring Function.
We need to give high values to states with high gold value. We will use:
We give exponentially more value to a higher score. The Beta here is a +ve constant. (T stands for Transpose)
But how to choose β?
If β is big we will give a very high score to good solutions and the chain will not be able to try new solutions as it can get stuck in local optima.
If we give a small value the chain will not converge to very good solutions.
So we use an Optimization Technique called Simulated Annealing i.e. we will start with a small value of β and increase as no of iterations go up.
That way the chain will explore in the starting stages and stay at the best solution in the later stages.
Now, we have everything we need to get started.
Let us start by creating a proposal function and the score function.
And the MCMC program.
Running the Main program:
OUTPUT________________________________________________________________MCMC Solution is : [0, 0, 0, 1, 1, 0, 0, 1, 1, 1, 1, 0, 0, 0] with Gold Value: 2435
Now I won’t say that this is the best solution.
The deterministic solution using DP will be the best for such use case but sometimes when the problems get large, having such techniques at disposal becomes invaluable.
In this post, I tried to solve two problems in computer science using MCMC.
Cryptography is itself a huge subject with many applications. The Knapsack problems appear in real-world decision-making processes in a wide variety of fields, such as finding the least wasteful way to cut raw materials, selection of investments and portfolios, selecting data chunks in Internet Download Manager and other optimizations.
Both these problems had a large state space and were not possible to solve using brute force.
So what do you think about MCMC Methods?
Also, If you find any good applications or would like to apply these techniques to some area, I would really be glad to know about them and help if possible.
You can find the whole code on GitHub or Google Colab.
One of the newest and best resources that you can keep an eye on is the Bayesian Methods for Machine Learning course in the Advanced machine learning specialization
I am going to be writing more of such posts in the future too. Let me know what you think about the series. Follow me up at Medium or Subscribe to my blog to be informed about them. As always, I welcome feedback and constructive criticism and can be reached on Twitter @mlwhiz.
Introduction to Probability Joseph K Blitzstein, Jessica HwangWikipedia
Introduction to Probability Joseph K Blitzstein, Jessica Hwang