- 
                Notifications
    
You must be signed in to change notification settings  - Fork 212
 
Add a new mp_todec_fast... #330
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
          
     Open
      
      
            MasterDuke17
  wants to merge
  7
  commits into
  libtom:develop
  
    
      
        
          
  
    
      Choose a base branch
      
     
    
      
        
      
      
        
          
          
        
        
          
            
              
              
              
  
           
        
        
          
            
              
              
           
        
       
     
  
        
          
            
          
            
          
        
       
    
      
from
MasterDuke17:barrett_todecimal
  
      
      
   
  
    
  
  
  
 
  
      
    base: develop
Could not load branches
            
              
  
    Branch not found: {{ refName }}
  
            
                
      Loading
              
            Could not load tags
            
            
              Nothing to show
            
              
  
            
                
      Loading
              
            Are you sure you want to change the base?
            Some commits from the old base branch may be removed from the timeline,
            and old review comments may become outdated.
          
          
  
     Open
                    Changes from all commits
      Commits
    
    
            Show all changes
          
          
            7 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      3ee4c5e
              
                Replace the mp_to_decimal macro with a function...
              
              
                MasterDuke17 3c0162d
              
                Address comments
              
              
                MasterDuke17 91543f4
              
                Turn while into a for
              
              
                MasterDuke17 8d03878
              
                Fix undef in test.c
              
              
                MasterDuke17 8f8a58b
              
                Add BN_MP_TO_DECIMAL_C to tommath_superclass.h
              
              
                MasterDuke17 fee03dd
              
                Zero-out arrays after declaration
              
              
                MasterDuke17 d2678b0
              
                Make s_mp_to_decimal_fast_rec static
              
              
                MasterDuke17 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
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| #include "tommath_private.h" | ||
| #ifdef BN_MP_TO_DECIMAL_C | ||
| /* LibTomMath, multiple-precision integer library -- Tom St Denis */ | ||
| /* SPDX-License-Identifier: Unlicense */ | ||
| 
     | 
||
| /* stores a bignum as a decimal ASCII string, using Barrett | ||
| * reduction if available. | ||
| */ | ||
| 
     | 
