Solana SDK

repository·master·Indexed 42 days ago

https://github.com/anza-xyz/solana-sdk

The official Rust SDK for the Solana blockchain, providing foundational libraries for on-chain programs (smart contracts) and the Agave validator. Includes specialized crates such as solana-bls12-381 for optimized elliptic curve operations and pairing-based cryptography, as well as tools for compute unit (CU) monitoring and program logging.

Tokens
122.6K
Snippets
447
Records
545
Agent score
53%

What's inside Solana SDK

  1. Overview of solana-bls12-381

    master

    The solana-bls12-381 crate provides BLS12-381 elliptic curve operations specifically optimized for Solana programs. It acts as a wrapper around native syscalls defined in SIMD-0388.

    It is designed for on-chain pairing-based cryptography, such as:

    • BLS signature verification
    • Zero-knowledge proof (e.g., Groth16) validation
  2. What is the Solana System Interface?

    master

    The solana-system-interface crate provides instructions and constructors for interacting with the Solana System program. The System program is used for core account operations, including:

    • Creating new accounts
    • Allocating account data
    • Assigning accounts to owning programs
    • Transferring lamports from System Program owned accounts
    • Paying transaction fees
  3. Choose between solana-program and solana-sdk

    master

    When building on Solana, select the appropriate crate based on your target environment:

    • Use solana-program if you are writing on-chain programs (smart contracts) in Rust.
    • Use solana-sdk if you are writing client-side applications (off-chain tools, wallets, or scripts) that interact with the blockchain.
  4. Follow the output buffer contract for _assign methods

    master

    All _assign methods in this crate follow a strict contract regarding the output buffer:

    • On true: The output buffer is fully initialized and safe to use via assume_init().
    • On false: The buffer must be treated as uninitialized and must not be read. A failed operation may have partially written to the buffer, poisoning any previous contents.
  5. How solana-program-log reduces compute unit costs

    master

    Standard logging in Solana (like the msg! macro) uses Rust's built-in format! routines, which are CPU-intensive in constrained environments. solana-program-log replaces these with a lightweight formatting routine that avoids additional allocations.

    Cost Components:

    1. Base Cost: The cost of the log syscall (either 100 CU or the message length, whichever is higher).
    2. Formatting Cost: The CU required to format the message. solana-program-log minimizes this variable cost.

    Observed Improvements:

    • Integers (u64): ~55% improvement over msg!.
    • String Arrays ([&str; 2]): ~93% improvement over msg!.
    • Address Bytes ([u8; 32]): ~67% improvement over msg!.
    • Precision Formatting: Up to 84% improvement.
  6. Choose between Solana SDK and Solana Program crates

    master

    The choice of crate depends on where your code will execute:

    • Client-side applications: Use the solana-sdk crate to write applications in Rust that interact with the Solana blockchain.
    • On-chain programs (Smart Contracts): Use the solana-program crate instead of solana-sdk.

    For practical implementation examples, refer to the Solana Program Library.

  7. Key features of solana-bls12-381

    master

    The crate includes several performance and memory optimizations for on-chain use:

    • Zero-copy deserialization: Types use #[repr(transparent)] and implement bytemuck::Pod. This allows instruction data to be cast directly into curve points without additional allocation.
    • In-place operations: Most group operations provide an _assign variant. These write results into a caller-supplied MaybeUninit buffer, which saves approximately 37 Compute Units (CU) per call and prevents large results from consuming the 4KB stack.
    • Validated and unchecked APIs: Standard group operations validate operands by default. For cheaper accumulation where validation is handled elsewhere, use the _unchecked variants to skip subgroup checks.
    • Pairing checks: The pairing_check function verifies if a product of pairings is the identity without needing to materialize a 576-byte Gt element. Note that pairing_check will fail if provided with an empty batch.
    • Dual endianness: Supports both Big-endian (canonical Zcash/IETF encoding) and little-endian layouts.
  8. Understand validation and subgroup checks

    master

    The validate method performs three checks: Field element verification, Curve equation satisfaction, and Subgroup membership.

    Most group operations (like mul_assign, decompress_assign, and pairing_check) perform all three checks automatically. The _unchecked variants (like add_assign_unchecked) perform the Field and Curve checks but skip the expensive subgroup check.

    Because the subgroup is closed under addition, you can validate points once at the entry point and then safely use _unchecked methods for all subsequent accumulations.

  9. Build the solana-sdk from source

    master

    To build the SDK from source, follow these steps:

    1. Install Rust toolchain components: Install rustc, cargo, and rustfmt using rustup.
    2. Download source: Clone the repository from GitHub.
    3. Verify toolchain: Ensure you are using the version specified in rust-toolchain.toml by running rustup show.
    4. Run tests: Execute cargo test to verify the build.
    # 1. Install rustc, cargo and rustfmt.
    curl https://sh.rustup.rs -sSf | sh
    source $HOME/.cargo/env
    rustup component add rustfmt
    
    # 2. Download the source code.
    git clone https://github.com/anza-xyz/solana-sdk.git
    cd solana-sdk
    
    # Verify toolchain
    rustup show
    
    # 3. Test.
    cargo test
  10. Install solana-system-interface

    master

    To add the solana-system-interface crate to your project, use cargo add. It is recommended to enable the bincode feature, as it provides the instruction constructors necessary to create instructions for the System program.

    cargo add solana-system-interface --features bincode
  11. Patch a local solana-sdk repository for Agave

    master

    If you are developing for Agave and need to use a local checkout of solana-sdk crates, use the provided patching script.

    Patch all crates: Run the script with the path to your Agave repository and the path to your local solana-sdk repository.

    Patch a specific crate: Provide the relative path to the specific crate as the third argument.

    Example for patching solana-bn254:

    ./scripts/patch-crates-no-header.sh ../agave . bn254
    # Patch all crates
    ./scripts/patch-crates-no-header.sh <AGAVE_PATH> <SOLANA_SDK_PATH>
    
    # Patch just one crate
    ./scripts/patch-crates-no-header.sh <AGAVE_PATH> <SOLANA_SDK_PATH> <RELATIVE_PATH>