add sha256 syscall (#12569)

This commit is contained in:
Jack May
2020-09-29 23:29:20 -07:00
committed by GitHub
parent 89424b29bf
commit 058bca6632
16 changed files with 321 additions and 6 deletions

View File

@@ -1912,6 +1912,13 @@ dependencies = [
"solana-sdk",
]
[[package]]
name = "solana-bpf-rust-sha256"
version = "1.4.0"
dependencies = [
"solana-sdk",
]
[[package]]
name = "solana-bpf-rust-sysval"
version = "1.4.0"

View File

@@ -54,6 +54,7 @@ members = [
"rust/param_passing_dep",
"rust/rand",
"rust/sanity",
"rust/sha256",
"rust/sysval",
]

View File

@@ -82,6 +82,7 @@ fn main() {
"param_passing",
"rand",
"sanity",
"sha256",
"sysval",
];
for program in rust_programs.iter() {

View File

@@ -0,0 +1,25 @@
/**
* @brief SHA256 Syscall test
*/
#include <solana_sdk.h>
extern uint64_t entrypoint(const uint8_t *input) {
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));
return SUCCESS;
}

View File

@@ -18,6 +18,16 @@ extern uint64_t entrypoint(const uint8_t *input) {
*val = *val + 1;
}
}
// // Uncomment for SHA256 syscall
// {
// uint8_t result[SHA256_RESULT_LENGTH];
// uint8_t bytes1[1024];
// const SolBytes bytes[] = {{bytes1, SOL_ARRAY_SIZE(bytes1)}};
// sol_sha256(bytes, SOL_ARRAY_SIZE(bytes), result);
// *val = result[0];
// }
}
return *val;
}

View File

@@ -0,0 +1,26 @@
# Note: This crate must be built using do.sh
[package]
name = "solana-bpf-rust-sha256"
version = "1.4.0"
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/"
edition = "2018"
[dependencies]
solana-sdk = { path = "../../../../sdk/", version = "1.4.0", default-features = false }
[features]
program = ["solana-sdk/program"]
default = ["program", "solana-sdk/default"]
[lib]
name = "solana_bpf_rust_sha256"
crate-type = ["cdylib"]
[package.metadata.docs.rs]
targets = ["x86_64-unknown-linux-gnu"]

View File

@@ -0,0 +1,2 @@
[target.bpfel-unknown-unknown.dependencies.std]
features = []

View File

@@ -0,0 +1,19 @@
//! @brief SHA256 Syscall test
extern crate solana_sdk;
use solana_sdk::{
hash::{hashv, Hasher},
info,
};
#[no_mangle]
pub extern "C" fn entrypoint(_input: *mut u8) -> u64 {
info!("sha256");
let vals = &["Gaggablaghblagh!".as_ref(), "flurbos".as_ref()];
let mut hasher = Hasher::default();
hasher.hashv(vals);
assert_eq!(hashv(vals), hasher.result());
0
}

View File

@@ -136,6 +136,7 @@ fn test_program_bpf_sanity() {
("relative_call", true),
("sanity", true),
("sanity++", true),
("sha256", true),
("struct_pass", true),
("struct_ret", true),
]);
@@ -154,6 +155,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_sysval", true),
]);
}