Botan 3.10.0
Crypto and TLS for C&
divide.h
Go to the documentation of this file.
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_BIGINT_DIVIDE_H_
9#define BOTAN_BIGINT_DIVIDE_H_
10
11#include <botan/bigint.h>
12
13namespace 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*/
23void vartime_divide(const BigInt& x, const BigInt& y, BigInt& q, BigInt& r);
24
25/**
26* BigInt division, const time variant
27*
28* This runs with control flow independent of the values of x/y.
29* Warning: the loop bounds still leak the sizes of x and y.
30*
31* @param x an integer
32* @param y a non-zero integer
33* @param q will be set to x / y
34* @param r will be set to x % y
35*/
37void ct_divide(const BigInt& x, const BigInt& y, BigInt& q, BigInt& r);
38
39/**
40* BigInt division, const time variant, 2^k variant
41*
42* This runs with control flow independent of the value of y.
43* This function leaks the value of k and the length of y.
44* If k < bits(y) this returns zero
45*
46* @param k an integer
47* @param y a positive integer
48* @return q equal to 2**k / y
49*/
51BigInt ct_divide_pow2k(size_t k, const BigInt& y);
52
53/**
54* BigInt division, variable time, 2^k variant
55*
56* This is identical to ct_divide_pow2k in functionality,
57* but leaks both k and y to side channels, so it should only
58* be used with public inputs.
59*
60* @param k an integer
61* @param y a positive integer
62* @return q equal to 2**k / y
63*/
65BigInt vartime_divide_pow2k(size_t k, const BigInt& y);
66
67/**
68* BigInt division, const time variant
69*
70* This runs with control flow independent of the values of x/y.
71* Warning: the loop bounds still leak the sizes of x and y.
72*
73* @param x an integer
74* @param y a non-zero integer
75* @return x/y with remainder discarded
76*/
77inline BigInt ct_divide(const BigInt& x, const BigInt& y) {
78 BigInt q;
79 BigInt r;
80 ct_divide(x, y, q, r);
81 return q;
82}
83
84/**
85* Constant time division
86*
87* This runs with control flow independent of the values of x/y.
88* Warning: the loop bounds still leaks the size of x.
89*
90* @param x an integer
91* @param y a non-zero integer
92* @param q will be set to x / y
93* @param r will be set to x % y
94*/
96void ct_divide_word(const BigInt& x, word y, BigInt& q, word& r);
97
98/**
99* Constant time division
100*
101* This runs with control flow independent of the values of x/y.
102* Warning: the loop bounds still leaks the size of x.
103*
104* @param x an integer
105* @param y a non-zero word
106* @return quotient floor(x / y)
107*/
108BigInt ct_divide_word(const BigInt& x, word y);
109
110/**
111* BigInt word modulo, const time variant
112*
113* This runs with control flow independent of the values of x/y.
114* Warning: the loop bounds still leaks the size of x.
115*
116* @param x a positive integer
117* @param y a non-zero word
118* @return r the remainder of x divided by y
119*/
121word ct_mod_word(const BigInt& x, word y);
122
123/**
124* BigInt modulo, const time variant
125*
126* Using this function is (slightly) cheaper than calling ct_divide and
127* using only the remainder.
128*
129* @param x a non-negative integer
130* @param modulo a positive integer
131* @return result x % modulo
132*/
134BigInt ct_modulo(const BigInt& x, const BigInt& modulo);
135
136} // namespace Botan
137
138#endif
#define BOTAN_TEST_API
Definition api.h:41
void vartime_divide(const BigInt &x, const BigInt &y_arg, BigInt &q_out, BigInt &r_out)
Definition divide.cpp:324
word ct_mod_word(const BigInt &x, word y)
Definition divide.cpp:167
BigInt ct_modulo(const BigInt &x, const BigInt &y)
Definition divide.cpp:191
BigInt vartime_divide_pow2k(size_t k, const BigInt &y_arg)
Definition divide.cpp:224
void ct_divide(const BigInt &x, const BigInt &y, BigInt &q_out, BigInt &r_out)
Definition divide.cpp:50
void ct_divide_word(const BigInt &x, word y, BigInt &q_out, word &r_out)
Definition divide.cpp:122
BigInt ct_divide_pow2k(size_t k, const BigInt &y)
Definition divide.cpp:82
std::conditional_t< HasNative64BitRegisters, std::uint64_t, uint32_t > word
Definition types.h:119