Add Keccak256 syscall and sdk support (#16498)

This commit is contained in:
Jack May
2021-05-10 16:16:58 -07:00
committed by GitHub
parent e511c442e6
commit 8eb05d6ed4
17 changed files with 366 additions and 73 deletions

View File

@@ -2761,6 +2761,7 @@ dependencies = [
"num-derive 0.3.0",
"num-traits",
"rand_core 0.6.2",
"sha3",
"solana-measure",
"solana-runtime",
"solana-sdk",
@@ -3004,7 +3005,7 @@ dependencies = [
]
[[package]]
name = "solana-bpf-rust-sha256"
name = "solana-bpf-rust-sha"
version = "1.7.0"
dependencies = [
"solana-program 1.7.0",
@@ -3364,6 +3365,7 @@ dependencies = [
"serde_bytes",
"serde_derive",
"sha2 0.9.2",
"sha3",
"solana-frozen-abi 1.7.0",
"solana-frozen-abi-macro 1.7.0",
"solana-logger 1.7.0",

View File

@@ -70,7 +70,7 @@ members = [
"rust/rand",
"rust/ro_modify",
"rust/sanity",
"rust/sha256",
"rust/sha",
"rust/spoof1",
"rust/spoof1_system",
"rust/sysvar",

View File

@@ -84,7 +84,7 @@ fn main() {
"rand",
"ro_modify",
"sanity",
"sha256",
"sha",
"spoof1",
"spoof1_system",
"upgradeable",

View File

@@ -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;
}

View File

@@ -1,25 +0,0 @@
/**
* @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

@@ -1,12 +1,12 @@
[package]
name = "solana-bpf-rust-sha256"
name = "solana-bpf-rust-sha"
version = "1.7.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/"
documentation = "https://docs.rs/solana-bpf-rust-sha256"
documentation = "https://docs.rs/solana-bpf-rust-sha"
edition = "2018"
[dependencies]

View File

@@ -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();
}
}

View File

@@ -1,36 +0,0 @@
//! @brief SHA256 Syscall test
extern crate solana_program;
use solana_program::{
custom_panic_default,
hash::{hashv, Hasher},
msg,
};
fn test_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!("sha256");
test_hasher();
0
}
custom_panic_default!();
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_sha256() {
test_hasher();
}
}

View File

@@ -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),
]);
@@ -443,7 +443,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_sha", true),
]);
}
@@ -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),
]);
@@ -1258,8 +1258,8 @@ fn assert_instruction_count() {
("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),
]);
}