Skip to content

Commit

Permalink
seal: remove method commitment
Browse files Browse the repository at this point in the history
seal method must be defined at the protocol level, such that all seals under a protocol are enforced to use the same method
  • Loading branch information
dr-orlovsky committed Nov 13, 2024
1 parent 0e94c5e commit 586d3fa
Show file tree
Hide file tree
Showing 25 changed files with 441 additions and 1,338 deletions.
160 changes: 65 additions & 95 deletions Cargo.lock

Large diffs are not rendered by default.

18 changes: 11 additions & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ default-members = [
]

[workspace.package]
version = "0.11.0-beta.9"
version = "0.12.0-alpha.1"
authors = ["Dr Maxim Orlovsky <[email protected]>"]
homepage = "https://github.com/BP-WG"
repository = "https://github.com/BP-WG/bp-core"
Expand All @@ -23,13 +23,13 @@ license = "Apache-2.0"

[workspace.dependencies]
amplify = "4.7.0"
strict_encoding = "2.7.0"
strict_encoding = "2.7.2"
strict_types = "2.7.2"
commit_verify = "0.11.0-beta.9"
single_use_seals = "0.11.0-beta.9"
bp-consensus = { version = "0.11.0-beta.9", path = "./consensus" }
bp-dbc = { version = "0.11.0-beta.9", path = "./dbc" }
bp-seals = { version = "0.11.0-beta.9", path = "./seals" }
commit_verify = "0.12.0-alpha.2"
single_use_seals = "0.12.0-alpha.2"
bp-consensus = { version = "0.12.0-alpha.1", path = "./consensus" }
bp-dbc = { version = "0.12.0-alpha.1", path = "./dbc" }
bp-seals = { version = "0.12.0-alpha.1", path = "./seals" }
secp256k1 = { version = "0.30.0", features = ["global-context", "rand"] }
serde_crate = { package = "serde", version = "1", features = ["derive"] }

Expand Down Expand Up @@ -89,3 +89,7 @@ wasm-bindgen-test = "0.3"

[package.metadata.docs.rs]
features = ["all"]

[patch.crates-io]
commit_verify = { git = "https://github.com/LNP-BP/client_side_validation", branch = "v0.12" }
single_use_seals = { git = "https://github.com/LNP-BP/client_side_validation", branch = "v0.12" }
4 changes: 2 additions & 2 deletions consensus/src/stl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ use crate::{
};

pub const LIB_ID_BP_TX: &str =
"stl:9WwTYiP2-OadKCZP-cR0bJ!Y-qruINYX-bXZFj8Y-fsQoGgo#signal-color-cipher";
"stl:9WwTYiP2-OadKCZP-cR0bJ~Y-qruINYX-bXZFj8Y-fsQoGgo#signal-color-cipher";
pub const LIB_ID_BP_CONSENSUS: &str =
"stl:q7G95wzt-SxT2BMV-t!PokBt-wNYgZTu-AaYAtM3-rYjlzs4#agenda-wolf-pagoda";
"stl:q7G95wzt-SxT2BMV-t~PokBt-wNYgZTu-AaYAtM3-rYjlzs4#agenda-wolf-pagoda";

#[deprecated(since = "0.10.8", note = "use _bp_tx_stl instead")]
fn _bitcoin_stl() -> Result<TypeLib, CompileError> { _bp_tx_stl() }
Expand Down
11 changes: 8 additions & 3 deletions dbc/src/proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ use crate::LIB_NAME_BPCORE;
pub struct MethodParseError(pub String);

/// Method of DBC construction.
///
/// Method defines a set of parameters used by a single-use seal, such as:
/// - selection of bitcoin input;
/// - commitment algorithm;
/// - used hash functions.
#[derive(Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash, Debug, Display)]
#[cfg_attr(
feature = "serde",
Expand All @@ -46,13 +51,13 @@ pub struct MethodParseError(pub String);
#[repr(u8)]
pub enum Method {
/// OP_RETURN commitment present in the first OP_RETURN-containing
/// transaction output.
/// transaction output, made with tagged SHA256 hash function.
#[display("opret1st")]
#[strict_type(dumb)]
OpretFirst = 0x00,

/// Taproot-based OP_RETURN commitment present in the first Taproot
/// transaction output.
/// transaction output, made with tagged SHA256 hash function.
#[display("tapret1st")]
TapretFirst = 0x01,
}
Expand All @@ -75,7 +80,7 @@ pub trait Proof: Clone + Eq + Debug + StrictSerialize + StrictDeserialize + Stri
const METHOD: Method;

