Skip to main content
SnarkVM’s serialization system provides canonical, deterministic serialization in little-endian format. This ensures that the same data always serializes to the same bytes, which is critical for cryptographic operations.

Core Traits

The serialization system is built around four core traits:
  1. CanonicalSerialize - Serialize data to bytes
  2. CanonicalDeserialize - Deserialize data from bytes
  3. CanonicalSerializeWithFlags - Serialize with metadata flags
  4. CanonicalDeserializeWithFlags - Deserialize with metadata flags

CanonicalSerialize

The primary trait for serializing data to bytes.

Compression Modes

The Compress enum controls whether data is compressed:
Compressed mode:
  • Elliptic curve points: Only x-coordinate + sign bit
  • Field elements: Minimal byte representation
  • Smaller serialized size, slightly slower
Uncompressed mode:
  • Elliptic curve points: Both x and y coordinates
  • Field elements: Full byte representation
  • Larger serialized size, slightly faster

Basic Usage

Custom Compression Control

CanonicalDeserialize

The primary trait for deserializing data from bytes.

Validation Modes

The Validate enum controls whether deserialized data is validated:
Validated mode:
  • Checks mathematical constraints (e.g., point on curve)
  • Checks range constraints (e.g., field element in range)
  • Slower but safer
  • Use for untrusted data
Unchecked mode:
  • Skips validation
  • Faster but potentially unsafe
  • Use only for trusted data

Basic Usage

Custom Validation Control

Valid Trait

Types that implement CanonicalDeserialize must also implement Valid:

Implementing Valid

Batch Validation

The batch_check method validates multiple items, potentially in parallel:
With the parallel feature enabled, this uses multiple threads for validation.

Flags

Flags allow encoding metadata in the serialization.

Use Case: Elliptic Curves

Elliptic curve points use flags to encode the y-coordinate sign:

CanonicalSerializeWithFlags

Serialize data along with metadata flags.

Example: Serializing with Flags

CanonicalDeserializeWithFlags

Deserialize data along with metadata flags.

Example: Deserializing with Flags

Derive Macros

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

Requirements for Derive

All fields must implement the relevant traits:

Standard Type Implementations

Many standard types implement the serialization traits:

Primitive Types

Collections

Tuples

Utility Functions

The serialization module provides utility functions:

Bit/Byte Calculations

SerializationError

The error type for serialization operations:

Error Handling

Best Practices

1. Choose the Right Compression Mode

2. Validate Untrusted Data

3. Use Batch Validation

4. Pre-calculate Sizes

5. Use Derive When Possible

Performance Considerations

Compression Trade-offs

Compressed:
  • ✓ Smaller size (30-50% reduction for curve points)
  • ✓ Better for network transmission
  • ✗ Slower (needs point decompression)
Uncompressed:
  • ✓ Faster serialization/deserialization
  • ✓ Better for in-memory operations
  • ✗ Larger size

Validation Trade-offs

Validated:
  • ✓ Safe for untrusted input
  • ✓ Catches malformed data
  • ✗ Slower (checks mathematical constraints)
Unchecked:
  • ✓ Faster deserialization
  • ✓ Good for trusted data
  • ✗ Unsafe with untrusted input

Batching

Batch operations are more efficient:

Example: Complete Serialization Workflow

Next Steps