Botan  1.10.9
mp_asm.cpp
Go to the documentation of this file.
1 /*
2 * Lowest Level MPI Algorithms
3 * (C) 1999-2010 Jack Lloyd
4 * 2006 Luca Piccarreta
5 *
6 * Distributed under the terms of the Botan license
7 */
8 
9 #include <botan/internal/mp_asm.h>
10 #include <botan/internal/mp_asmi.h>
11 #include <botan/internal/mp_core.h>
12 #include <botan/exceptn.h>
13 #include <botan/mem_ops.h>
14 
15 namespace Botan {
16 
17 extern "C" {
18 
19 /*
20 * Two Operand Addition, No Carry
21 */
22 word bigint_add2_nc(word x[], size_t x_size, const word y[], size_t y_size)
23  {
24  word carry = 0;
25 
26  const size_t blocks = y_size - (y_size % 8);
27 
28  for(size_t i = 0; i != blocks; i += 8)
29  carry = word8_add2(x + i, y + i, carry);
30 
31  for(size_t i = blocks; i != y_size; ++i)
32  x[i] = word_add(x[i], y[i], &carry);
33 
34  for(size_t i = y_size; i != x_size; ++i)
35  x[i] = word_add(x[i], 0, &carry);
36 
37  return carry;
38  }
39 
40 /*
41 * Three Operand Addition, No Carry
42 */
43 word bigint_add3_nc(word z[], const word x[], size_t x_size,
44  const word y[], size_t y_size)
45  {
46  if(x_size < y_size)
47  { return bigint_add3_nc(z, y, y_size, x, x_size); }
48 
49  word carry = 0;
50 
51  const size_t blocks = y_size - (y_size % 8);
52 
53  for(size_t i = 0; i != blocks; i += 8)
54  carry = word8_add3(z + i, x + i, y + i, carry);
55 
56  for(size_t i = blocks; i != y_size; ++i)
57  z[i] = word_add(x[i], y[i], &carry);
58 
59  for(size_t i = y_size; i != x_size; ++i)
60  z[i] = word_add(x[i], 0, &carry);
61 
62  return carry;
63  }
64 
65 /*
66 * Two Operand Addition
67 */
68 void bigint_add2(word x[], size_t x_size, const word y[], size_t y_size)
69  {
70  if(bigint_add2_nc(x, x_size, y, y_size))
71  x[x_size] += 1;
72  }
73 
74 /*
75 * Three Operand Addition
76 */
77 void bigint_add3(word z[], const word x[], size_t x_size,
78  const word y[], size_t y_size)
79  {
80  z[(x_size > y_size ? x_size : y_size)] +=
81  bigint_add3_nc(z, x, x_size, y, y_size);
82  }
83 
84 /*
85 * Two Operand Subtraction
86 */
87 word bigint_sub2(word x[], size_t x_size, const word y[], size_t y_size)
88  {
89  word borrow = 0;
90 
91  const size_t blocks = y_size - (y_size % 8);
92 
93  for(size_t i = 0; i != blocks; i += 8)
94  borrow = word8_sub2(x + i, y + i, borrow);
95 
96  for(size_t i = blocks; i != y_size; ++i)
97  x[i] = word_sub(x[i], y[i], &borrow);
98 
99  for(size_t i = y_size; i != x_size; ++i)
100  x[i] = word_sub(x[i], 0, &borrow);
101 
102  return borrow;
103  }
104 
105 /*
106 * Two Operand Subtraction x = y - x
107 */
108 void bigint_sub2_rev(word x[], const word y[], size_t y_size)
109  {
110  word borrow = 0;
111 
112  const size_t blocks = y_size - (y_size % 8);
113 
114  for(size_t i = 0; i != blocks; i += 8)
115  borrow = word8_sub2_rev(x + i, y + i, borrow);
116 
117  for(size_t i = blocks; i != y_size; ++i)
118  x[i] = word_sub(y[i], x[i], &borrow);
119 
120  if(borrow)
121  throw Internal_Error("bigint_sub2_rev: x >= y");
122  }
123 
124 /*
125 * Three Operand Subtraction
126 */
127 word bigint_sub3(word z[], const word x[], size_t x_size,
128  const word y[], size_t y_size)
129  {
130  word borrow = 0;
131 
132  const size_t blocks = y_size - (y_size % 8);
133 
134  for(size_t i = 0; i != blocks; i += 8)
135  borrow = word8_sub3(z + i, x + i, y + i, borrow);
136 
137  for(size_t i = blocks; i != y_size; ++i)
138  z[i] = word_sub(x[i], y[i], &borrow);
139 
140  for(size_t i = y_size; i != x_size; ++i)
141  z[i] = word_sub(x[i], 0, &borrow);
142 
143  return borrow;
144  }
145 
146 /*
147 * Two Operand Linear Multiply
148 */
149 void bigint_linmul2(word x[], size_t x_size, word y)
150  {
151  const size_t blocks = x_size - (x_size % 8);
152 
153  word carry = 0;
154 
155  for(size_t i = 0; i != blocks; i += 8)
156  carry = word8_linmul2(x + i, y, carry);
157 
158  for(size_t i = blocks; i != x_size; ++i)
159  x[i] = word_madd2(x[i], y, &carry);
160 
161  x[x_size] = carry;
162  }
163 
164 /*
165 * Three Operand Linear Multiply
166 */
167 void bigint_linmul3(word z[], const word x[], size_t x_size, word y)
168  {
169  const size_t blocks = x_size - (x_size % 8);
170 
171  word carry = 0;
172 
173  for(size_t i = 0; i != blocks; i += 8)
174  carry = word8_linmul3(z + i, x + i, y, carry);
175 
176  for(size_t i = blocks; i != x_size; ++i)
177  z[i] = word_madd2(x[i], y, &carry);
178 
179  z[x_size] = carry;
180  }
181 
182 }
183 
184 }
void bigint_sub2_rev(word x[], const word y[], size_t y_size)
Definition: mp_asm.cpp:108
word word8_sub2_rev(word x[8], const word y[8], word carry)
Definition: mp_asmi.h:94
word word8_add2(word x[8], const word y[8], word carry)
Definition: mp_asmi.h:33
word word8_linmul3(word z[8], const word x[8], word y, word carry)
Definition: mp_asmi.h:143
word bigint_sub2(word x[], size_t x_size, const word y[], size_t y_size)
Definition: mp_asm.cpp:87
void bigint_linmul2(word x[], size_t x_size, word y)
Definition: mp_asm.cpp:149
word bigint_add3_nc(word z[], const word x[], size_t x_size, const word y[], size_t y_size)
Definition: mp_asm.cpp:43
word word8_sub2(word x[8], const word y[8], word carry)
Definition: mp_asmi.h:78
word bigint_sub3(word z[], const word x[], size_t x_size, const word y[], size_t y_size)
Definition: mp_asm.cpp:127
word word_madd2(word a, word b, word *c)
Definition: mp_asm.h:86
void bigint_linmul3(word z[], const word x[], size_t x_size, word y)
Definition: mp_asm.cpp:167
word word8_linmul2(word x[8], word y, word carry)
Definition: mp_asmi.h:127
word bigint_add2_nc(word x[], size_t x_size, const word y[], size_t y_size)
Definition: mp_asm.cpp:22
word word8_add3(word z[8], const word x[8], const word y[8], word carry)
Definition: mp_asmi.h:49
word word_sub(word x, word y, word *carry)
Definition: mp_asmi.h:66
void bigint_add2(word x[], size_t x_size, const word y[], size_t y_size)
Definition: mp_asm.cpp:68
word word_add(word x, word y, word *carry)
Definition: mp_asmi.h:21
word word8_sub3(word z[8], const word x[8], const word y[8], word carry)
Definition: mp_asmi.h:110
void bigint_add3(word z[], const word x[], size_t x_size, const word y[], size_t y_size)
Definition: mp_asm.cpp:77