|
| 1 | +# RGB consensus commitments |
| 2 | + |
| 3 | +RGB commits to client-side validated data using dedicated serialization |
| 4 | +mechanism, implemented via `CommitEncode` trait. Depending on the specific data, |
| 5 | +the mechanism can be partially or completely different from strict |
| 6 | +serialization, used for data storage. For instance, all data which may be |
| 7 | +confidential must be concealed, such that parties having no access to the |
| 8 | +original non-confidential values are still able to generate the same |
| 9 | +deterministic commitment value and verify single-use seals. |
| 10 | + |
| 11 | +Any final consensus commitment is a SHA256 tagged hash. The tagging is performed |
| 12 | +according to BIP-340, where a commitment-specific fixed ASCII string value is |
| 13 | +first hashed with a single SHA256 hash, and the resulting 32 bytes are fed into |
| 14 | +a new SHA256 hasher twice before any actual data. |
| 15 | + |
| 16 | + |
| 17 | +## Generating commitment id |
| 18 | + |
| 19 | +The commitment mechanism uses traits from [`commit_verify`] module in |
| 20 | +`rgb-consensus`, specifically its `id.rs`and `merkle.rs` submodules. |
| 21 | + |
| 22 | +### `CommitEncode` trait |
| 23 | + |
| 24 | +It is the main trait which must be implemented for each type requiring a |
| 25 | +dedicated commitment id. |
| 26 | + |
| 27 | +The trait requires to define: |
| 28 | +- `CommitmentId` specifies a commitment id type, i.e. a type wrapping 32-byte |
| 29 | + tagged SHA256 hash, implementing `CommitmentId` trait (see details below). |
| 30 | + For instance `Operation` defines `OpId` as its commitment type. |
| 31 | +- `commit_encode` specifies an encoding for the bytestream that will be |
| 32 | + the input of the tagged hasher. Typical strategies are: |
| 33 | + * strict: the data is strict-serialized |
| 34 | + * conceal: the data is concealed and then strict-serialized |
| 35 | + * merkle: the data is organized in a merkle tree structure to obtain the merkle root |
| 36 | + |
| 37 | +NB: It should never be necessary to call methods of `CommitEncode` trait directly, |
| 38 | +since `CommitId` trait automatically extends it with user-facing methods. |
| 39 | + |
| 40 | +### `CommitmentId` trait |
| 41 | + |
| 42 | +Each consensus commitment must have a dedicated Rust type, which wraps over |
| 43 | +inner `Bytes32` - a 32-byte resulting tagged hash value. The type is marked as |
| 44 | +a consensus commitment by implementing `CommitmentId` trait for it, which |
| 45 | +requires to provide a tag string value for the tagged hash. |
| 46 | + |
| 47 | +The hash tags are defined using URN strings in form of |
| 48 | +`urn:<org>:<protocol>:<data>#<date>`, where `<org>` stands for the organization, |
| 49 | +`<protocol>` is the name of the protocol, `<data>` is the data type name |
| 50 | +producing the commitment, and `<date>` is a `YYYY-MM-DD` string for the latest |
| 51 | +revision of the commitment layout. |
| 52 | + |
| 53 | +Any type implementing `CommitmentId` must also implement `From<Sha256>`, which allows for |
| 54 | +automated construction of commitments from the hasher. |
| 55 | + |
| 56 | +### `CommitId` trait |
| 57 | + |
| 58 | +This trait is automatically implemented for all types that implement `CommitEncode` and |
| 59 | +it can't be implemented manually. |
| 60 | +It exposes a `CommitId::commit_id()` method to produce the final commitment (i.e. the result |
| 61 | +of the hashing procedure, wrapped in the the corresponding type implementing `CommitmentId`). |
| 62 | + |
| 63 | +The trait also provides `CommitId::commitment_layout()` method, which can be |
| 64 | +used for automatically generating the documentation on the commitment workflow. |
| 65 | + |
| 66 | +## Merklization procedure |
| 67 | + |
| 68 | +Merlization is the procedure of computing the root of a |
| 69 | +[Merkle Tree](glossary.md#merkle-tree) to be used as a commitment. |
| 70 | +It uses traits and data types from `merkle.rs` module of `commit_verify` crate and it |
| 71 | +commits to the tree parameters, such as number of elements, depth of the tree and |
| 72 | +depth of each node. |
| 73 | + |
| 74 | +The main data type, related to the merklization, is `MerkleHash`: it is a tagged |
| 75 | +hash (using `urn:ubideco:merkle:node#2024-01-31` tag) representing node at any |
| 76 | +position of the tree: leaves, branch nodes and merkle tree root. `MerkleHash` |
| 77 | +can be produced in the following ways: |
| 78 | +- as a result of merklziation procedure, when it represents Merkle tree root; |
| 79 | +- as a root of empty Merkle tree (i.e. collection having 0 elements), by calling |
| 80 | + `MerkleHash::void(0u8, 0u32)`, |
| 81 | +- as a Merkle leaf, by implementing `CommitEncode` on some type and setting |
| 82 | + commitment id to be `MerkleHash`. |
| 83 | + |
| 84 | +In all of the above cases the hash commits to the tree parameters, which makes |
| 85 | +it safe to use the same type for leaves, branches and root nodes. Specifically, |
| 86 | +it uses an intermediate structure `MerkleNode`, which is filled with information |
| 87 | +on: |
| 88 | +- type of node branching (no branches, one branch or two branches), |
| 89 | +- depth of the node, as 8-bit unsigned integer, |
| 90 | +- width of the tree at its base, as a 256-bit LE unsigned integer, |
| 91 | +- node hashes of the branches; if one or both branches are absent, they are |
| 92 | + replaced with 32 bytes of repeated 0xFF value. |
| 93 | + |
| 94 | +A collection in form of a list (Rust `Vec`) or an ordered set of unique |
| 95 | +non-repeating items (Rust `BTreeSet`), if wrapped into a confinement (i.e. has |
| 96 | +type-defined bounds on the minimum or maximum number of items) can be |
| 97 | +automatically merklized when passed as an argument to `MerkleHash::merklize()` |
| 98 | +call. The API puts the following requirements on the collection: either |
| 99 | +- maximum number of elements must be either 0xFF or 0xFFFF **and** each |
| 100 | + collection element must implement `CommitEncode` trait with target id set to |
| 101 | + `MerkleHash`, |
| 102 | +- or there is a manual implementation of `MerkleLeaves` trait. |
| 103 | + |
| 104 | +```mermaid |
| 105 | +flowchart BT |
| 106 | + subgraph Merklization |
| 107 | + direction LR |
| 108 | + subgraph MerkleNode |
| 109 | + branching |
| 110 | + depth |
| 111 | + width |
| 112 | + node1 |
| 113 | + node2 |
| 114 | + end |
| 115 | + MerkleNode -- encode to\ntagged hasher --> MerkleHash |
| 116 | + end |
| 117 | + MerkleHash ---> MerkleNode |
| 118 | + MerkleHash === Root |
| 119 | + Leaf -- commit_id ----> MerkleHash |
| 120 | +``` |
| 121 | + |
| 122 | + |
| 123 | +## Specific RGB consensus commitments |
| 124 | + |
| 125 | +Currently, RGB has three consensus commitments: schema, operation and bundle. |
| 126 | +Operation commitment for genesis has a second representation, named contract id, |
| 127 | +which uses reversed-byte encoding and a special string serialization, but is |
| 128 | +generated with the same procedure as the operation commitment. |
| 129 | + |
| 130 | +The commitment ids can be generated with either type-specific methods |
| 131 | +(`schema_id()` for schema, `bundle_id()` for state transition bundle and |
| 132 | +`id()` for any operation) or the `CommitId::commit_id()` method, which must provide |
| 133 | +the same result. |
| 134 | + |
| 135 | +Here are more details on each commitment type: |
| 136 | + |
| 137 | +| Commitment ID | Produced by | Procedure | Tag URN suffix(1) | |
| 138 | +|----------------------|--------------------------------------|------------------------------------------------------------------------------------------------|-------------------------------| |
| 139 | +| `SchemaID` | `Schema` | strict serialization | `rgb:schema#2024-02-03` | |
| 140 | +| `OpId`, `ContractId` | `Transition`, `Genesis` | nested commitments with concealing, merklization etc via intermediate `OpCommitment` structure | `rgb:operation#2024-02-03` | |
| 141 | +| `BundleId` | `TransitionBundle` | conceal and partial strict serialization | `rgb:bundle#2024-02-03` | |
| 142 | +| `SecretSeal` | `BlindSeal` | conceal and strict serialization | `seals:secret#2024-02-03` | |
| 143 | + |
| 144 | +(1): "URN suffix" is the part that follows the "urn:lnp-bp:" prefix. |
| 145 | + |
| 146 | +Additionally to these types there are three other commitment ids used internally |
| 147 | +by merklization and strict encoding procedures: |
| 148 | + |
| 149 | +| Commitment ID | Tag URN suffix | |
| 150 | +|-------------------|--------------------------------------------------| |
| 151 | +| `MerkleHash` | `urn:ubideco:merkle:node#2024-01-31` | |
| 152 | +| `StrictHash` | `urn:ubideco:strict-types:value-hash#2024-02-10` | |
| 153 | +| `mpc::Commitment` | `urn:ubideco:mpc:commitment#2024-01-31` | |
| 154 | + |
| 155 | +`StrictHash` can be produced as a result of serialization of any |
| 156 | +strict-encodable data; for instance, it is used in compactifying collections |
| 157 | +into a single hash field in the process of computing operation ids (described |
| 158 | +below). |
| 159 | + |
| 160 | +Finally, in `commit_verify::mpc`, multi-protocol commitment |
| 161 | +implementation, we have a type named `mpc::Commitment`, which is a commitment |
| 162 | +to a root of the MPC tree (i.e. the tree's root `MerkleHash` is tag-hashed once |
| 163 | +again to produce the final commitment value). |
| 164 | + |
| 165 | + |
| 166 | +### Schema ID |
| 167 | + |
| 168 | +Schema id, represented by `SchemaId` data type, is produced from `Schema` type |
| 169 | +via strict serialization of all the schema data. No conceal or merklization |
| 170 | +procedures are applied; i.e. the commitment id is the same as hashing serialized |
| 171 | +schema with the given tag. |
| 172 | + |
| 173 | +### Operation ID and Contract ID |
| 174 | + |
| 175 | +Operation id is represented by a `OpId` type and produced for `Genesis` and |
| 176 | +`Transition` types through a dedicated `OpCommitment` structure that is then |
| 177 | +strict-serialized and hashed. |
| 178 | + |
| 179 | +`OpCommitment` consists of a set of commitments to blocks of the operation data, each |
| 180 | +generated with a specific procedure. |
| 181 | + |
| 182 | +For instance, global state, inputs and assignments are merklized, such that compact |
| 183 | +proofs of inclusion can be produced and used in smart contracts. |
| 184 | +Additionally to that, assignments are concealed before the merklization, such that an |
| 185 | +entity that does not know the blinding factor can still reproduce the same operation ID. |
| 186 | +Other collections such as metadata are simply strict-serialized, producing a `StrictHash` as sub-commitment. |
| 187 | + |
| 188 | +```mermaid |
| 189 | +flowchart LR |
| 190 | + subgraph "Common data" |
| 191 | + Ffv --> OpCommitment |
| 192 | + TypeCommitment --> OpCommitment |
| 193 | + Metadata -- StrictHash --> OpCommitment |
| 194 | + Globals -- Merklize --> OpCommitment |
| 195 | + Inputs -- Merklize --> OpCommitment |
| 196 | + Assignments -- "Conceal\n + Merklize" --> OpCommitment |
| 197 | + end |
| 198 | +
|
| 199 | + subgraph "Genesis" |
| 200 | + schemaId --> BaseCommitment |
| 201 | + chainNet --> BaseCommitment |
| 202 | + end |
| 203 | +
|
| 204 | + subgraph "Transition" |
| 205 | + tcid[contractId] --> TypeCommitment |
| 206 | + transitionType --> TypeCommitment |
| 207 | + end |
| 208 | +
|
| 209 | + BaseCommitment --> TypeCommitment |
| 210 | +
|
| 211 | + OpCommitment -- hash --> OpId |
| 212 | + OpId -- "reverse bytes\n(genesis only)" --> ContractId |
| 213 | +``` |
| 214 | + |
| 215 | +Additionally to `OpId`, genesis produces `ContractId`, which is made out of the |
| 216 | +genesis `OpId` by reversing byte order and using Base58 encoding. |
| 217 | + |
| 218 | +### Bundle ID |
| 219 | + |
| 220 | +Bundle id is a unique identifier of state transition bundle, directly used in |
| 221 | +constructing multi-protocol commitment tree. Bundle id commits to the mapping between |
| 222 | +assignments spent within the bundle and the id of the operation spending them. |
| 223 | +`TransitionBundle::known_transitions` may contain a subset of the transitions in the |
| 224 | +bundle and thus it desn't contribute to the `BundleId`. |
| 225 | + |
| 226 | +The procedure is explained in detail in a [dedicated chapter](../rgb-state-and-operations/state-transitions.md#transition-bundle) |
| 227 | + |
| 228 | +```mermaid |
| 229 | +flowchart TD |
| 230 | + subgraph Discarded |
| 231 | + id((" ")) |
| 232 | + end |
| 233 | +
|
| 234 | + subgraph TransitionBundle |
| 235 | + inputMap |
| 236 | + knownTransitions |
| 237 | + end |
| 238 | +
|
| 239 | + inputMap -- encode \n hash --> BundleId |
| 240 | + knownTransitions --x Discarded |
| 241 | +``` |
0 commit comments