Botan 1.10.17
mp_shift.cpp
Go to the documentation of this file.
1/*
2* MP Shift Algorithms
3* (C) 1999-2007 Jack Lloyd
4*
5* Distributed under the terms of the Botan license
6*/
7
8#include <botan/internal/mp_core.h>
9#include <botan/mem_ops.h>
10
11namespace Botan {
12
13extern "C" {
14
15/*
16* Single Operand Left Shift
17*/
18void bigint_shl1(word x[], size_t x_size, size_t word_shift, size_t bit_shift)
19 {
20 if(word_shift)
21 {
22 for(size_t j = 1; j != x_size + 1; ++j)
23 x[(x_size - j) + word_shift] = x[x_size - j];
24 clear_mem(x, word_shift);
25 }
26
27 if(bit_shift)
28 {
29 word carry = 0;
30 for(size_t j = word_shift; j != x_size + word_shift + 1; ++j)
31 {
32 word temp = x[j];
33 x[j] = (temp << bit_shift) | carry;
34 carry = (temp >> (MP_WORD_BITS - bit_shift));
35 }
36 }
37 }
38
39/*
40* Single Operand Right Shift
41*/
42void bigint_shr1(word x[], size_t x_size, size_t word_shift, size_t bit_shift)
43 {
44 if(x_size < word_shift)
45 {
46 clear_mem(x, x_size);
47 return;
48 }
49
50 if(word_shift)
51 {
52 copy_mem(x, x + word_shift, x_size - word_shift);
53 clear_mem(x + x_size - word_shift, word_shift);
54 }
55
56 if(bit_shift)
57 {
58 word carry = 0;
59
60 size_t top = x_size - word_shift;
61
62 while(top >= 4)
63 {
64 word w = x[top-1];
65 x[top-1] = (w >> bit_shift) | carry;
66 carry = (w << (MP_WORD_BITS - bit_shift));
67
68 w = x[top-2];
69 x[top-2] = (w >> bit_shift) | carry;
70 carry = (w << (MP_WORD_BITS - bit_shift));
71
72 w = x[top-3];
73 x[top-3] = (w >> bit_shift) | carry;
74 carry = (w << (MP_WORD_BITS - bit_shift));
75
76 w = x[top-4];
77 x[top-4] = (w >> bit_shift) | carry;
78 carry = (w << (MP_WORD_BITS - bit_shift));
79
80 top -= 4;
81 }
82
83 while(top)
84 {
85 word w = x[top-1];
86 x[top-1] = (w >> bit_shift) | carry;
87 carry = (w << (MP_WORD_BITS - bit_shift));
88
89 top--;
90 }
91 }
92 }
93
94/*
95* Two Operand Left Shift
96*/
97void bigint_shl2(word y[], const word x[], size_t x_size,
98 size_t word_shift, size_t bit_shift)
99 {
100 for(size_t j = 0; j != x_size; ++j)
101 y[j + word_shift] = x[j];
102 if(bit_shift)
103 {
104 word carry = 0;
105 for(size_t j = word_shift; j != x_size + word_shift + 1; ++j)
106 {
107 word w = y[j];
108 y[j] = (w << bit_shift) | carry;
109 carry = (w >> (MP_WORD_BITS - bit_shift));
110 }
111 }
112 }
113
114/*
115* Two Operand Right Shift
116*/
117void bigint_shr2(word y[], const word x[], size_t x_size,
118 size_t word_shift, size_t bit_shift)
119 {
120 if(x_size < word_shift) return;
121
122 for(size_t j = 0; j != x_size - word_shift; ++j)
123 y[j] = x[j + word_shift];
124 if(bit_shift)
125 {
126 word carry = 0;
127 for(size_t j = x_size - word_shift; j > 0; --j)
128 {
129 word w = y[j-1];
130 y[j-1] = (w >> bit_shift) | carry;
131 carry = (w << (MP_WORD_BITS - bit_shift));
132 }
133 }
134 }
135
136}
137
138}
void bigint_shr1(word x[], size_t x_size, size_t word_shift, size_t bit_shift)
Definition mp_shift.cpp:42
void bigint_shl1(word x[], size_t x_size, size_t word_shift, size_t bit_shift)
Definition mp_shift.cpp:18
const size_t MP_WORD_BITS
Definition mp_core.h:18
void bigint_shl2(word y[], const word x[], size_t x_size, size_t word_shift, size_t bit_shift)
Definition mp_shift.cpp:97
void copy_mem(T *out, const T *in, size_t n)
Definition mem_ops.h:22
void bigint_shr2(word y[], const word x[], size_t x_size, size_t word_shift, size_t bit_shift)
Definition mp_shift.cpp:117
void clear_mem(T *ptr, size_t n)
Definition mem_ops.h:32