/// Verification error.
type Error: Error;
type Error: Clone + Error;

/// Verifies DBC proof against the provided transaction.
fn verify(&self, msg: &mpc::Commitment, tx: &Tx) -> Result<(), Self::Error>;
Expand Down
6 changes: 3 additions & 3 deletions seals/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
unused_mut,
unused_imports,
dead_code,
missing_docs
// missing_docs
)]
#![cfg_attr(docsrs, feature(doc_auto_cfg))]

Expand All @@ -43,8 +43,8 @@ extern crate commit_verify;
#[macro_use]
extern crate serde_crate as serde;

pub mod resolver;
pub mod txout;
mod txout;
mod secret;

pub use secret::SecretSeal;
pub use txout::{Anchor, Noise, TxoSeal, TxoSealExt};
42 changes: 0 additions & 42 deletions seals/src/resolver.rs

This file was deleted.

154 changes: 154 additions & 0 deletions seals/src/txout.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
// Bitcoin protocol single-use-seals library.
//
// SPDX-License-Identifier: Apache-2.0
//
// Written in 2019-2024 by
// Dr Maxim Orlovsky <[email protected]>
//
// 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.

//! Bitcoin single-use-seals defined by a transaction output and closed by
//! spending that output ("TxOut seals").
use core::fmt::Debug;
use core::marker::PhantomData;

use amplify::Bytes;
use bc::{Outpoint, Tx, Txid};
use commit_verify::mpc::{self, ProtocolId};
use commit_verify::ReservedBytes;
use single_use_seals::{ClientSideWitness, PublishedWitness, SealWitness, SingleUseSeal};
use strict_encoding::StrictDumb;

use crate::SecretSeal;

#[derive(Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash, Debug, From)]
#[derive(StrictType, StrictDumb, StrictEncode, StrictDecode)]

Check warning on line 38 in seals/src/txout.rs

View check run for this annotation

Codecov / codecov/patch

seals/src/txout.rs#L38

Added line #L38 was not covered by tests
#[strict_type(lib = dbc::LIB_NAME_BPCORE)]
#[cfg_attr(
feature = "serde",
derive(Serialize, Deserialize),

Check warning on line 42 in seals/src/txout.rs

View check run for this annotation

Codecov / codecov/patch

seals/src/txout.rs#L42

Added line #L42 was not covered by tests
serde(crate = "serde_crate", transparent)
)]
pub struct Noise(Bytes<68>);

#[derive(Clone, Ord, PartialOrd, Eq, PartialEq, Hash, Debug)]
#[derive(StrictType, StrictDumb, StrictEncode, StrictDecode)]

Check warning on line 48 in seals/src/txout.rs

View check run for this annotation

Codecov / codecov/patch

seals/src/txout.rs#L48

Added line #L48 was not covered by tests
#[strict_type(lib = dbc::LIB_NAME_BPCORE)]
#[cfg_attr(
feature = "serde",
derive(Serialize, Deserialize),

Check warning on line 52 in seals/src/txout.rs

View check run for this annotation

Codecov / codecov/patch

seals/src/txout.rs#L52

Added line #L52 was not covered by tests
serde(crate = "serde_crate", rename_all = "camelCase")
)]
pub struct Anchor<D: dbc::Proof> {
pub mpc_proof: mpc::MerkleProof,
pub dbc_proof: D,
#[cfg_attr(feature = "serde", serde(skip))]
// TODO: This should become an option
pub fallback_proof: ReservedBytes<1>,
}

pub struct Proof<D: dbc::Proof> {
pub mpc_commit: mpc::Commitment,
pub dbc_proof: D,
}

#[derive(Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash, Debug, Display)]
#[derive(StrictType, StrictEncode, StrictDecode)]
#[strict_type(lib = dbc::LIB_NAME_BPCORE, tags = custom)]
#[cfg_attr(
feature = "serde",
derive(Serialize, Deserialize),
serde(crate = "serde_crate", untagged)
)]
pub enum TxoSealExt {
#[display("~")]
#[strict_type(tag = 0)]
Noise(Noise),

#[display(inner)]
#[strict_type(tag = 1)]
Fallback(Outpoint),
}

