计算数论学习记录,如有错误,欢迎评论区指正。
使用 n \sqrt{n} n的简单连分数展开寻找或组合得到同余式: x 2 ≡ y 2 ( m o d p ) x^2\equiv y^2\ (mod\ p) x2≡y2 (mod p)。
n为待分解整数,算法步骤
计算 n \sqrt{n} n的简单连分数展开,同时计算:
P 0 = a 0 , P 1 = a 0 a 1 + 1 , P k = a k P k − 1 + P k − 2 P_0 = a_0,\ P_1 = a_0a_1+1,\ P_k = a_k P_{k-1}+P_{k-2} P0=a0, P1=a0a1+1, Pk=akPk−1+Pk−2
Q 0 = 1 , Q 1 = a 1 , Q k = a k Q k − 1 + Q k − 2 Q_0 = 1,\ Q_1 = a_1,\ Q_k = a_k Q_{k-1}+Q_{k-2} Q0=1, Q1=a1, Qk=akQk−1+Qk−2
W k = P k 2 − N Q k 2 W_k = P_k^2 - NQ_k^2 Wk=Pk2−NQk2
寻找所需同余式
利用 W k = ( k + [ n ] ) 2 W_k = (k+[\sqrt{n}])^2 Wk=(k+[n])2寻找平方同余式
简单版
计算 k = − 5 , − 4 , − 3 , − 2 , − 1 , 0 , 1 , 2 , 3... k=-5,-4,-3,-2,-1,0,1,2,3... k=−5,−4,−3,−2,−1,0,1,2,3...时 W k W_k Wk的值,选择分解基并从 W k W_k Wk寻找可以凑成平方项的值,得到 ∏ ( [ n ] + n i ) 2 ≡ W k 1 ⋅ W k 2 ⋅ W k 3 . . . \prod{([\sqrt{n}] + n_i)}^2\equiv W_{k1}\cdot W_{k2}\cdot W_{k3}... ∏([n]+ni)2≡Wk1⋅Wk2⋅Wk3... ,后者是个平方元。k在0附近选择,范围可大可小。
复杂版
同样,想法设法构造 x 2 ≡ y 2 ( m o d n ) x^2\equiv y^2 \ (mod \ n) x2≡y2 (mod n)
思路:
选择一个整数 θ ∉ Q \theta \notin Q θ∈/Q,由其产生代数数域 K = Q ( θ ) K=Q(\theta) K=Q(θ), θ \theta θ的极小多项式为 f ( x ) f(x) f(x)
寻找整数m使得 f ( m ) = k n f(m)=kn f(m)=kn,则可以定义同态环,K得阶 Z [ θ ] → Z n Z[\theta]\rightarrow Z_n Z[θ]→Zn: ϕ ( ∑ i = 0 n − 1 a i θ i = ∑ i = 0 n − 1 a i m i ( m o d n ) \phi(\sum_{i=0}^{n-1}a_i\theta^i=\sum_{i=0}^{n-1}a_im_i \ (mod \ n) ϕ(∑i=0n−1aiθi=∑i=0n−1aimi (mod n)
寻找集合S, S ⊂ { ( a , b ) ∈ Z × Z ∣ g c d ( a , b ) = 1 } S\subset \{(a, b) \in Z\times Z | gcd(a, b) =1 \} S⊂{(a,b)∈Z×Z∣gcd(a,b)=1},S满足
∏ ( a , b ) ∈ S ( a + b m ) = x 2 \prod_{(a,b)\in S}(a+bm)=x^2 ∏(a,b)∈S(a+bm)=x2
∏ ( a , b ) ∈ S ( a + b θ ) = α 2 \prod_{(a,b)\in S}(a+b\theta)=\alpha^2 ∏(a,b)∈S(a+bθ)=α2
则 α 2 ≡ x 2 \alpha^2\equiv x^2 α2≡x2
如何构造 f ( x ) f(x) f(x)?取 m = [ n 1 d ] m=[n^{\frac{1}{d}}] m=[nd1],将n写成m进制, n = C d m d + . . . + C 1 m + C 0 n = C_dm^d+...+C_1m+C_0 n=Cdmd+...+C1m+C0,
令 f ( x ) = C d x d + . . . + C 1 x + C 0 f(x)=C_dx^d+...+C_1x+C_0 f(x)=Cdxd+...+C1x+C0。
若 f ( x ) f(x) f(x)可约, f ( x ) = g ( x ) h ( x ) f(x)=g(x)h(x) f(x)=g(x)h(x),则 h = g ( m ) h ( m ) h=g(m)h(m) h=g(m)h(m),即得到n得非平凡因子。
f ( x ) = x 2 + 1 , x 0 = 2 , x k = f ( x k − 1 ) f(x)=x^2 + 1, \ x_0=2,\ x_k = f(x_{k-1}) f(x)=x2+1, x0=2, xk=f(xk−1)
计算 gcd ( x 2 k − x k , N ) , k = 1 , 2 , 3... \gcd(x_{2k} - x_k, N), \ k=1,2,3... gcd(x2k−xk,N), k=1,2,3...
变种:使用其他 f ( x ) f(x) f(x),计算 gcd ( x g ( t ) − x t , N ) \gcd(x_{g(t)}-x_t, N) gcd(xg(t)−xt,N)而不是 x 2 k − x k x_{2k}-x_k x2k−xk
计算 gcd ( 2 ( k ! ) − 1 , N ) \gcd(2^{(k!)}-1, N) gcd(2(k!)−1,N),k为自然数。可以选择前k个素数替换k的阶乘。
选择大于2得整数A构造Lucas序列
V 0 = 2 , V 1 = A , V k = A V k − 1 − V k − 2 V_0=2,\ V_1=A,\ V_k=AV_{k-1}-V_{k-2} V0=2, V1=A, Vk=AVk−1−Vk−2
计算多个 gcd ( V M − 2 , N ) \gcd(V_M - 2, N) gcd(VM−2,N)的值,其中M为连续阶乘1!, 2!, 3!, 4!
,若结果不为1和N,则找到一个因子。
V k ! ( A ) = V k ( V ( k − 1 ) ! ( A ) ) V_{k!}(A) = V_k(\ V_{(k-1)!}(A)\ ) Vk!(A)=Vk( V(k−1)!(A) )
例如,A=9,则
V 1 ( 9 ) = V 1 ! ( 9 ) = 9 V_{1}(9) = V_{1!}(9) = 9 V1(9)=V1!(9)=9
V 2 ( 9 ) = V 2 ! ( 9 ) = 79 V_{2}(9) = V_{2!}(9) = 79 V2(9)=V2!(9)=79
V 3 ( 79 ) = V 3 ! ( 9 ) = 41886 V_{3}(79) = V_{3!}(9) = 41886 V3(79)=V3!(9)=41886
V 4 ( 41886 ) = V 4 ! ( 9 ) = 79378 V_{4}(41886) = V_{4!}(9) = 79378 V4(41886)=V4!(9)=79378
V 5 ( 79378 ) = V 5 ! ( 9 ) = 1934 V_{5}(79378) = V_{5!}(9) = 1934 V5(79378)=V5!(9)=1934
V 6 ( 1934 ) = V 6 ! ( 9 ) = 10582 V_{6}(1934) = V_{6!}(9) = 10582 V6(1934)=V6!(9)=10582
V 7 ( 10582 ) = V 7 ! ( 9 ) = 84241 V_{7}(10582) = V_{7!}(9) = 84241 V7(10582)=V7!(9)=84241
V 8 ( 84241 ) = V 8 ! ( 9 ) = 93973 V_{8}(84241) = V_{8!}(9) = 93973 V8(84241)=V8!(9)=93973
V 9 ( 93973 ) = V 9 ! ( 9 ) = 91645 V_{9}(93973) = V_{9!}(9) = 91645 V9(93973)=V9!(9)=91645
选择椭圆曲线, y 2 = x 3 + a x 2 + b y^2=x^3+ax^2+b y2=x3+ax2+b,选择曲线上的点 P ( x , y ) P(x, y) P(x,y),选择阶乘或几个素因子的最小公倍数作为 K = l c m ( 2 , 3 , 5 , 7... ) K=lcm(2,3,5,7...) K=lcm(2,3,5,7...),计算 K P KP KP,当计算过程中λ无解的时候,计算分母 m 2 m_2 m2与N的公因子 gcd ( m 2 , N ) \gcd(m_2, N) gcd(m2,N) 即得到一个非平凡因子。
K P KP KP的计算主要依赖椭圆曲线上的加法操作:
P 1 = ( x 1 , y 1 ) , P 2 = ( x 2 , y 2 ) P_1=(x_1,y_1), P_2=(x_2, y_2) P1=(x1,y1),P2=(x2,y2), P 1 + P 2 = P 3 P_1+P_2=P_3 P1+P2=P3
x 3 = λ 2 − x 1 − x 2 ( m o d n ) x_3=\lambda^2 - x1 - x2 \ (mod\ n) x3=λ2−x1−x2 (mod n)
y 3 = λ ( x 1 − x 3 ) − y 1 ( m o d n ) y_3=\lambda(x_1-x_3)-y_1 \ (mod\ n) y3=λ(x1−x3)−y1 (mod n)
若 P 1 = P 2 P_1=P_2 P1=P2,则 λ = 3 x 1 2 + a 2 y 1 ( m o d n ) \lambda=\frac{3x_1^2+a}{2y_1} \ (mod\ n) λ=2y13x12+a (mod n)
若 P 1 ≠ P 2 P_1\ne P_2 P1=P2,则 λ = y 1 − y 2 x 1 − x 2 ( m o d n ) \lambda=\frac{y_1-y_2}{x_1-x_2} \ (mod\ n) λ=x1−x2y1−y2 (mod n)
分数取模转换为一次同余式利用欧几里得算法求解: x = b a m o d n ⇒ b ≡ a x m o d n x=\frac{b}{a} \ mod\ n \Rightarrow b \equiv ax \ mod \ n x=ab mod n⇒b≡ax mod n。
9x=5 (mod 25) => 9x + 25 * (-k) = 5
欧几里得不断取余数gcd(25, 9):
25 = 9 * 2 + 7
9 = 7 + 2
7 = 2 * 3 + 1
2 = 2 * 1
从而
1 = 7 - 2 * 3 = (25 - 9 * 2) - (9 - 7) * 3
= 25 - 9 * 2 - (9 - (25 - 9 * 2)) * 3
= 25 - 9 * 2 - (9 * 3 - 25) * 3
= 25 * 4 - 9 * 11
从而得到
25 * 4 + 9 * (-11) = 1
两边同乘以5有
25 * 20 + 9 * (-55) = 5
故 x = (-55) mod 25
x = 20
验证得 9*20 = 180 = 25 * 7 + 5 = 5 mod 25
计算 x = log a y ⇒ y = a x ( m o d n ) x=\log_ay \Rightarrow y=a^x\ (mod\ n) x=logay⇒y=ax (mod n)
x = [ n ] x=[\sqrt{n}] x=[n]
小步
S = { ( y , 0 ) , ( y a , 1 ) , ( y a 2 , 2 ) . . . ( y a s − 1 , s − 1 ) m o d n } S=\{ (y, 0), (ya, 1), (ya^2, 2) ... (ya^{s-1}, s-1)\ mod\ n \} S={(y,0),(ya,1),(ya2,2)...(yas−1,s−1) mod n}
大步
T = { ( a s , 1 ) , ( a 2 s , 2 ) , ( a 3 s , 3 ) , . . . , ( a s 2 , s ) m o d n } T=\{ (a^s, 1), (a^{2s}, 2), (a^{3s}, 3), ..., (a^{s^2}, s) \ mod\ n \} T={(as,1),(a2s,2),(a3s,3),...,(as2,s) mod n}
之后按照第一项排序,寻找 y a r = a t s ya^r = a^{ts} yar=ats,然后计算 x = t s − r x=ts-r x=ts−r即得到结果。
计算 x ≡ log a b m o d q x\equiv \log_{a}b \ mod \ q x≡logab mod q
首先得到q-1素因子分解, q − 1 ≡ ∏ i = 1 k p i α i q-1\equiv \prod_{i=1}^{k}p_i^{\alpha_i} q−1≡∏i=1kpiαi
针对每个素因子的幂 p i α i p_i^{\alpha_i} piαi,简写为 p α p^\alpha pα,将x分解为p进制形式,如下:
通过将x分解为p进制的形式计算同余式: x m o d p α = x 0 + x 1 p + x 2 p 2 + . . . + x α − 1 p α − 1 x\ mod\ p^\alpha = x_0 + x_1 p+x_2 p^2+...+x_{\alpha-1}p^{\alpha-1} x mod pα=x0+x1p+x2p2+...+xα−1pα−1
通过中国剩余定理,将每一个 p i α i p_i^{\alpha_i} piαi对应的同余式组合,即可得到x
求解 x 2 ≡ a ( m o d p ) x^2\equiv a \ (mod \ p) x2≡a (mod p),步骤
另一种方法:
计算 x 2 ≡ a ( m o d p ) x^2\equiv a \ (mod \ p) x2≡a (mod p)