Curve Package
The Curve package provides cryptographic operations for Starknet: hash functions, digital signatures, and key management. Use it for signing transactions, generating keys, and computing hashes.
What This Package Does
Cryptographic Hash Functions- Pedersen hash (legacy, widely used)
- Poseidon hash (modern, more efficient)
- Keccak hash
- Starknet Keccak
- Sign messages and transaction hashes
- Verify signatures
- ECDSA operations on the StarkCurve
- Generate random key pairs
- Derive public keys from private keys
- Elliptic curve point operations
Import
import "github.com/NethermindEth/starknet.go/curve"Quick Example
package main
import (
"fmt"
"log"
"math/big"
"github.com/NethermindEth/starknet.go/curve"
)
func main() {
// Generate random keys
privKey, pubKeyX, pubKeyY, err := curve.GetRandomKeys()
if err != nil {
log.Fatal(err)
}
fmt.Printf("Public key: (%s, %s)\n", pubKeyX.String(), pubKeyY.String())
// Sign a message
msgHash := big.NewInt(12345)
r, s, err := curve.Sign(msgHash, privKey)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Signature: (r=%s, s=%s)\n", r.String(), s.String())
// Verify the signature
valid, err := curve.Verify(msgHash, r, s, pubKeyX)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Signature valid: %t\n", valid)
}Available Functions
Hashing Functions
- Pedersen - Pedersen hash of two felts (legacy, widely used for compatibility)
- PedersenArray - Pedersen hash of multiple felts
- Poseidon - Poseidon hash of two felts (modern, more efficient)
- PoseidonArray - Poseidon hash of multiple felts
- StarknetKeccak - Starknet-specific Keccak variant
- ComputeHashOnElements - Hash array of elements using Pedersen
See Hashing Functions for details.
Cryptographic Functions
- Sign - Sign a message hash with private key
- SignFelts - Sign a felt message with felt private key
- Verify - Verify a signature
- VerifyFelts - Verify a felt signature
See Cryptographic Functions for details.
Key Operations
- GetRandomKeys - Generate random key pair
- PrivateToPoint - Derive public key from private key
- GetYCoordinate - Get Y coordinate from X coordinate on curve
See Key Operations for details.
Common Use Cases
Transaction Signing
Sign transaction hashes before submitting to Starknet:
txHash := big.NewInt(0) // Your transaction hash
privKey := big.NewInt(0) // Your private key
r, s, err := curve.Sign(txHash, privKey)
if err != nil {
log.Fatal(err)
}
// Use r, s as signatureHash Computation
Choose the appropriate hash function:
import "github.com/NethermindEth/juno/core/felt"
// Pedersen (for compatibility with existing systems)
hash1 := curve.Pedersen(
new(felt.Felt).SetUint64(123),
new(felt.Felt).SetUint64(456),
)
// Poseidon (for new implementations - more efficient)
hash2 := curve.Poseidon(
new(felt.Felt).SetUint64(123),
new(felt.Felt).SetUint64(456),
)- Use Pedersen for compatibility with older contracts/systems
- Use Poseidon for new implementations (faster and more efficient)
Account Creation
Generate keys for a new Starknet account:
privKey, pubKeyX, pubKeyY, err := curve.GetRandomKeys()
if err != nil {
log.Fatal(err)
}
// pubKeyX is typically used as the Starknet account public key
fmt.Printf("Account public key: %s\n", pubKeyX.String())See the Deploy Account example for the complete account deployment flow.
Security Notes
When using the curve package:
- Private Key Protection - Never log or expose private keys
- Randomness - Use
GetRandomKeys()for secure key generation - Hash Function Selection - Use Poseidon for new code, Pedersen for compatibility
- Signature Verification - Always verify signatures before trusting signed data
Related Packages
- Account Package - Uses curve for transaction signing
- Hash Package - Higher-level hash functions for transactions
- Utils Package - Additional cryptographic utilities
Examples
- Deploy Account - Uses
GetRandomKeys()for account creation - Invoke Contract - Transaction signing with curve functions
Reference
Full API reference: pkg.go.dev

