Skip to content

Commit

Permalink
feat: add more hashing functions
Browse files Browse the repository at this point in the history
  • Loading branch information
rvcas committed Oct 12, 2024
1 parent 0f6f2ed commit 211de54
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 3 deletions.
64 changes: 61 additions & 3 deletions crates/uplc/src/machine/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -394,10 +394,68 @@ impl<'a> Runtime<'a> {

Ok(value)
}
DefaultFunction::Sha3_256 => todo!(),
DefaultFunction::Blake2b_256 => todo!(),
DefaultFunction::Sha3_256 => {
use cryptoxide::{digest::Digest, sha3::Sha3_256};

let arg1 = self.args[0].unwrap_byte_string()?;

let mut hasher = Sha3_256::new();

hasher.input(arg1);

let mut bytes = BumpVec::with_capacity_in(hasher.output_bytes(), arena);

unsafe {
bytes.set_len(hasher.output_bytes());
}

hasher.result(&mut bytes);

let value = Value::byte_string(arena, bytes);

Ok(value)
}
DefaultFunction::Blake2b_256 => {
use cryptoxide::{blake2b::Blake2b, digest::Digest};

let arg1 = self.args[0].unwrap_byte_string()?;

let mut digest = BumpVec::with_capacity_in(32, arena);

unsafe {
digest.set_len(32);
}

let mut context = Blake2b::new(32);

context.input(arg1);
context.result(&mut digest);

let value = Value::byte_string(arena, digest);

Ok(value)
}
DefaultFunction::Keccak_256 => todo!(),
DefaultFunction::Blake2b_224 => todo!(),
DefaultFunction::Blake2b_224 => {
use cryptoxide::{blake2b::Blake2b, digest::Digest};

let arg1 = self.args[0].unwrap_byte_string()?;

let mut digest = BumpVec::with_capacity_in(28, arena);

unsafe {
digest.set_len(28);
}

let mut context = Blake2b::new(28);

context.input(arg1);
context.result(&mut digest);

let value = Value::byte_string(arena, digest);

Ok(value)
}
DefaultFunction::VerifyEd25519Signature => {
use cryptoxide::ed25519;

Expand Down
1 change: 1 addition & 0 deletions crates/uplc/src/syn/term.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ pub fn builtin_from_str<'a>(arena: &'a Bump, name: &str) -> Option<&'a Term<'a>>
"indexByteString" => Some(Term::index_byte_string(arena)),
"lessThanByteString" => Some(Term::less_than_byte_string(arena)),
"lessThanEqualsByteString" => Some(Term::less_than_equals_byte_string(arena)),
"blake2b_256" => Some(Term::blake2b_256(arena)),
_ => None,
}
}
6 changes: 6 additions & 0 deletions crates/uplc/src/term.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,4 +231,10 @@ impl<'a> Term<'a> {

Term::builtin(arena, fun)
}

pub fn blake2b_256(arena: &'a Bump) -> &'a Term<'a> {
let fun = arena.alloc(DefaultFunction::Blake2b_256);

Term::builtin(arena, fun)
}
}

0 comments on commit 211de54

Please sign in to comment.