| Crates.io | solana-pubkey-compare |
| lib.rs | solana-pubkey-compare |
| version | 0.0.0-alpha0.0.0 |
| created_at | 2025-09-05 04:13:31.914716+00 |
| updated_at | 2025-09-05 04:17:27.410516+00 |
| description | High-performance Solana public key comparison using optimized BPF assembly |
| homepage | |
| repository | https://github.com/switchboard-xyz/solana-pubkey-compare |
| max_upload_size | |
| id | 1825079 |
| size | 15,320 |
High-performance Solana public key comparison using optimized BPF assembly.
This crate provides ultra-fast public key comparison for Solana blockchain programs, achieving significant performance improvements through hand-optimized BPF assembly. Perfect for performance-critical Solana programs where compute unit efficiency matters.
#[no_std] compatibleAdd this to your Cargo.toml:
[dependencies]
solana-pubkey-compare = "0.1.0"
use solana_pubkey_compare::fast_eq;
use solana_program::pubkey::Pubkey;
// Compare Solana Pubkeys
let key1 = Pubkey::new_unique();
let key2 = Pubkey::new_unique();
if fast_eq(&key1, &key2) {
// Keys are equal - this is very fast!
}
// Works with any 32-byte types
let bytes1: [u8; 32] = [0; 32];
let bytes2: [u8; 32] = [1; 32];
assert!(!fast_eq(&bytes1, &bytes2));
use solana_program::{
account_info::AccountInfo,
entrypoint::ProgramResult,
pubkey::Pubkey,
};
use solana_pubkey_compare::fast_eq;
pub fn process_instruction(
program_id: &Pubkey,
accounts: &[AccountInfo],
_instruction_data: &[u8],
) -> ProgramResult {
let expected_authority = Pubkey::from([/* your authority key */]);
// Fast authority check - saves compute units!
if !fast_eq(&accounts[0].key, &expected_authority) {
return Err(/* unauthorized error */);
}
// Continue processing...
Ok(())
}
use solana_pubkey_compare::fast_eq;
fn find_account_index(accounts: &[AccountInfo], target: &Pubkey) -> Option<usize> {
accounts.iter().position(|account| {
// Each comparison saves ~9 compute units
fast_eq(&account.key, target)
})
}
The core of this crate is a hand-optimized BPF assembly function that:
ldxdw to load 8 bytes at a timejne) to exit immediately on first mismatch// Compare bytes 0-7
ldxdw r3, [r1+0] // Load first 8 bytes of key1
ldxdw r4, [r2+0] // Load first 8 bytes of key2
jne r3, r4, not_equal // Exit early if different
// Repeat for bytes 8-15, 16-23, 24-31...
// Return 1 if all equal, 0 if any different
On non-Solana platforms, the function falls back to the standard PartialEq implementation for compatibility with testing and development workflows.
Performance measurements on Solana BPF runtime:
| Operation | Standard PartialEq |
fast_eq |
Improvement |
|---|---|---|---|
| Equal keys | 28 CU | 19 CU | 32% faster |
| Different keys (first byte) | 28 CU | 11 CU | 61% faster |
| Different keys (last byte) | 28 CU | 19 CU | 32% faster |
CU = Compute Units
The generic type T must implement:
AsRef<[u8]> - For accessing the underlying byte dataPartialEq - For the native fallback implementationThis includes:
solana_program::pubkey::Pubkey[u8; 32]Vec<u8> (if exactly 32 bytes)This function is completely safe to call. While it uses unsafe internally to interface with the assembly function, all safety invariants are maintained:
# Build for native (testing)
cargo build
# Build for Solana BPF
cargo build-sbf
# Run tests (uses fallback implementation)
cargo test
# Test on Solana BPF runtime
cargo test-sbf
Contributions are welcome! Please ensure:
#[no_std] compatibilityMIT License - see LICENSE file for details.
Created by Switchboard for high-performance Solana applications.