Skip to content
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

feat: op_depth #12

Merged
merged 5 commits into from
Jul 26, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
2 changes: 2 additions & 0 deletions src/compiler.cairo
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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);
AbdelStark marked this conversation as resolved.
Show resolved Hide resolved
Compiler { opcodes }
}

Expand Down
8 changes: 7 additions & 1 deletion src/opcodes/opcodes.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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;
AbdelStark marked this conversation as resolved.
Show resolved Hide resolved

use shinigami::engine::Engine;
use shinigami::stack::ScriptStackTrait;
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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");
}
Expand Down
56 changes: 56 additions & 0 deletions src/opcodes/tests/test_opcodes.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,59 @@ 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_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"];
assert_eq!(dstack, expected_stack.span(), "Stack is not equal to expected for empty stack");
}

#[test]
fn test_op_depth_one_item() {
let program = "OP_1 OP_DEPTH";
let mut compiler = CompilerTraitImpl::new();
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"];
assert_eq!(dstack, expected_stack.span(), "Stack is not equal to expected for one item");
}

#[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);
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"];
assert_eq!(dstack, expected_stack.span(), "Stack is not equal to expected for multiple items");
}