bincode-org / bincode

A binary encoder / decoder implementation in Rust.
MIT License
2.69k stars 272 forks source link

Does bincode guarantee a 1:1 relationship between encodings and values? #691

Closed WesleyAC closed 7 months ago

WesleyAC commented 9 months ago

I require a encoding system that guarantees a 1:1 relationship between encodings and values.

That is to say, given a particular value, only one sequence of bytes will deserialize to that value, all other sequences either represent some other value or are invalid and will throw an error.

From looking at the bincode format, it seems like if it's correctly implemented, and if there aren't any custom decoders in play that violate this assumption, it should fit this, but I'm not 100% sure (for instance, it could not unreasonably canonicalize floating-point NaNs when decoding, which would violate this assumption — I assume it doesn't, but I don't know).

Is this a guarantee that bincode intends to provide? If so, could that be documented?

VictorKoenders commented 9 months ago

Bincode tries to achieve this but like you mentioned cases like NaN floating point deserialization could violate this assumption. Specifically we use a lot of from_le_bytes and from_be_bytes calls which could result in situations where the encoded value is different than the in-memory value.

That being said, I think in reality all values from the standard library will be a 1:1 mapping.

The bigger thing where this guarantee could break is when 3rd parties implement bincode for their types. At this point they could implement it any way they want, even using a randomizer to populate the data.

WesleyAC commented 9 months ago

Hmm, it seems like this isn't guaranteed for built in types, given that HashMap is serialized in a arbitrary order :/

I think for people who want this property Borsh or BCS would be a better choice.

See #230 for prior discussion.

stale[bot] commented 7 months ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.