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.

249 lines
8.5 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  1. # Tendermint Encoding
  2. ## Amino
  3. Tendermint uses the Protobuf3 derivative [Amino](https://github.com/tendermint/go-amino) for all data structures.
  4. Think of Amino as an object-oriented Protobuf3 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 Protobuf 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. ## Public Key Cryptography
  24. Tendermint uses Amino to distinguish between different types of private keys,
  25. public keys, and signatures. Additionally, for each public key, Tendermint
  26. defines an Address function that can be used as a more compact identifier in
  27. place of the public key. Here we list the concrete types, their names,
  28. and prefix bytes for public keys and signatures, as well as the address schemes
  29. for each PubKey. Note for brevity we don't
  30. include details of the private keys beyond their type and name, as they can be
  31. derived the same way as the others using Amino.
  32. All registered objects are encoded by Amino using a 4-byte PrefixBytes that
  33. uniquely identifies the object and includes information about its underlying
  34. type. For details on how PrefixBytes are computed, see the [Amino
  35. spec](https://github.com/tendermint/go-amino#computing-the-prefix-and-disambiguation-bytes).
  36. In what follows, we provide the type names and prefix bytes directly.
  37. Notice that when encoding byte-arrays, the length of the byte-array is appended
  38. to the PrefixBytes. Thus the encoding of a byte array becomes `<PrefixBytes>
  39. <Length> <ByteArray>`. In other words, to encode any type listed below you do not need to be
  40. familiar with amino encoding.
  41. You can simply use below table and concatenate Prefix || Length (of raw bytes) || raw bytes
  42. ( while || stands for byte concatenation here).
  43. | Type | Name | Prefix | Length |
  44. | ---- | ---- | ------ | ----- |
  45. | PubKeyEd25519 | tendermint/PubKeyEd25519 | 0x1624DE62 | 0x20 |
  46. | PubKeyLedgerEd25519 | tendermint/PubKeyLedgerEd25519 | 0x5C3453B2 | 0x20 |
  47. | PubKeySecp256k1 | tendermint/PubKeySecp256k1 | 0xEB5AE982 | 0x21 |
  48. | PrivKeyEd25519 | tendermint/PrivKeyEd25519 | 0xA3288912 | 0x40 |
  49. | PrivKeySecp256k1 | tendermint/PrivKeySecp256k1 | 0xE1B0F79A | 0x20 |
  50. | PrivKeyLedgerSecp256k1 | tendermint/PrivKeyLedgerSecp256k1 | 0x10CAB393 | variable |
  51. | PrivKeyLedgerEd25519 | tendermint/PrivKeyLedgerEd25519 | 0x0CFEEF9B | variable |
  52. | SignatureEd25519 | tendermint/SignatureKeyEd25519 | 0x3DA1DB2A | 0x40 |
  53. | SignatureSecp256k1 | tendermint/SignatureKeySecp256k1 | 0x16E1FEEA | variable |
  54. ### Examples
  55. 1. For example, the 33-byte (or 0x21-byte in hex) Secp256k1 pubkey
  56. `020BD40F225A57ED383B440CF073BC5539D0341F5767D2BF2D78406D00475A2EE9`
  57. would be encoded as
  58. `EB5AE98221020BD40F225A57ED383B440CF073BC5539D0341F5767D2BF2D78406D00475A2EE9`
  59. 2. For example, the variable size Secp256k1 signature (in this particular example 70 or 0x46 bytes)
  60. `304402201CD4B8C764D2FD8AF23ECFE6666CA8A53886D47754D951295D2D311E1FEA33BF02201E0F906BB1CF2C30EAACFFB032A7129358AFF96B9F79B06ACFFB18AC90C2ADD7`
  61. would be encoded as
  62. `16E1FEEA46304402201CD4B8C764D2FD8AF23ECFE6666CA8A53886D47754D951295D2D311E1FEA33BF02201E0F906BB1CF2C30EAACFFB032A7129358AFF96B9F79B06ACFFB18AC90C2ADD7`
  63. ## Other Common Types
  64. ### BitArray
  65. The BitArray is used in block headers and some consensus messages to signal
  66. whether or not something was done by each validator. BitArray is represented
  67. with a struct containing the number of bits (`Bits`) and the bit-array itself
  68. encoded in base64 (`Elems`).
  69. ```go
  70. type BitArray struct {
  71. Bits int
  72. Elems []uint64
  73. }
  74. ```
  75. This type is easily encoded directly by Amino.
  76. Note BitArray receives a special JSON encoding in the form of `x` and `_`
  77. representing `1` and `0`. Ie. the BitArray `10110` would be JSON encoded as
  78. `"x_xx_"`
  79. ### Part
  80. Part is used to break up blocks into pieces that can be gossiped in parallel
  81. and securely verified using a Merkle tree of the parts.
  82. Part contains the index of the part in the larger set (`Index`), the actual
  83. underlying data of the part (`Bytes`), and a simple Merkle proof that the part is contained in
  84. the larger set (`Proof`).
  85. ```go
  86. type Part struct {
  87. Index int
  88. Bytes byte[]
  89. Proof byte[]
  90. }
  91. ```
  92. ### MakeParts
  93. Encode an object using Amino and slice it into parts.
  94. ```go
  95. func MakeParts(obj interface{}, partSize int) []Part
  96. ```
  97. ## Merkle Trees
  98. Simple Merkle trees are used in numerous places in Tendermint to compute a cryptographic digest of a data structure.
  99. RIPEMD160 is always used as the hashing function.
  100. ### Simple Merkle Root
  101. The function `SimpleMerkleRoot` is a simple recursive function defined as follows:
  102. ```go
  103. func SimpleMerkleRoot(hashes [][]byte) []byte{
  104. switch len(hashes) {
  105. case 0:
  106. return nil
  107. case 1:
  108. return hashes[0]
  109. default:
  110. left := SimpleMerkleRoot(hashes[:(len(hashes)+1)/2])
  111. right := SimpleMerkleRoot(hashes[(len(hashes)+1)/2:])
  112. return SimpleConcatHash(left, right)
  113. }
  114. }
  115. func SimpleConcatHash(left, right []byte) []byte{
  116. left = encodeByteSlice(left)
  117. right = encodeByteSlice(right)
  118. return RIPEMD160 (append(left, right))
  119. }
  120. ```
  121. Note that the leaves are Amino encoded as byte-arrays (ie. simple Uvarint length
  122. prefix) before being concatenated together and hashed.
  123. Note: we will abuse notion and invoke `SimpleMerkleRoot` with arguments of type `struct` or type `[]struct`.
  124. For `struct` arguments, we compute a `[][]byte` by sorting elements of the `struct` according to
  125. field name and then hashing them.
  126. For `[]struct` arguments, we compute a `[][]byte` by hashing the individual `struct` elements.
  127. ### Simple Merkle Proof
  128. Proof that a leaf is in a Merkle tree consists of a simple structure:
  129. ```
  130. type SimpleProof struct {
  131. Aunts [][]byte
  132. }
  133. ```
  134. Which is verified using the following:
  135. ```
  136. func (proof SimpleProof) Verify(index, total int, leafHash, rootHash []byte) bool {
  137. computedHash := computeHashFromAunts(index, total, leafHash, proof.Aunts)
  138. return computedHash == rootHash
  139. }
  140. func computeHashFromAunts(index, total int, leafHash []byte, innerHashes [][]byte) []byte{
  141. assert(index < total && index >= 0 && total > 0)
  142. if total == 1{
  143. assert(len(proof.Aunts) == 0)
  144. return leafHash
  145. }
  146. assert(len(innerHashes) > 0)
  147. numLeft := (total + 1) / 2
  148. if index < numLeft {
  149. leftHash := computeHashFromAunts(index, numLeft, leafHash, innerHashes[:len(innerHashes)-1])
  150. assert(leftHash != nil)
  151. return SimpleHashFromTwoHashes(leftHash, innerHashes[len(innerHashes)-1])
  152. }
  153. rightHash := computeHashFromAunts(index-numLeft, total-numLeft, leafHash, innerHashes[:len(innerHashes)-1])
  154. assert(rightHash != nil)
  155. return SimpleHashFromTwoHashes(innerHashes[len(innerHashes)-1], rightHash)
  156. }
  157. ```
  158. ## JSON
  159. ### Amino
  160. TODO: improve this
  161. Amino also supports JSON encoding - registered types are simply encoded as:
  162. ```
  163. {
  164. "type": "<DisfixBytes>",
  165. "value": <JSON>
  166. }
  167. ```
  168. For instance, an ED25519 PubKey would look like:
  169. ```
  170. {
  171. "type": "AC26791624DE60",
  172. "value": "uZ4h63OFWuQ36ZZ4Bd6NF+/w9fWUwrOncrQsackrsTk="
  173. }
  174. ```
  175. Where the `"value"` is the base64 encoding of the raw pubkey bytes, and the
  176. `"type"` is the full disfix bytes for Ed25519 pubkeys.
  177. ### Signed Messages
  178. Signed messages (eg. votes, proposals) in the consensus are encoded using Amino-JSON, rather than in the standard binary format.
  179. When signing, the elements of a message are sorted by key and the sorted message is embedded in an
  180. outer JSON that includes a `chain_id` field.
  181. We call this encoding the CanonicalSignBytes. For instance, CanonicalSignBytes for a vote would look
  182. like:
  183. ```json
  184. {"chain_id":"my-chain-id","vote":{"block_id":{"hash":DEADBEEF,"parts":{"hash":BEEFDEAD,"total":3}},"height":3,"round":2,"timestamp":1234567890, "type":2}
  185. ```
  186. Note how the fields within each level are sorted.