Taproot 隱私優勢

分析 Taproot 升級如何提升比特幣交易隱私。

Taproot 隱私應用深度技術分析:從密碼學原理到實際部署

概述

Taproot 是比特幣於 2021 年 11 月激活的重大升級,包含了 BIP-340(Schnorr 簽章)、BIP-341(Taproot)和 BIP-342(Tapscript)三個核心提案。這次升級不僅提升了比特幣的可擴展性和腳本靈活性,還帶來了顯著的隱私改進。Taproot 使得複雜交易與簡單交易在外觀上無法區分,從根本上改變了區塊鏈分析的假設。本指南將深入分析 Taproot 的密碼學原理、隱私改進機制,並提供完整的 Python 程式碼範例。

密碼學基礎

橢圓曲線密碼學回顧

比特幣使用 secp256k1 橢圓曲線,其定義如下:

y² = x³ + 7 (mod p)

其中:

在 secp256k1 上:

Schnorr 簽章數學原理

Schnorr 簽章的標準化定義於 BIP-340。與 ECDSA 相比,Schnorr 簽章具有以下特點:

簽章生成

R = k × G  (k 是隨機nonce)
e = hash(R || P || m)  (P 是公鑰,m 是消息)
s = k + e × d  (d 是私鑰)
簽章 = (s, e)

驗證

s × G = R + e × P
e == hash(R || P || m) ?

Schnorr 簽章的關鍵特性是線性疊加性

假設有兩個私鑰 d₁ 和 d₂:

Q₁ = d₁ × G
Q₂ = d₂ × G
聚合公鑰 Q = Q₁ + Q₂ = (d₁ + d₂) × G

當雙方各自對消息 m 簽名時:

s₁ = k₁ + e × d₁
s₂ = k₂ + e × d₂
聚合簽章 s = s₁ + s₂ = (k₁ + k₂) + e × (d₁ + d₂)

驗證方只需驗證:

s × G == (k₁ + k₂) × G + e × (d₁ + d₂) × G

MuSig2 多簽名聚合協議

MuSig2 是比特幣用於實現多簽名聚合的協議,由 Blockstream 開發。其核心思想是允許 n 個簽名者共同生成一個聚合公鑰和一個聚合簽章。

協定流程

第一輪(準備階段):

對每個簽名者 i:
1. 生成隨機種子 θᵢ
2. 對每個參與者 j,計算:
   - tᵢⱼ = hash(θᵢ || j)
   - Rᵢⱼ = tᵢⱼ × G
3. 發送 {θᵢ} 給其他參與者(通過安全通道)

第二輪(簽名階段):

1. 每個簽名者計算:
   - R = Σᵢ Σⱼ Rᵢⱼ
   - e = hash(R || Q || m)
   - sᵢ = kᵢ + e × dᵢ  (kᵢ 從 θᵢ 派生)

2. 聚合簽章:s = Σᵢ sᵢ

3. 最终簽章:(s, e)

以下是 Python 實現的簡化版本:

import hashlib
import secrets

# 模擬 secp256k1 運算(實際使用 secp256k1-py 庫)
class Point:
    def __init__(self, x, y):
        self.x = x
        self.y = y
    
    def __add__(self, other):
        # 橢圓曲線點加法
        if self.x == other.x:
            if self.y == other.y:
                # 倍點
                lam = (3 * self.x * self.x) * pow(2 * self.y, -1, p)
            else:
                return Point(0, 0)  # 無窮遠點
        else:
            lam = (other.y - self.y) * pow(other.x - self.x, -1, p)
        x = (lam * lam - self.x - other.x) % p
        y = (lam * (self.x - x) - self.y) % p
        return Point(x, y)
    
    def __mul__(self, k):
        result = Point(0, 0)
        addend = self
        while k:
            if k & 1:
                result = result + addend
            addend = addend + addend
            k >>= 1
        return result

