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.

140 lines
6.8 KiB

  1. # RFC 007 : Deterministic Proto Byte Serialization
  2. ## Changelog
  3. - 09-Dec-2021: Initial draft (@williambanfield).
  4. ## Abstract
  5. This document discusses the issue of stable byte-representation of serialized messages
  6. within Tendermint and describes a few possible routes that could be taken to address it.
  7. ## Background
  8. We use the byte representations of wire-format proto messages to produce
  9. and verify hashes of data within the Tendermint codebase as well as for
  10. producing and verifying cryptographic signatures over these signed bytes.
  11. The protocol buffer [encoding spec][proto-spec-encoding] does not guarantee that the byte representation
  12. of a protocol buffer message will be the same between two calls to an encoder.
  13. While there is a mode to force the encoder to produce the same byte representation
  14. of messages within a single binary, these guarantees are not good enough for our
  15. use case in Tendermint. We require multiple different versions of a binary running
  16. Tendermint to be able to inter-operate. Additionally, we require that multiple different
  17. systems written in _different languages_ be able to participate in different aspects
  18. of the protocols of Tendermint and be able to verify the integrity of the messages
  19. they each produce.
  20. While this has not yet created a problem that we know of in a running network, we should
  21. make sure to provide stronger guarantees around the serialized representation of the messages
  22. used within the Tendermint consensus algorithm to prevent any issue from occurring.
  23. ## Discussion
  24. Proto has the following points of variability that can produce non-deterministic byte representation:
  25. 1. Encoding order of fields within a message.
  26. Proto allows fields to be encoded in any order and even be repeated.
  27. 2. Encoding order of elements of a repeated field.
  28. `repeated` fields in a proto message can be serialized in any order.
  29. 3. Presence or absence of default values.
  30. Types in proto have defined default values similar to Go's zero values.
  31. Writing or omitting a default value are both legal ways of encoding a wire message.
  32. 4. Serialization of 'unknown' fields.
  33. Unknown fields can be present when a message is created by a binary with a newer
  34. version of the proto that contains fields that the deserializer in a different
  35. binary does not yet know about. Deserializers in binaries that do not know about the field
  36. will maintain the bytes of the unknown field but not place them into the deserialized structure.
  37. We have a few options to consider when producing this stable representation.
  38. ### Options for deterministic byte representation
  39. #### Use only compliant serializers and constrain field usage
  40. According to [Cosmos-SDK ADR-27][cosmos-sdk-adr-27], when message types obey a simple
  41. set of rules, gogoproto produces a consistent byte representation of serialized messages.
  42. This seems promising, although more research is needed to guarantee gogoproto always
  43. produces a consistent set of bytes on serialized messages. This would solve the problem
  44. within Tendermint as written in Go, but would require ensuring that there are similar
  45. serializers written in other languages that produce the same output as gogoproto.
  46. #### Reorder serialized bytes to ensure determinism.
  47. The serialized form of a proto message can be transformed into a canonical representation
  48. by applying simple rules to the serialized bytes. Re-ordering the serialized bytes
  49. would allow Tendermint to produce a canonical byte representation without having to
  50. simultaneously maintain a custom proto marshaller.
  51. This could be implemented as a function in many languages that performed the following
  52. producing bytes to sign or hashing:
  53. 1. Does not add any of the data from unknown fields into the type to hash.
  54. Tendermint should not run into a case where it needs to verify the integrity of
  55. data with unknown fields for the following reasons:
  56. The purpose of checking hash equality within Tendermint is to ensure that
  57. its local copy of data matches the data that the network agreed on. There should
  58. therefore not be a case where a process is checking hash equality using data that it did not expect
  59. to receive. What the data represent may be opaque to the process, such as when checking the
  60. transactions in a block, _but the process will still have expected to receive this data_,
  61. despite not understanding what their internal structure is. It's not clear what it would
  62. mean to verify that a block contains data that a process does not know about.
  63. The same reasoning applies for signature verification within Tendermint. Processes
  64. verify that a digital signature signed over a set of bytes by locally reconstructing the
  65. data structure that the digital signature signed using the process's local data.
  66. 2. Reordered all message fields to be in tag-sorted order.
  67. Tag-sorting top-level fields will place all fields of the same tag in a adjacent
  68. to eachother within the serialized representation.
  69. 3. Reordered the contents of all `repeated` fields to be in lexicographically sorted order.
  70. `repeated` fields will appear in a message as having the same tag but will contain different
  71. contents. Therefore, lexicographical sorting will produce a stable ordering of
  72. fields with the same tag.
  73. 4. Deleted all default values from the byte representation.
  74. Encoders can include default values or omit them. Most encoders appear to omit them
  75. but we may wish to delete them just to be safe.
  76. 5. Recursively performed these operations on any length-delimited subfields.
  77. Length delimited fields may contain messages, strings, or just bytes. However,
  78. it's not possible to know what data is being represented by such a field.
  79. A 'string' may happen to have the same structure as an embedded message and we cannot
  80. disambiguate. For this reason, we must apply these same rules to all subfields that
  81. may contain messages. Because we cannot know if we have totally mangled the interior 'string'
  82. or not, this data should never be deserialized or used for anything beyond hashing.
  83. A **prototype** implementation by @creachadair of this can be found in [the wirepb repo][wire-pb].
  84. This could be implemented in multiple languages more simply than ensuring that there are
  85. canonical proto serializers that match in each language.
  86. ### Future work
  87. We should add clear documentation to the Tendermint codebase every time we
  88. compare hashes of proto messages or use proto serialized bytes to produces a
  89. digital signatures that we have been careful to ensure that the hashes are performed
  90. properly.
  91. ### References
  92. [proto-spec-encoding]: https://developers.google.com/protocol-buffers/docs/encoding
  93. [spec-issue]: https://github.com/tendermint/tendermint/issues/5005
  94. [cosmos-sdk-adr-27]: https://github.com/cosmos/cosmos-sdk/blob/master/docs/architecture/adr-027-deterministic-protobuf-serialization.md
  95. [cer-proto-3]: https://github.com/regen-network/canonical-proto3
  96. [wire-pb]: https://github.com/creachadair/wirepb