From 919c38ff9219e4b4986575b99a30032d6e86bd2e Mon Sep 17 00:00:00 2001 From: Tony Stark Date: Wed, 24 Jul 2024 16:53:15 -0500 Subject: [PATCH 1/5] feat: op_depth --- src/compiler.cairo | 2 + src/opcodes/opcodes.cairo | 8 +++- src/opcodes/tests/test_opcodes.cairo | 55 ++++++++++++++++++++++++++++ 3 files changed, 64 insertions(+), 1 deletion(-) diff --git a/src/compiler.cairo b/src/compiler.cairo index a9f388aa..cb61a851 100644 --- a/src/compiler.cairo +++ b/src/compiler.cairo @@ -1,3 +1,4 @@ +use core::dict::Felt252DictTrait; use shinigami::opcodes::Opcode; // Compiler that takes a Bitcoin Script program and compiles it into a bytecode @@ -21,6 +22,7 @@ pub impl CompilerTraitImpl of CompilerTrait { opcodes.insert('OP_0', Opcode::OP_0); opcodes.insert('OP_1', Opcode::OP_1); opcodes.insert('OP_ADD', Opcode::OP_ADD); + opcodes.insert('OP_DEPTH', Opcode::OP_DEPTH); Compiler { opcodes } } diff --git a/src/opcodes/opcodes.cairo b/src/opcodes/opcodes.cairo index 6a516e7b..8f458614 100644 --- a/src/opcodes/opcodes.cairo +++ b/src/opcodes/opcodes.cairo @@ -2,6 +2,7 @@ pub mod Opcode { pub const OP_0: u8 = 0; pub const OP_1: u8 = 81; pub const OP_ADD: u8 = 147; + pub const OP_DEPTH: u8 = 116; use shinigami::engine::Engine; use shinigami::stack::ScriptStackTrait; @@ -123,7 +124,7 @@ pub mod Opcode { 113 => not_implemented(ref engine), 114 => not_implemented(ref engine), 115 => not_implemented(ref engine), - 116 => not_implemented(ref engine), + 116 => opcode_depth(ref engine), 117 => not_implemented(ref engine), 118 => not_implemented(ref engine), 119 => not_implemented(ref engine), @@ -174,6 +175,11 @@ pub mod Opcode { engine.dstack.push_int(a + b); } + fn opcode_depth(ref engine: Engine) { + let depth: i64 = engine.dstack.len().into(); + engine.dstack.push_int(depth); + } + fn not_implemented(ref engine: Engine) { panic!("Opcode not implemented"); } diff --git a/src/opcodes/tests/test_opcodes.cairo b/src/opcodes/tests/test_opcodes.cairo index 1124ecba..1a2b5303 100644 --- a/src/opcodes/tests/test_opcodes.cairo +++ b/src/opcodes/tests/test_opcodes.cairo @@ -51,3 +51,58 @@ fn test_op_add() { let expected_stack = array!["\0\0\0\0\0\0\0\x02"]; assert_eq!(dstack, expected_stack.span(), "Stack is not equal to expected"); } + +#[test] +fn test_op_depth() { + // Test case 1: Empty stack + let program = "OP_DEPTH"; + let mut compiler = CompilerTraitImpl::new(); + let bytecode = compiler.compile(program); + let mut engine = EngineTraitImpl::new(bytecode); + + let res = engine.step(); + assert!(res, "Execution of step failed"); + + let dstack = engine.get_dstack(); + assert_eq!(dstack.len(), 1, "Stack length is not 1"); + + let expected_stack = array!["\0\0\0\0\0\0\0\0"]; // Representing 0 + assert_eq!(dstack, expected_stack.span(), "Stack is not equal to expected for empty stack"); + + // Test case 2: Stack with one item + let program = "OP_1 OP_DEPTH"; + let mut compiler = CompilerTraitImpl::new(); // Create a new compiler instance + let bytecode = compiler.compile(program); + let mut engine = EngineTraitImpl::new(bytecode); + + let _ = engine.step(); + let res = engine.step(); + assert!(res, "Execution of step failed"); + + let dstack = engine.get_dstack(); + assert_eq!(dstack.len(), 2, "Stack length is not 2"); + + let expected_stack = array!["\0\0\0\0\0\0\0\x01", "\0\0\0\0\0\0\0\x01"]; // [1, 1] + assert_eq!(dstack, expected_stack.span(), "Stack is not equal to expected for one item"); + + // Test case 3: Stack with multiple items + let program = "OP_1 OP_1 OP_ADD OP_1 OP_DEPTH"; + let mut compiler = CompilerTraitImpl::new(); // Create a new compiler instance + let bytecode = compiler.compile(program); + let mut engine = EngineTraitImpl::new(bytecode); + + let _ = engine.step(); + let _ = engine.step(); + let _ = engine.step(); + let _ = engine.step(); + let res = engine.step(); + assert!(res, "Execution of step failed"); + + let dstack = engine.get_dstack(); + assert_eq!(dstack.len(), 3, "Stack length is not 3"); + + let expected_stack = array![ + "\0\0\0\0\0\0\0\x02", "\0\0\0\0\0\0\0\x01", "\0\0\0\0\0\0\0\x02" + ]; // [2, 1, 2] + assert_eq!(dstack, expected_stack.span(), "Stack is not equal to expected for multiple items"); +} From 18a8eb4afb3bd4bbe924a60aac3e977c48207263 Mon Sep 17 00:00:00 2001 From: Tony Stark Date: Wed, 24 Jul 2024 17:20:19 -0500 Subject: [PATCH 2/5] minor clean --- src/opcodes/tests/test_opcodes.cairo | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/src/opcodes/tests/test_opcodes.cairo b/src/opcodes/tests/test_opcodes.cairo index 1a2b5303..737fc7df 100644 --- a/src/opcodes/tests/test_opcodes.cairo +++ b/src/opcodes/tests/test_opcodes.cairo @@ -66,12 +66,12 @@ fn test_op_depth() { let dstack = engine.get_dstack(); assert_eq!(dstack.len(), 1, "Stack length is not 1"); - let expected_stack = array!["\0\0\0\0\0\0\0\0"]; // Representing 0 + let expected_stack = array!["\0\0\0\0\0\0\0\0"]; assert_eq!(dstack, expected_stack.span(), "Stack is not equal to expected for empty stack"); // Test case 2: Stack with one item let program = "OP_1 OP_DEPTH"; - let mut compiler = CompilerTraitImpl::new(); // Create a new compiler instance + let mut compiler = CompilerTraitImpl::new(); let bytecode = compiler.compile(program); let mut engine = EngineTraitImpl::new(bytecode); @@ -82,12 +82,12 @@ fn test_op_depth() { let dstack = engine.get_dstack(); assert_eq!(dstack.len(), 2, "Stack length is not 2"); - let expected_stack = array!["\0\0\0\0\0\0\0\x01", "\0\0\0\0\0\0\0\x01"]; // [1, 1] + let expected_stack = array!["\0\0\0\0\0\0\0\x01", "\0\0\0\0\0\0\0\x01"]; assert_eq!(dstack, expected_stack.span(), "Stack is not equal to expected for one item"); // Test case 3: Stack with multiple items let program = "OP_1 OP_1 OP_ADD OP_1 OP_DEPTH"; - let mut compiler = CompilerTraitImpl::new(); // Create a new compiler instance + let mut compiler = CompilerTraitImpl::new(); let bytecode = compiler.compile(program); let mut engine = EngineTraitImpl::new(bytecode); @@ -101,8 +101,6 @@ fn test_op_depth() { let dstack = engine.get_dstack(); assert_eq!(dstack.len(), 3, "Stack length is not 3"); - let expected_stack = array![ - "\0\0\0\0\0\0\0\x02", "\0\0\0\0\0\0\0\x01", "\0\0\0\0\0\0\0\x02" - ]; // [2, 1, 2] + let expected_stack = array!["\0\0\0\0\0\0\0\x02", "\0\0\0\0\0\0\0\x01", "\0\0\0\0\0\0\0\x02"]; assert_eq!(dstack, expected_stack.span(), "Stack is not equal to expected for multiple items"); } From 59dd8e40c63b8a459c85b488e7561f20d7171e4c Mon Sep 17 00:00:00 2001 From: Tony Stark Date: Wed, 24 Jul 2024 17:24:32 -0500 Subject: [PATCH 3/5] broke tests into separate functions --- src/opcodes/tests/test_opcodes.cairo | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/opcodes/tests/test_opcodes.cairo b/src/opcodes/tests/test_opcodes.cairo index 737fc7df..3cc24fee 100644 --- a/src/opcodes/tests/test_opcodes.cairo +++ b/src/opcodes/tests/test_opcodes.cairo @@ -53,8 +53,7 @@ fn test_op_add() { } #[test] -fn test_op_depth() { - // Test case 1: Empty stack +fn test_op_depth_empty_stack() { let program = "OP_DEPTH"; let mut compiler = CompilerTraitImpl::new(); let bytecode = compiler.compile(program); @@ -68,8 +67,10 @@ fn test_op_depth() { let expected_stack = array!["\0\0\0\0\0\0\0\0"]; assert_eq!(dstack, expected_stack.span(), "Stack is not equal to expected for empty stack"); +} - // Test case 2: Stack with one item +#[test] +fn test_op_depth_one_item() { let program = "OP_1 OP_DEPTH"; let mut compiler = CompilerTraitImpl::new(); let bytecode = compiler.compile(program); @@ -84,8 +85,10 @@ fn test_op_depth() { let expected_stack = array!["\0\0\0\0\0\0\0\x01", "\0\0\0\0\0\0\0\x01"]; assert_eq!(dstack, expected_stack.span(), "Stack is not equal to expected for one item"); +} - // Test case 3: Stack with multiple items +#[test] +fn test_op_depth_multiple_items() { let program = "OP_1 OP_1 OP_ADD OP_1 OP_DEPTH"; let mut compiler = CompilerTraitImpl::new(); let bytecode = compiler.compile(program); From 7b6b5c78280dec01856326be9dbf5fa0a2aba855 Mon Sep 17 00:00:00 2001 From: Tony Stark Date: Thu, 25 Jul 2024 10:05:29 -0500 Subject: [PATCH 4/5] re-ordered OP_DEPTH --- src/compiler.cairo | 2 +- src/opcodes/opcodes.cairo | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/compiler.cairo b/src/compiler.cairo index cb61a851..6ef47a49 100644 --- a/src/compiler.cairo +++ b/src/compiler.cairo @@ -21,8 +21,8 @@ pub impl CompilerTraitImpl of CompilerTrait { // Add the opcodes to the dict opcodes.insert('OP_0', Opcode::OP_0); opcodes.insert('OP_1', Opcode::OP_1); - opcodes.insert('OP_ADD', Opcode::OP_ADD); opcodes.insert('OP_DEPTH', Opcode::OP_DEPTH); + opcodes.insert('OP_ADD', Opcode::OP_ADD); Compiler { opcodes } } diff --git a/src/opcodes/opcodes.cairo b/src/opcodes/opcodes.cairo index 8f458614..1c5c12b7 100644 --- a/src/opcodes/opcodes.cairo +++ b/src/opcodes/opcodes.cairo @@ -1,8 +1,8 @@ pub mod Opcode { pub const OP_0: u8 = 0; pub const OP_1: u8 = 81; - pub const OP_ADD: u8 = 147; pub const OP_DEPTH: u8 = 116; + pub const OP_ADD: u8 = 147; use shinigami::engine::Engine; use shinigami::stack::ScriptStackTrait; From 67dee42c9dc43e5348dfafe9bc1c958ac1b749aa Mon Sep 17 00:00:00 2001 From: Tony Stark Date: Thu, 25 Jul 2024 10:07:34 -0500 Subject: [PATCH 5/5] done --- src/compiler.cairo | 1 - 1 file changed, 1 deletion(-) diff --git a/src/compiler.cairo b/src/compiler.cairo index 6ef47a49..c2436c56 100644 --- a/src/compiler.cairo +++ b/src/compiler.cairo @@ -1,4 +1,3 @@ -use core::dict::Felt252DictTrait; use shinigami::opcodes::Opcode; // Compiler that takes a Bitcoin Script program and compiles it into a bytecode