Opening a linear congruent pseudorandom number generator
For training purposes, I want to hack the simplest RNG.
Even those who do not know about the linear congruent generator or about the RNG in principle can help here, because the question is a misunderstanding of the English text and a little mathematics.
For you in short: There is such a thing as a pseudorandom number generator. i.e. random, if easier. They are "pseudo-random" because they are not random, but they are similar to them. One of the implementations of such a generator:
Xn+1=(aXn+c) mod m
Where Xn is the n
th term of the sequence. a
, c
and m
- constants: a
- multiplier, c
- increment, m
- modulus. X0 – initial value.
My hacking conditions:
- We know that the generator is based on the linear congruent method.
- We don't know
a,
c
andm
. - We can get any members sequences.
Task: define a
,c
,m
(more likely).
I found several ways in the English version. I need either of them or another one that I do not know.
This site solves this by brute force. The only thing is that it does not give the entire sequence, but every second term. Therefore, as far as I understand, you do not need to do PowerMod. Questions about this method are as follows:
- Did I understand correctly, that the second code is implemented because the first one produced two results, and we need one?
- Question on modular arithmetic: How did you get that
c = X2 - ((X1 * a) % m)
(in the first code)? - Why
m < 10*M_START
? - What happens in the second code?
Here here is Plumstead's algorithm. And here is the algorithm of J. Marsagli Please explain who understood, from a mathematical point of view.
Other links :
1 answers
Okay, look.
-
Here the authors of the solution reduced the problem to the previous one. Indeed,
x[n + 2] = a * x[n + 1] + c = a * (a * x[n] + c) + c = (a * a) * x[n] + (a * c + c)
(mod m), so that the problem reduces to cracking a linear congruent generator with coefficientsa' = a * a
andc' = a * c + c
. Therefore, we believe that we have each member of the new sequence, and we find the new coefficientsa'
,c'
, and then we will calculate the old ones from thema
,c
.The first solution was quadratic by
а
,m
(busting all of thema
/m
and checking that they give the first few terms of the sequence correctly), which means it won't work on large modules, so the author gives the second solution, which is linear. - We assume that we know every member of the sequence. Since by the condition
x[2] = a' * x[1] + c'
modulom
, thenc' = x[2] - a' * x[1]
modulom
. - The constant 10 looks random. The minimum possible value of
m
is 3159 (because there is a member of the sequence with the value 3158). Author the solution simply iterates over a certain number of possiblem
. -
The second code is more interesting. To begin with, the author gets rid of
c
, moving on to the differences (for the sequence of differencesy[i] = x[i] - x[i - 1]
, we havey[i + 1] = a * y[i] (mod m)
). Next, he uses the function to find the multiplicative inverse of (that is, for a givenk
it isl
such thatk * l = 1 (mod m)
), based on the extended Euclid algorithm. It exists, ifk
andm
are mutually simple.So,
y[2] = a * y[1] (mod m)
, multiplying this by the inverse ofr
toy[1]
, we geta = y[2] * r
. (If the opposite is not found, we skip this valuem
.) Ifm
was guessed correctly, we should get the correct sequence of differences, which is checked by further code.