1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
// Deterministic bitcoin commitments library.
//
// SPDX-License-Identifier: Apache-2.0
//
// Written in 2019-2024 by
// Dr Maxim Orlovsky <orlovsky@lnp-bp.org>
//
// Copyright (C) 2019-2024 LNP/BP Standards Association. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//! Anchors are data structures used in deterministic bitcoin commitments for
//! keeping information about the proof of the commitment in connection to the
//! transaction which contains the commitment, and multi-protocol merkle tree as
//! defined by LNPBP-4.
use std::error::Error;
use bc::Tx;
use commit_verify::mpc::{self, Message, ProtocolId};
use strict_encoding::{StrictDumb, StrictEncode};
use crate::{DbcMethod, Method, LIB_NAME_BPCORE};
mod dbc {
pub use crate::Proof;
}
/// Errors verifying anchors.
#[derive(Clone, Eq, PartialEq, Debug, Display, Error, From)]
#[display(inner)]
#[cfg_attr(
feature = "serde",
derive(Serialize, Deserialize),
serde(crate = "serde_crate", rename_all = "camelCase")
)]
pub enum VerifyError<E: Error> {
/// Deterministic commitment error.
#[display(inner)]
Dbc(E),
/// invalid MPC proof. Details: {0}
#[from]
Mpc(mpc::InvalidProof),
}
/// Anchor is a data structure used in deterministic bitcoin commitments for
/// keeping information about the proof of the commitment in connection to the
/// transaction which contains the commitment, and multi-protocol merkle tree as
/// defined by LNPBP-4.
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
#[derive(StrictType, StrictDumb, StrictEncode, StrictDecode)]
#[strict_type(lib = LIB_NAME_BPCORE)]
#[cfg_attr(
feature = "serde",
derive(Serialize, Deserialize),
serde(crate = "serde_crate", rename_all = "camelCase")
)]
pub struct Anchor<L: mpc::Proof + StrictDumb, D: dbc::Proof<M>, M: DbcMethod = Method> {
/// Structured multi-protocol LNPBP-4 data the transaction commits to.
pub mpc_proof: L,
/// Proof of the DBC commitment.
pub dbc_proof: D,
/// Method used by the anchor
pub method: M,
}
impl<L: mpc::Proof + StrictDumb, D: dbc::Proof<M>, M: DbcMethod> Anchor<L, D, M> {
/// Constructs anchor for a given witness transaction id, MPC and DBC
/// proofs.
pub fn new(mpc_proof: L, dbc_proof: D) -> Self {
Self {
mpc_proof,
dbc_proof,
method: D::METHOD,
}
}
/// Verifies whether one anchor matches another ancor.
///
/// This is not the same as `Eq`, since two anchors may reveal different
/// messages in their MPC proofs, and this be non-equivalent, at the same
/// time matching each other , i.e. having the same merkle root and
/// producing the same commitments.
#[inline]
pub fn matches(&self, other: &Self) -> bool {
self.mpc_proof.matches(&other.mpc_proof)
&& self.dbc_proof == other.dbc_proof
&& self.method == other.method
}
}
/// Error merging two [`Anchor`]s.
#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug, Display, Error, From)]
#[display(doc_comments)]
pub enum MergeError {
/// Error merging two MPC proofs, which are unrelated.
#[display(inner)]
#[from]
MpcMismatch(mpc::MergeError),
/// anchors can't be merged since they have different DBC proofs.
DbcMismatch,
/// anchors can't be merged since they use different method.
MethodMismatch,
}
impl<D: dbc::Proof<M>, M: DbcMethod> Anchor<mpc::MerkleProof, D, M> {
/// Reconstructs anchor containing merkle block
pub fn into_merkle_block(
self,
protocol_id: impl Into<ProtocolId>,
message: impl Into<Message>,
) -> Result<Anchor<mpc::MerkleBlock, D, M>, mpc::InvalidProof> {
let lnpbp4_proof =
mpc::MerkleBlock::with(&self.mpc_proof, protocol_id.into(), message.into())?;
Ok(Anchor {
mpc_proof: lnpbp4_proof,
dbc_proof: self.dbc_proof,
method: self.method,
})
}
/// Reconstructs anchor containing merkle block
pub fn to_merkle_block(
&self,
protocol_id: impl Into<ProtocolId>,
message: impl Into<Message>,
) -> Result<Anchor<mpc::MerkleBlock, D, M>, mpc::InvalidProof> {
self.clone().into_merkle_block(protocol_id, message)
}
/// Verifies that the transaction commits to the anchor and the anchor
/// commits to the given message under the given protocol.
pub fn verify(
&self,
protocol_id: impl Into<ProtocolId>,
message: impl Into<Message>,
tx: &Tx,
) -> Result<mpc::Commitment, VerifyError<D::Error>> {
let mpc_commitment = self.convolve(protocol_id, message)?;
self.dbc_proof.verify(&mpc_commitment, tx).map_err(VerifyError::Dbc)?;
Ok(mpc_commitment)
}
/// Verifies that the anchor commits to the given message under the given
/// protocol.
pub fn convolve(
&self,
protocol_id: impl Into<ProtocolId>,
message: impl Into<Message>,
) -> Result<mpc::Commitment, mpc::InvalidProof> {
self.mpc_proof.convolve(protocol_id.into(), message.into())
}
}
impl<D: dbc::Proof<M>, M: DbcMethod> Anchor<mpc::MerkleBlock, D, M> {
/// Conceals all LNPBP-4 data except specific protocol and produces merkle
/// proof anchor.
pub fn to_merkle_proof(
&self,
protocol: impl Into<ProtocolId>,
) -> Result<Anchor<mpc::MerkleProof, D, M>, mpc::LeafNotKnown> {
self.clone().into_merkle_proof(protocol)
}
/// Conceals all LNPBP-4 data except specific protocol and converts anchor
/// into merkle proof anchor.
pub fn into_merkle_proof(
self,
protocol: impl Into<ProtocolId>,
) -> Result<Anchor<mpc::MerkleProof, D, M>, mpc::LeafNotKnown> {
let lnpbp4_proof = self.mpc_proof.to_merkle_proof(protocol.into())?;
Ok(Anchor {
mpc_proof: lnpbp4_proof,
dbc_proof: self.dbc_proof,
method: self.method,
})
}
/// Conceals all LNPBP-4 data except specific protocol.
pub fn conceal_except(
&mut self,
protocols: impl AsRef<[ProtocolId]>,
) -> Result<usize, mpc::LeafNotKnown> {
self.mpc_proof.conceal_except(protocols)
}
/// Merges two anchors keeping revealed data.
pub fn merge_reveal(mut self, other: Self) -> Result<Self, MergeError> {
if self.method != other.method {
return Err(MergeError::MethodMismatch);
}
if self.dbc_proof != other.dbc_proof {
return Err(MergeError::DbcMismatch);
}
self.mpc_proof.merge_reveal(other.mpc_proof)?;
Ok(self)
}
}