Skip to content

feat(cac): add chunk-based message serialization for network transmission#63

Merged
Zk2u merged 9 commits intomainfrom
feat/msg-serde
Feb 5, 2026
Merged

feat(cac): add chunk-based message serialization for network transmission#63
Zk2u merged 9 commits intomainfrom
feat/msg-serde

Conversation

@Zk2u
Copy link
Copy Markdown
Collaborator

@Zk2u Zk2u commented Feb 1, 2026

Description

This PR introduces chunk-based message serialization for the CaC protocol to handle large cryptographic data structures efficiently. The changes address two critical constraints:

  1. Network frame limit: Protocol messages must fit within the 4 MiB network frame limit
  2. FoundationDB transaction limits: The fasm framework executes each STF call as a single atomic FDB transaction, which has strict limits on data read/written per transaction

Key Changes

New mosaic-heap-array crate

  • Heap-allocated fixed-size arrays that construct via Vec to avoid stack allocation
  • Solves LLVM optimization hangs with 250+ element arrays of complex cryptographic types (e.g., [PolynomialCommitment; 256])
  • Implements ark-serialize traits for seamless integration

Chunk-based message types

  • CommitMsgChunk: 172 chunks (1 per input wire), ~2.76 MB uncompressed each
  • ChallengeResponseMsgChunk: 174 chunks (1 per opened circuit), ~1.68 MB each
  • AdaptorMsgChunk: 4 chunks (1 per deposit wire), ~1.6 MB uncompressed each
  • ChallengeMsg: Unchanged (fits in single frame at ~1.4 KB)

Updated type aliases to use HeapArray for large arrays:

  • WideLabelWirePolynomialCommitments, WideLabelWireShares, CircuitInputShares
  • ChallengeIndices, WideLabelWireAdaptors, AdaptorMsgChunkWithdrawals

Testing & benchmarks

  • Comprehensive property-based serialization roundtrip tests
  • Benchmark example for measuring serialization performance

Project organization

  • Reorganized net crates under crates/net/ directory
  • Added architecture and network documentation

Type of Change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature/Enhancement (non-breaking change which adds functionality or enhances an existing one)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation update
  • Refactor
  • New or updated tests
  • Dependency update
  • Security fix

Notes to Reviewers

⚠️ Follow-up PR required: The protocol crate (crates/cac/protocol) still references the old monolithic message types and needs to be updated in a separate PR to use the new chunk-based message flow. This is intentionally left for @sapinb to handle as it involves state machine logic changes.

Why chunks?

  1. 4 MiB frame limit: Full messages (especially CommitMsg with 172 wires × 256 polynomial commitments × 174 curve points) exceed the network frame limit
  2. FDB transaction limits: Since fasm executes each STF as a single FoundationDB transaction for atomicity, we cannot read/write arbitrarily large amounts of data. Chunking allows the state machine to process messages incrementally across multiple STF calls, each with its own bounded transaction.

Serialization strategy: We use uncompressed serialization (Compress::No) to reduce computation cost. While compressed is smaller on the wire, decompressing curve points requires solving y² = x³ + 7 which is computationally expensive. Uncompressed avoids this overhead at the cost of ~2x larger payloads (still within frame limits with chunking). Run cargo run --example bench_serde -r -p mosaic-cac-types to see the performance comparison.

…sion

Introduces chunked message types and HeapArray to handle large cryptographic
data structures while staying within the 4 MiB network frame limit and avoiding
LLVM optimization issues with large fixed-size arrays.

Key changes:

- Add `mosaic-heap-array` crate: heap-allocated fixed-size arrays that avoid
  LLVM optimization hangs with 250+ element arrays of complex types

- Replace monolithic messages with chunk-based types in `mosaic-cac-types`:
  - `CommitMsgChunk`: 172 chunks (1 per wire), ~1.4 MB each
  - `ChallengeResponseMsgChunk`: 174 chunks (1 per circuit), ~1.68 MB each
  - `AdaptorMsgChunk`: 4 chunks (1 per deposit wire), ~1.6 MB each
  - `ChallengeMsg`: unchanged (small enough for single frame)

- Update protocol type aliases to use HeapArray for large arrays:
  - `WideLabelWirePolynomialCommitments`, `WideLabelWireShares`
  - `CircuitInputShares`, `ChallengeIndices`, `WideLabelWireAdaptors`

- Add comprehensive serialization tests and benchmarks

- Implement `CanonicalSerialize`/`CanonicalDeserialize` for all message types

- Reorganize net crates under `crates/net/` directory

- Add architecture and network documentation

Note: Protocol state machine (`cac/protocol`) requires further updates to
handle chunk-based message flow.
@Zk2u Zk2u self-assigned this Feb 1, 2026
@Zk2u Zk2u requested review from AaronFeickert and sapinb February 1, 2026 00:12
@Zk2u Zk2u added this to the Network Service milestone Feb 1, 2026
@Zk2u Zk2u added documentation Improvements or additions to documentation enhancement New feature or request labels Feb 1, 2026
@AaronFeickert
Copy link
Copy Markdown
Collaborator

Does this supersede #62?

@AaronFeickert
Copy link
Copy Markdown
Collaborator

Follow-up PR required

Please open an issue for this.

@AaronFeickert AaronFeickert mentioned this pull request Feb 1, 2026
14 tasks
Copy link
Copy Markdown
Collaborator

@AaronFeickert AaronFeickert left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would be very cautious about support for unvalidated deserialization unless we're absolutely sure we need to support it for efficiency reasons. It's a huge footgun.

Comment thread crates/cac/types/src/msgs.rs
Comment thread crates/cac/types/src/serde_tests.rs
Comment thread crates/cac/types/examples/bench_serde.rs Outdated
Comment thread crates/cac/protocol/src/evaluator/stf.rs
@sapinb
Copy link
Copy Markdown
Collaborator

sapinb commented Feb 2, 2026

@Zk2u For future, please prefer smaller atomic commits rather than a single giant one.

@Zk2u Zk2u mentioned this pull request Feb 2, 2026
8 tasks
Zk2u added 3 commits February 2, 2026 11:31
Add tests to verify that invalid curve points and scalars are properly
rejected during deserialization with validation enabled. This covers:
- Corrupted, truncated, and empty data for points
- Out-of-range values and malformed data for scalars

Addresses PR review feedback requesting coverage for invalid input handling.
Replace the ad-hoc example with a proper Criterion benchmark that provides:
- Statistical analysis with multiple iterations
- Throughput measurements (bytes/sec)
- HTML reports in target/criterion/
- Comparison between compressed/uncompressed modes

Addresses PR feedback about unnecessary print statements in benchmarks.
@Zk2u Zk2u requested a review from AaronFeickert February 3, 2026 17:21
Copy link
Copy Markdown
Collaborator

@AaronFeickert AaronFeickert left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I still think supporting unvalidated deserialization is a very bad idea, and we should remove it.

Comment thread crates/cac/types/src/serde_tests.rs Outdated
Zk2u added 2 commits February 5, 2026 14:14
deserialization

ark-serialize rejects values >= field order with InvalidData error,
it does not reduce them mod field order as the comment incorrectly
stated.
@Zk2u Zk2u requested a review from AaronFeickert February 5, 2026 14:36
@Zk2u Zk2u merged commit 0cd0873 into main Feb 5, 2026
10 checks passed
@Zk2u Zk2u deleted the feat/msg-serde branch February 5, 2026 21:07
@Zk2u Zk2u modified the milestones: Network Service, Service Production Ready Feb 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants