From 43ed0b224a94237ef5371f79d02c6c1e01ca623f Mon Sep 17 00:00:00 2001 From: aritkulova Date: Thu, 11 Jun 2026 17:05:16 +0300 Subject: [PATCH] added logical operations --- simf/logical_operations.simf | 17 +++++++ simf/mock/logical_operations_mock.simf | 27 +++++++++++ tests/logical_operations_test.rs | 65 ++++++++++++++++++++++++++ 3 files changed, 109 insertions(+) create mode 100644 simf/logical_operations.simf create mode 100644 simf/mock/logical_operations_mock.simf create mode 100644 tests/logical_operations_test.rs diff --git a/simf/logical_operations.simf b/simf/logical_operations.simf new file mode 100644 index 0000000..a8b94de --- /dev/null +++ b/simf/logical_operations.simf @@ -0,0 +1,17 @@ +/// Returns the result of the NOT operation on the provided value +fn not(bit: bool) -> bool { + ::into(jet::complement_1(::into(bit))) +} + +/// Returns the result of the OR operation on the provided values +fn or(a: bool, b: bool) -> bool { + ::into(jet::or_1(::into(a), ::into(b))) +} + +/// Returns the result of the AND operation on the provided values +fn and(a: bool, b: bool) -> bool { + ::into(jet::and_1(::into(a), ::into(b))) +} + +// todo: remove after module functionality is implemented +fn main() {} diff --git a/simf/mock/logical_operations_mock.simf b/simf/mock/logical_operations_mock.simf new file mode 100644 index 0000000..4ab4e22 --- /dev/null +++ b/simf/mock/logical_operations_mock.simf @@ -0,0 +1,27 @@ +// todo: switch to function import when available +fn not(bit: bool) -> bool { + ::into(jet::complement_1(::into(bit))) +} + +fn or(a: bool, b: bool) -> bool { + ::into(jet::or_1(::into(a), ::into(b))) +} + +fn and(a: bool, b: bool) -> bool { + ::into(jet::and_1(::into(a), ::into(b))) +} + +fn main() { + assert!(not(false)); + assert!(not((not(true)))); + + assert!(or(true, false)); + assert!(or(false, true)); + assert!(or(true, true)); + assert!(not(or(false, false))); + + assert!(and(true, true)); + assert!(not(and(true, false))); + assert!(not(and(false, true))); + assert!(not(and(false, false))); +} diff --git a/tests/logical_operations_test.rs b/tests/logical_operations_test.rs new file mode 100644 index 0000000..035211d --- /dev/null +++ b/tests/logical_operations_test.rs @@ -0,0 +1,65 @@ +use simplex::simplicityhl::elements::Script; + +use simplex::transaction::{FinalTransaction, PartialInput, ProgramInput, RequiredSignature}; + +use simplicityhl_std::artifacts::mock::logical_operations_mock::LogicalOperationsMockProgram; +use simplicityhl_std::artifacts::mock::logical_operations_mock::derived_logical_operations_mock::{ + LogicalOperationsMockArguments, LogicalOperationsMockWitness, +}; + +fn get_script(context: &simplex::TestContext) -> (LogicalOperationsMockProgram, Script) { + let arguments = LogicalOperationsMockArguments {}; + + let logical_operations_program = LogicalOperationsMockProgram::new(arguments); + + let logical_operations_script = + logical_operations_program.get_script_pubkey(context.get_network()); + + (logical_operations_program, logical_operations_script) +} + +fn fund_script(context: &simplex::TestContext) -> anyhow::Result<()> { + let signer = context.get_default_signer(); + + let (_, logical_operations_script) = get_script(context); + + let tx_receipt = signer.send(logical_operations_script.clone(), 50)?; + println!("Broadcast: {}", tx_receipt); + + Ok(()) +} + +fn spend_script(context: &simplex::TestContext) -> anyhow::Result<()> { + let signer = context.get_default_signer(); + let provider = context.get_default_provider(); + + let (logical_operations_program, logical_operations_script) = get_script(context); + + let asserts_utxos = provider.fetch_scripthash_utxos(&logical_operations_script)?; + + let mut ft = FinalTransaction::new(); + + let witness = LogicalOperationsMockWitness {}; + + ft.add_program_input( + PartialInput::new(asserts_utxos[0].clone()), + ProgramInput::new( + Box::new(logical_operations_program.as_ref().clone()), + Box::new(witness.clone()), + ), + RequiredSignature::None, + ); + + let tx_receipt = signer.broadcast(&ft)?; + println!("Broadcast: {}", tx_receipt); + + Ok(()) +} + +#[simplex::test] +fn logical_operations_test(context: simplex::TestContext) -> anyhow::Result<()> { + fund_script(&context)?; + spend_script(&context)?; + + Ok(()) +}