← COMP5565 全部讲次
Q05

期中复习题|E 简答与 X 选做补练

英文题干,逐题折叠中文解析、英文参考答案与适用条件。

手机上宽表格可左右滑动;自测答案默认收起,点击题目下方按钮查看。

使用边界:2026-10-13 期中复习,非官方材料;正式范围、题型、时长和开闭卷要求以讲师通知为准。保留课件版本、适用条件与补充标注。

复习资料:密码学、账户与 Gas · 共识、钱包与代币 · Solidity、调用与代理 · DApp 签名与英文作答

先做题再看解析:A 判断题 · B 单选题 · C 计算题 · D 代码推演 · E 简答与 X 选做补练

统一约定:除题目另说,Solidity 为 0.8.30、默认检查算术溢出;账户按普通 EOA 发起交易的课堂模型,不讨论账户代码委托。代码是隔离的教学片段,不是生产合约;假设有足够 gas,且不存在题面未给出的外部操作。金额计算忽略题面之外的费用。页码回查一律使用 PDF 页序。

自查分值仅供练习,不代表教师评分标准;先作答,再展开每题答案。

来源代号:C01 = Lecture crypto foundation.pdf;L01 = Lecture1.pdf;L02 = Lecture2.pdf;L03 = Lecture3.pdf;L04a = Lecture 4.0.pdf;L04b = Lecture 4.1.pdf;L05 = Lecture 5.pdf。页码都是 PDF 页序。来源为对应逐讲注解;标“补充”的部分另列标准文档。所有题干均为自拟。

E · 简答题

每题 6 分。Answer each in 4-6 concise English sentences. Conditions matter more than memorized slogans.

E1 · Upgradeability without losing state · 6 marks

A team says: “We can upgrade a proxy by changing its implementation address, so any new Solidity contract will work and no new trust is introduced.”

Explain (a) where persistent application state lives, (b) why V2 cannot freely reorder V1’s state variables, and (c) two checks or controls needed around upgrades. Include the words storage layout, authorization, and initialization.

E1 · 查看答案与解析

E1 · Upgradeability

推理过程:按三个问题组织短答:① 旧余额放在哪里?在代理合约的存储里。② 新代码怎样读它?按槽号、偏移和类型解释,因此布局必须兼容;换实现地址不自动搬数据。③ 谁能换代码、初始状态谁来设?分别回答升级授权与代理初始化。由这条因果链可见,“任意新合约都能用”和“没有新增信任”都站不住。

Suggested answer: Persistent application state lives in the proxy, while the implementation provides code through delegatecall. V2 must preserve a compatible storage layout or it may interpret old bytes as different variables. Changing an implementation address does not migrate or repair storage automatically. Upgrade authorization must be restricted and protected because an authorized upgrade can change application behavior. Initialization must set the proxy’s state correctly and prevent unauthorized initialization or reinitialization; an implementation constructor does not initialize proxy storage.

中文直觉:换的是“解释同一份存储的代码”,不是搬家后自动整理好所有旧数据。只换地址,旧槽位还在。初始化和升级权限本身就是信任与攻击面。

自查分值:状态在 proxy、逻辑在 implementation 共 2;兼容布局及错误解释后果共 2;升级授权 1;proxy 初始化/防重初始化 1。可补充 timelock、多签等控制,但不要求某个工具名,也不能写成“有多签就绝对安全”。回查 L05 p.3-15、p.18-23。初始化属于补充的实现边界,依据 OpenZeppelin 升级注意事项,不声称这是老师另划的范围。

E2 · Signed authorization and replay · 6 marks

A mint service signs (recipient, amount, nonce, chainId). The contract verifies the signature against the designated signer but neither checks nor records consumed nonces. It also trusts the chainId supplied by the caller. The same signer is trusted by deployments on two chains.

Explain why a valid signature alone does not make the authorization single-use. Describe how to bind the authorization to the current chain and intended contract, how to check and consume a nonce, and why merely switching to EIP-712 is insufficient. Assume the signature binds every field shown and the cryptographic verification itself is correct.

E2 · 查看答案与解析

E2 · Authorization is not freshness

