Skip to main content
The 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

Available features:
  • derive: Enable derive macros for serialization traits
  • serial: Force serial execution (disable parallelism)
  • wasm: WebAssembly compatibility

Derive Macros

When the derive feature is enabled, you can derive serialization traits:

Module Overview

Serialization

Canonical serialization in little-endian format with compression support.
See Serialization for details.

Parallel Execution

Conditional parallel execution with fallback to serial when the serial feature is enabled.
See Parallel Execution for details.

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 the wasm feature, the crate is compatible with WebAssembly:
This disables features that aren’t available in WASM (like threading).

No Unsafe Code

The utilities crate forbids unsafe code:
This ensures memory safety and makes the crate suitable for security-critical applications.

Testing Utilities

The crate provides utilities specifically for testing:

TestRng

Deterministic random number generator for reproducible tests:

Dev Println

Conditional printing for development:
This only prints when the 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:
Behavior:
  • 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 when serial feature is enabled:
No runtime overhead when parallelism is disabled.

Best Practices

Use Appropriate Serialization Methods

Pre-allocate Collections

Use Parallel Iterators Appropriately

Next Steps