from Crypto.Util.number import * from secret import Curve,gx,gy
# flag = "hgame{" + hex(gx+gy)[2:] + "}"
defison(C, P): c, d, p = C u, v = P return (u**2 + v**2 - c**2 * (1 + d * u**2*v**2)) % p == 0
defadd(C, P, Q): c, d, p = C u1, v1 = P u2, v2 = Q assert ison(C, P) and ison(C, Q) u3 = (u1 * v2 + v1 * u2) * inverse(c * (1 + d * u1 * u2 * v1 * v2), p) % p v3 = (v1 * v2 - u1 * u2) * inverse(c * (1 - d * u1 * u2 * v1 * v2), p) % p return (int(u3), int(v3))
defmul(C, P, m): assert ison(C, P) c, d, p = C B = bin(m)[2:] l = len(B) u, v = P PP = (-u, v) O = add(C, P, PP) Q = O if m == 0: return O elif m == 1: return P else: for _ inrange(l-1): P = add(C, P, P) m = m - 2**(l-1) Q, P = P, (u, v) return add(C, Q, mul(C, P, m))
c, d, p = Curve
G = (gx, gy) P = (423323064726997230640834352892499067628999846, 44150133418579337991209313731867512059107422186218072084511769232282794765835) Q = (1033433758780986378718784935633168786654735170, 2890573833121495534597689071280547153773878148499187840022524010636852499684) S = (875772166783241503962848015336037891993605823, 51964088188556618695192753554835667051669568193048726314346516461990381874317) T = (612403241107575741587390996773145537915088133, 64560350111660175566171189050923672010957086249856725096266944042789987443125) assert ison(Curve, P) and ison(Curve, Q) and ison(Curve, G) e = 0x10001 print(f"eG = {mul(Curve, G, e)}")
from Crypto.Util.number import * from gmpy2 import * defison(C, P): c, d, p = C u, v = P return (u**2 + v**2 - c**2 * (1 + d * u**2*v**2)) % p == 0
defadd(C, P, Q): c, d, p = C u1, v1 = P u2, v2 = Q assert ison(C, P) and ison(C, Q) u3 = (u1 * v2 + v1 * u2) * inverse(c * (1 + d * u1 * u2 * v1 * v2), p) % p v3 = (v1 * v2 - u1 * u2) * inverse(c * (1 - d * u1 * u2 * v1 * v2), p) % p return (int(u3), int(v3))
defmul(C, P, m): assert ison(C, P) c, d, p = C B = bin(m)[2:] l = len(B) u, v = P PP = (-u, v) O = add(C, P, PP) Q = O if m == 0: return O elif m == 1: return P else: for _ inrange(l-1): P = add(C, P, P) m = m - 2**(l-1) Q, P = P, (u, v) return add(C, Q, mul(C, P, m))
P = (423323064726997230640834352892499067628999846, 44150133418579337991209313731867512059107422186218072084511769232282794765835) Q = (1033433758780986378718784935633168786654735170, 2890573833121495534597689071280547153773878148499187840022524010636852499684) S = (875772166783241503962848015336037891993605823, 51964088188556618695192753554835667051669568193048726314346516461990381874317) T = (612403241107575741587390996773145537915088133, 64560350111660175566171189050923672010957086249856725096266944042789987443125) #(u**2 + v**2 - c**2 * (1 + d * u**2*v**2)) % p == 0 t1 = (P[0]**2+P[1]**2-Q[0]**2-Q[1]**2)*(S[0]**2*S[1]**2-T[0]**2*T[1]**2) t2 = (S[0]**2+S[1]**2-T[0]**2-T[1]**2)*(P[0]**2*P[1]**2-Q[0]**2*Q[1]**2) t3 = (S[0]**2+S[1]**2-Q[0]**2-Q[1]**2)*(P[0]**2*P[1]**2-T[0]**2*T[1]**2) t4 = (P[0]**2+P[1]**2-T[0]**2-T[1]**2)*(S[0]**2*S[1]**2-Q[0]**2*Q[1]**2) p = GCD(t1-t2,t3-t4) c2d = ((P[0]**2+P[1]**2-Q[0]**2-Q[1]**2)*(inverse((P[0]**2*P[1]**2-Q[0]**2*Q[1]**2),p)))%p c2 = (P[0]**2+P[1]**2-c2d*(P[0]**2*P[1]**2))%p c = pow(c2,(p+1)//4,p) d = (c2d*(inverse(c2,p)))%p Curve = c,d,p
e = 0x10001 order = 67943764351073247630101943221474884302071957392340923189748226436548954126268 k = inverse(e,order) eG = (40198712137747628410430624618331426343875490261805137714686326678112749070113, 65008030741966083441937593781739493959677657609550411222052299176801418887407) G = mul(Curve, eG, k) print(G) assert (mul(Curve, G, e)==eG) flag = "hgame{" + hex(G[0]+G[1])[2:] + "}" print(flag)
defGCD(a, b): print(a.degree(), b.degree()) q, r = a.quo_rem(b) if r == 0: return b R00, R01, R10, R11 = HGCD(a, b) c = R00 * a + R01 * b d = R10 * a + R11 * b if d == 0: return c.monic() q, r = c.quo_rem(d) if r == 0: return d return GCD(d, r)
for i inrange(64): if N%((pbar<<4)+i) == 0: p = (pbar<<4)+i q = N//p print("[+] p =",p) print("[+] q =",q) break phi = (p-1)*(q-1) e=0x10001 d = inverse(e,phi) print(long_to_bytes(pow(c,d,N)))