Skip to content

added integer log with positive integer base #174

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

Closed
wants to merge 19 commits into from

Conversation

czurnieden
Copy link
Contributor

Example for a simple design to add a group of opt-in (independent) functions.

@minad
Copy link
Member

minad commented Mar 16, 2019

What is the reasoning for the extra function macro?

@czurnieden
Copy link
Contributor Author

What is the reasoning for the extra function macro?

Leftovers. Residual echoes.
Because it was all Macro in the beginning.
No, really, I have made the whole thing with preprocessor directives a couple of years ago! ;-)
(Not in the segmented version, a "standard" sieve only)

And I found another one: #define LTM_SIEVE_SIZE(bst) ((bst)->size) is useless, the struct entry is already named "size", no need for a "better name" macro.

Will tackle it after lunch.

@czurnieden
Copy link
Contributor Author

Oh, I broke the couch? I know I need to loose some weight, Travis, no need to put a finger on it!

Copy link
Member

@sjaeckel sjaeckel left a comment

Choose a reason for hiding this comment

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

first part of the review, more to come later or so


if (f->factors != NULL) {
for (i = 0; i < f->length; i++) {
mp_zero(&(f->factors[i]));
Copy link
Member

Choose a reason for hiding this comment

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

that's not required, mp_clear() also zeroes the mp before clearing

Copy link
Contributor Author

Choose a reason for hiding this comment

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

first part of the review, more to come later or so

Uh oh! ;-)

that's not required, mp_clear() also zeroes the mp before clearing

Yepp, residue from trying out if mp_zero would suffice here (playing with the heap is always costly) but that results in quite a mess which I found out quickly.

}

if (all == MP_NO) {
#if 0
Copy link
Member

Choose a reason for hiding this comment

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

can this code go or is there a reason this should stay?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The first one is useful for some numbers but will hold up everything for others and I'm benchmarking the other one if it is useful at that place. I'm thinking about adding a flag for the first one to let the caller decide. The latter is needed for the quadratic sieve, but that will come later, much later.

So: should it stay or should it go? Good question. (Why am I hearing The Clash in my head now?)

Oh, nearly forgot: both, mp_isperfpower and mp_ispower need a mp_n_root_ex with a normal runtime to begin with. It does not need the optimized version I have in a different branch (nthroot I think?) but at least the one I put into this PR. It implements just the standard seed (a bit above the actual root) and a slightly more paranoid check at the end. The usability of the original version is so small, I would consider the one in this PR nothing more than a bug-fix.

bn_mp_factor.c Outdated
}

/* Switched off temporarily */
#if 0
Copy link
Member

Choose a reason for hiding this comment

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

same here

*/

/* Decrement "a" by one like "a--". Changes input! */
#ifdef LTM_USE_EXTRA_FUNCTIONS
Copy link
Member

Choose a reason for hiding this comment

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

IMO this macro can be removed

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It changes the input, not a very common thing in LibTomMath, so I was a bit cautious here.

But if that (remove the macro for this function and this function only) is not what you meant drop me a note please.

#ifdef LTM_USE_EXTRA_FUNCTIONS
int mp_factors_init(mp_factors *f)
{
f->factors = OPT_CAST(mp_int) XMALLOC(sizeof(mp_int) * (size_t)(LTM_TRIAL_GROWTH));
Copy link
Member

Choose a reason for hiding this comment

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

this should be cleared, shouldn't it? so it could be a XCALLOC()!?

which brings me directly into... why aren't mp_init() and mp_init_size() using XCALLOC()?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

this should be cleared, shouldn't it?

It is more or less just the backbone and gets overwritten. On the other side: better save than sorry and it doesn't cost much, so I'll clear it.

which brings me directly into... why aren't mp_init() and mp_init_size() using XCALLOC()?

It might have had a reason once but now? Yes. would support a change to XCALLOC here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Lunchbreak wasn't shortened, so I was able to rebase before I really need to run and that reminded me that XCALLOC isn't in LTM anymore. Do you want to put it back in?

for (i = 1 ; i < f->length; i++) {
idx = i;
while ((idx > 0) && (mp_cmp(&(f->factors[idx-1]),&(f->factors[idx])) == MP_GT)) {
if ((e = mp_copy(&(f->factors[idx]), &tmp)) != MP_OKAY) {
Copy link
Member

Choose a reason for hiding this comment

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

we're deep inside the library, why should we do a deep copy of all mp_int elements where it would be sufficient to exchange the mp_int?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Another leftover. I always start with mp_copy and check later if it can be replaced with mp_exch.

Thanks for taking the time to go through the whole mess!
(Lunchtime is a bit short today, the whole thing needs to wait till tonight.)

@czurnieden
Copy link
Contributor Author

Docs still need updates, sorry.
@sjaeckel I used calloc directly for now, didn't want to reimplement the 'XCALLOC' macro (yet).

@minad
Copy link
Member

minad commented Apr 2, 2019

This one is also huge. Maybe splitting it would help? @sjaeckel What do you think?

@czurnieden
Copy link
Contributor Author

@minad Not all functions are independent from each other in this PR. Do you want one PR per function? Has it pros and cons but the work for me is roughly the same, no matter what, so it's up to you to decide.

What I can split off without any problems are
bn_mp_decr.c, bn_mp_incr.c, bn_mp_expt.c, bn_mp_ilogb.c, and bn_mp_pollard_rho.c

Small packets:

  • bn_mp_miller_bach.c and bn_mp_prime_is_prime_deterministic.c (could be merged into one function or at least one file)
  • (bn_mp_n_root_ex.c, bn_mp_ispower.c, bn_mp_isperfpower.c Note: the changes for nth-root in this PR are the same as in changed seed to make nth-root usable #189 and are needed to make the is*power functions work.

The rest are more or less dependent of each other to various degrees, see callgraph.txt for all the gory details.

@minad
Copy link
Member

minad commented Apr 2, 2019

Yes I understand that there are dependencies. Maybe multiple PRs in reverse topological order. No single functions but maybe logical units. This PR adds nearly 20000 lines, that's all I saw when I looked at it first ;)

@sjaeckel
Copy link
Member

sjaeckel commented Apr 2, 2019

First I'd like to see this LTM_USE_EXTRA_FUNCTIONS macro gone

there are no "extra functions" it's in the library or it isn't :-)

and AFAICT for now it looks like this doesn't break backwards compati

Maybe multiple PRs in reverse topological order. No single functions but maybe logical units

👍

I'd start with the simple implementations and keep the complicated stuff for the last PR's

  1. the change to mp_n_root_ex is independent to all this other stuff, right? so this could be a first quick PR I've seen changed seed to make nth-root usable #189 now
  2. mp_decr + mp_expt + mp_incr
  3. mp_ilogb
  4. mp_sieve_clear + mp_sieve_init + (why is there no mp_sieve in bn_mp_sieve.c but a function called mp_is_small_prime? :-D )
  5. mp_next_small_prime + mp_prec_small_prime
  6. mp_ispower + mp_miller_bach

the remaining stuff is ... well interdependent... probably it can at least commit-wise be split up into the factors-related functions and mp_primorial + mp_small_prime_array + mp_trial ... or something like that...

yes, that's a long way, but it's also 20k LOC

@czurnieden
Copy link
Contributor Author

there are no "extra functions" it's in the library or it isn't :-)

Now, that's something I can work with! ;-)

and AFAICT for now it looks like this doesn't break backwards compati

That was intentional. Anything not backwards compatible in my code shall be considered a bug and it would be nice if I get dropped a note.
Breaking things is reserved for Version 2.0 ;-)

(why is there no mp_sieve in bn_mp_sieve.c but a function called mp_is_small_prime? :-D )

Because programmers are really bad at naming things! ;-)
It was originally all in that one file and mp_is_small_prime is the only function left after the Big De-cluttering.
But you are right, of course. The files in LTM are named after the (exported) function therein, so it should be renamed. Thankfully, renaming a file in LTM is really easy

yes, that's a long way,

I will have a free weekend this weekend (he says boldly), should suffice for a large part of that work.

but it's also 20k LOC

That much? Really? Oh.

@minad
Copy link
Member

minad commented May 29, 2019

@czurnieden Can we close this one? ilogb, incr and decr are already merged. You are proposing more things but I lost sight somehow. I guess you want to reopen smaller separate PRs for those parts.

@czurnieden
Copy link
Contributor Author

Can we close this one?

Yes. (thought it was already?)

You are proposing more things but I lost sight somehow

Yeah, it's a bit of a mess, admitted.
I cleaned my PRs as far as possible up but that's already a couple of weeks ago, they will will get another scrubbing these days.

@minad minad closed this May 29, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants