157 |
158 | 30. 拥塞控制和流控制的说法,对的
159 | (a) 流控制在网络传输能力不足时使用
160 | (b) 拥塞控制解决接收端来不及处理高速到达的分组的问题
161 | (c) 拥塞控制提高整个网络的传输能力
162 | (d) 滑动窗口协议中,退后n帧的协议,是一种拥塞控制机制。
163 | (d)
164 |
165 | 31. POP3用于___用途。(用于从服务器收信)
166 | 用于从服务器收到信
167 |
168 | 32. 网络管理的五个基本管理功能,安全管理等。
169 |
170 | 33. FTP会话全过程,会建立几次控制连接,几次数据连接。
171 | * 1次控制连接; 若干次数据连接
172 |
--------------------------------------------------------------------------------
/software_engineering/software-eng.tex:
--------------------------------------------------------------------------------
1 | \documentclass{ctexart}
2 |
3 | \title{软件工程}
4 |
5 | \usepackage{graphicx}
6 |
7 | \begin{document}
8 | \maketitle
9 |
10 | \section{介绍}
11 | \paragraph{生命周期地看待} 软件不是写完就完了, 而是需要持续的维护.
12 | \paragraph{过程评估} 即使软件没有完成, 也可以在过程中评估软件.
13 | \pagebreak{项目产出} 项目的产出除了可用的程序, 还有如文档.
14 | \paragraph{人月神话} 软件开发中, 不是人越多就开发越快, 相反可能更多的中途加入的人会拉慢进度.
15 | \paragraph{开发和客户交流} 客户需要不断的和开发交流, 以修正轨迹和提供更精确的方向指导.
16 |
17 | \section{持续集成和git}
18 | 系统可能随时变化, 预测式的开发已经不合时宜了.
19 |
20 | \section{代码风格}
21 |
22 |
23 | \pagebreak
24 | \section{重构}
25 | \begin{figure}[ht!]
26 | \centering
27 | \includegraphics[width=\textwidth, height=\textheight, keepaspectratio]{refactor-1.jpg}
28 | \caption{Software Rot现象}
29 | \end{figure}
30 |
31 | \begin{figure}[ht!]
32 | \centering
33 | \includegraphics[width=\textwidth, height=\textheight, keepaspectratio]{refactor-2.jpg}
34 | \caption{何谓Refactoring}
35 | \end{figure}
36 |
37 | \begin{figure}[ht!]
38 | \centering
39 | \includegraphics[width=\textwidth, height=\textheight, keepaspectratio]{refactor-3.jpg}
40 | \caption{常见的Code Smell}
41 | \end{figure}
42 |
43 | \begin{figure}[ht!]
44 | \centering
45 | \includegraphics[width=\textwidth, height=\textheight, keepaspectratio]{refactor-4.jpg}
46 | \caption{常见的Refactor方法}
47 | \end{figure}
48 |
49 | \section{测试}
50 | 测试: 希望找到错误.
51 | 好的测试是容易找到错误的测试.\par
52 | 基本原则是, 测试是不可穷尽的.
53 | \paragraph{测试样例} 指定输入, 输出, 以及可能的执行前置条件etc.
54 | \subsection{分类}
55 | \begin{description}
56 | \item[白盒测试] 已知要测试的代码. 是基于代码测试的, 测试功能是代码的子集.\\
57 | 包含控制流覆盖率和数据流覆盖率;
58 | \item[黑盒测试] 已知要测试的特性, 是基于规格测试的, 测试功能是规格的子集.\\
59 | 包含需求覆盖率
60 | \end{description}
61 | 考虑测试韦恩图, 有集合$S$表示规格, $P$表示程序, $T$表示测试.
62 | 白盒测试是$T \subseteq P$; 黑盒测试是$T \subseteq S$.
63 | \subsection{控制流测试}
64 | \paragraph{Statement Coverage}
65 | C0测试; 所有语句都必须被测试覆盖.\par
66 | 方法: 在控制流图中找到覆盖所有语句 (即覆盖所有节点) 的路径 (可能的话需要路径集合), 寻找输入适配此路径.
67 | \paragraph{Decision Coverage}
68 | C1测试; 所有if语句的条件必须测试过true和false.\par
69 | 方法: 寻找覆盖所有分支 (每个节点的每条出边必须被覆盖) 的路径集合, 寻找输入适配路径集合.
70 | \paragraph{Predicate Converage}
71 | C1P测试; 所有if语句的条件中每个原子条件必须测试过true和false.\par
72 | 方法: 将if的复合条件改写成嵌套if之后看.
73 | \paragraph{Multiple Condition Coverage}
74 | CMCC测试; \emph{每个复合条件}中所有原子条件的组合都被测试. 组合会有依赖关系所以不一定是$2^n$种路径.\par
75 | 方法: 同C1P测试, 只是需要更多路径.
76 | \paragraph{All Path Coverage}
77 | C$\infty$测试; \emph{整个程序}中所有原子条件的组合都被测试, 即所有路径都被测试.\par
78 | 通常在有循环时时不可行的, 或者在条件互斥时也不可行 (某一条路径不存在对应的输入).
79 | \paragraph{cyclomatic complexity (McCabe complexity)}
80 | 覆盖所有控制语句 (C0测试) 需要的最少的测试例数. 其等于流图中, 决策的数量加1.
81 | \paragraph{Independent Path}
82 | 有向图中的独立路径. 数目等于 cyclomatic complexity.
83 |
84 | \subsection{数据流测试}
85 | 不正常的数据行为: \begin{itemize}
86 | \item (dd) 定义之后, 引用前重定义
87 | \item (ur) 未定义时引用
88 | \item (du) 定义后未引用
89 | \end{itemize}
90 | 数据流分析分为静态和动态, 以是否执行原代码为准.
91 | \paragraph{DU链} $[X, S, S']$的形式, $S$, $S'$是语句,
92 | $S$定义而$S'$引用$X$, 并且$S$的定义在$S'$是存活的.
93 | \paragraph{数据流图} 将数据的引用分为计算引用 (C-uses) 和条件引用 (P-uses),
94 | 基本结构类似流图, 但是结点是计算引用, 边与条件引用挂钩.
95 | \paragraph{覆盖度量} 包含 All paths (通常不可能),
96 | All DU paths, All Edges (通常最低要求)等.
97 |
98 | \subsection{黑盒测试}
99 | \paragraph{基本思想} 将输入空间分成多个等价类, 每个等价类对于程序来说是等价的, 从``发现错误''的角度.
100 | \paragraph{强弱测试} 强测试即, 可能有多个无效的输入, 弱测试即, 只有一个输入时无效的.
101 | \paragraph{普通鲁棒测试} 普通测试只覆盖有效数据, 鲁棒测试还覆盖无效数据.\par
102 | 普通鲁棒测试和强弱测试都是EP测试.
103 | \begin{figure}[ht!]
104 | \centering
105 | \includegraphics[width=\textwidth, height=\textheight, keepaspectratio]{eptest.png}
106 | \caption{EP测试的例子}
107 | \end{figure}
108 |
109 | \paragraph{边界值分析} 通常问题都是在输入在输入域边界时出现的.
110 | 因此, 选择数据时, 不是随机在数据空间选择, 而是在空间边界选择.
111 | 同时也从输出域中寻找数据.\par
112 |
113 |
114 | \section{xUnit}
115 | \paragraph{测试的重要性} 有``错误率恒定定律'', ``规模代价平方定律'', 所以需要
116 | 尽可能早地发现错误,并在尽量小的范围内定位并修复错误.
117 | \paragraph{测试驱动开发}
118 | 开发新特性之前先写测试 (此时必定失败). 能够使得开发人员专注于需求.
119 | \paragraph{基本架构}
120 | \begin{figure}[ht!]
121 | \centering
122 | \includegraphics[width=\textwidth, height=\textheight, keepaspectratio]{xunit-1.png}
123 | \caption{xunit基本架构}
124 | \end{figure}
125 | \paragraph{setup / teardown} 应当delegate inline setup / teardown;
126 | \paragraph{verification} 分为behavioural, state, delta etc.
127 | \paragraph{test doubles} 需要依赖的系统 (DOC) 不可用或者不方便. 可以用一个假的DOC, 也方便监控.\par
128 | 如Dummy Object: 假的值. Test Stub: 提供indirect input. Test Spy / Mock Object: 检查indirect output.
129 |
130 | \section{系统测试}
131 | \paragraph{测试分类}\begin{itemize}
132 | \item 单元测试, 单个模块的功能实现
133 | \item 集成测试, 模块之间的接口测试
134 | \item 系统测试, 包含其他系统, 如硬件等
135 | \end{itemize}
136 | \subsection{集成测试}
137 | 不要一次测试所有单元的继承, 而是增量式地, 一部分一部分测试然后再接起来.
138 | \paragraph{Top-down 集成}
139 | 从顶层单元开始. 未计入的底层用 Test Stubs 代替.\par
140 | 问题是, 需要写大量的stub.
141 | \paragraph{Bottom-down 集成}
142 | 从底层开始. 需要一个控制器 (test driver), 用于假装上层单元.\par
143 | 问题是, 最重要的主逻辑是最后测试的.
144 |
145 | \subsection{效率测试}
146 | 性能: 响应时间; 吞吐量; 数据容量; 实时性.\par
147 | 测试: 负载测试 (load testing): 在一个指定的负载下;
148 | 压力测试 (stress testing): 极限负载;
149 | soak testing: 在指定负载下的\emph{持续}表现;
150 | spike testing: 突然增加负载;
151 | configuration testing: 确定配置对于系统表现的影响.\par
152 |
153 | \section{需求工程}
154 | 需求将用户的目标和期望转化为工程师的规格说明.
155 | \paragraph{用户}
156 | 需要确定给谁做, 做什么, \emph{不做什么}.
157 | \paragraph{需求分层} \begin{enumerate}
158 | \item 业务需求 -> 愿景和范围文档
159 | \item 用户需求 -> 用户需求文档
160 | \item 软件需求 -> 软件需求规格说明
161 | \end{enumerate}
162 | \paragraph{需求建模泳道图}
163 | \begin{figure}[ht!]
164 | \centering
165 | \includegraphics[width=\textwidth, height=\textheight, keepaspectratio]{lane.png}
166 | \caption{泳道图}
167 | \end{figure}
168 | \paragraph{数据流图}
169 | 分层需要满足平衡规则, 即不漏不多.
170 | \paragraph{判定方法}
171 | \begin{figure}[ht!]
172 | \centering
173 | \includegraphics[width=\textwidth, height=\textheight, keepaspectratio]{pred-tree.png}
174 | \caption{判定树方法}
175 | \end{figure}
176 |
177 | \begin{figure}[ht!]
178 | \centering
179 | \includegraphics[width=\textwidth, height=\textheight, keepaspectratio]{pred-tbl.png}
180 | \caption{判定表方法}
181 | \end{figure}
182 | \paragraph{软件需求规格说明 SRS}
183 | 应当足够详细. 效率需要可测.\par
184 | 良好的SRS, 可验证, 每个需求是为了一个业务目标, 可测并且有界.\par
185 | [条件] [主体] [动作] [客体] [对某个参数的约束] [约束的值]\par
186 | 需求应当正确, 准确, 无二义性, 量化的, 并且覆盖所有情况\par
187 | 需求不应当随便变更, 变更前应当经过评估.\par
188 |
189 | \section{UML}
190 | \paragraph{基本架构} 4+1 视图 \begin{itemize}
191 | \item Design
192 | \item Implementation
193 | \item Process
194 | \item Deployment
195 | \item *** Use case ***
196 | \end{itemize}
197 | \paragraph{用例图}
198 | \begin{figure}[ht!]
199 | \centering
200 | \includegraphics[width=\textwidth, height=\textheight, keepaspectratio]{usecase.png}
201 | \caption{用例图例子}
202 | \end{figure}
203 | Actor: 一个和系统交互的实体.\par
204 | use case: 系统的主要的功能.\par
205 | \paragraph{Class Diagram}
206 | \paragraph{Sequence Diagram}
207 | 不一定, 但通常是一个use case之内的描述.\\
208 | 从实体和时间两个维度定义了系统的行为.\par
209 | \begin{figure}[ht!]
210 | \centering
211 | \includegraphics[width=\textwidth, height=\textheight, keepaspectratio]{seqdiag.png}
212 | \caption{Sequence Diagram例子}
213 | \end{figure}
214 | 并且class diagram也是sequence diagram的一部分, 如
215 | \begin{figure}[ht!]
216 | \centering
217 | \includegraphics[width=\textwidth, height=\textheight, keepaspectratio]{seqdiag1.png}
218 | \caption{Class Diagram例子}
219 | \end{figure}
220 | 方法就是, 将原需求拆成一系列的 (subject) {action} (object).
221 | subject和object统称物, 每个物形成一条数显, 按照正确的顺序,
222 | 从subject有一个到object的横箭头, 上面写action.\par
223 | 如数据, 电话线等等都算``物''. 一个例子如图.
224 | \begin{figure}[ht!]
225 | \centering
226 | \includegraphics[width=\textwidth, height=\textheight, keepaspectratio]{seqdiag2.png}
227 | \caption{构造sequence diagram}
228 | \end{figure}
229 | \begin{figure}[ht!]
230 | \centering
231 | \includegraphics[width=\textwidth, height=\textheight, keepaspectratio]{seqdiag3.png}
232 | \caption{构造sequence diagram}
233 | \end{figure}
234 |
235 | \section{软件设计}
236 | \paragraph{设计原则} 模块化, KISS, 面向变化和重用, seperation of concerns.
237 | \paragraph{KISS} 简化代码, 简化架构 (中间件总线), 即插即拔.
238 | \paragraph{模块化} 设计时候考虑 coupling 和 cohesion.
239 | 要弱耦合, 强内聚.
240 | \paragraph{seperation of concerns} 每个程序元素做一个且仅一个事情.
241 | \paragraph{面向变化} 封装可能变化的, 保持接口不变; 允许动态绑定.
242 | \paragraph{面向重用}
243 |
244 | \section{架构}
245 | 架构和功能不是分离的, 架构影响软件质量.\par
246 | \paragraph{性能}
247 | 包含 (并发性, 速度etc), 解决方法有队列, 均衡负载等
248 | \paragraph{可用性}
249 | 定义为足够长的时间段内可用时间的比例.\par
250 | 对于硬件, 可以用冗余投票提高可用性.\par
251 | 对于软件, 可以N版本编程. 但不能避免错误, 如多个团队犯了同样的错误, 或者尤其是规格中的错误.
252 | \paragraph{可修改性}
253 | 将旧的开发框架做成product line, 在新的开发中使用. 并且同时也不断优化旧的开发框架.
254 | \paragraph{架构风格}
255 | \begin{itemize}
256 | \item 事件驱动. 可以直接用callback管理.
257 | 也可以利用间接方法: 实现广播模型, 在总线上广播; 或者采用中断处理方法.
258 | \item 管道和filter. 每个模块的输入通过模块变换到输出, 一个模块的输出接到另一个模块的输入上.
259 | \item 分层架构. 如OSI RefModel.
260 | \item 仓库. 一个中心仓库保存所有子系统需要的数据.
261 | \end{itemize}
262 | \paragraph{分布式架构风格}
263 | 分布式的有C/S风格和P2P风格.
264 | \begin{figure}[ht!]
265 | \centering
266 | \includegraphics[width=\textwidth, height=\textheight, keepaspectratio]{csarch.png}
267 | \caption{C/S的架构风格}
268 | \end{figure}
269 | \paragraph{SOA} Service oriented architecture. C/S设计, 但是更强调软件部件间的解耦合.
270 | \paragraph{Micro-Service} 一个应用是一系列小而专一的服务组成的. 服务自己是一个进程,
271 | 其间通过轻量级的方式 (如HTTP API) 通信.
272 | \paragraph{Web应用架构} REST, 服务器集群\ldots
273 |
274 |
275 | \section{质量管理}
276 | \paragraph{critical system}
277 | 可靠性要求极高的系统, 甚至允许采用不经济的手段保证可靠性.
278 | \paragraph{错误种类} \begin{description}
279 | \item[fault] 错误可能的源头
280 | \item[error] 到达服务接口的fault
281 | \item[failure] 导致服务出现错误的error
282 | \end{description}
283 | \paragraph{V\&V} Verification: 是否符合规格; Validation: 是否符合客户期望.
284 | \paragraph{inspection}
285 | 包括code review, formal inspection.
286 | \paragraph{cleanroom computing}
287 | 目的是完全避免错误.
288 | 包含 peer review; 增量开发等等.\par
289 |
290 | \section{项目管理}
291 | \paragraph{团队} 需要团结etc.
292 | \paragraph{工作分解} 如responsibility assignment matrix.
293 | \paragraph{任务调度} 寻找什么任务是不能拖延的:
294 | 使用关键路径方法, 通过earliest和latest来完成.
295 | \paragraph{甘特图} 如图.
296 | \begin{figure}[ht!]
297 | \centering
298 | \includegraphics[width=\textwidth, height=\textheight, keepaspectratio]{gantt.png}
299 | \caption{甘特图}
300 | \end{figure}
301 | \paragraph{风险管理} 有``救火队员''的风险处置方法, 以及预先做好准备的风险管理.
302 | RMMM: Risk Mitigation, Monitoring, Management.
303 |
304 | \section{Software Process}
305 | 软件的质量被其过程管理的质量控制.
306 | \paragraph{软件开发模型}
307 | \begin{itemize}
308 | \item 瀑布模型. 需求确定, 一步一步地完成. 不能很好的适应需求改变.
309 | \item 增量模型. 一部分一部分地开发, 每次都只分析这一部分的需求.
310 | \item 原型模型. 开发过程中, 利用原型和客户交流, 细化需求.
311 | \item 螺旋模型. 更注重风险管理.
312 | \end{itemize}
313 | \paragraph{RUP} 常和UML配套使用.
314 | \begin{figure}[ht!]
315 | \centering
316 | \includegraphics[width=\textwidth, height=\textheight, keepaspectratio]{rup.png}
317 | \caption{rational unified process}
318 | \end{figure}
319 | \paragraph{CMM}
320 | \paragraph{SPICE}
321 | \end{document}
322 |
--------------------------------------------------------------------------------
/modern_cryptography/modern_cryptography_notes.txt:
--------------------------------------------------------------------------------
1 | 现代密码学
2 |
3 | 评分
4 | 三次大作业 30%
5 | 考勤和小测 10%
6 | 开卷考试 60%
7 |
8 | ------------------------------------------------------------------------------
9 | 历史
10 | 隐写术
11 | 隐藏消息 (信息的载体) 本身, 相对于密码隐藏有效信息
12 | Substitution 替换
13 | 单表 / 多表
14 | 密码盘
15 | Transposition 置换
16 | 轮转机
17 | > Enigma
18 | message -> 接线板: 随机交换6对字母
19 | [相当于单表代换]
20 | -> 3个'扰频器': 替换后替换函数也会变换 (shift-by-one)
21 | [通过变动的替换函数, 提供频率扰动, 防止频率分析]
22 | [相当于26*26*26多表代换]
23 | -> 反射器
24 | 对称密码体系
25 | e.g. DES, AES
26 | 公钥密码体系 (非对称密码体系)
27 | e.g. DH, RSA, ECC
28 | 量子密码体系
29 | 主要是量子密钥分发
30 |
31 | 术语
32 | 发送方 Alice
33 | 接收方 Bob
34 | 窃听者 Eve
35 | 不安全信道: 密码分析
36 | 消息认证码: 保证消息来自通讯双方, 抗篡改, 通常是使用对称密钥加密消息哈希
37 | 数字签名: 保证消息来自特定的某一个人, 抗抵赖, 通常是私钥加密消息哈希
38 | 证书: 签名了的公钥, 确保没有第三方伪造证书
39 |
40 | 基本原则
41 | 算法的保密非常脆弱: 相对于密钥, 算法很难保密, 一旦泄露代价很大
42 | Kerckhoffs Principle: 一个密码系统的安全性不在于对加密算法进行保密,
43 | 而仅在于对密钥的保密
44 | 一次一密: 绝对安全, C = M xor K, K 和 M 一样长而且只使用一次
45 |
46 | 密码学的分支
47 | 编码: 主要是设计密码
48 | 分析: 主要是对密码的破解
49 | 密钥管理: 密钥整个生命周期
50 |
51 | 密码分析
52 | 唯密文
53 | 只知道 (一系列) 的密文, 没有其他信息
54 | 已知明文
55 | 知道当前密钥下的一些明密文对
56 | 选择明文
57 | 在当前密钥下, 能选择一些明文得到其对应的密文
58 | 选择密文
59 | 在当前密钥下, 能选择一些密文得到其对应的明文
60 | 一般这里的密文不是随便选取的
61 | 最后两种一般评估密码算法的强度
62 |
63 | ------------------------------------------------------------------------------
64 | 古典密码学
65 | 设计原则主要就是 置换 和 代换
66 |
67 | 置换密码
68 | 密钥
69 | 1..n 上的置换 pi(i)
70 | 加密
71 | C_i = P_{pi(i)}
72 | 密码棒, 列密码, Naive分组置换
73 |
74 | 分析方法:
75 | 频率信息确定算法是置换算法
76 | 唯密文攻击: 暴力搜索, 密码空间比较小; 查字典
77 | 已知明文攻击: 很简单可以恢复密钥
78 |
79 | 代换密码
80 | 恺撒密码: 平移代换密码.
81 | c_i = (p_i + K) mod 26
82 | 知道密码算法是 Caesar 则直接测试 25 种可能
83 | 不知道密码可以通过频率分析确定
84 | Naive代换: 单表静态代换
85 | a in ALPHABET -> b in ALPHABET, 通过一个双射
86 | 密钥可以是一个单词, 方便古人记忆
87 | 暴力破解很困难, 密钥空间很大.
88 | 频率分析, k-gram 频率分析
89 | Vigenere: 多表代换
90 | c_i = (m_i + k_{i mod lk}) mod 26
91 | 和现代思想的对比 -> 流密码: 先产生密钥流
92 | 密钥空间大小很大: 无法穷举搜索
93 | 如果知道密钥长度, 则容易破解
94 | Kasiski测试:
95 | 相同的明文被加密成相同的密文段, 则间距是密钥长度倍数
96 | -> 搜索长度较长的相同密文段, 假设他们是相同明文加密所得,
97 | 则密文段间距离是 lk 的倍数
98 | 得到足够多组密文段后就可以 lk | gcd(间距)
99 | 重合指数法:
100 | Ic(x) 是 x 中两个随机元素相同的概率
101 | 随机的 Ic 显著低于正常英语串的 Ic
102 | 枚举密钥长度, 长度正确时则
103 | c_1, c_{1+lk}, c_{1+2lk} ...
104 | 的 Ic 应当接近正常英语串的 Ic
105 | 弱点
106 | 密钥长度 < 明文长度
107 | 加强 Vigenere
108 | 密钥产生: 密钥和消息一样长
109 | 密钥随机: 每次加密使用不同的密钥
110 | -> 这就是一次一密
111 | Playfair
112 | 代换基于双字母对 (2gram)
113 | 和现代思想的对比: 多字母加密 -> 块密码
114 | 一次一密
115 | 无论有多少计算资源, 都无法破解
116 | given C: forall T: exists K: Encrypt_K[T] = C
117 | 已知任何的密文, 都无法推出明文的信息
118 | 密文无法泄露关于明文的信息:
119 | Pr{P=p' | C=c'} = Pr{P=p'}
120 | 证明 (Bayes定理)
121 |
122 | 密码体系的安全性
123 | * 无条件安全性 无论计算资源多少都无法破解. OTP
124 | * 计算安全性 在可能的计算能力限制下, 破解的概率很小
125 | 即 ``实践中'' 无法控制
126 | * 可证明安全性 可以归约到数学难题. i.e. 不弱于一个数学难题.
127 | 事实上是给安全性提供 ``证据''
128 | 现有的计算资源
129 | e.g. 每秒 十亿亿次计算 ...
130 | 密钥空间只要应有 2**128 大小的计算空间
131 | i.e. 密钥长度需要至少 128 位
132 | 密钥空间过小, i.e. 短密钥是危险的
133 | 则无论已知明文还是唯密文都可以通过枚举密钥得到原文的性质
134 | (所谓明文性质, 包括明文空间上的概率分布也算,
135 | 得到明文空间的真子集也算)
136 | 归约方法 (概率性的描述)
137 | A 攻破密码的成功率是 e
138 | A' 要解决难题 X
139 | 如果通过调用 A, A' 以不可忽略的概率解决 X
140 | 实际的对称密码要求
141 | * 计算安全: 唯密文 / 选择明文
142 | * 方便实用: 不用使用冗长密钥, 不必频繁更换, 计算速度快
143 | 现在来看, 序列密码的可信度被认为较低 (虽然速度较快).
144 | 一般使用分组密码较多.
145 |
146 | ------------------------------------------------------------------------------
147 | 分组密码
148 | 概念
149 | 一次加密一大块数据
150 | 历史
151 | DES (Feistel), 3DES, AES (SPN, 2001 yr)
152 | 迭代结构
153 | 密钥 -密钥生成-> 密钥组
154 | m 迭代 -> a1 -> a2 ... -> c
155 |
156 | 分组密码架构
157 | Feistel结构 (1973 yr)
158 | 将输入分成左右两半, 第 k 轮完成
159 | L_t = L xor F(K_k, R_i)
160 | L_o = R_i
161 | R_o = L_t
162 | 加解密可以使用同一套设备, 除了密钥组顺序相反
163 | 变种
164 | * 非平衡 Feistel e.g. skipjack
165 | * 多叉 Feistel
166 | 安全性
167 | 轮函数 F 是安全的伪随机函数, 则
168 | Feistel 结构在很浅的轮数就能形成 [强的] 伪随机置换
169 | (事实上简单的 F 不满足伪随机, 所以轮数在实际中并不浅)
170 | Substitution Permutation Network
171 | 每一轮
172 | in -> xor K_k -> S-box -> P-box -> out
173 | S-box的属性
174 | * 简单的单表代换, 一般较小 -> 易于实现
175 | * 输出中任何比特与所有比特都有关系
176 | * 输入即使只有 1 比特改变, 输出有很多比特改变 (扩散)
177 | 加解密不能使用同一套设备
178 |
179 | DES
180 | 基本框架
181 | M -> 起始置换 -> 16 轮 -> 末置换 -> C
182 | F 函数
183 | in (L half) -> xor K_k
184 | -> S-box (S-box in=6b, out=4b)
185 | -> P-box (in=32b, out=32b)
186 | -> out
187 | 其中, S-box 的设计由其需要的属性决定,
188 | 在此基础上加上一些酒后涂鸦和上司的要求
189 | 密钥生成
190 | 移位和置换
191 |
192 | SM4
193 | 基于 4 分支的 Feistel (Generalized Feistel)
194 |
195 | 2-DES
196 | M - Ek1 >- Ek2 > C
197 | 被 MITM 破解.
198 | 考虑选择明文攻击 (给定任何 M 可求出 C)
199 | 选定一个 M, 枚举所有的 k1 计算 Ek1(M) 得到 {Ek1(M)}
200 | 可以看出, {Ek1(M)} 大小约为 |{k1}| = 2**56
201 | (Ek1(M) 是 64 位的)
202 | 将这些 Ek1(M) 用合适的数据结构组织起来,
203 | 可以在常数或者对数时间内查找是否存在
204 | 然后枚举 k2, 每次计算出 Dk2(C) 可以迅速查找是否有 Ek1(M) == Dk2(C)
205 | 对于 2-DES, (使用MITM) 可以在 2**56 + 2**56 时间和 2**56 空间内被破解
206 | 而暴力需要 2**112 时间 (和很少空间)
207 |
208 | 3-DES
209 | M - Ek1 >- Ek2 >- Ek3 > C
210 | 也被 MITM 破解, 可以在 2**56 空间和 2**112 时间内被破解
211 | 但是是计算安全的
212 |
213 | AES
214 | 基于 SPN
215 | 状态
216 | S = s0 s4 s8 s12
217 | s1 s5 s9 s13
218 | s2 s6 s10 s14
219 | s3 s7 s11 s15
220 | 伪代码
221 | AES(P) -> C
222 | S = P
223 | add_round_key(S, w[0])
224 | for r=1..Nr-1
225 | sub_bytes(S)
226 | shift_rows(S) 从0到3第 i 行作为整体 (32位字) 左移 i 位
227 | mix_columns(S) ... GF 上乘法
228 | add_round_key(S, w[r])
229 | sub_bytes(S)
230 | shift_rows(S)
231 | # 最后没有 Mix Columns
232 | add_round_key(S, w[Nr])
233 | return C
234 | GF(256) 上乘法
235 | 使用多项式表示元素, 乘完之后需模二元域GF(2)上一个8次不可约多项式的乘积
236 | AES 中是 x8 + x4 + x2 + x1 + x0
237 | e.g.
238 | 0x57 mul 0x83 = (x6+x4 + x2+x1+x0) mul (x7 + x+1)
239 | mod (x8+x4+x2+x1+x0)
240 | = x7 + x6 + 1
241 | = 0xc1
242 |
243 | 分组密码的工作模式
244 | 分组密码实际中的使用模式
245 | 对任意长的明文如何加密
246 |
247 | ECB: 明文的 N 个分组使用统一密钥加密和解密
248 | -> 可以并行, 效率高, 实现简单
249 | -> 相同的明文分组导致相同的密文, 变成单表代换
250 | -> 只能使用短消息, 或者高度随机的数据
251 | 错误敏感性:
252 | 一位传输错误导致一个分组无法正确解密
253 | [ECB.png]
254 |
255 | CBC: 每一组明文加密前, 先和前一组密文异或
256 | IV: 无需保密, 但是需要随机, 抗篡改
257 | -> 加密不能并行; 解密可以并行
258 | 错误敏感性
259 | 一位传输错误导致两个分组无法正确解密
260 | 这个错误所在的分组和下一个分组
261 | [CBC.png]
262 | CBC 是应用最广的工作模式.
263 |
264 | CFB: 从 IV 生成一个密钥流, 每次加密 r 位明文,
265 | 直接抽取加密算法得到前 r 位 与明文异或,
266 | 之后得到的 r 位密文又压入密钥流中
267 | -> 加密分组长度可变, 支持任意长度的明文, 加密长度不变
268 | -> 一位传输错误导致若干个分组无法正确解密
269 | -> 无法并行 / 效率低
270 | 错误敏感性
271 | 一位传输错误导致 n/r+1 个分组无法正确解密
272 | 通常应用在需要加密解密前后数据大小不变的场合, 如数据库和硬盘加密
273 | [CFB.png]
274 |
275 | OFB: 类似 CFB, 但是每次生成密钥仅仅依赖上次密钥
276 | IV: 每次使用不同的 IV, 虽然不用保密
277 | -> 1 bit 的错误解密后也只会有 1 bit 的错误
278 | 容易出现小圈 e.g. S1 -> S2 -> ... -> S1
279 | 这个圈的大小平均为 (生日攻击) 2**(sizeof(S1)/2)
280 | 错误敏感性
281 | 一位传输错误导致解密后一位错误
282 | [OFB.png]
283 |
284 | CTR: 密钥流通过 E_K[一个计数器] 生成, 密钥 K 对于每个计数器是一样的
285 | -> 加解密都可以并行
286 | 错误敏感性
287 | 一位传输错误导致解密后一位错误
288 | [CTR.png]
289 |
290 | 填充
291 | ECB 和 CBC 需要填充, CFB, OFB 和 CTR 不需要填充
292 | TODO: 为什么 CTR 不需要填充
293 | 明文填充
294 | -> 填充: 位填充 / 字节填充; 随机填充 / 零填充; 是否包含填充长度
295 | 注意, 无论分组满不满, 都要填充 (不然数据是填充还是明文?)
296 | cipher text stealing:
297 | 在 EBC 等模式中
298 | ECB:
299 | 考虑最后两个分组 Pi 和 P{i+1} 其中 P{i+1} 长度不满一整個分组
300 | 那么他们加密得到的结果是
301 | Pi P{i+1}
302 | v
303 | 加密
304 | v
305 | Ci = Ci^(1) || Ci^(2) 其中 Ci^(2) 和 P{i+1} 一样长
306 | V V
307 | E(P{i+1} || Ci^(2)) Ci^(1)
308 | CBC:
309 | 同上
310 | Pi P{i+1}
311 | Ext(P{i+1}) Ext 就是加上 padding
312 | v
313 | 加密
314 | v
315 | Ci
316 | V
317 | E(Ci xor Ext(P{i+1})) Shr(Ci)
318 | 最后 padding 的长度那一部分去掉
319 | 存储加密
320 | 和通信加密不同
321 | 一般模型考虑磁盘上的存储加密, 以块为独立并行的单位,
322 | 并且元信息只考虑块号
323 | -> XTS-AES
324 |
325 | 密码攻击
326 | 时间复杂度通常使用执行的加解密次数来计算
327 |
328 | Padding oracle attack
329 | 给予服务器给出的 padding 错误信息恢复明文
330 |
331 | 通用密码分析
332 | 强力攻击!
333 | 穷举密钥
334 | 字典攻击 对某个 K, 存储 2**n pairs
335 | 查表攻击 对某个 P, 存储 2**k pairs
336 | T-M Tradeoff
337 | 明文固定, 密文已知, 加密算法已知, 希望恢复密钥
338 |
339 | DES 差分分析
340 | 重绘 DES 结构 (只考虑一轮)
341 | +-----------------------------+--------------------------------+
342 | | L | R |
343 | +--------+--------------------+-----------------+--------------+
344 | | |
345 | | |
346 | | |
347 | v |
348 | xor <-- P <-- CS <-- xor <---- EP <-----+
349 | | ^ |
350 | | | |
351 | | | |
352 | | K_i +----+
353 | | |
354 | +---------------------------------+----+
355 | | |
356 | +---------------------------------+ |
357 | | |
358 | v v
359 | +-----------------------------+--------------------------------+
360 | | L | R |
361 | +--------+--------------------+-----------------+--------------+
362 | 线性和非线性部件
363 | xor, P, EP 是线性部件
364 | 如 DELTA $ xor i = xor $ DELTA i
365 | DELTA 是对不同的输入做减法 (减法在这里是 xor)
366 | CS (即 compressive s-box) 是非线性部件, 没有如上的性质
367 | CS 的性质
368 | 对于每一个 s-box (图中 CS 是多个 s-box)
369 | 考虑 DELTA $ CS a, 我们可以构建一张表
370 | -> a
371 | 就是说, 已知 CS 的输入和输出的差分, 我们可以得到有那些可能的输入
372 | 能造成这样的输入输出差分
373 | 列出表发现, 相当一部分 输入差分和输出差分的组合是不可能的.
374 | 破解最后一轮的密钥
375 | 考虑每一个 s-box.
376 | s-box 的输入差分可以从 Lt 差分得到, 输出差分可以从 Rt 差分得到,
377 | 这样我们就可以得到输入输出差分对. 查表得到可能的输入, 于是得到密钥
378 | 在这个 s-box 范围的部分.
379 | 推广到更大轮数的 DES
380 | # TODO 课件写的啥?
381 |
382 | 随机数 RNG
383 | 性质
384 | * 随机性
385 | * 不可预测性
386 | * 不可重现性
387 | 愈来愈强
388 |
389 | 种子 -> 内部状态 <自更新> -> 输出序列
390 | * 线性同余
391 | * 单向哈兮
392 | hash(istate++)
393 | * 密码法
394 | E_Prk (istate++)
395 |
396 | LFSR
397 | 布尔函数的线性递推表达式
398 | an = c1 a{n-1} xor c2 a{n-2} xor ...
399 | 多项式表示
400 | f(x) = 1 + sum{i>0, i<=l} ci x^i
401 | 用 来表示一个 LFSR
402 | m 序列
403 | l 级 LSFR 最长周期是 2^l - 1, 达到这个周期成为 m 序列
404 | f 生成 m 序列当且仅当 f 是一个不可约多项式
405 | 周期序列的 Golomb 随机性
406 | 1. 一个周期中 0 1 个数相差不超过 1
407 | 2. 长度为 k 的游程占游程总数 1/2**k
408 | 产生序列需要
409 | 1. f 函数 (或者递推函数)
410 | 2. 对于 的 LFSR 需要前 l 位
411 | 从序列的一个周期产生
412 | 1. 已知 l : 解方程
413 | 2. BM 算法
414 | def evalf(f, i):
415 | rv = 0
416 | for j in range(1, len(f)):
417 | rv = rv ^ (a(i-j) & f[j])
418 | return rv
419 | for i, ai in enumerate(_a):
420 | d = evalf(f[i], i) ^ ai
421 | if d == 0:
422 | f += [f[-1]]
423 | l += [l[-1]]
424 | else:
425 | if l[-1] == 0:
426 | l += [i+1]
427 | fnp1 = [0 for i in range(l[-1]+1)]
428 | fnp1[0] = fnp1[i+1] = 1
429 | f += [fnp1]
430 | else:
431 | m = i-1
432 | while (m > 0) and (l[m] == l[i]):
433 | m -= 1
434 | f += [add(mul(f[m], x^(i-m)), f[i])]
435 | l += [max(l[i], i+1-l[i])]
436 | 通过 LFSR 建立密钥流
437 | LFSR1 -> +---+
438 | LSSR2 -> | F +---> K
439 | ... | |
440 | LFSRk -> +---+
441 | F 是一个非线性函数, 提高输入的线性复杂度和保持伪随机性
442 |
443 | ------------------------------------------------------------------------------
444 | 哈希函数
445 | 定义
446 | 把任意长的消息压缩成 l 位 {0,1}^* -> {0,1}^l
447 | 注意压缩函数是把 l+m 位的映射到 l 上,
448 | 可以使用 Merkle 的迭代算法用无碰撞的压缩函数实现无碰撞的哈希函数,
449 | 成为 MD 结构, 这是现在许多算法的基石.
450 | 性质
451 | * 可计算性
452 | * 抗原像: 给 H(x), 计算得到 x 是困难的
453 | Brute force O(2^l) with 1-e^{-1} probability.
454 | (Euler's equality: (1-x^{-1])^{x} -> e^{-1})
455 | * 第二抗原像: 给 H(x), 求任何一个 z 使得 H(z) = H(x) 是困难的
456 | * 抗碰撞: 难以发现 x, y 使得 H(x) = H(y)
457 |
458 | 结构
459 | 块 Hash (MD 结构, Wide-pipe结构)
460 | +----------------+-------------------+---
461 | | 消息分块 | 消息分块 | ...
462 | +-+--------------+----+--------------+---
463 | | |
464 | | |
465 | | +--- | +---
466 | +----->| \ +----->| \
467 | | \ | \
468 | +---+ | \ +---+ | \
469 | |IV +--->|压缩 +--->|IV +--->|压缩 +---> ...
470 | | | | 函数| | | | 函数|
471 | +---+ +-----+ +---+ +-----+
472 | 特点: 压缩函数抗碰撞 <=> 如上得到的哈希函数抗碰撞
473 | 攻击: 加长攻击
474 | 海绵结构
475 | 整个哈希过程分成两部分
476 | 1. 从 IV 开始, 每次接受输入的一小部分, 之后做 F 变换
477 | 做完 1 相当于把输入吸收到了我们的状态中
478 | 2. 每次输出状态的一小部分, 然后做 F 变换
479 | 以上 F 是一个双蛇, 不改变长度
480 |
481 | 寻找 Hash 碰撞
482 | * 暴力穷举
483 | * 生日攻击: 需要 2^{n/2} 的存储
484 | * 随机路径算法:
485 | 选定 X0, 之后 X{i+1} = H(Xi)
486 | 使用 Floyd's 双指针算法可以只用 O(1) 空间:
487 | 最开始 P0, P1 都是 X0
488 | 每次迭代, P0 = H(P0), P1 = H(H(P1)), 相当于 P1 比 P0 快一倍
489 | 当 P1, P0 相遇的时候, 说明找到碰撞点
490 | 这时 P1 保持在碰撞点, P0 回到 X0
491 | 之后每次迭代, P0 = H(P0), P1 = H(P1), 相当于速度相同
492 | 当 H(P0) = H(P1) 时, 就找到了哈希的一个碰撞
493 |
494 | MD5
495 | 使用 MD 结构, 消息分块 512 位, IV 状态 128 位.
496 | 4 轮, 每轮对状态做 16 次变换
497 |
498 | SHA
499 | 使用 MD 结构, 消息分块 512 位, IV 状态 160 位.
500 | ------------------------------------------------------------------------------
501 | 消息认证码
502 |
503 | 认证
504 | * 消息完整性: 不被篡改
505 | * 消息来源: 对发消息的人的认证
506 | * 抗抵赖性, 抗重放性
507 |
508 | 针对消息完整性的认证
509 | * 安全通道传输消息哈希
510 | * 不加密消息, 加密消息哈希形成 MAC, 附在消息后面
511 | * 加密消息和消息哈希, 一起传输
512 |
513 | 消息真实性的认证
514 | * MAC: E_SK(M), SK 是共享的密钥
515 | 数据完整性, 消息来源认证, 不能实现数字签名
516 | 攻击: 伪造原文; 密钥恢复攻击; 重放攻击;
517 | * 数字签名 (见后)
518 |
519 | ------------------------------------------------------------------------------
520 | 公钥密码体制
521 |
522 | 对称密钥密码系统的缺陷
523 | * 密钥必须经过安全的信道分配
524 | * 无法用于数字签名
525 | * 密钥管理复杂, 密钥的数量: O(n^2)
526 |
527 | 对称体制下的解决方案: 密钥分发中心
528 | 所有人和 KDC 有一个共享密钥, 并且建立安全信道
529 | A 和 B 通信:
530 | A 向 KDC 请求
531 | KDC 鉴别 A 身份, 生成随机密钥分发给 A 和 B
532 | * KDC 负担重, 安全级别高
533 |
534 | 公钥密码体系带来的
535 | * 加解密
536 | * 数字签名
537 | * 开放环境中交换秘密的可能
538 |
539 | 实际中的使用
540 | 签密
541 | 签名和加密同时进行
542 | a 签名 加密 不安全信道
543 | M -> E_KRa -> E_KUb -> C ->
544 | 不安全信道 解密 验证签名 b
545 | -> C -> D_KRb -> E_KPa -> M
546 | 混合加密
547 | 通过公钥密码发送密钥, 使用这个密钥做对称密码加密
548 | 提供了安全信道
549 |
550 | ------------------------------------------------------------------------------
551 | 公钥密码数学基础
552 | * 素数, Fermat 小定理, Euler 函数和降幂公式, 中国剩余定理
553 | a^m equiv a^{m mod phi(n) + phi(n)} pmod n
554 | * Miller-Rabin 算法
555 | 有两个部分组成
556 | Fermat 小定理
557 | forall a : a^{p-1} equiv 1 pmod p
558 | Z_p 只有平凡 1 的剩余根
559 | b^2 equiv 1 pmod p <=> b = pm 1 pmod p
560 | 基本算法
561 | 随机选 a, 希望计算 a^{p-1} mod p
562 | 分解 p-1 = y 2^r, r 尽量大 i.e. y 是奇数
563 | 按照 a^{p-1} = sqr^r(a^y) 计算 a^{p-1},
564 | 过程中平方时检查是否遇到非平凡 1 的二次根
565 | 最后检查 a^{p-1} mod p 是否等于 1
566 | * 二次剩余
567 |
568 | RSA算法
569 | 算法
570 | 有
571 | p, q: 大素数, 保密
572 | n: n = pq
573 | e: e \perp phi(n), 一般是小整数
574 | d: de mod phi(n) = 1
575 | 则
576 | KU = n, e
577 | KR = d
578 | 加解密
579 | 要求消息 0 <= m < n:
580 | E_{KU}(m) = m^e mod n
581 | D_{KR}(c) = c^d mod n
582 | 强度
583 | RSA 不强于分解 (至今没有说明不弱于)
584 | 其他性质
585 | * 比 DES 慢 100 ~ 1000 倍
586 | 攻击
587 | 分解攻击
588 | 分解得到 n = pq, 之后得到 phi(n) = (p-1)(q-1),
589 | 然后通过 e 得到 d.
590 | 同模攻击
591 | 将同样消息加密发给多个人, 模数相同
592 | Eve 得到 m^e1, m^e2 ...
593 | 那么假设 e1, e2 互素, 就有 x1e1 + x2e2 = 1 (这里没有模)
594 | 那么容易恢复出 m: m = (m^e1)^x1 * (m^e2)^x2 (mod n)
595 | 小指数加密
596 | 假设 e 都取 3, Eve 得到
597 | m^3 mod n1, m^3 mod n2 ...
598 | 那么通过 CRT 得到 m^3, 然后开三次方得到 m
599 |
600 | Diffie-Hellman 密钥交换
601 | 有
602 | q: 素数
603 | g: Z^*_q 的原根
604 | 通过以下步骤, A, B 在不安全的信道上也能建立共享秘密
605 | A B
606 | 选择一个私有的 XA 选择私有的 XB
607 | 计算YA = g^XA 公开 计算 YB = g^XB 公开
608 | 共同秘密 K = YB^XA = YA^XB = g^XAXB
609 |
610 | 二次剩余加密
611 | 模素数
612 | 每个二次剩余有且只有两个平方根
613 | J_p(x) = x是二次剩余 ? 1 : -1
614 | J_p(x) = x^{(p-1)/2}
615 | J_p(xy) = J_p(x) J_p(y)
616 | 如果 p+1 equiv 0 mod 4, 那么 x^{(p+1)/4} 是 x 的一个平方根
617 | 模 N = pq
618 | y 是 N 的二次剩余 <=>
619 | y mod p 是 p 的二次剩余, y mod q 是 q 的二次剩余
620 | J_pq(y) = J_p(y) J_q(y)
621 | J_pq(xy) = J_pq(x) J_pq(y)
622 | 出现新的一类数, J_pq = 1 但是不是二次剩余, 用 QNR^{+1} 表示
623 | 已知 N 的分解 pq 时, 二次剩余判定是容易的,
624 | 只知道 N 时, 二次剩余判定是困难的
625 | Rabin 方法
626 | 有
627 | KR p q 素数
628 | KU N = pq
629 | 则加密 M < N:
630 | C = M^2 mod N
631 | 解密
632 | m1 = +- C^{(p+1)/4} mod p
633 | m2 = +- C^{(q+1)/4} mod q
634 | a = p (p^{-1} mod q), a equiv 0 mod p, a equiv 1 mod q
635 | b = q (q^{-1} mod p), b equiv 1 mod p, b equiv 0 mod q
636 | M = am1 + bm2
637 | 原理
638 | 考虑设 M = a' p + b' q
639 | Goldwasser Micali 方法
640 | 有
641 | KR p q 素数
642 | KU N = pq, z 是 QNR^{+1}
643 | 则加密 M in {0,1}
644 | C = z^m x^2, x 是随机选取的
645 | 解密
646 | 如果 C 是模 N 二次剩余, 则 M = 0
647 | 否则 M = 1
648 |
649 | ElGamal 加密
650 | 概率性的公钥算法
651 | 基本设置
652 | 有
653 | p 素数
654 | alpha Z_p 原根
655 | a 随机
656 | beta = alpha^a mod p
657 | 则
658 | KU = p, alpha, beta
659 | KR = a
660 | 加解密
661 | E_{KU}(m, k) = (alpha^k mod p, m beta^k mod p), k 是秘密随机数
662 | D_{KR}((c1, c2)) = (c1^a)^-1 c2
663 |
664 | ------------------------------------------------------------------------------
665 | 数字签名 - MAC
666 | 都可以保证消息完整性
667 | 数字签名:
668 | 不可抵赖
669 | 因为使用公钥算法, 慢 100 倍以上
670 |
671 | 数字签名 - 加密
672 | 签名使用私钥, 加密使用公钥
673 |
674 | 签名方法
675 | 直接使用 m 签名:
676 | 無消息攻击 按照签名伪造消息
677 | m1, s1; m2, s2 --> m1m2, s1s2
678 | 如上, 解决: 对消息 hash 之后签名
679 |
680 | RSA 签名
681 | 直接使用私钥加密即可,
682 | 签名
683 | sign(m) = gamma
684 | gamma = m^d
685 | 验证
686 | verify(m, gamma)
687 | v1 = gamma^e
688 | assert v1==m
689 | 问题:
690 | 無消息攻击: 可以按照签名伪造消息
691 | 乘积攻击: m1 m2 的签名是 s1 s2, sx 是 mx 的签名
692 | 解决: 签名 = H(M)^d, H 是抗碰撞的 Hash 函数
693 |
694 | ElGamal
695 | 签名
696 | sign(m, k) = (gamma, delta) k 随机, 和 p-1 互素
697 | gamma = alpha^k mod p
698 | delta = (m - a gamma) (k^{-1} mod p-1)
699 | 验证
700 | verify(m, gamma, delta)
701 | v1 = alpha m mod p
702 | v2 = beta^gamma gamma^delta mod p
703 | assert v1==v2
704 | 唯公钥伪造
705 | 有
706 | i,j < p-1
707 | j \perp p-1
708 | 令
709 | gamma = alpha^i beta^j mod p
710 | delta = -gamma j^{-1} mod p-1
711 | m = -gamma i j^{-1} mod p-1
712 | 解决: 对消息的 hash 签名
713 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | Attribution-NonCommercial-ShareAlike 4.0 International
2 |
3 | =======================================================================
4 |
5 | Creative Commons Corporation ("Creative Commons") is not a law firm and
6 | does not provide legal services or legal advice. Distribution of
7 | Creative Commons public licenses does not create a lawyer-client or
8 | other relationship. Creative Commons makes its licenses and related
9 | information available on an "as-is" basis. Creative Commons gives no
10 | warranties regarding its licenses, any material licensed under their
11 | terms and conditions, or any related information. Creative Commons
12 | disclaims all liability for damages resulting from their use to the
13 | fullest extent possible.
14 |
15 | Using Creative Commons Public Licenses
16 |
17 | Creative Commons public licenses provide a standard set of terms and
18 | conditions that creators and other rights holders may use to share
19 | original works of authorship and other material subject to copyright
20 | and certain other rights specified in the public license below. The
21 | following considerations are for informational purposes only, are not
22 | exhaustive, and do not form part of our licenses.
23 |
24 | Considerations for licensors: Our public licenses are
25 | intended for use by those authorized to give the public
26 | permission to use material in ways otherwise restricted by
27 | copyright and certain other rights. Our licenses are
28 | irrevocable. Licensors should read and understand the terms
29 | and conditions of the license they choose before applying it.
30 | Licensors should also secure all rights necessary before
31 | applying our licenses so that the public can reuse the
32 | material as expected. Licensors should clearly mark any
33 | material not subject to the license. This includes other CC-
34 | licensed material, or material used under an exception or
35 | limitation to copyright. More considerations for licensors:
36 | wiki.creativecommons.org/Considerations_for_licensors
37 |
38 | Considerations for the public: By using one of our public
39 | licenses, a licensor grants the public permission to use the
40 | licensed material under specified terms and conditions. If
41 | the licensor's permission is not necessary for any reason--for
42 | example, because of any applicable exception or limitation to
43 | copyright--then that use is not regulated by the license. Our
44 | licenses grant only permissions under copyright and certain
45 | other rights that a licensor has authority to grant. Use of
46 | the licensed material may still be restricted for other
47 | reasons, including because others have copyright or other
48 | rights in the material. A licensor may make special requests,
49 | such as asking that all changes be marked or described.
50 | Although not required by our licenses, you are encouraged to
51 | respect those requests where reasonable. More considerations
52 | for the public:
53 | wiki.creativecommons.org/Considerations_for_licensees
54 |
55 | =======================================================================
56 |
57 | Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International
58 | Public License
59 |
60 | By exercising the Licensed Rights (defined below), You accept and agree
61 | to be bound by the terms and conditions of this Creative Commons
62 | Attribution-NonCommercial-ShareAlike 4.0 International Public License
63 | ("Public License"). To the extent this Public License may be
64 | interpreted as a contract, You are granted the Licensed Rights in
65 | consideration of Your acceptance of these terms and conditions, and the
66 | Licensor grants You such rights in consideration of benefits the
67 | Licensor receives from making the Licensed Material available under
68 | these terms and conditions.
69 |
70 |
71 | Section 1 -- Definitions.
72 |
73 | a. Adapted Material means material subject to Copyright and Similar
74 | Rights that is derived from or based upon the Licensed Material
75 | and in which the Licensed Material is translated, altered,
76 | arranged, transformed, or otherwise modified in a manner requiring
77 | permission under the Copyright and Similar Rights held by the
78 | Licensor. For purposes of this Public License, where the Licensed
79 | Material is a musical work, performance, or sound recording,
80 | Adapted Material is always produced where the Licensed Material is
81 | synched in timed relation with a moving image.
82 |
83 | b. Adapter's License means the license You apply to Your Copyright
84 | and Similar Rights in Your contributions to Adapted Material in
85 | accordance with the terms and conditions of this Public License.
86 |
87 | c. BY-NC-SA Compatible License means a license listed at
88 | creativecommons.org/compatiblelicenses, approved by Creative
89 | Commons as essentially the equivalent of this Public License.
90 |
91 | d. Copyright and Similar Rights means copyright and/or similar rights
92 | closely related to copyright including, without limitation,
93 | performance, broadcast, sound recording, and Sui Generis Database
94 | Rights, without regard to how the rights are labeled or
95 | categorized. For purposes of this Public License, the rights
96 | specified in Section 2(b)(1)-(2) are not Copyright and Similar
97 | Rights.
98 |
99 | e. Effective Technological Measures means those measures that, in the
100 | absence of proper authority, may not be circumvented under laws
101 | fulfilling obligations under Article 11 of the WIPO Copyright
102 | Treaty adopted on December 20, 1996, and/or similar international
103 | agreements.
104 |
105 | f. Exceptions and Limitations means fair use, fair dealing, and/or
106 | any other exception or limitation to Copyright and Similar Rights
107 | that applies to Your use of the Licensed Material.
108 |
109 | g. License Elements means the license attributes listed in the name
110 | of a Creative Commons Public License. The License Elements of this
111 | Public License are Attribution, NonCommercial, and ShareAlike.
112 |
113 | h. Licensed Material means the artistic or literary work, database,
114 | or other material to which the Licensor applied this Public
115 | License.
116 |
117 | i. Licensed Rights means the rights granted to You subject to the
118 | terms and conditions of this Public License, which are limited to
119 | all Copyright and Similar Rights that apply to Your use of the
120 | Licensed Material and that the Licensor has authority to license.
121 |
122 | j. Licensor means the individual(s) or entity(ies) granting rights
123 | under this Public License.
124 |
125 | k. NonCommercial means not primarily intended for or directed towards
126 | commercial advantage or monetary compensation. For purposes of
127 | this Public License, the exchange of the Licensed Material for
128 | other material subject to Copyright and Similar Rights by digital
129 | file-sharing or similar means is NonCommercial provided there is
130 | no payment of monetary compensation in connection with the
131 | exchange.
132 |
133 | l. Share means to provide material to the public by any means or
134 | process that requires permission under the Licensed Rights, such
135 | as reproduction, public display, public performance, distribution,
136 | dissemination, communication, or importation, and to make material
137 | available to the public including in ways that members of the
138 | public may access the material from a place and at a time
139 | individually chosen by them.
140 |
141 | m. Sui Generis Database Rights means rights other than copyright
142 | resulting from Directive 96/9/EC of the European Parliament and of
143 | the Council of 11 March 1996 on the legal protection of databases,
144 | as amended and/or succeeded, as well as other essentially
145 | equivalent rights anywhere in the world.
146 |
147 | n. You means the individual or entity exercising the Licensed Rights
148 | under this Public License. Your has a corresponding meaning.
149 |
150 |
151 | Section 2 -- Scope.
152 |
153 | a. License grant.
154 |
155 | 1. Subject to the terms and conditions of this Public License,
156 | the Licensor hereby grants You a worldwide, royalty-free,
157 | non-sublicensable, non-exclusive, irrevocable license to
158 | exercise the Licensed Rights in the Licensed Material to:
159 |
160 | a. reproduce and Share the Licensed Material, in whole or
161 | in part, for NonCommercial purposes only; and
162 |
163 | b. produce, reproduce, and Share Adapted Material for
164 | NonCommercial purposes only.
165 |
166 | 2. Exceptions and Limitations. For the avoidance of doubt, where
167 | Exceptions and Limitations apply to Your use, this Public
168 | License does not apply, and You do not need to comply with
169 | its terms and conditions.
170 |
171 | 3. Term. The term of this Public License is specified in Section
172 | 6(a).
173 |
174 | 4. Media and formats; technical modifications allowed. The
175 | Licensor authorizes You to exercise the Licensed Rights in
176 | all media and formats whether now known or hereafter created,
177 | and to make technical modifications necessary to do so. The
178 | Licensor waives and/or agrees not to assert any right or
179 | authority to forbid You from making technical modifications
180 | necessary to exercise the Licensed Rights, including
181 | technical modifications necessary to circumvent Effective
182 | Technological Measures. For purposes of this Public License,
183 | simply making modifications authorized by this Section 2(a)
184 | (4) never produces Adapted Material.
185 |
186 | 5. Downstream recipients.
187 |
188 | a. Offer from the Licensor -- Licensed Material. Every
189 | recipient of the Licensed Material automatically
190 | receives an offer from the Licensor to exercise the
191 | Licensed Rights under the terms and conditions of this
192 | Public License.
193 |
194 | b. Additional offer from the Licensor -- Adapted Material.
195 | Every recipient of Adapted Material from You
196 | automatically receives an offer from the Licensor to
197 | exercise the Licensed Rights in the Adapted Material
198 | under the conditions of the Adapter's License You apply.
199 |
200 | c. No downstream restrictions. You may not offer or impose
201 | any additional or different terms or conditions on, or
202 | apply any Effective Technological Measures to, the
203 | Licensed Material if doing so restricts exercise of the
204 | Licensed Rights by any recipient of the Licensed
205 | Material.
206 |
207 | 6. No endorsement. Nothing in this Public License constitutes or
208 | may be construed as permission to assert or imply that You
209 | are, or that Your use of the Licensed Material is, connected
210 | with, or sponsored, endorsed, or granted official status by,
211 | the Licensor or others designated to receive attribution as
212 | provided in Section 3(a)(1)(A)(i).
213 |
214 | b. Other rights.
215 |
216 | 1. Moral rights, such as the right of integrity, are not
217 | licensed under this Public License, nor are publicity,
218 | privacy, and/or other similar personality rights; however, to
219 | the extent possible, the Licensor waives and/or agrees not to
220 | assert any such rights held by the Licensor to the limited
221 | extent necessary to allow You to exercise the Licensed
222 | Rights, but not otherwise.
223 |
224 | 2. Patent and trademark rights are not licensed under this
225 | Public License.
226 |
227 | 3. To the extent possible, the Licensor waives any right to
228 | collect royalties from You for the exercise of the Licensed
229 | Rights, whether directly or through a collecting society
230 | under any voluntary or waivable statutory or compulsory
231 | licensing scheme. In all other cases the Licensor expressly
232 | reserves any right to collect such royalties, including when
233 | the Licensed Material is used other than for NonCommercial
234 | purposes.
235 |
236 |
237 | Section 3 -- License Conditions.
238 |
239 | Your exercise of the Licensed Rights is expressly made subject to the
240 | following conditions.
241 |
242 | a. Attribution.
243 |
244 | 1. If You Share the Licensed Material (including in modified
245 | form), You must:
246 |
247 | a. retain the following if it is supplied by the Licensor
248 | with the Licensed Material:
249 |
250 | i. identification of the creator(s) of the Licensed
251 | Material and any others designated to receive
252 | attribution, in any reasonable manner requested by
253 | the Licensor (including by pseudonym if
254 | designated);
255 |
256 | ii. a copyright notice;
257 |
258 | iii. a notice that refers to this Public License;
259 |
260 | iv. a notice that refers to the disclaimer of
261 | warranties;
262 |
263 | v. a URI or hyperlink to the Licensed Material to the
264 | extent reasonably practicable;
265 |
266 | b. indicate if You modified the Licensed Material and
267 | retain an indication of any previous modifications; and
268 |
269 | c. indicate the Licensed Material is licensed under this
270 | Public License, and include the text of, or the URI or
271 | hyperlink to, this Public License.
272 |
273 | 2. You may satisfy the conditions in Section 3(a)(1) in any
274 | reasonable manner based on the medium, means, and context in
275 | which You Share the Licensed Material. For example, it may be
276 | reasonable to satisfy the conditions by providing a URI or
277 | hyperlink to a resource that includes the required
278 | information.
279 | 3. If requested by the Licensor, You must remove any of the
280 | information required by Section 3(a)(1)(A) to the extent
281 | reasonably practicable.
282 |
283 | b. ShareAlike.
284 |
285 | In addition to the conditions in Section 3(a), if You Share
286 | Adapted Material You produce, the following conditions also apply.
287 |
288 | 1. The Adapter's License You apply must be a Creative Commons
289 | license with the same License Elements, this version or
290 | later, or a BY-NC-SA Compatible License.
291 |
292 | 2. You must include the text of, or the URI or hyperlink to, the
293 | Adapter's License You apply. You may satisfy this condition
294 | in any reasonable manner based on the medium, means, and
295 | context in which You Share Adapted Material.
296 |
297 | 3. You may not offer or impose any additional or different terms
298 | or conditions on, or apply any Effective Technological
299 | Measures to, Adapted Material that restrict exercise of the
300 | rights granted under the Adapter's License You apply.
301 |
302 |
303 | Section 4 -- Sui Generis Database Rights.
304 |
305 | Where the Licensed Rights include Sui Generis Database Rights that
306 | apply to Your use of the Licensed Material:
307 |
308 | a. for the avoidance of doubt, Section 2(a)(1) grants You the right
309 | to extract, reuse, reproduce, and Share all or a substantial
310 | portion of the contents of the database for NonCommercial purposes
311 | only;
312 |
313 | b. if You include all or a substantial portion of the database
314 | contents in a database in which You have Sui Generis Database
315 | Rights, then the database in which You have Sui Generis Database
316 | Rights (but not its individual contents) is Adapted Material,
317 | including for purposes of Section 3(b); and
318 |
319 | c. You must comply with the conditions in Section 3(a) if You Share
320 | all or a substantial portion of the contents of the database.
321 |
322 | For the avoidance of doubt, this Section 4 supplements and does not
323 | replace Your obligations under this Public License where the Licensed
324 | Rights include other Copyright and Similar Rights.
325 |
326 |
327 | Section 5 -- Disclaimer of Warranties and Limitation of Liability.
328 |
329 | a. UNLESS OTHERWISE SEPARATELY UNDERTAKEN BY THE LICENSOR, TO THE
330 | EXTENT POSSIBLE, THE LICENSOR OFFERS THE LICENSED MATERIAL AS-IS
331 | AND AS-AVAILABLE, AND MAKES NO REPRESENTATIONS OR WARRANTIES OF
332 | ANY KIND CONCERNING THE LICENSED MATERIAL, WHETHER EXPRESS,
333 | IMPLIED, STATUTORY, OR OTHER. THIS INCLUDES, WITHOUT LIMITATION,
334 | WARRANTIES OF TITLE, MERCHANTABILITY, FITNESS FOR A PARTICULAR
335 | PURPOSE, NON-INFRINGEMENT, ABSENCE OF LATENT OR OTHER DEFECTS,
336 | ACCURACY, OR THE PRESENCE OR ABSENCE OF ERRORS, WHETHER OR NOT
337 | KNOWN OR DISCOVERABLE. WHERE DISCLAIMERS OF WARRANTIES ARE NOT
338 | ALLOWED IN FULL OR IN PART, THIS DISCLAIMER MAY NOT APPLY TO YOU.
339 |
340 | b. TO THE EXTENT POSSIBLE, IN NO EVENT WILL THE LICENSOR BE LIABLE
341 | TO YOU ON ANY LEGAL THEORY (INCLUDING, WITHOUT LIMITATION,
342 | NEGLIGENCE) OR OTHERWISE FOR ANY DIRECT, SPECIAL, INDIRECT,
343 | INCIDENTAL, CONSEQUENTIAL, PUNITIVE, EXEMPLARY, OR OTHER LOSSES,
344 | COSTS, EXPENSES, OR DAMAGES ARISING OUT OF THIS PUBLIC LICENSE OR
345 | USE OF THE LICENSED MATERIAL, EVEN IF THE LICENSOR HAS BEEN
346 | ADVISED OF THE POSSIBILITY OF SUCH LOSSES, COSTS, EXPENSES, OR
347 | DAMAGES. WHERE A LIMITATION OF LIABILITY IS NOT ALLOWED IN FULL OR
348 | IN PART, THIS LIMITATION MAY NOT APPLY TO YOU.
349 |
350 | c. The disclaimer of warranties and limitation of liability provided
351 | above shall be interpreted in a manner that, to the extent
352 | possible, most closely approximates an absolute disclaimer and
353 | waiver of all liability.
354 |
355 |
356 | Section 6 -- Term and Termination.
357 |
358 | a. This Public License applies for the term of the Copyright and
359 | Similar Rights licensed here. However, if You fail to comply with
360 | this Public License, then Your rights under this Public License
361 | terminate automatically.
362 |
363 | b. Where Your right to use the Licensed Material has terminated under
364 | Section 6(a), it reinstates:
365 |
366 | 1. automatically as of the date the violation is cured, provided
367 | it is cured within 30 days of Your discovery of the
368 | violation; or
369 |
370 | 2. upon express reinstatement by the Licensor.
371 |
372 | For the avoidance of doubt, this Section 6(b) does not affect any
373 | right the Licensor may have to seek remedies for Your violations
374 | of this Public License.
375 |
376 | c. For the avoidance of doubt, the Licensor may also offer the
377 | Licensed Material under separate terms or conditions or stop
378 | distributing the Licensed Material at any time; however, doing so
379 | will not terminate this Public License.
380 |
381 | d. Sections 1, 5, 6, 7, and 8 survive termination of this Public
382 | License.
383 |
384 |
385 | Section 7 -- Other Terms and Conditions.
386 |
387 | a. The Licensor shall not be bound by any additional or different
388 | terms or conditions communicated by You unless expressly agreed.
389 |
390 | b. Any arrangements, understandings, or agreements regarding the
391 | Licensed Material not stated herein are separate from and
392 | independent of the terms and conditions of this Public License.
393 |
394 |
395 | Section 8 -- Interpretation.
396 |
397 | a. For the avoidance of doubt, this Public License does not, and
398 | shall not be interpreted to, reduce, limit, restrict, or impose
399 | conditions on any use of the Licensed Material that could lawfully
400 | be made without permission under this Public License.
401 |
402 | b. To the extent possible, if any provision of this Public License is
403 | deemed unenforceable, it shall be automatically reformed to the
404 | minimum extent necessary to make it enforceable. If the provision
405 | cannot be reformed, it shall be severed from this Public License
406 | without affecting the enforceability of the remaining terms and
407 | conditions.
408 |
409 | c. No term or condition of this Public License will be waived and no
410 | failure to comply consented to unless expressly agreed to by the
411 | Licensor.
412 |
413 | d. Nothing in this Public License constitutes or may be interpreted
414 | as a limitation upon, or waiver of, any privileges and immunities
415 | that apply to the Licensor or You, including from the legal
416 | processes of any jurisdiction or authority.
417 |
418 | =======================================================================
419 |
420 | Creative Commons is not a party to its public
421 | licenses. Notwithstanding, Creative Commons may elect to apply one of
422 | its public licenses to material it publishes and in those instances
423 | will be considered the “Licensor.” The text of the Creative Commons
424 | public licenses is dedicated to the public domain under the CC0 Public
425 | Domain Dedication. Except for the limited purpose of indicating that
426 | material is shared under a Creative Commons public license or as
427 | otherwise permitted by the Creative Commons policies published at
428 | creativecommons.org/policies, Creative Commons does not authorize the
429 | use of the trademark "Creative Commons" or any other trademark or logo
430 | of Creative Commons without its prior written consent including,
431 | without limitation, in connection with any unauthorized modifications
432 | to any of its public licenses or any other arrangements,
433 | understandings, or agreements concerning use of licensed material. For
434 | the avoidance of doubt, this paragraph does not form part of the
435 | public licenses.
436 |
437 | Creative Commons may be contacted at creativecommons.org.
438 |
--------------------------------------------------------------------------------
/compiler_techniques/compile.tex:
--------------------------------------------------------------------------------
1 | % 编译使用xelatex
2 | \documentclass{ctexart}
3 |
4 | \usepackage{amsmath}
5 | \usepackage{amsfonts}
6 | \usepackage{amssymb}
7 | \usepackage{tabularx}
8 | \usepackage{bigfoot}
9 | \usepackage{fancyvrb}
10 | \usepackage{graphicx}
11 | \usepackage{subfig}
12 | \usepackage{placeins} % \FloatBarrier
13 | % define new stretchable column types
14 | \newcolumntype{L}{>{\raggedright\arraybackslash}X}
15 | \newcolumntype{R}{>{\raggedleft\arraybackslash}X}
16 | \newcolumntype{C}{>{\centering\arraybackslash}X}
17 |
18 | \DeclareMathOperator{\Def}{\;\mathbf{Def}\;}
19 | \DeclareMathOperator{\Ref}{\;\mathbf{Ref}\;}
20 |
21 | % for simulating booktabs rules; so they work with vertical lines
22 | \newlength{\Oldarrayrulewidth}
23 | \newcommand{\Cline}[2]{
24 | \noalign{\global\setlength{\Oldarrayrulewidth}{\arrayrulewidth}}
25 | \noalign{\global\setlength{\arrayrulewidth}{#1}}\cline{#2}
26 | \noalign{\global\setlength{\arrayrulewidth}{\Oldarrayrulewidth}}}
27 | \newcommand{\Hline}[1]{
28 | \noalign{\global\setlength{\Oldarrayrulewidth}{\arrayrulewidth}}
29 | \noalign{\global\setlength{\arrayrulewidth}{#1}}\hline
30 | \noalign{\global\setlength{\arrayrulewidth}{\Oldarrayrulewidth}}}
31 | \newcommand{\Topline}{\Hline{0.08em}}
32 | \newcommand{\Bottomline}{\Hline{0.08em}}
33 | \newcommand{\Midline}{\Hline{0.05em}}
34 | \newcommand{\CMidLine}[1]{\Cline{0.05em}{#1}}
35 |
36 |
37 | \title{编译原理}
38 |
39 | \begin{document}
40 | \maketitle
41 |
42 | \tableofcontents
43 |
44 | \section{词法分析}
45 | \paragraph{目的}
46 | 将字符流转为符号流.
47 | \paragraph{符号定义} \begin{itemize}
48 | \item 巴克斯范式
49 | \item 正则表达式
50 | \item 混合 (如lex的实现)
51 | \end{itemize}
52 | \paragraph{解析方式}
53 | 基本通过正则表达式语言和有限状态自动机的等价原理, 将符号定义转为正则表达式,
54 | 之后变为有限状态自动机, $\epsilon$-NFA到NFA到DFA.\par
55 | 通常采取最长匹配方法, 即允许自动机向前偷看一个符号.
56 | 如果当前满足一个符号, 但是按照偷看符号转移后不满足任何符号, 则返回当前匹配的符号.
57 |
58 | \section{语法分析}
59 | \paragraph{目的}
60 | 将符号串解析为符合程序语法的生成树
61 | \paragraph{语法描述}
62 | 通过上下文无关语言描述语法. 后记此语法为$G[S]$
63 | \paragraph{惯例} \begin{itemize}
64 | \item 终结符串最后还认为有一个终止符号$\$$ (课程中是$\#$)
65 | \item $\alpha, \beta \ldots \in (V\cup T)^*$
66 | \item $w \in T^*$
67 | \item $a, b \ldots \in T$
68 | \item $A, B \ldots \in V$
69 | \item $X, Y \ldots \in V\cup T$
70 | \end{itemize}
71 | \subsection{自顶向下分析}
72 | 从起始符号$S$开始, 不断利用产生式展开非终结符, 直到原串.\par
73 | \paragraph{不确定性} 这样的方法有两个不确定性 \begin{enumerate}
74 | \item 选择哪一个非终结符展开
75 | \item 选择哪一个产生式
76 | \end{enumerate}\par
77 | 第一个问题很容易, 每次都选择当前符号串$w A \alpha$的最左边的非终结符$A$展开.
78 | \subsubsection{产生式的选择}
79 | \paragraph{向前查看} 对于$wA\alpha$, 选择产生式如果只考虑$A$, 则没有任何线索, 能够确定选择哪一个产生式.\par
80 | 设$\alpha = X_1 X_2 \ldots X_l$.
81 | 由上, 选择产生式时,
82 | 不仅考虑$A$, 还考虑$X_1, X_2 \ldots X_k,\; (k < l)$,
83 | 选择的产生式$p$是它们的函数$p = f(A, X_1 \ldots X_k)$.\par
84 | 称这种方法为LL(k)方法. 下述LL(1)方法.
85 | \paragraph{First函数} First是符号串的函数, 定义如下\[
86 | First(\alpha) = \{a \in T \;|\; \exists \beta\,:\,\alpha \Rightarrow^* a\beta\}\]
87 | 特别地, 如果$\alpha \Rightarrow^* \epsilon$, 则说$\epsilon \in First(\alpha)$.
88 | 注意$First$定义给$\alpha$, 可能包含$\epsilon$.\par
89 | First集合的意义是符号串对应的终结符串的首个终结符的取值集合.
90 | \paragraph{Follow函数} Follow是非终结符的函数, 定义如下\[
91 | Follow(A) = \{a \in T \;|\; \exists \alpha, \beta \,:\, S\$ \Rightarrow^* \alpha A \beta\$, a \in First(\beta\$)\}
92 | \]
93 | 注意$Follow$定义给$A$, 可能包含$\$$.\par
94 | Follow集合的意思是$G[S]$句型中中, 可能跟在$A$的后面的终结符所有取值.
95 | \paragraph{选择产生式} 希望展开$A$, 并且$\text{lookahead} = a$, 则选择的产生式$A \to \beta$应当满足\[
96 | a \in PS(A \to \beta) = \begin{cases}
97 | First(\beta) & \epsilon \not\in First(\beta)\\
98 | First(\beta) \cup Follow(A) - \{\epsilon\} & \epsilon \in First(\beta)
99 | \end{cases} \]
100 | 如果$\forall A \in V, a \in T$有$\Big|\{A \to \beta \;|\; a \in PS(A \to \beta)\}\Big| = 1$ (即产生式的选择唯一),
101 | 则称$G[S]$是LL(1)文法, 可以按照上述方法解析.
102 | \subsubsection{First和Follow的计算}
103 | \paragraph{First的计算} 考虑计算$First(\alpha)$, 设$\alpha = X_1 X_2 \ldots X_k$.\par
104 | 若$X_1\ldots X_{l-1}$可空, $X_l$不可空 (显然终结符不可空), 有\[
105 | First(\alpha) = \cup_{1 \le i \le l} \left( First(X_i) - \{\epsilon\} \right) \]\par
106 | 若$X_1 \ldots X_k$都可空, 则\[
107 | First(\alpha) = \cup_{1 \le i \le k} First(X_i)\]\par
108 | 非终结符$First(A) = \cup_{\beta\;:\; A \to \beta} First(\beta)$.\par
109 | 通过迭代即可求解如上的集合约束问题.
110 | \paragraph{Follow的计算} 考虑计算$Follow(A)$. 首先寻找所有形如$B \to \alpha A \beta$的产生式.\par
111 | 如果$\epsilon \not\in First(\beta)$, 则$Follow(A) \supseteq First(\beta)$.\par
112 | 如果$\epsilon \in First(\beta)$, 则$Follow(A) \supseteq First(\beta) \cup Follow(B) - \{\epsilon\}$.\par
113 | 通过迭代求解如上的集合约束问题, 要求$Follow(A)$取最小 (即满足上述条件的最小集合).
114 | \subsubsection{递归下降程序} 每个非终结符的解析都是一个函数.
115 | 其中根据lookahead选择产生式, 匹配非终结符就调用对应函数, 匹配非终结符就直接\texttt{match\_token}.\par
116 | 常见的递归下降程序如\begin{verbatim}
117 | void parse_B()
118 | {
119 | switch (lookahead) {
120 | case c:
121 | // B -> c A d, First(c A d) = {c}
122 | match_token(c);
123 | parse_A()
124 | match_token(d);
125 | break;
126 | case b:
127 | // B -> epsilon, Follow(B) = {b}
128 | break;
129 | default:
130 | // no production matches here
131 | report error;
132 | }
133 | }\end{verbatim}
134 | \subsubsection{表驱动程序}
135 | 记$M[A, a]$表示希望展开$A$, $\text{lookahead} = a$时, 按照上述方法得到的可用产生式.\par
136 | 表驱动利用一个栈, 最初栈中只有$S\$$, $S$在栈顶. 之后重复检查栈顶$X$和输入$\text{lookahead} = a$ \begin{itemize}
137 | \item $X = \$$, 分析完成
138 | \item $X \in T$, 此时应有$X$等于$a$. 符合则消耗$a$, 否则报错
139 | \item $X \in V$, 设$M[X, a] = X \to \alpha$, 则$X$出栈, $\alpha$从右到左入栈
140 | \end{itemize}
141 | \subsubsection{语法变换}
142 | 通过消除左递归和公共左公因子, 能将很多非LL(1)语言转换为LL(1)语言, 如$S \to Sa | b$.
143 | \paragraph{去除直接左递归} 原为\begin{align*}
144 | A & \to A \alpha_1 \,|\, A \alpha_2 \,|\, \ldots \,|\, \beta_1 \,|\, \beta_2 \,|\, \ldots
145 | \end{align*}改写为\begin{align*}
146 | A &\to \beta_1 B \,|\, \beta_2 B \,|\, \ldots \\
147 | B &\to \alpha_1 B \,|\, \alpha_2 B \,|\, \ldots \,|\, \epsilon
148 | \end{align*}
149 | \paragraph{去除间接左递归} 要求原文法无环$A \not\Rightarrow^+ A$, 无$\epsilon$产生式$A \to \epsilon$.\par
150 | 首先将非终结符编号为$A_1, A_2 \ldots A_n$, 然后\begin{verbatim}
151 | for i = 1 to n
152 | for j = 1 to i-1
153 | 对于所有 A_i -> A_j \alpha,
154 | 代替以 A_i -> \beta \alpha,
155 | 其中 A_j -> \beta
156 | 消除A_i的直接递归
157 | \end{verbatim}
158 | \paragraph{消除左公因子} 直接将左公因子作为新的符号即可.
159 | \subsection{Bottom-up parsing}
160 | 从输入串开始, 不停将串中一个子串规约成一个非终结符, 直到规约成$S$.
161 | \paragraph{不确定性} 有2个不确定性
162 | \begin{enumerate}
163 | \item 选择哪个子串
164 | \item 选择哪个产生式, 如果有多个产生式的右边一样
165 | \end{enumerate}
166 | 采用一类称为LR分析的解决此问题.
167 | 每次都选择``句柄'', 选择的产生式通过LR分析表确定.
168 | \subsubsection{概念}
169 | 以下是语法本身的概念
170 | \begin{description}
171 | \item[短语] 如果$S\Rightarrow^* \alpha A \beta$且$A \Rightarrow^+ \gamma$,
172 | 则称$\gamma$是$\alpha A \beta$中$A$的短语.\\
173 | 在$\alpha A \beta$确定的情况下, 可以称$\gamma$是$A$的短语.
174 | 这时在$S \Rightarrow^* \alpha \gamma \beta$的分析树中,
175 | $\gamma$是$A$的子树的叶遍历, 并且要求$A$本身不能是叶子.
176 | \item[直接短语] $S\Rightarrow^* \alpha A \beta$且$A \Rightarrow \gamma$,
177 | 则称$\gamma$是$\alpha A \beta$中$A$的直接短语.\\
178 | 直接短语就是可以一步推出的短语.
179 | 在$\alpha \gamma \beta$的分析树中, 对应的是$A$所有儿子都是叶子的情况.
180 | \item[句柄] $S \Rightarrow^* \alpha A w$且$A \Rightarrow \beta$,
181 | 则称$\beta$是$\alpha A w$中$A$的句柄.\par
182 | 句柄就是从左到右第一个出现的直接短语.
183 | 可以理解为``最右推导''反过来就是``最左规约''.
184 | \end{description}
185 | \subsubsection{通用框架}
186 | 所有LR分析, 分析框架是一样的, 只有分析表是不一样的.\par
187 | 基本分析方法就是基于一个栈, 从左到右扫描输入串,
188 | 每次根据当前控制状态, 栈中内容 (不限于栈顶), 下一个输入符号来决定动作, 并且转移到新的控制状态.\par
189 | 所有LR分析都有一个$action[i, a]$表
190 | \[\begin{array}{ll}
191 | \Topline
192 | action[i, a] & \text{含义}\\
193 | \Midline
194 | \text{shift}\,j & \text{将}a\text{移入栈中, 状态变为}j \\
195 | \text{reduce}\,A\to\alpha & \text{此时栈顶应为}\alpha\text{, 规约栈顶. 之后}A\text{入栈, 状态变为}goto[i, A] \\
196 | \text{success} & \text{分析成功, 即将原输入串规约成了}S \\
197 | \text{error} & \text{出错} \\
198 | \Bottomline
199 | \end{array}\]\par
200 | 所有LR分析都有一个$goto[i, A]$表.
201 | $goto[i, A] = j$表示当前状态为$i$,
202 | 一个非终结符$A$入栈后, 那么状态变为$j$.\par
203 | 所有LR分析都是用增广文法, 即加入产生式 $S' \to S$的文法$G[S']$. $S' \to .S$对应的状态为初状态.
204 |
205 | \subsubsection{LR(0)分析表}
206 | \paragraph{item} 加点的产生式. 为了方便认识用方括号括起.
207 | \paragraph{closure} 考虑item间的关系$R$, 为$[A \to \alpha . B \beta] \;R\; [B \to .\gamma]$.
208 | 定义$closure(I)$等于$I$在关系$R$的传递闭包, $I$是item集合.
209 | \paragraph{go} $I \text{ is closure},\;X \in V \cup T$\[
210 | go(I, X) = closure(\{[A \to \alpha X .\beta] \;|\; [A \to \alpha . X \beta] \in I\})\]
211 | \paragraph{action} \[action[i, a] = \begin{cases}
212 | \text{shift}\; j & J = go(I, a)\\
213 | \text{reduce}\; A\to\beta & [A \to \beta.] \in I\\
214 | \text{accept} & [S' \to S.] \in I\\
215 | \text{error} & \text{otherwise}
216 | \end{cases}\]
217 | \paragraph{goto} \[goto[i, A] = j \quad J = go(I, A)\]
218 |
219 | \subsubsection{SLR(1)分析表}
220 | 基本同LR(0), 但是完成reduce的条件变得更精确.\par
221 | 原理: 完成规约$A\to\alpha$时, 下一个输入$a$应当满足$a \in Follow(A)$.
222 | \paragraph{action} \[action[i, a] = \begin{cases}
223 | \text{shift}\; j & J = go(I, a)\\
224 | \text{reduce}\; A\to\beta & [A \to \beta.] \in I \quad\land\quad a \in Follow(A)\\
225 | \text{accept} & [S' \to S.] \in I \quad\land\quad a = \$\\
226 | \text{error} & \text{otherwise}
227 | \end{cases}\]
228 |
229 | \subsubsection{LR(1)分析表}
230 | \paragraph{item} 之前是一遇到可规约的$A \to \alpha.$就规约 (规则2.),
231 | 但是不一定是这样 (前看$\alpha$之后一个符号得到的信息被忽略了).\par
232 | item的表示是$[A \to \alpha . \beta, a],\quad a \in T$. 如果$\beta \neq \epsilon$, 那么一切和SLR一样.\par
233 | 如果$\beta = \epsilon$, 那么当且仅当$a \in Follow(A)$时, 才进行规约.
234 | \paragraph{closure} $[A \to \alpha . B \beta, a] \; \mathbf{R} \; [B \to .\gamma, b] \quad b \in \mathbf{FIRST}(\beta a)$
235 | 如上关系的闭包.
236 | \paragraph{构造go} $go(I, X) = closure([A \to \alpha X. \beta] \,|\, [A \to \alpha . X \beta] \in I)$
237 | \paragraph{构造item集} 从$closure([S' \to .S, \$])$按照$go$拓展得图.
238 | \paragraph{初态} $I_0 = closure(\{[S' \to .S,\, \$]\})$
239 | \paragraph{action} 更精确的描述了完成reduce的条件.
240 | % 原理: 完成规约$A\to\alpha$时, 下一个输入$a$应当满足$a \in Follow(A\to\alpha)$,
241 | % 即各个产生式之后能跟的符号是不同的. 如$S \to Ab;\quad A \to Aa\;|\;\epsilon$.
242 | % 产生式$A\to Aa$之后只能跟$b$,
243 | %
244 | %$
245 | %S ::= A a | b A c | d c | b d a
246 | %
247 | %A ::= d
248 | %$
249 | % TODO
250 | \[ action[I, a] = \begin{cases}
251 | \text{shift}\; go(I, a) & [A \to \alpha .a \beta,\, b] \in I\\
252 | \text{reduce}\; A\to\alpha & [A \to \alpha.,\, a] \in I,\, A\to\alpha \neq S'\to S\\
253 | \text{accept} & [S' \to S., \$] \in I\\
254 | \text{error} & \text{otherwise}\end{cases} \]
255 | \paragraph{LR(1)但非SLR(1)的例子} \begin{align*}
256 | S &\to L = R \quad | \quad R\\
257 | L &\to *R \quad | \quad \text{id}\\
258 | R &\to L
259 | \end{align*}问题就出在, $S\to R$和$L \to *R$中的$R$是不一样的$R$.
260 | 如果把它们分成分开变成两个非终结符$R_1$和$R_2$, 容易发现$Follow(R_1) = \{\$\}$,
261 | 而$Follow(R_2) = \{ \$, = \}$. 也就是说, 分析的过程中就已经限定了非终结符的一些性质,
262 | 在用其一般的$Follow$去套, 自然就可能出现问题.
263 |
264 | \subsubsection{LALR(1)}
265 | 和LR(0)一样状态数目.
266 | 其余方法和LR(1)相同.
267 | \paragraph{合并LR(1)同芯状态} 同芯: $[A\to\alpha.\beta, a]$中$A\to\alpha.\beta$相同
268 |
269 |
270 | \section{符号表}
271 | \paragraph{基本概念}
272 | 不同的作用域有自己的符号表, 某处可见的符号是其自身包含所有父亲作用域 (亦称开作用域) 符号表的并.
273 | \paragraph{实现} 可以实现为全局的一个符号表, 也可以实现成各个作用域符号表的栈.
274 |
275 | \section{基于语法的语义计算}
276 | \subsection{基于属性文法}
277 | \subsubsection{基本概念}
278 | \paragraph{定义}
279 | 在CFG基础上, 对每个$X \in V$关联属性, 记为$X.a, X.b$等.\par
280 | 每个产生式$A\to X_1 X_2 \ldots X_N,\; X_i \in V \cup T, X_0 = A$关联一个语法动作,
281 | 语法动作是若干个属性计算的序列, 每个属性计算形如$X_i.a = f(X_j.b \;|\; j \neq i, b\text{是}X_j\text{的属性}),\; 0 \le m \le N$.
282 | \paragraph{综合属性} $A \to X_0 \ldots X_N$关联的语法动作是$A.a = f(X_i.b)$则称$A.a$是综合属性.
283 | 综合属性代表语法树中自下而上传递的信息.
284 | \paragraph{继承属性} $A \to X_0 \ldots X_N$关联的语法动作是$X_i.a = f(X_j.b),X_i \neq A$则称$X_i.a$是继承属性.
285 | 继承属性代表语法树中自上而下传递的信息.
286 | \paragraph{S-属性文法} 只包含综合属性的属性文法称S-属性文法.
287 | \paragraph{L-属性文法} 允许综合属性和继承属性, 但是语法动作要求有$X_i.a = f(X_{\neq i}.b) =f(X_{< i}.b)$.
288 | 即产生式中某符号的属性不能依赖产生式中位于它之后的符号的属性.
289 |
290 | \subsubsection{两趟方法的属性计算}
291 | 生成语法树之后, 计算属性的大致步骤如下
292 | \begin{enumerate}
293 | \item 分析语法树中结点属性 (即符号属性) 的依赖关系
294 | \item 如果依赖关系不存在环, 则按照依赖关系的拓扑顺序计算属性值.\\
295 | 如果依赖关系有环, 认为属性语法不是良定义的, 不予处理.\par
296 | \end{enumerate}
297 | 两趟方法通用, 但是效率开销较大.
298 |
299 | \subsubsection{一趟方法的属性计算}
300 | \paragraph{S-属性文法} 采用自底向上文法分析 (如LR), 每次按$A \to X_1 X_2 \ldots X_N$进行规约时, 一定有$X_1, X_2 \ldots X_N$是栈顶$N$个元素.
301 | 因此将元素属性一并存放在栈中, 每次规约时直接通过栈顶元素$N$个元素$X_1, X_2\ldots X_N$的属性计算$A.a$即可.
302 | \paragraph{L-属性文法} \label{onepass-l-attr-grammar} 采用自顶向下文法分析 (如LL(1)递归下降),
303 | 将继承属性作为参数传入子符号的分析过程, 并且要求子符号分析过程返回子符号的综合属性.\par
304 | 即将$\mathbf{Parse}(A) \to \mathtt{void}$改为$\mathbf{Parse}(A, A.i) \to A.s$, 其中$A.i$表示$A$的继承属性, $A.s$表示$A$的综合属性.
305 |
306 | \subsection{基于翻译模式}
307 | \subsubsection{基本概念}
308 | \paragraph{翻译模式} 类似属性文法, 但是允许语法动作出现在产生式中任何地方, 表示匹配到该处时即刻执行该语法动作.
309 | \paragraph{消除左递归} 讨论消除左递归时, 保持翻译动作等价. 以直接左递归为例\begin{align*}
310 | A & \to A_1 \alpha \; \{ A.s = f(A_1.s, \alpha.s)\}\\
311 | A & \to \beta \; \{ A.s = g(\beta.s)\}
312 | \end{align*}
313 | 按照消除直接左递归的方法变换之后, 变为 \begin{align*}
314 | A & \to \beta \; \{ R.i = g(\beta.s) \} \; R \; \{A.a = f(R.s)\}\\
315 | R & \to \epsilon \; \{ R.s = R.i\}\\
316 | R & \to \alpha \; \{ R_1.i = f(R.i, \alpha.s) \}\; R_1 \; \{ R.s = R_1.s \}
317 | \end{align*}
318 | 可以认为$R.i$中保存了原来式子中所有其左边的所有信息.
319 | \subsubsection{自上而下计算}
320 | 类似\ref{onepass-l-attr-grammar}, 但是计算属性在$\mathbf{parse(A)}$过程中进行.
321 | \subsubsection{自下而上计算}
322 | 使用从下而上分析方法, 将综合属性和符号一起存储在栈中.
323 | 这样有一个问题, 无法访问继承属性, 因为需要在确定非终结符前完成语法动作.
324 | 因此需要提供一种方法实现继承属性的访问.\par
325 | 可以将语法稍作变换, 添加若干空非终结符,
326 | 由它们完成中间的语法规则的计算,
327 | 使得产生式中间只有复写规则$X_i.i = X_j.s,\quad j < i$.\par
328 | 这样可以沿着复写规则, 对于继承属性$X_i.i$,
329 | 最终找到综合属性$X_j.s$,可以用$X_j.s$来代替$X_i.i$.\par
330 | 注意如果有多个产生式中都复写了$A.i = B.s$, 则需要保证这些产生式中,
331 | $A$和$B$的相对位置不变, i.e. $A$和$B$之间隔多少个符号是定值,
332 | 这样生成规约时代码 (参见图\ref{gen-deduction-code}) 片段.
333 |
334 | \begin{figure}%
335 | \centering
336 | \subfloat[最初的翻译模式]{{\includegraphics[width=0.95\textwidth]{trans-ex-1.png}}}%
337 | \\
338 | \subfloat[转换为适合自下而上的翻译模式]{{\includegraphics[width=0.95\textwidth]{trans-ex-2.png}}}%
339 | \\
340 | \subfloat[规约时代码片段]{{\includegraphics[width=0.95\textwidth]{trans-ex-3.png}}}%
341 | \caption{自下而上计算的例子}%
342 | \label{gen-deduction-code}
343 | \end{figure}
344 |
345 | \section{中间代码生成}
346 | \subsection{静态语义分析}
347 | 包含类型检查, 作用域分析, 控制流检查等. 通过翻译模式实现.
348 | \subsection{中间代码生成}
349 | 课程中只考虑三地址码的生成.
350 | \subsubsection{布尔表达式的翻译}
351 | \paragraph{直接翻译} 不处理短路特性. 相当于普通表达式的计算.
352 | \paragraph{L-属性文法}
353 | 每个表达式$E$有继承属性$E.true$和$E.false$, 这些属性分别都是标号. 具体如图.
354 | 实现如图\ref{bool-1}
355 | \begin{figure*}[ht]
356 | \centering
357 | \includegraphics[width=0.9\textwidth]{bool-1.png}
358 | \caption{L-属性文法的布尔短路翻译}
359 | \label{bool-1}
360 | \end{figure*}
361 | 如果翻译循环等, 还需要加入$S.next$等.
362 | \paragraph{S-属性文法}
363 | 将标号作为综合属性, 待顶层布尔表达式翻译完后,
364 | 将所有下层表达式中未知的标号回填. 称为拉链方法 (backpatching).\par
365 | 表达式$E$有两种综合属性$E.truelist$和$E.falselist$,
366 | 包含未确定的, 但是知道是跳转到真还是假的标号.\par
367 | 还包含函数如表\ref{backpatch-func}
368 | \begin{table}[ht!]
369 | \centering
370 | \begin{tabularx}{\textwidth}{cL}
371 | \Topline
372 | 函数名 & 行为\\
373 | \Midline
374 | $makelist(initial\_entry)\;\to list$ & 创建一个待回填的表\\
375 | $merge(list, list)\;\to list$ & 返回两个表的合并\\
376 | $backpatch(list, lbl)$ & 确定$list$中的标号为$lbl$\\
377 | \Bottomline
378 | \end{tabularx}
379 | \caption{拉链方法的函数}
380 | \label{backpatch-func}
381 | \end{table}
382 | 实现如图\ref{bool-2}
383 | \begin{figure*}[ht]
384 | \centering
385 | \includegraphics[width=0.9\textwidth]{bool-2.png}
386 | \caption{S-属性文法的布尔短路翻译}
387 | \label{bool-2}
388 | \end{figure*}
389 |
390 | \subsubsection{控制流的翻译}
391 | \paragraph{L-属性文法}
392 | 如图\ref{ctrlflow-1},
393 | 循环通过继承属性$S.next$实现, break语句通过继承属性$S.break$传递.
394 | \begin{figure*}[ht]
395 | \centering
396 | \includegraphics[width=0.9\textwidth]{ctrlflow-1.png}
397 | \caption{L-属性文法的控制流翻译}
398 | \label{ctrlflow-1}
399 | \end{figure*}
400 |
401 | \paragraph{S-属性文法}
402 | 如图\ref{ctrlflow-2},
403 | 循环通过综合属性$S.nextlist$实现, break语句通过综合属性$S.breaklist$传递.
404 | \begin{figure*}[ht]
405 | \centering
406 | \includegraphics[width=0.9\textwidth]{ctrlflow-2.png}
407 | \caption{S-属性文法的控制流翻译}
408 | \label{ctrlflow-2}
409 | \end{figure*}
410 | \FloatBarrier
411 |
412 | \section{运行时存储组织}
413 | \subsection{存储分配策略}
414 | 决定变量如何安排在内存中. 可以分为静态和动态分配, 其中动态分配又分为栈式和堆式.
415 | \paragraph{静态分配}
416 | 编译期确定变量在内存中的位置, 常用于全局变量或\verb/static/变量.
417 | 这样无法处理递归函数.
418 | \paragraph{栈式分配}
419 | 空间的分配根据进入退出函数在运行期确定.
420 | 可有效实现动态嵌套的程序结构.
421 | \paragraph{堆式分配}
422 | 最灵活, 需要操作系统和程序员或运行时内存系统共同管理.
423 | \subsection{活动记录}
424 | 活动记录即栈帧, 其中保存一个函数调用的所有相关数据, 包括实参, 返回地址, 局部变量, 保存的寄存器等等.\par
425 | \subsubsection{不允许嵌套函数}
426 | 这种情况下, 进入一个函数分配一个栈帧, 从函数中返回就回收其 栈帧 .
427 | 每个函数能看到的符号只有全局符号和自己栈帧中的符号.
428 | \subsubsection{允许嵌套函数}
429 | \paragraph{基本约定}
430 | 称被嵌套的函数是子函数, 嵌套其他函数的函数是父函数.
431 | 一个子函数有且仅有一个父函数, 只有父函数的函数体中才能调用子函数.\par
432 | 子函数能够访问父函数的变量\footnote{这里变量声明都类似Pascal语言, 是在函数体前声明的}.
433 | \paragraph{问题}
434 | 一个函数能看到的符号除了全局符号和自己栈帧中的符号, 还可能有父函数的符号.
435 | 因此需要查找父函数的栈帧.
436 | \paragraph{函数继承树}
437 | 将所有函数按照父子函数关系建立一颗树,
438 | 其中\verb/main/为树根, 深度为$0$.
439 | 注意此处的\verb/main/不像C的\verb/main/是一个全局函数,
440 | 而相当于整个程序, 可以参考Pascal的语法\begin{verbatim}program ...
441 | ...
442 | begin
443 | (* main *)
444 | end;\end{verbatim} 所以全局变量是\verb/main/的变量, 全局函数是\verb/main/的子函数.\par
445 | 容易证明, 在程序运行的任何时刻, 深度为$k$的所有函数至多有一个是活动的,
446 | 即深度为$k$的函数至多有一个, 其栈帧在当前运行栈中.
447 | 并且如果深度为$k$的某函数是活动的, 则一定有深度是$k-1$的某函数也是活动的, 且后者是前者的父函数.
448 | \paragraph{Display表方法}
449 | 维护一个表$D_n$.
450 | $D_n$表示从\verb/main/到当前函数在函数继承树上构成的链中,
451 | 当前函数深度为$n$的祖先函数的栈帧位置.\par
452 | 如对于以下程序 \begin{verbatim}
453 | program main;
454 |
455 | function A(params): ret
456 | function B(params): ret
457 | function C(params): ret
458 | begin
459 | ... C(args); ...
460 | end; (* end function C *)
461 | begin
462 | ... C(args); ...
463 | end; (* end function B *)
464 | begin
465 | ... B(args); ...
466 | end; (* end function A *)
467 |
468 | function A1(params): ret
469 | begin
470 | ... A(args); ...
471 | end; (* end function A1 *)
472 |
473 | begin
474 | ... A1(args); ...
475 | end.
476 | \end{verbatim}
477 | 第二次运行\verb/C/时, 栈应当类似于\\
478 | \begin{center}\begin{tabularx}{0.6\textwidth}{r|C|c|}
479 | \cline{2-3}
480 | & whose stackframe & depth\\
481 | \cline{2-3}
482 | $D_3 \to$ & \verb/C/ & 3
483 | \\\cline{2-3}
484 | & \verb/C/ & 3
485 | \\\cline{2-3}
486 | $D_2 \to$ & \verb/B/ & 2
487 | \\\cline{2-3}
488 | $D_1 \to$ & \verb/A/ & 1
489 | \\\cline{2-3}
490 | & \verb/A1/ & 1
491 | \\\cline{2-3}
492 | $D_0 \to$ & \verb/main/ & 0
493 | \\\cline{2-3}
494 | \end{tabularx}
495 | \end{center}
496 | \paragraph{维护Display}
497 | 考虑到每次对Display表的修改至多一项,
498 | 所以可以在栈帧中直接保存被替换的Display表项.
499 | \paragraph{静态动态表方法}
500 | 除了最初的\verb/main/外每个函数的栈帧都额外保存两个指针, 叫静态链和动态链.
501 | 某函数的动态链指向本次运行中调用该函数的函数栈帧;
502 | 静态链指向本次运行栈中其父函数, 并且是在该函数栈帧之前的第一个父函数.
503 | \subsection{面向对象系统的实现}
504 | \subsubsection{对象的存储组织}
505 | \paragraph{朴素方法}
506 | 当前对象的所有特性,
507 | 包括继承的特性 (域和方法),
508 | 都复制到对象存储区内.
509 | \paragraph{类存储}
510 | 将每个类的结构的描述保存在类的存储空间, 使用父类指针完成继承.
511 | 之后每个对象除了自己的成员数据外, 只需要访问自己类的结构描述得到方法.
512 | \paragraph{虚函数表}
513 | 将每个类的结构的描述保存在类的存储空间, 但是将父类方法也直接保存到自己类的描述.
514 | 之后每个对象除了自己的成员数据外, 只需要访问自己类的结构描述得到方法.
515 |
516 | \section{目标代码生成}
517 | \subsection{数据流分析}
518 | \subsubsection{概念}
519 | 假设程序是 TAC 语句的序列.
520 | \begin{description}
521 | \item[基本块] 如果将程序按照语句访问组织成图,
522 | 顺序语句和无条件跳转有且只有一条出边,
523 | 有条件跳转有两条出边.
524 | 定义基本块是其中一条极大链, 链内部非首尾节点入度出度都为$1$.
525 | \item[流图] 原程序的图进行基本块缩链后, 得到的以基本块为结点的图.
526 | 程序首条语句所在的流图结点成为流图首结点.
527 | 一般都去掉首结点不可达的语句.
528 | \item[支配节点集] 流图中, 称$u$是$v$的支配节点,
529 | 当且仅当任何从流图首结点到$v$的路径都需要经过$u$, 记为$u \mathrm{DOM} v$
530 | \item[回边] 若$u \mathrm{DOM} v$且存在边$v\to u$, 则称$v\to u$是一条回边.
531 | \item[循环] $v\to u$是一条回边,
532 | 则定义其对应的自然循环为
533 | $ \{v, u\} \cup \{ t \;|\; \exists P\;:\; t\to v, u \not\in P\} $
534 | \end{description}
535 |
536 | \subsubsection{正向数据流分析}
537 | \paragraph{定值和引用}
538 | 对于语句$I_n \;:\; \mathrm{lhs} = \mathrm{rhs}$,
539 | 称$\mathrm{lhs}$中变量都被\emph{定值},
540 | $\mathrm{rhs}$中所有变量都被\emph{引用},
541 | 分别记为$I_n \Def \mathrm{lhs}$,
542 | $I_n \Ref \mathrm{rhs}$.
543 | $I_n$称为$\mathrm{lhs}$的一个定值点.\par
544 | 同一条语句中, 引用发生在定值之前.\par
545 | 如果$I_n \Def A \;\land\; I_m \Ref A$,
546 | 并且存在路径$P\,:\,I_n\to I_m$,
547 | 满足$\forall I\in P, I \neq I_n\;:\; \neg(I \Def A)$,
548 | 则称$A$的定值点$I_n$可达引用点$I_m$.
549 | \paragraph{基本集合}
550 | \begin{itemize}
551 | \item $GEN[B]$, 是$B$中定值点的集合, 其可达$B$的出口.
552 | \item $KILL[B]$, 是$B$外定值点的集合, 其可达$B$的入口, 但不能达到$B$的出口.
553 | \item $IN[B]$, 是可达$B$入口的定值点的集合.
554 | \item $OUT[B]$, 是可达$B$出口的定值点的集合.
555 | \end{itemize}
556 | 其中, $GEN$和$KILL$可以直接通过流图得到.
557 | 计算时可以取$KILL$为所有可能被$B$中定值点覆盖的外部定值点.
558 | \paragraph{到达-定值分析}
559 | 数据流方程
560 | \begin{align*}
561 | IN[B] &= \bigcup_{C \in \mathrm{Prev}[B]} OUT[C]\\
562 | OUT[B] &= IN[B]\cup GEN[B] - KILL[B]
563 | \end{align*}
564 | 计算集合约束问题, 即得诸集合的值.
565 |
566 | \subsubsection{后向数据流分析}
567 | \paragraph{活跃变量}
568 | 对于语句$I_n$和变量$A$, 如果存在路径$P\;:\;I_n\to I_m$,
569 | 满足$I_m \Ref A$, 则称$A$在$I_n$处是活跃的.
570 | \paragraph{基本集合}
571 | \begin{itemize}
572 | \item $Def[B]$, 是变量的集合, 其在$B$中被引用前被定值
573 | \item $LiveUse[B]$, 是变量的集合, 其在$B$中被定值前被引用
574 | \item $LiveIn[B]$, 是变量的集合, 其在$B$的入口寸前是活跃的
575 | \item $LiveOut[B]$, 是变量的集合, 其在$B$的出口寸后是活跃的
576 | \end{itemize}
577 | 一定有$Def \cap LiveUse = \emptyset$
578 | \paragraph{活跃变量分析}
579 | 数据流方程
580 | \begin{align*}
581 | LiveOut[B] &= \bigcup_{C \in \mathrm{Succ}[B]} LiveIn[B]\\
582 | LiveIn[B] &= LiveOut[B] \cup LiveUse[B] - Def[B]
583 | \end{align*}
584 | 计算集合约束问题, 即得诸集合的值.
585 |
586 | \subsubsection{其他流信息}
587 | \paragraph{UD链} 考虑$I_n$点引用了$A$的值, 则定义$A$在$I_n$的引用-定值链为
588 | \[\{ I \;|\; I \Def A \land I \text{可达} I_n\}\]\par
589 | 显然, 如果$A \in Def[B]$, 那么其UD链就是$B$中定值$A$的那条语句.
590 | 否则, 应有$A \in LiveIn[B]$, 则其UD链就是$IN[B]$中所有$A$的定值点.
591 | \paragraph{DU链} 考虑$I_n$点定义了$A$的值, 则定义$A$在$I_n$的定值-引用链为
592 | \[\{ I \;|\; I \Ref A \land I_n \text{可达} I\}\]\par
593 | \paragraph{待用信息和活跃信息}
594 | 待用信息追踪变量在一个基本块中下一次引用的位置. 考虑加上待用信息注解,
595 | 则需要对于基本块中每个语句其中每个变量, 找到该基本块中其下一次被引用的位置,
596 | 然后标记在右上角标. 位置用语句的编号表示, 编号从1开始, 如果没有下一次引用则置为0.\par
597 |
598 | 活跃信息追踪变量的一个值的生命周期. 考虑加上活跃信息注解,
599 | 即对基本块中每个语句中每个变量加一个右上角标, 为L或者F.
600 | 如果是L, 表示此变量在这条语句之后, 还会在被定值前被引用;
601 | 为F则表示在这条语句之后, 此变量的这个值已经不会被引用了.
602 | 初始条件可以从基本块的$LiveOut$信息得到.
603 |
604 | \subsection{基本块的DAG表示}
605 | \paragraph{约定} 基本块中, 只考虑三种语句, 如图
606 | \begin{figure*}[ht]
607 | \centering
608 | \includegraphics[width=0.9\textwidth]{bb-dag.png}
609 | \caption{三种语句, 以及其DAG子图表示}
610 | \label{bb-dag}
611 | \end{figure*}
612 | 之后, 对于每一个语句. 如果其$\mathrm{rhs}$中,
613 | 有变量没有对应的结点, 则为其新建一个结点.\par
614 | 之后构建过程中, 完成的优化有合并常量,
615 | 删除冗余运算, 合并已知量, 删除无用赋值.
616 | 例如下图.
617 | \begin{figure*}[ht]
618 | \centering
619 | \includegraphics[width=0.9\textwidth]{bb-dag-ex.png}
620 | \caption{右边的基本块生成左边的DAG}
621 | \label{bb-dag-ex}
622 | \end{figure*}
623 |
624 | \subsection{代码生成}
625 | \subsubsection{指令选择}
626 | \paragraph{要求} 首先要求正确性, 即意思不改变.
627 | 其次是代价, 包括减少语句条数, 减少内存访问等等.
628 | 具体实现通过AVALUE和RVALUE来完成, 即及时维护一个寄存器-变量的一对多关系.
629 | \subsubsection{Ershov数} 一个表达式的Ershov数定义为其求值所需最少多少寄存器.\par
630 | 可以递归地计算, 考虑表达式树(DAG亦可)$T$, 左右儿子是$L$, $R$. 有\[
631 | \text{Ershov}(T) = \begin{cases}
632 | 1 & L = R = \text{NULL}\\
633 | \text{Ershov}(R) & L = \text{NULL}\\
634 | \text{Ershov}(L) & R = \text{NULL}\\
635 | \text{Ershov}(L) + 1 & \text{Ershov}(L) = \text{Ershov}(R)\\
636 | max\{\text{Ershov}(L), \text{Ershov}(R)\} & \text{Ershov}(L) \neq \text{Ershov}(R)\\
637 | \end{cases}\]
638 | 由Ershov数的定义容易得到求值算法 (Sethi-Ullman算法)
639 | \subsubsection{寄存器分配}
640 | 即将伪寄存器映射到物理寄存器的方法, 不考虑到泄露到内存的情况.
641 | 采用寄存器相干图. 原理是, 如果在$A$的定值点寸后$B$是活跃的,
642 | 则$A$, $B$应当分配到不同的物理存储器, 否则对$A$的定值会修改$B$的值.
643 | 于是将伪寄存器作为结点建图, 求图色数就是程序最少使用的物理存储器.
644 |
645 | %\paragraph{程序内存布局} 程序的虚拟内存空间的典型安排如\\
646 | % \begin{center}
647 | % \begin{tabularx}{0.5\textwidth}{|C|}
648 | % \hline
649 | % 0xFFFF\ldots\\
650 | % \hline
651 | % 保留\\
652 | % \hline
653 | % 栈\\
654 | % $\downarrow$\\
655 | % \hline
656 | % $\uparrow$\\
657 | % 堆\\
658 | % \hline
659 | % 运行时库\\
660 | % \hline
661 | % 静态数据\\
662 | % \hline
663 | % 代码段\\
664 | % \hline
665 | % 保留\\
666 | % \hline
667 | % 0x0000\ldots\\
668 | % \hline
669 | % \end{tabularx}
670 | % \end{center}
671 |
672 | %If the attributes are all synthesized, and the actions occur at the ends of the
673 | %productions, then we can compute the attributes for the head when we reduce
674 | %the body to the head
675 | \end{document}
676 |
--------------------------------------------------------------------------------
/network_security/netsecurity.tex:
--------------------------------------------------------------------------------
1 | % 编译使用xelatex
2 | \documentclass{ctexart}
3 |
4 | \usepackage{amsmath}
5 | \usepackage{amsfonts}
6 | \usepackage{amssymb}
7 | \usepackage{graphicx}
8 | \usepackage{subfigure}
9 | \usepackage{tabularx}
10 |
11 | \DeclareMathOperator{\Zset}{\mathbb{Z}}
12 |
13 | % for simulating booktabs rules; so they work with vertical lines
14 | \newlength{\Oldarrayrulewidth}
15 | \newcommand{\Cline}[2]{
16 | \noalign{\global\setlength{\Oldarrayrulewidth}{\arrayrulewidth}}
17 | \noalign{\global\setlength{\arrayrulewidth}{#1}}\cline{#2}
18 | \noalign{\global\setlength{\arrayrulewidth}{\Oldarrayrulewidth}}}
19 | \newcommand{\Hline}[1]{
20 | \noalign{\global\setlength{\Oldarrayrulewidth}{\arrayrulewidth}}
21 | \noalign{\global\setlength{\arrayrulewidth}{#1}}\hline
22 | \noalign{\global\setlength{\arrayrulewidth}{\Oldarrayrulewidth}}}
23 | \newcommand{\Topline}{\Hline{0.08em}}
24 | \newcommand{\Bottomline}{\Hline{0.08em}}
25 | \newcommand{\Midline}{\Hline{0.05em}}
26 | \newcommand{\CMidLine}[1]{\Cline{0.05em}{#1}}
27 |
28 | % for formatting a row
29 | \newcolumntype{+}{>{\global\let\currentrowstyle\relax}}
30 | \newcolumntype{^}{>{\currentrowstyle}}
31 | \newcommand{\rowstyle}[1]{\gdef\currentrowstyle{#1}#1\ignorespaces}
32 |
33 | % define new stretchable column types
34 | \newcolumntype{L}{>{\raggedright\arraybackslash}X}
35 | \newcolumntype{R}{>{\raggedleft\arraybackslash}X}
36 | \newcolumntype{C}{>{\centering\arraybackslash}X}
37 |
38 | \addtolength{\oddsidemargin}{-.875in}
39 | \addtolength{\evensidemargin}{-.875in}
40 | \addtolength{\textwidth}{2.00in}
41 | \addtolength{\topmargin}{-.875in}
42 | \addtolength{\textheight}{1.75in}
43 |
44 | \title{计算机网络安全技术}
45 |
46 | \begin{document}
47 | \maketitle
48 |
49 | \tableofcontents
50 |
51 | \section{基本密码学}
52 | \subsection{基本概念}
53 | \paragraph{传统加密和私钥加密} 传统加密包含代换, 置换密码及其组合, 重点依赖于算法的保密.
54 | 私钥加密亦称非对称加密, 算法和公约公开, 私钥保密.
55 | \paragraph{块加密和流加密} 输入是字符, 每次是处理一个字符还是处理一组字符.
56 | \paragraph{无条件安全和计算安全} 无条件安全即, 拥有无论多少密文对破译都没有帮助.
57 | 计算安全即, 破译代价大于加密数据本身价值, 或者破译时间长于加密数据有效时间.
58 | \paragraph{记号}
59 | \begin{description}
60 | \item[加密函数] $c = E_{K_E}(m),\quad m \in \mathcal{M}, c \in \mathcal{C}, K_E \in \mathcal{K}_E$
61 | \item[解密函数] $m = D_{K_D}(c),\quad c \in \mathcal{C}, m \in \mathcal{M}, K_D \in \mathcal{K}_D$
62 | \end{description}
63 |
64 | \subsection{代换密码}
65 | 如Caesar密码等单表代换密码, Playfair密码和Vigenere密码等多表代换密码.
66 | \paragraph{攻击} 拥有足够密文即可通过统计学分析破译.
67 |
68 | \subsection{置换密码}
69 | $D(m) = \sigma m$, 要求$\mathcal{M} = \Sigma^L$, $\sigma$是$[0, L)$上的置换.
70 | 代换密码变换的是每个位置上的字母, 而置换密码变换的是消息中各个字母的位置.
71 | \paragraph{攻击} 频率分析, 包括多元组频率分析.
72 |
73 | \subsection{对称密码}
74 | \subsubsection{Feistel密码结构}
75 | \paragraph{Feistel的密码观点}
76 | \begin{enumerate}
77 | \item 使用乘积密码
78 | \item 交替使用代换和置换
79 | \end{enumerate}
80 | \paragraph{Shannon的密码观点}
81 | \begin{description}
82 | \item[扩散] 密文不包含明文的统计信息, 每个明文字符影响多个密文字符
83 | \item[混淆] 复杂的密文和密钥间的统计关系, 防止从密文推出密钥
84 | \end{description}
85 | \paragraph{Feistel网络}
86 | 大致如图所示.
87 | \begin{figure}[ht!]
88 | \centering
89 | \includegraphics[width=0.6\textwidth]{feistel-net.png}
90 | \caption{经典的Feistel网络结构}
91 | \label{lenet-structure}
92 | \end{figure}
93 | \paragraph{Feistel网络的元素}
94 | \begin{description}
95 | \item[分组长度和密钥长度] 越长越安全, 但是效率越低
96 | \item[迭代层数] 越多越安全
97 | \item[$K_i$的产生算法] 越复杂越安全
98 | \item[$F$函数] 越复杂越安全
99 | \end{description}
100 |
101 | \subsubsection{常见对称密码}
102 | 对称密码的特点是只有一个密钥, 加密过程和解密过程是基本相同的.\par
103 | 通信双方如果需要交换密钥, 则需要在安全的信道上交换密钥.\par
104 | 对称密码的速度通常较非对称密码快, 因此常用于大量数据的加密上.\par
105 | \paragraph{DES算法}
106 | DES是对称密钥算法, 基于Feistel网络结构.
107 | 密钥长度为56位, 块长度是64位, 迭代16轮.\par
108 | 和Feistel的区别是, 加密初始和末尾有一个置换.\par
109 | 已被破解.
110 | \paragraph{3-DES算法}
111 | 密钥长度加倍到112位, 并且加密函数变成了 \[
112 | \mathbf{3-DES}(M) = \mathbf{DES}(\mathbf{DES}(\mathbf{DES}(M))) \]
113 | 很安全, 但是效率很低.
114 | \paragraph{Blowfish算法}
115 | 基于Feistel网络结构, 但是每轮中左右两半都进行计算.\par
116 | 未被破解.
117 | \paragraph{RC5算法}
118 | 仍然是多轮加密, 但是每轮结构更加复杂.
119 | \paragraph{AES算法}
120 | AES不是Feistel结构, 每一轮处理整个输入 (而非分成2部分).
121 |
122 | \subsubsection{S-DES算法}
123 | 此处的 S-DES 意为 Simplified DES, 主要做教学展示用.
124 | S-DES基于 Feistel 结构, 输入输出是8位的, 密钥是10位的, 位指二进制位.\par
125 | \paragraph{生成密钥}
126 | 参考图\ref{s-des-keygen}. 其中置换如
127 | \begin{center}
128 | \begin{tabularx}{0.8\textwidth}{cCCCCCCCCCC}
129 | \Topline
130 | $n$ & $0$ & $1$ & $2$ & $3$ & $4$ & $5$ & $6$ & $7$ & $8$ & $9$ \\
131 | \Midline
132 | $P10(n)$ & $2$& $4$& $1$& $6$& $3$& $9$& $0$& $8$& $7$& $5$\\
133 | $shift_1(n)$ & $1$ & $2$ & $3$ & $4$ & $0$ & $6$ & $7$ & $8$ & $9$ & $5$\\
134 | $shift_2(n)$ & $2$ & $3$ & $4$ & $0$ & $1$ & $7$ & $8$ & $9$ & $5$ & $6$\\
135 | \Bottomline
136 | \end{tabularx}
137 | \end{center}
138 | 另外函数$P8(0,\ldots 9) = \left\langle 5, 2, 6, 3, 7, 4, 9, 8 \right\rangle$
139 |
140 | \begin{figure}[ht!]
141 | \centering
142 | \includegraphics[keepaspectratio,height=0.25\textheight,width=1.0\textwidth]{s-des-keygen.jpg}
143 | \caption{S-DES 生成密钥}
144 | \label{s-des-keygen}
145 | \end{figure}
146 |
147 | \paragraph{加密}
148 | 参考图\ref{s-des-enc} 和 \ref{s-des-fk}, 其中有\par
149 | \[IP(0,\ldots 7) = \left\langle 1, 5, 2, 0, 3, 7, 4, 6 \right\rangle\]
150 | \[IP^{-1}(0,\ldots7) = \left\langle 3, 0, 2, 4, 6, 1, 7, 5\right\rangle\]
151 | \[E/P(0, 1, 2, 3) = \left\langle 3, 0, 1, 2, 1, 2, 3, 0 \right\rangle\]
152 | 另外$S$的求法是, $S(n_0, n_1, n_2, n_3)$中, $n_0*2 + n_3$作为行, $n_1*2+n_2$作为列, 寻找矩阵中对应元素\[
153 | S_0 = \begin{bmatrix}
154 | 01 & 00 & 11 & 10\\
155 | 11 & 10 & 01 & 00\\
156 | 00 & 10 & 01 & 11\\
157 | 11 & 01 & 11 & 10\end{bmatrix}\qquad
158 | S_1 = \begin{bmatrix}
159 | 00 & 01 & 10 & 11\\
160 | 10 & 00 & 01 & 11\\
161 | 11 & 00 & 01 & 00\\
162 | 10 & 01 & 00 & 11\end{bmatrix}
163 | \]
164 | \begin{figure}[ht!]
165 | \centering
166 | \includegraphics[height=0.5\textheight,width=0.8\textwidth,keepaspectratio]{s-des-enc.jpg}
167 | \caption{S-DES 加密}
168 | \label{s-des-enc}
169 | \end{figure}
170 |
171 | \begin{figure}[ht!]
172 | \centering
173 | \includegraphics[height=0.5\textheight,width=0.5\textwidth,keepaspectratio]{s-des-fk.jpg}
174 | \caption{S-DES 的 $F$函数}
175 | \label{s-des-fk}
176 | \end{figure}
177 |
178 | \subsection{公钥密码}
179 | 公钥密码即非对称密码.
180 | 其中加密和解密使用的是不同的密钥, 称为公钥和私钥.
181 | \emph{公钥公开, 用于加密和验证签名; 私钥保密, 用于解密和签名.}\par
182 | 非对称密码的速度通常较对称密码要慢,
183 | 所以常常只用于签名\footnote{验证发送者身份的过程},
184 | 或者用于加密发送对称密码的密钥.
185 | \paragraph{加密方法} Alice 向 Bob 发送希望加密的信息, 则 Alice 使用 Bob 的公钥加密信息后发送给 Bob.
186 | \paragraph{签名算法} Bob 希望验证 Alice 的身份,
187 | 其给出一个特定的消息, 要求 Alice 用 Alice 自己的私钥加密后发送给 Bob,
188 | 之后 Bob 再用 Alice 的公钥解密验证.
189 | \subsubsection{RSA算法}
190 | 原文和密文都是$[0, N)$中的整数, 常常$N$是2的幂.\par
191 | \paragraph{前置}
192 | \begin{enumerate}
193 | \item 计算大素数 $p$, $q$.
194 | \item 计算$N = pq$, 公开.
195 | \item 寻找一个小整数$e,\;e < N,\,(e, N) = 1$
196 | \item 求解$d e \equiv 1 \pmod{\varphi(N) = (p-1)(q-1)}$, 得到$d$
197 | \item 公钥为$\langle e, N \rangle$, 私钥为$\langle d, N \rangle$
198 | \end{enumerate}
199 | \paragraph{加密}
200 | 对于$m \in [0, N)$, 加密函数如下
201 | \[ E(t) = m^e \bmod N \]
202 | \paragraph{解密}
203 | 对于$c \in [0, N)$, 加密函数如下
204 | \[ D(c) = c^d \bmod N \]
205 | \paragraph{原理}
206 | 有效性由Euler定理保证 \[ a^{\varphi(n)} \equiv 1 \pmod{n},\quad \forall a\,:\,(a, n) = 1 \]
207 | 安全性由大数分解困难性保证.
208 | \paragraph{攻击}
209 | \begin{description}
210 | \item[暴力破解] 需要枚举$t \in [0, N)$的空间
211 | \item[数学攻击] 因子分解$N$
212 | \item[计时攻击] 按照加密时间推测私钥.
213 | \end{description}
214 | \subsubsection{Diffie-Hellman算法}
215 | 用于密钥交换, 而非加密解密, 即通过双方自身的私有密文得到一个共有密文.
216 | \paragraph{前置}
217 | \begin{enumerate}
218 | \item 寻找素数$p$, 以及$\Zset_p$的生成元$a$ i.e. $a$是原根, 均公开.
219 | \end{enumerate}
220 | \paragraph{私有密文产生共有密文}
221 | \begin{enumerate}
222 | \item 双方由自己的私有密文$X_A,\, X_B$计算公开的$Y_A = a^{X_A} \bmod p,\, Y_B = a^{X_B} \bmod p$
223 | \item 双方由公开的$Y_A, Y_B$计算得到共有密文$K = Y_B^{X_A} \bmod p = Y_A^{X_B} \bmod p = a^{X_A X_B} \bmod p$
224 | \end{enumerate}
225 | \paragraph{原理}
226 | 安全性由离散对数困难性保证.
227 | \subsubsection{和对称密码的比较}
228 | \begin{itemize}
229 | \item 笼统地说, 非对称密码并不比对称密码安全.
230 | \item 非对称密码不是通用的, 对称密码仍然由于快速而广泛使用
231 | \item 公钥密码的密码分配不必传统密码分配简单
232 | \end{itemize}
233 |
234 | \subsection{密钥分配}
235 | \subsubsection{传统对称密钥分配}
236 | \paragraph{可能的方法}
237 | \begin{enumerate}
238 | \item Alice选择密钥, 亲自交给Bob
239 | \item 第三方Charlie选择密钥, 亲自交给Alice和Bob
240 | \item Alice选择密钥, 用最近使用的密钥加密后发给Bob
241 | \item 第三方Charlie选择密钥, 通过某个秘密渠道交给Alice和Bob
242 | \end{enumerate}
243 | \paragraph{密钥分发中心}
244 | 基本假设: 每个人有一个仅他自己和KDC知道的主密钥. 此假设下, Alice和Bob得到一次性次密钥的方法为:
245 | \begin{enumerate}
246 | \item Alice向KDC请求次密钥
247 | \item KDC发送给Alice消息, 消息用$PK_a$加密, 包含用次密钥$K_s$, 以及$PK_b$加密的$K_s$和$ID_a$
248 | \item Alice将$PK_b$加密的消息发送给Bob
249 | \end{enumerate}
250 | 为了效率和安全性, KDC通常也是层次性的, 而非一个巨大的中心KDC.
251 | \subsubsection{非对称公钥发布}
252 | \paragraph{公开发布} Alice向所有人广播自己的公钥. 问题是容易伪造广播消息.
253 | \paragraph{公开目录} 只有一个受信任的实体 (管理员) 能够广播公钥. 问题是如果受信任的实体被攻击, 公钥可以任意更改.
254 | \paragraph{公钥授权} 双方通讯之前先向管理员请求对方公钥, 管理员返回消息时用管理员私钥加密.
255 | \subsubsection{通过非对称公钥分配传统密钥}
256 | 非对称加密的问题是效率太低, 一般通过非对称加密传输传统密钥, 之后消息用传统加密方法求解.
257 | \paragraph{Merkel朴素方法} Alice给Bob请求共有密钥. Bob用Alice公钥加密某密文后传输给Alice, Alice之后使用自己的私钥解密得到共有密文.
258 | 问题: 中间人攻击, 考虑若Bob是假Bob.
259 | \paragraph{保密真实方法} \begin{enumerate}
260 | \item Alice发送给Bob: $E_{K_{B, pub}}(ID_A, N_A)$, $N_A$是一个随机校验数
261 | \item Bob得到$ID_A, N_A$, 发送给Alice: $E_{K_{A, pub}}(N_A, N_B)$
262 | \item Alice得到$N_A', N_B$, 检查$N_A = N_A'$, 发送给B$E_{K_{B, pub}}(N_B)$
263 | \item Bob得到$N_B'$, 检查$N_B = n_B'$
264 | \end{enumerate}
265 |
266 |
267 | \section{计算机网络安全体系结构}
268 | \subsection{安全目标}
269 | \begin{description}
270 | \item[保密性 Confidentiality] 未授权的实体不能获得信息内容
271 | \item[完整性 Integrity] 信息不能被篡改, 或者能检测篡改
272 | \item[可用性 Availability] 授权用户能够访问资源, 防止DoS攻击
273 | \end{description}
274 | \subsection{手段}
275 | \begin{description}
276 | \item[加密] 防止未授权的外人获得通信内容, 如防止窃听.
277 | \item[认证] 验证通信对等体的身份真实性, 如防止中间人攻击. 可以通俗的理解为, ``发送这条消息的人的确是 Alice''.
278 | \item[数字签名] 相当于对通信对等体和消息同时认证, 如防止抵赖. 可以通俗的理解为, ``Alice 的确发送过这条消息''.
279 | \end{description}
280 |
281 | \section{消息认证}
282 | \subsection{攻击类型}
283 | \begin{description}
284 | \item[泄密] 保密性范畴.
285 | \item[传输分析] 保密性范畴.
286 | \item[伪装] 消息认证.
287 | \item[内容修改] 消息认证.
288 | \item[抵赖] 数字签名, 以及协议设计.
289 | \end{description}
290 | \subsection{消息认证基本概念}
291 | \subsubsection{消息认证} 确认发送方是真实的, 确认消息违背篡改.
292 | \begin{quote}{\itshape 消息认证就是验证所收到的消息确实是来自真正的发送方且未被修改的消息}\end{quote}
293 | \subsubsection{基本框架} 发送方产生一个认证符; 接收方产生一个认证符, 并且检查双方认证符是否匹配.
294 |
295 | \subsection{产生认证符}
296 | \subsubsection{消息加密作为认证符}
297 | \paragraph{对称加密} 为了认证, 要么消息空间是稀疏的, 要么消息中带有校验符号 FCS.
298 | FCS 的计算是在加密之前, 将发送的内容计算出一个校验符, 附到发送内容后一并加密发送.\par
299 | 可以提供加密和认证, 但是无法提供数字签名.
300 | \paragraph{非对称加密} 使用私钥加密可以完成签名和认证, 使用公钥加密可以提供保密性,
301 | 两者都使用可以同时提供加密和认证以及签名.\par
302 | 但是这样需要 4 次加解密运算, 代价较高.
303 | \subsubsection{消息认证码MAC}
304 | 通过消息和密钥 (两者都需要, 以验证\emph{这条消息}是发自这个\emph{发送者}),
305 | 利用公开的算法计算消息认证码 $MAC = C_K(M)$,
306 | 其中$K$是共有密钥, $M$是消息, $C_K$生成一个固定长度的短数据块.\par
307 | 提供认证, 但是不提供数字签名: 接收方也有$K$, 因此可以伪造消息.\par
308 | 可以先加密在使用MAC, 提供加密和认证.
309 | \subsubsection{消息哈希}
310 | 通过消息, 计算认证符 $MD = \mathbf{Hash}(M)$, $M$是消息, $\mathbf{Hash}$输出固定长度的短数据. 亦称消息摘要.\par
311 | 哈希本身不包含对于发送者的验证, 因此一定要对哈希加密. 根据需要的服务, 具体有\begin{itemize}
312 | \item 对称加密中, 可以加密消息和哈希码, 可以完成加密和认证.
313 | \item 对称加密中, 可以只加密哈希码, 可以完成认证.
314 | \item 非对称加密中, 用发送方的私钥加密哈希码, 可以完成认证和数字签名.
315 | \item 非对称加密中, 用发送方的私钥加密消息和哈希码, 可以完成加密, 认证和数字签名.
316 | \end{itemize}
317 |
318 | \subsection{哈希函数}
319 | \subsubsection{哈希函数的理论要求}
320 | 要求哈希函数$h = H(M)$有如下性质 \begin{description}
321 | \item[单向性] 对于$h$, 难以寻找$M$, 满足$H(M) = h$
322 | \item[弱抗碰撞] 对于$M$, 难以寻找$M'$, 满足$H(M) = H(M')$
323 | \item[强抗碰撞] $\forall M$, 难以寻找$M'$, 满足$H(M) = H(M')$
324 | \end{description}
325 | \subsubsection{安全哈希函数的Merkel结构}
326 | 参见图 \ref{merkel-hash}.
327 | \begin{figure}[ht!]
328 | \centering
329 | \includegraphics[width=0.8\textwidth]{merkel-hash}
330 | \caption{Merkel哈希结构}
331 | \label{merkel-hash}
332 | \end{figure}
333 | \subsubsection{常用哈希举例} 有MD5, SHA-1, SHA-2, GOST等等.
334 | \paragraph{MD5} 有如下特点 \begin{itemize}
335 | \item 消息可以无限长
336 | \item 采用小端结构, 将消息表示为 32 位字的序列
337 | \item 填充部分包含了消息的长度
338 | \item 填充后消息位数是 512 的倍数, 每个分组$Y_i$大小为 512 位 (64字节, 16个字)
339 | \item 中间结果$CV_i$和最后结果$MD$的长度是 128 位 (16字节, 4个字寄存器)
340 | \item 由四轮运算组成, 每轮16步迭代
341 | \end{itemize}
342 |
343 |
344 | \subsection{数字签名DSS算法}
345 | \paragraph{应用场景} 来自消息发送双方的攻击, 如接收方伪造发送, 发送方抵赖发送.
346 | \paragraph{分类} 直接数字签名 (仅通信双方), 仲裁数字签名 (受信任的实体).
347 | \paragraph{前置} \begin{description}
348 | \item[全局公钥] $p$是素数\\ $q$是素数且$q \mid p -1$\\
349 | $g = h^{(p-1) / q} \bmod p$
350 | \item[用户私钥] $x$, 满足$x < q-1$
351 | \item[用户公钥] $y = g^x \bmod p$
352 | \item[随机数] $k < q$, 每次认证都应产生一个新的$k$, 且$k$保密
353 | \end{description}
354 | \paragraph{DSS算法签名} 签名用一组数$(r, s)$代表, 计算过程如下方程, 哈希函数$\mathbf{H}$一般取SHA-1
355 | \begin{align*}
356 | r &= g^k \bmod p \bmod q\\
357 | s &= (k^{-1} (\mathbf{H}(M) + xr)) \bmod q
358 | \end{align*}验证要求 \begin{equation*}
359 | g^{(s^{-1} M) \bmod q} y^{(s^{-1} r) \bmod q} = r
360 | \end{equation*}
361 |
362 | \section{访问控制}
363 | \subsection{基本概念}
364 | \paragraph{主体} 提出访问资源请求的人/实体.
365 | \paragraph{客体} 含有被访问资源的实体.
366 | \paragraph{访问} 对资源的使用行为, 有时还需包括主客体.
367 | \paragraph{访问控制矩阵} 定义不同主体对于不同客体可以执行的操作, 如
368 | \begin{center}
369 | \begin{tabularx}{0.6\textwidth}{|C|C|C|C|}
370 | \hline
371 | & $O_2$ & $O_3$ & $O_4$\\
372 | \hline
373 | $S_1$ & R/W & R & \\
374 | \hline
375 | $S_2$ & W & & \\
376 | \hline
377 | $S_3$ & R/W & R/W & R/W\\
378 | \hline
379 | \end{tabularx}
380 | \end{center}
381 | 按列看, 就是客体的访问控制列表; 按行看, 就是主体的访问能力列表.
382 |
383 | \subsection{访问控制模型}
384 | \subsubsection{自主性访问控制}
385 | 每个客体有一个管理者, 管理者将访问权限授权给其他人. 可以看成以主体为核心.
386 | 如Unix文件系统.\par
387 | \paragraph{性质}
388 | 易用方便灵活. 但是不能控制信息的流动, i.e. 其他人取得资源后可以自由分发.
389 | \subsubsection{强制性访问控制}
390 | 每个主体和客体有固定的安全级别, 由系统管理员决定. 可以看成以客体为核心.
391 | \paragraph{No Read Up}
392 | 主体只能读取安全级别更低的客体.
393 | \paragraph{No Write Down}
394 | 主题只能写入安全级别更高的客体.
395 | \subsubsection{基于角色的访问控制}
396 | 主体(用户)属于用户组(角色). 每个角色(而非主体)对于不同的客体有不同访问权限.
397 | 可以看成以访问为核心.
398 |
399 | \subsection{防火墙}
400 | 在两个不同网络间建立访问控制的软硬件.
401 | \subsubsection{设计目标}
402 | 监控两个网络间所有通信流量, 只有授权的流量才允许通过.
403 | \subsubsection{常用技术} 基于控制, 有\begin{description}
404 | \item[服务控制] 基于服务端口号 / IP地址
405 | \item[方向控制] 内到外 / 外到内
406 | \item[用户控制] 基于用户的身份, 如 VPN
407 | \item[行为控制] 基于用户的行为进行控制, 如垃圾邮件过滤
408 | \end{description}\par
409 | 基于分层, 有\begin{description}
410 | \item[网络层] 包过滤技术.\\
411 | 根据定义好的过滤规则, 包含IP地址, 端口和协议等, 审查每个数据包. 性能较高, 但控制能力不强.\\
412 | 如路由器中的ACL.
413 | \item[网络层] 地址转换.\\
414 | 完成转发和地址转换. 不提供额外的安全性,但是可以隐蔽内部网络,节省地址空间
415 | \item[传输层] 电路层网关.\\
416 | 检查包所属的会话, 即是不是属于客户端和服务器的某一个链接, 不检查协议和内容.
417 | 不支持无连接的UDP, 性能开销较大.
418 | \item[应用层] 应用层代理.\\
419 | 功能强大, 但是性能较低, 并且实现麻烦.
420 | \end{description}
421 |
422 | \subsubsection{访问控制列表} 一组预先定义好的规则. 其根据包头, 指定包是否被拦截.
423 | \paragraph{标准和拓展} 标准ACL只检查 Frame 中源IP地址.
424 | 拓展ACL还支持端口, 目标IP, 协议, 检查 Frame 和 Packet.\par
425 | 路由过程中, 路由前后分别有入口/出口端ACL. 入口端ACL在查路由表前检查, 出口端ACL在转发前检查.
426 |
427 | \subsection{VLAN虚拟局域网}
428 | \paragraph{基本概念} VLAN类似一个独立的网桥, 但是可能一个VLAN跨越不同的交换机, 同一个交换机有不同的VLAN.
429 | \paragraph{类型} 如 \begin{description}
430 | \item[基于物理接口] 按照交换机的接口分配.\\ 配置简单, 但是不灵活.
431 | \item[基于MAC地址] 局域网中每个MAC地址有对应的VLAN分组.
432 | \item[基于协议] 根据网络层协议分划VLAN.
433 | \end{description}
434 | \paragraph{优点} \begin{itemize}
435 | \item 解决人员维护性
436 | \item 控制广播流量, 防止交换机后向学习的洪泛造成的``洪范灾难''
437 | \item 增强安全性
438 | \end{itemize}
439 | \paragraph{缺点} 缺乏标准
440 |
441 | % \section{网络安全协议}
442 | % 协议基本要素: 层次 (OSI 7层中哪一层), 格式, 行为 (亦称控制).
443 |
444 | \section{IP层级安全}
445 | \subsection{IPsec}
446 | 与 IP 在同一层. 功能 \begin{description}
447 | \item[认证] 确认包的发送者真实, 包不被篡改
448 | \item[加密] 流量管理
449 | \item[密钥管理] 密钥的安全交换
450 | \end{description}
451 | \subsubsection{SA}
452 | \paragraph{背景} 假设 Alice 希望给 Bob 发送数据.
453 | 无论加密 (对称加密) 还是认证 (HMAC\footnote{即MAC, 参考rfc 2104}), 他们都需要一个共有的密钥.
454 | IPsec讨论中假设这个双方已有这个共有密钥了.
455 | \paragraph{SPD} 安全策略数据库, 其过滤所有IP流量.
456 | 并且对于每个IP包, 其指定对于此IP包的处理,
457 | 是丢弃\verb/DISCARD/, 直接通过IP发送\verb/BYPASS/,
458 | 还是通过IPsec即\verb/PROTECT/.
459 | 如果指定是\verb/PROTECT/, 它还需指定是\verb/ESP/还是\verb/AH/,
460 | 使用传输模式还是隧道模式,
461 | 之后或者利用IKE生成一个SA记录到SAD中, 或者交由SAD处理.
462 | \paragraph{SA内容} Bob 可能有很多个入境连接都使用 IPsec,
463 | 因此每个连接都需要一个标识, 用来让 Bob 确定这个连接的参数,
464 | 如哈希算法, 公共密钥等. IPsec中此标识实现为 SPI, 是一个 32 位的整数.\par
465 | Bob本地有一个 SAD, 对于每一个有效的 SPI 在 SAD 中都有对应的SA (包含SPI, 使用ESP还是AH, 目标IP地址),
466 | 以及源地址, SA记录等等.
467 | \paragraph{传输模式和隧道模式}
468 | 对于加密或认证, 如果针对的是IP载荷 (即TCP头加载荷), 则成为传输模式, 用于端到端的传输. 通常就在主机上实现.
469 | 如果针对的整个IP包, 则成为隧道模式, 用于网络上构建VPN. 通常在防火墙或者路由器上实现.
470 | \paragraph{原理} IPsec有两种协议, 有AH, 用于认证; 以及ESP, 用于加密和可选的认证. 参见如图\par
471 | \begin{figure}[ht!]
472 | \centering
473 | \includegraphics[width=1.0\textwidth]{ah-transport.png}
474 | \caption{传输模式的AH}
475 | \label{ah-transport}
476 | \end{figure}
477 |
478 | \begin{figure}[ht!]
479 | \centering
480 | \includegraphics[width=1.0\textwidth]{ah-tunnel.png}
481 | \caption{隧道模式的AH}
482 | \label{ah-tunnel}
483 | \end{figure}
484 |
485 | \begin{figure}[ht!]
486 | \centering
487 | \includegraphics[width=1.0\textwidth]{esp-transport.png}
488 | \caption{传输模式的ESP}
489 | \label{esp-transport}
490 | \end{figure}
491 |
492 | \begin{figure}[ht!]
493 | \centering
494 | \includegraphics[width=1.0\textwidth]{esp-tunnel.png}
495 | \caption{隧道模式的ESP}
496 | \label{esp-tunnel}
497 | \end{figure}
498 | \clearpage
499 |
500 | \subsection{IKE}
501 | IKE位于应用层 (UDP端口500), 目的是在不安全的互联网上交换密钥,
502 | 使得通信双方能得到一个共有的公共密钥, 用于对称加密或签名.
503 | 此外IKE还需要和SPD, SAD协同工作, 后两者可能请求IKE协商SA.
504 | \par
505 | 可以采用传输模式端对端地工作, 也可以利用隧道模式在安全网关之间工作.\par
506 |
507 | \paragraph{朴素DH的问题} 朴素DH容易受到中间人攻击,
508 | 即欺骗者对 Alice 声称自己是 Bob, 对 Bob 声称自己是 Alice. 解决需要提供Alice和Bob的身份验证.
509 | \subsubsection{第一阶段}
510 | 此阶段协商IKE SA. 有主模式\verb/main mode/和快速模式\verb/aggressive mode/.\par
511 | 双方认证可以有多种方法, 此处以签名认证为例, 摘抄rfc如下\begin{verbatim}
512 | Phase 1 is where the two ISAKMP peers establish a secure,
513 | authenticated channel with which to communicate.
514 | 5.1 IKE Phase 1 Authenticated With Signatures
515 | Main Mode with signature authentication is described as follows.
516 | The first two messages negotiate policy.
517 | The next two exchange Diffie-Hellman public values
518 | and ancillary data (e.g. nonces) necessary for the exchange;
519 | The last two messages authenticate the Diffie-Hellman Exchange.
520 |
521 | Initiator Responder
522 | ----------- -----------
523 | HDR, SA -->
524 | <-- HDR, SA
525 | HDR, KE, Ni -->
526 | <-- HDR, KE, Nr
527 | HDR*, IDii, [ CERT, ] SIG_I -->
528 | <-- HDR*, IDir, [ CERT, ] SIG_R
529 |
530 | Aggressive mode with signatures in conjunction with ISAKMP is described as follows:
531 | The first two messages negotiate policy, exchange Diffie-Hellman public values
532 | and ancillary data necessary for the exchange, and identities.
533 | In addition the second message authenticates the responder.
534 | The third message authenticates the initiator
535 | and provides a proof of participation in the exchange.
536 |
537 | Initiator Responder
538 | ----------- -----------
539 | HDR, SA, KE, Ni, IDii -->
540 | <-- HDR, SA, KE, Nr, IDir,
541 | [ CERT, ] SIG_R
542 | HDR, [ CERT, ] SIG_I -->
543 |
544 | In both modes, the signed data, SIG_I or SIG_R, is the result of the
545 | negotiated digital signature algorithm applied to HASH_I or HASH_R
546 | respectively.
547 |
548 | c.f.
549 | HDR: ISAKMP header; HDR* denotes payloads encrypted.
550 | SA: an SA negotiation payload with one or more proposals.
551 | KE: public information exchanged in a Diffie-Hellman exchange.
552 | Nx: nonce payload;
553 | IDx: identification payload, e.g. IP address
554 | SIG_x: necessary info (g^xy, SAx, IDx, Nx etc) signed by x.
555 | \end{verbatim}
556 | \subsubsection{第二阶段}
557 | 协商如IPsec的加密算法, 哈希算法, DH组等等参数.
558 | 使用快速模式\verb/Quick mode/, 需要三次消息交换.
559 |
560 | \section{SSL}
561 | \subsection{特点}
562 | \begin{itemize}
563 | \item 在IP/TCP参考模型中, SSL / TLS 位于应用层和传输层之间.
564 | \item SSL工作在 TCP 之上, 不支持 UDP.
565 | \end{itemize}
566 | \subsection{概念}
567 | \begin{description}
568 | \item[会话] 交流双方的一个虚拟的连接关系, 指定了密码算法, 主密钥等等消息.
569 | \item[连接] 一个通信信道, 通常是一个 TCP 连接.\\
570 | 一个会话可以被多个连接先后使用.
571 | \end{description}
572 | \subsection{协议}
573 | \subsubsection{SSL记录协议} SSL记录协议定义了SSL传输信息的格式, 是其他SSL协议的基础.\par
574 | 高层数据, 通过SSL, 会经过一下步骤 \begin{enumerate}
575 | \item 分段, 将高层数据分成小块
576 | \item 压缩, 这一步是可选的
577 | \item 计算 MAC 附在数据之后
578 | \item 使用共享密钥对数据对称加密
579 | \item 在加密后的数据前添加 SSL 头部
580 | \end{enumerate}
581 | \subsubsection{握手协议} 握手协议有以下功能\begin{description}
582 | \item[身份认证] 认证服务器的身份, 并可选地认证客户的身份
583 | \item[协商密码算法] 包含对称密码算法, MAC 算法以及密钥交换算法
584 | \item[协商主密钥] 即 master secret, 不过其并不直接用于加密和认证
585 | \end{description} 之后分为4个阶段\begin{enumerate}
586 | \item 建立连接, 协商算法
587 | \item 服务器认证, 服务器发送密钥交换相关消息
588 | \item 可选的客户端认证, 客户端发送密钥交换相关消息
589 | \item 完成确认
590 | \end{enumerate}
591 | \subsection{https应用}
592 | \subsubsection{http的问题}
593 | \begin{description}
594 | \item[嗅探监听] 信息不加密
595 | \item[篡改] 信息没有认证
596 | \item[伪造服务器] http不验证服务器的可信度
597 | \end{description}
598 | \subsubsection{https基本概念}
599 | 相对于http, https将消息按照 SSL 加密后再传输, 并且支持通过服务器证书完成的身份验证等.
600 |
601 |
602 | \section{安全电子邮件}
603 | \subsection{RFC 822}
604 | 定义了最基础的电子邮件传输方式.\par
605 | \paragraph{消息格式} 消息包含信封和内容两个部分, 通过空行隔开. 要求消息是ASCII文本消息.\par
606 | 信封的格式如``关键字: 参数'', 通常有\verb/From/, \verb/To/, \verb/Subject/, \verb/Date/等关键字.
607 | \subsection{MIME}
608 | 针对RFC 822在多媒体传输, 编码等问题, 提出的扩展.\par
609 | 在报头部分增加若干关键字如\verb/Content-Type/, 并且允许多种编码如\verb/base64/.
610 | \subsection{请求响应协议} \begin{description}
611 | \item[RFC 821] 即SMTP协议, 只处理ASCII数据传输. 没有任何安全措施, 没有密码登陆.
612 | \item[POP3] 类似SMTP协议, 允许用户名密码登陆.
613 | \item[IMAP4] 允许有选择地接收邮件等功能.
614 | \end{description}
615 | \subsection{电子邮件安全问题}
616 | \begin{description}
617 | \item[匿名转发] 收件人无法知道真正的发件人
618 | \item[邮件伪造] 假冒其他人发送邮件
619 | \item[抵赖] 可伪造的都可以抵赖
620 | \item[其他] 垃圾邮件和邮件炸弹, 包括邮件病毒等等
621 | \end{description}
622 | \subsection{S/MIME}
623 | 允许发送 MIME 数据, 提供认证, 加密, 数据完整性和抗抵赖.
624 |
625 | \begin{figure}[ht!]
626 | \centering
627 | \includegraphics[height=0.5\textheight,width=0.8\textwidth,keepaspectratio]{s-mime-enc.jpg}
628 | \caption{S/MIME 的加密过程}
629 | \label{s-mime-enc}
630 | \end{figure}
631 |
632 | \begin{figure}[ht!]
633 | \centering
634 | \includegraphics[height=0.4\textheight,width=0.8\textwidth,keepaspectratio]{s-mime-ds.jpg}
635 | \caption{S/MIME 的数字签名}
636 | \label{s-mime-ds}
637 | \end{figure}
638 |
639 | S/MIME支持透明签名, 即对空消息进行数字签名后发送.
640 |
641 | \section{安全电子商务}
642 | \subsection{安全需求和安全问题}
643 | \begin{itemize}
644 | \item 资金流动的保密性. 防止第三方截获交易信息
645 | \item 支付结算数据的完整性. 防止篡改数据
646 | \item 支付双方身份认证.
647 | \item 抗抵赖
648 | \item 效率等
649 | \end{itemize}
650 | SET协议基于信用卡, 完成了\begin{itemize}
651 | \item 私密性
652 | \item 保密性
653 | \item 完整性
654 | \item 抗抵赖
655 | \end{itemize}
656 | \subsection{SET协议}
657 | \subsubsection{参与方}
658 | \begin{description}
659 | \item[持卡人] 意即消费者
660 | \item[商家] 事先和收款行建立信任关系
661 | \item[发卡行] 消费者持卡的银行
662 | \item[收款行] 代替商家与多个发卡行联系, 验证持卡人信息的有效性
663 | \item[证书权威] 可信的第三方, 为持卡人, 商家, 收款行提供证书
664 | \item[支付网关] 收款行控制, 处理商家的支付报文.
665 | \end{description}
666 | \subsubsection{流程}
667 | \paragraph{双签名} 交易中有两种信息, 订单相关和支付相关的.
668 | 只有商家才能看到订单相关的, 只有银行才能看到支付相关的.
669 | 然而需要对两种信息都签名, 并允许商家和银行验证两种信息的签名.\par
670 | 双签名的计算即如\[
671 | \mathrm{Dual\,Signature} = E_{\mathrm{KR}_C} [H(\mathrm{PIMD} \;\|\; \mathrm{OIMD})] \]
672 | \paragraph{购买请求}
673 | 如图
674 | \begin{figure}[ht!]
675 | \centering
676 | \includegraphics[width=\textwidth]{set-1.jpg}
677 | \caption{购买请求, Customer端}
678 | \label{set-1}
679 | \end{figure}
680 | \begin{figure}[ht!]
681 | \centering
682 | \includegraphics[height=\textheight, width=\textwidth, keepaspectratio]{set-2.jpg}
683 | \caption{购买请求, Merchant端}
684 | \label{set-2}
685 | \end{figure}
686 |
687 | \clearpage
688 | \paragraph{支付授权}
689 | \begin{enumerate}
690 | \item Merchant 给 Acquirer 发送支付授权请求
691 | \item Acquirer 确认授权, 给 Issuer 发送划款请求
692 | \end{enumerate}
693 | \paragraph{支付获取}
694 | 完成转账业务
695 |
696 | \section{入侵技术}
697 | \subsection{入侵检测}
698 | \paragraph{审计记录} 包含用户活动记录, 用于检测用户的行为.
699 | \subsubsection{基于统计的入侵检测}
700 | 能够防范假冒者, 但是不能防范合法用户.\par
701 | \begin{description}
702 | \item[阈值检测] 检测一段时间内某用户产生的各种事件.
703 | 如果, 事件发生次数超过其阈值, 则认为可能存在入侵.\\
704 | 本身是很粗糙的, 容易误判.
705 | \item[轮廓检测] 为每个用户建立一个行为轮廓, 学习其行为模式,
706 | 检测用户行为模式的异常变化.
707 | \end{description}
708 | \subsubsection{基于规则的入侵检测}
709 | 基本原理是检测系统中发生的事件,
710 | 运用先定的规则集确定某一个活动模式是否可疑.
711 | 其中先定的规则集是专家系统定义的.
712 | \subsubsection{蜜罐}
713 | 创建一个正常用户不会访问的蜜罐系统,
714 | 引诱攻击者攻击这个没有有用信息的蜜罐系统.\par
715 | 任何对于蜜罐的访问都是可疑的.
716 | \subsection{软件入侵}
717 | 入侵技术分为用户入侵和软件入侵, 课程中着重考虑软件入侵. 软件入侵主要考虑恶意软件.
718 | \begin{table}[ht!]
719 | \centering
720 | \begin{tabularx}{\textwidth}{|C|C|C|C|}
721 | \Topline
722 | & 病毒 & 蠕虫 & 木马 \\
723 | \Midline
724 | 宿主 & 需要 & 不需要 & 需要 \\
725 | \Midline
726 | 表现形式 & 不以文件 & 独立的文件 & 伪装成其他文件 \\
727 | \Midline
728 | 传播方式 & 依赖宿主 & 自主传播 & 依靠用户主动传播 \\
729 | \Midline
730 | 危害 & 破坏数据/系统完整性 & 侵占资源 & 留下后门 \\
731 | \Midline
732 | 传播速度 & 快 & 很快 & 慢 (不能自我复制) \\
733 | \Bottomline
734 | \end{tabularx}
735 | \caption{各种恶意软件的特点}
736 | \end{table}
737 | \subsubsection{后门}
738 | 软件中秘密入口, 可以绕过通常步骤获得权限.
739 | 控制方式包含本地权限提升, 远程执行程序等.
740 | \subsubsection{逻辑炸弹}
741 | 当特定事件发生时, 在被执行制造破坏的代码.
742 | \subsubsection{特洛伊木马}
743 | 伪装成正常程序, 欺骗安装和运行, 使攻击者能获得权限.
744 | 类似后门, 但重点在伪装成正常程序的欺骗性.
745 | \subsubsection{Zombie}
746 | 秘密接管Internet上的计算机, 利用这些计算机发送攻击 (常是DoS).
747 | \subsubsection{病毒}
748 | 一段可以通过修改自身, 修改其他程序的 程序片段.
749 | \subsubsection{蠕虫}
750 | 完整独立的计算机程序, 能够自我复制, 并通过计算机网络传播.
751 |
752 | %\pagebreak
753 | %\appendix
754 | %
755 | %\section{实验 IDS}
756 | %\subsection{检测技术}
757 | %\subsubsection{基于异常}
758 | % 学习正常流量的特征, 只允许满足特征的包.\par
759 | % 较高误报率, 较低漏报率.
760 | %\subsubsection{基于特征}
761 | % 学习异常流量的特征, 不允许满足特征的包.\par
762 | % 较高漏报率, 较低误报率.
763 | %\subsection{常见攻击}
764 | %\subsubsection{DoS/DDoS攻击}
765 | % 不停访问目标资源, 使得目标资源耗尽.
766 | %\subsubsection{DNS放大攻击}
767 | % DNS特点: 回复包比请求包大很多. 方法是伪造发送者, 类似DoS.
768 | %\subsection{实验命令}
769 | %\paragraph{离线模式}
770 | %\begin{verbatim} suricate -c CONFIG_PATH -l LOG_PATH -r DETECT_OUTPUT_PATH \end{verbatim}
771 | %
772 | %\section{实验 Packet Tracer}
773 | %\subsection{设备使用}
774 | %\subsubsection{交换机} 位于链路层. 为端到端通信提供不被干扰的链路.
775 | %\subsubsection{路由器} 位于网路层. cisco IOS 四种模式包含.
776 | % usr mode router>
777 | % privlge mode router#
778 | % conf mode router(config)#
779 | % spec conf mode
780 | % enable : usr -> privlge
781 | % disable: privlge -> usr
782 | % configure terminal: privlge -> conf
783 | % routing tbl:
784 | % interface connections:
785 | % PC: 1 2 send; 3 6 recv;
786 | % switch: 1 2 recv; 3 6 send;
787 | % straight-through: PC - switch
788 | % cross-over: PC - PC, switch - switch
789 | % switch 2960: both straight-through / cross-over are ok to use
790 |
791 |
792 | \end{document}
793 |
794 |
--------------------------------------------------------------------------------
/signal_processing/signal.tex:
--------------------------------------------------------------------------------
1 | % 编译使用xelatex
2 | \documentclass{ctexart}
3 |
4 | \usepackage{amsmath}
5 | \usepackage{amsfonts}
6 | \usepackage{amssymb}
7 | \usepackage{tabularx}
8 | \usepackage[math]{cellspace}
9 | \setlength\cellspacetoplimit{6pt}
10 | \setlength\cellspacebottomlimit{6pt}
11 |
12 | \title{信号处理原理}
13 |
14 | \DeclareMathOperator{\CWT}{\mathbf{CWT}}
15 | \DeclareMathOperator{\DWT}{\mathbf{DWT}}
16 | \DeclareMathOperator{\CTFT}{\mathbf{CTFT}}
17 | \DeclareMathOperator{\ICTFT}{\mathbf{ICTFT}}
18 | \DeclareMathOperator{\DTFT}{\mathbf{DTFT}}
19 | \DeclareMathOperator{\IDTFT}{\mathbf{IDTFT}}
20 | \DeclareMathOperator{\DFT}{\mathbf{DFT}}
21 | \DeclareMathOperator{\IDFT}{\mathbf{IDFT}}
22 | \DeclareMathOperator{\ZT}{\mathbf{ZT}}
23 | \DeclareMathOperator{\ENERGY}{\mathbf{ENERGY}}
24 | \DeclareMathOperator{\POWER}{\mathbf{POWER}}
25 | \DeclareMathOperator{\D}{\mathbf{D}}
26 | \DeclareMathOperator{\ud}{\mathrm{d}}
27 | \DeclareMathOperator{\Rset}{\mathbb{R}}
28 | \DeclareMathOperator{\Cset}{\mathbb{C}}
29 | \DeclareMathOperator{\Zset}{\mathbb{Z}}
30 | \DeclareMathOperator{\sgn}{\mathrm{sgn}}
31 | \DeclareMathOperator{\Sa}{\mathrm{Sa}}
32 | \DeclareMathOperator{\Res}{\mathrm{Res}}
33 | \DeclareMathOperator{\STFT}{\mathbf{STFT}}
34 |
35 | % for simulating booktabs rules; so they work with vertical lines
36 | \newlength{\Oldarrayrulewidth}
37 | \newcommand{\Cline}[2]{
38 | \noalign{\global\setlength{\Oldarrayrulewidth}{\arrayrulewidth}}
39 | \noalign{\global\setlength{\arrayrulewidth}{#1}}\cline{#2}
40 | \noalign{\global\setlength{\arrayrulewidth}{\Oldarrayrulewidth}}}
41 | \newcommand{\Hline}[1]{
42 | \noalign{\global\setlength{\Oldarrayrulewidth}{\arrayrulewidth}}
43 | \noalign{\global\setlength{\arrayrulewidth}{#1}}\hline
44 | \noalign{\global\setlength{\arrayrulewidth}{\Oldarrayrulewidth}}}
45 | \newcommand{\Topline}{\Hline{0.08em}}
46 | \newcommand{\Bottomline}{\Hline{0.08em}}
47 | \newcommand{\Midline}{\Hline{0.05em}}
48 | \newcommand{\CMidLine}[1]{\Cline{0.05em}{#1}}
49 |
50 | % for formatting a row
51 | \newcolumntype{+}{>{\global\let\currentrowstyle\relax}}
52 | \newcolumntype{^}{>{\currentrowstyle}}
53 | \newcommand{\rowstyle}[1]{\gdef\currentrowstyle{#1}#1\ignorespaces}
54 |
55 | % define new stretchable column types
56 | \newcolumntype{L}{>{\raggedright\arraybackslash}X}
57 | \newcolumntype{R}{>{\raggedleft\arraybackslash}X}
58 | \newcolumntype{C}{>{\centering\arraybackslash}X}
59 |
60 | \begin{document}
61 | \maketitle
62 |
63 | \tableofcontents
64 |
65 | \section{记号与定义}
66 | \subsection{记号}
67 | \begin{table}[ht!]
68 | \centering
69 | \begin{tabularx}{\textwidth}{|c|L|}
70 | \Topline
71 | 记号 & 解释和性质 \\ \Midline
72 | $u(t)$ & 单位阶跃信号\\ \Midline
73 | $\delta(t)$ & 单位冲激信号 $\delta(t) = \frac{1}{a} \delta(\frac{t}{a})$ \\ \Midline
74 | $G(t)$ & 原点单位脉冲信号, $G(t) = \begin{cases}
75 | 1 & -\frac{1}{2} < t < \frac{1}{2}\\
76 | 0 & \text{otherwise} \end{cases}$
77 | \\ \Midline
78 | $\Sa(t$) & Sample函数, $\Sa(t) = \begin{cases}
79 | \frac{\sin t}{t} & t \neq 0 \\
80 | 1 & t = 0 \end{cases}$ \\ \Midline
81 | $\Delta_{T} (t)$ & 周期单位冲激序列, $\Delta_T(t) = \sum_{n} \delta(t - n T)$\newline
82 | 其有特别的性质: $\int_{\Rset} f(t) \Delta_T(t) \ud t = \sum_n f(nT)$
83 | \\ \Midline
84 | $\delta(n)$ & 单位冲激序列 $\delta(n) = \begin{cases}1 & n = 0 \\ 0 & \text{otherwise}\end{cases}$ \\ \Bottomline
85 | \end{tabularx}
86 | \end{table}
87 |
88 | \subsection{定义}
89 | \paragraph{信号能量} 对于连续函数, $\ENERGY[f(t)] = \int_{-\infty}^{+\infty} \|f(t)\|^2 \ud t$.
90 | 对于离散函数, $\ENERGY[x(t)] = \sum_{n = -\infty}^{\infty} \|x(n)\|^2$.
91 | \paragraph{信号功率} 对于连续函数, $\POWER[f(t)] = \lim_{T \to \infty} \frac{1}{T} \int_{-T/2}^{+T/2} \|f(t)\|^2 \ud t$.
92 | 对于离散函数, $\POWER[x(t)] = \lim_{N \to \infty} \frac{1}{2N + 1} \sum_{n = -N}^{N} \|x(n)\|^2$.
93 | \subsection{其他}
94 | \begin{description}
95 | \item[Dirichlet积分] $\int_{-\infty}^{\infty} \Sa(t) \ud t = \pi$
96 | \item[Gaussian积分] $\int_{-\infty}^{\infty} e^{-t^2} \ud t = \sqrt{\pi}$
97 | \item[冲激函数的筛选特性] $\int_{-\infty}^{\infty} f(t) \delta(t - t_0) = f(t_0)$
98 | \item[冲激函数的位移特性] $f(t) * \delta(t - t_0) = f(t - t_0)$, 注意冲激函数点乘得值, 卷积得函数
99 | \item[卷积的性质] 一般的卷积满足交换律, 结合律, 分配律.
100 | \item[不收敛的求和] $\sum_{n = -\infty}^{+\infty} e^{-j n \omega T_1} = \omega_1 \Delta_{\omega_1}(\omega)$
101 | \end{description}
102 |
103 | \section{傅里叶级数}
104 | \subsection{三角函数形式的傅里叶级数}
105 | 性质足够良好的周期函数都可以表达为\[
106 | f(t) = a_0 + \sum_{n = 1}^{\infty} \left( a_n \cos n\omega t \,+\, b_n \sin n\omega t \right)\]
107 | 其中$T$为函数的周期, $\omega = \frac{2\pi}{T}$.
108 | \paragraph{求解系数}
109 | 要求$n, m \in \Zset^+$, 则函数基性质为\begin{eqnarray*}
110 | \int_0^{T} \sin nt\, \sin mt \ud t &=& \delta_{nm} \frac{T}{2}\\
111 | \int_0^{T} \cos nt\, \cos mt \ud t &=& \delta_{nm} \frac{T}{2} \\
112 | \int_0^{T} \sin nt\, \cos mt \ud t &=& 0
113 | \end{eqnarray*}
114 | 直接由函数基性质\begin{align*}
115 | a_0 &= \frac{1}{T} \int_T f(t) \ud t\\
116 | a_n &= \frac{2}{T} \int_T f(t) \cos n\omega t \,\ud t\\
117 | b_n &= \frac{2}{T} \int_T f(t) \sin n\omega t \,\ud t
118 | \end{align*}
119 | \subsection{复数形式的傅里叶级数}
120 | 性质足够良好的周期函数都可以表达为\[
121 | f(t) = \sum_{n = -\infty}^{\infty} F_n e^{j n \omega t}
122 | \]其中$T$为函数的周期, $\omega = \frac{2\pi}{T}$.\par
123 | 注意这里指数没有负号, 与傅里叶变换中形式不同.
124 | \paragraph{求解系数}
125 | 利用函数基性质 \[
126 | \int_T e^{j n \omega t} e^{j (-m) \omega t} \ud t = T \delta_{nm}\]
127 | 有\[
128 | F_n = \frac{1}{T} \int_T f(t) e^{-j n \omega t} \ud t\]
129 |
130 | \section{CTFT}
131 | \subsection{定义}
132 | 注意信号的傅里叶变换不一定存在. 本课程中不考虑不存在的情况.
133 | \paragraph{对于非周期信号} $f(t)$的CTFT定义为 \[
134 | \CTFT[f(t)] = F(\omega) = \int_{-\infty}^{+\infty} f(t)\; e^{-j \omega t} \ud t \]
135 | 可以看成$f(t)$和基函数$e^{j \omega t}$的内积.\footnote{这里内积是酉空间内积, 需满足共轭交换}\par
136 | 逆变换定义为 \[
137 | \ICTFT[F(\omega)] = f(t) = \frac{1}{2\pi} \int_{-\infty}^{+\infty} f(t)\; e^{j \omega t} \ud t \]
138 |
139 | \paragraph{对于周期信号}
140 | 设周期为$T_1$, 周期信号$f$的单周期截断是$f_0$, 显然$f = f_0 * \Delta_T$,
141 | 则有 \[
142 | \CTFT[ f(t) ] = \omega_1 \Delta_{\omega_1}(\omega) \CTFT[ f_0(t) ] ,\quad \omega_1 = \frac{2\pi}{T_1} \]
143 |
144 | \begin{table}[ht!]
145 | \begin{tabularx}{\textwidth}{|>{\bfseries}r L|}
146 | \Topline
147 | 线性性 & $\displaystyle \CTFT[\alpha f + \beta g] = \alpha \CTFT[f] + \beta \CTFT[g]$\\
148 | 唯一性 & $\displaystyle \ICTFT[ \, \CTFT[f(t)] ] = f(t)$,
149 | 由此有$\displaystyle \ICTFT = \frac{1}{2\pi} \mathbf{R} \cdot \CTFT$,
150 | 其中$\mathbf{R}$是反转算子\\
151 | 对偶性 & $\displaystyle \CTFT [ \, \CTFT[ f(t) ] ] = 2 \pi f(-t)$, $\displaystyle \CTFT [ F(\nu) ] = f(-t)$
152 | 特别注意最后的负号\\
153 | 反褶 & $\displaystyle \CTFT[f(-t)] = F(-\omega)$\\
154 | 共轭 & $\displaystyle \CTFT[f^*(t)] = F^*(-\omega)$\\
155 | 压阔 & $\displaystyle \CTFT[f(at)] = \frac{1}{|a|} F(\frac{\omega}{a})$\\
156 | 时移 & $\displaystyle \CTFT[f(t + \tau)] = F(\omega) e^{j \omega \tau}$\\
157 | 频移 & $\displaystyle \CTFT[f(t) e^{j \omega_0 t}] = F(\omega - \omega_0)$\\
158 | 微分 & $\displaystyle \CTFT[\D f(t)] = j \omega F(\omega)$ \newline
159 | $\CTFT[-j \omega f(t)] = \D F(\omega)$\\
160 | 卷积 & $\displaystyle \CTFT[f g] = \frac{1}{2\pi} \CTFT[f] * \CTFT[g]$\newline
161 | $\CTFT[f * g] = \CTFT[f] \CTFT[g]$\\
162 | 和FS的关系 & $f(t)$是周期为$T_1$的函数, 傅里叶级数为$f(t) = \sum_n F_n e^{j n \omega_1 t}$,
163 | 则$F_0(n\omega_1) = T_1 F_n$, 且$F(\omega) = \sum_n 2\pi F_n \delta(\omega - n \omega_1)$\\
164 | \Bottomline
165 | \end{tabularx}
166 | \caption{CTFT的性质}
167 | \end{table}
168 |
169 | \begin{table}[ht!]
170 | \centering
171 | \begin{tabular}{|c|c|}
172 | \hline $\displaystyle f(t)$ & $\displaystyle F(\omega)$ \\ \hline
173 | $\displaystyle e^{-at}\,u(t),\quad a > 0$ & $\displaystyle \frac{1}{a + j \omega}$ \\ \hline
174 | $\displaystyle e^{at}\,u(t),\quad a \in \Rset$ & 不存在 \\ \hline
175 | $\displaystyle e^{j \omega_0 t},\quad a \in \Rset$ & $\displaystyle 2 \pi \delta(\omega - \omega_0)$ \\ \hline
176 | $\displaystyle G(t)$ & $\displaystyle \Sa(\omega / 2)$ \\ \hline
177 | $\displaystyle \delta(t)$ & $\displaystyle 1$ \\ \hline
178 | $\displaystyle 1$ & $\displaystyle 2\pi \delta(\omega)$ \\ \hline
179 | $\displaystyle u(t)$ & $\displaystyle \pi\delta(\omega) + \frac{1}{j\omega}$ \\ \hline
180 | $\displaystyle \cos (\omega_0 t)$ & $\displaystyle \pi \delta(\omega - \omega_0) + \pi \delta(\omega + \omega_0)$ \\ \hline
181 | $\displaystyle \sin (\omega_0 t)$ & $\displaystyle j \pi \delta(\omega + \omega_0) - j \pi \delta(\omega - \omega_0)$ \\ \hline
182 | $\displaystyle \Delta_{T_1} (t)$ & $\displaystyle \omega_1 \Delta_{\omega_1}(\omega),\quad \omega_1 = \frac{2\pi}{T_1}$ \\ \hline
183 | $\displaystyle e^{j \omega_0 t} [\alpha \le t < \beta]$ & $\displaystyle \frac{e^{j(\omega_0 - \omega)\beta} - e^{j(\omega_0-\omega)\alpha}}{j (\omega_0 - \omega)}$ \\ \hline
184 | $\displaystyle \sgn(t)$ & $\displaystyle \frac{2}{j\omega}$\\\hline
185 | $\displaystyle \frac{1}{t}$ & $\displaystyle -j \pi \sgn(\omega)$\\\hline
186 | $\displaystyle \Sa(t)$ & $\displaystyle \pi G \left( \frac{\omega}{2} \right) $\\ \hline
187 | \end{tabular}
188 | \caption{常见函数的CTFT}
189 | \end{table}
190 |
191 | \subsection{抽样信号}
192 | \subsubsection{抽样定理}
193 | \paragraph{抽样过程} 自然地, 采用冲激串抽样 $f_S(T) = f(t)\cdot \Delta_{T_S}(t)$
194 | \paragraph{抽样后傅里叶变换} $F_S(\omega) = \CTFT[ f_S(t) ] = \frac{1}{T_S} \sum_n F(\omega - n \omega_S)$.
195 | 可以看出, 信号经过$T_S$抽样之后, 其CTFT变成一个周期为$\omega_S$的周期函数.
196 | \paragraph{抽样定理} 容易通过上面的和式证明如下定理.
197 | 对于截止频率为$\omega_c$的信号$f(t)$, 若$\omega_S \ge 2 \omega_c$, 则$F_S$是周期为$\omega_S$的函数,
198 | 并且\[
199 | \forall \omega\in\Rset\;:\;F_S(\omega) = \frac{1}{T_S} F(\omega \bmod \omega_S)\]
200 | 其一个重要特例就是
201 | $\forall \omega \in [-\frac{\omega_S}{2}, \frac{\omega_S}{2}]\;:\;F_S(\omega) = \frac{1}{T_S} F(\omega)$.
202 | 其中$2\omega_c$称为Nyquist频率.
203 | \subsubsection{抽样应用}
204 | \paragraph{欠采样} 事实上, 利用$F_S(\omega) = \frac{1}{T_S} \sum_n F(\omega - n\omega_S)$,
205 | 可以使用低于Nyquist频率的采样, 得到无损结果.
206 | 这要求信号的频率宽度小于采样频率$|\{\omega\,|\,F(\omega) \neq 0\}| < \omega_S$.
207 | (这里$|S|$是$\Rset$上的长度测度)\par
208 | 这样上面和式中只有一项非0, 因此
209 | \[\forall \omega\in\Rset\;:\;\exists k \in \Zset\;:\;F_S(\omega) = \frac{1}{T_S} F(\omega + k \omega_S)\]
210 | 如果我们再知道抽样前函数的频率范围, 就可以无损的得到原函数的CTFT.
211 | \paragraph{超采样} 主要是工程应用, 略.
212 |
213 | \clearpage
214 |
215 |
216 | \section{DTFT}
217 | \subsection{前置定理}
218 | \paragraph{抽样离散定理} 由定义可证下式
219 | \[F_S(\omega) = \sum_{n=-\infty}^{\infty} f(nT_S) e^{- j \omega n T_S}\]过程如下
220 | \begin{align*}
221 | F_S(\omega) &= \int_{-\infty}^{\infty} f(t) \cdot \Delta_{T_S}(t) \cdot e^{-j \omega t} \ud t\\
222 | &= \int_{-\infty}^{\infty} f(t) \cdot e^{-j \omega t} \cdot \Delta_{T_S}(t) \ud t\\
223 | &= \sum_{n=-\infty}^{\infty} f(nT_S) e^{-j \omega nT_S}
224 | \end{align*}
225 | 可以看成是一种抽样近似的思想, 因为在Nyquist区间内, 有\begin{eqnarray*}
226 | F(\omega) &=& \frac{1}{T_S} F_S(\omega)\\
227 | \int_{-\infty}^{+\infty} f(t) e^{-j \omega t} \ud t &=& \frac{1}{T_S} \sum_{n = -\infty}^{+\infty} f(nT_S) e^{-j \omega nT_S}
228 | \end{eqnarray*}
229 |
230 | \subsection{定义} $x(n)$为频率/时间归一化的信号, $x(n) = f(nT_S)$, 则有\[
231 | \DTFT[x(n)] = \sum_{n = -\infty}^{+\infty} x(n) e^{-j \omega n} \]
232 | 并且定义逆变换为 \[
233 | \IDTFT[X(\omega)] = \frac{1}{2\pi} \int_{-\pi}^{\pi} X(\omega) e^{j \omega n} \ud \omega\]
234 |
235 | \subsection{时间/频率归一化}
236 | CTFT处理, $f(t), F(\omega)$中的$t, \omega$是物理空间中的时间/频率, 称为模拟时间/频率.
237 | DTFT中, 以上定义中$x(n), X(\omega)$中的$n, \omega$是数字时间/频率.\par
238 | 数字时间用$n$表示, 范围是整数集$\Zset$, 而模拟时间$t$的范围是实数集$\Rset$.
239 | 数字 (角) 频率的范围是一个Nyquist区间$[0, 2\pi)$, 而模拟频率的范围也是整个实数集$\Rset$.\par
240 | 当且仅当完成无损采样的情况下, 数字时间/频率才有意义.
241 | 数字时间$n$, 数字频率$\omega_D$, 模拟时间$t$和模拟频率$\omega_A$的关系如下式.
242 | \begin{eqnarray*}
243 | n & \Rightarrow & t = n T_S \\
244 | t & \Rightarrow & n =
245 | \begin{cases} \frac{t}{T_S} & \frac{t}{T_S} \in \Zset \\ \text{undefined} & \text{otherwise (not on sampled point)} \end{cases}\\
246 | \omega_A & \Rightarrow & \omega_D = \frac{\omega_A}{\omega_S} 2\pi \\
247 | \omega_D & \Rightarrow & \omega_A = \frac{\omega_D}{2 \pi} \omega_S
248 | \end{eqnarray*}
249 |
250 | 上式中$T_S$是符合Nyquist要求的采样周期, $\omega_S = \frac{2\pi}{T_S}$.
251 | 可以看出, 数字时间的范围是离散的$\Zset$.
252 |
253 | \begin{table}[ht!]
254 | \begin{tabularx}{\textwidth}{|>{\bfseries}r L|}
255 | \Topline
256 | 和CTFT关系 & $\displaystyle \DTFT[x](\omega) = \CTFT[f_S](\frac{\omega}{T_S})$
257 | 如果采样频率在Nyquist区间内, 则有 $X(\omega) = \frac{1}{T_S} F\left(\frac{\omega}{T_S}\right)$\\
258 | 线性性 & $\displaystyle \DTFT[\alpha x + \beta y] = \alpha \DTFT[x] + \beta \DTFT[y]$\\
259 | 周期性 & $\displaystyle X(\omega + 2\pi) = X(\omega)$\\
260 | 反褶 & $\displaystyle \DTFT[x(-n)] = X(-\omega)$\\
261 | 共轭 & $\displaystyle \DTFT[x^*(n)] = X^*(-\omega)$\\
262 | 压扩 & $\displaystyle \DTFT[x(n/a)] = X(a\omega), \quad a \in \Zset$\\
263 | 时移 & $\displaystyle \DTFT[x(n + n_0)] = X(\omega) e^{j \omega n_0}$\\
264 | 频移 & $\displaystyle \DTFT[x(n) e^{j \omega_0 n}] = X(\omega - \omega_0)$\\
265 | 微分 & $\displaystyle \DTFT[nx(n)] = j \D X(\omega)$\\
266 | 卷积 & $\displaystyle \DTFT[x * y] = X Y,\quad \DTFT[x y] = \frac{1}{2\pi} X * Y$
267 | 其中的卷积$X * Y$是周期函数的卷积$\int_{-pi}^{\pi} X(\vartheta) Y(\omega - \vartheta) \ud \vartheta$\\
268 | 能量 & $\displaystyle \ENERGY[x] = \frac{1}{2\pi} \ENERGY[\DTFT[x]]$, 亦称Parseval定理\\
269 | \Bottomline
270 | \end{tabularx}
271 | \caption{DTFT的性质}
272 | \end{table}
273 |
274 | \clearpage
275 |
276 | \section{DFT}
277 | \subsection{定义}
278 | 数字信号$x$是长度为$L$的信号$x(0), x(1) \ldots x(L - 1)$,
279 | 考虑$X = \DTFT[x]$在$\omega_k$上的值$X(\omega_k)$. 其中
280 | $\omega_0 = 0, \omega_1 = \frac{2\pi}{N} \ldots \omega_{N - 1} = \frac{(N-1) \cdot 2\pi}{N}$
281 | 是Nyquist区间$[0, 2\pi)$上均匀分布的$N$个频率. 这样即得到DFT的定义
282 | \[
283 | X(k) = X(\omega_k) = \DFT[x(n)] = \sum_{n = 0}^{L - 1} x(n) W_N^{nk},\quad 0 \le k \le N - 1\]
284 | 称为其N点DFT. 其中$W_N$是$N$次单位根\emph{倒数}$W_N = e^{- j \frac{2 \pi}{N}}$.\par
285 | 另外定义其逆变换IDFT为\[
286 | \hat{x}(n) = \frac{1}{N} \sum_{k = 0}^{N - 1} X(k) W_N^{-nk},\quad 0 \le n \le N -1 \]
287 |
288 | \subsection{补零和回绕} 对于$L \neq N$的$x(n)$, 可以通过补零和回绕, 得到一个$L = N$的$\hat{x}(n)$, 满足$\hat{X}(k) = X(k)$.
289 | 注意补零和回绕只会改变$L$.
290 | \paragraph{补零} 如果$L < N$, 考虑$\hat{x}(n) = \begin{cases} x(n) & 0 \le n \le L - 1\\ 0 & \text{otherwise} \end{cases}$
291 | \paragraph{回绕} 如果$L > N$, 考虑$\hat{x}(n) = \begin{cases} \sum_m x(n + mN) & 0 \le n \le N - 1 \\ 0 & \text{otherwise} \end{cases}$\par
292 | 由定义, 从IDFT只能得到回绕序列$\hat{x}$, 即DFT不满足唯一性 $\IDFT[\DFT [x]] \neq x$. 但是在$N = L$的情况下, DFT仍然满足唯一性.
293 | \paragraph{信号在时间上的约定}
294 | 归一化情况下, 对于$n < 0$或者$n \ge L$, $x(n)$的取值有多种约定 \begin{enumerate}
295 | \item $x(n) = x(n \mod L),\quad n < 0, n \ge L$
296 | \item $x(n) = 0,\quad n < 0, n \ge L$
297 | \item $x(n)$未定义$,\quad n < 0, n \ge L$
298 | \end{enumerate}
299 | 不同的作者, 不同的上下文会选择不同的约定.
300 |
301 | \subsection{和诸傅里叶变换的相关性}
302 | \paragraph{和DTFT} DFT可以看成DTFT抽样,
303 | 其只取Nyquist区间中等间距的$N$个点$\frac{k}{N}2\pi$.
304 | 因此$N$越大, DFT越接近DTFT.
305 | \paragraph{和CTFT} CTFT经过可能有损失的时域抽样和有损失的加窗, 以及频域抽样到DFT,
306 | 损失了很多信息. 引入的误差包括 \begin{itemize}
307 | \item $f(t)$时域抽样时, 如果不满足Nyquist定理, 引入混叠
308 | \item $x(n)$加窗时, 引入的频率泄露. 这个在课程中一般忽略.
309 | \item $X(\omega)$抽样时, 损失的细节信息. 这个没有严密讨论过.
310 | \end{itemize}
311 | 在课程中, 一般认为只要满足Nyquist定理的采样, DFT都可以大致反映CTFT.
312 | 对于无法满足Nyquist定理的情况, 如频域在$\Rset$上都有,
313 | 一般先在频域加窗, 即通过一个低通滤波器.\par
314 | 例如$f(t)$峰值频率在$\omega_p$, 在符合Nyquist定理的采样条件下,
315 | DFT的峰值应在$\mathrm{round}\left(\frac{\omega_p}{\omega_S}N\right)$.\par
316 | \paragraph{采样分辨率} 即频域分辨率. 它是一个没有严格定义的概念,
317 | 意义就是频谱分量 (i.e. 频谱中明显较高的峰) 之间的最小可分辨距离,
318 | 即所谓的 ``波峰可分辨'' 性.\par
319 | DTFT的结果已经在抽样定理中讨论过.\par
320 | 对于DFT, 如果希望达到$\Delta \omega$的分辨率,
321 | 假设$\Delta \omega$是数字频率, 有充要条件$L \ge \frac{2\pi}{\Delta \omega}$.
322 |
323 | \subsection{性质}
324 | 基本与DTFT性质相同. 另外容易看出, 可以使用矩阵乘法计算DFT.\par
325 | \begin{table}[ht!]
326 | \begin{tabularx}{\textwidth}{|>{\bfseries}r L|}
327 | \Topline
328 | 和DTFT的关系 & $\DFT[x](k) = \DTFT[x](\frac{k}{N} 2\pi)$\newline
329 | 注意DTFT参数是有限长的$x(n),\;n\in[0, L)$\newline
330 | 而不是通常的$f(nT_S),\;n\in\Zset$ \\
331 | 和CTFT的关系 & 如果采样符合Nyquist频率, 则有\newline
332 | $X(k) \approx \frac{1}{T_S} \CTFT[f] (\frac{k}{N} \omega_S)$ \newline
333 | 其中$f$是原函数, $f_{0\ldots LT_S}$是$f$截断到区间$[0, LT_S)$的函数,
334 | $x(n) = f(nT_S)$, 不是严格等于因为加窗的引入的误差 \\
335 | 和FS的关系 & TODO: check (课件上写的, 时间缘故我没还有证明)\newline
336 | $\frac{1}{N} X(k) = F_k$, 要求$f$是周期的, 一个周期抽样$N$个点, 为$N$点DFT. \\
337 | 线性性 & $\displaystyle \DFT[\alpha x + \beta y] = \alpha \DFT[x] + \beta \DFT[y]$ \\
338 | 周期性 & $\displaystyle X(k + N) = X(k)$ \\
339 | 对偶性 & $\displaystyle \DFT[X(k)] = N x(-n)$ \\
340 | 反褶 & $\displaystyle \DFT[x(-n)] = X(-k)$ \\
341 | 共轭 & $\displaystyle \DFT[x^*(n)] = X^*(-k)$ \\
342 | 时移 & $\displaystyle \DFT[x(n + m)] = X(k) W_N^{km}$ \\
343 | 频移 & $\displaystyle \DFT[x(n) W_N^{nm}] = X(k + m)$ \\
344 | 卷积 & $\displaystyle \DFT[x * y] = X Y$\newline
345 | $\DFT[x y] = \frac{1}{N} X * Y$\newline
346 | 其中的卷积$X * Y$是周期函数的卷积$\sum_{m = 0}^{N-1} X(m) Y(n-m)$ \\
347 | 能量 & $\displaystyle \ENERGY[x] = \frac{1}{N} \ENERGY[\DFT[x]]$, 亦称Parseval定理\\
348 | \Bottomline
349 | \end{tabularx}
350 | \caption{DFT的性质}
351 | \end{table}
352 |
353 | \subsection{例子}
354 | 以下均假设DFT为N点DFT.
355 | \begin{table}[ht!]
356 | \centering
357 | \begin{tabularx}{\textwidth}{|CC|}
358 | \Topline $\displaystyle x(n)$ & $\displaystyle X(k)$ \\ \Midline
359 | $\displaystyle G_N(n)$ & $\displaystyle N\delta(k) = \begin{cases} N & k = 0\\ 0 & \text{otherwise}\end{cases}$
360 | \\
361 | \Bottomline
362 | \end{tabularx}
363 | \caption{DFT的例子}
364 | \end{table}
365 |
366 | \subsection{FFT}
367 | 设$N = 2N'$, 令$y(n) = x(2n), z(n) = x(2n+1),\quad 0 \le n \le N' - 1$, 则有\[
368 | \DFT_{N} [x] = \DFT_{N'} [y] + W_N^k \DFT_{N'} [z] \]
369 | 按照此原理, 容易得到$\Theta(n \lg n)$的DFT算法, 而按定义计算是$\Theta(n^2)$的.
370 |
371 | \section{STFT}
372 | \subsection{时变信号} 从物理的角度, 一个信号在不同时间包含的分量是不同的.
373 | 如开来的火车笛声, 最开始是较高, 之后变低.
374 | 因此需要考虑信号在某个时间点/段的频域表示, 然而传统CTFT只在整个$\Rset$上考虑.
375 | \subsection{STFT}
376 | 对于时变信号$f(t)$, 定义其STFT为\[
377 | \STFT[f(t)] = F(\tau, \omega) = \int_{-\infty}^{+\infty} f(t) w(t - \tau) e^{-j\omega t} \ud t\]
378 | 其中$w(t)$是窗函数.
379 | 课程中不知为何积分中是$w^*$, 本应是先将$f(t)$加窗之后再和傅里叶基求内积的.
380 | 因此为了自然这里稍有改动.
381 | \paragraph{窗函数相关} 显然窗函数$w$需要满足的性质是,
382 | 只在一个小区间$[-t_0, t_0]$中的值足够大, 此区间外都可以看成$0$.
383 | 这个区间的长度称为窗口大小.\par
384 | 显然, 窗口越小, 时间分辨率越好 i.e. $F(\tau, \omega)$越能反映在$\tau$这个点而不是其附近一片的性质;
385 | 窗口越大, 频率分辨率越好.\par
386 | 窗函数可以选择矩形窗函数, 但更常选择Gaussian窗$w(t) = e^{-\frac{t^2}{2 \sigma^2}}$
387 | \paragraph{和原始CTFT的关系} 显然有下式. 从中看出, STFT事实上就是信号加窗的CTFT.
388 | \[ \STFT[f(t)](\tau, \omega) = \CTFT[f(t)w(\tau-t)](\omega)\]
389 |
390 | \section{Wavelet Transform}
391 | \subsection{基本思想}
392 | \subsubsection{背景}
393 | STFT的问题是, 窗函数是固定的 i.e. 窗口大小不能变化.
394 | 即, 时间分辨率/频率分辨率不能变.\par
395 | 而考虑现实情况, 比如人对于一个频率范围内的声音最敏感.
396 | 如同样的声高变化, 在1000 Hz左右比在15000 Hz左右更容易被察觉.
397 | 即, 在1000 Hz 左右, 人耳的频率分辨率更高.\par
398 | 因此需要对于不同频率-时间点, 有不同的分辨率.
399 | \subsubsection{小波定义}
400 | 称$\psi(t)$为小波基函数, 或小波母函数, 当且仅当
401 | \[ \int_{-\infty}^{+\infty} \psi(t) \ud t = 0
402 | \quad\land\quad
403 | \ENERGY[\psi(t)] < \infty \]
404 | \paragraph{小波基的时移和压扩}
405 | 单个小波基类似于$e^{j\omega_0 t}$, 只包含频率信息.
406 | 为了让其包含时间和窗口大小信息, 实际使用的基是$
407 | \psi_{s\tau}(t) = \frac{1}{\sqrt{s}} \psi\left(\frac{t-\tau}{s}\right) $\par
408 | 可以看出, 时移用于确定时间; 压阔用于控制在这个时间-频率点上, 窗函数的大小;
409 | 而$\frac{1}{\sqrt{s}}$用来使得能量不变.
410 | \subsubsection{连续小波变换}
411 | 类似所有的基分解思想, $f(t)$与基$\psi_{s\tau}(t)$内积得到分量值, 如下式所示
412 | \[
413 | \CWT[f(t), \psi(t)](s, \tau) = \int_{-\infty}^{+\infty} f(t) \psi^*_{s\tau}(t) \ud t
414 | \]
415 | 其中频域信息$\omega$被包含在了$\psi(t)$中.
416 | \subsubsection{Haar小波变换}
417 | Haar小波变换用于图像编码, 利用了差分思想.
418 | \paragraph{1维情况} 考虑长度为$l = 2^n$的整向量$ \mathbf{v}$, 则定义其2元差分Haar变换 (下称1维Haar变换或直接Haar变换) 为
419 | \begin{eqnarray*}
420 | \mathrm{HT}(\mathbf{v}) &=&
421 | \mathrm{HT}\left(\left\langle \frac{v_1+v_2}{2} \ldots \frac{v_{l-1} + v_{l}}{2} \right\rangle\right)
422 | \| \left\langle \frac{v_1-v_2}{2} \ldots \frac{v_{l-1}-v_{l}}{2}\right\rangle\\
423 | \mathrm{HT}\left(\left\langle v_1 \right\rangle\right) &=& \left\langle v_1 \right\rangle
424 | \end{eqnarray*}显然这是一个可逆变换.\par
425 |
426 | \paragraph{2维情况} 只考虑灰度图像$I_{n\times m}$, 其中要求$m$是$2$的幂.
427 | 事先选取一个阈值$\delta$, 认为小于$\delta$的差分值就是0. 之后按照如下步骤编码
428 | \begin{enumerate}
429 | \item 对所有行执行上述1位Haar小波变换
430 | \item 对所有列执行上述1位Haar小波变换
431 | \item (有损压缩) 将$I$中所有模小于$\delta$的数置零
432 | \end{enumerate}
433 | 对结果依次执行列/行Haar逆变换就得到压缩后的图像.
434 |
435 | \paragraph{Haar变换的矩阵描述} 为方便, 考虑1维情形, $n=8$.
436 | 迭代计算Haar变换, 第一次得到结果是
437 | $\left\langle \frac{v_1+v_2}{2}, \frac{v_3+v_4}{2}, \frac{v_5+v_6}{2}, \frac{v_7+v_8}{2},
438 | \frac{v_1-v_2}{2}, \frac{v_3-v_4}{2}, \frac{v_5-v_6}{2}, \frac{v_7-v_8}{2}\right\rangle$.\par
439 | 其等于$\left[ v_1, v_2, v_3, v_4, v_5, v_6, v_7, v_8\right] H_1$, 其中
440 | \[
441 | H_1 = \begin{bmatrix}
442 | \frac{1}{2} & 0 & 0 & 0 & \frac{1}{2} & 0 & 0 & 0 \\
443 | \frac{1}{2} & 0 & 0 & 0 & -\frac{1}{2} & 0 & 0 & 0 \\
444 | 0 & \frac{1}{2} & 0 & 0 & 0 & \frac{1}{2} & 0 & 0 \\
445 | 0 & \frac{1}{2} & 0 & 0 & 0 & -\frac{1}{2} & 0 & 0 \\
446 | 0 & 0 & \frac{1}{2} & 0 & 0 & 0 & \frac{1}{2} & 0 \\
447 | 0 & 0 & \frac{1}{2} & 0 & 0 & 0 & -\frac{1}{2} & 0 \\
448 | 0 & 0 & 0 & \frac{1}{2} & 0 & 0 & 0 & \frac{1}{2} \\
449 | 0 & 0 & 0 & \frac{1}{2} & 0 & 0 & 0 & -\frac{1}{2} \\
450 | \end{bmatrix}
451 | \]同理可以构造$H_2$等, 且可证$H_i$可逆.
452 |
453 | \section{系统}
454 | \subsection{基本概念}
455 | \paragraph{理解}
456 | 一般的看法是, 信号进入一个系统, 然后出来后变成另一个信号. 即, 系统对信号做了一个变换.
457 | 因此, 系统可以看作一个信号域上的算子.\par
458 | 以$\mathcal{F}, \mathcal{G}\ldots$记系统.\par
459 | 滤波器也是系统, 但是着重强调其对于系统在频域上的变换.
460 | \paragraph{课程限制}
461 | 课程中只考虑离散时间系统, 信号的时域是离散的.
462 | 即信号域是$x(n) \in \Cset,\; x\in \Zset$, 输入输出信号都是这种形式.
463 | 滤波器也是数字滤波器, 频域只考虑一个Nyquist区间$[0, 2\pi)$或者$[-\pi, \pi)$.\par
464 | 并且都满足线性性, 时不变性, 因果性.\par
465 | 另外假设输入$x$满足因果性, 即$x(n) = 0, \;\forall\, n < 0$.
466 | \paragraph{其他概念}
467 | \begin{description}
468 | \item[线性性] $\mathcal{F}[\alpha x + \beta y] = \alpha \mathcal{F}[x] + \beta \mathcal{F}[y]$
469 | \item[时不变性] $\mathcal{F}[x(n + m)](k) = \mathcal{F}[x(n)](k + m)$
470 | \item[因果性] $y = \mathcal{F}[x]$, 则$y(n)$是$\{ x(k) \;|\; k \le n \}$的函数
471 | \item[稳定性] $y = \mathcal{F}[x]$, 若$x$是有界的, 则$y$是有界的.\\
472 | i.e. $\|x\|_{\infty} < \infty \Rightarrow \|y\|_{\infty} < \infty$
473 | \end{description}
474 | 课程考虑的系统必定满足 线性性, 时不变性, 因果性. 如不考虑$x(n) \to x^2(n)$.
475 |
476 | \subsection{时域描述}
477 | \subsubsection{差分方程}
478 | $y = \mathcal{F}[x]$由以下方程确定 \[
479 | \sum_{k = 0}^N a_k y(n - k) = \sum_{k = 0}^M b_k x(n - k)\]
480 | 称如上的系统为$N$阶滤波器.
481 | \subsubsection{流图}
482 | 有三种单元: 延时单元, 加法单元, 乘法单元. 如图\ref{sys-unit-graph}所示.
483 | \begin{figure*}[ht!]
484 | \centering
485 | \includegraphics[width=0.6\textwidth]{sys-unit-graph.png}
486 | \caption{三种流图单元}
487 | \label{sys-unit-graph}
488 | \end{figure*}
489 | \paragraph{构建方法} \begin{itemize}
490 | \item 直接I型构建法. 简单明白.
491 | \item 级联滤波器. 减少延时单元的深度, 降低因过深的延时单元链造成的指数级误差.
492 | \item 直接II型构建法. 减少了对输入和输出状态的存储, 即延时单元减少.
493 | \end{itemize}注意图\ref{dir-1}和图\ref{dir-2}中$a_k, b_k$和差分方程中的$a_k, b_k$定义不同. 下图中, 是\[
494 | y(n) = \sum_{k = 0}^M a_k x(n-k) + \sum_{k = 1}^{N} b_k y(n-k)\]
495 | \begin{figure}[ht!]
496 | \centering
497 | \includegraphics[width=0.95\textwidth]{dir-1.png}
498 | \caption{直接I型}
499 | \label{dir-1}
500 | \end{figure}
501 | \begin{figure}[ht!]
502 | \centering
503 | \includegraphics[width=0.95\textwidth]{dir-2.png}
504 | \caption{直接II型}
505 | \label{dir-2}
506 | \end{figure}
507 |
508 | \subsubsection{冲激响应}
509 | $\mathcal{F}[\delta(n)]$称为$\mathcal{F}$的冲激响应 (Impluse Response).
510 | 根据冲激响应是否在有限时间内收敛到$0$, 也分为有限和无限冲激响应.\par
511 | 记$h(n) = \mathcal{F}[\delta]$, 因为$x(n) = \sum_k x(k) \delta(n - k) = x * \delta$,
512 | 故不严谨地有,
513 | \begin{align*}
514 | \mathcal{F}\left[x\right] &= \mathcal{F}\left[x * \delta\right] \\
515 | &= \mathcal{F}\left[\sum_k x(k)\delta(n - k)\right]\\
516 | &= \sum_k x(k) \mathcal{F}\left[\delta(n - k)\right]\\
517 | &= \sum_k x(k) h(n - k)\\
518 | &= x * \mathcal{F}\left[\delta\right]
519 | \end{align*}
520 | \paragraph{应用} \begin{description}
521 | \item[稳定定理] $\sum_n |h(n)|$绝对收敛 $\Leftrightarrow$ $\mathcal{F}$是稳定系统.
522 | \item[串联] 设$\mathcal{F}[\delta] = h_{\mathcal{F}},\,\mathcal{G}[\delta] = h_{\mathcal{G}}$,
523 | 则$\mathcal{F} \mathcal{G} [\delta] = h_{\mathcal{G}} * h_{\mathcal{F}} $
524 | \item[并联] $\left(\mathcal{F} + \mathcal{G}\right) [\delta] = h_{\mathcal{F}} + h_{\mathcal{G}}$
525 | \end{description}
526 |
527 | \subsection{频域描述}
528 | 以下频域描述都采用DTFT而非CTFT.
529 | \subsubsection{定义} $X = \DTFT[x]$是信号的频域表示, $Y = \mathcal{F}[X]$,
530 | 则定义$H(\omega) = \frac{X(\omega)}{Y(\omega)}$为滤波器的频率响应.\par
531 | 其中将$|H(\omega)|$称为幅频响应, $\arg H(\omega)$称为相频响应.
532 | \paragraph{和冲激响应的关系} 对$y = x * h$两边DTFT得到$Y = X \cdot \DTFT[h]$\\ 故有$H = \DTFT[h]$, 其满足DTFT的所有性质.
533 |
534 | \subsection{Z变换}
535 | \subsubsection{定义} $x(n)$是离散时域信号,
536 | 则$x$的Z变换定义为$X(z) = \ZT[x(n)](z) = \sum_n x(n) z^{-n}$\par
537 | 形式上类似DTFT中$e^{-jwn}$换成了$z^{-n}$, 变成了Laurent级数的形式.
538 | 但是注意Laurent级数是$\sum_n x (n) z^n$, 而Z变换是$\sum_n x (n) z^{-n}$.
539 | \subsubsection{收敛域} $X(z)$的收敛域 (ROC) 定义为 $\{z \;|\; X(z)\text{收敛}\}$.
540 | Laurent级数收敛域是一个环, 但是课程中不考虑环的边界.
541 | \paragraph{收敛域求解} $\Omega$为Laurent级数$x(n),\,n\in\Zset$的收敛环, 则
542 | \[\forall z \in \mathring{\Omega}\;:\;
543 | \sum_n x(n)z^{-n}\;\text{收敛} \Leftrightarrow
544 | \sum_n x(n)z^{-n}\;\text{绝对收敛} \]
545 | 收敛环可以通过比值法或根值法求出.
546 | \paragraph{零--极点图} 在复平面上用$\times$表示极点,
547 | $\circ$表示零点, 画出所有零点和极点, 即得到零--极点图.
548 | \subsection{函数空间的理解}
549 | 可以认为, 基函数是$u_n(z) = z^{-n},\,n \in \Zset$, 由Laurent级数容易得到基函数的完备性.\par
550 | 设$C$是任何一个以$O$为圆心的圆环, 内积定义为\begin{align*}
551 | \left\langle u_n(z), u_m(z) \right\rangle &= \oint_C u_n(z) u_m^*(z) z^{-1} \ud z\\
552 | &= \oint_C z^{-n} (z^{-m})^* z^{-1} \ud z\\
553 | &= \oint_C r^{-n} e^{-i \theta n} r^{-m} e^{-i \theta m} r^{-1} e^{-i \theta} \ud z\\
554 | &= \int_0^{2\pi} i r^{-n-m-1} e^{i \theta (m - n)} \ud \theta\\
555 | &= 2 \pi i r^{-n-m-1} \delta_{nm}\end{align*}
556 | 其中假设$C$是半径为$r$的圆环, $\delta$是Kronecker delta记号.
557 | \subsection{唯一性}
558 | Z变换不具有唯一/可逆性. 这一点可以从Laurent展开的非唯一性看出.
559 | 一般地, 有如下性质:
560 | \paragraph{Z变换级数展开}
561 | 如果$f(z)$的极点 (即复函数的奇点) 有$N$种不同的模 (即$N=|\{|z_p|\}|$) 则存在$N+1$个不同的信号, 使得它们的ZT都是$f(z)$.
562 | 若将其极点的模从小到大排成$\left\langle a_1, a_2\ldots a_N\right\rangle$,
563 | 则这些信号的ROC是$\langle r1=0, r2=a_1 \rangle,\;$$\langle r1=a_1, r2=a_2\rangle\ldots$
564 | $\langle r1=a_N, r2=\infty\rangle$.\par
565 | 例如, 对于$f(z) = \frac{1}{1- a^{-1}z}$, 显然$x(n) = a^n u(n)$和$y(n) = -a^n u(-n-1)$的ZT都是$f(z)$.
566 | 两者的收敛域分别是$\langle r1=a, r2=\infty\rangle$和$\langle r1=0, r2=a\rangle$
567 | \subsubsection{性质}
568 | \begin{center}
569 | \begin{tabularx}{\textwidth}{>{\bfseries}Sl SL}
570 | 线性性 & $\displaystyle \ZT[\alpha x + \beta y] = \alpha \ZT[x] + \beta \ZT[y]$\\
571 | 反褶 & $\displaystyle \ZT[x(-n)] = X(z^{-1})$\\
572 | 共轭 & $\displaystyle \ZT[x^*(n)] = X^*(z^*)$\\
573 | 压扩 & $\displaystyle \ZT\left[x(\frac{n}{a})\right](z) = \ZT[x](z^a)$, 其中如果$\frac{n}{a}$不是整数则$x(\frac{n}{a}) = 0$\\
574 | 时移 & $\displaystyle \ZT[x(n + m)] = \ZT[x]z^m$\\
575 | 频移 & $\displaystyle \ZT[a^n x(n)] = \ZT[x]\left(\frac{x}{a}\right)$\\
576 | 卷积 & $\displaystyle \ZT[x * y] = X Y,\qquad \ZT[x y] = \frac{1}{2 \pi i} \oint X(v) \,Y\!\left( \frac{z}{v} \right) v^{-1}\ud v $\\
577 | 初值 & $\displaystyle x(0) = \lim_{z \to \infty} X(z)$, 当然极限不存在时此式无效\\
578 | 终值 & $\displaystyle \lim_{n \to \infty} x(n) = \lim_{z \to 1} (z - 1) X(z)$, 当然极限不存在时此式无效\\
579 | \end{tabularx}
580 | \end{center}
581 |
582 | \begin{table}[ht!]
583 | \centering
584 | \begin{tabularx}{\textwidth}{|SCSCSC|}
585 | \Topline $\displaystyle x(n)$ & $\displaystyle X(z)$ & ROC(r1, r2) \\ \Midline
586 | $\displaystyle \delta(n) $ & $\displaystyle 1 $ & $\displaystyle [0, \infty]$ \\
587 | $\displaystyle u(n) $ & $\displaystyle \frac{1}{1 - z^{-1}}$ & $\displaystyle (1, \infty] $\\
588 | $\displaystyle G_N(n) $ & $\displaystyle \frac{1 - z^{-N}}{1 - z^{-1}} $ & $\displaystyle (0, \infty] $\\
589 | $\displaystyle a^n $ & 不存在 & 空 \\
590 | $\displaystyle a^nu(n) $ & $\displaystyle \frac{1}{1 - az^{-1}}$ & $\displaystyle (\,|a|, \infty] $\\
591 | %$\displaystyle $ & $\displaystyle $ & $\displaystyle $\\
592 | \Bottomline
593 | \end{tabularx}
594 | \caption{常见信号的Z变换}
595 | \end{table}
596 |
597 | \subsubsection{逆变换求解}
598 | \paragraph{数值方法} 如果$X(z)$是有理函数, 则可以通过多项式除法来求解. 可以依次求出$x(0), x(1) \ldots$.
599 | \paragraph{留数法} 根据留数定理, 可求得$x(n) = \sum_{z_p\text{是极点}} \Res\left[X(Z) z^{n - 1}, z_p\right]$.\
600 | 设$z_0$是$f(z)$的$n$级极点, 则$\Res[f(z), z_0] = \frac{1}{(n-1)!} \lim_{z \to z_0} \D^{n-1} \left( (z - z_0)^n f(z) \right)$.
601 | 可以一次得到对于一个$n$的$x(n)$.
602 | \paragraph{有理函数展开法} 类似生成函数, 直接求根展开有理函数, 可以一次得到所有$x(n)$.
603 | 基本原理是, 任何有理函数$R(z^{-1}) = \frac{P(z^{-1})}{Q(z^{-1})}$
604 | 可以写成一个多项式$S(z^{-1})$和若干$\frac{T(z^{-1})}{(1-z_pz^{-1})^l}$之和,
605 | 其中$T(z^{-1})$是多项式且$\deg T < l$, 并且$z_p$是$l$级极点.
606 |
607 | \subsection{系统函数}
608 | 记$X(z) = \ZT[x], Y(z) = \ZT[y]$, 则定义\[H(z) = \frac{Y(z)}{X(z)}\]称$H(z)$为系统的响应函数.\par
609 | \paragraph{性质} 频率响应基本相同, 另有\begin{description}
610 | \item[因果性] $H(z)$是因果系统的充要条件是其收敛域无界, 即$r2 = \infty$
611 | \item[不唯一性] 不同的系统可能有同样的系统函数, 但是ROC不同
612 | \item[稳定性] 某系统是稳定系统的充要条件是单位圆环在其系统函数的ROC内部.
613 | \end{description}
614 | \subsubsection{和差分方程的关系}
615 | 对于差分方程描述$\sum_{k = 0}^N a_k y(n - k) = \sum_{k = 0}^M b_k x(n - k)$, 两边做ZT即有
616 | \[
617 | H(z) = \frac { \sum_{k = 0}^M b_k z^{-k}} {\sum_{k = 0}^N a_k z^{-k}}
618 | \]
619 |
620 | \subsection{滤波器的设计}
621 | \subsubsection{FIR低通滤波器的设计}
622 | \paragraph{理想滤波器} 显然$\CTFT[\frac{\sin(t \omega_C)}{t \pi}] = G(\frac{\omega}{2\omega_C})$.
623 | 因此在数字频率上, $\frac{\sin(n \omega_C)}{n \pi}$是一个理想滤波器,
624 | 其中$\omega_C$是数字截止频率$\omega_C = 2\pi \frac{f_C}{f_S}$.
625 | \paragraph{设计步骤} 基本有如下几步 \begin{enumerate}
626 | \item 选择$\omega_C$, 为理想滤波器的数字截止频率
627 | \item 计算$h(n) = \frac{\sin(n \omega_C)}{n \pi}$
628 | \item 根据阻带衰减, 选择窗函数
629 | \item 查表计算得到窗长$N$ ($N$舍入到最近奇数, $N = 2k +1$)
630 | \item 根据$N$计算窗函数的表达式$w(n)$
631 | \item FIR表达式为$h(n)w(n),\quad |n| \le k$
632 | \item 平移FIR, 得到因果FIR为$h(n-k)w(n-k)$
633 | \end{enumerate}
634 | FIR滤波器必定是稳定的.
635 | \subsubsection{FIR其他滤波器}
636 | \paragraph{高通和带通} 将低通最终得到的$h_1(n) = h(n-k)w(n-k)$移位即可$h_2(n) = h_1(n) \cos(n\omega_0)$.\par
637 | 另外带通也可以通过低通且高通的方法实现.
638 | \paragraph{带阻} 带足就等于低通加高通.
639 | \subsubsection{IIR滤波器的实现}
640 | \paragraph{双线性变换} 双线性变换将$H(s)$变为$H(z)$, 式为\[
641 | s = 2 f_S \frac{z-1}{z+1}\]
642 | \paragraph{巴特沃斯滤波器}
643 | 对于一阶, 是$H(s) = \frac{\Omega_{p1}}{s + \Omega_{p1}}$, 其中$s = j \Omega$.
644 | 因此 $|H(\Omega)| = \frac{1}{\sqrt{\left(\frac{\Omega}{\Omega_{p1}}\right)^2 + 1}}$
645 | \paragraph{求解步骤}
646 | \begin{enumerate}
647 | \item 根据题目写出模拟边缘频率 $f_{p1},\;f_{s1}$.
648 | 其中$f_{p1}$是$-3 \;\textrm{dB}$的频率
649 | \item 计算阻带边缘增益, $\delta_S = 10^{x/20}$,
650 | 其中$f_{s1}$的增益是$x \;\textrm{dB}$
651 | \item 计算得到数字边缘频率 $\omega_{p1} = 2\pi \frac{f_{p1}}{f_S}$,
652 | 以及$\omega_{s1} = 2\pi \frac{f_{s1}}{f_S}$
653 | \item 用预扭曲方程求出滤波器的边缘频率
654 | $\Omega_{p1} = 2 f_S \tan \frac{\omega_{p1}}{2}$,
655 | 以及
656 | $\Omega_{s1} = 2 f_S \tan \frac{\omega_{s1}}{2}$
657 | \item 根据方程\[
658 | n \ge \frac
659 | { \lg\left(\delta_S^{-2} - 1\right) }
660 | { 2 \lg\left( \frac{\Omega_{s1}}{\Omega_{p1}}\right)} \]
661 | 求得IIR的阶数$n$
662 | \item 如果$n = 1$, 利用上述一阶公式计算$H(z)$
663 | \end{enumerate}
664 |
665 | \section{好题}
666 | \paragraph{CTFT} 不用暴力求$F(\omega)$, 其中$f(t) = \begin{cases} |\sin(\pi t)| & |t| < 1\\ 0 & \text{otherwise} \end{cases}$
667 |
668 |
669 | \pagebreak
670 | \section{好题解析}
671 | \paragraph{CTFT} 不用暴力求$F(\omega)$, 其中$f(t) = \begin{cases} |\sin(\pi t)| & |t| < 1\\ 0 & \text{otherwise} \end{cases}$\par
672 | 考虑$f(t) = \sin(\pi t) \cdot \left( G(t-0.5) + -G(-t+0.5) \right)$\par
673 | 答案是 $\left[\Sa\left(\frac{\omega - \pi}{2}\right) - \Sa\left(\frac{\omega + \pi}{2}\right)\right] \cos \frac{\omega}{2}$
674 |
675 | \section{其他技巧}
676 | \subsection{卷积求法}
677 | 序列线卷积可以撕纸来求. 注意项数要足够!\par
678 | 圆卷积的是先填零/回绕之后再求.\par
679 | 也可采用类似乘法的方法, 例如
680 |
681 | \begin{table}[ht!]
682 | \centering
683 | \begin{tabular}{*{7}{c}}
684 | x(6) & x(5) & x(4) & x(3) & x(2) & x(1) & x(0) \\
685 | \Midline
686 | & & & 1 & 2 & 3 & 4 \\
687 | & & $\times$ & 4 & 3 & 2 & 1 \\
688 | \Midline
689 | & & & 1 & 2 & 3 & 4 \\
690 | & & 2 & 4 & 6 & 8 & \\
691 | & 3 & 6 & 9 & 12 & & \\
692 | 4 & 8 & 12 & 16 & & & \\
693 | \Midline
694 | 4 & 11 & 20 & 30 & 20 & 11 & 4 \\
695 | \end{tabular}
696 | \caption{线卷积}
697 | \end{table}
698 |
699 | \begin{table}[ht!]
700 | \centering
701 | \begin{tabular}{*{7}{c}}
702 | x(6) & x(5) & x(4) & x(3) & x(2) & x(1) & x(0) \\
703 | \Midline
704 | & & & 1 & 2 & 3 & 4 \\
705 | & & $\times$ & 4 & 3 & 2 & 1 \\
706 | \Midline
707 | & & & 1 & 2 & 3 & 4 \\
708 | & & 2 & 4 & 6 & 8 & \\
709 | & 3 & 6 & 9 & 12 & & \\
710 | 4 & 8 & 12 & 16 & & & \\
711 | \Midline
712 | & & & 1 & 2 & 3 & 4 \\
713 | & & & 4 & 6 & 8 & \bfseries 2 \\
714 | & & & 9 & 12 & \bfseries 3 & \bfseries 6 \\
715 | & & & 16 & \bfseries 4 & \bfseries 8 & \bfseries 12 \\
716 | \Midline
717 | & & & 30 & 24 & 22 & 24 \\
718 | \end{tabular}
719 | \caption{圆卷积}
720 | \end{table}
721 |
722 | \end{document}
723 |
--------------------------------------------------------------------------------