# BIP-340 指定的 hash function
def bip340_tagged_hash(tag, msg):
    tag_hash = hashlib.sha256(tag.encode()).digest()
    return hashlib.sha256(tag_hash + tag_hash + msg).digest()

def SchnorrSign(m, d):
    """Schnorr 簽章生成"""
    # d 是私鑰(作為整數)
    P = d * G  # 公鑰
    d0 = d if d < p - d else p - d  # 確保小端序
    
    # 生成 nonce
    k = int.from_bytes(
        bip340_tagged_hash("BIP 340 test", bytes([d0]) + m),
        'little'
    ) % n
    if k == 0:
        raise ValueError("Invalid nonce")
    
    R = k * G
    # BIP-340 要求 R 的 y 座標為偶數
    if R.y % 2 != 0:
        k = n - k
        R = k * G
    
    # 計算 e
    e = int.from_bytes(
        bip340_tagged_hash("BIP 340 e", R.x.to_bytes(32, 'little') + P.x.to_bytes(32, 'little') + m),
        'little'
    ) % n
    
    # 計算 s
    s = (k + e * d) % n
    
    return (s, R.x)  # BIP-340 簽章格式

def SchnorrVerify(m, P, sig):
    """Schnorr 簽章驗證"""
    s, Rx = sig
    R = Point(Rx, y_from_x(Rx))  # 需要從 x 推導 y 或從簽章獲取
    
    e = int.from_bytes(
        bip340_tagged_hash("BIP 340 e", Rx.to_bytes(32, 'little') + P.x.to_bytes(32, 'little') + m),
        'little'
    ) % n
    
    # 驗證 s*G == R + e*P
    return s * G == R + e * P

Taproot 隱私改進原理

傳統腳本的指紋識別問題

在 Taproot 之前,比特幣腳本會在區塊鏈上留下明顯的指紋:

P2PKH(Pay to Public Key Hash)

OP_DUP OP_HASH160 <pubKeyHash> OP_EQUALVERIFY OP_CHECKSIG

區塊鏈分析公司可以識別:

P2SH(Pay to Script Hash)

OP_HASH160 <20-byte-hash> OP_EQUAL

分析師可以:

P2WSH(Pay to Witness Script Hash)

<witness program>: 32 bytes
 witness: <script>

揭示的問題:

Taproot 的解決方案:MAST 結構

Taproot 引入 Merkelized Abstract Syntax Tree(MAST),將腳本結構進行默克爾化:

MAST 工作原理

原始腳本:
IF
  條件A: 2-of-3 多簽
ELSE
  條件B: 90天後 + 單簽名
ELSE
  條件C: 2-of-2 多簽 + 30天後
END

MAST 結構:
                      Merkle Root
                     /          \
            hash(A)              hash(B+C)
                              /          \
                         hash(B)      hash(C)

廣播時只透露

外部觀察者只知道

P2TR 地址格式

Taproot 使用 Bech32m 編碼的 P2TR 地址:

地址格式

bc1p + 58-character data

Program 結構

生成過程

def create_p2tr_address(tweaked_pubkey):
    """生成 P2TR 地址"""
    # x-only 公鑰(32 位元組)
    x = tweaked_pubkey.x
    
    # Bech32m 編碼
    return bech32m_encode("bc", 1, [x])  # 1 表示 P2TR witness version

def tweak_public_key(Q, merkle_root):
    """Tweak 公鑰"""
    # Taproot tweak: hash(apubkey || merkle_root)
    t = int_from_bytes(
        sha256(Q.x.to_bytes(32, 'little') + merkle_root)
    ) % n
    
    # 最終公鑰
    return Q + t * G

def x_only_pubkey(P):
    """轉換為 x-only 公鑰"""
    # 如果 y 座標為奇數,則 negate
    if P.y % 2 != 0:
        return n - P.x
    return P.x

Schnorr 簽章的隱私優勢

簽章聚合的隱私效益

傳統 ECDSA 多簽名(2-of-3)

見證數據(每個簽名者單獨提供):
<sig1> <pubkey1>
<sig2> <pubkey2>

分析師可以識別:

Schnorr 聚合簽章(MuSig2)

見證數據:
<aggregate_sig>

分析師只能識別:

門檻簽名隱藏

2-of-3 門檻簽名

實際應用場景

def musig_2_of_3_aggregate(public_keys, signatures, message):
    """
    模擬 2-of-3 多簽名聚合
    
    實際實現需要使用 secp256k1-py 庫和 MuSig2 協議
    """
    # 聚合公鑰
    Q = sum(public_keys, Point(0, 0))  # Q = P1 + P2 + P3
    
    # 只需 2 個簽章即可完成驗證
    # 但外部觀察者無法確定使用了哪 2 個
    
    # 聚合簽章
    s = sum(s.x for s in signatures)  # 簡化示例
    e = compute_challenge(...)  # 計算挑戰值
    
    # 最終聚合簽章
    aggregated_sig = (s, e)
    
    # 驗證
    return SchnorrVerify(message, Q, aggregated_sig)

閃電網路隱私改進

傳統 LN 承諾交易指紋

每筆 LN 承諾交易包含:

Output 0: to_local (延遲索賠)
  <revocationpubkey>
  OP_CHECKSIGVERIFY
  <cltv_expiry>
  OP_CHECKLOCKTIMEVERIFY
  OP_DROP
  <localpubkey>

Output 1: to_remote (即時)
  <remotePubkey>

Output 2: HTLC (如果存在)
  OP_SIZE 32 OP_EQUALVERIFY
  OP_HASH160 <ripemd160-of-hash> OP_EQUAL
  OP_IF
    OP_CHECKSIG
  OP_ELSE
    <remote_htlcpubkey>
    OP_CHECKSIGVERIFY
    <cltv_expiry>
    OP_CHECKLOCKTIMEVERIFY
    OP_DROP
    OP_INPUTPATHVENDOR
  OP_ENDIF

區塊鏈分析師可以:

Taproot LN 承諾交易

使用 Taproot 後:

所有輸出都被隱藏在單一公鑰中
外部觀察者只知道:
- 這是一筆有效的 Taproot 交易
- 金額可能包含 HTLC
- 但無法確認

應用場景深度分析

閃電網路 Taproot 通道

PTLC(Point Time Locked Contract)

Taproot 啟用的 PTLC 相比 HTLC 提供更強的隱私:

class PTLC:
    """
    PTLC 實現示例
    
    PTLC 使用 Schnorr 簽名實現時間鎖定合約
    """
    
    def __init__(self, amount, timeout_blocks, payment_secret):
        self.amount = amount
        self.timeout_blocks = timeout_blocks
        self.payment_secret = payment_secret
        
        # 生成 PTLC 錨點
        self.sha256_payment = sha256(payment_secret)
        self.point = int_from_bytes(self.sha256_payment) * G
        
    def create_htlc_script(self):
        """
        PTLC 腳本結構
        
        與 HTLC 不同,PTLC 使用點 Scalar 而非 hash
        """
        return f"""
        OP_DUP
        OP_SHA256
        <{self.sha256_payment.hex()}>
        OP_EQUAL
        OP_IF
            # 接收方可以立即索賠
            # 使用 point 的離散對數作為密鑰
            <receiver_key>
            OP_CHECKSIG
        OP_ELSE
            # 發送方在超時後索還
            <timeout>
            OP_CHECKLOCKTIMEVERIFY
            OP_DROP
            <sender_key>
            OP_CHECKSIG
        OP_ENDIF
        """
    
    def generate_secret_point(self):
        """接收方生成並揭示秘密"""
        # 接收方持有 payment_secret
        # 揭示時只需提供對應的公鑰
        return self.point
    
    def claim_with_secret(self, secret):
        """
        使用秘密索賠
        
        secret 必須是 payment_secret 的離散對數
        """
        assert sha256(secret) == self.sha256_payment
        # 驗證秘密並執行索賠腳本
        return True

