This guide walks you through creating a simple SnarkVM application that demonstrates key functionality: account generation, cryptographic operations, and working with field elements.
Before starting, ensure you have installed SnarkVM and have Rust 1.88.0 or higher.
use snarkvm::prelude::*;use anyhow::Result;type CurrentNetwork = MainnetV0;fn main() -> Result<()> { // Initialize a random number generator let rng = &mut snarkvm_utilities::TestRng::default(); // Generate a new private key let private_key = PrivateKey::<CurrentNetwork>::new(rng)?; println!("Private Key: {}", private_key); // Derive the view key let view_key = ViewKey::try_from(&private_key)?; println!("View Key: {}", view_key); // Derive the address let address = Address::try_from(&private_key)?; println!("Address: {}", address); Ok(())}
SnarkVM includes several hash functions optimized for zero-knowledge proofs.
use snarkvm::prelude::*;use anyhow::Result;type CurrentNetwork = MainnetV0;fn main() -> Result<()> { // Create some data to hash let data = Field::<CurrentNetwork>::from_u64(12345); // Hash using Poseidon (optimized for SNARKs) let hash = CurrentNetwork::hash_to_field(&[data])?; println!("Input: {}", data); println!("Poseidon Hash: {}", hash); // Hash multiple inputs let field_a = Field::<CurrentNetwork>::from_u64(100); let field_b = Field::<CurrentNetwork>::from_u64(200); let field_c = Field::<CurrentNetwork>::from_u64(300); let multi_hash = CurrentNetwork::hash_to_field(&[field_a, field_b, field_c])?; println!("Multi-input Hash: {}", multi_hash); Ok(())}
use snarkvm_utilities::TestRng;// For deterministic testinglet rng = &mut TestRng::fixed(12345);// For random generationlet rng = &mut TestRng::default();
TestRng is for testing only. For production applications, use cryptographically secure random number generators.