-
Notifications
You must be signed in to change notification settings - Fork 211
support custom random data source via mp_rand_source #236
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
Conversation
minad
commented
May 7, 2019
- deprecate MP_PRNG_ENABLE_LTM_RNG
- custom mp_rand_source is used always if set, which should be more aligned with user expectations
- use custom source in tune.c
- don't call random number generator once per digit, which is slow
To clarify the commit message a bit more - the reasons for this PR:
|
I really like this PR! TBH I'd prefer to keep the API signature of the "custom random source" closer to |
Btw. the reason why the "custom random source" was disabled by default is to prevent the user from shooting himself in the foot ... |
I just pushed again.
Hmm, not really an argument isn't it? The user has to manually explicitly select a custom random source. I think the lib could also provide multiple sources like:
and those can be passed to mp_rand_source. The default is the safe one and this is important. |
nope. now even less where it's not just a global variable assignment but a function call. I like it.
I'd leave this work to the user if he really wants it. |
You mean returning the size instead of an error value? I think it is more consistent returning the error but ymmv. The callback should go away. |
I meant both, but we should most likely update |
a985e1a
to
cd3e470
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for cleaning the random bigint generation up ( bn_mp_rand.c)!
How do you make the thumbs-up…uh, here it is 👍
Oh, and the RNG in tune.c
is supposed to wander into the testbed in demo/
once and if you reached an agreement about the testing of private functions. Yes, that was a not-so-hidden hint and it wasn't about the code-trekking;-)
Now, that the compiler complained, I remember: The failure of the test for BTW: I think all of the tests should print their input at failure, especially if it is non-repeatable random input. If feasible, of course, no giant numbers. |
@czurnieden I fixed the issues with the casts. But what is the reason for the other test failures? I don't think the reason is that the random int is zero. Maybe I made a mistake in the random number generator? |
921a588
to
ec9c834
Compare
@czurnieden Could you check please? I get errors in mp_is_square and mp_montgomery_reduce. I basically took your s_random implementation from tune and replaced the inefficient version in mp_rand. It seems correct but now we get those failures. |
With low_mp ( 8-bit, 16-bit )? If it is MP_8BIT is most probably a zero but that is easily checked. MP_16BIT on the other side is a bit too large for that. (you have put |
Yes, with MP_8BIT and is_square I get a zero as the input. Propose to reinstall the little loop that guarantees the first digit to be different from zero. The way you do it can also cause leading zeros, propose a |
Done. Problem persists.
Moving this in the beginning fixes the issue with montgomery. Is it a guaranteed invariant that unused allocated digits must be zero? |
This is a bad idea. I don't want random numbers to have some properties like being nonzero at some bits always. The is_square and montgomery tests should be fixed instead. |
143802e
to
20c4ed7
Compare
I understand that but changing it would change the API quite, well, not drastically but not in a small amount, there is a huge difference between "can return zero" or "cannot return zero". It might brake a lot of user programs. What's with LTC? I don't use it, so I cannot tell quickly but it might rely on "first digit non-zero" somewhere, please check.
There is nothing to fix, they were based on the current API, not your new one. Yeah, nitpicking, I know ;-) |
The documentation says The current API is horribly broken. If programs or tests relying on the broken behavior break it is good. I don't know why I should assume that a random function has any kind of bias? @czurnieden I am thinking a bit more about it. And it seems that the current functions does lshd after initializing the first digit to ensure that the generated number always has a nonzero highest digit. Such that the random number has the number of requested digits. Is that right? But to be honest, I am out of my comfort zone here. Someone who knows more should tell. @sjaeckel |
@czurnieden @sjaeckel It seems that mp_rand was not meant for cryptographic in the beginning since it relied on rand(). That biased behaviour is a remnant from back then. @sjaeckel made the function safer in #103. The question is what we want here. |
If LTC has no problems with it: change the API. Well, it's not even the API as you said, the non-zero first digit behaviour is not documented. And if it is not documented, we can change it without much ado. But may I humbly suppose that we warn the users before we do it? And if LTC relies on it we should give them a chance to get prepared. Oh, nearly forgotten: |
we cannot set it together with -Wsystem-headers since the system headers are usually not c89 but c99
* deprecate MP_PRNG_ENABLE_LTM_RNG * custom mp_rand_source is used always if set, which should be more aligned with user expectations * use custom source in tune.c * don't call random number generator once per digit, which is slow
@czurnieden Ok I restored the old behavior for now and opened #237. This PR can then be merged without further changes and the behavior of mp_rand can be discussed separately. |
@sjaeckel Tests are green now. If you agree with the API, it is ready to merge. |
Guaranteeing the MSD to be non-zero removes the need for the final
No, that doesn't apply to the original LTM, I have implemented it that way in my own (unpublished) version, sorry. I still don't understand the problem here. You want a specified number of limbs filled with random bits and if the MSD is zero you don't get that specified number of limbs and, in the case of the generation of primes for cryptographic purposes, you won't get the needed number of bits as often as possible. The cryptographic problem shows up only in the case of a single limb. That would be non-zero also. But if you want a single limb that might be zero, too, you can and should use It would a bit too brutal, I think, to return Oh, I saw @sjaeckel was faster than me. |
I know. But I kept it intentionally for the case that we remove the while loop. |
@czurnieden My problem is mostly regarding to testing. I am not using tommath for anything cryptographical and I am not using tomcrypt. For testing the current behavior is definitely bad since we omit many test cases. I think we should offer another function mp_rand_interval which can be used in tests. This function should generate numbers within a given interval including small numbers. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The public API didn't change and the implementation looks much cleaner now and is supposedly a lot faster, so for sure this will be a yes :)
@sjaeckel Addressed your comments. Let's see if the tests come out green. |