<commit_before>/*
* Montgomery Reduction
* (C) 1999-2010 <NAME>
*     2006 <NAME>
*
* Distributed under the terms of the Botan license
*/

#include <botan/internal/mp_core.h>
#include <botan/internal/mp_asm.h>
#include <botan/internal/mp_asmi.h>
#include <botan/mem_ops.h>

namespace Botan {

extern "C" {

/*
* Montgomery Reduction Algorithm
*/
void bigint_monty_redc(word z[], u32bit z_size,
                       word ws[],
                       const word x[], u32bit x_size,
                       word u)
   {
   const u32bit blocks_of_8 = x_size - (x_size % 8);

   for(u32bit i = 0; i != x_size; ++i)
      {
      word* z_i = z + i;

      const word y = z_i[0] * u;

      /*
      bigint_linmul3(ws, x, x_size, y);
      bigint_add2(z_i, z_size - i, ws, x_size+1);
      */
      word carry = 0;

      for(u32bit j = 0; j != blocks_of_8; j += 8)
         carry = word8_madd3(z_i + j, x + j, y, carry);

      for(u32bit j = blocks_of_8; j != x_size; ++j)
         z_i[j] = word_madd3(x[j], y, z_i[j], &carry);

      word z_sum = z_i[x_size] + carry;
      carry = (z_sum < z_i[x_size]);
      z_i[x_size] = z_sum;

      // Note: not constant time
      for(u32bit j = x_size + 1; carry && j != z_size - i; ++j)
         {
         ++z_i[j];
         carry = !z_i[j];
         }
      }

   word borrow = 0;
   for(u32bit i = 0; i != x_size; ++i)
      ws[i] = word_sub(z[x_size + i], x[i], &borrow);

   ws[x_size] = word_sub(z[x_size+x_size], 0, &borrow);

   copy_mem(ws + x_size + 1, z + x_size, x_size + 1);

   clear_mem(z, z_size);

   copy_mem(z, ws + borrow*(x_size+1), x_size + 1);
   }

}

}<commit_msg>Minor performance tweak, avoid zeroing things we'll immediately write to. Helps more than I would have thought.<commit_after>/*
* Montgomery Reduction
* (C) 1999-2010 <NAME>
*     2006 <NAME>
*
* Distributed under the terms of the Botan license
*/

#include <botan/internal/mp_core.h>
#include <botan/internal/mp_asm.h>
#include <botan/internal/mp_asmi.h>
#include <botan/mem_ops.h>

namespace Botan {

extern "C" {

/*
* Montgomery Reduction Algorithm
*/
void bigint_monty_redc(word z[], u32bit z_size,
                       word ws[],
                       const word x[], u32bit x_size,
                       word u)
   {
   const u32bit blocks_of_8 = x_size - (x_size % 8);

   for(u32bit i = 0; i != x_size; ++i)
      {
      word* z_i = z + i;

      const word y = z_i[0] * u;

      /*
      bigint_linmul3(ws, x, x_size, y);
      bigint_add2(z_i, z_size - i, ws, x_size+1);
      */
      word carry = 0;

      for(u32bit j = 0; j != blocks_of_8; j += 8)
         carry = word8_madd3(z_i + j, x + j, y, carry);

      for(u32bit j = blocks_of_8; j != x_size; ++j)
         z_i[j] = word_madd3(x[j], y, z_i[j], &carry);

      word z_sum = z_i[x_size] + carry;
      carry = (z_sum < z_i[x_size]);
      z_i[x_size] = z_sum;

      // Note: not constant time
      for(u32bit j = x_size + 1; carry && j != z_size - i; ++j)
         {
         ++z_i[j];
         carry = !z_i[j];
         }
      }

   word borrow = 0;
   for(u32bit i = 0; i != x_size; ++i)
      ws[i] = word_sub(z[x_size + i], x[i], &borrow);

   ws[x_size] = word_sub(z[x_size+x_size], 0, &borrow);

   copy_mem(ws + x_size + 1, z + x_size, x_size + 1);

   copy_mem(z, ws + borrow*(x_size+1), x_size + 1);
   clear_mem(z + x_size + 1, z_size - x_size - 1);
   }

}

}
{'fraction_non_alphanumeric': 0.15616681455190773, 'fraction_numerical': 0.03076013013901213, 'mean_word_length': 2.143122676579926, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4812500', 'n_tokens_mistral': 1510, 'n_tokens_neox': 1403, 'n_words': 340}