Botan 3.3.0
Crypto and TLS for C&
monty_exp.h
Go to the documentation of this file.
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 <botan/bigint.h>
11#include <memory>
12
13namespace Botan {
14
15class Modular_Reducer;
16
17class Montgomery_Params;
18
19class Montgomery_Exponentation_State;
20
21/*
22* Precompute for calculating values g^x mod p
23*/
24std::shared_ptr<const Montgomery_Exponentation_State> monty_precompute(
25 const 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*/
33BigInt monty_execute(const Montgomery_Exponentation_State& precomputed_state, const BigInt& k, size_t max_k_bits);
34
35/*
36* Return g^k mod p taking variable time depending on k
37* @warning only use this if k is public
38*/
39BigInt monty_execute_vartime(const Montgomery_Exponentation_State& precomputed_state, const BigInt& k);
40
41inline BigInt monty_exp(const std::shared_ptr<const Montgomery_Params>& params_p,
42 const BigInt& g,
43 const BigInt& k,
44 size_t max_k_bits) {
45 auto precomputed = monty_precompute(params_p, g, 4, true);
46 return monty_execute(*precomputed, k, max_k_bits);
47}
48
49inline BigInt monty_exp_vartime(const std::shared_ptr<const Montgomery_Params>& params_p,
50 const BigInt& g,
51 const BigInt& k) {
52 auto precomputed = monty_precompute(params_p, g, 4, false);
53 return monty_execute_vartime(*precomputed, k);
54}
55
56/**
57* Return (x^z1 * y^z2) % p
58*/
59BigInt monty_multi_exp(const std::shared_ptr<const Montgomery_Params>& params_p,
60 const BigInt& x,
61 const BigInt& z1,
62 const BigInt& y,
63 const BigInt& z2);
64
65} // namespace Botan
66
67#endif
BigInt monty_exp(const std::shared_ptr< const Montgomery_Params > &params_p, const BigInt &g, const BigInt &k, size_t max_k_bits)
Definition monty_exp.h:41
BigInt monty_multi_exp(const std::shared_ptr< const Montgomery_Params > &params_p, const BigInt &x_bn, const BigInt &z1, const BigInt &y_bn, const BigInt &z2)
BigInt monty_exp_vartime(const std::shared_ptr< const Montgomery_Params > &params_p, const BigInt &g, const BigInt &k)
Definition monty_exp.h:49
BigInt monty_execute_vartime(const Montgomery_Exponentation_State &precomputed_state, const BigInt &k)
BigInt monty_execute(const Montgomery_Exponentation_State &precomputed_state, const BigInt &k, size_t max_k_bits)
std::shared_ptr< const Montgomery_Exponentation_State > monty_precompute(const std::shared_ptr< const Montgomery_Params > &params, const BigInt &g, size_t window_bits, bool const_time)