Skip to content

An anchor project to verify Ethereum signed messages on solana and retrieve the public key using the secp256k1 library

Notifications You must be signed in to change notification settings

swymbnsl/anchor-secp256k1

Repository files navigation

Cross-Chain Signature Verification

A Solana program that verifies Ethereum signatures using secp256k1 elliptic curve cryptography, enabling cross-chain authentication between Ethereum and Solana blockchains.

Table of Contents

  1. Overview
  2. Prerequisites
  3. Cryptographic Theory
  4. Architecture
  5. Installation
  6. Usage
  7. Testing
  8. Frontend Application
  9. Security Considerations

Overview

This project implements a cross-chain signature verification system that allows Ethereum signatures to be verified on the Solana blockchain. The system uses the secp256k1 elliptic curve, which is the same curve used by both Bitcoin and Ethereum, enabling seamless cross-chain authentication.

Key Features

  • Ethereum signature verification on Solana
  • secp256k1 public key recovery from signatures
  • Cross-chain message authentication
  • Support for Ethereum's personal message signing format
  • Comprehensive test suite with real signature examples

Prerequisites

  • Rust 1.70.0 or later
  • Solana CLI 1.16.0 or later
  • Anchor Framework 0.29.0 or later
  • Node.js 18.0.0 or later (for testing)
  • MetaMask browser extension (for frontend testing)

Cryptographic Theory

Elliptic Curve Digital Signature Algorithm (ECDSA)

ECDSA is a cryptographic algorithm used for digital signatures. It provides:

  • Authentication: Proves the identity of the signer
  • Non-repudiation: Prevents the signer from denying they created the signature
  • Integrity: Ensures the message hasn't been tampered with

secp256k1 Curve

The secp256k1 curve is defined by the equation:

y² = x³ + 7 (mod p)

where p = 2²⁵⁶ - 2³² - 2⁹ - 2⁸ - 2⁷ - 2⁶ - 2⁴ - 1

secp256k1 Curve

Properties:

  • 256-bit prime field
  • Used by Bitcoin, Ethereum, and many other cryptocurrencies
  • Provides approximately 128-bit security level
  • Efficient implementation available in most cryptographic libraries

Keccak-256 Hash Function

Keccak-256 is a cryptographic hash function that:

  • Produces a 256-bit (32-byte) output
  • Is the core of the SHA-3 family
  • Used by Ethereum for message hashing
  • Provides collision resistance and preimage resistance

Ethereum Message Signing Format

Ethereum uses a specific format for signing messages:

"\x19Ethereum Signed Message:\n" + length(message) + message

This format prevents replay attacks and ensures signatures are only valid for Ethereum messages.

Signature Components

An ECDSA signature consists of three components:

  1. r: First 32 bytes of the signature (x-coordinate of a point on the curve)
  2. s: Second 32 bytes of the signature (y-coordinate of a point on the curve)
  3. v: Recovery ID (1 byte) - determines which of the two possible y-coordinates to use

Public Key Recovery

Given a message hash and signature (r, s, v), the original public key can be recovered using:

  1. Calculate the point R on the curve using r and v
  2. Use the message hash and signature to compute the public key
  3. Verify the signature is valid for the recovered public key

Architecture

Program Structure

programs/cross-chain-verify/src/
├── lib.rs                 # Main program logic
├── instructions/          # Instruction handlers
└── state/                # Account state definitions

Core Components

  1. Signature Verification: Implements secp256k1 signature verification
  2. Public Key Recovery: Recovers Ethereum public keys from signatures
  3. Message Formatting: Handles Ethereum's personal message format
  4. Account Management: Stores verification results on-chain

Data Flow

  1. User signs a message with MetaMask (Ethereum)
  2. Signature components (r, s, v) are extracted
  3. Message is formatted according to Ethereum standards
  4. Solana program verifies the signature using secp256k1
  5. Public key is recovered and stored on-chain
  6. Verification result is returned to the user

Installation

Clone the Repository

git clone <repository-url>
cd cross-chain-verify

Install Dependencies

# Install Rust dependencies
cargo build

# Install Node.js dependencies for testing
yarn install

Build the Program

anchor build

Deploy to Devnet

anchor deploy --provider.cluster devnet

Usage

Basic Signature Verification

import { Program } from "@coral-xyz/anchor"
import { CrossChainVerify } from "./target/types/cross_chain_verify"

// Initialize program
const program = new Program(idl, programId, provider)

// Verify Ethereum signature
const tx = await program.methods
  .verifyEthSignature(
    message, // String message
    signatureR, // [u8; 32] - First 32 bytes of signature
    signatureS, // [u8; 32] - Second 32 bytes of signature
    recoveryId // u8 - Recovery ID (v - 27)
  )
  .accounts({
    verificationAccount: verificationAccount.publicKey,
    user: user.publicKey,
  })
  .signers([verificationAccount])
  .rpc()

Retrieving Verification Data

// Fetch verification results
const accountData = await program.account.verificationData.fetch(
  verificationAccount.publicKey
)

console.log("Message:", accountData.message)
console.log("Is Verified:", accountData.isVerified)
console.log("Signer Public Key:", accountData.signerPubkey)
console.log("Timestamp:", accountData.timestamp)

Testing

Run Tests

anchor test

Test Coverage

The test suite includes:

  • Basic signature verification
  • Multiple account verification
  • Error handling for invalid signatures
  • Public key recovery validation
  • Cross-chain signature format testing

Test Data

The tests use real Ethereum signatures generated from the message "Anchorpoint Assemble" to ensure authenticity and reliability.

Frontend Application

A React-based frontend application is available for generating and testing signatures:

Repository: https://github.com/swymbnsl/secp256k1-test-frontend.git

Frontend Interface

Features:

  • MetaMask integration for message signing
  • Signature component extraction (r, s, recovery_id)
  • Copy-to-clipboard functionality
  • Ready-to-use Solana program call generation
  • Mobile-responsive design

Usage:

  1. Connect MetaMask wallet
  2. Enter message to sign
  3. Generate signature components
  4. Copy components for use in Solana program

Security Considerations

Cryptographic Security

  • Uses industry-standard secp256k1 curve
  • Implements proper signature verification
  • Validates all signature components
  • Handles edge cases and error conditions

Blockchain Security

  • Verifies signatures on-chain
  • Stores verification results immutably
  • Prevents signature replay attacks
  • Validates message format

Best Practices

  • Always verify signatures on-chain
  • Use proper message formatting
  • Validate all input parameters
  • Handle errors gracefully
  • Implement rate limiting if needed

About

An anchor project to verify Ethereum signed messages on solana and retrieve the public key using the secp256k1 library

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published