Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Less errors in RandomBits methods #534

Open
fjarri opened this issue Dec 29, 2023 · 7 comments
Open

Less errors in RandomBits methods #534

fjarri opened this issue Dec 29, 2023 · 7 comments

Comments

@fjarri
Copy link
Contributor

fjarri commented Dec 29, 2023

A possible change of contract of RandomBits that can eliminate some error/panic cases: in the methods that take bits_precision, treat it as at_least_bits_precision in BoxedUint. That is if RandomBits::random_bits_with_precision() is called with bits_precision == 128 for U256, it will return U256 instead of panicking. Or is that too dynamic?

@tarcieri
Copy link
Member

Is there a use case where this would actually make sense? Otherwise it seems like an error condition to me

@fjarri
Copy link
Contributor Author

fjarri commented Dec 29, 2023

In any use case where the user generalizes on T: Integer? BoxedUint methods are already automatically expanding precision to the nearest limb size multiple, this seems to be the same principle.

@tarcieri
Copy link
Member

But in particular I'm asking about miscomputing the size and causing the error case. Why is it being miscomputed? Why is it being passed explicitly instead of inferred?

@fjarri
Copy link
Contributor Author

fjarri commented Dec 29, 2023

Because RandomBits has to work for both Uint and BoxedUint. bits_precision is needed in the latter case, but for Uint it duplicates the information that's already contained in the type.

@tarcieri
Copy link
Member

But more specifically, what is an example of a case where random_bits_with_precision is being preferred over random_bits, where the caller wants to pass an explicit precision but for some reason can't compute it correctly for the size of the underlying type?

@fjarri
Copy link
Contributor Author

fjarri commented Dec 29, 2023

I admit I don't have a specific example in mind, I see it as more of a quality of life improvement — the general principle being, when it's possible, instead of an error do something that doesn't lead to incorrect behavior.

@tarcieri
Copy link
Member

Shouldn't the answer for those cases be "use random_bits instead of random_bits_with_precision"?

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

No branches or pull requests

2 participants