Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions src/human_encoding/named_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ use crate::node::{
self, Commit, CommitData, CommitNode, Construct, ConstructData, Constructible as _, Converter,
CoreConstructible as _, Inner, NoDisconnect, NoWitness, Node,
};
use crate::types;
use crate::types::arrow::{Arrow, FinalArrow};
use crate::types::{self, Arrow, FinalArrow};
use crate::{encode, ConstructNode, Value};
use crate::{BitWriter, Cmr, Ihr};

Expand Down
2 changes: 1 addition & 1 deletion src/merkle/amr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use crate::jet::Jet;
use crate::merkle::compact_value;
use crate::types::arrow::FinalArrow;
use crate::types::FinalArrow;
use crate::value::Word;
use crate::{Cmr, Tmr, Value};
use hashes::sha256::Midstate;
Expand Down
50 changes: 27 additions & 23 deletions src/merkle/cmr.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
// SPDX-License-Identifier: CC0-1.0

use crate::jet::Jet;
#[cfg(feature = "elements")]
use crate::node::{CoreConstructible, DisconnectConstructible, WitnessConstructible};
use crate::types::{self, Error};
#[cfg(feature = "elements")]
use crate::types::{self, Arrow, Error};
use crate::value::Word;
use crate::{FailEntropy, Tmr};
use hashes::sha256::Midstate;
Expand Down Expand Up @@ -254,118 +256,118 @@ impl 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.
#[cfg(feature = "elements")] // only used by policy module
pub struct ConstructibleCmr<'brand> {
pub cmr: Cmr,
pub inference_context: types::Context<'brand>,
pub arrow: Arrow<'brand>,
}

#[cfg(feature = "elements")] // only used by policy module
impl<'brand> CoreConstructible<'brand> for ConstructibleCmr<'brand> {
fn iden(inference_context: &types::Context<'brand>) -> Self {
ConstructibleCmr {
cmr: Cmr::iden(),
inference_context: inference_context.shallow_clone(),
arrow: Arrow::iden(inference_context),
}
}

fn unit(inference_context: &types::Context<'brand>) -> Self {
ConstructibleCmr {
cmr: Cmr::unit(),
inference_context: inference_context.shallow_clone(),
arrow: Arrow::unit(inference_context),
}
}

fn injl(child: &Self) -> Self {
ConstructibleCmr {
cmr: Cmr::injl(child.cmr),
inference_context: child.inference_context.shallow_clone(),
arrow: Arrow::injl(child.arrow()),
}
}

fn injr(child: &Self) -> Self {
ConstructibleCmr {
cmr: Cmr::injr(child.cmr),
inference_context: child.inference_context.shallow_clone(),
arrow: Arrow::injr(child.arrow()),
}
}

fn take(child: &Self) -> Self {
ConstructibleCmr {
cmr: Cmr::take(child.cmr),
inference_context: child.inference_context.shallow_clone(),
arrow: Arrow::take(child.arrow()),
}
}

fn drop_(child: &Self) -> Self {
ConstructibleCmr {
cmr: Cmr::drop(child.cmr),
inference_context: child.inference_context.shallow_clone(),
arrow: Arrow::drop_(child.arrow()),
}
}

fn comp(left: &Self, right: &Self) -> Result<Self, Error> {
left.inference_context.check_eq(&right.inference_context)?;
Ok(ConstructibleCmr {
cmr: Cmr::comp(left.cmr, right.cmr),
inference_context: left.inference_context.shallow_clone(),
arrow: Arrow::comp(left.arrow(), right.arrow())?,
})
}

fn case(left: &Self, right: &Self) -> Result<Self, Error> {
left.inference_context.check_eq(&right.inference_context)?;
Ok(ConstructibleCmr {
cmr: Cmr::case(left.cmr, right.cmr),
inference_context: left.inference_context.shallow_clone(),
arrow: Arrow::case(left.arrow(), right.arrow())?,
})
}

fn assertl(left: &Self, right: Cmr) -> Result<Self, Error> {
Ok(ConstructibleCmr {
cmr: Cmr::case(left.cmr, right),
inference_context: left.inference_context.shallow_clone(),
arrow: Arrow::assertl(left.arrow())?,
})
}

fn assertr(left: Cmr, right: &Self) -> Result<Self, Error> {
Ok(ConstructibleCmr {
cmr: Cmr::case(left, right.cmr),
inference_context: right.inference_context.shallow_clone(),
arrow: Arrow::assertr(right.arrow())?,
})
}

fn pair(left: &Self, right: &Self) -> Result<Self, Error> {
left.inference_context.check_eq(&right.inference_context)?;
Ok(ConstructibleCmr {
cmr: Cmr::pair(left.cmr, right.cmr),
inference_context: left.inference_context.shallow_clone(),
arrow: Arrow::pair(left.arrow(), right.arrow())?,
})
}

fn fail(inference_context: &types::Context<'brand>, entropy: FailEntropy) -> Self {
ConstructibleCmr {
cmr: Cmr::fail(entropy),
inference_context: inference_context.shallow_clone(),
arrow: Arrow::fail(inference_context),
}
}

fn const_word(inference_context: &types::Context<'brand>, word: Word) -> Self {
ConstructibleCmr {
cmr: Cmr::const_word(&word),
inference_context: inference_context.shallow_clone(),
arrow: Arrow::const_word(inference_context, &word),
}
}

fn jet(inference_context: &types::Context<'brand>, jet: &dyn Jet) -> Self {
ConstructibleCmr {
cmr: jet.cmr(),
inference_context: inference_context.shallow_clone(),
arrow: Arrow::jet(inference_context, jet),
}
}

fn inference_context(&self) -> &types::Context<'brand> {
&self.inference_context
fn arrow(&self) -> &Arrow<'brand> {
&self.arrow
}
}

#[cfg(feature = "elements")] // only used by policy module
impl<'brand, X> DisconnectConstructible<'brand, X> for ConstructibleCmr<'brand> {
// Specifically with disconnect we don't check for consistency between the
// type inference context of the disconnected node, if any, and that of
Expand All @@ -374,16 +376,17 @@ impl<'brand, X> DisconnectConstructible<'brand, X> for ConstructibleCmr<'brand>
fn disconnect(left: &Self, _right: &X) -> Result<Self, Error> {
Ok(ConstructibleCmr {
cmr: Cmr::disconnect(left.cmr),
inference_context: left.inference_context.shallow_clone(),
arrow: left.arrow.shallow_clone(),
})
}
}

#[cfg(feature = "elements")] // only used by policy module
impl<'brand, W> WitnessConstructible<'brand, W> for ConstructibleCmr<'brand> {
fn witness(inference_context: &types::Context<'brand>, _witness: W) -> Self {
ConstructibleCmr {
arrow: Arrow::witness(inference_context),
cmr: Cmr::witness(),
inference_context: inference_context.shallow_clone(),
}
}
}
Expand All @@ -393,6 +396,7 @@ mod tests {
use super::*;

use crate::node::{ConstructNode, CoreConstructible};
use crate::types;

use std::str::FromStr;
use std::sync::Arc;
Expand Down
2 changes: 1 addition & 1 deletion src/merkle/ihr.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// SPDX-License-Identifier: CC0-1.0

use crate::jet::Jet;
use crate::types::arrow::FinalArrow;
use crate::types::FinalArrow;
use crate::value::Word;
use crate::{Cmr, Tmr, Value};
use hashes::sha256::Midstate;
Expand Down
38 changes: 27 additions & 11 deletions src/node/commit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

use crate::dag::{DagLike, MaxSharing, NoSharing, PostOrderIterItem};
use crate::jet::Jet;
use crate::types::arrow::{Arrow, FinalArrow};
use crate::types::{Arrow, FinalArrow};
use crate::{encode, types, Value};
use crate::{Amr, BitIter, BitWriter, Cmr, DecodeError, Ihr, Imr};

use super::{
Construct, ConstructData, ConstructNode, Constructible, Converter, Inner, Marker, NoDisconnect,
NoWitness, Node, Redeem, RedeemNode,
Construct, ConstructData, ConstructNode, Converter, Inner, Marker, NoDisconnect, NoWitness,
Node, Redeem, RedeemNode,
};

use std::io;
Expand Down Expand Up @@ -213,14 +213,30 @@ impl CommitNode {
&Option<Value>,
>,
) -> Result<ConstructData<'brand>, Self::Error> {
let inner = inner
.map(|node| node.arrow())
.map_disconnect(|maybe_node| maybe_node.as_ref().map(|node| node.arrow()));
let inner = inner.disconnect_as_ref(); // lol sigh rust
Ok(ConstructData::new(Arrow::from_inner(
self.inference_context,
inner,
)?))
use crate::node::DisconnectConstructible as _;

let new_arrow = match inner {
Inner::Iden => Arrow::iden(self.inference_context),
Inner::Unit => Arrow::unit(self.inference_context),
Inner::InjL(child) => Arrow::injl(child.arrow()),
Inner::InjR(child) => Arrow::injr(child.arrow()),
Inner::Take(child) => Arrow::take(child.arrow()),
Inner::Drop(child) => Arrow::drop_(child.arrow()),
Inner::Comp(lft, rgt) => Arrow::comp(lft.arrow(), rgt.arrow())?,
Inner::Case(lft, rgt) => Arrow::case(lft.arrow(), rgt.arrow())?,
Inner::Pair(lft, rgt) => Arrow::pair(lft.arrow(), rgt.arrow())?,
Inner::Disconnect(lft, rgt) => {
Arrow::disconnect(lft.arrow(), &rgt.as_ref().map(|node| node.arrow()))?
}
Inner::AssertL(lft, _) => Arrow::assertl(lft.arrow())?,
Inner::AssertR(_, rgt) => Arrow::assertr(rgt.arrow())?,
Inner::Witness(_) => Arrow::witness(self.inference_context),
Inner::Fail(_) => Arrow::fail(self.inference_context),
Inner::Jet(ref jet) => Arrow::jet(self.inference_context, jet.as_ref()),
Inner::Word(ref word) => Arrow::const_word(self.inference_context, word),
};

Ok(ConstructData::new(new_arrow))
}
}

