RustCrypto / crypto-bigint

Cryptography-oriented big integer library with constant-time, stack-allocated (no_std-friendly) implementations of modern formulas
Apache License 2.0
167 stars 45 forks source link

Deserializing bigger Uint into smaller truncates unexpectedly. #605

Closed dvdplm closed 3 weeks ago

dvdplm commented 3 weeks ago

During work on #604 I noticed that when deserializing a serialized bigger Uint into a smaller one, the deserialization "works", but yields a truncated number. See this test for an illustration of the current behaviour.

Correct behaviour is for the deserialization to fail.

tarcieri commented 3 weeks ago

cc @daxpedda

dvdplm commented 3 weeks ago

I actually think this is an invalid issue, here's why: bincode::deserialize() does this:

bincode::DefaultOptions::new()
                .with_fixint_encoding()
                .allow_trailing_bytes()
                .deserialize::<U64>(&three_ser)

…but what we want here is for bincode to reject trailing bytes:

bincode::DefaultOptions::new()
                .with_fixint_encoding()
                .reject_trailing_bytes()
                .deserialize::<U64>(&three_ser)

It's a bit of a foot gun I guess, but definitely "user error". Not sure what we should do here though. Keep the test as a warning? Add some docs somewhere?