From 01d88d08d4f329b47fd2c3ee2675cf56e121bc89 Mon Sep 17 00:00:00 2001 From: Christian Lewe Date: Thu, 24 Apr 2025 00:41:13 +0200 Subject: [PATCH 1/2] feat: Disable debug symbols --- bitcoind-tests/tests/common/test.rs | 4 ++-- fuzz/fuzz_targets/compile_parse_tree.rs | 2 +- fuzz/fuzz_targets/compile_text.rs | 2 +- src/compile.rs | 25 ++++++++++++++++----- src/lib.rs | 29 +++++++++++++++++++------ src/main.rs | 4 ++-- src/witness.rs | 4 ++-- 7 files changed, 50 insertions(+), 20 deletions(-) diff --git a/bitcoind-tests/tests/common/test.rs b/bitcoind-tests/tests/common/test.rs index 0e63842..5dd151f 100644 --- a/bitcoind-tests/tests/common/test.rs +++ b/bitcoind-tests/tests/common/test.rs @@ -53,7 +53,7 @@ impl<'a> TestCase<'a> { pub fn program_path>(mut self, path: P) -> Self { let text = std::fs::read_to_string(path).expect("path should be readable"); - let compiled = simfony::CompiledProgram::new(text.as_str(), simfony::Arguments::default()) + let compiled = simfony::CompiledProgram::new(text.as_str(), simfony::Arguments::default(), false) .expect("program should compile"); self.compiled = Some(compiled); self @@ -72,7 +72,7 @@ impl<'a> TestCase<'a> { .template .as_ref() .expect("template should exist") - .instantiate(arguments) + .instantiate(arguments, false) .expect("arguments should be consistent with the program"); self.compiled = Some(compiled); self diff --git a/fuzz/fuzz_targets/compile_parse_tree.rs b/fuzz/fuzz_targets/compile_parse_tree.rs index fd7319d..718d055 100644 --- a/fuzz/fuzz_targets/compile_parse_tree.rs +++ b/fuzz/fuzz_targets/compile_parse_tree.rs @@ -21,7 +21,7 @@ fuzz_target!(|data: &[u8]| { Err(..) => return, }; let simplicity_named_construct = ast_program - .compile(arguments) + .compile(arguments, false) .with_file("") .expect("AST should compile with given arguments"); let _simplicity_commit = named::to_commit_node(&simplicity_named_construct) diff --git a/fuzz/fuzz_targets/compile_text.rs b/fuzz/fuzz_targets/compile_text.rs index 2d8ed0f..5c0776d 100644 --- a/fuzz/fuzz_targets/compile_text.rs +++ b/fuzz/fuzz_targets/compile_text.rs @@ -46,7 +46,7 @@ fuzz_target!(|data: &[u8]| -> Corpus { Ok(arguments) => arguments, Err(..) => return Corpus::Reject, }; - let _ = template.instantiate(arguments); + let _ = template.instantiate(arguments, false); Corpus::Keep }); diff --git a/src/compile.rs b/src/compile.rs index 36d8351..41f01bb 100644 --- a/src/compile.rs +++ b/src/compile.rs @@ -66,6 +66,7 @@ struct Scope { call_tracker: Arc, /// Values for parameters inside the Simfony program. arguments: Arguments, + include_debug_symbols: bool, } impl Scope { @@ -77,12 +78,17 @@ impl Scope { /// /// The supplied `arguments` are consistent with the program's parameters. /// Call [`Arguments::is_consistent`] before calling this method! - pub fn new(call_tracker: Arc, arguments: Arguments) -> Self { + pub fn new( + call_tracker: Arc, + arguments: Arguments, + include_debug_symbols: bool, + ) -> Self { Self { variables: vec![vec![Pattern::Ignore]], ctx: simplicity::types::Context::new(), call_tracker, arguments, + include_debug_symbols, } } @@ -93,6 +99,7 @@ impl Scope { ctx: self.ctx.shallow_clone(), call_tracker: Arc::clone(&self.call_tracker), arguments: self.arguments.clone(), + include_debug_symbols: self.include_debug_symbols, } } @@ -193,12 +200,12 @@ impl Scope { span: &S, ) -> Result, RichError> { match self.call_tracker.get_cmr(span.as_ref()) { - Some(cmr) => { + Some(cmr) if self.include_debug_symbols => { let false_and_args = ProgNode::bit(self.ctx(), false).pair(args); let nop_assert = ProgNode::assertl_drop(body, cmr); false_and_args.comp(&nop_assert).with_span(span) } - None => args.comp(body).with_span(span), + _ => args.comp(body).with_span(span), } } @@ -246,8 +253,16 @@ impl Program { /// /// The supplied `arguments` are consistent with the program's parameters. /// Call [`Arguments::is_consistent`] before calling this method! - pub fn compile(&self, arguments: Arguments) -> Result { - let mut scope = Scope::new(Arc::clone(self.call_tracker()), arguments); + pub fn compile( + &self, + arguments: Arguments, + include_debug_symbols: bool, + ) -> Result { + let mut scope = Scope::new( + Arc::clone(self.call_tracker()), + arguments, + include_debug_symbols, + ); self.main().compile(&mut scope).map(PairBuilder::build) } } diff --git a/src/lib.rs b/src/lib.rs index 39240cf..4f12bec 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -71,7 +71,11 @@ impl TemplateProgram { /// /// The arguments are not consistent with the parameters of the program. /// Use [`TemplateProgram::parameters`] to see which parameters the program has. - pub fn instantiate(&self, arguments: Arguments) -> Result { + pub fn instantiate( + &self, + arguments: Arguments, + include_debug_symbols: bool, + ) -> Result { arguments .is_consistent(self.simfony.parameters()) .map_err(|error| error.to_string())?; @@ -79,7 +83,7 @@ impl TemplateProgram { debug_symbols: self.simfony.debug_symbols(self.file.as_ref()), simplicity: self .simfony - .compile(arguments) + .compile(arguments, include_debug_symbols) .with_file(Arc::clone(&self.file))?, witness_types: self.simfony.witness_types().shallow_clone(), }) @@ -112,8 +116,13 @@ impl CompiledProgram { /// /// - [`TemplateProgram::new`] /// - [`TemplateProgram::instantiate`] - pub fn new>>(s: Str, arguments: Arguments) -> Result { - TemplateProgram::new(s).and_then(|template| template.instantiate(arguments)) + pub fn new>>( + s: Str, + arguments: Arguments, + include_debug_symbols: bool, + ) -> Result { + TemplateProgram::new(s) + .and_then(|template| template.instantiate(arguments, include_debug_symbols)) } /// Access the debug symbols for the Simplicity target code. @@ -166,8 +175,9 @@ impl SatisfiedProgram { s: Str, arguments: Arguments, witness_values: WitnessValues, + include_debug_symbols: bool, ) -> Result { - let compiled = CompiledProgram::new(s, arguments)?; + let compiled = CompiledProgram::new(s, arguments, include_debug_symbols)?; compiled.satisfy(witness_values) } @@ -295,7 +305,7 @@ mod tests { } pub fn with_arguments(self, arguments: Arguments) -> TestCase { - let program = match self.program.instantiate(arguments) { + let program = match self.program.instantiate(arguments, true) { Ok(x) => x, Err(error) => panic!("{error}"), }; @@ -585,7 +595,12 @@ fn main() { assert!(my_true()); } "#; - match SatisfiedProgram::new(prog_text, Arguments::default(), WitnessValues::default()) { + match SatisfiedProgram::new( + prog_text, + Arguments::default(), + WitnessValues::default(), + false, + ) { Ok(_) => panic!("Accepted faulty program"), Err(error) => { if !error.contains("Expected expression of type `bool`, found type `()`") { diff --git a/src/main.rs b/src/main.rs index 04894a5..644f736 100644 --- a/src/main.rs +++ b/src/main.rs @@ -29,7 +29,7 @@ fn run() -> Result<(), String> { let prog_file = &args[1]; let prog_path = std::path::Path::new(prog_file); let prog_text = std::fs::read_to_string(prog_path).map_err(|e| e.to_string())?; - let compiled = CompiledProgram::new(prog_text, Arguments::default())?; + let compiled = CompiledProgram::new(prog_text, Arguments::default(), false)?; if args.len() >= 3 { let wit_file = &args[2]; @@ -73,7 +73,7 @@ fn run() -> Result<(), String> { let prog_file = &args[1]; let prog_path = std::path::Path::new(prog_file); let prog_text = std::fs::read_to_string(prog_path).map_err(|e| e.to_string())?; - let compiled = CompiledProgram::new(prog_text, Arguments::default())?; + let compiled = CompiledProgram::new(prog_text, Arguments::default(), false)?; let program_bytes = compiled.commit().encode_to_vec(); println!( diff --git a/src/witness.rs b/src/witness.rs index d47a190..7aa17ec 100644 --- a/src/witness.rs +++ b/src/witness.rs @@ -236,7 +236,7 @@ mod tests { WitnessName::from_str_unchecked("A"), Value::u16(42), )])); - match SatisfiedProgram::new(s, Arguments::default(), witness) { + match SatisfiedProgram::new(s, Arguments::default(), witness, false) { Ok(_) => panic!("Ill-typed witness assignment was falsely accepted"), Err(error) => assert_eq!( "Witness `A` was declared with type `u32` but its assigned value is of type `u16`", @@ -255,7 +255,7 @@ fn main() { assert!(jet::is_zero_32(f())); }"#; - match CompiledProgram::new(s, Arguments::default()) { + match CompiledProgram::new(s, Arguments::default(), false) { Ok(_) => panic!("Witness outside main was falsely accepted"), Err(error) => { assert!(error From 23cd66e78905049203947e9257d5d0201d2cbc34 Mon Sep 17 00:00:00 2001 From: Christian Lewe Date: Thu, 24 Apr 2025 00:43:17 +0200 Subject: [PATCH 2/2] fix: Clippy --- src/types.rs | 2 +- src/value.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/types.rs b/src/types.rs index 30c8505..22f5bf0 100644 --- a/src/types.rs +++ b/src/types.rs @@ -55,7 +55,7 @@ impl TypeInner { TypeInner::Tuple(elements) => match n_children_yielded { 0 => { f.write_str("(")?; - if 0 == elements.len() { + if elements.is_empty() { f.write_str(")")?; } Ok(()) diff --git a/src/value.rs b/src/value.rs index 276002e..6de7d07 100644 --- a/src/value.rs +++ b/src/value.rs @@ -145,7 +145,7 @@ impl UIntValue { return Err(Error::ExpressionTypeMismatch(ty.into(), bit_ty.into())); } - let byte_len = (bit_len.get() + 7) / 8; + let byte_len = bit_len.get().div_ceil(8); let mut bytes = Vec::with_capacity(byte_len); let padding_len = 8usize.saturating_sub(bit_len.get()); let padding = std::iter::repeat('0').take(padding_len);