Skip to content

Commit

Permalink
remove seal closing method as a generic parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
dr-orlovsky committed Nov 6, 2024
1 parent a928721 commit 0e94c5e
Show file tree
Hide file tree
Showing 12 changed files with 99 additions and 158 deletions.
26 changes: 9 additions & 17 deletions dbc/src/anchor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,12 @@
//! defined by LNPBP-4.
use std::error::Error;
use std::marker::PhantomData;

use bc::Tx;
use commit_verify::mpc::{self, Message, ProtocolId};
use strict_encoding::{StrictDumb, StrictEncode};

use crate::{DbcMethod, Method, LIB_NAME_BPCORE};
use crate::LIB_NAME_BPCORE;

mod dbc {
pub use crate::Proof;
Expand Down Expand Up @@ -67,26 +66,21 @@ pub enum VerifyError<E: Error> {
derive(Serialize, Deserialize),
serde(crate = "serde_crate", rename_all = "camelCase")
)]
pub struct Anchor<L: mpc::Proof + StrictDumb, D: dbc::Proof<M>, M: DbcMethod = Method> {
pub struct Anchor<L: mpc::Proof + StrictDumb, D: dbc::Proof> {
/// Structured multi-protocol LNPBP-4 data the transaction commits to.
pub mpc_proof: L,

/// Proof of the DBC commitment.
pub dbc_proof: D,

#[strict_type(skip)]
#[cfg_attr(feature = "serde", serde(skip))]
_method: PhantomData<M>,
}

impl<L: mpc::Proof + StrictDumb, D: dbc::Proof<M>, M: DbcMethod> Anchor<L, D, M> {
impl<L: mpc::Proof + StrictDumb, D: dbc::Proof> Anchor<L, D> {
/// 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: PhantomData,
}
}

Expand Down Expand Up @@ -115,19 +109,18 @@ pub enum MergeError {
DbcMismatch,
}

impl<D: dbc::Proof<M>, M: DbcMethod> Anchor<mpc::MerkleProof, D, M> {
impl<D: dbc::Proof> Anchor<mpc::MerkleProof, D> {
/// 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> {
) -> Result<Anchor<mpc::MerkleBlock, D>, 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: PhantomData,
})
}

Expand All @@ -136,7 +129,7 @@ impl<D: dbc::Proof<M>, M: DbcMethod> Anchor<mpc::MerkleProof, D, M> {
&self,
protocol_id: impl Into<ProtocolId>,
message: impl Into<Message>,
) -> Result<Anchor<mpc::MerkleBlock, D, M>, mpc::InvalidProof> {
) -> Result<Anchor<mpc::MerkleBlock, D>, mpc::InvalidProof> {
self.clone().into_merkle_block(protocol_id, message)
}

Expand Down Expand Up @@ -164,13 +157,13 @@ impl<D: dbc::Proof<M>, M: DbcMethod> Anchor<mpc::MerkleProof, D, M> {
}
}

impl<D: dbc::Proof<M>, M: DbcMethod> Anchor<mpc::MerkleBlock, D, M> {
impl<D: dbc::Proof> Anchor<mpc::MerkleBlock, D> {
/// 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> {
) -> Result<Anchor<mpc::MerkleProof, D>, mpc::LeafNotKnown> {
self.clone().into_merkle_proof(protocol)
}

Expand All @@ -179,12 +172,11 @@ impl<D: dbc::Proof<M>, M: DbcMethod> Anchor<mpc::MerkleBlock, D, M> {
pub fn into_merkle_proof(
self,
protocol: impl Into<ProtocolId>,
) -> Result<Anchor<mpc::MerkleProof, D, M>, mpc::LeafNotKnown> {
) -> Result<Anchor<mpc::MerkleProof, D>, 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: PhantomData,
})
}

Expand Down
2 changes: 1 addition & 1 deletion dbc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,4 @@ pub mod tapret;
mod proof;

pub use anchor::Anchor;
pub use proof::{DbcMethod, Method, MethodParseError, Proof};
pub use proof::{Method, MethodParseError, Proof};
4 changes: 2 additions & 2 deletions dbc/src/opret/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,10 @@ pub struct OpretProof(());
impl StrictSerialize for OpretProof {}
impl StrictDeserialize for OpretProof {}

impl Proof<Method> for OpretProof {
impl Proof for OpretProof {
type Error = EmbedVerifyError<OpretError>;

fn method(&self) -> Method { Method::OpretFirst }
const METHOD: Method = Method::OpretFirst;

fn verify(&self, msg: &Commitment, tx: &Tx) -> Result<(), EmbedVerifyError<OpretError>> {
tx.verify(msg, self)
Expand Down
25 changes: 4 additions & 21 deletions dbc/src/proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,6 @@ use strict_encoding::{StrictDecode, StrictDeserialize, StrictDumb, StrictEncode,

use crate::LIB_NAME_BPCORE;

/// Trait defining DBC method - or enumberation of allowed DBC methods used by
/// proofs, single-use-seals etc.
pub trait DbcMethod:
Copy
+ Eq
+ Ord
+ std::hash::Hash
+ strict_encoding::StrictDumb
+ strict_encoding::StrictEncode
+ strict_encoding::StrictDecode
{
}

/// wrong deterministic bitcoin commitment closing method id '{0}'.
#[derive(Clone, PartialEq, Eq, Debug, Display, Error, From)]
#[display(doc_comments)]
Expand Down Expand Up @@ -70,8 +57,6 @@ pub enum Method {
TapretFirst = 0x01,
}

impl DbcMethod for Method {}

impl FromStr for Method {
type Err = MethodParseError;

Expand All @@ -85,15 +70,13 @@ impl FromStr for Method {
}

/// Deterministic bitcoin commitment proof types.
pub trait Proof<M: DbcMethod = Method>:
Clone + Eq + Debug + StrictSerialize + StrictDeserialize + StrictDumb
{
pub trait Proof: Clone + Eq + Debug + StrictSerialize + StrictDeserialize + StrictDumb {
/// Returns a single-use seal closing method used by the DBC proof.
const METHOD: Method;

/// Verification error.
type Error: Error;

/// Returns a single-use seal closing method used by the DBC proof.
fn method(&self) -> M;

/// Verifies DBC proof against the provided transaction.
fn verify(&self, msg: &mpc::Commitment, tx: &Tx) -> Result<(), Self::Error>;
}
4 changes: 2 additions & 2 deletions dbc/src/tapret/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -375,10 +375,10 @@ impl TapretProof {
}
}

impl Proof<Method> for TapretProof {
impl Proof for TapretProof {
type Error = ConvolveVerifyError;

fn method(&self) -> Method { Method::TapretFirst }
const METHOD: Method = Method::TapretFirst;

fn verify(&self, msg: &Commitment, tx: &Tx) -> Result<(), ConvolveVerifyError> {
ConvolveCommitProof::<_, Tx, _>::verify(self, msg, tx)
Expand Down
5 changes: 0 additions & 5 deletions seals/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,3 @@ pub mod txout;
mod secret;

pub use secret::SecretSeal;

/// Method for closing BP single-use-seals.
pub trait SealCloseMethod: dbc::DbcMethod {}

impl SealCloseMethod for dbc::Method {}
Loading

0 comments on commit 0e94c5e

Please sign in to comment.