Skip to content

Optionally disable debug symbols #124

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
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
4 changes: 2 additions & 2 deletions bitcoind-tests/tests/common/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ impl<'a> TestCase<'a> {

pub fn program_path<P: AsRef<std::path::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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion fuzz/fuzz_targets/compile_parse_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion fuzz/fuzz_targets/compile_text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
});
25 changes: 20 additions & 5 deletions src/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ struct Scope {
call_tracker: Arc<CallTracker>,
/// Values for parameters inside the Simfony program.
arguments: Arguments,
include_debug_symbols: bool,
}

impl Scope {
Expand All @@ -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<CallTracker>, arguments: Arguments) -> Self {
pub fn new(
call_tracker: Arc<CallTracker>,
arguments: Arguments,
include_debug_symbols: bool,
) -> Self {
Self {
variables: vec![vec![Pattern::Ignore]],
ctx: simplicity::types::Context::new(),
call_tracker,
arguments,
include_debug_symbols,
}
}

Expand All @@ -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,
}
}

Expand Down Expand Up @@ -193,12 +200,12 @@ impl Scope {
span: &S,
) -> Result<PairBuilder<ProgNode>, 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),
}
}

Expand Down Expand Up @@ -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<ProgNode, RichError> {
let mut scope = Scope::new(Arc::clone(self.call_tracker()), arguments);
pub fn compile(
&self,
arguments: Arguments,
include_debug_symbols: bool,
) -> Result<ProgNode, RichError> {
let mut scope = Scope::new(
Arc::clone(self.call_tracker()),
arguments,
include_debug_symbols,
);
self.main().compile(&mut scope).map(PairBuilder::build)
}
}
Expand Down
29 changes: 22 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,19 @@ 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<CompiledProgram, String> {
pub fn instantiate(
&self,
arguments: Arguments,
include_debug_symbols: bool,
) -> Result<CompiledProgram, String> {
arguments
.is_consistent(self.simfony.parameters())
.map_err(|error| error.to_string())?;
Ok(CompiledProgram {
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(),
})
Expand Down Expand Up @@ -112,8 +116,13 @@ impl CompiledProgram {
///
/// - [`TemplateProgram::new`]
/// - [`TemplateProgram::instantiate`]
pub fn new<Str: Into<Arc<str>>>(s: Str, arguments: Arguments) -> Result<Self, String> {
TemplateProgram::new(s).and_then(|template| template.instantiate(arguments))
pub fn new<Str: Into<Arc<str>>>(
s: Str,
arguments: Arguments,
include_debug_symbols: bool,
) -> Result<Self, String> {
TemplateProgram::new(s)
.and_then(|template| template.instantiate(arguments, include_debug_symbols))
}

/// Access the debug symbols for the Simplicity target code.
Expand Down Expand Up @@ -166,8 +175,9 @@ impl SatisfiedProgram {
s: Str,
arguments: Arguments,
witness_values: WitnessValues,
include_debug_symbols: bool,
) -> Result<Self, String> {
let compiled = CompiledProgram::new(s, arguments)?;
let compiled = CompiledProgram::new(s, arguments, include_debug_symbols)?;
compiled.satisfy(witness_values)
}

Expand Down Expand Up @@ -295,7 +305,7 @@ mod tests {
}

pub fn with_arguments(self, arguments: Arguments) -> TestCase<CompiledProgram> {
let program = match self.program.instantiate(arguments) {
let program = match self.program.instantiate(arguments, true) {
Ok(x) => x,
Err(error) => panic!("{error}"),
};
Expand Down Expand Up @@ -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 `()`") {
Expand Down
4 changes: 2 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down Expand Up @@ -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!(
Expand Down
2 changes: 1 addition & 1 deletion src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ impl<A> TypeInner<A> {
TypeInner::Tuple(elements) => match n_children_yielded {
0 => {
f.write_str("(")?;
if 0 == elements.len() {
if elements.is_empty() {
f.write_str(")")?;
}
Ok(())
Expand Down
2 changes: 1 addition & 1 deletion src/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions src/witness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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`",
Expand All @@ -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
Expand Down
Loading