Skip to content

Commit

Permalink
Merge #180: Precise type splits
Browse files Browse the repository at this point in the history
e0a2cae Bit Machine: Split types (Christian Lewe)
8cc05da Types: Replace split with split_{sum, product} (Christian Lewe)
1afbd35 Types: Split sum and product (Christian Lewe)
cbfa045 Fuzz: Update toolchain (Christian Lewe)
dd2e7f8 MSRV: Pin byteorder (Christian Lewe)
454727e Clippy: Fix lints (Christian Lewe)

Pull request description:

  Split sum and product types with separate methods for better readability.

ACKs for top commit:
  apoelstra:
    ACK e0a2cae

Tree-SHA512: e36c41714c9a9c102e4069448696d002fbc844d677bbdf5ccfb83c0dbbf84ff68950b474c8310f23d7018f6171ba8098f9f1f80559cc3f8b3d99027208d6dbc4
  • Loading branch information
apoelstra committed Nov 27, 2023
2 parents 7b3d20a + e0a2cae commit b0cc9d2
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 52 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/fuzz.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ parse_human,
key: cache-${{ matrix.target }}-${{ hashFiles('**/Cargo.toml','**/Cargo.lock') }}
- uses: actions-rs/toolchain@v1
with:
toolchain: 1.64
toolchain: 1.65.0
override: true
profile: minimal
- name: fuzz
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ jobs:
cargo update -p proc-macro2 --precise 1.0.65
# 1.8.0 requires cargo 1.60+
cargo update -p regex --precise 1.7.0
# 1.5.0 uses edition 2021
cargo update -p byteorder --precise 1.4.3
fi
for f in $FEATURES; do echo "Features: $f" && cargo test --no-default-features --features="$f"; done
cd simplicity-sys
Expand Down
2 changes: 1 addition & 1 deletion fuzz/generate-files.sh
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ $(for name in $(listTargetNames); do echo "$name,"; done)
key: cache-\${{ matrix.target }}-\${{ hashFiles('**/Cargo.toml','**/Cargo.lock') }}
- uses: actions-rs/toolchain@v1
with:
toolchain: 1.64
toolchain: 1.65.0
override: true
profile: minimal
- name: fuzz
Expand Down
43 changes: 10 additions & 33 deletions src/bit_machine/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@
mod frame;

