Botan 3.14.1
Crypto and TLS for C&
monty_exp.h
Go to the documentation of this file.
1/*
2* (C) 2018,2025 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/internal/monty.h>
11#include <memory>
12
13namespace Botan {
14
15class BigInt;
16
17/**
18* Precomputed state for computing g^x mod p
19*/
21 public:
23
26
29
30 /**
31 * Return g^k mod p, in time independent of k (assuming k is at most max_k_bits long)
32 */
33 virtual Montgomery_Int exponentiation(const BigInt& k, size_t max_k_bits) const = 0;
34
35 /**
36 * Return g^k mod p in variable time
37 */
38 virtual Montgomery_Int exponentiation_vartime(const BigInt& k) const = 0;
39
40 protected:
42};
43
44/*
45* Precompute for calculating values g^x mod p
46*/
47BOTAN_TEST_API std::shared_ptr<const Montgomery_Exponentiation_State> monty_precompute(
48 const Montgomery_Params& params_p, const BigInt& g, size_t window_bits);
49
50/*
51* Precompute for calculating values g^x mod p
52*/
53BOTAN_TEST_API std::shared_ptr<const Montgomery_Exponentiation_State> monty_precompute(const Montgomery_Int& g,
54 size_t window_bits);
55
56/*
57* Return g^k mod p
58*/
60 const BigInt& k,
61 size_t max_k_bits);
62
63/*
64* Return g^k mod p taking variable time depending on k
65* @warning only use this if k is public
66*/
68 const BigInt& k);
69
71 const BigInt& g,
72 const BigInt& k,
73 size_t max_k_bits) {
74 const auto precomputed = monty_precompute(params_p, g, 4);
75 return monty_execute(*precomputed, k, max_k_bits);
76}
77
78inline Montgomery_Int monty_exp_vartime(const Montgomery_Params& params_p, const BigInt& g, const BigInt& k) {
79 const auto precomputed = monty_precompute(params_p, g, 4);
80 return monty_execute_vartime(*precomputed, k);
81}
82
83/**
84* Return (x^z1 * y^z2) % p
85*/
86Montgomery_Int monty_multi_exp(
87 const Montgomery_Params& params_p, const BigInt& x, const BigInt& z1, const BigInt& y, const BigInt& z2);
88
89} // namespace Botan
90
91#endif
#define BOTAN_TEST_API
Definition api.h:41
Montgomery_Exponentiation_State & operator=(Montgomery_Exponentiation_State &&other)=delete
Montgomery_Exponentiation_State & operator=(const Montgomery_Exponentiation_State &other)=delete
Montgomery_Exponentiation_State(Montgomery_Exponentiation_State &&other)=delete
virtual Montgomery_Int exponentiation(const BigInt &k, size_t max_k_bits) const =0
virtual Montgomery_Int exponentiation_vartime(const BigInt &k) const =0
virtual ~Montgomery_Exponentiation_State()=default
Montgomery_Exponentiation_State(const Montgomery_Exponentiation_State &other)=delete
Montgomery_Int monty_exp_vartime(const Montgomery_Params &params_p, const BigInt &g, const BigInt &k)
Definition monty_exp.h:78
std::shared_ptr< const Montgomery_Exponentiation_State > monty_precompute(const Montgomery_Int &g, size_t window_bits)
Montgomery_Int monty_multi_exp(const Montgomery_Params &params_p, const BigInt &x_bn, const BigInt &z1, const BigInt &y_bn, const BigInt &z2)
Montgomery_Int monty_execute_vartime(const Montgomery_Exponentiation_State &precomputed_state, const BigInt &k)
Montgomery_Int monty_exp(const Montgomery_Params &params_p, const BigInt &g, const BigInt &k, size_t max_k_bits)
Definition monty_exp.h:70
Montgomery_Int monty_execute(const Montgomery_Exponentiation_State &precomputed_state, const BigInt &k, size_t max_k_bits)