Skip to content

Commit 47b6705

Browse files
committed
docs: add DL and DLP
1 parent 4ad1377 commit 47b6705

File tree

1 file changed

+68
-5
lines changed

1 file changed

+68
-5
lines changed

docs/ctf/crypto/basis.md

Lines changed: 68 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
title: 数学基础
2+
title: 密码学数学基础
33
---
44

55
## 二次剩余
@@ -39,12 +39,75 @@ $p$ 为合数的讨论较为复杂(NP-hard),这里只讨论 $p$ 为奇素
3939

4040
---
4141

42-
## 循环群
42+
## 离散对数
4343

4444
::fold{expand success title=定义}
45-
设 $G$ 是一个群,若存在 $g \in G$ 使得 $G = \{g^k\mid k\in\mathbb{Z}\}$,则称 $G$ 是一个**循环群**,$g$ 是 $G$ 的一个**生成元**.
45+
1. 设 $G$ 是一个群,若存在 $g \in G$ 使得 $G = \{g^k\mid k\in\mathbb{N}\}$,则称 $G$ 是一个**循环群**,$g$ 是 $G$ 的**生成元**.
46+
2. 对 $G$ 中的任意元素 $y$,都可以写成 $y = g^k$ 的形式,称 $k$ 为 $y$ 在群 $G$ 中的离散对数,简称**对数**.
47+
3. 设 $m \geqslant 1$,$(a, m) = 1$,称使得 $a^d \equiv 1 \pmod{m}$ 的最小正整数 $d$ 为 $a$ 模 $m$ 的****,记作 $\operatorname{ord}_m(a)$ 或 $\delta_m(a)$. 当 $\operatorname{ord}_m(a) = \varphi(m)$ 时,称 $a$ 是 $m$ 的**原根**.
48+
::
49+
50+
**模 $m$ 剩余系存在原根的充要条件**:$m = 2, 4, p^k, 2p^k$,其中 $p$ 是奇素数,$k$ 是正整数.
51+
52+
**Lagrange 定理**:$g^k$ 在有限群 $G$ 中的阶是 $\dfrac{\operatorname{ord}(g)}{\gcd(k, \operatorname{ord}(g))}$.
53+
54+
---
55+
56+
### 离散对数问题 DLP
57+
58+
已知 $g$、$p$ 和 $y$,对于方程 $y = g^x \pmod{p}$,求解 $x$ 是一个困难问题.
59+
60+
用到离散对数的加密算法:[Diffie-Hellman 密钥交换](./diffie-hellman)、ElGamal 加密算法、椭圆曲线算法等.
61+
62+
---
63+
64+
### 攻击途径
65+
66+
[**Alpertron 离散对数求解器**](https://www.alpertron.com.ar/DILOG.HTM)
67+
68+
[**SageMath**](https://doc.sagemath.org/html/en/reference/groups/sage/groups/generic.html):能自动选择算法,可计算椭圆曲线上的离散对数.
69+
70+
**Baby-Step Giant-Step 算法**:时间复杂度 $\Theta(\sqrt{p})$.
71+
72+
**Pohlig-Hellman 算法**:当 $\operatorname{ord}_p(g)$ 是光滑数时,可分解为子问题求解.
73+
74+
- 若 $n = \displaystyle\prod_{i=1}^k p_i^{e_i}$ 的所有质因子 $p_i$ 都很小,则称 $n$ 是一个**光滑数**.(不是严格定义)
75+
- 如果 $p$ 是质数,则 $\operatorname{ord}_p(g) = \varphi(p) = p-1$,这也是常用 $p=2q+1$ 的原因.
76+
- 时间复杂度 $\Theta\left(\displaystyle\sum_{i=1}^k e_i\left(\log p + \sqrt{p_i}\right)\right)$.
77+
78+
**Pollard's $\rho$ 算法**:一种随机算法,时间复杂度 $\Theta(\sqrt{p})$.
79+
80+
**Pollard's Kangaroo 算法**:如果已知 $x \in [a, b]$,时间复杂度 $\Theta(\sqrt{b-a})$.
81+
82+
---
83+
84+
### Pohlig-Hellman 算法原理
85+
86+
::note{info}
87+
以下证明来自 [Wikipedia](https://en.wikipedia.org/wiki/Pohlig%E2%80%93Hellman_algorithm).
88+
::
89+
90+
对于离散对数问题 $y = g^x \pmod{p}$,设 $n := \operatorname{ord}_p(g) = \displaystyle\prod_{i=1}^k p_i^{e_i}$.
91+
92+
1. 对每个 $i \in \{1, \cdots, r\}$:
93+
- 计算 $g_i\equiv g^{n/p_i^{e_i}}\pmod p$. 由 Lagrange 定理,$g_i$ 的阶是 $p_i^{e_i}$.
94+
- 计算 $y_i \equiv y^{n/p_i^{e_i}} \equiv g^{xn/p_i^{e_i}} \equiv g_i^x \equiv g_i^{x\bmod p_i^{e_i}} \equiv g_i^{x_i} \pmod p$. 由于 $n$ 是光滑数,$x_i$ 的范围为 $[0, p_i^{e_i})$,可知其范围较小,可以用其他算法求解.
95+
2. 得到 $x\equiv x_i\pmod{p_i^{e_i}}$,由中国剩余定理得到 $x$.
96+
97+
::fold{expand info title="用 Pohlig-Hellman 算法求解椭圆曲线上的离散对数问题 $G\times x=A$"}
98+
```python
99+
facts = list(factor(G.order()))
100+
mods, rems = [], []
101+
102+
for fact in facts:
103+
t = fact[0] ** fact[1]
104+
G0 = G * (G.order() // t)
105+
A0 = A * (G.order() // t)
106+
mods.append(t)
107+
rems.append(discrete_log(A0, G0))
46108

47-
若存在最小 $n \in \mathbb{Z}$ 使得 $G = \{e, g, g^2, \ldots, g^{n-1}\}$,其中 $e$ 是 $G$ 的单位元,则称 $G$ 是一个**有限循环群**. 此时 $n$ 称为 $G$ 的****,记作 $|G|$.
109+
x = crt(rems, mods)
110+
```
48111
::
49112

50-
**循环群元素的阶**:对循环群中任意一个元素,都可以写成 $g^k$ 的形式,$g^k$ 的阶可由 $\operatorname{ord}(g^k) = \dfrac{n}{\gcd(n, k)}$ 求得.
113+
若 $n$ 并非完全光滑,例如存在一个大素数因子,仍可通过 Pohlig-Hellman 算法限制 $x$ 的范围,见 [PicoCTF 2017: ECC2 的 Writeup](https://gist.github.com/jproney/7e6cb7a40a8bf342e978a900a32e4dfc).

0 commit comments

Comments
 (0)