You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

350 lines
12 KiB

  1. # Encoding
  2. ## Amino
  3. Tendermint uses the proto3 derivative [Amino](https://github.com/tendermint/go-amino) for all data structures.
  4. Think of Amino as an object-oriented proto3 with native JSON support.
  5. The goal of the Amino encoding protocol is to bring parity between application
  6. logic objects and persistence objects.
  7. Please see the [Amino
  8. specification](https://github.com/tendermint/go-amino#amino-encoding-for-go) for
  9. more details.
  10. Notably, every object that satisfies an interface (eg. a particular kind of p2p message,
  11. or a particular kind of pubkey) is registered with a global name, the hash of
  12. which is included in the object's encoding as the so-called "prefix bytes".
  13. We define the `func AminoEncode(obj interface{}) []byte` function to take an
  14. arbitrary object and return the Amino encoded bytes.
  15. ## Byte Arrays
  16. The encoding of a byte array is simply the raw-bytes prefixed with the length of
  17. the array as a `UVarint` (what proto calls a `Varint`).
  18. For details on varints, see the [protobuf
  19. spec](https://developers.google.com/protocol-buffers/docs/encoding#varints).
  20. For example, the byte-array `[0xA, 0xB]` would be encoded as `0x020A0B`,
  21. while a byte-array containing 300 entires beginning with `[0xA, 0xB, ...]` would
  22. be encoded as `0xAC020A0B...` where `0xAC02` is the UVarint encoding of 300.
  23. ## Hashing
  24. Tendermint uses `SHA256` as its hash function.
  25. Objects are always Amino encoded before being hashed.
  26. So `SHA256(obj)` is short for `SHA256(AminoEncode(obj))`.
  27. ## Public Key Cryptography
  28. Tendermint uses Amino to distinguish between different types of private keys,
  29. public keys, and signatures. Additionally, for each public key, Tendermint
  30. defines an Address function that can be used as a more compact identifier in
  31. place of the public key. Here we list the concrete types, their names,
  32. and prefix bytes for public keys and signatures, as well as the address schemes
  33. for each PubKey. Note for brevity we don't
  34. include details of the private keys beyond their type and name, as they can be
  35. derived the same way as the others using Amino.
  36. All registered objects are encoded by Amino using a 4-byte PrefixBytes that
  37. uniquely identifies the object and includes information about its underlying
  38. type. For details on how PrefixBytes are computed, see the [Amino
  39. spec](https://github.com/tendermint/go-amino#computing-the-prefix-and-disambiguation-bytes).
  40. In what follows, we provide the type names and prefix bytes directly.
  41. Notice that when encoding byte-arrays, the length of the byte-array is appended
  42. to the PrefixBytes. Thus the encoding of a byte array becomes `<PrefixBytes> <Length> <ByteArray>`. In other words, to encode any type listed below you do not need to be
  43. familiar with amino encoding.
  44. You can simply use below table and concatenate Prefix || Length (of raw bytes) || raw bytes
  45. ( while || stands for byte concatenation here).
  46. | Type | Name | Prefix | Length | Notes |
  47. | ----------------------- | ---------------------------------- | ---------- | -------- | ----- |
  48. | PubKeyEd25519 | tendermint/PubKeyEd25519 | 0x1624DE64 | 0x20 | |
  49. | PubKeySecp256k1 | tendermint/PubKeySecp256k1 | 0xEB5AE987 | 0x21 | |
  50. | PrivKeyEd25519 | tendermint/PrivKeyEd25519 | 0xA3288910 | 0x40 | |
  51. | PrivKeySecp256k1 | tendermint/PrivKeySecp256k1 | 0xE1B0F79B | 0x20 | |
  52. | PubKeyMultisigThreshold | tendermint/PubKeyMultisigThreshold | 0x22C1F7E2 | variable | |
  53. ### Example
  54. For example, the 33-byte (or 0x21-byte in hex) Secp256k1 pubkey
  55. `020BD40F225A57ED383B440CF073BC5539D0341F5767D2BF2D78406D00475A2EE9`
  56. would be encoded as
  57. `EB5AE98721020BD40F225A57ED383B440CF073BC5539D0341F5767D2BF2D78406D00475A2EE9`
  58. ### Key Types
  59. Each type specifies it's own pubkey, address, and signature format.
  60. #### Ed25519
  61. TODO: pubkey
  62. The address is the first 20-bytes of the SHA256 hash of the raw 32-byte public key:
  63. ```
  64. address = SHA256(pubkey)[:20]
  65. ```
  66. The signature is the raw 64-byte ED25519 signature.
  67. #### Secp256k1
  68. TODO: pubkey
  69. The address is the RIPEMD160 hash of the SHA256 hash of the OpenSSL compressed public key:
  70. ```
  71. address = RIPEMD160(SHA256(pubkey))
  72. ```
  73. This is the same as Bitcoin.
  74. The signature is the 64-byte concatenation of ECDSA `r` and `s` (ie. `r || s`),
  75. where `s` is lexicographically less than its inverse, to prevent malleability.
  76. This is like Ethereum, but without the extra byte for pubkey recovery, since
  77. Tendermint assumes the pubkey is always provided anyway.
  78. #### Multisig
  79. TODO
  80. ## Other Common Types
  81. ### BitArray
  82. The BitArray is used in some consensus messages to represent votes received from
  83. validators, or parts received in a block. It is represented
  84. with a struct containing the number of bits (`Bits`) and the bit-array itself
  85. encoded in base64 (`Elems`).
  86. ```go
  87. type BitArray struct {
  88. Bits int
  89. Elems []uint64
  90. }
  91. ```
  92. This type is easily encoded directly by Amino.
  93. Note BitArray receives a special JSON encoding in the form of `x` and `_`
  94. representing `1` and `0`. Ie. the BitArray `10110` would be JSON encoded as
  95. `"x_xx_"`
  96. ### Part
  97. Part is used to break up blocks into pieces that can be gossiped in parallel
  98. and securely verified using a Merkle tree of the parts.
  99. Part contains the index of the part (`Index`), the actual
  100. underlying data of the part (`Bytes`), and a Merkle proof that the part is contained in
  101. the set (`Proof`).
  102. ```go
  103. type Part struct {
  104. Index int
  105. Bytes []byte
  106. Proof SimpleProof
  107. }
  108. ```
  109. See details of SimpleProof, below.
  110. ### MakeParts
  111. Encode an object using Amino and slice it into parts.
  112. Tendermint uses a part size of 65536 bytes, and allows a maximum of 1601 parts
  113. (see `types.MaxBlockPartsCount`). This corresponds to the hard-coded block size
  114. limit of 100MB.
  115. ```go
  116. func MakeParts(block Block) []Part
  117. ```
  118. ## Merkle Trees
  119. For an overview of Merkle trees, see
  120. [wikipedia](https://en.wikipedia.org/wiki/Merkle_tree)
  121. We use the RFC 6962 specification of a merkle tree, with sha256 as the hash function.
  122. Merkle trees are used throughout Tendermint to compute a cryptographic digest of a data structure.
  123. The differences between RFC 6962 and the simplest form a merkle tree are that:
  124. 1. leaf nodes and inner nodes have different hashes.
  125. This is for "second pre-image resistance", to prevent the proof to an inner node being valid as the proof of a leaf.
  126. The leaf nodes are `SHA256(0x00 || leaf_data)`, and inner nodes are `SHA256(0x01 || left_hash || right_hash)`.
  127. 2. When the number of items isn't a power of two, the left half of the tree is as big as it could be.
  128. (The largest power of two less than the number of items) This allows new leaves to be added with less
  129. recomputation. For example:
  130. ```
  131. Simple Tree with 6 items Simple Tree with 7 items
  132. * *
  133. / \ / \
  134. / \ / \
  135. / \ / \
  136. / \ / \
  137. * * * *
  138. / \ / \ / \ / \
  139. / \ / \ / \ / \
  140. / \ / \ / \ / \
  141. * * h4 h5 * * * h6
  142. / \ / \ / \ / \ / \
  143. h0 h1 h2 h3 h0 h1 h2 h3 h4 h5
  144. ```
  145. ### MerkleRoot
  146. The function `MerkleRoot` is a simple recursive function defined as follows:
  147. ```go
  148. // SHA256(0x00 || leaf)
  149. func leafHash(leaf []byte) []byte {
  150. return tmhash.Sum(append(0x00, leaf...))
  151. }
  152. // SHA256(0x01 || left || right)
  153. func innerHash(left []byte, right []byte) []byte {
  154. return tmhash.Sum(append(0x01, append(left, right...)...))
  155. }
  156. // largest power of 2 less than k
  157. func getSplitPoint(k int) { ... }
  158. func MerkleRoot(items [][]byte) []byte{
  159. switch len(items) {
  160. case 0:
  161. return nil
  162. case 1:
  163. return leafHash(items[0])
  164. default:
  165. k := getSplitPoint(len(items))
  166. left := MerkleRoot(items[:k])
  167. right := MerkleRoot(items[k:])
  168. return innerHash(left, right)
  169. }
  170. }
  171. ```
  172. Note: `MerkleRoot` operates on items which are arbitrary byte arrays, not
  173. necessarily hashes. For items which need to be hashed first, we introduce the
  174. `Hashes` function:
  175. ```
  176. func Hashes(items [][]byte) [][]byte {
  177. return SHA256 of each item
  178. }
  179. ```
  180. Note: we will abuse notion and invoke `MerkleRoot` with arguments of type `struct` or type `[]struct`.
  181. For `struct` arguments, we compute a `[][]byte` containing the amino encoding of each
  182. field in the struct, in the same order the fields appear in the struct.
  183. For `[]struct` arguments, we compute a `[][]byte` by amino encoding the individual `struct` elements.
  184. ### Simple Merkle Proof
  185. Proof that a leaf is in a Merkle tree is composed as follows:
  186. ```golang
  187. type SimpleProof struct {
  188. Total int
  189. Index int
  190. LeafHash []byte
  191. Aunts [][]byte
  192. }
  193. ```
  194. Which is verified as follows:
  195. ```golang
  196. func (proof SimpleProof) Verify(rootHash []byte, leaf []byte) bool {
  197. assert(proof.LeafHash, leafHash(leaf)
  198. computedHash := computeHashFromAunts(proof.Index, proof.Total, proof.LeafHash, proof.Aunts)
  199. return computedHash == rootHash
  200. }
  201. func computeHashFromAunts(index, total int, leafHash []byte, innerHashes [][]byte) []byte{
  202. assert(index < total && index >= 0 && total > 0)
  203. if total == 1{
  204. assert(len(proof.Aunts) == 0)
  205. return leafHash
  206. }
  207. assert(len(innerHashes) > 0)
  208. numLeft := getSplitPoint(total) // largest power of 2 less than total
  209. if index < numLeft {
  210. leftHash := computeHashFromAunts(index, numLeft, leafHash, innerHashes[:len(innerHashes)-1])
  211. assert(leftHash != nil)
  212. return innerHash(leftHash, innerHashes[len(innerHashes)-1])
  213. }
  214. rightHash := computeHashFromAunts(index-numLeft, total-numLeft, leafHash, innerHashes[:len(innerHashes)-1])
  215. assert(rightHash != nil)
  216. return innerHash(innerHashes[len(innerHashes)-1], rightHash)
  217. }
  218. ```
  219. The number of aunts is limited to 100 (`MaxAunts`) to protect the node against DOS attacks.
  220. This limits the tree size to 2^100 leaves, which should be sufficient for any
  221. conceivable purpose.
  222. ### IAVL+ Tree
  223. Because Tendermint only uses a Simple Merkle Tree, application developers are expect to use their own Merkle tree in their applications. For example, the IAVL+ Tree - an immutable self-balancing binary tree for persisting application state is used by the [Cosmos SDK](https://github.com/cosmos/cosmos-sdk/blob/ae77f0080a724b159233bd9b289b2e91c0de21b5/docs/interfaces/lite/specification.md)
  224. ## JSON
  225. ### Amino
  226. Amino also supports JSON encoding - registered types are simply encoded as:
  227. ```
  228. {
  229. "type": "<amino type name>",
  230. "value": <JSON>
  231. }
  232. ```
  233. For instance, an ED25519 PubKey would look like:
  234. ```
  235. {
  236. "type": "tendermint/PubKeyEd25519",
  237. "value": "uZ4h63OFWuQ36ZZ4Bd6NF+/w9fWUwrOncrQsackrsTk="
  238. }
  239. ```
  240. Where the `"value"` is the base64 encoding of the raw pubkey bytes, and the
  241. `"type"` is the amino name for Ed25519 pubkeys.
  242. ### Signed Messages
  243. Signed messages (eg. votes, proposals) in the consensus are encoded using Amino.
  244. When signing, the elements of a message are re-ordered so the fixed-length fields
  245. are first, making it easy to quickly check the type, height, and round.
  246. The `ChainID` is also appended to the end.
  247. We call this encoding the SignBytes. For instance, SignBytes for a vote is the Amino encoding of the following struct:
  248. ```go
  249. type CanonicalVote struct {
  250. Type byte
  251. Height int64 `binary:"fixed64"`
  252. Round int64 `binary:"fixed64"`
  253. BlockID CanonicalBlockID
  254. Timestamp time.Time
  255. ChainID string
  256. }
  257. ```
  258. The field ordering and the fixed sized encoding for the first three fields is optimized to ease parsing of SignBytes
  259. in HSMs. It creates fixed offsets for relevant fields that need to be read in this context.
  260. For more details, see the [signing spec](../consensus/signing.md).
  261. Also, see the motivating discussion in
  262. [#1622](https://github.com/tendermint/tendermint/issues/1622).