Open Addressing 与 Cryptographic Hashing
Algorithms:Open Addressing and Cryptographic Hashing
本文逐段翻译 MIT 6.006 Fall 2011 Lecture 10《Open Addressing, Cryptographic Hashing》,介绍不使用 chaining 的 open addressing、linear probing、double hashing、uniform hashing 下的 expected probes,并进一步区分 cryptographic hash 的 one-way、collision-resistance 与 target collision-resistance。
TL;DR
- Open addressing:所有 items 都直接存放在 table 内,每个 slot 最多一个 item,因此必须有
m ≥ n。 - Probe sequence:hash function 不再只返回一个 slot,而是为每个 key 指定一个覆盖 table slots 的 probe sequence。
- Deletion:不能简单把已删除位置设为 empty,否则会截断后续 search;需要使用
DeleteMetombstone。 - Probing:linear probing 简单但会产生 primary clustering;double hashing 用两个 hash functions 生成更分散的 sequence。
- Expected cost:在 uniform hashing assumption 下,load factor
α=n/m<1时,下一次 operation 的 expected probes 不超过1/(1-α)。 - Cryptographic hash:用于 password storage、file modification detection 和 digital signatures;核心性质包括 OW、CR 和 TCR,它们并不等价。
目录
| 章节 | 说明 |
|---|---|
| Lecture 10:Hashing III:Open Addressing | 从 chaining 转向 open addressing |
| Open addressing | probe sequence 与基本 operations |
| Deletion 与 tombstone | 为什么不能直接清空 slot |
| Probing strategies | linear probing 与 double hashing |
| Uniform hashing 与分析 | 1/(1-α) expected bound |
| Open addressing 与 chaining 的比较 | memory、cache 与 load factor |
| Cryptographic hashing | OW、CR、TCR 的定义 |
| Cryptographic hash 的应用 | password、file、signature |
| 小结 | 本讲要点 |
Lecture 10:Hashing III:Open Addressing
上一讲的 chaining 为每个 table slot 保存一个 linked list。本讲介绍另一种处理 collision 的方法:open addressing。
open addressing 不在 table 外部建立 chains,而是把所有 items 都存放在 table 本身:
T[0] T[1] T[2] ... T[m-1]
item₁ empty item₂ item₃
因为每个 slot 最多保存一个 item,所以必须满足:
m ≥ n
open addressing 的 hash function 不只回答“这个 key 的首选 slot 是什么”,还要回答“发生 collision 后接下来应该尝试哪些 slots”。
Open addressing
令 U 是 key universe,m 是 table size。open addressing 使用如下形式的 hash function:
h: U × {0, 1, ..., m-1} → {0, 1, ..., m-1}
其中第二个参数 i 是 trial count。对于每个 key k,probe sequence 是:
⟨h(k,0), h(k,1), ..., h(k,m-1)⟩
要求这组 slots 是 0..m-1 的一个 permutation。也就是说,如果不断增加 i,最终会访问 table 的每一个 slot。
Insert
插入 (k,v) 时,按 probe sequence 依次查找第一个 empty slot:
Insert(T, k, v):
for i in range(m):
j ← h(k, i)
if T[j] is EMPTY:
T[j] ← (k, v)
return
raise FULL
如果 table 未满,因为 probe sequence 会遍历所有 slots,最终一定能找到 empty slot。
Search
search 同样沿着 key 对应的 probe sequence 前进:
Search(T, k):
for i in range(m):
j ← h(k, i)
if T[j] is EMPTY:
return NOT-FOUND
if key(T[j]) = k:
return T[j]
return NOT-FOUND
为什么遇到 empty slot 就可以返回失败?因为如果 k 曾经插入过,它会被放在 probe sequence 中第一个当时可用的位置;search 不应该越过一个从未被占用过的位置。
Deletion 与 tombstone
open addressing 的 deletion 比 chaining 更微妙。不能找到 item 后直接执行:
T[h(k,i)] ← EMPTY
例如,假设 586 先占据某个 slot,496 因 collision 被放到它之后的 probe slot:
probe sequence: ... → slot 3 → slot 4 → slot 5 → ...
... 586 496
如果删除 586 并把 slot 3 设为 empty,那么 search 496 时会在 slot 3 停止,并错误地报告 496 不存在。
DeleteMe marker
解决方案是用特殊的 tombstone 标记,例如 DeleteMe:
EMPTY:search 可以停止;insert 可以使用
DeleteMe:search 必须继续;insert 可以使用
occupied item:search 比较 key;insert 继续 probing
删除时:
Delete(T, k):
找到 key 为 k 的 slot
将该 slot 设置为 DeleteMe
search 不把 DeleteMe 当作 chain 的终点;insert 则可以把它视为空闲位置并复用。实际实现还需要控制 tombstones 的数量,否则大量 delete 会让 probe sequence 变长,通常需要周期性 rebuild / rehash。
Probing strategies
不同的 open-addressing hash functions,主要区别在于如何生成 probe sequence。
Linear probing
linear probing 使用一个普通 hash function h'(k) 作为起点,然后按顺序检查后续 slots:
h(k, i) = (h'(k) + i) mod m
它很像在一条已经停满车的街道上继续向后寻找停车位:
起点 → 下一个 slot → 下一个 slot → ...
优点是实现简单、memory access 连续,通常有不错的 cache performance。但它会产生 primary clustering:连续的 occupied slots 会形成 cluster;任何 probe sequence 一旦进入这个 cluster,就可能继续扩大它。
cluster 越长,新的 key 越容易遇到它;而 cluster 变长又使更多 keys 进入它,形成正反馈。在常见的 0.01 < α < 0.99 范围内,可以证明 cluster size 可能达到 Θ(log n)。
Double hashing
double hashing 使用两个普通 hash functions:
h(k, i) = (h₁(k) + i · h₂(k)) mod m
h₁(k) 决定起点,h₂(k) 决定每次 probe 的 step size。不同 keys 通常拥有不同 step size,因此比 linear probing 更能避免 primary clustering。
为了保证 probe sequence 真正访问所有 slots,需要 h₂(k) 与 m relatively prime。证明如下:如果两个不同 trial i 和 j 访问了同一个 slot,则:
h₁(k) + i·h₂(k) ≡ h₁(k) + j·h₂(k) (mod m)
因此:
m | (i-j)·h₂(k)
如果 gcd(h₂(k),m)=1,就只能有 m | (i-j);在 0 ≤ i,j < m 的范围内,这意味着 i=j,不会提前重复 slot。
一个方便的特例是 m = 2^r,此时让 h₂(k) 始终为 odd,就能保证它与 m relatively prime。
Uniform hashing assumption
理想的 uniform hashing assumption 是:每个 key 都等概率地产生 m! 种 probe permutations 中的任意一种。这个 assumption 在实际中并不严格成立,但 double hashing 可以接近它。
它比上一讲的 simple uniform hashing 更强:simple uniform hashing 只把每个 key 随机分配到一个 slot;uniform hashing 则要求整个 probe sequence 近似随机 permutation。
Uniform hashing 与分析
假设 table size 是 m,已经插入了 n 个 items,load factor 为:
α = n / m < 1
在 uniform hashing assumption 下,下一次 operation 的 expected probe 数量不超过:
1 / (1 - α)
例如:
α = 90% → expected probes ≤ 1/(1-0.9) = 10
证明
考虑插入一个不在 table 中的 key k。当前有 m-n 个 good slots(empty 或可使用位置),所以第一次 probe 成功的概率是:
p = (m-n)/m = 1-α
如果第一次失败,那么已经发现了一个 occupied slot;剩下 m-1 个 slots,其中仍有 m-n 个 good slots。因此第二次成功的概率是:
(m-n)/(m-1) ≥ (m-n)/m = p
同理,如果前两次都失败,第三次成功的概率是:
(m-n)/(m-2) ≥ p
之后每一次 trial 的成功概率都至少为 p。因此 probe 次数被 geometric distribution dominated,其 expected value 不超过:
1/p = 1/(1-α)
对于 search 和 delete,也有同样数量级的 expected bound;如果尝试插入一个已经存在的 item,也必须找到它,所以分析相同:
expected search/delete time = O(1/(1-α))
这解释了为什么 open addressing 不能让 α 达到或超过 1,也解释了为什么实际实现通常在 table 达到约 70%–80% occupancy 时就进行 resize。
Open addressing 与 chaining 的比较
两种 collision-handling strategies 各有取舍:
| 维度 | Open addressing | Chaining |
|---|---|---|
| 存储位置 | 所有 items 在 table 内 | items 在每个 slot 的 chain 中 |
| slot 限制 | 每个 slot 一个 item,必须 m ≥ n | slot 可以连接多个 items |
| 额外 pointers | 不需要 | linked list 通常需要 |
| cache performance | 通常较好,访问更连续 | 可能较差,pointer chasing 多 |
| 对 hash function 的敏感度 | 较高,clustering 会严重影响性能 | 相对较低 |
| 对 load factor 的容忍度 | α 接近 1 时迅速退化 | α 可以大于 1,但 chain 变长 |
| worst-case | Θ(n) | Θ(n) |
open addressing 的优势是 memory usage 更紧凑、没有外部 linked-list pointers,cache behavior 通常更好;代价是必须精心选择 probing strategy,并严格控制 load factor。chaining 更容易处理高 load factor,也更不容易因 probing sequence 产生 clustering。
Cryptographic hashing
普通 hash table 的 hash function 主要追求把 keys 分散到 slots;cryptographic hash function 的用途不同。
cryptographic hash 是一个 deterministic procedure:它接收任意长度的 data block,返回 fixed-size bit string。输入 data 常称为 message,输出 hash value 有时称为 message digest 或简称 digest。
理想的 cryptographic hash function 应使 data 的 accidental 或 intentional change 导致 hash value 改变。设 hash output 有 d bits,则输出空间为:
{0, 1}^d
讲义指出,实际 cryptographic hash 的 output 通常至少有 160 bits;它们当然也可以用于 hash-table indexing,但更常用于 computer security。
Cryptographic hash 的性质
One-way(OW)
给定一个随机的 d-bit output y,应当 infeasible 找到任意 input x,使得:
h(x) = y
这称为 one-way property,也就是 hash function 很难被 inversion。注意:hash function 可以很快计算 h(x),但从 output 反向寻找 input 应该非常困难。
Collision-resistance(CR)
应当 infeasible 找到两个不同的 inputs x 和 x',使得:
x ≠ x' 且 h(x) = h(x')
这就是 collision。因为 input space 通常比 fixed-size output space 大,collision 在数学上必然存在;CR 的含义不是“不存在 collision”,而是对攻击者而言难以找到 collision。
Target collision-resistance(TCR)
给定一个特定的 input x,应当 infeasible 找到另一个不同的 x',使得:
x' ≠ x 且 h(x') = h(x)
这也称 second-preimage resistance 的目标形式。TCR 比 CR 弱:如果 hash function 满足 CR,就自动满足 TCR;但 TCR 本身不保证攻击者无法从头寻找任意一对 collision。
OW 与 CR/TCR 之间没有简单的 implication relationship。它们描述的是不同的攻击任务:反转一个 digest、任意寻找 collision,以及针对给定 message 寻找另一个 collision message。
Cryptographic hash 的应用
Password storage
不要把 plaintext password PW 直接存储在 computer 上,而是存储:
h(PW)
用户输入 PW' 后,系统计算 h(PW'),再与保存的 h(PW) 比较:
compute h(PW')
compare with stored h(PW)
这个场景主要需要 OW:攻击者拿到 digest 后不应容易恢复 password。讲义也指出,少量 hash collisions 本身不一定直接破坏 password storage,但现实系统还必须考虑 password guessing、salt、slow password KDF 等工程问题。
File modification detector
对 file F 计算 hash,并将 h(F) 安全保存:
securely store h(F)
之后重新计算 h(F) 并比较
如果攻击者能够修改 F,但又找到另一个 F' ≠ F 使 h(F')=h(F),就能绕过 detector。因此这里需要的是 TCR:给定原始 file,很难构造一个 hash 相同的替代 file。
Digital signatures
在 public-key cryptography 中,Alice 有 public key PK_A 和 private key SK_A。她可以使用 private key 对 message M 生成 signature:
σ = sign(SK_A, M)
其他人使用 Alice 的 public key 验证:
verify(M, σ, PK_A) = True
对于 large message,通常签署 h(M) 而不是整个 M:
σ = sign(SK_A, h(M))
这里需要 collision-resistance。如果攻击者能找到 M' ≠ M 且 h(M')=h(M),就可以让 Alice 签署 M,之后把 signature 声称为对 M' 的签名。
Implementations 与历史背景
历史上提出过许多满足 OW、CR 和 TCR 的 cryptographic hash functions,但其中一些后来被攻破。例如,讲义指出 MD5 已被证明不再满足 collision-resistance;当时 SHA-3 competition 正在进行,以确定由 NIST 认证的 Secure Hash Algorithm。
cryptographic hash functions 比普通 hash-table functions 复杂得多。可以把它们粗略理解为:反复运行 regular hash-like transformations,并在中间加入 pseudo-random permutations,使输入的微小变化扩散到整个 output。
这里的历史叙述属于 2011 年课程背景;选择现代安全算法时,还需要依据当前密码学标准和安全工程建议,不能把课程讲义中的历史状态直接当作今天的算法推荐。
复杂度与性质总结
| 主题 | 关键结论 |
|---|---|
| Open addressing space | 所有 items 在 table 中,m ≥ n |
| Uniform hashing expected probes | O(1/(1-α)) |
| Linear probing | 简单、cache-friendly,但有 primary clustering |
| Double hashing | 用第二个 hash function 生成 step,减少 clustering |
| Chaining | 对高 load factor 更宽容,但有 pointers 和 cache cost |
| OW | 给定 digest,难以找到 preimage |
| CR | 难以找到任意两个不同 inputs 的 collision |
| TCR | 给定 input,难以找到另一个 collision input |
小结
open addressing 将 collision resolution 完全放在 table 内,通过 probe sequence 找到 empty slot 或目标 key。probe sequence 必须覆盖所有 slots;linear probing 会造成 primary clustering,double hashing 通过两个 hash functions 改善分布。uniform hashing assumption 下,load factor 为 α 时,expected probes 是 O(1/(1-α)),因此 open addressing 必须严格控制 α。
cryptographic hash 的目标不是单纯减少 table collisions,而是提供 security properties:OW 防止从 digest 反推出 input,CR 防止任意寻找两个 collision inputs,TCR 防止针对给定 input 构造 collision。password storage、file integrity 和 digital signatures 需要的性质不同,不能混为一谈。
参考资料
评论 (0)