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.

246 lines
6.7 KiB

  1. # Tendermint Encoding (Pre-Amino)
  2. ## PubKeys and Addresses
  3. PubKeys are prefixed with a type-byte, followed by the raw bytes of the public
  4. key.
  5. Two keys are supported with the following type bytes:
  6. ```
  7. TypeByteEd25519 = 0x1
  8. TypeByteSecp256k1 = 0x2
  9. ```
  10. ```
  11. // TypeByte: 0x1
  12. type PubKeyEd25519 [32]byte
  13. func (pub PubKeyEd25519) Encode() []byte {
  14. return 0x1 | pub
  15. }
  16. func (pub PubKeyEd25519) Address() []byte {
  17. // NOTE: the length (0x0120) is also included
  18. return RIPEMD160(0x1 | 0x0120 | pub)
  19. }
  20. // TypeByte: 0x2
  21. // NOTE: OpenSSL compressed pubkey (x-cord with 0x2 or 0x3)
  22. type PubKeySecp256k1 [33]byte
  23. func (pub PubKeySecp256k1) Encode() []byte {
  24. return 0x2 | pub
  25. }
  26. func (pub PubKeySecp256k1) Address() []byte {
  27. return RIPEMD160(SHA256(pub))
  28. }
  29. ```
  30. See https://github.com/tendermint/go-crypto/blob/v0.5.0/pub_key.go for more.
  31. ## Binary Serialization (go-wire)
  32. Tendermint aims to encode data structures in a manner similar to how the corresponding Go structs
  33. are laid out in memory.
  34. Variable length items are length-prefixed.
  35. While the encoding was inspired by Go, it is easily implemented in other languages as well, given its intuitive design.
  36. XXX: This is changing to use real varints and 4-byte-prefixes.
  37. See https://github.com/tendermint/go-wire/tree/sdk2.
  38. ### Fixed Length Integers
  39. Fixed length integers are encoded in Big-Endian using the specified number of bytes.
  40. So `uint8` and `int8` use one byte, `uint16` and `int16` use two bytes,
  41. `uint32` and `int32` use 3 bytes, and `uint64` and `int64` use 4 bytes.
  42. Negative integers are encoded via twos-complement.
  43. Examples:
  44. ```go
  45. encode(uint8(6)) == [0x06]
  46. encode(uint32(6)) == [0x00, 0x00, 0x00, 0x06]
  47. encode(int8(-6)) == [0xFA]
  48. encode(int32(-6)) == [0xFF, 0xFF, 0xFF, 0xFA]
  49. ```
  50. ### Variable Length Integers
  51. Variable length integers are encoded as length-prefixed Big-Endian integers.
  52. The length-prefix consists of a single byte and corresponds to the length of the encoded integer.
  53. Negative integers are encoded by flipping the leading bit of the length-prefix to a `1`.
  54. Zero is encoded as `0x00`. It is not length-prefixed.
  55. Examples:
  56. ```go
  57. encode(uint(6)) == [0x01, 0x06]
  58. encode(uint(70000)) == [0x03, 0x01, 0x11, 0x70]
  59. encode(int(-6)) == [0xF1, 0x06]
  60. encode(int(-70000)) == [0xF3, 0x01, 0x11, 0x70]
  61. encode(int(0)) == [0x00]
  62. ```
  63. ### Strings
  64. An encoded string is length-prefixed followed by the underlying bytes of the string.
  65. The length-prefix is itself encoded as an `int`.
  66. The empty string is encoded as `0x00`. It is not length-prefixed.
  67. Examples:
  68. ```go
  69. encode("") == [0x00]
  70. encode("a") == [0x01, 0x01, 0x61]
  71. encode("hello") == [0x01, 0x05, 0x68, 0x65, 0x6C, 0x6C, 0x6F]
  72. encode("¥") == [0x01, 0x02, 0xC2, 0xA5]
  73. ```
  74. ### Arrays (fixed length)
  75. An encoded fix-lengthed array is the concatenation of the encoding of its elements.
  76. There is no length-prefix.
  77. Examples:
  78. ```go
  79. encode([4]int8{1, 2, 3, 4}) == [0x01, 0x02, 0x03, 0x04]
  80. encode([4]int16{1, 2, 3, 4}) == [0x00, 0x01, 0x00, 0x02, 0x00, 0x03, 0x00, 0x04]
  81. encode([4]int{1, 2, 3, 4}) == [0x01, 0x01, 0x01, 0x02, 0x01, 0x03, 0x01, 0x04]
  82. encode([2]string{"abc", "efg"}) == [0x01, 0x03, 0x61, 0x62, 0x63, 0x01, 0x03, 0x65, 0x66, 0x67]
  83. ```
  84. ### Slices (variable length)
  85. An encoded variable-length array is length-prefixed followed by the concatenation of the encoding of
  86. its elements.
  87. The length-prefix is itself encoded as an `int`.
  88. An empty slice is encoded as `0x00`. It is not length-prefixed.
  89. Examples:
  90. ```go
  91. encode([]int8{}) == [0x00]
  92. encode([]int8{1, 2, 3, 4}) == [0x01, 0x04, 0x01, 0x02, 0x03, 0x04]
  93. encode([]int16{1, 2, 3, 4}) == [0x01, 0x04, 0x00, 0x01, 0x00, 0x02, 0x00, 0x03, 0x00, 0x04]
  94. encode([]int{1, 2, 3, 4}) == [0x01, 0x04, 0x01, 0x01, 0x01, 0x02, 0x01, 0x03, 0x01, 0x4]
  95. encode([]string{"abc", "efg"}) == [0x01, 0x02, 0x01, 0x03, 0x61, 0x62, 0x63, 0x01, 0x03, 0x65, 0x66, 0x67]
  96. ```
  97. ### BitArray
  98. BitArray is encoded as an `int` of the number of bits, and with an array of `uint64` to encode
  99. value of each array element.
  100. ```go
  101. type BitArray struct {
  102. Bits int
  103. Elems []uint64
  104. }
  105. ```
  106. ### Time
  107. Time is encoded as an `int64` of the number of nanoseconds since January 1, 1970,
  108. rounded to the nearest millisecond.
  109. Times before then are invalid.
  110. Examples:
  111. ```go
  112. encode(time.Time("Jan 1 00:00:00 UTC 1970")) == [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]
  113. encode(time.Time("Jan 1 00:00:01 UTC 1970")) == [0x00, 0x00, 0x00, 0x00, 0x3B, 0x9A, 0xCA, 0x00] // 1,000,000,000 ns
  114. encode(time.Time("Mon Jan 2 15:04:05 -0700 MST 2006")) == [0x0F, 0xC4, 0xBB, 0xC1, 0x53, 0x03, 0x12, 0x00]
  115. ```
  116. ### Structs
  117. An encoded struct is the concatenation of the encoding of its elements.
  118. There is no length-prefix.
  119. Examples:
  120. ```go
  121. type MyStruct struct{
  122. A int
  123. B string
  124. C time.Time
  125. }
  126. encode(MyStruct{4, "hello", time.Time("Mon Jan 2 15:04:05 -0700 MST 2006")}) ==
  127. [0x01, 0x04, 0x01, 0x05, 0x68, 0x65, 0x6C, 0x6C, 0x6F, 0x0F, 0xC4, 0xBB, 0xC1, 0x53, 0x03, 0x12, 0x00]
  128. ```
  129. ## Merkle Trees
  130. Simple Merkle trees are used in numerous places in Tendermint to compute a cryptographic digest of a data structure.
  131. RIPEMD160 is always used as the hashing function.
  132. The function `SimpleMerkleRoot` is a simple recursive function defined as follows:
  133. ```go
  134. func SimpleMerkleRoot(hashes [][]byte) []byte{
  135. switch len(hashes) {
  136. case 0:
  137. return nil
  138. case 1:
  139. return hashes[0]
  140. default:
  141. left := SimpleMerkleRoot(hashes[:(len(hashes)+1)/2])
  142. right := SimpleMerkleRoot(hashes[(len(hashes)+1)/2:])
  143. return RIPEMD160(append(left, right))
  144. }
  145. }
  146. ```
  147. Note: we abuse notion and call `SimpleMerkleRoot` with arguments of type `struct` or type `[]struct`.
  148. For `struct` arguments, we compute a `[][]byte` by sorting elements of the `struct` according to
  149. field name and then hashing them.
  150. For `[]struct` arguments, we compute a `[][]byte` by hashing the individual `struct` elements.
  151. ## JSON (TMJSON)
  152. Signed messages (eg. votes, proposals) in the consensus are encoded in TMJSON, rather than TMBIN.
  153. TMJSON is JSON where `[]byte` are encoded as uppercase hex, rather than base64.
  154. When signing, the elements of a message are sorted by key and the sorted message is embedded in an
  155. outer JSON that includes a `chain_id` field.
  156. We call this encoding the CanonicalSignBytes. For instance, CanonicalSignBytes for a vote would look
  157. like:
  158. ```json
  159. {"chain_id":"my-chain-id","vote":{"block_id":{"hash":DEADBEEF,"parts":{"hash":BEEFDEAD,"total":3}},"height":3,"round":2,"timestamp":1234567890, "type":2}
  160. ```
  161. Note how the fields within each level are sorted.
  162. ## Other
  163. ### MakeParts
  164. Encode an object using TMBIN and slice it into parts.
  165. ```go
  166. MakeParts(object, partSize)
  167. ```
  168. ### Part
  169. ```go
  170. type Part struct {
  171. Index int
  172. Bytes byte[]
  173. Proof byte[]
  174. }
  175. ```