Skip to content

Commit

Permalink
feat: Expose debug symbols of satisfied program
Browse files Browse the repository at this point in the history
Return a plain-old-data type that wraps the Simplicity target code and
its debug symbols. When we add a "release" mode to the Simfony compiler,
we won't return debug symbols.
  • Loading branch information
uncomputable committed Sep 16, 2024
1 parent 0ff3f2c commit bfb6a54
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 9 deletions.
2 changes: 1 addition & 1 deletion bitcoind-tests/tests/test_arith.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ pub fn test_simplicity(cl: &ElementsD, program_file: &str, witness_file: &str) {
let program_text = std::fs::read_to_string(program_path).unwrap();
let witness_text = std::fs::read_to_string(witness_path).unwrap();
let witness_values = serde_json::from_str::<WitnessValues>(&witness_text).unwrap();
let program = simfony::satisfy(&program_text, &witness_values).unwrap();
let program = simfony::satisfy(&program_text, &witness_values).unwrap().simplicity;

let secp = secp256k1::Secp256k1::new();
let internal_key = XOnlyPublicKey::from_str("f5919fa64ce45f8306849072b26c1bfdd2937e6b81774796ff372bd1eb5362d2").unwrap();
Expand Down
26 changes: 19 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ use simplicity::{jet::Elements, CommitNode, RedeemNode};
pub extern crate simplicity;
pub use simplicity::elements;

use crate::debug::DebugSymbols;
use crate::error::WithFile;
use crate::parse::ParseFromStr;
use crate::witness::WitnessValues;
Expand All @@ -38,10 +39,16 @@ pub fn compile(prog_text: &str) -> Result<Arc<CommitNode<Elements>>, String> {
Ok(simplicity_commit)
}

pub fn satisfy(
prog_text: &str,
witness: &WitnessValues,
) -> Result<Arc<RedeemNode<Elements>>, String> {
/// A satisfied Simfony program, compiled to Simplicity.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct SatisfiedProgram {
/// Simplicity target code, including witness data.
pub simplicity: Arc<RedeemNode<Elements>>,
/// Debug symbols for the Simplicity target code.
pub debug_symbols: DebugSymbols,
}

pub fn satisfy(prog_text: &str, witness: &WitnessValues) -> Result<SatisfiedProgram, String> {
let parse_program = parse::Program::parse_from_str(prog_text)?;
let ast_program = ast::Program::analyze(&parse_program).with_file(prog_text)?;
let simplicity_named_construct = ast_program.compile().with_file(prog_text)?;
Expand All @@ -50,7 +57,12 @@ pub fn satisfy(
.map_err(|e| e.to_string())?;

let simplicity_witness = named::to_witness_node(&simplicity_named_construct, witness);
simplicity_witness.finalize().map_err(|e| e.to_string())
let simplicity_redeem = simplicity_witness.finalize().map_err(|e| e.to_string())?;

Ok(SatisfiedProgram {
simplicity: simplicity_redeem,
debug_symbols: ast_program.debug_symbols(prog_text),
})
}

/// Recursively implement [`PartialEq`], [`Eq`] and [`std::hash::Hash`]
Expand Down Expand Up @@ -159,12 +171,12 @@ mod tests {
}

pub fn with_witness_values(self, witness_values: &WitnessValues) -> TestCase<Compiled> {
let redeem_program = match satisfy(self.program.0.as_ref(), witness_values) {
let program = match satisfy(self.program.0.as_ref(), witness_values) {
Ok(x) => x,
Err(error) => panic!("{error}"),
};
TestCase {
program: Compiled(redeem_program),
program: Compiled(program.simplicity),
lock_time: self.lock_time,
sequence: self.sequence,
}
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ fn run() -> Result<(), String> {
let witness = serde_json::from_str::<WitnessValues>(&wit_text).unwrap();

let program = satisfy(&prog_text, &witness)?;
let (program_bytes, witness_bytes) = program.encode_to_vec();
let (program_bytes, witness_bytes) = program.simplicity.encode_to_vec();
println!(
"Program:\n{}",
Base64Display::new(&program_bytes, &STANDARD)
Expand Down

0 comments on commit bfb6a54

Please sign in to comment.