Skip to content

Commit

Permalink
Add Keccak256 syscall and sdk support (backport #16498) (#17157)
Browse files Browse the repository at this point in the history
* Add Keccak256 syscall and sdk support (#16498)

(cherry picked from commit 8eb05d6)

# Conflicts:
#	Cargo.lock
#	programs/bpf/Cargo.lock
#	programs/bpf/rust/sha/Cargo.toml
#	programs/bpf/tests/programs.rs
#	programs/bpf_loader/Cargo.toml
#	sdk/program/Cargo.toml
#	sdk/program/src/lib.rs
#	sdk/src/feature_set.rs

* resolve conflicts

Co-authored-by: Jack May <jack@solana.com>
  • Loading branch information
mergify[bot] and jackcmay authored May 11, 2021
1 parent f91d7da commit 3303ead
Show file tree
Hide file tree
Showing 17 changed files with 368 additions and 74 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion programs/bpf/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion programs/bpf/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ members = [
"rust/rand",
"rust/ro_modify",
"rust/sanity",
"rust/sha256",
"rust/sha",
"rust/spoof1",
"rust/spoof1_system",
"rust/sysvar",
Expand Down
2 changes: 1 addition & 1 deletion programs/bpf/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ fn main() {
"rand",
"ro_modify",
"sanity",
"sha256",
"sha",
"spoof1",
"spoof1_system",
"sysvar",
Expand Down
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.

Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
[package]
name = "solana-bpf-rust-sha256"
name = "solana-bpf-rust-sha"
version = "1.6.9"
description = "Solana BPF test program written in Rust"
authors = ["Solana Maintainers <maintainers@solana.foundation>"]
repository = "https://github.com/solana-labs/solana"
license = "Apache-2.0"
homepage = "https://solana.com/"
documentation = "https://docs.rs/solana-bpf-rust-sha256"
documentation = "https://docs.rs/solana-bpf-rust-sha"
edition = "2018"

[dependencies]
Expand Down
43 changes: 43 additions & 0 deletions programs/bpf/rust/sha/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
//! @brief SHA Syscall test
extern crate solana_program;
use solana_program::{custom_panic_default, msg};

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

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

#[no_mangle]
pub extern "C" fn entrypoint(_input: *mut u8) -> u64 {
msg!("sha");

test_sha256_hasher();
test_keccak256_hasher();

0
}

custom_panic_default!();

#[cfg(test)]
mod test {
use super::*;

#[test]
fn test_sha() {
test_sha256_hasher();
test_keccak256_hasher();
}
}
36 changes: 0 additions & 36 deletions programs/bpf/rust/sha256/src/lib.rs

This file was deleted.

13 changes: 7 additions & 6 deletions programs/bpf/tests/programs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ fn test_program_bpf_sanity() {
("relative_call", true),
("sanity", true),
("sanity++", true),
("sha256", true),
("sha", true),
("struct_pass", true),
("struct_ret", true),
]);
Expand All @@ -444,8 +444,7 @@ fn test_program_bpf_sanity() {
("solana_bpf_rust_param_passing", true),
("solana_bpf_rust_rand", true),
("solana_bpf_rust_sanity", true),
("solana_bpf_rust_sha256", true),
("solana_bpf_rust_sysvar", true),
("solana_bpf_rust_sha", true),
]);
}

Expand Down Expand Up @@ -1251,6 +1250,7 @@ fn assert_instruction_count() {
("relative_call", 10),
("sanity", 175),
("sanity++", 177),
("sha", 694),
("struct_pass", 8),
("struct_ret", 22),
]);
Expand All @@ -1261,12 +1261,13 @@ fn assert_instruction_count() {
("solana_bpf_rust_128bit", 572),
("solana_bpf_rust_alloc", 8906),
("solana_bpf_rust_dep_crate", 2),
("solana_bpf_rust_external_spend", 498),
("solana_bpf_rust_external_spend", 521),
("solana_bpf_rust_iter", 724),
("solana_bpf_rust_many_args", 237),
("solana_bpf_rust_noop", 472),
("solana_bpf_rust_noop", 495),
("solana_bpf_rust_param_passing", 46),
("solana_bpf_rust_sanity", 894),
("solana_bpf_rust_sanity", 917),
("solana_bpf_rust_sha", 29099),
]);
}

