Core Traits
The serialization system is built around four core traits:CanonicalSerialize- Serialize data to bytesCanonicalDeserialize- Deserialize data from bytesCanonicalSerializeWithFlags- Serialize with metadata flagsCanonicalDeserializeWithFlags- Deserialize with metadata flags
CanonicalSerialize
The primary trait for serializing data to bytes.Compression Modes
TheCompress enum controls whether data is compressed:
- Elliptic curve points: Only x-coordinate + sign bit
- Field elements: Minimal byte representation
- Smaller serialized size, slightly slower
- 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
TheValidate enum controls whether deserialized data is validated:
- Checks mathematical constraints (e.g., point on curve)
- Checks range constraints (e.g., field element in range)
- Slower but safer
- Use for untrusted data
- Skips validation
- Faster but potentially unsafe
- Use only for trusted data
Basic Usage
Custom Validation Control
Valid Trait
Types that implementCanonicalDeserialize must also implement Valid:
Implementing Valid
Batch Validation
Thebatch_check method validates multiple items, potentially in parallel:
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 thederive 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)
- ✓ 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)
- ✓ Faster deserialization
- ✓ Good for trusted data
- ✗ Unsafe with untrusted input
Batching
Batch operations are more efficient:Example: Complete Serialization Workflow
Next Steps
- Utilities Overview - Overview of all utility modules
- Parallel Execution - Parallel processing utilities