The Serialization Audit: Tracing Every Field Across the Rust-Go Boundary in CuZK
Introduction
In the complex architecture of the CuZK proving engine—a high-performance, GPU-accelerated zero-knowledge proof system for Filecoin—data flows across multiple language boundaries. Rust structs are serialized to JSON, handed to Go for orchestration, potentially forwarded or modified, and then deserialized back into Rust structs by a downstream server. At each boundary, there is an opportunity for data to be silently lost: a field present in Rust's serialized JSON but absent from Go's struct definition will be silently dropped during Go's json.Unmarshal → json.Marshal round-trip. When the Rust server subsequently attempts to deserialize the Go-produced JSON with serde_json::from_slice, a missing required field triggers a deserialization error.
This article chronicles a methodical investigation into precisely this kind of cross-language boundary bug. The session, spanning messages from a user request to a comprehensive 14-level field-by-field comparison, systematically traced every nested type from the top-level SealCommitPhase1Output struct down to the leaf-level PathElement and PhantomData marker fields. The goal was to determine whether the Go Commit1OutRaw struct—and all its nested type definitions—faithfully modeled the Rust serde JSON representation, or whether there were missing fields being dropped during the round-trip.
The Opening Hypothesis
The investigation began with a precise hypothesis from the user ([msg 0]). The CuZK Rust server was intermittently failing when deserializing JSON produced by Go's json.Marshal after it had been unmarshalled from Rust's original JSON. The user suspected a structural mismatch: perhaps a field existed in Rust's SealCommitPhase1Output that was not modeled in Go's Commit1OutRaw, causing it to be silently dropped during the Go round-trip.
The user's request was specific and thorough. They asked the assistant to:
- Find the exact Rust definition of
SealCommitPhase1Outputin thefilecoin-proofs-apicrate (version 19.0) - Trace ALL nested types:
VanillaSealProofenum variants,RawVanillaSealProof, anyPhantomDataor_hmarker fields, and theRegisteredSealProofenum's serde serialization strategy - Find upstream
storage-proofs-corecrate structs includingProof, nested proof data types, and any#[serde(skip)]or#[serde(default)]annotations - Check the
storage-proofs-porepcrate for PoRep-specific proof types The ultimate deliverable was a complete field-by-field comparison between the Rust and Go structs, identifying any fields that would be dropped during the JSON round-trip.
The Investigation Strategy
The assistant's approach was methodical and top-down. Rather than attempting to understand the entire codebase at once, the assistant worked from the known entry point—SealCommitPhase1Output—and expanded outward through the dependency tree.
Phase 1: Locating the Source Files
In [msg 1], the assistant began by globbing for the relevant Rust source files. Two glob patterns were used: one targeting the specific version 19.0 of filecoin-proofs-api, and a broader pattern to catch any version. The results confirmed the existence of seal.rs, types.rs, and registry.rs in the API crate. The assistant also read the Go Commit1OutRaw struct definition from /tmp/czk/lib/proof/porep_vproof_types.go.
This initial reconnaissance revealed the Go struct's fields: CommD, CommR, RegisteredProof, ReplicaID, Seed, Ticket, and VanillaProofs—a map[StringRegisteredProofType][][]VanillaStackedProof. The structure already hinted at how Go modeled Rust's externally-tagged enum: a map with a single key corresponding to the variant name.
Phase 2: Reading the Rust Definitions
In [msg 2], the assistant read the Rust types.rs and registry.rs files, along with the full Go file. The registry.rs file contained the RegisteredSealProof enum—a straightforward enum with no #[serde(rename_all)] annotation, meaning it serializes as a bare PascalCase string. The Go side uses StringRegisteredProofType which is simply type string, making this a direct match.
Message [msg 3] marked a critical step: reading seal.rs to find the actual SealCommitPhase1Output struct definition. The struct had seven fields: registered_proof, vanilla_proofs, comm_r, comm_d, replica_id, seed, and ticket. The assistant also globbed for the upstream crates storage-proofs-porep and storage-proofs-core, locating their source files in the cargo registry.
Phase 3: Descending into the Upstream Crates
With the top-level struct located, the assistant needed to trace every nested type. The VanillaSealProof enum wraps proof types from storage-proofs-porep, specifically the stacked vanilla proof system. In [msg 4], the assistant read five key files from storage-proofs-porep-19.0.1:
params.rs: Defined theProofstruct (the stacked vanilla proof) with fieldscomm_d_proofs,comm_r_last_proof,replica_column_proofs,labeling_proofs, andencoding_proofcolumn.rs: Defined theColumnstruct withindex,rows, and a critical_h: PhantomData<H>fieldlabeling_proof.rs: DefinedLabelingProofwithparents,layer_index,node, and a#[serde(skip)]-annotated_h: PhantomData<H>encoding_proof.rs: DefinedEncodingProofwith the same structure asLabelingProofcolumn_proof.rs: DefinedColumnProofwithcolumnandinclusion_proofThe selection of these five files was not arbitrary. The assistant was systematically walking the type dependency graph, reading each file that defined a type referenced by the proof hierarchy.
Phase 4: The Merkle Proof Chain
Messages [msg 5] through [msg 8] traced the Merkle proof types. The assistant used grep to locate the Proof struct definition in params.rs, then read the MerkleProof type from storage-proofs-core-19.0.1/src/merkle/proof.rs. This revealed the nested chain:
MerkleProofwrapsProofData(an enum with variantsSingle,Sub,Top)ProofData::SinglewrapsSingleProofwithroot,leaf, andpath: InclusionPathProofData::SubwrapsSubProofwithbase_proof,sub_proof,root,leafProofData::TopwrapsTopProofwithbase_proof,sub_proof,top_proof,root,leafInclusionPathwrapsVec<PathElement>PathElementhashashes,index, and a#[serde(skip)]-annotated_arity: PhantomData<Arity>The assistant also discovered a crucial detail in [msg 7]: theVanillaSealProoftype alias. A grep forpub type VanillaSealProofrevealed it was defined asstacked::Proof<Tree, DefaultPieceHasher>in thefilecoin-proofscrate, confirming the type chain.
The Critical PhantomData Investigation
One of the most important threads in this investigation was the handling of PhantomData marker fields. In Rust, PhantomData<T> is a zero-sized type used to track generic type parameters without storing data. By default, serde serializes PhantomData as null. If a struct has a PhantomData field without #[serde(skip)], it will appear in the JSON output as "field_name": null. If the Go side does not model this field, it will be dropped during the round-trip.
The assistant systematically checked every PhantomData field across all structs ([msg 12], [msg 13], [msg 14]). The findings were:
| Struct | PhantomData Field | #[serde(skip)]? | In JSON? | Go Models It? | |--------|------------------|-------------------|----------|---------------| | Column<H> | _h: PhantomData<H> | NO | YES ("_h": null) | YES (H any json:"_h") | | LabelingProof<H> | _h: PhantomData<H> | YES | NO | NO (commented out) | | EncodingProof<H> | _h: PhantomData<H> | YES | NO | NO (commented out) | | PathElement<H, Arity> | _arity: PhantomData<Arity> | YES | NO | NO |
The Column struct was the only one where the PhantomData field leaked into the JSON. However, the Go side already modeled this with H any json:"_h", which would correctly deserialize null as nil and re-serialize it as null. The round-trip was preserved.
The Go Side: Custom Marshaling and the testMarshal Flag
In [msg 9] and [msg 10], the assistant examined the Go Merkle proof types. The Go file merkle.go defined MerkleProof[H HasherDomain] with a Data ProofData[H] field. But more importantly, it revealed a testMarshal flag ([msg 10], line 5) that changed the serialization behavior of Sha256Domain and PoseidonDomain:
var testMarshal = false
type Sha256Domain [32]byte
func (s Sha256Domain) MarshalJSON() ([]byte, error) {
if testMarshal {
return json.Marshal(s[:])
}
return json.Marshal([32]byte(s))
}
When testMarshal is true, domain types serialize as arrays rather than fixed-size byte arrays. This was a subtle but important finding: if the testMarshal flag were ever set to true in production, the JSON output would have a different shape than what Rust expects, potentially causing deserialization failures.
The assistant also checked for custom MarshalJSON/UnmarshalJSON functions on ProofData and Commit1OutRaw ([msg 11]), finding none. This meant the default Go JSON marshaling behavior applied, which was consistent with the struct definitions.
The Existing Round-Trip Test
A serendipitous discovery came in [msg 16] and [msg 17]: there was already a json-roundtrip-check test in the Go codebase at /tmp/czk/lib/proof/porep_vproof_test.go. This test deserialized C1 output into Go types and re-serialized it, then compared with the original. The existence of this test was significant—it meant the developers had already anticipated the round-trip problem and had written a test to catch regressions.
However, the test's existence did not guarantee correctness. The assistant noted that the test might not cover all edge cases, particularly around the testMarshal flag and the specific domain type serialization formats.
The Final Verdict: A 14-Level Field-by-Field Comparison
The culmination of the investigation was the comprehensive field-by-field comparison delivered in [msg 18]. The assistant traced 14 levels of nesting, from SealCommitPhase1Output at the top to PathElement at the bottom. Every field was checked against its Go counterpart.
The verdict was definitive: there were NO missing fields in the Go Commit1OutRaw struct relative to the Rust SealCommitPhase1Output struct. Every Rust field that appears in the serialized JSON was correctly modeled in Go, and every #[serde(skip)] field was correctly omitted from Go.
The comparison covered:
- Level 1:
SealCommitPhase1OutputvsCommit1OutRaw— all 7 fields matched - Level 2:
RegisteredSealProofenum — string serialization matched - Level 3:
VanillaSealProofenum — externally-tagged enum matched by Go'smap[string] - Level 4:
Proof<Tree, G>vsVanillaStackedProof— all 5 fields matched - Level 5:
ReplicaColumnProof— all 3 fields matched - Level 6:
ColumnProof— all 2 fields matched - Level 7:
Column— all 3 fields matched, including the_h: PhantomDatafield - Level 8:
LabelingProof— all 3 fields matched,_hcorrectly skipped - Level 9:
EncodingProof— all 3 fields matched,_hcorrectly skipped - Level 10:
MerkleProof— matched - Level 11:
ProofDataenum — Go's struct-with-pointers model matched Rust's externally-tagged enum - Level 12:
SingleProof,SubProof,TopProof— all fields matched - Level 13:
InclusionPath— matched - Level 14:
PathElement— all fields matched,_aritycorrectly skipped
The Implications
The investigation ruled out one major class of root causes for the CuZK Rust server deserialization failure. The Go struct definitions were not the problem—they faithfully modeled every field that Rust expected.
This forced the investigation to look elsewhere for the intermittent failure. The assistant identified three alternative areas to investigate:
- Value-level differences: Numeric precision issues, byte encoding format differences (e.g., how
[32]byteis serialized vs how Rust'sPoseidonDomainis serialized) - Custom MarshalJSON implementations: The
testMarshalflag inmerkle.gothat changes domain type serialization from fixed-size byte arrays to variable-length arrays - Specific domain type serialization format: Rust's
PoseidonDomainmay serialize differently than Go's[32]byteeven without thetestMarshalflag The investigation also produced a valuable artifact: a complete, 14-level type hierarchy map documenting every field, every serde annotation, and every Go counterpart. This document serves as both a debugging artifact and a reference for future development, ensuring that any changes to the Rust struct definitions can be quickly checked against the Go models.
Conclusion
This chunk of the CuZK debugging session exemplifies systematic cross-language debugging at its finest. The assistant's methodical approach—starting from the known entry point, expanding outward through the dependency tree, checking every PhantomData field, and verifying serde annotations—left no stone unturned. The 14-level field-by-field comparison provided a definitive answer: the Go struct definitions were complete and faithful.
The investigation also demonstrated the importance of understanding serde's serialization semantics, particularly for enums (externally tagged by default) and PhantomData fields (serialized as null unless skipped). The discovery of the testMarshal flag in Go's merkle.go was a bonus finding that could prove critical in subsequent debugging.
By eliminating the structural mismatch hypothesis, the investigation narrowed the search space for the intermittent CuZK failure. The root cause, whatever it turned out to be, would not be found in missing fields—it would be found in the values themselves, in the byte-level encoding, or in the subtle differences between Rust's and Go's JSON serialization of domain types.