Prerequisites
Before getting started with Forge EC, ensure you have the following installed:
- Rust 1.70+ - Install from rustup.rs
- Cargo - Comes with Rust installation
- Git - For cloning repositories (optional)
Installation
Add Forge EC to your Rust project using Cargo:
[dependencies]
forge-ec = "0.1.0"
Or install using the command line:
cargo add forge-ec
Your First Example
Let's create a simple example that demonstrates key generation and digital signatures:
use forge_ec::ecdsa::*;
use forge_ec::curves::secp256k1::*;
fn main() -> Result<(), Box> {
println!("🦀 Welcome to Forge EC!");
// Generate a new private key
let private_key = PrivateKey::new();
let public_key = private_key.public_key();
println!("✅ Generated keypair successfully");
// Message to sign
let message = b"Hello, Forge EC!";
// Sign the message
let signature = private_key.sign(message)?;
println!("✅ Message signed");
// Verify the signature
let is_valid = public_key.verify(message, &signature)?;
println!("✅ Signature verification: {}", is_valid);
Ok(())
}
Run this example with:
cargo run
🦀 Welcome to Forge EC! ✅ Generated keypair successfully ✅ Message signed ✅ Signature verification: true
Key Generation
Forge EC provides secure key generation using cryptographically secure random number generators:
use forge_ec::ecdsa::PrivateKey;
use forge_ec::curves::secp256k1::Secp256k1;
// Generate a new random private key
let private_key = PrivateKey::::new();
// Derive the corresponding public key
let public_key = private_key.public_key();
// Export keys for storage (hex format)
let private_hex = private_key.to_hex();
let public_hex = public_key.to_hex();
println!("Private key: {}", private_hex);
println!("Public key: {}", public_hex);
Digital Signatures
Create digital signatures to prove authenticity and integrity of your data:
use forge_ec::ecdsa::*;
use forge_ec::curves::secp256k1::*;
fn sign_message(private_key: &PrivateKey, message: &[u8]) -> Result {
// Hash the message (SHA-256 is used internally)
let signature = private_key.sign(message)?;
println!("Message signed successfully");
println!("Signature: {}", signature.to_hex());
Ok(signature)
}
// Usage
let private_key = PrivateKey::new();
let message = b"Important document content";
let signature = sign_message(&private_key, message)?;
Signature Verification
Verify digital signatures to ensure data authenticity:
use forge_ec::ecdsa::*;
fn verify_signature(
public_key: &PublicKey,
message: &[u8],
signature: &Signature
) -> Result {
let is_valid = public_key.verify(message, signature)?;
if is_valid {
println!("✅ Signature is valid - message is authentic");
} else {
println!("❌ Signature is invalid - message may be tampered");
}
Ok(is_valid)
}
// Usage
let is_authentic = verify_signature(&public_key, message, &signature)?;
assert!(is_authentic);
Next Steps
Congratulations! You've successfully created your first Forge EC application. Here's what to explore next: