Home
Writeups Misc About
Lo-Hi Card Game

Lo-Hi Card Game

For the challenge, we need to gain enough money (self.dollars >= 130), we should obtain the flag. Observing the PRNG, we can see that the PRNG is a Linear Congruential Generator, with properly randomized parameters.

Let's first discuss how to break the LCG. Denote the multiplier mul as M, increment inc as I, modulo as N. We can easily retrieve M,I after obtaining three numbers from the LCG, denoted by A,B,C. Indeed, we have:

B=MA+ImodN
C=MB+ImodN
CB=M(BA)modN
M=(CB)(BA)1modN

M should be retrieved, as we know A,B,C. With M figured out, I can be retrieved by I=BMAmodN

Now, the task is to retrieve 3 number from the LCG.

The "shuffle" is done using the rebase function, which essentially converts the state into a Base-52 representation. The cards are drawn with replacement. From local testing, we observe that every shuffled deck should have 11 cards in it, given the division of 52 every step, and 60 bits of randomly generated state.

Hence, we can recover a single RNG state by seeing all hands in a shuffle and interpreting them as base 52. Three states will take 33 tries (plus 1 for retrieving the last card of the third state). However, as we do not have any information on the next RNG state when we are in the process of retrieving the three states, we have to do the choices randomly, or based on some heuristics.

For a better guarantee that we will retrieve enough cards to reconstruct the three RNG states, I use the simple heuristics of picking the choice where there are more possibilities. This means, when we are holding "Four of Spades", the choice will be "higher", as there are more cards with higher values than "Four of Spades" than cards with lower values. Doing this keeps the casino balance above 0 after 34 rounds, which will provide enough information to retrieve three states.

Other than that, the challenge is just a matter of writing everything out and squash bugs. I would recommend local tests, for full information of the PRNG states and the shuffled deck, which would be very useful in debugging.

Python Implementation: