Skip to main content

Overview

The secp256k1 inline provides optimized elliptic curve operations for the secp256k1 curve (used in Bitcoin and Ethereum). It includes field arithmetic, point operations, and ECDSA signature verification.

API Reference

Secp256k1Fq

Base field element (coordinates).

Methods

Secp256k1Fr

Scalar field element (private keys, signatures).

Methods

Secp256k1Point

Affine point on the secp256k1 curve.

Methods

ecdsa_verify()

Verifies an ECDSA signature.

Error Handling

Use .unwrap_or_spoil_proof() to make proofs unsatisfiable on error (e.g., invalid signature).

Usage Examples

ECDSA Signature Verification

Proving Valid Signature (Spoil Proof on Invalid)

Point Addition

Field Arithmetic

Implementation Details

Custom Instructions

The secp256k1 inline provides seven custom instructions:

Base Field (Fq) Operations

  1. SECP256K1_MULQ (funct3=0x00): Multiply two Fq elements
  2. SECP256K1_SQUAREQ (funct3=0x01): Square an Fq element
  3. SECP256K1_DIVQ (funct3=0x02): Divide Fq elements (computes a/b mod q)

Scalar Field (Fr) Operations

  1. SECP256K1_MULR (funct3=0x04): Multiply two Fr elements
  2. SECP256K1_SQUARER (funct3=0x05): Square an Fr element
  3. SECP256K1_DIVR (funct3=0x06): Divide Fr elements (computes a/b mod r)

Advice (Non-deterministic)

  1. SECP256K1_GLVR_ADV (funct3=0x07): GLV scalar decomposition (pure advice, verified in constraints)

Curve Equation

secp256k1: y² = x³ + 7 over the prime field:
Scalar field (order of generator):

GLV Endomorphism

The implementation uses the GLV method to accelerate scalar multiplication:
  • Decompose scalar k into k = k₁ + k₂·λ where |k₁|, |k₂| ≤ 2¹²⁸
  • Compute k·G = k₁·G + k₂·(λ·G) using 4-dimensional windowed multiplication
  • λ = 0x5363ad4cc05c30e0a5261c028812645a122e22ea20816678df02967c1b23bd72
This reduces scalar multiplication from 256 doublings to ~128 combined operations.

ECDSA Verification Algorithm

Point Representation

Points use affine coordinates with infinity represented as (0, 0) (not on curve).

Non-Montgomery Form

Unlike arkworks, this implementation stores field elements in standard (non-Montgomery) form to match inline semantics. Addition/subtraction still use arkworks (same in both forms), but multiplication/division use custom inlines.

Error Types

Constants

Performance Characteristics

  • Field multiplication: ~10-20x faster than pure Rust
  • ECDSA verification: ~50-100x faster end-to-end
  • Scalar multiplication: GLV optimization provides 2x speedup

Feature Flags

  • host: Enables reference implementation for host-side execution
    • Guest code: Compile WITHOUT this feature
    • Prover code: Compile WITH this feature

Source Code Location

See Also