Fix syscall featurization (#12714)

* Fix syscall featurization

* nudge
This commit is contained in:
Jack May 2020-10-07 18:38:38 -07:00 committed by GitHub
parent 346890ded3
commit dd7fae4afb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 83 additions and 34 deletions

View File

@ -2,21 +2,13 @@
pub mod ristretto;
extern crate solana_sdk;
use crate::ristretto::ristretto_mul;
use curve25519_dalek::{constants::RISTRETTO_BASEPOINT_POINT, scalar::Scalar};
use solana_sdk::{
account_info::AccountInfo, entrypoint, entrypoint::ProgramResult, info, pubkey::Pubkey,
};
entrypoint!(process_instruction);
fn process_instruction(
_program_id: &Pubkey,
_accounts: &[AccountInfo],
_instruction_data: &[u8],
) -> ProgramResult {
info!("Ristretto multiply");
fn test_ristretto_mul() -> ProgramResult {
let point = RISTRETTO_BASEPOINT_POINT;
let scalar = Scalar::zero();
let result = ristretto_mul(&point, &scalar)?;
@ -29,3 +21,28 @@ fn process_instruction(
Ok(())
}
entrypoint!(process_instruction);
fn process_instruction(
_program_id: &Pubkey,
_accounts: &[AccountInfo],
_instruction_data: &[u8],
) -> ProgramResult {
info!("Ristretto multiply");
test_ristretto_mul()?;
Ok(())
}
#[cfg(test)]
mod test {
use super::*;
// Pull in syscall stubs when building for non-BPF targets
solana_sdk::program_stubs!();
#[test]
fn test_ristretto() {
test_ristretto_mul().unwrap();
}
}

View File

@ -1,5 +1,7 @@
use curve25519_dalek::{ristretto::RistrettoPoint, scalar::Scalar};
use solana_sdk::{entrypoint::SUCCESS, program_error::ProgramError};
#[cfg(all(feature = "program", target_arch = "bpf"))]
use solana_sdk::entrypoint::SUCCESS;
use solana_sdk::program_error::ProgramError;
/// Multiply a ristretto point with a scalar
///
@ -11,23 +13,34 @@ pub fn ristretto_mul(
point: &RistrettoPoint,
scalar: &Scalar,
) -> Result<RistrettoPoint, ProgramError> {
let mut result = RistrettoPoint::default();
let status = unsafe {
sol_ristretto_mul(
point as *const _ as *const u8,
scalar as *const _ as *const u8,
&mut result as *const _ as *mut u8,
)
};
match status {
SUCCESS => Ok(result),
_ => Err(status.into()),
// Perform the calculation inline, calling this from within a program is
// not supported
#[cfg(not(all(feature = "program", target_arch = "bpf")))]
{
Ok(point * scalar)
}
// Call via a system call to perform the calculation
#[cfg(all(feature = "program", target_arch = "bpf"))]
{
extern "C" {
fn sol_ristretto_mul(
point_addr: *const u8,
scalar_addr: *const u8,
result_addr: *mut u8,
) -> u64;
}
let mut result = RistrettoPoint::default();
let status = unsafe {
sol_ristretto_mul(
point as *const _ as *const u8,
scalar as *const _ as *const u8,
&mut result as *const _ as *mut u8,
)
};
match status {
SUCCESS => Ok(result),
_ => Err(status.into()),
}
}
}
extern "C" {
fn sol_ristretto_mul(
point_addr: *const u8,
scalar_addr: *const u8,
result_addr: *mut u8,
) -> u64;
}

View File

@ -6,14 +6,30 @@ use solana_sdk::{
info,
};
#[no_mangle]
pub extern "C" fn entrypoint(_input: *mut u8) -> u64 {
info!("sha256");
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 {
info!("sha256");
test_hasher();
0
}
#[cfg(test)]
mod test {
use super::*;
// Pull in syscall stubs when building for non-BPF targets
solana_sdk::program_stubs!();
#[test]
fn test_sha256() {
test_hasher();
}
}

View File

@ -103,13 +103,16 @@ impl Hash {
/// Return a Sha256 hash for the given data.
pub fn hashv(vals: &[&[u8]]) -> Hash {
#[cfg(not(feature = "program"))]
// Perform the calculation inline, calling this from within a program is
// not supported
#[cfg(not(all(feature = "program", target_arch = "bpf")))]
{
let mut hasher = Hasher::default();
hasher.hashv(vals);
hasher.result()
}
#[cfg(feature = "program")]
// Call via a system call to perform the calculation
#[cfg(all(feature = "program", target_arch = "bpf"))]
{
extern "C" {
fn sol_sha256(vals: *const u8, val_len: u64, hash_result: *mut u8) -> u64;