Skip to content

Commit

Permalink
C support and cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
jackcmay committed May 10, 2021
1 parent 5b34570 commit cb8323f
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 41 deletions.
47 changes: 47 additions & 0 deletions programs/bpf/c/src/sha/sha.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/**
* @brief SHA256 Syscall test
*/
#include <solana_sdk.h>

extern uint64_t entrypoint(const uint8_t *input) {

// SHA256
{
uint8_t result[SHA256_RESULT_LENGTH];
uint8_t expected[] = {0x9f, 0xa2, 0x7e, 0x8f, 0x7b, 0xc1, 0xec, 0xe8,
0xae, 0x7b, 0x9a, 0x91, 0x46, 0x53, 0x20, 0xf,
0x1c, 0x22, 0x8e, 0x56, 0x10, 0x30, 0x59, 0xfd,
0x35, 0x8d, 0x57, 0x54, 0x96, 0x47, 0x2c, 0xc9};

uint8_t bytes1[] = {'G', 'a', 'g', 'g', 'a', 'b', 'l', 'a',
'g', 'h', 'b', 'l', 'a', 'g', 'h', '!'};
uint8_t bytes2[] = {'f', 'l', 'u', 'r', 'b', 'o', 's'};
const SolBytes bytes[] = {{bytes1, SOL_ARRAY_SIZE(bytes1)},
{bytes2, SOL_ARRAY_SIZE(bytes2)}};

sol_sha256(bytes, SOL_ARRAY_SIZE(bytes), result);

sol_assert(0 == sol_memcmp(result, expected, SHA256_RESULT_LENGTH));
}

// Keccak
{
uint8_t result[KECCAK_RESULT_LENGTH];
uint8_t expected[] = {0xd1, 0x9a, 0x9d, 0xe2, 0x89, 0x7f, 0x7c, 0x9e,
0x5, 0x32, 0x32, 0x22, 0xe8, 0xc6, 0xb4, 0x88,
0x6b, 0x5b, 0xbb, 0xec, 0xd4, 0x42, 0xfd, 0x10,
0x7d, 0xd5, 0x9a, 0x6f, 0x21, 0xd3, 0xb8, 0xa7};

uint8_t bytes1[] = {'G', 'a', 'g', 'g', 'a', 'b', 'l', 'a',
'g', 'h', 'b', 'l', 'a', 'g', 'h', '!'};
uint8_t bytes2[] = {'f', 'l', 'u', 'r', 'b', 'o', 's'};
const SolBytes bytes[] = {{bytes1, SOL_ARRAY_SIZE(bytes1)},
{bytes2, SOL_ARRAY_SIZE(bytes2)}};

sol_keccak256(bytes, SOL_ARRAY_SIZE(bytes), result);

sol_assert(0 == sol_memcmp(result, expected, KECCAK_RESULT_LENGTH));
}

return SUCCESS;
}
25 changes: 0 additions & 25 deletions programs/bpf/c/src/sha256/sha256.c

This file was deleted.

8 changes: 4 additions & 4 deletions programs/bpf/rust/sha/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ fn test_sha256_hasher() {
}

fn test_keccak256_hasher() {
use solana_program::keccak::{hash, Hasher};
let vals = "Gaggablaghblagh!".as_ref();
use solana_program::keccak::{hashv, Hasher};
let vals = &["Gaggablaghblagh!".as_ref(), "flurbos".as_ref()];
let mut hasher = Hasher::default();
hasher.hash(vals);
assert_eq!(hash(vals), hasher.result());
hasher.hashv(vals);
assert_eq!(hashv(vals), hasher.result());
}

