Skip to main content
Secp256r1 (also known as P-256) is a widely adopted elliptic curve for public-key cryptography, supported by most modern cryptographic libraries and hardware. It is defined by parameters: prime p, coefficients a and b, base point G, and its prime order n. Unlike native Ed25519, secp256r1 requires a precompiled program for signature verification.

Why use Secp256r1 with Swig?

  • Native support for passkeys and hardware-backed authentication
  • Secure, standards-based cryptography
  • Compatible with modern browsers and devices
  • Enables session-based authorities for limited-lifetime delegation

Technical Overview

Secp256r1 for SWIG Wallet: Secp256r1 Authority: Represents an account or role that can sign transactions using a Secp256r1 key. Session Authority: Supports session-based authorities for limited-lifetime delegation. Signature Odometer: Each authority tracks a counter to prevent replay attacks.

How to Use Secp256r1 Authority

The walkthrough covers both Rust and TypeScript implementations:
  1. Key Generation
  2. Authority Management
  3. Transaction Signing & Replay Protection
  4. Session Authorities

TypeScript Implementation

Key Generation & Basic Usage

Here’s a complete TypeScript example using secp256r1 keys with Swig Wallet:

WebAuthn Passkey Integration

For browser-based applications, you can integrate WebAuthn passkeys:

Rust Implementation

A. Key Generation:

To use Secp256r1, a valid key pair is needed. A create_secp256r1_keypair function handles this using the openssl crate.
  • Curve: It uses the Nid::X9_62_PRIME256V1 curve, which is the standard for P-256.
  • Public Key Format: The public key is serialized into a 33-byte compressed format. This is crucial, as it’s the format the on-chain program expects.

B. Authority Management:

A SWIG account can be created with a secp256r1 key as its primary authority or have one added later. This gives the secp256r1 key holder control over the account. For creating an Account with a secp256r1 Authority, the create_swig_secp256r1 function shows how to initialize a new swig account where a secp256r1 public key is the owner.
A new secp256r1 authority can be added to an existing account. This operation must be signed by a current authority. The test test_secp256r1_add_authority_with_secp256r1 demonstrates a secp256r1 key authorizing the addition of another secp256r1 key.

C. Transaction Signing & Replay Protection:

Signing transactions with secp256r1 is more complex than with Ed25519. It involves a critical feature for security: the signature_odometer. The signature_odometer: This is an on-chain counter (u32) that is part of the Secp256r1Authority struct. A get_secp256r1_counter helper function reads this value from the swig account’s data.
The test_secp256r1_basic_signing test provides a perfect, low-level example of the signing flow.

D. Session Authorities:

SWIG also supports session-based secp256r1 authorities. These are temporary keys with a defined lifespan, measured in Solana slots.

Live Examples

Explore these working implementations to see secp256r1 authorities in action:

TypeScript secp256r1 Transfer

Complete TypeScript example demonstrating secp256r1 key generation, Swig creation, and transfer operations using LiteSVM

Passkey Helper Functions

TypeScript helper functions for WebAuthn passkey integration with secp256r1 authorities

Interactive Passkey UI

Complete UI example showing how to build user-facing passkey authentication with secp256r1

Key Differences: TypeScript vs Rust

Both implementations provide the same cryptographic security and are fully compatible with the Swig protocol. Choose based on your deployment environment and integration needs.