Coverage Report

Created: 2021-10-13 08:49

/src/botan/build/include/botan/internal/monty_exp.h
Line
Count
Source
1
/*
2
* (C) 2018 Jack Lloyd
3
*
4
* Botan is released under the Simplified BSD License (see license.txt)
5
*/
6
7
#ifndef BOTAN_MONTY_EXP_H_
8
#define BOTAN_MONTY_EXP_H_
9
10
#include <memory>
11
#include <botan/bigint.h>
12
13
namespace Botan {
14
15
class Modular_Reducer;
16
17
class Montgomery_Params;
18
19
class Montgomery_Exponentation_State;
20
21
/*
22
* Precompute for calculating values g^x mod p
23
*/
24
std::shared_ptr<const Montgomery_Exponentation_State>
25
monty_precompute(std::shared_ptr<const Montgomery_Params> params_p,
26
                 const BigInt& g,
27
                 size_t window_bits,
28
                 bool const_time = true);
29
30
/*
31
* Return g^k mod p
32
*/
33
BigInt monty_execute(const Montgomery_Exponentation_State& precomputed_state,
34
                     const BigInt& k, size_t max_k_bits);
35
36
/*
37
* Return g^k mod p taking variable time depending on k
38
* @warning only use this if k is public
39
*/
40
BigInt monty_execute_vartime(const Montgomery_Exponentation_State& precomputed_state,
41
                             const BigInt& k);
42
43
inline
44
BigInt monty_exp(std::shared_ptr<const Montgomery_Params> params_p,
45
                 const BigInt& g, const BigInt& k, size_t max_k_bits)
46
458
   {
47
458
   auto precomputed = monty_precompute(params_p, g, 4, true);
48
458
   return monty_execute(*precomputed, k, max_k_bits);
49
458
   }
50
51
inline
52
BigInt monty_exp_vartime(std::shared_ptr<const Montgomery_Params> params_p,
53
                         const BigInt& g, const BigInt& k)
54
65.8k
   {
55
65.8k
   auto precomputed = monty_precompute(params_p, g, 4, false);
56
65.8k
   return monty_execute_vartime(*precomputed, k);
57
65.8k
   }
58
59
/**
60
* Return (x^z1 * y^z2) % p
61
*/
62
BigInt monty_multi_exp(std::shared_ptr<const Montgomery_Params> params_p,
63
                       const BigInt& x,
64
                       const BigInt& z1,
65
                       const BigInt& y,
66
                       const BigInt& z2);
67
68
}
69
70
#endif