From 42544af7990219850cd1f863e384f415c6de1e77 Mon Sep 17 00:00:00 2001 From: microproofs Date: Fri, 30 Jun 2023 23:06:38 -0400 Subject: [PATCH] fix: safe encode bits to check for 2^num_bits <= byte we are encoding fix: I thought namedDeBruijn takes advantage of Binder for encoding and decoding. It does not... fix: Debruijn was being converted to NamedDeBruijn incorrectly --- crates/flat-rs/src/encode/encoder.rs | 2 +- crates/uplc/src/debruijn.rs | 2 +- crates/uplc/src/flat.rs | 5 +++-- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/crates/flat-rs/src/encode/encoder.rs b/crates/flat-rs/src/encode/encoder.rs index 03cdd429..67044f0b 100644 --- a/crates/flat-rs/src/encode/encoder.rs +++ b/crates/flat-rs/src/encode/encoder.rs @@ -320,6 +320,6 @@ impl Encoder { self.buffer.push(chunk.len() as u8); self.buffer.extend(chunk); } - self.buffer.push(0); + self.buffer.push(0_u8); } } diff --git a/crates/uplc/src/debruijn.rs b/crates/uplc/src/debruijn.rs index d6b3374c..d0f94584 100644 --- a/crates/uplc/src/debruijn.rs +++ b/crates/uplc/src/debruijn.rs @@ -251,7 +251,7 @@ impl Converter { match term { Term::Var(name) => Term::Var( NamedDeBruijn { - text: format!("i_{name}"), + text: "i".to_string(), index: *name.as_ref(), } .into(), diff --git a/crates/uplc/src/flat.rs b/crates/uplc/src/flat.rs index ff36a9b6..a84d1958 100644 --- a/crates/uplc/src/flat.rs +++ b/crates/uplc/src/flat.rs @@ -676,6 +676,7 @@ impl<'b> Decode<'b> for NamedDeBruijn { impl<'b> Binder<'b> for NamedDeBruijn { fn binder_encode(&self, e: &mut Encoder) -> Result<(), en::Error> { self.text.encode(e)?; + self.index.encode(e)?; Ok(()) } @@ -683,7 +684,7 @@ impl<'b> Binder<'b> for NamedDeBruijn { fn binder_decode(d: &mut Decoder) -> Result { Ok(NamedDeBruijn { text: String::decode(d)?, - index: DeBruijn::new(0), + index: DeBruijn::decode(d)?, }) } @@ -778,7 +779,7 @@ fn decode_term_tag(d: &mut Decoder) -> Result { } fn safe_encode_bits(num_bits: u32, byte: u8, e: &mut Encoder) -> Result<(), en::Error> { - if 2_u8.pow(num_bits) < byte { + if 2_u8.pow(num_bits) <= byte { Err(en::Error::Message(format!( "Overflow detected, cannot fit {byte} in {num_bits} bits." )))