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

@ -391,6 +391,32 @@ static bool sol_deserialize(
return true;
}
/**
* Byte array pointer and string
*/
typedef struct {
const uint8_t *addr; /** bytes */
uint64_t len; /** number of bytes*/
} SolBytes;
/**
* Length of a sha256 hash result
*/
#define SHA256_RESULT_LENGTH 32
/**
* Sha256
*
* @param bytes Array of byte arrays
* @param bytes_len Number of byte arrays
* @param result 32 byte array to hold the result
*/
static uint64_t sol_sha256(
const SolBytes *bytes,
int bytes_len,
const uint8_t *result
);
/**
* Account Meta
*/
@ -428,7 +454,7 @@ typedef struct {
uint64_t len; /** Number of seeds */
} SolSignerSeeds;
/*
/**
* Create a program address
*
* @param seeds Seed bytes used to sign program accounts
@ -448,7 +474,7 @@ static uint64_t sol_create_program_address(
* * @{
*/
/*
/**
* Invoke another program and sign for some of the keys
*
* @param instruction Instruction to process
@ -480,7 +506,7 @@ static uint64_t sol_invoke_signed(
signers_seeds_len
);
}
/*
/**
* Invoke another program
*
* @param instruction Instruction to process

View File

@ -103,9 +103,27 @@ impl Hash {
/// Return a Sha256 hash for the given data.
pub fn hashv(vals: &[&[u8]]) -> Hash {
let mut hasher = Hasher::default();
hasher.hashv(vals);
hasher.result()
#[cfg(not(feature = "program"))]
{
let mut hasher = Hasher::default();
hasher.hashv(vals);
hasher.result()
}
#[cfg(feature = "program")]
{
extern "C" {
fn sol_sha256(vals: *const u8, val_len: u64, hash_result: *mut u8) -> u64;
};
let mut hash_result = [0; HASH_BYTES];
unsafe {
sol_sha256(
vals as *const _ as *const u8,
vals.len() as u64,
&mut hash_result as *mut _ as *mut u8,
);
}
Hash::new_from_array(hash_result)
}
}
/// Return a Sha256 hash for the given data.