Expand Down
1 change: 1 addition & 0 deletions programs/bpf_loader/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ log = "0.4.11"
num-derive = "0.3"
num-traits = "0.2"
rand_core = "0.6.2"
sha3 = "0.9.1"
solana-measure = { path = "../../measure", version = "=1.6.9" }
solana-runtime = { path = "../../runtime", version = "=1.6.9" }
solana-sdk = { path = "../../sdk", version = "=1.6.9" }
Expand Down
79 changes: 77 additions & 2 deletions programs/bpf_loader/src/syscalls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,13 @@ 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,
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,
instruction::{AccountMeta, Instruction, InstructionError},
keccak,
keyed_account::KeyedAccount,
native_loader,
process_instruction::{stable_log, ComputeMeter, InvokeContext, Logger},
Expand Down Expand Up @@ -127,6 +128,10 @@ pub fn register_syscalls(

syscall_registry.register_syscall_by_name(b"sol_sha256", SyscallSha256::call)?;

if invoke_context.is_feature_active(&keccak256_syscall_enabled::id()) {
syscall_registry.register_syscall_by_name(b"sol_keccak256", SyscallKeccak256::call)?;
}

if invoke_context.is_feature_active(&sysvar_via_syscall::id()) {
syscall_registry
.register_syscall_by_name(b"sol_get_clock_sysvar", SyscallGetClockSysvar::call)?;
Expand Down Expand Up @@ -253,6 +258,17 @@ pub fn bind_syscall_context_objects<'a>(
None,
)?;

bind_feature_gated_syscall_context_object!(
vm,
invoke_context.is_feature_active(&keccak256_syscall_enabled::id()),
Box::new(SyscallKeccak256 {
base_cost: bpf_compute_budget.sha256_base_cost,
byte_cost: bpf_compute_budget.sha256_byte_cost,
compute_meter: invoke_context.get_compute_meter(),
loader_id,
}),
);

let is_sysvar_via_syscall_active = invoke_context.is_feature_active(&sysvar_via_syscall::id());

let invoke_context = Rc::new(RefCell::new(invoke_context));
Expand Down Expand Up @@ -1072,6 +1088,65 @@ impl<'a> SyscallObject<BpfError> for SyscallGetRentSysvar<'a> {
}
}

// Keccak256
pub struct SyscallKeccak256<'a> {
base_cost: u64,
byte_cost: u64,
compute_meter: Rc<RefCell<dyn ComputeMeter>>,
loader_id: &'a Pubkey,
}
impl<'a> SyscallObject<BpfError> for SyscallKeccak256<'a> {
fn call(
&mut self,
vals_addr: u64,
vals_len: u64,
result_addr: u64,
_arg4: u64,
_arg5: u64,
memory_mapping: &MemoryMapping,
result: &mut Result<u64, EbpfError<BpfError>>,
) {
question_mark!(self.compute_meter.consume(self.base_cost), result);
let hash_result = question_mark!(
translate_slice_mut::<u8>(
memory_mapping,
result_addr,
keccak::HASH_BYTES as u64,
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, true),
result
);
for val in vals.iter() {
let bytes = question_mark!(
translate_slice::<u8>(
memory_mapping,
val.as_ptr() as u64,
val.len() as u64,
self.loader_id,
true,
),
result
);
question_mark!(
self.compute_meter
.consume(self.byte_cost * (val.len() as u64 / 2)),
result
);
hasher.hash(bytes);
}
}
hash_result.copy_from_slice(&hasher.result().to_bytes());
*result = Ok(0);
}
}

// Cross-program invocation syscalls

struct AccountReferences<'a> {
Expand Down
Loading

0 comments on commit 3303ead

Please sign in to comment.