diff --git a/programs/bpf/rust/ristretto/src/lib.rs b/programs/bpf/rust/ristretto/src/lib.rs index 162f5acb3f..14c49d4663 100644 --- a/programs/bpf/rust/ristretto/src/lib.rs +++ b/programs/bpf/rust/ristretto/src/lib.rs @@ -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(); + } +} diff --git a/programs/bpf/rust/ristretto/src/ristretto.rs b/programs/bpf/rust/ristretto/src/ristretto.rs index 801cf31788..588685c5d9 100644 --- a/programs/bpf/rust/ristretto/src/ristretto.rs +++ b/programs/bpf/rust/ristretto/src/ristretto.rs @@ -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 { - 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; -} diff --git a/programs/bpf/rust/sha256/src/lib.rs b/programs/bpf/rust/sha256/src/lib.rs index 74b5002498..38762bc530 100644 --- a/programs/bpf/rust/sha256/src/lib.rs +++ b/programs/bpf/rust/sha256/src/lib.rs @@ -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(); + } +} diff --git a/sdk/src/hash.rs b/sdk/src/hash.rs index 957f5d93db..1ddff46a39 100644 --- a/sdk/src/hash.rs +++ b/sdk/src/hash.rs @@ -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;