Home
Writeups Misc About
Modular Binomials

Modular Binomials

This problem is the problem I did not manage to solve in NUS Greyhats GreyCTF2022. Although I am pretty close to the solution, there is one trick that I missed that is crucial to this particular type of challenge.

We are given the above system of equations, and the goal is to solve for p and q. For n>0 and coefficients a,b, using Newton's binomial expansion, we have:

(ap+bq)n(ap)n+(bq)nmodN

This result is derived from the fact that the expansion of (ap+bq)n, except for the first and last element in the expansion (namely (ap)n and (bq)n), every other element will have a factor of pq.

The task is to solve this system of equations. The aim is to eliminate one of the variables, in this case I choose p. We can raise the first equation to the power of e2 and the second equation to the power of e1

c1e2((2p+3q)e1)e2=(2p+3q)e1×e2modN
c2e1((5p+7q)e2)e1=(5p+7q)e1×e2modN

From the above result from Newton's binomial expansion, the system of equations now become:

c1e2(2p)e1×e2+(3q)e1×e2modN
c2e1(5p)e1×e2+(7q)e1×e2modN

We want to eliminate p, hence we can do:

5e1×e2c1e210e1×e2pe1×e2+15e1×e2qe1×e2
2e1×e2c2e110e1×e2pe1×e2+14e1×e2qe1×e2

Subtracting the two equations, we have

D=5e1×e2c1e22e1×e2c2e115e1×e2qe1×e214e1×e2qe1×e2

Clearly, qe1×e2 is divisible by q. Hence, as N=pq, we have gcd(N,D)=q.

From q, we can obtain p by doing N/q. Python implementation of the solution: