{"eip":8016,"title":"SSZ CompatibleUnion","description":"SSZ type for forward-compatible unions","authors":["Etan Kissling (@etan-status)","Cayman (@wemeetagain)"],"status":"Draft","type":"Standards Track","category":"Core","created":"2025-08-28","requires":[7495,7916],"discussionsTo":"https://ethereum-magicians.org/t/eip-8016-ssz-compatibleunion/25275","repo":"EIPs","label":"EIP-8016","officialUrl":"https://eips.ethereum.org/EIPS/eip-8016","sourceUrl":"https://github.com/ethereum/EIPs/blob/master/EIPS/eip-8016.md","url":"https://eips.huatuo.xyz/eip-8016/","tldr":{"en":"Defines CompatibleUnion, an SSZ tagged-union type whose variants must Merkleize compatibly, so a field keeps the same generalized index (and proof path) no matter which variant is used.","ja":"バリアントが互換的に Merkleize されなければならない SSZ のタグ付きユニオン型 CompatibleUnion を定義する。どのバリアントを使っても、フィールドは同じ generalized index（と証明パス）を保つ。","ko":"변형들이 호환되는 방식으로 Merkleize되어야 하는 SSZ 태그드 유니온 타입 CompatibleUnion을 정의한다. 어떤 변형을 쓰더라도 필드는 같은 generalized index(와 증명 경로)를 유지한다.","zh":"定义 CompatibleUnion，一种 SSZ 带标签联合类型，其各变体必须以兼容的方式 Merkleize，这样无论使用哪个变体，字段都保持相同的 generalized index（及证明路径）。"},"tags":["ssz","serialization","merkle","consensus","core"],"explainers":{"en":{"lang":"en","tldr":"Defines CompatibleUnion, an SSZ tagged-union type whose variants must Merkleize compatibly, so a field keeps the same generalized index (and proof path) no matter which variant is used.","tags":["ssz","serialization","merkle","consensus","core"],"related":[7495,7916,7688],"updated":"2026-10-19","url":"https://eips.huatuo.xyz/eip-8016/","markdown":"## What problem does it solve?\n\nSome Ethereum objects come in several variants that share most of their fields; transaction types are the obvious example. If each variant is a separate SSZ container, the same field (say `to` or `value`) can end up at a different position in the Merkle tree for each variant. A light client or contract that verifies Merkle proofs then needs a per-variant table of field positions, and that table has to be updated whenever a new variant appears.\n\nEIP-8016 adds a union type that only accepts variants whose trees line up, so a common field always has the same **generalized index (gindex)** across all of them.\n\n## How it works\n\n- **Notation:** `CompatibleUnion({selector: type})`, for example `CompatibleUnion({1: Square, 2: Circle})`. Selectors must be in `1..127`; `0` and values above `127` are reserved.\n- **Compatibility rules:** variants must Merkleize the same way. Identical types are compatible; lists, vectors and bitfields need the same capacity; `Container`s need the same field names in the same order. [ProgressiveContainer](/eip-7495/) variants are compatible when their shared active fields have the same names and compatible types and no other field name is shared. [ProgressiveList](/eip-7916/) needs a compatible element type.\n- **Serialization:** one selector byte followed by the serialized value. The type is always variable-size and has no default value.\n- **Merkleization:** `hash(hash_tree_root(data), selector)`, i.e. the selector is mixed in. This guarantees two variants with identical tree shapes still get distinct roots.\n- **JSON:** `{ \"selector\": \"\u003cn\u003e\", \"data\": ... }`.\n\n## Sample code\n\nIllustrative Python in the style of the consensus specs:\n\n```python\nclass Square(ProgressiveContainer(active_fields=[1, 0, 1])):\n    side: uint16\n    color: uint8\n\nclass Circle(ProgressiveContainer(active_fields=[0, 1, 1])):\n    radius: uint16\n    color: uint8\n\nShape = CompatibleUnion({1: Square, 2: Circle})\n\ndef serialize_union(value) -\u003e bytes:\n    return value.selector.to_bytes(1, \"little\") + serialize(value.data)\n\ndef hash_tree_root_union(value) -\u003e Root:\n    return hash(hash_tree_root(value.data), value.selector.to_bytes(1, \"little\"))\n\n# `color` sits at the same gindex in both Square and Circle,\n# so one Merkle proof path works for every Shape.\n```\n\n## Things to know\n\n- Status is **Draft**. It is part of the broader effort to move Ethereum data structures to SSZ, alongside [EIP-7495](/eip-7495/) and [EIP-7916](/eip-7916/) (both in Review).\n- Wrapping an existing field in a `CompatibleUnion` is **not** backwards compatible, so the spec recommends introducing it early wherever more variants are expected, even with only one option today. After that, options can be added or dropped without breaking verifiers.\n- It replaces an earlier SSZ union design that was only used in deprecated specs.\n- Reference implementation: `ethereum/remerkleable`.\n"},"ja":{"lang":"ja","tldr":"バリアントが互換的に Merkleize されなければならない SSZ のタグ付きユニオン型 CompatibleUnion を定義する。どのバリアントを使っても、フィールドは同じ generalized index（と証明パス）を保つ。","tags":["ssz","serialization","merkle","consensus","core"],"related":[7495,7916,7688],"updated":"2026-10-19","url":"https://eips.huatuo.xyz/ja/eip-8016/","markdown":"## 何を解決するのか\n\nEthereum のオブジェクトには、ほとんどのフィールドを共有する複数のバリアントを持つものがあり、トランザクションタイプがその代表例です。各バリアントを別々の SSZ コンテナにすると、同じフィールド（たとえば `to` や `value`）が、バリアントごとに Merkle ツリーの異なる位置に置かれることがあります。すると、Merkle proof を検証するライトクライアントやコントラクトは、バリアントごとのフィールド位置の表を持つ必要があり、新しいバリアントが現れるたびにその表を更新しなければなりません。\n\nEIP-8016 は、ツリーの形が揃っているバリアントだけを受け入れるユニオン型を追加し、共通フィールドがすべてのバリアントで常に同じ **generalized index（gindex）** を持つようにします。\n\n## 仕組み\n\n- **表記:** `CompatibleUnion({selector: type})`。たとえば `CompatibleUnion({1: Square, 2: Circle})` です。セレクターは `1..127` の範囲でなければならず、`0` と `127` を超える値は予約されています。\n- **互換性ルール:** バリアントは同じ方法で Merkleize される必要があります。同一の型は互換です。リスト、ベクトル、ビットフィールドは同じ容量が必要です。`Container` は同じフィールド名が同じ順序で並んでいる必要があります。[ProgressiveContainer](/eip-7495/) のバリアントは、共有するアクティブフィールドが同じ名前と互換性のある型を持ち、それ以外にフィールド名を共有しない場合に互換となります。[ProgressiveList](/eip-7916/) は互換性のある要素型が必要です。\n- **シリアライズ:** セレクター 1 バイトに続いてシリアライズされた値が来ます。この型は常に可変サイズで、デフォルト値はありません。\n- **Merkleization:** `hash(hash_tree_root(data), selector)`、つまりセレクターが混ぜ込まれます。これにより、ツリーの形が同一の 2 つのバリアントでも、異なるルートになることが保証されます。\n- **JSON:** `{ \"selector\": \"\u003cn\u003e\", \"data\": ... }`。\n\n## サンプルコード\n\nコンセンサス仕様のスタイルによる、説明用の Python:\n\n```python\nclass Square(ProgressiveContainer(active_fields=[1, 0, 1])):\n    side: uint16\n    color: uint8\n\nclass Circle(ProgressiveContainer(active_fields=[0, 1, 1])):\n    radius: uint16\n    color: uint8\n\nShape = CompatibleUnion({1: Square, 2: Circle})\n\ndef serialize_union(value) -\u003e bytes:\n    return value.selector.to_bytes(1, \"little\") + serialize(value.data)\n\ndef hash_tree_root_union(value) -\u003e Root:\n    return hash(hash_tree_root(value.data), value.selector.to_bytes(1, \"little\"))\n\n# `color` sits at the same gindex in both Square and Circle,\n# so one Merkle proof path works for every Shape.\n```\n\n## 知っておくべきこと\n\n- ステータスは **Draft** です。Ethereum のデータ構造を SSZ に移行するより広い取り組みの一部で、[EIP-7495](/eip-7495/) や [EIP-7916](/eip-7916/)（どちらも Review）と並ぶものです。\n- 既存のフィールドを `CompatibleUnion` で包むことは、後方互換では**ありません**。そのため仕様では、現時点で選択肢が 1 つだけでも、バリアントが増えそうな箇所には早めに導入することを推奨しています。その後は、検証側を壊さずに選択肢を追加・削除できます。\n- 非推奨の仕様でしか使われていなかった、以前の SSZ ユニオン設計に置き換わるものです。\n- リファレンス実装: `ethereum/remerkleable`。\n"},"ko":{"lang":"ko","tldr":"변형들이 호환되는 방식으로 Merkleize되어야 하는 SSZ 태그드 유니온 타입 CompatibleUnion을 정의한다. 어떤 변형을 쓰더라도 필드는 같은 generalized index(와 증명 경로)를 유지한다.","tags":["ssz","serialization","merkle","consensus","core"],"related":[7495,7916,7688],"updated":"2026-10-19","url":"https://eips.huatuo.xyz/ko/eip-8016/","markdown":"## 어떤 문제를 해결하나요?\n\n이더리움의 일부 객체는 대부분의 필드를 공유하는 여러 변형으로 존재하며, 트랜잭션 타입이 대표적인 예입니다. 각 변형이 별도의 SSZ 컨테이너라면, 같은 필드(예: `to`나 `value`)가 변형마다 Merkle 트리의 다른 위치에 놓일 수 있습니다. 그러면 Merkle proof를 검증하는 라이트 클라이언트나 컨트랙트는 변형별 필드 위치 표가 필요하고, 새 변형이 생길 때마다 그 표를 갱신해야 합니다.\n\nEIP-8016은 트리 구조가 맞는 변형만 받아들이는 유니온 타입을 추가해, 공통 필드가 모든 변형에서 항상 같은 **generalized index(gindex)**를 갖도록 합니다.\n\n## 어떻게 동작하나요?\n\n- **표기법:** `CompatibleUnion({selector: type})`, 예를 들어 `CompatibleUnion({1: Square, 2: Circle})`. selector는 `1..127` 범위여야 하며, `0`과 `127`을 넘는 값은 예약되어 있습니다.\n- **호환성 규칙:** 변형들은 같은 방식으로 Merkleize되어야 합니다. 동일한 타입은 호환됩니다. 리스트, 벡터, 비트필드는 같은 용량이 필요합니다. `Container`는 같은 필드 이름이 같은 순서로 있어야 합니다. [ProgressiveContainer](/eip-7495/) 변형은 공유하는 활성 필드가 같은 이름과 호환되는 타입을 가지고 다른 필드 이름을 공유하지 않을 때 호환됩니다. [ProgressiveList](/eip-7916/)는 호환되는 요소 타입이 필요합니다.\n- **직렬화:** selector 바이트 하나 뒤에 직렬화된 값이 옵니다. 이 타입은 항상 가변 크기이며 기본값이 없습니다.\n- **Merkleization:** `hash(hash_tree_root(data), selector)`, 즉 selector가 섞여 들어갑니다. 이로써 트리 모양이 같은 두 변형도 서로 다른 루트를 갖게 됩니다.\n- **JSON:** `{ \"selector\": \"\u003cn\u003e\", \"data\": ... }`.\n\n## 샘플 코드\n\n컨센서스 사양 스타일의 설명용 Python:\n\n```python\nclass Square(ProgressiveContainer(active_fields=[1, 0, 1])):\n    side: uint16\n    color: uint8\n\nclass Circle(ProgressiveContainer(active_fields=[0, 1, 1])):\n    radius: uint16\n    color: uint8\n\nShape = CompatibleUnion({1: Square, 2: Circle})\n\ndef serialize_union(value) -\u003e bytes:\n    return value.selector.to_bytes(1, \"little\") + serialize(value.data)\n\ndef hash_tree_root_union(value) -\u003e Root:\n    return hash(hash_tree_root(value.data), value.selector.to_bytes(1, \"little\"))\n\n# `color` sits at the same gindex in both Square and Circle,\n# so one Merkle proof path works for every Shape.\n```\n\n## 알아둘 점\n\n- 상태는 **Draft**입니다. 이더리움 데이터 구조를 SSZ로 옮기려는 더 큰 노력의 일부이며, [EIP-7495](/eip-7495/), [EIP-7916](/eip-7916/)(둘 다 Review)과 함께 진행됩니다.\n- 기존 필드를 `CompatibleUnion`으로 감싸는 것은 하위 호환이 **아닙니다**. 그래서 사양은 지금은 선택지가 하나뿐이더라도 변형이 늘어날 것으로 예상되는 곳에는 일찍 도입할 것을 권장합니다. 그 이후에는 검증기를 깨뜨리지 않고 선택지를 추가하거나 제거할 수 있습니다.\n- 폐기된 사양에서만 쓰였던 이전의 SSZ 유니온 설계를 대체합니다.\n- 참조 구현: `ethereum/remerkleable`.\n"},"zh":{"lang":"zh","tldr":"定义 CompatibleUnion，一种 SSZ 带标签联合类型，其各变体必须以兼容的方式 Merkleize，这样无论使用哪个变体，字段都保持相同的 generalized index（及证明路径）。","tags":["ssz","serialization","merkle","consensus","core"],"related":[7495,7916,7688],"updated":"2026-10-19","url":"https://eips.huatuo.xyz/zh/eip-8016/","markdown":"## 它解决什么问题？\n\n以太坊的一些对象有多个变体，且大部分字段相同；交易类型就是最明显的例子。如果每个变体都是单独的 SSZ 容器，同一个字段（比如 `to` 或 `value`）在不同变体的 Merkle 树中可能处于不同位置。这样，验证 Merkle proof 的轻客户端或合约就需要一张按变体划分的字段位置表，每出现一个新变体都要更新这张表。\n\nEIP-8016 增加了一种联合类型，只接受树结构对齐的变体，使公共字段在所有变体中始终具有相同的 **generalized index（gindex）**。\n\n## 它如何运作\n\n- **记法：** `CompatibleUnion({selector: type})`，例如 `CompatibleUnion({1: Square, 2: Circle})`。selector 必须在 `1..127` 范围内；`0` 和大于 `127` 的值被保留。\n- **兼容性规则：** 各变体必须以相同方式 Merkleize。相同的类型互相兼容；列表、向量和位域需要相同的容量；`Container` 需要相同顺序的相同字段名。[ProgressiveContainer](/eip-7495/) 变体在其共有的活动字段具有相同名称和兼容类型、且不共享其他字段名时互相兼容。[ProgressiveList](/eip-7916/) 需要兼容的元素类型。\n- **序列化：** 一个 selector 字节，后接序列化后的值。该类型始终是可变大小，且没有默认值。\n- **Merkleization：** `hash(hash_tree_root(data), selector)`，即把 selector 混入其中。这保证树形相同的两个变体仍会得到不同的根。\n- **JSON：** `{ \"selector\": \"\u003cn\u003e\", \"data\": ... }`。\n\n## 示例代码\n\n采用共识规范风格的示意 Python 代码：\n\n```python\nclass Square(ProgressiveContainer(active_fields=[1, 0, 1])):\n    side: uint16\n    color: uint8\n\nclass Circle(ProgressiveContainer(active_fields=[0, 1, 1])):\n    radius: uint16\n    color: uint8\n\nShape = CompatibleUnion({1: Square, 2: Circle})\n\ndef serialize_union(value) -\u003e bytes:\n    return value.selector.to_bytes(1, \"little\") + serialize(value.data)\n\ndef hash_tree_root_union(value) -\u003e Root:\n    return hash(hash_tree_root(value.data), value.selector.to_bytes(1, \"little\"))\n\n# `color` sits at the same gindex in both Square and Circle,\n# so one Merkle proof path works for every Shape.\n```\n\n## 需要注意\n\n- 状态为 **Draft**。它是把以太坊数据结构迁移到 SSZ 这一更大工作的一部分，与 [EIP-7495](/eip-7495/) 和 [EIP-7916](/eip-7916/)（均处于 Review）并列。\n- 用 `CompatibleUnion` 包装现有字段**不是**向后兼容的，因此规范建议在预期会有更多变体的地方尽早引入，哪怕目前只有一个选项。此后，可以增减选项而不破坏验证方。\n- 它取代了早先只在已弃用规范中使用的 SSZ 联合类型设计。\n- 参考实现：`ethereum/remerkleable`。\n"}},"requiredBy":[6404,6466]}
