Skip to content

Commit

Permalink
feat: keep-starknet-strange#17 add OP_MIN opcode
Browse files Browse the repository at this point in the history
  • Loading branch information
ptisserand committed Jul 25, 2024
1 parent 480a943 commit 7d1b350
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/compiler.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ pub impl CompilerTraitImpl of CompilerTrait {
opcodes.insert('OP_15', Opcode::OP_15);
opcodes.insert('OP_16', Opcode::OP_16);
opcodes.insert('OP_ADD', Opcode::OP_ADD);
opcodes.insert('OP_MIN', Opcode::OP_MIN);
Compiler { opcodes }
}

Expand Down
28 changes: 28 additions & 0 deletions src/opcodes/opcodes.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ pub mod Opcode {
pub const OP_15: u8 = 95;
pub const OP_16: u8 = 96;
pub const OP_ADD: u8 = 147;
pub const OP_MIN: u8 = 163;

use shinigami::engine::Engine;
use shinigami::stack::ScriptStackTrait;
Expand Down Expand Up @@ -170,6 +171,22 @@ pub mod Opcode {
145 => not_implemented(ref engine),
146 => not_implemented(ref engine),
147 => opcode_add(ref engine),
148 => not_implemented(ref engine),
149 => not_implemented(ref engine),
150 => not_implemented(ref engine),
151 => not_implemented(ref engine),
152 => not_implemented(ref engine),
153 => not_implemented(ref engine),
154 => not_implemented(ref engine),
155 => not_implemented(ref engine),
156 => not_implemented(ref engine),
157 => not_implemented(ref engine),
158 => not_implemented(ref engine),
159 => not_implemented(ref engine),
160 => not_implemented(ref engine),
161 => not_implemented(ref engine),
162 => not_implemented(ref engine),
163 => opcode_min(ref engine),
_ => not_implemented(ref engine)
}
}
Expand All @@ -189,6 +206,17 @@ pub mod Opcode {
engine.dstack.push_int(a + b);
}

fn opcode_min(ref engine: Engine) {
let a = engine.dstack.pop_int();
let b = engine.dstack.pop_int();

engine.dstack.push_int(if a < b {
a
} else {
b
});
}

fn not_implemented(ref engine: Engine) {
panic!("Opcode not implemented");
}
Expand Down
57 changes: 57 additions & 0 deletions src/opcodes/tests/test_opcodes.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -143,3 +143,60 @@ 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_min_min_first() {
let program = "OP_1 OP_2 OP_MIN";
let mut compiler = CompilerTraitImpl::new();
let bytecode = compiler.compile(program);
let mut engine = EngineTraitImpl::new(bytecode);

let _ = engine.step();
let _ = engine.step();
let res = engine.step();
assert!(res, "Execution of run 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\x01"];
assert_eq!(dstack, expected_stack.span(), "Stack is not equal to expected");
}

#[test]
fn test_op_min_min_second() {
let program = "OP_2 OP_1 OP_MIN";
let mut compiler = CompilerTraitImpl::new();
let bytecode = compiler.compile(program);
let mut engine = EngineTraitImpl::new(bytecode);

let _ = engine.step();
let _ = engine.step();
let res = engine.step();
assert!(res, "Execution of run 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\x01"];
assert_eq!(dstack, expected_stack.span(), "Stack is not equal to expected");
}

#[test]
fn test_op_min_same_value() {
let program = "OP_1 OP_1 OP_MIN";
let mut compiler = CompilerTraitImpl::new();
let bytecode = compiler.compile(program);
let mut engine = EngineTraitImpl::new(bytecode);

let _ = engine.step();
let _ = engine.step();
let res = engine.step();
assert!(res, "Execution of run 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\x01"];
assert_eq!(dstack, expected_stack.span(), "Stack is not equal to expected");
}

0 comments on commit 7d1b350

Please sign in to comment.