dbc/tapret/
tx.rs

1// Deterministic bitcoin commitments library.
2//
3// SPDX-License-Identifier: Apache-2.0
4//
5// Written in 2019-2024 by
6//     Dr Maxim Orlovsky <orlovsky@lnp-bp.org>
7//
8// Copyright (C) 2019-2024 LNP/BP Standards Association. All rights reserved.
9//
10// Licensed under the Apache License, Version 2.0 (the "License");
11// you may not use this file except in compliance with the License.
12// You may obtain a copy of the License at
13//
14//     http://www.apache.org/licenses/LICENSE-2.0
15//
16// Unless required by applicable law or agreed to in writing, software
17// distributed under the License is distributed on an "AS IS" BASIS,
18// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19// See the License for the specific language governing permissions and
20// limitations under the License.
21
22use bc::Tx;
23use commit_verify::{mpc, ConvolveCommit, ConvolveCommitProof};
24
25use super::{TapretFirst, TapretKeyError, TapretProof};
26
27/// Errors during tapret commitment.
28#[derive(Clone, Eq, PartialEq, Debug, Display, Error, From)]
29#[cfg_attr(
30    feature = "serde",
31    derive(Serialize, Deserialize),
32    serde(crate = "serde_crate", rename_all = "camelCase")
33)]
34pub enum TapretError {
35    /// Error embedding tapret commitment into x-only key.
36    #[from]
37    #[display(inner)]
38    KeyEmbedding(TapretKeyError),
39
40    /// tapret commitment in a transaction lacking any taproot outputs.
41    #[display(doc_comments)]
42    NoTaprootOutput,
43}
44
45impl ConvolveCommitProof<mpc::Commitment, Tx, TapretFirst> for TapretProof {
46    type Suppl = Self;
47
48    fn restore_original(&self, commitment: &Tx) -> Tx {
49        let mut tx = commitment.clone();
50
51        for txout in &mut tx.outputs {
52            if txout.script_pubkey.is_p2tr() {
53                txout.script_pubkey = self.original_pubkey_script();
54                break;
55            }
56        }
57        tx
58    }
59
60    fn extract_supplement(&self) -> &Self::Suppl { self }
61}
62
63impl ConvolveCommit<mpc::Commitment, TapretProof, TapretFirst> for Tx {
64    type Commitment = Tx;
65    type CommitError = TapretError;
66
67    fn convolve_commit(
68        &self,
69        supplement: &TapretProof,
70        msg: &mpc::Commitment,
71    ) -> Result<(Tx, TapretProof), Self::CommitError> {
72        let mut tx = self.clone();
73
74        for txout in &mut tx.outputs {
75            if txout.script_pubkey.is_p2tr() {
76                let (commitment, proof) =
77                    txout.convolve_commit(supplement, msg).map_err(TapretError::from)?;
78                *txout = commitment;
79                return Ok((tx, proof));
80            }
81        }
82
83        Err(TapretError::NoTaprootOutput)
84    }
85}
86
87#[cfg(test)]
88mod test {
89    use std::str::FromStr;
90
91    use amplify::hex::FromHex;
92    use amplify::Bytes32;
93    use bc::InternalPk;
94    use commit_verify::mpc::Commitment;
95    use commit_verify::ConvolveVerifyError;
96    use secp256k1::{ffi, XOnlyPublicKey};
97
98    use super::*;
99    use crate::tapret::TapretPathProof;
100
101    #[test]
102    fn no_commitment() {
103        let tx = Tx::from_str(
104            "020000000001027763e2a0ad25d45b63a19c33491b67c5037e72709121290bac5481a5d5d0c9330100000000ffffffff7763e2a0ad25d45b63a19c33491b67c5037e72709121290bac5481a5d5d0c9330400000000ffffffff02026e010000000000225120455dfcc062ef80609b007377f127e4abdb5cb0052158af1fab7aa628c34563f1d508000000000000225120a2788d4208ec6b4b600aef4c13075cf1d47bda0299ed1e6eedce4e7a90fb2a2c0141150df5377a34deded048dc01bff3d4f5f31d8a89fe2fbf1d0295993c1f899b3cefd1a63900ea6346b78edd476524c08ae094ff417bfa525b585ee66ebc26bb9e010141d959f21b498d90c2ff9f5b0bf3aee9158527501162eab2e3d56371714877a97df80caab15e366855aa56443b7d081c234a4ce4d6414815a874624cbe46b643370100000000"
105        ).unwrap();
106
107        let internal_pk: XOnlyPublicKey = unsafe {
108            ffi::XOnlyPublicKey::from_array_unchecked(<[u8; 64]>::from_hex(
109                "cb5271aa59fc637e29d034ec75363ca241fda5d3939684603b469b185be7e50f18ec6fd539e7dc1fd5fb4cf046d2cef5028a5ca0cdb09a252683e6a6eb2ad61d",
110            ).unwrap()).into()
111        };
112        let proof = TapretProof {
113            path_proof: TapretPathProof {
114                partner_node: None,
115                nonce: 0,
116            },
117            internal_pk: InternalPk::from(internal_pk),
118        };
119
120        let msg = Commitment::from(Bytes32::zero());
121        assert_eq!(
122            ConvolveCommitProof::<_, Tx, _>::verify(&proof, &msg, &tx),
123            Err(ConvolveVerifyError::CommitmentMismatch)
124        );
125    }
126}