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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
|
/*
* Arithmetic for prime fields GF(p)
*
* (C) 2007 Martin Doering, Christoph Ludwig, Falko Strenzke
* 2009-2010 Jack Lloyd
*
* Distributed under the terms of the Botan license
*/
#ifndef BOTAN_GFP_ELEMENT_H__
#define BOTAN_GFP_ELEMENT_H__
#include <botan/bigint.h>
#include <iosfwd>
namespace Botan {
struct BOTAN_DLL Illegal_Transformation : public Exception
{
Illegal_Transformation(const std::string& err =
"Requested transformation is not possible") :
Exception(err) {}
};
/**
* This class represents one element in GF(p). Enables the convenient,
* transparent use of the montgomery multiplication.
*/
class BOTAN_DLL GFpElement
{
public:
/** construct an element of GF(p) with the given value.
* use_montg defaults to false and determines wether Montgomery
* multiplications will be use when applying operators *, *=
* @param p the prime number of the field
* @param value the element value
*/
GFpElement(const BigInt& p, const BigInt& value);
// GFpElement(const GFpElement& other) = default;
// const GFpElement& operator=(const GFpElement& other) = default;
/**
* += Operator
* @param rhs the GFpElement to add to the local value
* @result *this
*/
GFpElement& operator+=(const GFpElement& rhs);
/**
* -= Operator
* @param rhs the GFpElement to subtract from the local value
* @result *this
*/
GFpElement& operator-=(const GFpElement& rhs);
/**
* *= Operator
* @param rhs the GFpElement to multiply with the local value
* @result *this
*/
GFpElement& operator*=(const GFpElement& rhs);
/**
* /= Operator
* @param rhs the GFpElement to divide the local value by
* @result *this
*/
GFpElement& operator/=(const GFpElement& rhs);
/**
* *= Operator
* @param rhs the value to multiply with the local value
* @result *this
*/
GFpElement& operator*=(u32bit rhs);
/**
* Negate internal value(*this *= -1 )
* @return *this
*/
GFpElement& negate();
/**
* Assigns the inverse of *this to *this, i.e.
* *this = (*this)^(-1)
* @result *this
*/
GFpElement& inverse_in_place();
/**
* checks whether the value is zero (without provoking
* a backtransformation to the ordinary-residue)
* @result true, if the value is zero, false otherwise.
*/
bool is_zero() const;
/**
* return prime number of GF(p)
* @result a prime number
*/
const BigInt& get_p() const { return mod_p; }
/**
* Return the represented value in GF(p)
* @result The value in GF(p)
*/
const BigInt& get_value() const { return m_value; }
/**
* swaps the states of *this and other, does not throw!
* @param other The value to swap with
*/
void swap(GFpElement& other);
private:
BigInt mod_p; // modulus
BigInt m_value;
};
// relational operators
bool BOTAN_DLL operator==(const GFpElement& lhs, const GFpElement& rhs);
inline bool operator!=(const GFpElement& lhs, const GFpElement& rhs )
{
return !operator==(lhs, rhs);
}
// arithmetic operators
GFpElement BOTAN_DLL operator+(const GFpElement& lhs, const GFpElement& rhs);
GFpElement BOTAN_DLL operator-(const GFpElement& lhs, const GFpElement& rhs);
GFpElement BOTAN_DLL operator-(const GFpElement& lhs);
GFpElement BOTAN_DLL operator*(const GFpElement& lhs, const GFpElement& rhs);
GFpElement BOTAN_DLL operator/(const GFpElement& lhs, const GFpElement& rhs);
GFpElement BOTAN_DLL operator*(const GFpElement& lhs, u32bit rhs);
GFpElement BOTAN_DLL operator*(u32bit rhs, const GFpElement& lhs);
/**
* write a GFpElement to an output stream.
* @param output the output stream to write to
* @param elem the object to write
* @result the output stream
*/
BOTAN_DLL std::ostream& operator<<(std::ostream& output, const GFpElement& elem);
// return (*this)^(-1)
GFpElement BOTAN_DLL inverse(const GFpElement& elem);
// encoding and decoding
SecureVector<byte> BOTAN_DLL FE2OSP(const GFpElement& elem);
GFpElement BOTAN_DLL OS2FEP(MemoryRegion<byte> const& os, BigInt p);
inline void swap(GFpElement& x, GFpElement& y)
{
x.swap(y);
}
}
namespace std {
template<> inline
void swap<Botan::GFpElement>(Botan::GFpElement& x,
Botan::GFpElement& y)
{
x.swap(y);
}
}
#endif
|