Expand Down
22 changes: 11 additions & 11 deletions src/node/construct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use crate::dag::{InternalSharing, PostOrderIterItem};
use crate::jet::{Jet, JetEnvironment};
use crate::types::{self, arrow::Arrow};
use crate::types::{self, Arrow};
use crate::{encode, BitIter, BitWriter, Cmr, FailEntropy, FinalizeError, RedeemNode, Value, Word};

use std::io;
Expand Down Expand Up @@ -330,15 +330,15 @@ impl<'brand> CoreConstructible<'brand> for ConstructData<'brand> {
})
}

fn assertl(left: &Self, right: Cmr) -> Result<Self, types::Error> {
fn assertl(left: &Self, _: Cmr) -> Result<Self, types::Error> {
Ok(ConstructData {
arrow: Arrow::assertl(&left.arrow, right)?,
arrow: Arrow::assertl(&left.arrow)?,
})
}

fn assertr(left: Cmr, right: &Self) -> Result<Self, types::Error> {
fn assertr(_: Cmr, right: &Self) -> Result<Self, types::Error> {
Ok(ConstructData {
arrow: Arrow::assertr(left, &right.arrow)?,
arrow: Arrow::assertr(&right.arrow)?,
})
}

Expand All @@ -348,15 +348,15 @@ impl<'brand> CoreConstructible<'brand> for ConstructData<'brand> {
})
}

fn fail(inference_context: &types::Context<'brand>, entropy: FailEntropy) -> Self {
fn fail(inference_context: &types::Context<'brand>, _: FailEntropy) -> Self {
ConstructData {
arrow: Arrow::fail(inference_context, entropy),
arrow: Arrow::fail(inference_context),
}
}

fn const_word(inference_context: &types::Context<'brand>, word: Word) -> Self {
ConstructData {
arrow: Arrow::const_word(inference_context, word),
arrow: Arrow::const_word(inference_context, &word),
}
}

Expand All @@ -366,8 +366,8 @@ impl<'brand> CoreConstructible<'brand> for ConstructData<'brand> {
}
}

fn inference_context(&self) -> &types::Context<'brand> {
self.arrow.inference_context()
fn arrow(&self) -> &Arrow<'brand> {
&self.arrow
}
}

Expand All @@ -388,7 +388,7 @@ impl<'brand> DisconnectConstructible<'brand, Option<Arc<ConstructNode<'brand>>>>
impl<'brand> WitnessConstructible<'brand, Option<Value>> for ConstructData<'brand> {
fn witness(inference_context: &types::Context<'brand>, _witness: Option<Value>) -> Self {
ConstructData {
arrow: Arrow::witness(inference_context, NoWitness),
arrow: Arrow::witness(inference_context),
}
}
}
Expand Down
Loading
Loading