---
# System prepended metadata

title: '[CH] How Is Your Math 2'
tags: [Chinese, Crypto, Writeup]

---

# [EN] How Is Your Math 2
###### tags: `Writeup` `Crypto` `English`
> [name=Curious]

## Train of Thought & Solution
From `chal.py`, it can be inferred that `n = (3 * (p - 17) ** 2 - 15 * p + l) * p`, where `l` is not large. Therefore, we can start by attempting to find `p` from the root of `(3 * (p - 17) ** 2 - 15 * p) * p - n` and decreasing `p` gradually, until `n - (3 * (p - 17) ** 2 - 15 * p) * p` becomes a multiple of `p`. Afterward, it is necessary to verify whether both `(3 * (p - 17) ** 2 - 15 * p + l)` and `p` are prime numbers to complete this problem.

Solve Script :
```python=
from sage.all import *
from Crypto.Util.number import isPrime, long_to_bytes

n ,e = (3969984505403119774371755569557425392427590139465803501525601217847265596901386645668202365182528194509244221289088321624570882510050693572406978847408990905844146406568190136848581725588559583717320750532024607805706853739824105343396747529696643055348650158896881443246763356236405239999749122538123879808445531306090478750807146043088460657401527005306926981268335507470535931566142794298200795364649235222113072993441700451797207404344467668255354849730970023, 65537)
c = 1322605148927340848146139420813369906746923681749058626380709421609676564566263218802903868306770053970643034651151142971393992156063710824218132804846948965188729079332091006096805842476803868973521815440243711118847241559975293485803491671350396845976338964999268365522138302733716942218777094957185278175755696051191072951682864550881018804515023967248576602921620891299047414398133722257148063072522359384499002415007953879402417268906832123487251219848135096

x = var('x')
p = (3 * (x - 17) ** 2 - 15 * x) * x - n

p_test = int(solve(p, x)[2].rhs())

while True:
    lp = n - (3 * (p_test - 17) ** 2 - 15 * p_test) * p_test
    if (lp % p_test) == 0:
        p = p_test
        q = 3 * (p_test - 17) ** 2 - 15 * p_test + (lp // p_test)
        break
    p_test -= 1

assert isPrime(p) and isPrime(q)

d = pow(e, -1, (p - 1) * (q - 1))
m = pow(c, d, n)

print(long_to_bytes(m))
```

{%hackmd M1bgOPoiQbmM0JRHWaYA1g %}
