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; pub mod ristretto;
extern crate solana_sdk;
use crate::ristretto::ristretto_mul; use crate::ristretto::ristretto_mul;
use curve25519_dalek::{constants::RISTRETTO_BASEPOINT_POINT, scalar::Scalar}; use curve25519_dalek::{constants::RISTRETTO_BASEPOINT_POINT, scalar::Scalar};
use solana_sdk::{ use solana_sdk::{
account_info::AccountInfo, entrypoint, entrypoint::ProgramResult, info, pubkey::Pubkey, account_info::AccountInfo, entrypoint, entrypoint::ProgramResult, info, pubkey::Pubkey,
}; };
entrypoint!(process_instruction); fn test_ristretto_mul() -> ProgramResult {
fn process_instruction(
_program_id: &Pubkey,
_accounts: &[AccountInfo],
_instruction_data: &[u8],
) -> ProgramResult {
info!("Ristretto multiply");
let point = RISTRETTO_BASEPOINT_POINT; let point = RISTRETTO_BASEPOINT_POINT;
let scalar = Scalar::zero(); let scalar = Scalar::zero();
let result = ristretto_mul(&point, &scalar)?; let result = ristretto_mul(&point, &scalar)?;
@ -29,3 +21,28 @@ fn process_instruction(
Ok(()) 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 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 /// Multiply a ristretto point with a scalar
/// ///
@ -11,6 +13,23 @@ pub fn ristretto_mul(
point: &RistrettoPoint, point: &RistrettoPoint,
scalar: &Scalar, scalar: &Scalar,
) -> Result<RistrettoPoint, ProgramError> { ) -> Result<RistrettoPoint, ProgramError> {
// 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 mut result = RistrettoPoint::default();
let status = unsafe { let status = unsafe {
sol_ristretto_mul( sol_ristretto_mul(
@ -23,11 +42,5 @@ pub fn ristretto_mul(
SUCCESS => Ok(result), SUCCESS => Ok(result),
_ => Err(status.into()), _ => 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, info,
}; };
#[no_mangle] fn test_hasher() {
pub extern "C" fn entrypoint(_input: *mut u8) -> u64 {
info!("sha256");
let vals = &["Gaggablaghblagh!".as_ref(), "flurbos".as_ref()]; let vals = &["Gaggablaghblagh!".as_ref(), "flurbos".as_ref()];
let mut hasher = Hasher::default(); let mut hasher = Hasher::default();
hasher.hashv(vals); hasher.hashv(vals);
assert_eq!(hashv(vals), hasher.result()); assert_eq!(hashv(vals), hasher.result());
}
#[no_mangle]
pub extern "C" fn entrypoint(_input: *mut u8) -> u64 {
info!("sha256");
test_hasher();
0 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. /// Return a Sha256 hash for the given data.
pub fn hashv(vals: &[&[u8]]) -> Hash { 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(); let mut hasher = Hasher::default();
hasher.hashv(vals); hasher.hashv(vals);
hasher.result() hasher.result()
} }
#[cfg(feature = "program")] // Call via a system call to perform the calculation
#[cfg(all(feature = "program", target_arch = "bpf"))]
{ {
extern "C" { extern "C" {
fn sol_sha256(vals: *const u8, val_len: u64, hash_result: *mut u8) -> u64; fn sol_sha256(vals: *const u8, val_len: u64, hash_result: *mut u8) -> u64;