多方哈希谜题 (Multiparty Hash Puzzles)¶
在哈希谜题合约中,花费者必须提供一个原像 x
,它散列到一个预定义的值 y
来解锁一个 UTXO。它可以扩展到多方,因此必须提供多个原像,使得 y1 = H(x1)
, y2 = H(x2)
, …, yN = H(xN) )
[1].下面显示了一个三方的例子。
contract MultiPartyHashPuzzles {
Sha256 hash1; // hash1 = b'136523B9FEA2B7321817B28E254A81A683D319D715CEE2360D051360A272DD4C'
Sha256 hash2; // hash2 = b'E222E30CF5C982E5F6251D755B0B16F608ACE631EB3BA9BDAF624FF1651ABF98'
Sha256 hash3; // hash3 = b'2A79F5D9F8B3770A59F91E0E9B4C379F7C7A32353AA6450065E43A8616EF5722'
// preimage1: e.g., "bsv" -> b'627376'
// preimage2: e.g., "sCrypt" -> b'734372797074'
// preimage3: e.g., "IDE" -> b'494445'
public function unlock(bytes preimage1, bytes preimage2, bytes preimage3) {
require(sha256(preimage1) == this.hash1);
require(sha256(preimage2) == this.hash2);
require(sha256(preimage3) == this.hash3);
}
}
当 N 很大时,上述解决方案是有问题的,因为所有的 N 哈希都必须包含在锁定脚本中,从而使交易膨胀。相反,我们可以将所有 y
组合成一个 y,使得 y = H(H(y1 || y2) || y3)
[2] 如下所示。
contract MultiPartyHashPuzzlesCompact {
// only 1 hash needs to go into the locking script, saving space
Sha256 combinedHash; // combinedHash = b'C9392767AB23CEFF09D207B9223C0C26F01A7F81F8C187A821A4266F8020064D'
// preimage1: e.g., "bsv" -> b'627376'
// preimage2: e.g., "sCrypt" -> b'734372797074'
// preimage3: e.g., "IDE" -> b'494445'
public function unlock(bytes preimage1, bytes preimage2, bytes preimage3) {
Sha256 hash1 = sha256(preimage1);
Sha256 hash2 = sha256(preimage2);
Sha256 hash3 = sha256(preimage3);
Sha256 hash12 = sha256(hash1 + hash2);
Sha256 hash123 = sha256(hash12 + hash3);
require(hash123 == this.combinedHash);
}
}
[1] | H 是一个哈希函数。在线哈希计算器 |
[2] | || 表示串联。 |