推理过程:先模拟两种误用:同一链重复提交旧授权,验签仍可能通过;换到另一链,若仍信调用者填的 chainId,也可能重建同一验签内容。修复分别对应两个缺口:核验并消费应用授权编号,防再次使用;用实际链标识与预期合约地址绑定签名,限制生效范围。两者都保留,不能用交易序号代替业务授权编号。

Suggested answer: A valid signature proves approval of the signed message, not that it has never been used. The contract must check the signed nonce against application state and consume it as part of successful authorization processing, before unsafe external interactions. It should bind the signed data to the actual chain and intended verifying contract, using trusted context rather than a caller-supplied chain ID. EIP-712 can provide structured signing and domain separation, but the application still needs replay-prevention logic. If the transaction reverts, its nonce-state change reverts too; a successfully consumed nonce must not be usable again.

中文直觉:签名是授权书,nonce 状态是使用记录,domain 是授权生效的场所。缺哪一层,就可能把“签过”误当成“现在可以再做一次”。普通重放:同一签名重复调用仍通过;跨链重放:另一链信同一 signer、重建相同消息且没有正确域绑定时,也可能通过。

自查分值:普通重放成因 1;核对 nonce 1;成功路径消费且注意重入/回滚 1;本链 chainId 1;verifying contract 绑定 1;EIP-712 本身不自动防重放 1。回查 L05 p.60-64。不是必须递增 nonce:已用授权 ID 集合等设计也可能成立,但必须正确检查、写入并覆盖预期重放域。

X · Optional boundary drills

补充·自拟小题,选做且不计入 100 分。不是额外公布的考试范围。

X1 · BIP32

Given a parent extended public key, a non-hardened child index, and the corresponding child private key, what additional secret may be recovered? Explain why a hardened boundary changes this situation. For the scalar-arithmetic step only, use the deliberately insecure toy values n=19, I_L=15, k_i=3 and k_i=(k_par+I_L) mod n.

X1 · 查看答案与解析

X1 · 补充,BIP32。 父扩展公钥给出父公钥和 chain code;知道普通子索引时可算派生偏移 I_L。再泄露对应子私钥,就可由 k_par=(k_i-I_L) mod n 恢复父私钥。玩具数值 (3-15) mod 19=7。这是教学模运算,不是实际 secp256k1 参数。硬化派生的 HMAC 输入依赖父私钥,父 xpub 无法完成同样的偏移计算;但硬化不意味着泄露 seed 也安全。L03 p.45-48、p.55;BIP32 安全说明。

推理过程:把式子当作“父私钥加偏移后取模”:已知子私钥 3 与偏移 15,就反向相减得到 -12;加一个模数 19,落回范围得到 7。先能从父 xpub 算出这个偏移,减法才有用;硬化派生恰好让只持有父 xpub 的人缺少计算偏移所需的私钥材料。

X2 · UTXO

A transaction consumes one UTXO worth 15 units and creates outputs of 10 to Bob and 3 as Alice’s change. What is its fee? Why can the same spent output not be spent again on the same accepted ledger history? Does this by itself eliminate replay on a separately forked chain?

X2 · 查看答案与解析

X2 · 基础回顾。 手续费 15-10-3=2。输入引用具体输出,在同一被接受的账本历史中,该输出花掉后不再属于 UTXO 集,不能再次合法花费。这不自动提供独立分叉链之间的重放隔离;若两链共享该输入及验证规则,同一签名交易仍可能在两链各生效。L01 p.44-53;跨链部分为补充条件。

推理过程:先做金额守恒:旧输出 15 被整笔消耗,新输出合计 10 + 3 = 13,差额 2 是费用。再查花费资格:同一历史里旧输出已被消耗,不能再花。分叉链却各有自己的状态;在题设共享输入与规则的情况下,一条链花过不等于另一条链也记为花过。

X3 · Memory vs storage

A state array initially contains [1, 2]. Inside a function, uint[] storage s = values; s[0] = 7; uint[] memory m = values; m[1] = 9;. What remains in values after success? What changes if the enclosing transaction reverts?

X3 · 查看答案与解析

X3 · [7, 2]。 s 是 storage 别名,s[0] = 7 修改持久数组;m 是 memory 副本,m[1] = 9 不写回 storage。若整个交易回滚,则恢复交易前 [1, 2]。L04b p.17;不要把“变量名不同”当成一定复制。

