Botan  2.4.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/mp_types.h>
14 
15 namespace Botan {
16 
17 class BigInt;
18 
19 /*
20 * The size of the word type, in bits
21 */
22 const size_t MP_WORD_BITS = BOTAN_MP_WORD_BITS;
23 
24 /*
25 * If cond == 0, does nothing.
26 * If cond > 0, swaps x[0:size] with y[0:size]
27 * Runs in constant time
28 */
30 void bigint_cnd_swap(word cnd, word x[], word y[], size_t size);
31 
32 /*
33 * If cond > 0 adds x[0:size] to y[0:size] and returns carry
34 * Runs in constant time
35 */
37 word bigint_cnd_add(word cnd, word x[], const word y[], size_t size);
38 
39 /*
40 * If cond > 0 subs x[0:size] to y[0:size] and returns borrow
41 * Runs in constant time
42 */
44 word bigint_cnd_sub(word cnd, word x[], const word y[], size_t size);
45 
46 /*
47 * 2s complement absolute value
48 * If cond > 0 sets x to ~x + 1
49 * Runs in constant time
50 */
52 void bigint_cnd_abs(word cnd, word x[], size_t size);
53 
54 /**
55 * Two operand addition
56 * @param x the first operand (and output)
57 * @param x_size size of x
58 * @param y the second operand
59 * @param y_size size of y (must be >= x_size)
60 */
61 void bigint_add2(word x[], size_t x_size,
62  const word y[], size_t y_size);
63 
64 /**
65 * Three operand addition
66 */
67 void bigint_add3(word z[],
68  const word x[], size_t x_size,
69  const word y[], size_t y_size);
70 
71 /**
72 * Two operand addition with carry out
73 */
74 word bigint_add2_nc(word x[], size_t x_size, const word y[], size_t y_size);
75 
76 /**
77 * Three operand addition with carry out
78 */
79 word bigint_add3_nc(word z[],
80  const word x[], size_t x_size,
81  const word y[], size_t y_size);
82 
83 /**
84 * Two operand subtraction
85 */
86 word bigint_sub2(word x[], size_t x_size,
87  const word y[], size_t y_size);
88 
89 /**
90 * Two operand subtraction, x = y - x; assumes y >= x
91 */
92 void bigint_sub2_rev(word x[], const word y[], size_t y_size);
93 
94 /**
95 * Three operand subtraction
96 */
97 word bigint_sub3(word z[],
98  const word x[], size_t x_size,
99  const word y[], size_t y_size);
100 
101 /*
102 * Shift Operations
103 */
104 void bigint_shl1(word x[], size_t x_size,
105  size_t word_shift, size_t bit_shift);
106 
107 void bigint_shr1(word x[], size_t x_size,
108  size_t word_shift, size_t bit_shift);
109 
110 void bigint_shl2(word y[], const word x[], size_t x_size,
111  size_t word_shift, size_t bit_shift);
112 
113 void bigint_shr2(word y[], const word x[], size_t x_size,
114  size_t word_shift, size_t bit_shift);
115 
116 /*
117 * Linear Multiply
118 */
119 void bigint_linmul2(word x[], size_t x_size, word y);
120 void bigint_linmul3(word z[], const word x[], size_t x_size, word y);
121 
122 /**
123 * Montgomery Reduction
124 * @param z integer to reduce, of size exactly 2*(p_size+1).
125  Output is in the first p_size+1 words, higher
126  words are set to zero.
127 * @param p modulus
128 * @param p_size size of p
129 * @param p_dash Montgomery value
130 * @param workspace array of at least 2*(p_size+1) words
131 */
132 void bigint_monty_redc(word z[],
133  const word p[], size_t p_size,
134  word p_dash,
135  word workspace[]);
136 
137 /*
138 * Montgomery Multiplication
139 */
140 void bigint_monty_mul(BigInt& z, const BigInt& x, const BigInt& y,
141  const word p[], size_t p_size, word p_dash,
142  word workspace[]);
143 
144 /*
145 * Montgomery Squaring
146 */
147 void bigint_monty_sqr(BigInt& z, const BigInt& x,
148  const word p[], size_t p_size, word p_dash,
149  word workspace[]);
150 
151 /**
152 * Compare x and y
153 */
154 int32_t bigint_cmp(const word x[], size_t x_size,
155  const word y[], size_t y_size);
156 
157 /**
158 * Compute ((n1<<bits) + n0) / d
159 */
160 word bigint_divop(word n1, word n0, word d);
161 
162 /**
163 * Compute ((n1<<bits) + n0) % d
164 */
165 word bigint_modop(word n1, word n0, word d);
166 
167 /*
168 * Comba Multiplication / Squaring
169 */
170 void bigint_comba_mul4(word z[8], const word x[4], const word y[4]);
171 void bigint_comba_mul6(word z[12], const word x[6], const word y[6]);
172 void bigint_comba_mul8(word z[16], const word x[8], const word y[8]);
173 void bigint_comba_mul9(word z[18], const word x[9], const word y[9]);
174 void bigint_comba_mul16(word z[32], const word x[16], const word y[16]);
175 
176 void bigint_comba_sqr4(word out[8], const word in[4]);
177 void bigint_comba_sqr6(word out[12], const word in[6]);
178 void bigint_comba_sqr8(word out[16], const word in[8]);
179 void bigint_comba_sqr9(word out[18], const word in[9]);
180 void bigint_comba_sqr16(word out[32], const word in[16]);
181 
182 /*
183 * High Level Multiplication/Squaring Interfaces
184 */
185 void bigint_mul(BigInt& z, const BigInt& x, const BigInt& y, word workspace[]);
186 
187 void bigint_mul(word z[], size_t z_size,
188  const word x[], size_t x_size, size_t x_sw,
189  const word y[], size_t y_size, size_t y_sw,
190  word workspace[]);
191 
192 void bigint_sqr(word z[], size_t z_size, word workspace[],
193  const word x[], size_t x_size, size_t x_sw);
194 
195 }
196 
197 #endif
void bigint_shr1(word x[], size_t x_size, size_t word_shift, size_t bit_shift)
Definition: mp_core.cpp:281
void bigint_sub2_rev(word x[], const word y[], size_t y_size)
Definition: mp_core.cpp:180
void bigint_shr2(word y[], const word x[], size_t x_size, size_t word_shift, size_t bit_shift)
Definition: mp_core.cpp:356
int32_t bigint_cmp(const word x[], size_t x_size, const word y[], size_t y_size)
Definition: mp_core.cpp:378
void bigint_cnd_abs(word cnd, word x[], size_t size)
Definition: mp_core.cpp:75
word bigint_sub2(word x[], size_t x_size, const word y[], size_t y_size)
Definition: mp_core.cpp:157
void bigint_linmul2(word x[], size_t x_size, word y)
Definition: mp_core.cpp:222
word bigint_add3_nc(word z[], const word x[], size_t x_size, const word y[], size_t y_size)
Definition: mp_core.cpp:113
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:198
word bigint_divop(word n1, word n0, word d)
Definition: mp_core.cpp:404
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_linmul3(word z[], const word x[], size_t x_size, word y)
Definition: mp_core.cpp:240
void bigint_comba_sqr16(word z[32], const word x[16])
Definition: mp_comba.cpp:598
void bigint_monty_sqr(BigInt &z, const BigInt &x, const word p[], size_t p_size, word p_dash, word workspace[])
Definition: mp_monty.cpp:108
word bigint_cnd_sub(word cnd, word x[], const word y[], size_t size)
Definition: mp_core.cpp:61
void bigint_sqr(word z[], size_t z_size, word workspace[], const word x[], size_t x_size, size_t x_sw)
Definition: mp_karat.cpp:321
void bigint_monty_mul(BigInt &z, const BigInt &x, const BigInt &y, const word p[], size_t p_size, word p_dash, word workspace[])
Definition: mp_monty.cpp:97
word bigint_add2_nc(word x[], size_t x_size, const word y[], size_t y_size)
Definition: mp_core.cpp:90
Definition: alg_id.cpp:13
void bigint_comba_sqr9(word z[18], const word x[9])
Definition: mp_comba.cpp:386
void bigint_shl2(word y[], const word x[], size_t x_size, size_t word_shift, size_t bit_shift)
Definition: mp_core.cpp:336
void bigint_shl1(word x[], size_t x_size, size_t word_shift, size_t bit_shift)
Definition: mp_core.cpp:258
void bigint_comba_mul8(word z[16], const word x[8], const word y[8])
Definition: mp_comba.cpp:283
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:138
void bigint_comba_mul16(word z[32], const word x[16], const word y[16])
Definition: mp_comba.cpp:805
void bigint_mul(BigInt &z, const BigInt &x, const BigInt &y, word workspace[])
Definition: mp_karat.cpp:253
void bigint_monty_redc(word z[], const word p[], size_t p_size, word p_dash, word workspace[])
Definition: mp_monty.cpp:22
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
void bigint_add3(word z[], const word x[], size_t x_size, const word y[], size_t y_size)
Definition: mp_core.cpp:147
word bigint_modop(word n1, word n0, word d)
Definition: mp_core.cpp:437
const size_t MP_WORD_BITS
Definition: mp_core.h:22