Skip to content

Commit

Permalink
Feat/add ripemd160 (#126)
Browse files Browse the repository at this point in the history
<!-- enter the gh issue after hash -->


- [X] follows contribution
[guide](https://github.com/keep-starknet-strange/shinigami/blob/main/CONTRIBUTING.md)
- [X] code change includes tests

<!-- PR description below -->
Update to cairo 2.7
Add opcode for RIPEMD-160

---------

Co-authored-by: j1mbo64 <j1mbo64@none.dev>
Co-authored-by: Brandon Roberts <brandonjroberts22@gmail.com>
  • Loading branch information
3 people committed Aug 13, 2024
1 parent 5c3b312 commit d43570c
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 6 deletions.
8 changes: 8 additions & 0 deletions Scarb.lock
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
# Code generated by scarb DO NOT EDIT.
version = 1

[[package]]
name = "ripemd160"
version = "0.1.0"
source = "git+https://github.com/j1mbo64/ripemd160_cairo.git#bebe67c235b12e0a3668f49c78423d4eff6d7131"

[[package]]
name = "shinigami"
version = "0.1.0"
dependencies = [
"ripemd160",
]
7 changes: 5 additions & 2 deletions Scarb.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
[package]
name = "shinigami"
version = "0.1.0"
edition = "2023_11"
edition = "2024_07"

[dependencies]
ripemd160 = { git = "https://github.com/j1mbo64/ripemd160_cairo.git" }

[dev-dependencies]
cairo_test = "2.7.0"
cairo_test = "2.7.0"
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::Felt252Dict;
use shinigami::opcodes::Opcode;
use shinigami::utils;

Expand Down Expand Up @@ -163,6 +164,7 @@ pub impl CompilerTraitImpl of CompilerTrait {
compiler.add_opcode('OP_MIN', Opcode::OP_MIN);
compiler.add_opcode('OP_MAX', Opcode::OP_MAX);
compiler.add_opcode('OP_WITHIN', Opcode::OP_WITHIN);
compiler.add_opcode('OP_RIPEMD160', Opcode::OP_RIPEMD160);
compiler.add_opcode('OP_RESERVED', Opcode::OP_RESERVED);
compiler.add_opcode('OP_RESERVED1', Opcode::OP_RESERVED1);
compiler.add_opcode('OP_RESERVED2', Opcode::OP_RESERVED2);
Expand Down
2 changes: 2 additions & 0 deletions src/cond_stack.cairo
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use core::dict::Felt252Dict;

#[derive(Destruct)]
pub struct ConditionalStack {
stack: Felt252Dict<u8>,
Expand Down
7 changes: 7 additions & 0 deletions src/opcodes/crypto.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,10 @@ pub fn opcode_sha256(ref engine: Engine) -> Result<(), felt252> {
engine.dstack.push_byte_array(res_bytes);
return Result::Ok(());
}

pub fn opcode_ripemd160(ref engine: Engine) -> Result<(), felt252> {
let m = engine.dstack.pop_byte_array()?;
let h: ByteArray = ripemd160::ripemd160_hash(@m).into();
engine.dstack.push_byte_array(h);
return Result::Ok(());
}
3 changes: 2 additions & 1 deletion src/opcodes/opcodes.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ pub mod Opcode {
pub const OP_MIN: u8 = 163;
pub const OP_MAX: u8 = 164;
pub const OP_WITHIN: u8 = 165;
pub const OP_RIPEMD160: u8 = 166;
pub const OP_SHA256: u8 = 168;
pub const OP_NOP1: u8 = 176;
pub const OP_NOP4: u8 = 179;
Expand Down Expand Up @@ -343,7 +344,7 @@ pub mod Opcode {
163 => arithmetic::opcode_min(ref engine),
164 => arithmetic::opcode_max(ref engine),
165 => arithmetic::opcode_within(ref engine),
166 => utils::not_implemented(ref engine),
166 => crypto::opcode_ripemd160(ref engine),
167 => utils::not_implemented(ref engine),
168 => crypto::opcode_sha256(ref engine),
169 => utils::not_implemented(ref engine),
Expand Down
63 changes: 61 additions & 2 deletions src/opcodes/tests/test_crypto.cairo
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use shinigami::engine::{Engine, EngineTrait};
use shinigami::stack::ScriptStackTrait;
use shinigami::opcodes::tests::utils;
use shinigami::errors::Error;
use shinigami::utils::hex_to_bytecode;
use shinigami::errors::Error;

#[test]
fn test_opcode_sha256_1() {
let program = "OP_1 OP_SHA256";
let mut engine = utils::test_compile_and_run(program);
Expand Down Expand Up @@ -63,3 +64,61 @@ fn test_opcode_sha256_14_double_sha256() {
let expected_dstack = array![hex_data];
utils::check_expected_dstack(ref engine, expected_dstack.span());
}

#[test]
fn test_op_ripemd160() {
// 0x5368696E6967616D69 == 'Shinigami'
let program = "OP_PUSHDATA1 0x09 0x5368696E6967616D69 OP_RIPEMD160";
let mut engine = utils::test_compile_and_run(program);
utils::check_dstack_size(ref engine, 1);
let expected_stack = array![hex_to_bytecode(@"0xE51F342A8246B579DAE6B574D161345865E3CE3D")];
utils::check_expected_dstack(ref engine, expected_stack.span());
}

#[test]
fn test_op_ripemd160_1() {
let program = "OP_1 OP_RIPEMD160";
let mut engine = utils::test_compile_and_run(program);
utils::check_dstack_size(ref engine, 1);
let expected_stack = array![hex_to_bytecode(@"0xF291BA5015DF348C80853FA5BB0F7946F5C9E1B3")];
utils::check_expected_dstack(ref engine, expected_stack.span());
}

#[test]
fn test_op_ripemd160_2() {
let program = "OP_2 OP_RIPEMD160";
let mut engine = utils::test_compile_and_run(program);
utils::check_dstack_size(ref engine, 1);
let expected_stack = array![hex_to_bytecode(@"0x1E9955C5DBF77215CC79235668861E435FA2C3AB")];
utils::check_expected_dstack(ref engine, expected_stack.span());
}

#[test]
fn test_op_ripemd160_data_8() {
let program = "OP_DATA_8 0x0102030405060708 OP_RIPEMD160";
let mut engine = utils::test_compile_and_run(program);
utils::check_dstack_size(ref engine, 1);
let expected_stack = array![hex_to_bytecode(@"0xC9883EECE7DCA619B830DC9D87E82C38478111C0")];
utils::check_expected_dstack(ref engine, expected_stack.span());
}

#[test]
fn test_ripemd160_push_data_2() {
let byte_data: ByteArray =
"0x000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F202122232425262728292A2B2C2D2E2F303132333435363738393A3B3C3D3E3F404142434445464748494A4B4C4D4E4F505152535455565758595A5B5C5D5E5F606162636465666768696A6B6C6D6E6F707172737475767778797A7B7C7D7E7F808182838485868788898A8B8C8D8E8F909192939495969798999A9B9C9D9E9FA0A1A2A3A4A5A6A7A8A9AAABACADAEAFB0B1B2B3B4B5B6B7B8B9BABBBCBDBEBFC0C1C2C3C4C5C6C7C8C9CACBCCCDCECFD0D1D2D3D4D5D6D7D8D9DADBDCDDDEDFE0E1E2E3E4E5E6E7E8E9EAEBECEDEEEFF0F1F2F3F4F5F6F7F8F9FAFBFCFDFEFF";
let program = format!("OP_PUSHDATA2 0x0100 {} OP_RIPEMD160", byte_data);
let mut engine = utils::test_compile_and_run(program);
utils::check_dstack_size(ref engine, 1);
let hex_data: ByteArray = hex_to_bytecode(@"0x9C4FA072DB2C871A5635E37F791E93AB45049676");
let expected_dstack = array![hex_data];
utils::check_expected_dstack(ref engine, expected_dstack.span());
}

#[test]
fn test_op_ripemd160_14_double_ripemd160() {
let program = "OP_14 OP_RIPEMD160 OP_RIPEMD160";
let mut engine = utils::test_compile_and_run(program);
utils::check_dstack_size(ref engine, 1);
let expected_stack = array![hex_to_bytecode(@"0xA407E5C9190ACA4F4A6C676D130F5A72CEFB0D60")];
utils::check_expected_dstack(ref engine, expected_stack.span());
}
2 changes: 1 addition & 1 deletion src/stack.cairo
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use core::dict::Felt252DictEntryTrait;
use core::dict::{Felt252Dict, Felt252DictEntryTrait};
use shinigami::scriptnum::ScriptNum;
use shinigami::errors::Error;
use shinigami::utils;
Expand Down

0 comments on commit d43570c

Please sign in to comment.