密码学常用工具 引自crypto常用工具 | Lazzaro (lazzzaro.github.io) 
1.gmpy2 1 2 3 4 5 6 7 8 9 10 from  gmpy2 import  *mpz(n)	 mpfr(x)	 d = invert(e,n) 	 c = powmod(m,e,n)	 is_prime(n)	  gcd(a,b)  	 gcdext(a,b)  	 iroot(x,n) 	 
2.sympy 1 2 3 4 5 6 7 8 from  sympy import  *prime(n)   isprime(n)   primepi(n)   nextprime(n)   prevprime(n)   nthroot_mod(c,e,p,all_roots=True )   
3.Sage 1 2 3 4 5 6 R.<X> = PolynomialRing(Zmod(n)) 
4.数论 1 2 3 4 5 6 7 8 prime_pi(n)   divisors(n)   number_of_divisors(n)   factor(n)   euler_phi(n)   two_squares(n)    three_squares(n)   four_squares(n)   
5.多项式 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 f.subs({x:x1})  f.univariate_polynomial()  f.univariate_polynomial().roots()  f.coefficients()  f.list ()  f.monic()  x = PolynomialRing(RationalField(), 'x' ).gen() f = (x^3  - 1 )^2 -(x^2 -1 )^2  f.factor() x, y = PolynomialRing(RationalField(), 2 , ['x' ,'y' ]).gens() f =  (9 *y^6  - 9 *x^2 *y^5  - 18 *x^3 *y^4  - 9 *x^5 *y^4  + 9 *x^6 *y^2  + 9 *x^7 *y^3  + 18 *x^8 *y^2  - 9 *x^11 ) f.factor() x = PolynomialRing(RationalField(), 'x' ).gen() f = 3 *x^3  + x g = 9 *x*(x+1 ) f.gcd(g) R.<x,y,z> = PolynomialRing(RationalField(), order='lex' ) f = 3 *x^2 *(x+y) g = 9 *x*(y^2  - x^2 ) f.gcd(g) PR = PolynomialRing(GF(2 ),'x' ) R.<x> = GF(2 ^2049 ) pc = R.fetch_int(xx)  xx = R(PR(pc)).integer_representation()  
6.矩阵 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 A = matrix(ZZ, [[1 ,1 ],[0 ,4 ]]) A.nrows()  A.ncols()  A.transpose()  A.inverse() 或 A^(-1 )  A.rank()  A.det()  A.stack(vector([1 ,2 ]))  A.insert_row(1 , vector([1 ,2 ]))  A.change_ring(QQ)  A.solve_left(B) 或 A/B  A.solve_right(B) 或 A\B  A.left_kernel()  A.right_kernel()  A.LLL()  A.multiplicative_order()  zero_matrix(2 ,3 )  identity_matrix(2 ,3 )  block_matrix(QQ,[[A,zero_matrix(n,1 )],[matrix(b),matrix([1e-16 ])]])  
7.解方程  x+y=10
xy=21
1 2 var('x y' ) solve([x+y==10 ,x*y==21 ],[x,y]) 
8.解线性方程组 AX=B
1 2 3 4 5 6 A = Matrix([[1 ,2 ,3 ],[3 ,2 ,1 ],[1 ,1 ,1 ]])  Y = vector([0 ,-4 ,-1 ])  X = A.solve_right(Y) A \ Y 
9.中国剩余定理 x≡2(mod3)
x≡3(mod5)
x≡2(mod7)
1 2 3 4 5 6 7 8 9 10 11 crt([2 ,3 ,2 ],[3 ,5 ,7 ]) def  chinese_remainder (modulus, remainders ):    Sum = 0      prod = reduce(lambda  a, b: a*b, modulus)     for  m_i, r_i in  zip (modulus, remainders):         p = prod // m_i         Sum += r_i * (inverse_mod(p,m_i)*p)     return  Sum % prod chinese_remainder([3 ,5 ,7 ],[2 ,3 ,2 ])  
10.离散对数  a ** x ≡ b (mod n)
1 2 3 4 5 6 7 8 9 10 11 x = discrete_log(mod(b,n),mod(a,n))  R = Integers(99 ) a = R(4 ) b = a^9  b.log(a) x = int (pari(f"znlog({int (b)} ,Mod({int (a)} ,{int (n)} ))" )) x = gp.znlog(b, gp.Mod(a, n)) 
11.整数域椭圆曲线  y ** 2=x ** 3+a * x+ b (mod n)
输出所有整数点
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 F = GF(7 ) print (F.order())E = EllipticCurve(F,[0 ,0 ,0 ,2 ,3 ]) G = E.gens()[0 ] q = E.order() allPoints = E.points() P = E(2 ,1 ) P.xy() G = E.lift_x(ZZ(getPrime(64 )))   
12.解模方程  a * x ** 2+b * x + c ≡ d (mod p)
1 2 3 4 P.<x> = PolynomialRing(Zmod(p)) f = a * x^2  + b * x + c - d x = f.monic().roots() print (x)
13.解方程组 N=pq
φ=(p−1)(q−1)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 P.<p, q> = PolynomialRing(ZZ) def  solve (f1, f2 ):	g = f1.resultant(f2, q) 	roots = g.univariate_polynomial().roots() 	if  len (roots) == 0 : 		return  False  	p_ = abs (roots[0 ][0 ]) 	q_ = abs (roots[1 ][0 ]) 	return  (min (p_, q_), max (p_, q_)) N =  phi =  f1 = (N + 1 ) - phi - p - q f2 = N - p*q p, q = solve(f1, f2) (p, q) 
14.结式  1 2 3 4 5 6 7 8 9 10 11 12 13 from  sage.matrix.matrix2 import  Matrixdef  resultant (f1, f2, var ):    return  Matrix.determinant(f1.sylvester_matrix(f2, var)) n =  P.<k,t2,t3,d> = PolynomialRing(Integers(n)) f1 = s1*k - h - r*d f2 = s2*(k+t2) - h - r*d f3 = s3*(k+t3) - h - r*d h1 = resultant(f1, f2, d) h2 = resultant(f1, f3, d) g1 = resultant(h1, h2, k) roots = g1.univariate_polynomial().roots()