#[no_mangle]
Expand Down
10 changes: 5 additions & 5 deletions programs/bpf/tests/programs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@ fn test_program_bpf_sanity() {
("relative_call", true),
("sanity", true),
("sanity++", true),
("sha256", true),
("sha", true),
("struct_pass", true),
("struct_ret", true),
]);
Expand Down Expand Up @@ -1239,7 +1239,7 @@ fn assert_instruction_count() {
("relative_call", 10),
("sanity", 175),
("sanity++", 177),
("sha256", 348),
("sha", 694),
("struct_pass", 8),
("struct_ret", 22),
]);
Expand All @@ -1251,15 +1251,15 @@ fn assert_instruction_count() {
("solana_bpf_rust_alloc", 8906),
("solana_bpf_rust_custom_heap", 516),
("solana_bpf_rust_dep_crate", 2),
("solana_bpf_rust_external_spend", 521),
("solana_bpf_rust_external_spend", 498),
("solana_bpf_rust_iter", 724),
("solana_bpf_rust_many_args", 237),
("solana_bpf_rust_mem", 2297),
("solana_bpf_rust_noop", 472),
("solana_bpf_rust_param_passing", 46),
("solana_bpf_rust_rand", 475),
("solana_bpf_rust_sanity", 869),
("solana_bpf_rust_sha256", 10830),
("solana_bpf_rust_sanity", 894),
("solana_bpf_rust_sha", 29099),
]);
}

Expand Down
13 changes: 7 additions & 6 deletions programs/bpf_loader/src/syscalls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ use solana_sdk::{
epoch_schedule::EpochSchedule,
feature_set::{
cpi_data_cost, cpi_share_ro_and_exec_accounts, demote_sysvar_write_locks,
enforce_aligned_host_addrs, set_upgrade_authority_via_cpi_enabled, sysvar_via_syscall,
update_data_on_realloc, keccak256_syscall_enabled,
enforce_aligned_host_addrs, keccak256_syscall_enabled,
set_upgrade_authority_via_cpi_enabled, sysvar_via_syscall, update_data_on_realloc,
},
hash::{Hasher, HASH_BYTES},
ic_msg,
Expand Down Expand Up @@ -261,7 +261,6 @@ pub fn bind_syscall_context_objects<'a>(
vm,
invoke_context.is_feature_active(&keccak256_syscall_enabled::id()),
Box::new(SyscallKeccak256 {
// TODO based on sha256 costs for now
base_cost: bpf_compute_budget.sha256_base_cost,
byte_cost: bpf_compute_budget.sha256_byte_cost,
compute_meter: invoke_context.get_compute_meter(),
Expand Down Expand Up @@ -1110,14 +1109,15 @@ impl<'a> SyscallObject<BpfError> for SyscallKeccak256<'a> {
memory_mapping,
result_addr,
keccak::HASH_BYTES as u64,
self.loader_id
self.loader_id,
true,
),
result
);
let mut hasher = keccak::Hasher::default();
if vals_len > 0 {
let vals = question_mark!(
translate_slice::<&[u8]>(memory_mapping, vals_addr, vals_len, self.loader_id),
translate_slice::<&[u8]>(memory_mapping, vals_addr, vals_len, self.loader_id, true),
result
);
for val in vals.iter() {
Expand All @@ -1126,7 +1126,8 @@ impl<'a> SyscallObject<BpfError> for SyscallKeccak256<'a> {
memory_mapping,
val.as_ptr() as u64,
val.len() as u64,
self.loader_id
self.loader_id,
true,
),
result
);
Expand Down
18 changes: 18 additions & 0 deletions sdk/bpf/c/inc/solana_sdk.h
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,24 @@ uint64_t sol_sha256(
const uint8_t *result
);

/**
* Length of a Keccak hash result
*/
#define KECCAK_RESULT_LENGTH 32

/**
* Keccak
*
* @param bytes Array of byte arrays
* @param bytes_len Number of byte arrays
* @param result 32 byte array to hold the result
*/
uint64_t sol_keccak256(
const SolBytes *bytes,
int bytes_len,
const uint8_t *result
);

/**
* Account Meta
*/
Expand Down
1 change: 0 additions & 1 deletion sdk/program/src/keccak.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ impl Hasher {
}
}
pub fn result(self) -> Hash {
// TODO
// At the time of this writing, the sha3 library is stuck on an old version
// of generic_array (0.9.0). Decouple ourselves with a clone to our version.
Hash(<[u8; HASH_BYTES]>::try_from(self.hasher.finalize().as_slice()).unwrap())
Expand Down

0 comments on commit cb8323f

Please sign in to comment.