||
| mp_err mp_to_decimal(const mp_int *a, char *str, size_t maxlen) | ||
| { | ||
| mp_err err; | ||
| 
     | 
||
| if (MP_HAS(S_MP_TO_DECIMAL_FAST) && (a->used > 10)) { | ||
| err = s_mp_to_decimal_fast(a, str, maxlen); | ||
| } else { | ||
| err = mp_to_radix(a, str, maxlen, 10); | ||
| } | ||
| 
     | 
||
| 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
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,246 @@ | ||
| #include "tommath_private.h" | ||
| #include <string.h> | ||
| #ifdef BN_S_MP_TO_DECIMAL_FAST_C | ||
| /* LibTomMath, multiple-precision integer library -- Tom St Denis */ | ||
| /* SPDX-License-Identifier: Unlicense */ | ||
| 
     | 
||
| /* store a bignum as a decimal ASCII string */ | ||
| static mp_err s_mp_to_decimal_fast_rec(const mp_int *number, mp_int *nL, mp_int *shiftL, mp_int *mL, | ||
| int precalc_array_index, | ||
| int left, | ||
| char **result, | ||
| size_t *maxlen) | ||
| { | ||
| mp_int q, nLq, r; | ||
| mp_err err; | ||
| 
     | 
||
| if (precalc_array_index < 0) { | ||
| int n = mp_get_i32(number), n_orig = n, digits_to_copy = 0, copy_counter; | ||
| char *result_str = *result; | ||
| char sprintf_str[4] = "000"; | ||
| 
     | 
||
| while (n) { | ||
| sprintf_str[2 - digits_to_copy] = mp_s_rmap[n % 10]; | ||
| digits_to_copy++; | ||
| n /= 10; | ||
| } | ||
| 
     | 
||
| if (!left && n_orig < 100) { | ||
| digits_to_copy++; | ||
| if (n_orig < 10) { | ||
| digits_to_copy++; | ||
| } | ||
| if (n_orig == 0) { | ||
| digits_to_copy++; | ||
| } | ||
| } | ||
| 
     | 
||
| if (*maxlen < ((size_t)digits_to_copy + 1)) { | ||
| /* no more room */ | ||
| return MP_VAL; | ||
| } | ||
| 
     | 
||
| for (copy_counter = 0; copy_counter < digits_to_copy; copy_counter++) { | ||
| result_str[copy_counter] = sprintf_str[3 - digits_to_copy + copy_counter]; | ||
| } | ||
| 
     | 
||
| *maxlen -= (size_t)digits_to_copy; | ||
| *result += digits_to_copy; | ||
| 
     | 
||
| return MP_OKAY; | ||
| } | ||
| 
     | 
||
| if ((err = mp_init_multi(&q, &nLq, &r, NULL)) != MP_OKAY) { | ||
| goto LBL_ERR; | ||
| } | ||
| if ((err = mp_mul(number, &mL[precalc_array_index], &q)) != MP_OKAY) { | ||
| goto LBL_ERR; | ||
| } | ||
| if ((err = mp_div_2d(&q, mp_get_i32(&shiftL[precalc_array_index]), &q, NULL)) != MP_OKAY) { | ||
| goto LBL_ERR; | ||
| } | ||
| 
     | 
||
| if ((err = mp_mul(&nL[precalc_array_index], &q, &nLq)) != MP_OKAY) { | ||
| goto LBL_ERR; | ||
| } | ||
| 
     | 
||
| if ((err = mp_sub(number, &nLq, &r)) != MP_OKAY) { | ||
| goto LBL_ERR; | ||
| } | ||
| 
     | 
||
| if (mp_isneg(&r)) { | ||
| if ((err = mp_decr(&q)) != MP_OKAY) { | ||
| goto LBL_ERR; | ||
| } | ||
| if ((err = mp_add(&r, &nL[precalc_array_index], &r)) != MP_OKAY) { | ||
| goto LBL_ERR; | ||
| } | ||
| } | ||
| 
     | 
||
| --precalc_array_index; | ||
| if (left && mp_iszero(&q)) { | ||
| if ((err = s_mp_to_decimal_fast_rec(&r, nL, shiftL, mL, precalc_array_index, 1, result, maxlen)) != MP_OKAY) { | ||
| goto LBL_ERR; | ||
| } | ||
| } else { | ||
| if ((err = s_mp_to_decimal_fast_rec(&q, nL, shiftL, mL, precalc_array_index, left, result, maxlen)) != MP_OKAY) { | ||
| goto LBL_ERR; | ||
| } | ||
| if ((err = s_mp_to_decimal_fast_rec(&r, nL, shiftL, mL, precalc_array_index, 0, result, maxlen)) != MP_OKAY) { | ||
| goto LBL_ERR; | ||
| } | ||
| } | ||
| 
     | 
||
| err = MP_OKAY; | ||
| 
     | 
||
| LBL_ERR: | ||
| mp_clear_multi(&q, &nLq, &r, NULL); | ||
| return err; | ||
| } | ||
| 
     | 
||
| mp_err s_mp_to_decimal_fast(const mp_int *a, char *result, size_t maxlen) | ||
| { | ||
| mp_int number, n, shift, M, M2, M22, M4, M44; | ||
| mp_int nL[20], shiftL[20], mL[20]; | ||
| mp_err err; | ||
| char **result_addr = &result; | ||
| int precalc_array_index = 1, c; | ||
| 
     | 
||
| /* check range of the maxlen */ | ||
| if (maxlen < 2) { | ||
| return MP_VAL; | ||
| } | ||
| 
     | 
||
| MP_ZERO_BUFFER(nL, sizeof(nL)); | ||
| MP_ZERO_BUFFER(shiftL, sizeof(shiftL)); | ||
| MP_ZERO_BUFFER(mL, sizeof(mL)); | ||
| 
     | 
||
| if ((err = mp_init_multi(&number, &n, &shift, &M, &M2, &M22, &M4, &M44, &nL[0], &shiftL[0], &mL[0], NULL)) != MP_OKAY) { | ||
| goto LBL_ERR; | ||
| } | ||
| 
     | 
||
| if ((err = mp_copy(a, &number)) != MP_OKAY) { | ||
| goto LBL_ERR; | ||
| } | ||
| if (mp_isneg(&number)) { | ||
| if ((err = mp_neg(&number, &number)) != MP_OKAY) { | ||
| goto LBL_ERR; | ||
| } | ||
| result[0] = '-'; | ||
| *result_addr += 1; | ||
| maxlen -= 1; | ||
                
      
                  sjaeckel marked this conversation as resolved.
               
          
            Show resolved
            Hide resolved
         | 
||
| } | ||
| mp_set_u32(&n, 1000); | ||
| 
     | 
||
| if ((err = mp_copy(&n, &nL[0])) != MP_OKAY) { | ||
| goto LBL_ERR; | ||
| } | ||
| 
     | 
||
| mp_set_u32(&shift, 20); | ||
| 
     | 
||
| if ((err = mp_copy(&shift, &shiftL[0])) != MP_OKAY) { | ||
| goto LBL_ERR; | ||
| } | ||
| 
     | 
||
| /* (8 * 2**$shift) / $n rounded up */ | ||
| mp_set_u32(&M, 8389); | ||
| 
     | 
||
| /* $M / 8, rounded up */ | ||
| mp_set_u32(&mL[0], 1049); | ||
| 
     | 
||
| for (precalc_array_index = 1; precalc_array_index < 20; precalc_array_index++) { | ||
| if ((err = mp_sqr(&n, &n)) != MP_OKAY) { | ||
| goto LBL_ERR; | ||
| } | ||
| if (mp_cmp(&n, &number) == MP_GT) { | ||
| break; | ||
| } | ||
| 
     | 
||
| if ((err = mp_mul_2(&shift, &shift)) != MP_OKAY) { | ||
| goto LBL_ERR; | ||
| } | ||
| 
     | 
||
| /* The following is a Newton-Raphson step, to restore the invariant | ||
| * that $M is (8 * 2**$shift) / $n, rounded up. */ | ||
| { | ||
| if ((err = mp_sqr(&M, &M2)) != MP_OKAY) { | ||
| goto LBL_ERR; | ||
| } | ||
| if ((err = mp_sqr(&M2, &M4)) != MP_OKAY) { | ||
| goto LBL_ERR; | ||
| } | ||
| 
     | 
||
| if ((err = mp_mul(&M4, &n, &M4)) != MP_OKAY) { | ||
| goto LBL_ERR; | ||
| } | ||
| if ((err = mp_div_2d(&M4, mp_get_i32(&shift) + 6, &M4, NULL)) != MP_OKAY) { | ||
| goto LBL_ERR; | ||
| } | ||
| if ((err = mp_mul_2(&M2, &M2)) != MP_OKAY) { | ||
| goto LBL_ERR; | ||
| } | ||
| if ((err = mp_sub(&M4, &M2, &M4)) != MP_OKAY) { | ||
| goto LBL_ERR; | ||
| } | ||
| if ((err = mp_incr(&M4)) != MP_OKAY) { | ||
| goto LBL_ERR; | ||
| } | ||
| if ((err = mp_div_2d(&M4, 3, &M4, NULL)) != MP_OKAY) { | ||
| goto LBL_ERR; | ||
| } | ||
| if ((err = mp_decr(&M4)) != MP_OKAY) { | ||
| goto LBL_ERR; | ||
| } | ||
| if ((err = mp_neg(&M4, &M)) != MP_OKAY) { | ||
| goto LBL_ERR; | ||
| } | ||
| } | ||
| 
     | 
||
| if ((err = mp_init_copy(&nL[precalc_array_index], &n)) != MP_OKAY) { | ||
| goto LBL_ERR; | ||
| } | ||
| if ((err = mp_init_copy(&shiftL[precalc_array_index], &shift)) != MP_OKAY) { | ||
| goto LBL_ERR; | ||
| } | ||
| 
     | 
||
| /* Divide by 8, round up */ | ||
| { | ||
| if ((err = mp_add_d(&M4, 1, &M4)) != MP_OKAY) { | ||
| goto LBL_ERR; | ||
| } | ||
| if ((err = mp_div_2d(&M4, 3, &M4, NULL)) != MP_OKAY) { | ||
| goto LBL_ERR; | ||
| } | ||
| if ((err = mp_sub_d(&M4, 1, &M4)) != MP_OKAY) { | ||
| goto LBL_ERR; | ||
| } | ||
| if ((err = mp_neg(&M4, &M4)) != MP_OKAY) { | ||
| goto LBL_ERR; | ||
| } | ||
| } | ||
| if ((err = mp_init_copy(&mL[precalc_array_index], &M4)) != MP_OKAY) { | ||
| goto LBL_ERR; | ||
| } | ||
| } | ||
| if (precalc_array_index >= 20) { | ||
| err = MP_VAL; | ||
| goto LBL_ERR; | ||
| } | ||
| 
     | 
||
| if ((err = s_mp_to_decimal_fast_rec(&number, nL, shiftL, mL, precalc_array_index - 1, 1, result_addr, | ||
| &maxlen)) != MP_OKAY) { | ||
| goto LBL_ERR; | ||
| } | ||
| *result_addr[0] = '\0'; | ||
| 
     | 
||
| err = MP_OKAY; | ||
| 
     | 
||
| LBL_ERR: | ||
| mp_clear_multi(&number, &n, &shift, &M, &M2, &M22, &M4, &M44, &nL[0], &shiftL[0], &mL[0], NULL); | ||
                
      
                  sjaeckel marked this conversation as resolved.
               
          
            Show resolved
            Hide resolved
         | 
||
| for (c = 1; c < precalc_array_index; c++) { | ||
| mp_clear_multi(&nL[c], &shiftL[c], &mL[c], 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
    
  
  
    
              
  
    
      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
    
  
  
    
              
      
      Oops, something went wrong.
        
    
  
  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.
@fperrad can you please expand [1] with the rules on how such statements should be embraced as of your usual linting?
[1] https://github.com/libtom/libtommath/wiki/The-Sourcecode:-Formatting-and-Style