推理过程:① s 与 values 指向同一份持久数据,写 s[0] 后原件变成 [7, 2]。② m 此时复制 [7, 2],改 m[1] 后只有副本变成 [7, 9]。③ 成功时原件保留 [7, 2];整笔回滚时撤销第①步,恢复交易前 [1, 2]。“不同变量名”不保证是不同数据。

X4 · State-trie keys

An Ethereum address is 20 bytes. In the hash-keyed account state trie, the lookup path is based on keccak256(address). How many bytes and hexadecimal nibbles does that key have? Does this mean that every proof must contain that many nodes?

X4 · 查看答案与解析

X4 · 补充,32 bytes / 64 nibbles。 地址自身为 20 bytes / 40 nibbles;题定 secure state trie 先哈希地址,路径键为 256 bits。压缩路径、节点结构与编码会影响证明节点数,不能把 64 个 nibble 写成“每个证明必有 64 个节点”。L02 p.30-33 的示意与 Ethereum MPT 文档 对照。原始地址长度与实际 trie key 长度不是同一层。

推理过程:先判断题目问地址还是查询键:地址是 20 字节,但题设先哈希,所以查询键看哈希输出的 256 位。256 ÷ 8 = 32 字节;一个 nibble(半字节)是 4 位,256 ÷ 4 = 64。最后区分键长与证明节点数:路径可压缩,不能按一位必占一个节点来数。

X5 · Commit-reveal

Why add a high-entropy secret salt to a three-choice game commitment? What problem remains if the losing player refuses to reveal? Distinguish this secret salt from an account transaction nonce.

X5 · 查看答案与解析

X5 · 随机性、约束与退出规则。 三种 choice 可枚举;加入足够高熵、保密的随机 salt 扩大搜索空间。应让承诺绑定参与者、轮次及选择,并明确承诺/揭示截止与违约处理。输家仍可能拒绝揭示,所以需押金、超时或其他明确的终止/结算规则;不能声称哈希强迫其公开。这里 secret salt 用来隐藏选择,账户 transaction nonce 是顺序/防重放计数,不应拿公开递增计数器代替秘密随机盐。L03 p.12-14;参与者/轮次绑定为设计补充。

推理过程:按攻击顺序思考:公布三个候选之一的哈希,旁观者容易枚举;加上揭示前保密、足够难猜的盐值后,候选组合才扩大。到揭示阶段,输家仍可以不发送揭示消息,哈希无法替他执行这个动作,所以还要约定超时与结算。交易序号是公开的顺序信息,不能承担秘密随机量的隐藏作用。

X6 · Gas failures

Distinguish an ordinary transaction whose gas limit is below intrinsic gas, an included top-level out-of-gas execution, and an included execution ending in REVERT. Are all three “a valid transaction that spends all its gas”?

X6 · 查看答案与解析

X6 · 三者不同。 低于 intrinsic gas:交易不满足纳入有效块的要求,不是先正常执行再失败;网络接收/拒绝广播不是链上执行。顶层 out of gas:入块执行后异常终止,状态效果回滚,该执行可用 gas 被耗尽。REVERT:失败帧效果回滚,未耗尽的 gas 可以保留;顶层若失败,已消耗部分仍收费。低级子调用的 gas 边界和外层是否继续另看调用代码,不能把它当顶层 OOG。L02 p.43、p.57;L04b p.55-59。

推理过程:按时间顺序分类最清楚:① 执行前基础 gas 要求都没满足,不能有效入块;② 已入块开始执行,后来 gas 用尽,是顶层 out of gas;③ 已入块但代码执行 REVERT,撤销效果,尚未消耗的 gas 可保留。后两者用过的 gas 都收费,但只有第二种在题设顶层边界耗尽可用 gas。

作答后怎么复盘

先记录错题号,再分成 概念 / 英文限定词 / 算术与单位 / 调用帧与状态 / 版本与条件 五类。展开对应题目的解析看错因,遮住答案后换一组数字或条件再答一次。不要只背 T/F 和选项字母。

下一页 →
Q06 期中模拟卷 B|单选题 1-8

个人整理的学习笔记,不是官方材料;数字与结论以课件和讲师为准。