Skip to content

Commit

Permalink
cmr: pull Constructible impl on Cmr into an impl on an auxiliary type
Browse files Browse the repository at this point in the history
This looks like a purely noise-increasing change, but when we introduce
type inference contexts, we will need the auxiliary type to hold an
inference context (which will be unused except for sanity-checking that
users are being consistent with their inference contexts).
  • Loading branch information
apoelstra committed Jul 3, 2024
1 parent b55b8e7 commit 9b0790e
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 24 deletions.
73 changes: 53 additions & 20 deletions src/merkle/cmr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,75 +253,108 @@ impl Cmr {
];
}

impl CoreConstructible for Cmr {
/// Wrapper around a CMR which allows it to be constructed with the
/// `*Constructible*` traits, allowing CMRs to be computed using the
/// same generic construction code that nodes are.
pub struct ConstructibleCmr {
pub cmr: Cmr,
}

impl CoreConstructible for ConstructibleCmr {
fn iden() -> Self {
Cmr::iden()
ConstructibleCmr { cmr: Cmr::iden() }
}

fn unit() -> Self {
Cmr::unit()
ConstructibleCmr { cmr: Cmr::unit() }
}

fn injl(child: &Self) -> Self {
Cmr::injl(*child)
ConstructibleCmr {
cmr: Cmr::injl(child.cmr),
}
}

fn injr(child: &Self) -> Self {
Cmr::injl(*child)
ConstructibleCmr {
cmr: Cmr::injl(child.cmr),
}
}

fn take(child: &Self) -> Self {
Cmr::take(*child)
ConstructibleCmr {
cmr: Cmr::take(child.cmr),
}
}

fn drop_(child: &Self) -> Self {
Cmr::drop(*child)
ConstructibleCmr {
cmr: Cmr::drop(child.cmr),
}
}

fn comp(left: &Self, right: &Self) -> Result<Self, Error> {
Ok(Cmr::comp(*left, *right))
Ok(ConstructibleCmr {
cmr: Cmr::comp(left.cmr, right.cmr),
})
}

fn case(left: &Self, right: &Self) -> Result<Self, Error> {
Ok(Cmr::case(*left, *right))
Ok(ConstructibleCmr {
cmr: Cmr::case(left.cmr, right.cmr),
})
}

fn assertl(left: &Self, right: Cmr) -> Result<Self, Error> {
Ok(Cmr::case(*left, right))
Ok(ConstructibleCmr {
cmr: Cmr::case(left.cmr, right),
})
}

fn assertr(left: Cmr, right: &Self) -> Result<Self, Error> {
Ok(Cmr::case(left, *right))
Ok(ConstructibleCmr {
cmr: Cmr::case(left, right.cmr),
})
}

fn pair(left: &Self, right: &Self) -> Result<Self, Error> {
Ok(Cmr::pair(*left, *right))
Ok(ConstructibleCmr {
cmr: Cmr::pair(left.cmr, right.cmr),
})
}

fn fail(entropy: FailEntropy) -> Self {
Cmr::fail(entropy)
ConstructibleCmr {
cmr: Cmr::fail(entropy),
}
}

fn const_word(word: Arc<Value>) -> Self {
Cmr::const_word(&word)
ConstructibleCmr {
cmr: Cmr::const_word(&word),
}
}
}

impl<X> DisconnectConstructible<X> for Cmr {
impl<X> DisconnectConstructible<X> for ConstructibleCmr {
fn disconnect(left: &Self, _right: &X) -> Result<Self, Error> {
Ok(Cmr::disconnect(*left))
Ok(ConstructibleCmr {
cmr: Cmr::disconnect(left.cmr),
})
}
}

impl<W> WitnessConstructible<W> for Cmr {
impl<W> WitnessConstructible<W> for ConstructibleCmr {
fn witness(_witness: W) -> Self {
Cmr::witness()
ConstructibleCmr {
cmr: Cmr::witness(),
}
}
}

impl<J: Jet> JetConstructible<J> for Cmr {
impl<J: Jet> JetConstructible<J> for ConstructibleCmr {
fn jet(jet: J) -> Self {
jet.cmr()
ConstructibleCmr { cmr: jet.cmr() }
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/policy/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,9 @@ impl<Pk: ToXOnlyPubkey> Policy<Pk> {

/// Return the CMR of the policy.
pub fn cmr(&self) -> Cmr {
self.serialize_no_witness()
self.serialize_no_witness::<crate::merkle::cmr::ConstructibleCmr>()
.expect("CMR is defined for asm fragment")
.cmr
}
}

Expand Down
7 changes: 4 additions & 3 deletions src/policy/serialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
//! Serialization of Policy as Simplicity

use crate::jet::{Elements, Jet};
use crate::merkle::cmr::ConstructibleCmr;
use crate::node::{CoreConstructible, JetConstructible, WitnessConstructible};
use crate::{Cmr, ConstructNode, ToXOnlyPubkey};
use crate::{FailEntropy, Value};
Expand All @@ -14,13 +15,13 @@ use std::sync::Arc;
pub trait AssemblyConstructible: Sized {
/// Construct the assembly fragment with the given CMR.
///
/// The construction fails if the CMR alone is not enough information to construct the type.
/// The construction fails if the CMR alone is not enough information to construct the object.
fn assembly(cmr: Cmr) -> Option<Self>;
}

impl AssemblyConstructible for Cmr {
impl AssemblyConstructible for ConstructibleCmr {
fn assembly(cmr: Cmr) -> Option<Self> {
Some(cmr)
Some(ConstructibleCmr { cmr })
}
}

Expand Down

0 comments on commit 9b0790e

Please sign in to comment.