隱私改進原理

特性HTLCPTLC (Taproot)
鎖定類型Hash LockPoint Lock
鏈上指紋獨特 HTLC 結構普通 Taproot 交易
路由隱私Hash 可關聯Point 不可關聯
多路徑隱私

原子交換的隱私實現

跨鏈原子交換

def create_taproot_atomic_swap(bitcoin_wallet, other_chain_wallet, amount, hash_lock):
    """
    使用 Taproot 實現跨鏈原子交換
    
    優勢:
    - 比特幣端使用 Taproot 腳本
    - 外部觀察者無法識別這是原子交換
    - 交換失敗場景也不會暴露意圖
    """
    
    # 比特幣端:創建 Taproot MAST 腳本
    bitcoin_script = f"""
    OP_IF
        # 選項1:使用 hash lock 索賠
        OP_SHA256
        <{hash_lock.hex()}>
        OP_EQUAL
        <counterparty_pubkey>
        OP_CHECKSIG
    OP_ELSE
        # 選項2:超時後自動退回
        <timeout_blocks>
        OP_CHECKLOCKTIMEVERIFY
        OP_DROP
        <my_pubkey>
        OP_CHECKSIG
    OP_ENDIF
    """
    
    # 將腳本編譯為 MAST 結構
    bitcoin_script_hash = taproot_script_tree(bitcoin_script)
    
    # 創建 Taproot 地址
    bitcoin_address = create_p2tr_address(tweaked_pubkey(bitcoin_pubkey, bitcoin_script_hash))
    
    # 另一條鏈使用對應的合約
    other_chain_address = other_chain_wallet.create_hash_lock_address(hash_lock)
    
    return {
        'bitcoin_address': bitcoin_address,
        'other_chain_address': other_chain_address,
        'timeout_blocks': timeout_blocks
    }

機構保管方案

多層權限保管

class MultiLayerCustody:
    """
    使用 Taproot 的多層權限保管方案
    
    示例:冷錢包 + 時間延遲 + 多重簽名
    """
    
    def __init__(self, keys):
        self.keys = keys  # [(pubkey, role), ...]
        
    def create_custody_script(self):
        """
        創建 MAST 結構的保管腳本
        
        不同的權限組合形成不同的葉子節點
        """
        
        # 路徑1:單一管理員(緊急訪問)
        path1 = create_leaf_script("""
            <admin_key>
            OP_CHECKSIG
        """, version=0xc0)
        
        # 路徑2:2-of-3 委員會
        path2 = create_leaf_script("""
            OP_2
            <committee_key_1>
            <committee_key_2>
            <committee_key_3>
            OP_3
            OP_CHECKMULTISIG
        """, version=0xc0)
        
        # 路徑3:時間延遲 + 單籤(常規訪問)
        path3 = create_leaf_script("""
            <delay_blocks>
            OP_CHECKLOCKTIMEVERIFY
            OP_DROP
            <operations_key>
            OP_CHECKSIG
        """, version=0xc0)
        
        # 路徑4:全部 5 把鑰匙(最終備份)
        path4 = create_leaf_script("""
            OP_5
            <key_1> <key_2> <key_3> <key_4> <key_5>
            OP_5
            OP_CHECKMULTISIG
        """, version=0xc0)
        
        # 構建 MAST
        return build_merkle_tree([path1, path2, path3, path4])
    
    def create_custody_address(self):
        """創建保管地址"""
        script_tree = self.create_custody_script()
        merkle_root = compute_merkle_root(script_tree)
        
        # 使用內部公鑰(可以是 2-of-3 熱錢包)
        internal_key = aggregate_keys([self.keys[0][0], self.keys[1][0]])
        
        # Tweak
        tweaked = tweak_pubkey(internal_key, merkle_root)
        
        return create_p2tr_address(tweaked)

隱私級別深度分析

等級一:基礎隱私

使用 P2TR 地址

