Skip to content

Commit

Permalink
updated inner_call for hashing fn to follow generic approach (#33128)
Browse files Browse the repository at this point in the history
* updated inner_call for hashing fn to follow generic approach

* different hash compute budget values for all digests

* fixed conflicts

* reverted changes to compute_budget.rs and added 3method to trait to get compute budget values

* updated type for result fn for HasherImpl

* using Hash directly in result fn, got rid of HASH_BYTES and removed comment form compute_budget

* updated import statement

* cargo fmt -all

* removed unused import and reference related warning

* oops forgot semicolon

* removed trailing white space

(cherry picked from commit a60d185)
  • Loading branch information
Lichtso committed Oct 21, 2023
1 parent a5188a4 commit 3a425d1
Showing 1 changed file with 99 additions and 2 deletions.
101 changes: 99 additions & 2 deletions programs/bpf_loader/src/syscalls/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ use {
remaining_compute_units_syscall_enabled, stop_sibling_instruction_search_at_parent,
stop_truncating_strings_in_syscalls, switch_to_new_elf_parser,
},
hash::{Hasher, HASH_BYTES},
hash::{Hash, Hasher},
instruction::{
AccountMeta, InstructionError, ProcessedSiblingInstruction,
TRANSACTION_LEVEL_STACK_HEIGHT,
Expand Down Expand Up @@ -133,6 +133,103 @@ pub enum SyscallError {

type Error = Box<dyn std::error::Error>;

pub trait HasherImpl {
const NAME: &'static str;
type Output: AsRef<[u8]>;

fn create_hasher() -> Self;
fn hash(&mut self, val: &[u8]);
fn result(self) -> Self::Output;
fn get_base_cost(compute_budget: &ComputeBudget) -> u64;
fn get_byte_cost(compute_budget: &ComputeBudget) -> u64;
fn get_max_slices(compute_budget: &ComputeBudget) -> u64;
}

pub struct Sha256Hasher(Hasher);
pub struct Blake3Hasher(blake3::Hasher);
pub struct Keccak256Hasher(keccak::Hasher);

impl HasherImpl for Sha256Hasher {
const NAME: &'static str = "Sha256";
type Output = Hash;

fn create_hasher() -> Self {
Sha256Hasher(Hasher::default())
}

fn hash(&mut self, val: &[u8]) {
self.0.hash(val);
}

fn result(self) -> Self::Output {
self.0.result()
}

fn get_base_cost(compute_budget: &ComputeBudget) -> u64 {
compute_budget.sha256_base_cost
}
fn get_byte_cost(compute_budget: &ComputeBudget) -> u64 {
compute_budget.sha256_byte_cost
}
fn get_max_slices(compute_budget: &ComputeBudget) -> u64 {
compute_budget.sha256_max_slices
}
}

impl HasherImpl for Blake3Hasher {
const NAME: &'static str = "Blake3";
type Output = blake3::Hash;

fn create_hasher() -> Self {
Blake3Hasher(blake3::Hasher::default())
}

fn hash(&mut self, val: &[u8]) {
self.0.hash(val);
}

fn result(self) -> Self::Output {
self.0.result()
}

fn get_base_cost(compute_budget: &ComputeBudget) -> u64 {
compute_budget.sha256_base_cost
}
fn get_byte_cost(compute_budget: &ComputeBudget) -> u64 {
compute_budget.sha256_byte_cost
}
fn get_max_slices(compute_budget: &ComputeBudget) -> u64 {
compute_budget.sha256_max_slices
}
}

impl HasherImpl for Keccak256Hasher {
const NAME: &'static str = "Keccak256";
type Output = keccak::Hash;

fn create_hasher() -> Self {
Keccak256Hasher(keccak::Hasher::default())
}

fn hash(&mut self, val: &[u8]) {
self.0.hash(val);
}

fn result(self) -> Self::Output {
self.0.result()
}

fn get_base_cost(compute_budget: &ComputeBudget) -> u64 {
compute_budget.sha256_base_cost
}
fn get_byte_cost(compute_budget: &ComputeBudget) -> u64 {
compute_budget.sha256_byte_cost
}
fn get_max_slices(compute_budget: &ComputeBudget) -> u64 {
compute_budget.sha256_max_slices
}
}

fn consume_compute_meter(invoke_context: &InvokeContext, amount: u64) -> Result<(), Error> {
invoke_context.consume_checked(amount)?;
Ok(())
Expand Down Expand Up @@ -2043,7 +2140,7 @@ mod tests {
account::{create_account_shared_data_for_test, AccountSharedData},
bpf_loader,
fee_calculator::FeeCalculator,
hash::hashv,
hash::{hashv, HASH_BYTES},
instruction::Instruction,
program::check_type_assumptions,
stable_layout::stable_instruction::StableInstruction,
Expand Down

0 comments on commit 3a425d1

Please sign in to comment.