Applied Mathematics For Reversers I: An Introduction Into Modular Arithmetics and Where To Use It. by Haldir (RET)
Applied Mathematics For Reversers I: An Introduction Into Modular Arithmetics and Where To Use It. by Haldir (RET)
a = 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
m=7
r=123456012345601
Ain't hard, is it ?
a == b (mod m)
A example could be m = 7, a = 8 , b = 15
In our example we see both values have a remainder of 1.
Multiplication and Addition can be also used as an operator for modulo:
a == b (mod m) AND c == d (mod m)
Let's say: The Greatest Common Divisor GCD(c,m) = 1 (means their biggest
common divisor is 1)
GCD of two primes is always 1.
those m_x[] Arrays can be displayed as a 5*5 Matrix (A) and our mod2 Array
is a Vector of Length 5 (x)
our wanted Array mod1 can be also displayed as Vector of Length 5 (b)
Now: A*b = x
You can solve that using the known Gaussian Elimination algorithm or
several other methods ( i won't discuss these here )
Now what happens if the whole System is "modulo" (means using modular
Arithmetics)
Well if the Matrix is again 5*5, both Vectors are 5 long, we can solve that like
our first example
(No differences, except you need to do mod m each step)
Probably you're wondering why the hell i'm writing about it again:
If we have our second problem and the Result Vector is not of Length 5 but
Length 3 (again the missing 2 values)
We again have our 3*3 Matrix, we can use, but we can now resize it to a 5*5
Matrix. HOW ?
We just use our old 5*5 Matrix (or we can alternatively, for easier
calculations, replace the last two rows with 0
and just place a 1 in the appropriate diagonal value A[i][i], because for the
Gaussian Elimination Algo you need to have
a lower triangular matrix (row echelon form) (that should ring a bell in your
head if you know the Gaussian Elimination)
And we add to our 3 element Vector two more values to a Vector of Length 5.
Well easy for each missing value in the array (in our example we have 2)
there are just m possible Values (because it's all modulo)
so it doesn't matter if you do 500*2000 (mod m) or just 5*20 (mod m), it's
the same as if you would do 10 (mod m).
So what we do is, we again bruteforce the values in our A*b = x system, we
just try every possible value between 0 and m-1
Now we find a solution in max. m^n steps. m being the modulo value and n
is the amount of "missing" vector elements.
In our example it would be F^2 = 15*15 = 255.
Most of you already know Libraries like MiRACL or crypto++, they have the
basic functions for doing crypto mathematics, but what if you need
some more advanced Library for Math Stuff, like our generic modular
Arithmetics. Well you could either use Maple,Matlab etc. or use some
ready made Library for that, there are several out there, like Lidia or GMP,
but most are just for Linux or won't compile correctly with MSVC.
I would recommend NTL (Number Theory Library) by Victor Shoup
(http://shoup.net/ntl/) for solving modular Arithmetics.
It's very fast and easy to use and compiles without problems on about every
C++ Compiler (including MSVC). One thing you need to know is
that NTL does not do A*b = x for Linear Equation Solving, but b*A = x, this is
not the same, so you need to transpose() the Matrix before
using the solve() function.
4.) Resumée
As I showed in this essay, modular Arithmetics is not that hard at all and
might even help us solving some otherwise "impossible" problems.
I hope you'll understand now the basics about it and might even remember
that "two for-loops" might just be a Linear Equation System, if you see
something like this in your next Reversing Project. I might write more essays
about "applied maths for reversers", which probably won't discuss the
basics of cryptography, because you all should read Applied Cryptography :),
but I'll focus more on some special cases, which might be interesting.
Haldir[RET]
12/10/2002