Skip to content

Conversation

@andrewwhitehead
Copy link
Contributor

This adds fallible parsing for binary, decimal, and hex integers (and any base from 2 to 36).

The format of these numbers generally matches the from_str_radix methods of the standard library. One exception is that underscores may used as spacers, as in +1_000_000 for example. This is consistent with the num_bigint crate.

The DecodeError enum is promoted from the uint.boxed module and reused.

@tarcieri tarcieri mentioned this pull request Jun 17, 2024
@ndavd
Copy link

ndavd commented Jun 17, 2024

Is this already fully working or still a WIP?

@andrewwhitehead
Copy link
Contributor Author

andrewwhitehead commented Jun 17, 2024

It's fully working. I have encoding done too (to_string_radix_vartime), but I didn't want to submit too many changes at once.

@ndavd
Copy link

ndavd commented Jun 17, 2024

That will be a must, was about to start working on a solution for it locally but it's great to know this is being added 🙏🏻

@ndavd
Copy link

ndavd commented Jun 17, 2024

When do you estimate both from and to methods will be available?

@tarcieri
Copy link
Member

@ndavd I have a lot on my plate right now and higher priority issues to work on, even in this crate. It will also be going into a prerelease, and we likely won't have a final release out for several months.

Comment on lines +541 to +574
/// Possible errors in variable-time integer decoding methods.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum DecodeError {
/// The input value was empty.
Empty,

/// The input was not consistent with the format restrictions.
InvalidDigit,

/// Input size is too small to fit in the given precision.
InputSize,

/// The deserialized number is larger than the given precision.
Precision,
}

impl fmt::Display for DecodeError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Empty => write!(f, "empty value provided"),
Self::InvalidDigit => {
write!(f, "invalid digit character")
}
Self::InputSize => write!(f, "input size is too small to fit in the given precision"),
Self::Precision => write!(
f,
"the deserialized number is larger than the given precision"
),
}
}
}

#[cfg(feature = "std")]
impl std::error::Error for DecodeError {}
Copy link
Member

@tarcieri tarcieri Jul 26, 2024

Choose a reason for hiding this comment

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

Seems like an odd place for this. Perhaps we should extract an errors.rs. But I'll save that for another PR.

@tarcieri tarcieri changed the title Implement from_str_radix_vartime for Uint, BoxedUint Implement from_str_radix_vartime for Uint, BoxedUint Jul 26, 2024
@tarcieri tarcieri merged commit 70d04b5 into RustCrypto:master Jul 26, 2024
@tarcieri tarcieri mentioned this pull request Jan 22, 2025
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.

3 participants