Botan  2.6.0
Crypto and TLS for C++11
mp_core.h
Go to the documentation of this file.
1 /*
2 * MPI Algorithms
3 * (C) 1999-2010 Jack Lloyd
4 * 2006 Luca Piccarreta
5 * 2016 Matthias Gierlings
6 *
7 * Botan is released under the Simplified BSD License (see license.txt)
8 */
9 
10 #ifndef BOTAN_MP_CORE_OPS_H_
11 #define BOTAN_MP_CORE_OPS_H_
12 
13 #include <botan/types.h>
14 
15 namespace Botan {
16 
17 const word MP_WORD_MASK = ~static_cast<word>(0);
18 const word MP_WORD_TOP_BIT = static_cast<word>(1) << (8*sizeof(word) - 1);
20 
21 /*
22 * If cond == 0, does nothing.
23 * If cond > 0, swaps x[0:size] with y[0:size]
24 * Runs in constant time
25 */
27 void bigint_cnd_swap(word cnd, word x[], word y[], size_t size);
28 
29 /*
30 * If cond > 0 adds x[0:size] to y[0:size] and returns carry
31 * Runs in constant time
32 */
34 word bigint_cnd_add(word cnd, word x[], const word y[], size_t size);
35 
36 /*
37 * If cond > 0 subs x[0:size] to y[0:size] and returns borrow
38 * Runs in constant time
39 */
41 word bigint_cnd_sub(word cnd, word x[], const word y[], size_t size);
42 
43 /*
44 * 2s complement absolute value
45 * If cond > 0 sets x to ~x + 1
46 * Runs in constant time
47 */
49 void bigint_cnd_abs(word cnd, word x[], size_t size);
50 
51 /**
52 * Two operand addition
53 * @param x the first operand (and output)
54 * @param x_size size of x
55 * @param y the second operand
56 * @param y_size size of y (must be >= x_size)
57 */
58 void bigint_add2(word x[], size_t x_size,
59  const word y[], size_t y_size);
60 
61 /**
62 * Three operand addition
63 */
64 void bigint_add3(word z[],
65  const word x[], size_t x_size,
66  const word y[], size_t y_size);
67 
68 /**
69 * Two operand addition with carry out
70 */
71 word bigint_add2_nc(word x[], size_t x_size, const word y[], size_t y_size);
72 
73 /**
74 * Three operand addition with carry out
75 */
76 word bigint_add3_nc(word z[],
77  const word x[], size_t x_size,
78  const word y[], size_t y_size);
79 
80 /**
81 * Two operand subtraction
82 */
83 word bigint_sub2(word x[], size_t x_size,
84  const word y[], size_t y_size);
85 
86 /**
87 * Two operand subtraction, x = y - x; assumes y >= x
88 */
89 void bigint_sub2_rev(word x[], const word y[], size_t y_size);
90 
91 /**
92 * Three operand subtraction
93 */
94 word bigint_sub3(word z[],
95  const word x[], size_t x_size,
96  const word y[], size_t y_size);
97 
98 /*
99 * Shift Operations
100 */
101 void bigint_shl1(word x[], size_t x_size,
102  size_t word_shift, size_t bit_shift);
103 
104 void bigint_shr1(word x[], size_t x_size,
105  size_t word_shift, size_t bit_shift);
106 
107 void bigint_shl2(word y[], const word x[], size_t x_size,
108  size_t word_shift, size_t bit_shift);
109 
110 void bigint_shr2(word y[], const word x[], size_t x_size,
111  size_t word_shift, size_t bit_shift);
112 
113 /*
114 * Linear Multiply
115 */
116 void bigint_linmul2(word x[], size_t x_size, word y);
117 void bigint_linmul3(word z[], const word x[], size_t x_size, word y);
118 
119 /**
120 * Montgomery Reduction
121 * @param z integer to reduce, of size exactly 2*(p_size+1).
122  Output is in the first p_size+1 words, higher
123  words are set to zero.
124 * @param p modulus
125 * @param p_size size of p
126 * @param p_dash Montgomery value
127 * @param workspace array of at least 2*(p_size+1) words
128 * @param ws_size size of workspace in words
129 */
130 void bigint_monty_redc(word z[],
131  const word p[], size_t p_size,
132  word p_dash,
133  word workspace[],
134  size_t ws_size);
135 
136 /**
137 * Compare x and y
138 */
139 int32_t bigint_cmp(const word x[], size_t x_size,
140  const word y[], size_t y_size);
141 
142 /**
143 * Compute ((n1<<bits) + n0) / d
144 */
145 word bigint_divop(word n1, word n0, word d);
146 
147 /**
148 * Compute ((n1<<bits) + n0) % d
149 */
150 word bigint_modop(word n1, word n0, word d);
151 
152 /*
153 * Comba Multiplication / Squaring
154 */
155 void bigint_comba_mul4(word z[8], const word x[4], const word y[4]);
156 void bigint_comba_mul6(word z[12], const word x[6], const word y[6]);
157 void bigint_comba_mul8(word z[16], const word x[8], const word y[8]);
158 void bigint_comba_mul9(word z[18], const word x[9], const word y[9]);
159 void bigint_comba_mul16(word z[32], const word x[16], const word y[16]);
160 
161 void bigint_comba_sqr4(word out[8], const word in[4]);
162 void bigint_comba_sqr6(word out[12], const word in[6]);
163 void bigint_comba_sqr8(word out[16], const word in[8]);
164 void bigint_comba_sqr9(word out[18], const word in[9]);
165 void bigint_comba_sqr16(word out[32], const word in[16]);
166 
167 /*
168 * High Level Multiplication/Squaring Interfaces
169 */
170 
171 void bigint_mul(word z[], size_t z_size,
172  const word x[], size_t x_size, size_t x_sw,
173  const word y[], size_t y_size, size_t y_sw,
174  word workspace[], size_t ws_size);
175 
176 void bigint_sqr(word z[], size_t z_size,
177  const word x[], size_t x_size, size_t x_sw,
178  word workspace[], size_t ws_size);
179 
180 }
181 
182 #endif
void bigint_shr1(word x[], size_t x_size, size_t word_shift, size_t bit_shift)
Definition: mp_core.cpp:301
void bigint_sub2_rev(word x[], const word y[], size_t y_size)
Definition: mp_core.cpp:200
void bigint_shr2(word y[], const word x[], size_t x_size, size_t word_shift, size_t bit_shift)
Definition: mp_core.cpp:376
int32_t bigint_cmp(const word x[], size_t x_size, const word y[], size_t y_size)
Definition: mp_core.cpp:398
void bigint_cnd_abs(word cnd, word x[], size_t size)
Definition: mp_core.cpp:95
word bigint_sub2(word x[], size_t x_size, const word y[], size_t y_size)
Definition: mp_core.cpp:177
void bigint_linmul2(word x[], size_t x_size, word y)
Definition: mp_core.cpp:242
word bigint_add3_nc(word z[], const word x[], size_t x_size, const word y[], size_t y_size)
Definition: mp_core.cpp:133
void bigint_cnd_swap(word cnd, word x[], word y[], size_t size)
Definition: mp_core.cpp:22
word bigint_cnd_add(word cnd, word x[], const word y[], size_t size)
Definition: mp_core.cpp:39
void bigint_comba_mul4(word z[8], const word x[4], const word y[4])
Definition: mp_comba.cpp:50
word bigint_sub3(word z[], const word x[], size_t x_size, const word y[], size_t y_size)
Definition: mp_core.cpp:218
word bigint_divop(word n1, word n0, word d)
Definition: mp_core.cpp:424
void bigint_comba_mul9(word z[18], const word x[9], const word y[9])
Definition: mp_comba.cpp:474
#define BOTAN_TEST_API
Definition: compiler.h:41
void bigint_sqr(word z[], size_t z_size, const word x[], size_t x_size, size_t x_sw, word workspace[], size_t ws_size)
Definition: mp_karat.cpp:351
void bigint_linmul3(word z[], const word x[], size_t x_size, word y)
Definition: mp_core.cpp:260
void bigint_comba_sqr16(word z[32], const word x[16])
Definition: mp_comba.cpp:598
word bigint_cnd_sub(word cnd, word x[], const word y[], size_t size)
Definition: mp_core.cpp:69
word bigint_add2_nc(word x[], size_t x_size, const word y[], size_t y_size)
Definition: mp_core.cpp:110
Definition: alg_id.cpp:13
void bigint_comba_sqr9(word z[18], const word x[9])
Definition: mp_comba.cpp:386
void bigint_mul(word z[], size_t z_size, const word x[], size_t x_size, size_t x_sw, const word y[], size_t y_size, size_t y_sw, word workspace[], size_t ws_size)
Definition: mp_karat.cpp:296
void bigint_monty_redc(word z[], const word p[], size_t p_size, word p_dash, word workspace[], size_t ws_size)
Definition: mp_monty.cpp:22
void bigint_shl2(word y[], const word x[], size_t x_size, size_t word_shift, size_t bit_shift)
Definition: mp_core.cpp:356
const word MP_WORD_MASK
Definition: mp_core.h:17
void bigint_shl1(word x[], size_t x_size, size_t word_shift, size_t bit_shift)
Definition: mp_core.cpp:278
void bigint_comba_mul8(word z[16], const word x[8], const word y[8])
Definition: mp_comba.cpp:283
const word MP_WORD_MAX
Definition: mp_core.h:19
void bigint_comba_sqr8(word z[16], const word x[8])
Definition: mp_comba.cpp:208
void bigint_add2(word x[], size_t x_size, const word y[], size_t y_size)
Definition: mp_core.cpp:158
void bigint_comba_mul16(word z[32], const word x[16], const word y[16])
Definition: mp_comba.cpp:805
void bigint_comba_mul6(word z[12], const word x[6], const word y[6])
Definition: mp_comba.cpp:141
void bigint_comba_sqr4(word z[8], const word x[4])
Definition: mp_comba.cpp:17
void bigint_comba_sqr6(word z[12], const word x[6])
Definition: mp_comba.cpp:89
const word MP_WORD_TOP_BIT
Definition: mp_core.h:18
void bigint_add3(word z[], const word x[], size_t x_size, const word y[], size_t y_size)
Definition: mp_core.cpp:167
word bigint_modop(word n1, word n0, word d)
Definition: mp_core.cpp:457