Blake3 syscall (#17358)

This commit is contained in:
Arthur Greef
2021-06-08 11:04:10 -07:00
committed by GitHub
parent 7b58dcac14
commit 28fdfed1ba
11 changed files with 355 additions and 7 deletions

View File

@@ -3069,6 +3069,7 @@ dependencies = [
name = "solana-bpf-rust-sha"
version = "1.8.0"
dependencies = [
"blake3",
"solana-program 1.8.0",
]

19
programs/bpf/c/src/sha/sha.c Normal file → Executable file
View File

@@ -43,5 +43,24 @@ extern uint64_t entrypoint(const uint8_t *input) {
sol_assert(0 == sol_memcmp(result, expected, KECCAK_RESULT_LENGTH));
}
// Blake3
{
uint8_t result[BLAKE3_RESULT_LENGTH];
uint8_t expected[] = {0xad, 0x5d, 0x97, 0x5b, 0xc2, 0xc7, 0x46, 0x19,
0x31, 0xb4, 0x87, 0x5d, 0x19, 0x6, 0xc5, 0x36,
0xf4, 0x97, 0xa8, 0x45, 0x55, 0xec, 0xaf, 0xf2,
0x50, 0x70, 0xe3, 0xe2, 0x3d, 0xbe, 0x7, 0x8c};
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_blake3(bytes, SOL_ARRAY_SIZE(bytes), result);
sol_assert(0 == sol_memcmp(result, expected, BLAKE3_RESULT_LENGTH));
}
return SUCCESS;
}

View File

@@ -10,6 +10,7 @@ documentation = "https://docs.rs/solana-bpf-rust-sha"
edition = "2018"
[dependencies]
blake3 = "0.3.7"
solana-program = { path = "../../../../sdk/program", version = "=1.8.0" }
[lib]

11
programs/bpf/rust/sha/src/lib.rs Normal file → Executable file
View File

@@ -19,12 +19,22 @@ fn test_keccak256_hasher() {
assert_eq!(hashv(vals), hasher.result());
}
fn test_blake3_hasher() {
use solana_program::blake3::hashv;
let v0: &[u8] = b"Gaggablaghblagh!";
let v1: &[u8] = b"flurbos!";
let vals: &[&[u8]] = &[v0, v1];
let hash = blake3::hash(&[v0, v1].concat());
assert_eq!(hashv(vals).0, *hash.as_bytes());
}
#[no_mangle]
pub extern "C" fn entrypoint(_input: *mut u8) -> u64 {
msg!("sha");
test_sha256_hasher();
test_keccak256_hasher();
test_blake3_hasher();
0
}
@@ -39,5 +49,6 @@ mod test {
fn test_sha() {
test_sha256_hasher();
test_keccak256_hasher();
test_blake3_hasher();
}
}

View File

@@ -1282,7 +1282,7 @@ fn assert_instruction_count() {
("relative_call", 10),
("sanity", 174),
("sanity++", 174),
("sha", 694),
("sha", 1040),
("struct_pass", 8),
("struct_ret", 22),
]);
@@ -1303,7 +1303,7 @@ fn assert_instruction_count() {
("solana_bpf_rust_param_passing", 46),
("solana_bpf_rust_rand", 498),
("solana_bpf_rust_sanity", 917),
("solana_bpf_rust_sha", 29099),
("solana_bpf_rust_sha", 32384),
]);
}