-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBigNum.h
More file actions
108 lines (87 loc) · 4 KB
/
BigNum.h
File metadata and controls
108 lines (87 loc) · 4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
// Copyright (C) 2019 by Jakub Wojciech
// This file is part of Lelo Remote Music Player.
// Lelo Remote Music Player is free software: you can redistribute it
// and/or modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation, either version 3 of
// the License, or (at your option) any later version.
// Lelo Remote Music Player is distributed in the hope that it will be
// useful, but WITHOUT ANY WARRANTY; without even the implied warranty
// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Lelo Remote Music Player. If not, see
// <https://www.gnu.org/licenses/>.
#ifndef LRM_BIGNUM_H_
#define LRM_BIGNUM_H_
#include <string_view>
#include <vector>
#include <openssl/bn.h>
#include "config.h"
namespace lrm::crypto {
class BigNum {
public:
BigNum() noexcept;
explicit BigNum(const BIGNUM* bignum) noexcept;
explicit BigNum(std::string_view dec_num_str) noexcept;
BigNum(BN_ULONG num) noexcept;
explicit BigNum(const std::vector<unsigned char>& bytes) noexcept;
explicit BigNum(const Bytes& bytes) noexcept;
explicit BigNum(const unsigned char* bytes, size_t size) noexcept;
BigNum(const BigNum& other) noexcept;
BigNum(BigNum&& other) noexcept;
~BigNum();
BigNum& operator=(const BigNum& other) noexcept;
BigNum& operator=(BigNum&& other) noexcept;
BigNum& operator+=(const BigNum& rhs) noexcept;
friend BigNum operator+(BigNum lhs, const BigNum& rhs) noexcept;
BigNum& operator-=(const BigNum& rhs) noexcept;
friend BigNum operator-(BigNum lhs, const BigNum& rhs) noexcept;
BigNum& operator*=(const BigNum& rhs) noexcept;
friend BigNum operator*(BigNum lhs, const BigNum& rhs) noexcept;
BigNum& operator/=(const BigNum& rhs) noexcept;
friend BigNum operator/(BigNum lhs, const BigNum& rhs) noexcept;
BigNum& operator%=(const BigNum& rhs) noexcept;
friend BigNum operator%(BigNum lhs, const BigNum& rhs) noexcept;
BigNum& operator^=(const BigNum& rhs) noexcept;
friend BigNum operator^(BigNum lhs, const BigNum& rhs) noexcept;
friend bool operator==(const BigNum& lhs, const BigNum& rhs) noexcept;
friend bool operator!=(const BigNum& lhs, const BigNum& rhs) noexcept;
friend bool operator>(const BigNum& lhs, const BigNum& rhs) noexcept;
friend bool operator>=(const BigNum& lhs, const BigNum& rhs) noexcept;
friend bool operator<(const BigNum& lhs, const BigNum& rhs) noexcept;
friend bool operator<=(const BigNum& lhs, const BigNum& rhs) noexcept;
BigNum ModAdd(const BigNum& other, const BigNum& mod) const noexcept;
BigNum ModSub(const BigNum& other, const BigNum& mod) const noexcept;
BigNum ModMul(const BigNum& other, const BigNum& mod) const noexcept;
BigNum ModSqr(const BigNum& mod) const noexcept;
BigNum ModExp(const BigNum& power, const BigNum& mod) const;
inline const BIGNUM* get() const noexcept {
return bignum_;
}
bool IsPrime() const noexcept;
bool IsOdd() const noexcept;
friend std::ostream& operator<<(std::ostream& stream, const BigNum& num);
std::string to_string() const;
operator std::string() const;
Bytes to_bytes() const;
private:
static thread_local struct Context {
Context() : ctx{BN_CTX_new()} {}
~Context() { BN_CTX_free(ctx); }
BN_CTX* ctx;
} ctx_;
BIGNUM* bignum_;
};
BigNum PrimeGenerate(int bits, bool safe,
const BigNum& add, const BigNum& rem);
BigNum PrimeGenerate(int bits, bool safe = false);
/// Generate a random number from range [0; \e ex_upper_bound)
/// \param ex_upper_bound Upper bound, excluded from the set.
BigNum RandomInRange(const BigNum& ex_upper_bound);
/// Generate a random number from range [\e in_lower_bound; \e in_upper_bound]
/// \param in_lower_bound Lower bound, included in the set.
/// \param in_upper_bound Upper bound, included in the set.
BigNum RandomInRange(const BigNum& in_lower_bound,
const BigNum& in_upper_bound);
}
#endif // LRM_BIGNUM_H_