-
Notifications
You must be signed in to change notification settings - Fork 215
Addition of shortcuts for bases that are powers of two and for base 10 to mp_radix_size #371
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
Closed
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
fd7c847
Addition of shortcuts for bases that are powers of two and for base 1…
czurnieden 223e7fc
adaption of mp_radix_size to new mp_log_u32
czurnieden ccedd6a
Addition of BN_MP_LOG_U32 to tommath_superclass.h
czurnieden aab7e6c
rebase
czurnieden ae492e0
further refinement in s_mp_radix_size_radix_10
czurnieden 39cc86e
added branch for MP_16BIT to s_mp_radix_size_radix_10
czurnieden 9bf64b3
unrolled loop
czurnieden 04c1332
cleanup
czurnieden File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| #include "tommath_private.h" | ||
| #ifdef S_MP_LOG_POWER_OF_TWO_C | ||
| /* LibTomMath, multiple-precision integer library -- Tom St Denis */ | ||
| /* SPDX-License-Identifier: Unlicense */ | ||
|
|
||
|
|
||
|
|
||
| int s_mp_log_power_of_two(const mp_int *a, int p_of_2) | ||
| { | ||
| int x, bit_count; | ||
| for (x = 0; (x < 7) && !((unsigned int)p_of_2 & 1u); x++) { | ||
| p_of_2 >>= 1; | ||
| } | ||
| bit_count = mp_count_bits(a) - 1; | ||
| return (bit_count/x); | ||
| } | ||
|
|
||
|
|
||
| #endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| #include "tommath_private.h" | ||
| #ifdef S_MP_RADIX_SIZE_RADIX_10_C | ||
| /* LibTomMath, multiple-precision integer library -- Tom St Denis */ | ||
| /* SPDX-License-Identifier: Unlicense */ | ||
|
|
||
| #define LTM_RADIX_SIZE_SCALE 64 | ||
| int s_mp_radix_size_radix_10(const mp_int *a, size_t *size) | ||
| { | ||
| mp_err err; | ||
| /* | ||
| floor(2^64/log_2(10)) = 5553023288523357132 | ||
|
|
||
| We need that much precision. Example: | ||
|
|
||
| log_2(x) = log(x)/log(2) | ||
| 198096465/log_2(10) = 59632978.000000002598316594477929217520 | ||
|
|
||
| Which are more than 16 decimal digits, so with BINARY_64 (C's "double") | ||
| a 'ceil(198096465/log_2(10))' would result wrongly in 59632978.0. | ||
| */ | ||
| mp_int bi_bit_count, bi_k, t; | ||
| int bit_count; | ||
|
|
||
| #ifdef MP_16BIT | ||
| #define LTM_RADIX_SIZE_CONST_SHIFT 32 | ||
| const uint32_t inv_log_2_10[2] = {0x4d104d42UL, 0x7de7fbccUL}; | ||
| #endif | ||
| if ((err = mp_init_multi(&bi_bit_count, &bi_k, &t, NULL)) != MP_OKAY) { | ||
| return err; | ||
| } | ||
| #ifdef MP_16BIT | ||
| mp_set_u32(&t, inv_log_2_10[0]); | ||
| if ((err = mp_mul_2d(&bi_k, LTM_RADIX_SIZE_CONST_SHIFT, &bi_k)) != MP_OKAY) { | ||
| goto LTM_E1; | ||
| } | ||
| if ((err = mp_add(&bi_k, &t, &bi_k)) != MP_OKAY) { | ||
| goto LTM_E1; | ||
| } | ||
| mp_set_u32(&t, inv_log_2_10[1]); | ||
| if ((err = mp_mul_2d(&bi_k, LTM_RADIX_SIZE_CONST_SHIFT, &bi_k)) != MP_OKAY) { | ||
| goto LTM_E1; | ||
| } | ||
| if ((err = mp_add(&bi_k, &t, &bi_k)) != MP_OKAY) { | ||
| goto LTM_E1; | ||
| } | ||
| #else | ||
| mp_set_u64(&bi_k, 0x4d104d427de7fbccULL); | ||
| #endif | ||
| bit_count = mp_count_bits(a) + 1; | ||
| mp_set_l(&bi_bit_count, bit_count); | ||
| if ((err = mp_mul(&bi_bit_count, &bi_k, &bi_k)) != MP_OKAY) { | ||
| goto LTM_E1; | ||
| } | ||
| if ((err = mp_div_2d(&bi_k, LTM_RADIX_SIZE_SCALE, &bi_k, NULL)) != MP_OKAY) { | ||
| goto LTM_E1; | ||
| } | ||
| *size = (size_t)mp_get_ul(&bi_k); | ||
| *size += 2u + (size_t)(a->sign == MP_NEG); | ||
| LTM_E1: | ||
| mp_clear_multi(&bi_bit_count, &bi_k, &t, NULL); | ||
| return err; | ||
| } | ||
|
|
||
|
|
||
| #endif | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
@czurnieden why don't you add a function s_mp_log10 which is called from mp_log and used indirectly bymp_radix_size?
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.
I was, and still am, a bit torn between calling that optimization in
mp_logor inmp_radix_size.Calling it in
mp_radix_sizeallows for easy reduction to the restricted radix-set2,4,8,10,16,32,64and would also getting rid of the dependencymp_logwhich isn't a very small function.Calling it in
mp_logis cleaner if we want to keep the full radix-set.Mmh…
*strokes sesquipedalian beard*
I don't know.
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.
Well, but you can strip down mp_log by disabling s_mp_log and only enabling the power of two and base 10 versions. We had that discussion in #389. I think it should go to mp_log since it is more general.
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.
If you strip down 'mp_log' you don't have a general log-function anymore.
If you strip down 'mp_radix_size' you still have a radix-size function, just not for the small range of radices 2-64, only for the smaller range of powers-of-two and base 10.
You expect of a log-function that it works over the whole range, with no holes in it.
You expect from a radix-size function to work over a very small range, it might even have holes in it. Restricting the string out/input to only a handfull of bases, sometimes down to only two (10 and 16) won't get many complains (vid. e.g.:
printf).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.
Yes, but we don't get an optimized log function if we add the base10 optimization only to radix_size. We already have other functions with holes in it if configured as such.