use crate::analysis;
use crate::dag::{DagLike, NoSharing};
use crate::jet::{Jet, JetFailed};
use crate::node::{self, RedeemNode};
use crate::{analysis, types};
use crate::{Cmr, FailEntropy, Value};
use frame::Frame;
use std::fmt;
Expand Down Expand Up @@ -255,25 +255,15 @@ impl BitMachine {
self.copy(size_a);
}
node::Inner::InjL(left) => {
let padl_b_c =
if let types::CompleteBound::Sum(b, _) = &ip.arrow().target.bound() {
ip.arrow().target.bit_width() - b.bit_width() - 1
} else {
unreachable!()
};

let (b, _c) = ip.arrow().target.split_sum().unwrap();
let padl_b_c = ip.arrow().target.bit_width() - b.bit_width() - 1;
self.write_bit(false);
self.skip(padl_b_c);
call_stack.push(CallStack::Goto(left));
}
node::Inner::InjR(left) => {
let padr_b_c =
if let types::CompleteBound::Sum(_, c) = &ip.arrow().target.bound() {
ip.arrow().target.bit_width() - c.bit_width() - 1
} else {
unreachable!()
};

let (_b, c) = ip.arrow().target.split_sum().unwrap();
let padr_b_c = ip.arrow().target.bit_width() - c.bit_width() - 1;
self.write_bit(true);
self.skip(padr_b_c);
call_stack.push(CallStack::Goto(left));
Expand Down Expand Up @@ -313,31 +303,18 @@ impl BitMachine {
}
node::Inner::Take(left) => call_stack.push(CallStack::Goto(left)),
node::Inner::Drop(left) => {
let size_a =
if let types::CompleteBound::Product(a, _) = &ip.arrow().source.bound() {
a.bit_width()
} else {
unreachable!()
};

let size_a = ip.arrow().source.split_product().unwrap().0.bit_width();
self.fwd(size_a);
call_stack.push(CallStack::Back(size_a));
call_stack.push(CallStack::Goto(left));
}
node::Inner::Case(..) | node::Inner::AssertL(..) | node::Inner::AssertR(..) => {
let choice_bit = self.read[self.read.len() - 1].peek_bit(&self.data);

let (size_a, size_b) = if let types::CompleteBound::Product(sum_a_b, _c) =
&ip.arrow().source.bound()
{
if let types::CompleteBound::Sum(a, b) = &sum_a_b.bound() {
(a.bit_width(), b.bit_width())
} else {
unreachable!()
}
} else {
unreachable!()
};
let (sum_a_b, _c) = ip.arrow().source.split_product().unwrap();
let (a, b) = sum_a_b.split_sum().unwrap();
let size_a = a.bit_width();
let size_b = b.bit_width();

match (ip.inner(), choice_bit) {
(node::Inner::Case(_, right), true)
Expand Down
6 changes: 3 additions & 3 deletions src/human_encoding/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,13 @@ impl ErrorSet {
pub fn add<P: Into<Position>, E: Into<Error>>(&mut self, position: P, err: E) {
self.errors
.entry(Some(position.into()))
.or_insert(vec![])
.or_default()
.push(err.into());
}

/// Adds an error to the error set.
pub fn add_no_position<E: Into<Error>>(&mut self, err: E) {
self.errors.entry(None).or_insert(vec![]).push(err.into());
self.errors.entry(None).or_default().push(err.into());
}

/// Merges another set of errors into the current set.
Expand All @@ -115,7 +115,7 @@ impl ErrorSet {
for (pos, errs) in &other.errors {
self.errors
.entry(*pos)
.or_insert(vec![])
.or_default()
.extend(errs.iter().cloned());
}
}
Expand Down
14 changes: 7 additions & 7 deletions src/merkle/amr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ impl Amr {
/// Produce a CMR for an injl combinator
pub fn injl(ty: &FinalArrow, child: Amr) -> Self {
let a = &ty.source;
let (b, c) = ty.target.split().unwrap();
let (b, c) = ty.target.split_sum().unwrap();
Self::INJL_IV
.update(a.tmr().into(), b.tmr().into())
.update(c.tmr().into(), child)
Expand All @@ -69,15 +69,15 @@ impl Amr {
/// Produce a CMR for an injr combinator
pub fn injr(ty: &FinalArrow, child: Amr) -> Self {
let a = &ty.source;
let (b, c) = ty.target.split().unwrap();
let (b, c) = ty.target.split_sum().unwrap();
Self::INJR_IV
.update(a.tmr().into(), b.tmr().into())
.update(c.tmr().into(), child)
}

/// Produce a CMR for a take combinator
pub fn take(ty: &FinalArrow, child: Amr) -> Self {
let (a, b) = ty.source.split().unwrap();
let (a, b) = ty.source.split_product().unwrap();
let c = &ty.target;
Self::TAKE_IV
.update(a.tmr().into(), b.tmr().into())
Expand All @@ -86,7 +86,7 @@ impl Amr {

/// Produce a CMR for a drop combinator
pub fn drop(ty: &FinalArrow, child: Amr) -> Self {
let (a, b) = ty.source.split().unwrap();
let (a, b) = ty.source.split_product().unwrap();
let c = &ty.target;
Self::DROP_IV
.update(a.tmr().into(), b.tmr().into())
Expand All @@ -105,8 +105,8 @@ impl Amr {
}

fn case_helper(iv: Amr, ty: &FinalArrow, left: Amr, right: Amr) -> Self {
let (sum_a_b, c) = ty.source.split().unwrap();
let (a, b) = sum_a_b.split().unwrap();
let (sum_a_b, c) = ty.source.split_product().unwrap();
let (a, b) = sum_a_b.split_sum().unwrap();
let d = &ty.target;
iv.update(a.tmr().into(), b.tmr().into())
.update(c.tmr().into(), d.tmr().into())
Expand Down Expand Up @@ -148,7 +148,7 @@ impl Amr {
/// Produce a CMR for a disconnect combinator
pub fn disconnect(ty: &FinalArrow, right_arrow: &FinalArrow, left: Amr, right: Amr) -> Self {
let a = &ty.source;
let (b, d) = ty.target.split().unwrap();
let (b, d) = ty.target.split_product().unwrap();
let c = &right_arrow.source;
Self::DISCONNECT_IV
.update(a.tmr().into(), b.tmr().into())
Expand Down
20 changes: 13 additions & 7 deletions src/types/final_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ impl Eq for Final {}

impl PartialOrd for Final {
fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> {
self.tmr.partial_cmp(&other.tmr)
Some(self.cmp(other))
}
}
impl Ord for Final {
Expand Down Expand Up @@ -213,13 +213,19 @@ impl Final {
self.bound == CompleteBound::Unit
}

/// Accessor for both children of the type, if they exist.
pub fn split(&self) -> Option<(Arc<Self>, Arc<Self>)> {
/// Return both children, if the type is a sum type
pub fn split_sum(&self) -> Option<(Arc<Self>, Arc<Self>)> {
match &self.bound {
CompleteBound::Unit => None,
CompleteBound::Sum(left, right) | CompleteBound::Product(left, right) => {
Some((Arc::clone(left), Arc::clone(right)))
}
CompleteBound::Sum(left, right) => Some((left.clone(), right.clone())),
_ => None,
}
}

/// Return both children, if the type is a product type
pub fn split_product(&self) -> Option<(Arc<Self>, Arc<Self>)> {
match &self.bound {
CompleteBound::Product(left, right) => Some((left.clone(), right.clone())),
_ => None,
}
}
}
Expand Down

0 comments on commit b0cc9d2

Please sign in to comment.