aboutsummaryrefslogtreecommitdiffstats
path: root/src/mp_shift.cpp
blob: a7ed06caa4d57e0e359a7c7d765392685f665ad6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/*************************************************
* MP Shift Algorithms Source File                *
* (C) 1999-2006 The Botan Project                *
*************************************************/

#include <botan/mp_core.h>
#include <botan/mem_ops.h>

namespace Botan {

extern "C" {

/*************************************************
* Single Operand Left Shift                      *
*************************************************/
void bigint_shl1(word x[], u32bit x_size, u32bit word_shift, u32bit bit_shift)
   {
   if(word_shift)
      {
      for(u32bit j = 1; j != x_size + 1; ++j)
         x[(x_size - j) + word_shift] = x[x_size - j];
      clear_mem(x, word_shift);
      }

   if(bit_shift)
      {
      word carry = 0;
      for(u32bit j = word_shift; j != x_size + word_shift + 1; ++j)
         {
         word temp = x[j];
         x[j] = (temp << bit_shift) | carry;
         carry = (temp >> (MP_WORD_BITS - bit_shift));
         }
      }
   }

/*************************************************
* Single Operand Right Shift                     *
*************************************************/
void bigint_shr1(word x[], u32bit x_size, u32bit word_shift, u32bit bit_shift)
   {
   if(x_size < word_shift)
      {
      clear_mem(x, x_size);
      return;
      }

   for(u32bit j = 0; j != x_size - word_shift; ++j)
      x[j] = x[j + word_shift];
   for(u32bit j = x_size - word_shift; j != x_size; ++j)
      x[j] = 0;

   if(bit_shift)
      {
      word carry = 0;
      for(u32bit j = x_size - word_shift; j > 0; --j)
         {
         word temp = x[j-1];
         x[j-1] = (temp >> bit_shift) | carry;
         carry = (temp << (MP_WORD_BITS - bit_shift));
         }
      }
   }

/*************************************************
* Two Operand Left Shift                         *
*************************************************/
void bigint_shl2(word y[], const word x[], u32bit x_size,
                 u32bit word_shift, u32bit bit_shift)
   {
   for(u32bit j = 0; j != x_size; ++j)
      y[j + word_shift] = x[j];
   if(bit_shift)
      {
      word carry = 0;
      for(u32bit j = word_shift; j != x_size + word_shift + 1; ++j)
         {
         word temp = y[j];
         y[j] = (temp << bit_shift) | carry;
         carry = (temp >> (MP_WORD_BITS - bit_shift));
         }
      }
   }

/*************************************************
* Two Operand Right Shift                        *
*************************************************/
void bigint_shr2(word y[], const word x[], u32bit x_size,
                 u32bit word_shift, u32bit bit_shift)
   {
   if(x_size < word_shift) return;

   for(u32bit j = 0; j != x_size - word_shift; ++j)
      y[j] = x[j + word_shift];
   if(bit_shift)
      {
      word carry = 0;
      for(u32bit j = x_size - word_shift; j > 0; --j)
         {
         word temp = y[j-1];
         y[j-1] = (temp >> bit_shift) | carry;
         carry = (temp << (MP_WORD_BITS - bit_shift));
         }
      }
   }

}

}