From 454727ea1e4d070987592469fa031454dcdeef02 Mon Sep 17 00:00:00 2001 From: Christian Lewe Date: Wed, 1 Nov 2023 22:19:04 +0100 Subject: [PATCH 1/6] Clippy: Fix lints --- src/human_encoding/error.rs | 6 +++--- src/types/final_data.rs | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/human_encoding/error.rs b/src/human_encoding/error.rs index 2b67e867..5fa90b56 100644 --- a/src/human_encoding/error.rs +++ b/src/human_encoding/error.rs @@ -88,13 +88,13 @@ impl ErrorSet { pub fn add, E: Into>(&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>(&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. @@ -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()); } } diff --git a/src/types/final_data.rs b/src/types/final_data.rs index 30b7f36b..07cbbf82 100644 --- a/src/types/final_data.rs +++ b/src/types/final_data.rs @@ -61,7 +61,7 @@ impl Eq for Final {} impl PartialOrd for Final { fn partial_cmp(&self, other: &Self) -> Option { - self.tmr.partial_cmp(&other.tmr) + Some(self.cmp(other)) } } impl Ord for Final { From dd2e7f8c6a01fd104edbc428b218348fcb34b878 Mon Sep 17 00:00:00 2001 From: Christian Lewe Date: Mon, 6 Nov 2023 14:18:02 +0100 Subject: [PATCH 2/6] MSRV: Pin byteorder --- .github/workflows/main.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index e93abf96..5b086dad 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -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 From cbfa045a1834a9577094731a274d1dc8397be24f Mon Sep 17 00:00:00 2001 From: Christian Lewe Date: Sun, 26 Nov 2023 12:31:36 +0100 Subject: [PATCH 3/6] Fuzz: Update toolchain Regex crate requires rust 1.65+ --- .github/workflows/fuzz.yml | 2 +- fuzz/generate-files.sh | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/fuzz.yml b/.github/workflows/fuzz.yml index 73b60795..b66bf887 100644 --- a/.github/workflows/fuzz.yml +++ b/.github/workflows/fuzz.yml @@ -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 diff --git a/fuzz/generate-files.sh b/fuzz/generate-files.sh index cfeb6add..aee7506e 100755 --- a/fuzz/generate-files.sh +++ b/fuzz/generate-files.sh @@ -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 From 1afbd3534c322a9384005287e81d2ce433348f62 Mon Sep 17 00:00:00 2001 From: Christian Lewe Date: Sat, 25 Nov 2023 21:26:23 +0100 Subject: [PATCH 4/6] Types: Split sum and product We assert that the type is a sum / product and access its children at the same time. This is more precise than using split. --- src/types/final_data.rs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/types/final_data.rs b/src/types/final_data.rs index 07cbbf82..0667785d 100644 --- a/src/types/final_data.rs +++ b/src/types/final_data.rs @@ -222,6 +222,22 @@ impl Final { } } } + + /// Return both children, if the type is a sum type + pub fn split_sum(&self) -> Option<(Arc, Arc)> { + match &self.bound { + 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, Arc)> { + match &self.bound { + CompleteBound::Product(left, right) => Some((left.clone(), right.clone())), + _ => None, + } + } } #[cfg(test)] From 8cc05daac132c2ee93051e7a2fe8151eb7b3fce9 Mon Sep 17 00:00:00 2001 From: Christian Lewe Date: Sat, 25 Nov 2023 21:29:54 +0100 Subject: [PATCH 5/6] Types: Replace split with split_{sum, product} These methods are more precise and make the code more readable. --- src/merkle/amr.rs | 14 +++++++------- src/types/final_data.rs | 10 ---------- 2 files changed, 7 insertions(+), 17 deletions(-) diff --git a/src/merkle/amr.rs b/src/merkle/amr.rs index 34010c35..465d022a 100644 --- a/src/merkle/amr.rs +++ b/src/merkle/amr.rs @@ -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) @@ -69,7 +69,7 @@ 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) @@ -77,7 +77,7 @@ impl Amr { /// 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()) @@ -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()) @@ -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()) @@ -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()) diff --git a/src/types/final_data.rs b/src/types/final_data.rs index 0667785d..7239af05 100644 --- a/src/types/final_data.rs +++ b/src/types/final_data.rs @@ -213,16 +213,6 @@ impl Final { self.bound == CompleteBound::Unit } - /// Accessor for both children of the type, if they exist. - pub fn split(&self) -> Option<(Arc, Arc)> { - match &self.bound { - CompleteBound::Unit => None, - CompleteBound::Sum(left, right) | CompleteBound::Product(left, right) => { - Some((Arc::clone(left), Arc::clone(right))) - } - } - } - /// Return both children, if the type is a sum type pub fn split_sum(&self) -> Option<(Arc, Arc)> { match &self.bound { From e0a2caed3342b9694a47a89fc88a2b34a368c069 Mon Sep 17 00:00:00 2001 From: Christian Lewe Date: Sat, 25 Nov 2023 21:43:20 +0100 Subject: [PATCH 6/6] Bit Machine: Split types Simplifies the code --- src/bit_machine/mod.rs | 43 ++++++++++-------------------------------- 1 file changed, 10 insertions(+), 33 deletions(-) diff --git a/src/bit_machine/mod.rs b/src/bit_machine/mod.rs index 39cf132e..396e1645 100644 --- a/src/bit_machine/mod.rs +++ b/src/bit_machine/mod.rs @@ -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; @@ -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)); @@ -313,13 +303,7 @@ 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)); @@ -327,17 +311,10 @@ impl BitMachine { 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)