def phase2(high_m, n, c,e): R.<x> = PolynomialRing(Zmod(n), implementation='NTL') m = high_m + x mm = m((m^e - c).small_roots()[0]) print(libnum.n2s(int(mm)))
def getFullP(low_p, n): R. < x > = PolynomialRing(Zmod(n), implementation='NTL') p = x * 2 ^ bit + low_p root = (p - n).monic().small_roots(X=2 ^ 128, beta=0.4) if root: return p(root[0]) return None
def phase4(low_d, n, c): maybe_p = [] for k in range(1, 4): p = var('p') p0 = solve_mod([3 * p * low_d == p + k * (n * p - p ^ 2 - n + p)], 2 ^ bit) maybe_p += [int(x[0]) for x in p0] # print(maybe_p) for x in maybe_p: P = getFullP(x, n) if P: break
bit = 486 n = 99233273001596380809501393613886417854988989363311895592445631124571940638105064581031336422148895730117448547059137492090852484233539441265373247940823283633017582469362503632785297924194187912199716752955609363457416190782142095008241313065484612071705711161086869849047664563205898255359661312582650481473 e = 3 c = 175676150266403937224898870626869248307097859453341599800113943191154294552011908698393750389195590199207971365632903719917006078351629939912360175671032635640354766675409868021903917260597989036476083685690139071290022752606720020238530580507331902326179201548484337939338062870542095137303322195300197 d1 = 66155515334397587206334262409257611903325992908874597061630420749714627092070043054020890948099263820078299031372758328060568322822359627510248831960548842374360111368587702677016263988651746874367056185352095585037936034196599496577057035033718066207964292781685419156037246789689219471468151618592221802699
phase4(d1, n, c)
第一个函数getFullP
1 2 3 4 5 6 7
def getFullP(low_p, n): R. < x > = PolynomialRing(Zmod(n), implementation='NTL') p = x * 2 ^ bit + low_p root = (p - n).monic().small_roots(X=2 ^ 128, beta=0.4) if root: return p(root[0]) return None
def phase4(low_d, n, c): maybe_p = [] for k in range(1, 4): p = var('p') p0 = solve_mod([3 * p * low_d == p + k * (n * p - p ^ 2 - n + p)], 2 ^ bit) maybe_p += [int(x[0]) for x in p0] # print(maybe_p) for x in maybe_p: P = getFullP(x, n) if P: break
p0 = solve_mod([3 * p * low_d == p + k * (n * p - p ^ 2 - n + p)], 2 ^ bit)
这行代码使用了SageMath的solve_mod函数,它可以解决模方程。方程的左侧为$3 \cdot p \cdot low_d$,右侧为$p + k \cdot (n \cdot p - p ^ 2 - n + p)$,其中$k$是一个在1到3之间的整数。求解模$2^{bit}$意味着$p$的值将是一个$bit$位二进制数。p0将是所有解的列表,每个解都是一个元组,包含唯一的一个元素$p$的值。
solve_mod函数
SageMath是一款基于Python的数学计算软件,solve_mod()函数是SageMath中的一个模数方程求解函数,它可以求解形如f(x) = 0 (mod m)的模数方程。其中,f(x)是一个关于变量x的多项式,m是模数。该函数的返回值是所有满足方程的x值的列表。在此处,p0 = solve_mod([3 * p * low_d == p + k * (n * p - p ^ 2 - n + p)], 2 ^ bit)表示求解方程3plow_d = p + k*(n*p - p^2 - n + p) (mod 2^bit),其中p是变量,low_d、n、k、bit都是已知数值。函数solve_mod()会返回所有满足方程的p值的列