snarkvm-utilities crate provides foundational utilities used throughout SnarkVM. It has no dependencies on other SnarkVM crates, making it the base of the dependency tree.
Architecture
The utilities crate is organized into several modules:- Serialization: Traits and implementations for canonical serialization/deserialization
- Parallel: Macros and utilities for parallel execution
- Bits: Bit manipulation and iteration
- Bytes: Byte operations and conversions
- BigInteger: Big integer implementations for field arithmetic
- BitIterator: Efficient iteration over bits
- Errors: Common error types
- Rand: Randomness utilities
Key Features
Zero Dependencies on SnarkVM
The utilities crate is self-contained and has no dependencies on other SnarkVM crates. This makes it suitable for:- Reuse in other projects
- Testing without pulling in the entire VM
- Building new SnarkVM components
Feature Flags
derive: Enable derive macros for serialization traitsserial: Force serial execution (disable parallelism)wasm: WebAssembly compatibility
Derive Macros
When thederive feature is enabled, you can derive serialization traits:
Module Overview
Serialization
Canonical serialization in little-endian format with compression support.Parallel Execution
Conditional parallel execution with fallback to serial when theserial feature is enabled.
Bits and Bytes
Utilities for bit and byte manipulation.BigInteger
Big integer implementations for cryptographic field arithmetic.BitIterator
Efficient iteration over the bits of integers and field elements.Error Handling
The utilities crate provides common error types:SerializationError
Specialized error type for serialization operations:Randomness
Utilities for random number generation in tests and cryptographic operations.Iteration Utilities
Helper types for advanced iteration patterns.Deferred Execution
Execute code when a guard is dropped.Common Patterns
Bit Manipulation
Byte Conversion
Parallel Processing
Batch Processing
Platform Compatibility
Standard Targets
The utilities crate works on all standard Rust targets:- x86_64
- aarch64
- armv7
WebAssembly
With thewasm feature, the crate is compatible with WebAssembly:
No Unsafe Code
The utilities crate forbids unsafe code:Testing Utilities
The crate provides utilities specifically for testing:TestRng
Deterministic random number generator for reproducible tests:Dev Println
Conditional printing for development:dev_println feature is enabled, useful for debugging without cluttering test output.
Performance
CPU Detection
The parallel module detects the CPU type to optimize thread usage:- Intel CPUs: Uses physical core count (avoiding hyperthreading overhead)
- AMD CPUs: Uses all available threads
- Unknown: Uses all available threads
Zero-Cost Abstractions
The parallel macros compile to serial code whenserial feature is enabled:
Best Practices
Use Appropriate Serialization Methods
Pre-allocate Collections
Use Parallel Iterators Appropriately
Next Steps
- Serialization - Detailed serialization traits and methods
- Parallel Execution - Parallel processing utilities and macros