def upgrade_to_p2tr(wallet):
    """
    將錢包升級到 P2TR 地址
    
    隱私改進:
    - 與傳統隔離見證地址無縫兼容
    - 單簽名交易自動獲得 Taproot 隱私
    """
    
    # 生成傳統主公鑰
    master_pubkey = wallet.get_master_public_key()
    
    # 轉換為 x-only 公鑰
    xonly_key = to_xonly(master_pubkey)
    
    # 生成 P2TR 地址(不使用 MAST)
    # 將 internal_key 作為唯一的葉子節點
    p2tr_address = create_p2tr_address(xonly_key)
    
    # 設置為新地址
    wallet.set_default_address(p2tr_address)
    
    return p2tr_address

隱私特性

等級二:中級隱私

結合 CoinJoin 使用

def taproot_coinjoin_output(outputs, amount_per_output):
    """
    使用 Taproot 結構的 CoinJoin 輸出
    
    優勢:
    - 混合後的輸出與普通 Taproot 交易相同
    - 匿名集包含所有 Taproot 用戶
    """
    
    # 構建參與者公鑰列表
    participant_keys = [p.pubkey for p in outputs]
    
    # 使用 MuSig2 聚合
    aggregated_key = musig_aggregate(participant_keys)
    
    # 創建 CoinJoin 輸出
    coinjoin_outputs = []
    for i in range(len(outputs)):
        coinjoin_outputs.append({
            'pubkey': aggregated_key,
            'amount': amount_per_output,
            'blinding_factor': generate_blinding()
        })
    
    # 最終輸出結構
    return {
        'type': 'p2tr',
        'key': aggregated_key,
        'script': None,  # 沒有腳本(key path spend)
        'witness': None  # 在花費時提供簽名
    }

配合 Wasabi/JoinMarket

當前主流隱私錢包正在逐步支持 Taproot:

等級三:高級隱私

組合多層隱私工具

class MaximumPrivacyStack:
    """
    最大隱私組合棧
    
    1. 源頭清潔:從無歷史關聯的比特幣開始
    2. 混合:使用 Taproot CoinJoin
    3. 傳輸:通過閃電網路進行小額支付
    4. 接收:使用 Silent Payments 接收
    """
    
    def __init__(self):
        self.lightning_wallet = LightningWallet()
        self.coinjoin_coordinator = TaprootCoinJoin()
        self.silent_payment = SilentPayments()
        
    def receive_with_privacy(self, amount):
        """
        高隱私接收流程
        """
        # 1. 生成一次性閃電節點
        lnd_node = self.lightning_wallet.create_ephemeral_node()
        
        # 2. 創建靜默支付地址
        sp_address = self.silent_payment.derive_address(lnd_node.pubkey)
        
        # 3. 通過 CoinJoin 充值(可選)
        coinjoin_funded = self.coinjoin_coordinator.mix(
            source='clean_bitcoin',
            amount=amount * 1.1  # 多 10% 覆蓋費用
        )
        
        # 4. 打開閃電通道
        channel = self.lightning_wallet.open_channel(
            amount=amount,
            node_id=lnd_node.node_id
        )
        
        return {
            'lightning_address': f'{lnd_node.pubkey}@localhost:9735',
            'onchain_address': sp_address,
            'channel_id': channel.channel_id
        }
    
    def spend_with_privacy(self, destination, amount):
        """
        高隱私發送流程
        """
        # 1. 通過閃電網路發送(如果金額較小)
        if amount < 1000000:  # < 0.01 BTC
            invoice = self.lightning_wallet.create_invoice(amount, destination)
            return {'type': 'lightning', 'invoice': invoice}
        
        # 2. 使用 Taproot PayJoin 發送
        return {
            'type': 'payjoin',
            'destination': destination,
            'amount': amount,
            'payjoin_endpoint': self.get_payjoin_endpoint()
        }

技術深度解析

MAST 結構實現

def create_merkle_tree(leaves):
    """
    創建 Merkle Tree
    
    用於 Taproot MAST 結構
    """
    if len(leaves) == 0:
        return None
    
    if len(leaves) == 1:
        return hash_leaf(leaves[0])
    
    # 遞迴構建
    mid = (len(leaves) + 1) // 2
    left = create_merkle_tree(leaves[:mid])
    right = create_merkle_tree(leaves[mid:])
    
    return hash_internal_node(left, right)

def hash_leaf(script, version=0xc0):
    """
    哈希 MAST 葉子節點
    
    BIP-341 定義的葉子節點哈希
    """
    version_byte = bytes([version])
    script_bytes = script.encode()
    return sha256(version_byte + script_bytes)

def hash_internal_node(left, right):
    """
    哈希 MAST 內部節點
    
    BIP-341 定義的內部節點哈希
    """
    # Tag 確保內部和葉子節點可區分
    return sha256(b'\x00' + left + right)

def build_taproot_output(internal_key, script_tree):
    """
    構建 Taproot 輸出
    
    BIP-341 定義
    """
    if script_tree is None:
        # 沒有腳本:只使用 internal_key
        merkle_root = b'\x00' * 32
    else:
        # 有腳本:計算 MAST 根
        merkle_root = script_tree
    
    # Tweak internal_key
    t = int_from_bytes(
        tagged_hash("TapTweak", internal_key + merkle_root)
    ) % n
    
    tweaked_key = internal_key + t * G
    
    # x-only 公鑰
    if tweaked_key.y % 2 != 0:
        tweaked_key = -tweaked_key
    
    return (tweaked_key.x, merkle_root)

Tapscript 特性

# Tapscript 相比 Bitcoin Script 的改進

TAPSCRIPT_FEATURES = {
    # OP_CHECKSIG 和 OP_CHECKSIGVERIFY 現在使用 Schnorr 簽名
    # 支援簽名聚合
    'schnorr_verification': True,
    
    # 新增 OP_CHECKSIGADD
    # 用於批量簽名驗證
    'op_checksigadd': True,
    
    # 更大的腳本容量
    'max_script_size': 10000,  # bytes
    'max_script_element_size': 520,  # bytes
    
    # 更高的運算限制
    'max_opstack_size': 1000,
    'max_numpubkeys': 1000,
}

採用現況與數據

2024-2026 年採用數據

P2TR 地址使用率

時間P2TR 輸出占比P2TR 交易占比
2022 Q1< 1%< 1%
2023 Q1~ 5%~ 3%
2024 Q1~ 15%~ 10%
2025 Q1~ 30%~ 20%
2026 Q1~ 45%~ 35%

採用驅動因素

錢包支持矩陣

錢包P2TR 接收P2TR 發送MuSig2PTLC
Bitcoin Core
Sparrow Wallet
Ledger
Trezor
Wasabi
JoinMarket
LND測試中

局限性與風險

並非完美解決方案

  1. 金額仍然可見:Taproot 不隱藏交易金額,需要 Confidential Transactions 或閃電網路
  1. 時序分析
  1. 歷史追溯
  1. 網路層隱私

配套需求

充分發揮 Taproot 隱私優勢需要:

總結

Taproot 升級為比特幣帶來了革命性的隱私改進。通過 Schnorr 簽章聚合和 MAST 腳本結構,複雜的交易策略變得與簡單交易無法區分。這不僅提升了個人隱私,也為閃電網路等二層解決方案提供了更強的隱私保護。

隨著採用率提升,比特幣的隱私特性將會越來越強大。建議比特幣用戶:

  1. 優先使用支持 Taproot 的錢包
  2. 新資金直接接收至 P2TR 地址
  3. 考慮在 CoinJoin 中使用 Taproot 格式
  4. 配合閃電網路進行小額支付
  5. 持續關注隱私技術發展

延伸閱讀與來源

這篇文章對您有幫助嗎?

評論

發表評論

注意:由於這是靜態網站,您的評論將儲存在本地瀏覽器中,不會公開顯示。

目前尚無評論,成為第一個發表評論的人吧!