/src/botan/build/include/botan/internal/divide.h
Line | Count | Source |
1 | | /* |
2 | | * Division |
3 | | * (C) 1999-2007 Jack Lloyd |
4 | | * |
5 | | * Botan is released under the Simplified BSD License (see license.txt) |
6 | | */ |
7 | | |
8 | | #ifndef BOTAN_DIVISON_ALGORITHM_H_ |
9 | | #define BOTAN_DIVISON_ALGORITHM_H_ |
10 | | |
11 | | #include <botan/bigint.h> |
12 | | |
13 | | namespace Botan { |
14 | | |
15 | | /** |
16 | | * BigInt Division |
17 | | * @param x an integer |
18 | | * @param y a non-zero integer |
19 | | * @param q will be set to x / y |
20 | | * @param r will be set to x % y |
21 | | */ |
22 | | BOTAN_TEST_API |
23 | | void vartime_divide(const BigInt& x, |
24 | | const BigInt& y, |
25 | | BigInt& q, |
26 | | BigInt& r); |
27 | | |
28 | | /** |
29 | | * BigInt division, const time variant |
30 | | * |
31 | | * This runs with control flow independent of the values of x/y. |
32 | | * Warning: the loop bounds still leak the sizes of x and y. |
33 | | * |
34 | | * @param x an integer |
35 | | * @param y a non-zero integer |
36 | | * @param q will be set to x / y |
37 | | * @param r will be set to x % y |
38 | | */ |
39 | | BOTAN_TEST_API |
40 | | void ct_divide(const BigInt& x, |
41 | | const BigInt& y, |
42 | | BigInt& q, |
43 | | BigInt& r); |
44 | | |
45 | | inline void divide(const BigInt& x, |
46 | | const BigInt& y, |
47 | | BigInt& q, |
48 | | BigInt& r) |
49 | 688 | { |
50 | 688 | ct_divide(x, y, q, r); |
51 | 688 | } |
52 | | |
53 | | /** |
54 | | * BigInt division, const time variant |
55 | | * |
56 | | * This runs with control flow independent of the values of x/y. |
57 | | * Warning: the loop bounds still leak the sizes of x and y. |
58 | | * |
59 | | * @param x an integer |
60 | | * @param y a non-zero integer |
61 | | * @return x/y with remainder discarded |
62 | | */ |
63 | | inline BigInt ct_divide(const BigInt& x, const BigInt& y) |
64 | 110k | { |
65 | 110k | BigInt q, r; |
66 | 110k | ct_divide(x, y, q, r); |
67 | 110k | return q; |
68 | 110k | } |
69 | | |
70 | | /** |
71 | | * BigInt division, const time variant |
72 | | * |
73 | | * This runs with control flow independent of the values of x/y. |
74 | | * Warning: the loop bounds still leak the sizes of x and y. |
75 | | * |
76 | | * @param x an integer |
77 | | * @param y a non-zero integer |
78 | | * @param q will be set to x / y |
79 | | * @param r will be set to x % y |
80 | | */ |
81 | | BOTAN_TEST_API |
82 | | void ct_divide_u8(const BigInt& x, |
83 | | uint8_t y, |
84 | | BigInt& q, |
85 | | uint8_t& r); |
86 | | |
87 | | /** |
88 | | * BigInt modulo, const time variant |
89 | | * |
90 | | * Using this function is (slightly) cheaper than calling ct_divide and |
91 | | * using only the remainder. |
92 | | * |
93 | | * @param x a non-negative integer |
94 | | * @param modulo a positive integer |
95 | | * @return result x % modulo |
96 | | */ |
97 | | BOTAN_TEST_API |
98 | | BigInt ct_modulo(const BigInt& x, |
99 | | const BigInt& modulo); |
100 | | |
101 | | } |
102 | | |
103 | | #endif |