Botan 3.0.0-alpha0
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 <memory>
11#include <botan/bigint.h>
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>
25monty_precompute(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,
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*/
40BigInt monty_execute_vartime(const Montgomery_Exponentation_State& precomputed_state,
41 const BigInt& k);
42
43inline
44BigInt monty_exp(std::shared_ptr<const Montgomery_Params> params_p,
45 const BigInt& g, const BigInt& k, size_t max_k_bits)
46 {
47 auto precomputed = monty_precompute(params_p, g, 4, true);
48 return monty_execute(*precomputed, k, max_k_bits);
49 }
50
51inline
52BigInt monty_exp_vartime(std::shared_ptr<const Montgomery_Params> params_p,
53 const BigInt& g, const BigInt& k)
54 {
55 auto precomputed = monty_precompute(params_p, g, 4, false);
56 return monty_execute_vartime(*precomputed, k);
57 }
58
59/**
60* Return (x^z1 * y^z2) % p
61*/
62BigInt monty_multi_exp(const 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
Definition: alg_id.cpp:13
BigInt monty_exp(std::shared_ptr< const Montgomery_Params > params_p, const BigInt &g, const BigInt &k, size_t max_k_bits)
Definition: monty_exp.h:44
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)
Definition: monty_exp.cpp:174
BigInt monty_exp_vartime(std::shared_ptr< const Montgomery_Params > params_p, const BigInt &g, const BigInt &k)
Definition: monty_exp.h:52
BigInt monty_execute_vartime(const Montgomery_Exponentation_State &precomputed_state, const BigInt &k)
Definition: monty_exp.cpp:168
BigInt monty_execute(const Montgomery_Exponentation_State &precomputed_state, const BigInt &k, size_t max_k_bits)
Definition: monty_exp.cpp:162
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)
Definition: monty_exp.cpp:154