#!/usr/bin/env sage import random from Crypto.Util.number import * from gmpy2 import iroot
flag = b''
N = 100 yes = 0 for _ inrange(N): k = 3 d = k/(2*(k+1)) ns = [] pqs = [] es = [] zs = []
for i inrange(3): p = getPrime(512) q = getPrime(512) if p < q: tmp = p p = q q = tmp n = p*q ns.append(n) pqs.append((p,q))
n = min(ns) x = random.randint(0,int(n^(d/2))) x = next_prime(x)
for i inrange(3): p,q = pqs[i][0],pqs[i][1] bound1 = int((p-q)/(3*(p+q)) * x * n ^ 0.25) bound2 = int((p-q)/(3*(p+q)) * x^2 * n ^ 0.25) z = random.randint(bound1,bound2) zs += [z] f = (p-1)*(q-1) e = inverse(x^2,f) * z % f es.append(e)
n = ns pq = pqs e = es z = zs
s = [] k = [] y = [] for i inrange(3): s += [sum(pq[i])] phi = (pq[i][0]-1) * (pq[i][1]-1) assert (x^2 * e[i] - z[i]) % phi == 0 k += [(x^2 * e[i] - z[i]) // phi] y += [z[i] - k[i] * s[i]]
ppq = ppq >> a pmq = pmq >> a p = (ppq + pmq) // 2 p = p >> error print(hex(p)) print(hex(p0 >> (a + error))) if p == p0 >> (a + error): yes += 1 print(p.nbits()) print(Integer(n[0]).nbits())
#!/usr/bin/env sage from sage.allimport * from sage.parallel.multiprocessing_sage import parallel_iter from tqdm import tqdm
defgao(n, ph, pl=1, pbits=512): global tqdm hbits = Integer(ph).nbits() lbits = Integer(pl).nbits() PR = PolynomialRing(Zmod(n), 'x') x = PR.gen() f = ph * 2**(pbits-hbits) + x * 2**lbits + pl f = f.monic() # p:512 ph:262 #roots = f.small_roots(X=2**(pbits-hbits-lbits+1), beta=0.48, epsilon=0.0106) roots = f.small_roots(X=2**(pbits-hbits-lbits+1), beta=0.48, epsilon=0.0103) if roots: pm = Integer(roots[0]) p = ph * 2**(pbits-hbits) + pm * 2**lbits + pl if n % p == 0: q = n // p print() print('p = %d' % p) print('q = %d' % q) return p, q returnNone
defmgao(n, ph, T=16, gmax=262): hbits = Integer(ph).nbits() gbits = gmax - hbits ph = ph << gbits N = 2**gbits for i in tqdm(range(N//T + 1)): pars = [] for j inrange(T): pars += [((n, ph+(T*i+j), 1, 512), {})] res = list(parallel_iter(T, gao, pars)) for r in res: if r[1] != None: return r[1]
ph = 4154105556012159824007632555267171047079134414914023397680651326221752696528 n = 58456238154727772714762362790039415372652580738847549549926175214592421074440425380491278175531057453959583518365006871715668115289674464868754600641087664868445977308497244134179400977293896807231964047365956545629327100737851868274388108150918741474301542596310528990700043925342513137054619092876834352167 mgao(n, ph)
#!/usr/bin/env sage import string # https://tover.xyz/p/n-root-in-F/ from nth import *
e = 8462913 c = 45042826649205831967869785980034342377048541926664036544108272069702081866501394370318117629151408517708467341069558466115205805860156690204194355692872459196902123082567148537856941845388225814307822482217762135547080677443326657146552580523747535577686386312386011950929734955156100305548239483424574706729 p = 7696200979887856341831037613988614280823562431557099321251289719686349578851157595313091133333376255237947592610103860940743200558583228583143108125315529 q = 7595466686419558151205913432118661460574378336265188601308332178368424478703273480167149453312906478797521330243646557948961908036589158891684701262055023
checker = genStrChecker(string.printable, 48) #res = nthRSA_n(c, e, [p, q], checker=checker) res = nthRSA_n(c, e, [p], checker=checker) print(res)