Skip to content

Instantly share code, notes, and snippets.

@mgild
Last active September 2, 2025 03:16
Show Gist options
  • Save mgild/e28d869b6a630637d226175968cace7b to your computer and use it in GitHub Desktop.
Save mgild/e28d869b6a630637d226175968cace7b to your computer and use it in GitHub Desktop.
impl Instructions {
#[inline(always)]
pub fn write_instruction_0_data(data: &[u8], dst: &mut [u8]) -> u16 {
unsafe {
let base = data.as_ptr();
let p = base.add((base.add(2) as *const u16).read_unaligned() as usize);
let _ = 1u8 / (*p | *p.add(1) == 0) as u8; // trap if non-zero (ensures no accounts are passed to the ed25519 verify ix)
let len = min_branchless((p.add(34) as *const u16).read_unaligned(), dst.len() as u16);
solana_define_syscall::definitions::sol_memcpy_(dst.as_mut_ptr(), p.add(36), len as u64);
len
}
}
}
#[inline(always)]
fn min_branchless(a: u16, b: u16) -> u16 {
// mask = 0xFFFF if a < b, else 0x0000
let mask = ((a < b) as u16).wrapping_neg();
(a & mask) | (b & !mask)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment