( p/ ]# z# a$ g8 Z2 G
更有野心的是,交易所可以建立一个未经储户同意无法提取储户资金的系统。我们可以尝试探索「不作恶」有职业素养的 CEX 与「无法作恶」却泄漏隐私的低效链上 DEX 之间的界限。这篇文章将深入探讨让 CEX 更加去信任的历史尝试,与其采用技术的局限性,以及一些依赖 ZK-SNARKs 等先进技术的有力手段。
' L6 o; F1 i- l9 `0 W
余额表和 Merkle 树:传统的可偿付证明交易所试图用密码学来证明自己没有欺骗用户的最早尝试可以追溯到很久以前。2011 年,当时最大的比特币交易所 MtGox 通过发送一笔移动 424,242 个 BTC 到预先公布地址的交易来证明他们拥有该笔资金。2013 年,大家开始讨论如何解决该问题的另一面:证明用户存款的总规模。如果你证明用户的存款等于 X (负债证明 proof of liabilities),并证明拥有 X 个代币的私钥(资产证明 proof of assets),那么就提供了可偿付证明(proof of solvency):你证明了交易所有足够的资金偿还给储户。& {+ a# ^5 p+ b/ b- q
提供存款证明的最简单方法是公布一个列表。每个用户都可以检查他们在列表中的余额,而且任何人都可以检查完整的列表:(i)每项余额都是非负的;(ii)总额是宣称的金额。
当然,这会破坏隐私,所以我们可以稍微改变一下该方案:发布一个 列表,并私下给用户发送 salt 值。但即使这样也会泄漏余额与其分布。为了保护隐私,我们采用了后续技术:Merkle 树技术。/ I" K9 F0 \2 l+ o. P @, i1 m7 z7 m
![1672307050728799.jpg 1672307050728799.jpg](https://att.bitmere.com/forum/202212/30/163207ykzvztqv4huvu9u4.jpg)
2 f) y6 N$ U7 c% i8 \
绿色:Charlie 的节点。蓝色:Charlie 收到用于证明的节点。黄色:根节点,向所有人公布4 q. v2 a6 B# N7 ]9 B$ Z( n
Merkle 树技术会将用户余额表放进 Merkle 总和树。在 Merkle 总和树中,每个节点都是对。底层叶子节点表示各个用户的余额以及用户名的加盐哈希。在每个更高层的节点中,余额是下面两个节点余额的总和,而哈希是下面两个节点的哈希。Merkle 总和证明和 Merkle 证明一样,是一个由叶子节点到根节点路径上所有姐妹节点组成的「分支」。
首先,交易所会向每个用户发送一份其余额的 Merkle 总和证明。然后,用户能够确定其余额作为总额的一部分而被正确地包含。可以在这里找到简单的示例代码。: C5 @9 a# u1 T# B2 l0 E
6 U1 x" z0 x, r& C+ b
- # The function for computing a parent node given two child nodes' H' Q- Q: Y' M7 W6 n
- def combine_tree_nodes(L, R):
- L_hash, L_balance = L
- R_hash, R_balance = R
- assert L_balance >= 0 and R_balance >= 07 I( P( P" L' @
- new_node_hash = hash(% j5 F+ I; a d" h
- L_hash + L_balance.to_bytes(32, 'big') +, `) \8 S" F% Q0 G4 Q! ~7 h) [
- / f( S3 y7 K1 K; J) y/ R& T
- R_hash + R_balance.to_bytes(32, 'big')
- ); s! W6 V1 _4 _4 r. P
- 7 Y& K0 o7 U2 i
- return (new_node_hash, L_balance + R_balance)
- # Builds a full Merkle tree. Stored in flattened form where9 J, J3 `" Z9 j0 ^
- & E# H- y9 Y6 d9 {2 G6 e
- # node i is the parent of nodes 2i and 2i+1* s& ]% Q! v3 O! Z9 [3 a* Q
- def build_merkle_sum_tree(user_table: "List[(username, salt, balance)]"):" d( \6 ~& s6 _0 i; x
- 4 R, C8 @- P! t/ J9 [
- tree_size = get_next_power_of_2(len(user_table))
- 6 F0 T* G$ i& l( n8 n5 \$ g& z; P
- tree = (
- [None] * tree_size +
- ( D0 p+ Y/ \$ k6 E4 ]8 h
- [userdata_to_leaf(*user) for user in user_table] +
- T. p. B5 z" O. Q4 Y. z) g1 W
- [EMPTY_LEAF for _ in range(tree_size - len(user_table))]
- . j" Q a0 S7 e9 P
- )3 b _% k: | \( C
- for i in range(tree_size - 1, 0, -1):
- * a( z& w6 Z% L/ e1 N7 w
- tree = combine_tree_nodes(tree[i*2], tree[i*2+1])
- return tree5 Q0 i/ e8 `1 O4 G1 ]: W9 A2 A8 [- x
- # Root of a tree is stored at index 1 in the flattened form
- ' `' t% D# K$ h8 S
- def get_root(tree):
- return tree[1]8 D F- _8 z: N( K" O0 b O
- 3 p( P' R+ w* Y: z* ]5 B1 M8 |
- # Gets a proof for a node at a particular index$ y* Z7 ? V p9 B2 e
- # ~- u9 Z1 I* N: a `
- def get_proof(tree, index):
- ( g t5 U, O+ d5 P3 E+ x/ U" N. I
- branch_length = log2(len(tree)) - 1
- # ^ = bitwise xor, x ^ 1 = sister node of x
- . b7 q9 k: U7 @
- index_in_tree = index + len(tree) // 21 S( a1 w% S/ |% E, i, u7 {
- 6 \! `0 b8 z; D. e
- return [tree[(index_in_tree // 2**i) ^ 1] for i in range(branch_length)]- x9 R1 t+ T' E3 N7 V( l( O
- ) q2 b/ Y0 b* j2 ~- S4 u* `0 M
- # Verifies a proof (duh)$ k( H( _1 I9 L) ]1 F3 E1 f
- ' r1 G. [9 o, ^ w# u: `% b
- def verify_proof(username, salt, balance, index, user_table_size, root, proof):
- 2 f* x2 n4 O0 }8 V) k/ X8 l& ]9 p
- leaf = userdata_to_leaf(username, salt, balance)% F1 L/ ]5 Y* U2 y* i. F" O7 G5 B% Y
- + [$ w% s2 N" h: Q6 ~$ w. ~) |+ W
- branch_length = log2(get_next_power_of_2(user_table_size)) - 1. X6 F' r* \! Z) l0 V+ t# c# X( Q0 J3 O
- - X' j1 y6 E8 l! W
- for i in range(branch_length):
- if index & (2**i):6 S* `# h/ w* l1 t2 W3 P
- leaf = combine_tree_nodes(proof, leaf)
- else:
- 2 J, u! Y7 R. L4 P
- leaf = combine_tree_nodes(leaf, proof)# B5 n d0 j+ M) B* \! s& q
- $ H. Z/ }% y7 V. `, [7 @ N
- return leaf == root