Skip to content
This repository was archived by the owner on Jun 11, 2026. It is now read-only.

Commit 402094c

Browse files
committed
feat: Ripemd_160
Signed-off-by: yHSJ <josh@securitybot.info>
1 parent c74e86d commit 402094c

6 files changed

Lines changed: 38 additions & 1 deletion

File tree

crates/uplc/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ publish = true
1414
blst = "0.3.13"
1515
bumpalo = { version = "3.16.0", features = ["collections"] }
1616
chumsky = { version = "=1.0.0-alpha.7", features = ["pratt"] }
17-
cryptoxide = "0.4.4"
17+
cryptoxide = { version = "0.4.4", features = ["ripemd160"] }
1818
hamming = "0.1.3"
1919
minicbor = { version = "0.25.1", features = ["std"] }
2020
num = "0.4.3"

crates/uplc/src/builtin/default_function.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ pub enum DefaultFunction {
110110
RotateByteString = 83,
111111
CountSetBits = 84,
112112
FindFirstSetBit = 85,
113+
Ripemd_160 = 86,
113114
}
114115

115116
impl DefaultFunction {
@@ -201,6 +202,7 @@ impl DefaultFunction {
201202
DefaultFunction::RotateByteString => 0,
202203
DefaultFunction::CountSetBits => 0,
203204
DefaultFunction::FindFirstSetBit => 0,
205+
DefaultFunction::Ripemd_160 => 0,
204206
}
205207
}
206208

@@ -292,6 +294,7 @@ impl DefaultFunction {
292294
DefaultFunction::RotateByteString => 2,
293295
DefaultFunction::CountSetBits => 1,
294296
DefaultFunction::FindFirstSetBit => 1,
297+
DefaultFunction::Ripemd_160 => 1,
295298
}
296299
}
297300
}

crates/uplc/src/machine/cost_model/builtin_costs.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ pub struct BuiltinCosts {
104104
rotate_byte_string: TwoArgumentsCosting,
105105
count_set_bits: OneArgumentCosting,
106106
find_first_set_bit: OneArgumentCosting,
107+
ripemd_160: OneArgumentCosting,
107108
}
108109

109110
impl Default for BuiltinCosts {
@@ -653,6 +654,13 @@ impl BuiltinCosts {
653654
)
654655
}
655656

657+
pub fn ripemd_160(&self, args: [i64; 1]) -> ExBudget {
658+
ExBudget::new(
659+
self.ripemd_160.mem.cost(args),
660+
self.ripemd_160.cpu.cost(args),
661+
)
662+
}
663+
656664
pub fn v3() -> Self {
657665
Self {
658666
add_integer: TwoArgumentsCosting::new(
@@ -1009,6 +1017,10 @@ impl BuiltinCosts {
10091017
OneArgumentCosting::constant_cost(1),
10101018
OneArgumentCosting::linear_cost(106057, 655),
10111019
),
1020+
ripemd_160: OneArgumentCosting::new(
1021+
OneArgumentCosting::constant_cost(3),
1022+
OneArgumentCosting::linear_cost(1964219, 24520),
1023+
),
10121024
}
10131025
}
10141026
}

crates/uplc/src/machine/runtime.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2335,6 +2335,22 @@ impl<'a> Machine<'a> {
23352335
let result = self.arena.alloc(first_bit);
23362336
Ok(Value::integer(self.arena, result))
23372337
}
2338+
DefaultFunction::Ripemd_160 => {
2339+
use cryptoxide::{digest::Digest, ripemd160::Ripemd160};
2340+
let input = runtime.args[0].unwrap_byte_string()?;
2341+
let budget = self
2342+
.costs
2343+
.builtin_costs
2344+
.ripemd_160([cost_model::byte_string_ex_mem(input)]);
2345+
self.spend_budget(budget)?;
2346+
2347+
let mut hasher = Ripemd160::new();
2348+
hasher.input(input);
2349+
let result = self.arena.alloc(vec![0; hasher.output_bytes()]);
2350+
hasher.result(result);
2351+
2352+
Ok(Value::byte_string(self.arena, result))
2353+
}
23382354
}
23392355
}
23402356
}

crates/uplc/src/syn/term.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,7 @@ pub fn builtin_from_str<'a>(arena: &'a Bump, name: &str) -> Option<&'a Term<'a,
263263
"rotateByteString" => Some(Term::rotate_byte_string(arena)),
264264
"countSetBits" => Some(Term::count_set_bits(arena)),
265265
"findFirstSetBit" => Some(Term::find_first_set_bit(arena)),
266+
"ripemd_160" => Some(Term::ripemd_160(arena)),
266267
_ => None,
267268
}
268269
}

crates/uplc/src/term.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -629,6 +629,11 @@ impl<'a, V> Term<'a, V> {
629629
pub fn find_first_set_bit(arena: &'a Bump) -> &'a Term<'a, V> {
630630
let fun = arena.alloc(DefaultFunction::FindFirstSetBit);
631631

632+
Term::builtin(arena, fun)
633+
}
634+
pub fn ripemd_160(arena: &'a Bump) -> &'a Term<'a, V> {
635+
let fun = arena.alloc(DefaultFunction::Ripemd_160);
636+
632637
Term::builtin(arena, fun)
633638
}
634639
}

0 commit comments

Comments
 (0)