impl StrictDumb for TxoSealExt {
fn strict_dumb() -> Self { TxoSealExt::Noise(Noise::from(Bytes::from_byte_array([0u8; 68]))) }
}

#[derive(Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash, Debug, Display)]
#[display("{primary}/{secondary}")]
#[derive(StrictType, StrictDumb, StrictEncode, StrictDecode)]

Check warning on line 92 in seals/src/txout.rs

View check run for this annotation

Codecov / codecov/patch

seals/src/txout.rs#L92

Added line #L92 was not covered by tests
#[strict_type(lib = dbc::LIB_NAME_BPCORE)]
#[derive(CommitEncode)]
#[commit_encode(strategy = strict, id = SecretSeal)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(crate = "serde_crate"))]

Check warning on line 96 in seals/src/txout.rs

View check run for this annotation

Codecov / codecov/patch

seals/src/txout.rs#L96

Added line #L96 was not covered by tests
pub struct TxoSeal<D: dbc::Proof> {
pub primary: Outpoint,
pub secondary: TxoSealExt,
#[strict_type(skip)]
#[cfg_attr(feature = "serde", serde(skip))]
_phantom: PhantomData<D>,
}

impl<D: dbc::Proof> SingleUseSeal for TxoSeal<D> {
type Message = Proof<D>;
type PubWitness = Tx;
type CliWitness = Anchor<D>;

fn is_included(&self, witness: &SealWitness<Self>) -> bool {
let mut inputs = witness.published.inputs();
match self.secondary {

Check warning on line 112 in seals/src/txout.rs

View check run for this annotation

Codecov / codecov/patch

seals/src/txout.rs#L110-L112

Added lines #L110 - L112 were not covered by tests
TxoSealExt::Noise(_) => {
inputs.any(|input| input.prev_output == self.primary)

Check warning on line 114 in seals/src/txout.rs

View check run for this annotation

Codecov / codecov/patch

seals/src/txout.rs#L114

Added line #L114 was not covered by tests
// TODO: && witness.client.fallback_proof.is_none()
}
TxoSealExt::Fallback(fallback) => {
inputs.any(|input| input.prev_output == fallback)

Check warning on line 118 in seals/src/txout.rs

View check run for this annotation

Codecov / codecov/patch

seals/src/txout.rs#L117-L118

Added lines #L117 - L118 were not covered by tests
// TODO: && witness.client.fallback_proof.is_some()
}
}
}

Check warning on line 122 in seals/src/txout.rs

View check run for this annotation

Codecov / codecov/patch

seals/src/txout.rs#L122

Added line #L122 was not covered by tests
}

// TODO: It's not just a transaction, it should be an SPV proof
impl<D: dbc::Proof> PublishedWitness<TxoSeal<D>> for Tx {
type PubId = Txid;
type Error = D::Error;

fn pub_id(&self) -> Txid { self.txid() }
fn verify_commitment(&self, proof: Proof<D>) -> Result<(), Self::Error> {
proof.dbc_proof.verify(&proof.mpc_commit, self)
}

Check warning on line 133 in seals/src/txout.rs

View check run for this annotation

Codecov / codecov/patch

seals/src/txout.rs#L130-L133

Added lines #L130 - L133 were not covered by tests
}

impl<D: dbc::Proof> ClientSideWitness for Anchor<D> {
type Message = (ProtocolId, mpc::Message);
type Seal = TxoSeal<D>;
type Error = mpc::InvalidProof;

fn convolve_commit(
&self,
(protocol_id, message): (ProtocolId, mpc::Message),
) -> Result<Proof<D>, Self::Error> {

Check warning on line 144 in seals/src/txout.rs

View check run for this annotation

Codecov / codecov/patch

seals/src/txout.rs#L141-L144

Added lines #L141 - L144 were not covered by tests
// TODO: Verify fallback proof
// if let Some(_fallback_proof) = self.fallback_proof {
// }
let mpc_commit = self.mpc_proof.convolve(protocol_id, message)?;
Ok(Proof {
mpc_commit,
dbc_proof: self.dbc_proof.clone(),
})
}

Check warning on line 153 in seals/src/txout.rs

View check run for this annotation

Codecov / codecov/patch

seals/src/txout.rs#L148-L153

Added lines #L148 - L153 were not covered by tests
}
Loading

0 comments on commit 586d3fa

Please sign in to comment.