Add ristretto multiply syscall (#12699)

This commit is contained in:
Jack May
2020-10-06 23:52:13 -07:00
committed by GitHub
parent d0596ce9c8
commit 973f0965e1
12 changed files with 195 additions and 12 deletions

View File

@@ -0,0 +1,31 @@
//! @brief Example Rust-based BPF program that performs a ristretto multiply
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");
let point = RISTRETTO_BASEPOINT_POINT;
let scalar = Scalar::zero();
let result = ristretto_mul(&point, &scalar)?;
assert_ne!(point, result);
let point = RISTRETTO_BASEPOINT_POINT;
let scalar = Scalar::one();
let result = ristretto_mul(&point, &scalar)?;
assert_eq!(point, result);
Ok(())
}

View File

@@ -0,0 +1,33 @@
use curve25519_dalek::{ristretto::RistrettoPoint, scalar::Scalar};
use solana_sdk::{entrypoint::SUCCESS, program_error::ProgramError};
/// Multiply a ristretto point with a scalar
///
/// @param point - Ristretto point
/// @param scalar - Scalar to mulitply against
/// @return - result of the multiplication
#[inline]
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()),
}
}
extern "C" {
fn sol_ristretto_mul(
point_addr: *const u8,
scalar_addr: *const u8,
result_addr: *mut u8,
) -> u64;
}