Goldilocks Crypto
ECgFp5 elliptic curve and Schnorr signatures over the Goldilocks field for Rust.
Features
- ECgFp5 Elliptic Curve: Point operations over the Goldilocks field extension
- Schnorr Signatures: Modern signature scheme with Poseidon2-based hashing
- Scalar Field Arithmetic: Efficient scalar operations for cryptographic protocols
- Windowed Scalar Multiplication: Optimized point multiplication for performance
- Type-Safe API: Strong compile-time guarantees for cryptographic operations
Overview
This crate provides elliptic curve cryptography primitives specifically designed for the Goldilocks field:
- ECgFp5 Curve: Elliptic curve operations over the Fp5 extension field
- Schnorr Signatures: Signature generation and verification using Poseidon2 hashing
- Point Arithmetic: Addition, multiplication, encoding, and decoding
- Scalar Field: Efficient scalar operations for private keys and nonces
Dependencies
This crate depends on poseidon-hash for:
- Goldilocks field arithmetic
- Poseidon2 hash function
- Fp5 extension field operations
Installation
Add to your Cargo.toml:
[]
= "0.1"
= "0.1" # Required dependency
Or use the latest version from git (until published):
[]
= { = "https://github.com/elliottech/lighter-rust", = "rust-signer/crypto" }
= { = "https://github.com/elliottech/lighter-rust", = "rust-signer/poseidon-hash" }
Usage
Key Generation
use ;
// Generate a random private key
let private_key = sample_crypto;
// Derive public key
let public_key = generator.mul;
Schnorr Signatures
use ;
// Generate keys
let private_key = sample_crypto;
let private_key_bytes = private_key.to_bytes_le;
// Derive public key
let public_key = generator.mul;
let public_key_bytes = public_key.encode.to_bytes_le;
// Sign a message (40 bytes)
let message = ;
let nonce = sample_crypto;
let nonce_bytes = nonce.to_bytes_le;
let signature = sign_with_nonce.unwrap;
// Verify signature
let is_valid = verify_signature.unwrap;
assert!;
Point Operations
use ;
// Create points
let generator = generator;
let point1 = generator.mul;
let point2 = generator.mul;
// Point addition
let sum = point1.add;
// Point encoding/decoding
let encoded = sum.encode;
let decoded = decode.unwrap;
Private Key Management
use ScalarField;
// Generate a new random private key (recommended)
let private_key = sample_crypto;
let private_key_bytes = private_key.to_bytes_le;
// Create from bytes (40 bytes, little-endian)
let private_key_bytes = ;
let private_key = from_bytes_le.unwrap;
// Convert to bytes
let bytes = private_key.to_bytes_le; // Returns [u8; 40]
// Create from hex string (80 hex characters = 40 bytes)
let hex_key = "0".repeat;
let private_key = from_hex.unwrap;
Integration Guide
Complete Signing Example
Here's a complete example of generating keys, signing, and verifying:
use ;
// Step 1: Generate key pair
let private_key = sample_crypto;
let private_key_bytes = private_key.to_bytes_le;
// Step 2: Derive public key
let generator = generator;
let public_key_point = generator.mul;
let public_key_bytes = public_key_point.encode.to_bytes_le;
// Step 3: Prepare message (must be 40 bytes)
let message = b"Hello, World! This is a 40-byte message!!"; // 40 bytes
assert_eq!;
// Step 4: Generate nonce (CRITICAL: must be cryptographically secure and unique)
let nonce = sample_crypto;
let nonce_bytes = nonce.to_bytes_le;
// Step 5: Sign the message
let signature = sign_with_nonce
.expect;
// Step 6: Verify the signature
let is_valid = verify_signature
.expect;
assert!;
Key Derivation Pattern
use ;
Batch Signature Verification
use verify_signature;
Error Handling
use ;
Optional Features
serde: Enable serialization/deserialization support
[]
= { = "0.1", = ["serde"] }
= { = "0.1", = ["serde"] }
Use Cases
- Transaction signing for blockchain protocols
- Cryptographic signature schemes
- Key exchange protocols
- Zero-Knowledge proof systems requiring curve operations
- Secure authentication mechanisms
Performance
The implementation is optimized for:
- Fast point multiplication using windowed algorithm
- Efficient signature generation and verification
- Low memory allocations
- Production-grade cryptographic security
Security Considerations
- Private Keys: Never expose private keys in logs or error messages
- Nonces: Always use cryptographically secure random nonces (
ScalarField::sample_crypto()) - Side Channels: The implementation is designed to be constant-time where possible
Documentation
Full API documentation is available at docs.rs.
License
Licensed under either of:
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.
Contributing
This library is ported from the Lighter-Go of the Lighter Protocol project. Feel free to contribute or request any changes.