Skip to content

Add fallible hex decoding for Uint#247

Closed
andrewwhitehead wants to merge 1 commit intoRustCrypto:masterfrom
andrewwhitehead:feat/fallible-hex
Closed

Add fallible hex decoding for Uint#247
andrewwhitehead wants to merge 1 commit intoRustCrypto:masterfrom
andrewwhitehead:feat/fallible-hex

Conversation

@andrewwhitehead
Copy link
Contributor

@andrewwhitehead andrewwhitehead commented Jun 2, 2023

This is mostly for convenience, although it can be irritating to validate hex inputs outside of the library.

Perhaps it would be nice to support hex strings of different lengths?

Signed-off-by: Andrew Whitehead <cywolf@gmail.com>
Comment on lines +193 to +207
/// Check that the input consists of hexadecimal characters,
/// short-circuiting on invalid input.
const fn validate_hex(bytes: &[u8]) -> Option<usize> {
if bytes.len() % 2 == 1 {
return None;
}
let mut i = 0;
while i < bytes.len() {
if !matches!(bytes[i], b'0'..=b'9' | b'a' ..= b'f' | b'A'..=b'F') {
return None;
}
i += 1;
}
Some(bytes.len() / 2)
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not constant-time.

Perhaps you could change it to be? Or otherwise you need to make the methods *_vartime to honor the "constant time by default" contract of this library, but I would prefer you just make the implementation constant-time instead.

It should also return CtOption rather than Option.

Copy link
Contributor Author

@andrewwhitehead andrewwhitehead Aug 28, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't really see how this can be constant time overall, since the implementation short circuits on invalid input, and I'm not sure what the alternative would be. It would also be nice for it to handle leading zeros on hex values, as well as trimmed values (f.ex. parsing "FF" into a U256) but in that case is it meant to be constant time in the input length or in the number's representation?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the implementation short circuits on invalid input, and I'm not sure what the alternative would be.

You can defer checking an error condition until the end of the data, which is what from_be_hex does already:

https://github.com/RustCrypto/crypto-bigint/blob/2122b4a/src/uint/encoding.rs#L55-L56

This was referenced Dec 15, 2023
@tarcieri
Copy link
Member

tarcieri commented May 7, 2024

Closing as stale

@tarcieri tarcieri closed this May 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants