aboutsummaryrefslogtreecommitdiffstats
path: root/src/math/gfpmath/gfp_element.cpp
blob: d49ca49896eab2d2af778c59a2512bc5a7485053 (plain)
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/*
* Arithmetic for prime fields GF(p)
*
* (C) 2007 Martin Doering, Christoph Ludwig, Falko Strenzke
*
* Distributed under the terms of the Botan license
*/

#include <botan/gfp_element.h>
#include <botan/numthry.h>
#include <botan/internal/def_powm.h>
#include <botan/internal/mp_asm.h>
#include <botan/internal/mp_asmi.h>
#include <ostream>
#include <assert.h>

namespace Botan {

GFpElement::GFpElement(const BigInt& p, const BigInt& value) :
   mod_p(p),
   m_value(value %p)
   {
   }

GFpElement& GFpElement::operator+=(const GFpElement& rhs)
   {
   BigInt workspace = m_value;
   workspace += rhs.m_value;
   if(workspace >= mod_p)
      workspace -= mod_p;

   m_value = workspace;
   assert(m_value < mod_p);
   assert(m_value >= 0);

   return *this;
   }

GFpElement& GFpElement::operator-=(const GFpElement& rhs)
   {
   BigInt workspace = m_value;

   workspace -= rhs.m_value;

   if(workspace.is_negative())
      workspace += mod_p;

   m_value = workspace;
   assert(m_value < mod_p);
   assert(m_value >= 0);
   return *this;
   }

GFpElement& GFpElement::operator*= (u32bit rhs)
   {
   BigInt workspace = m_value;
   workspace *= rhs;
   workspace %= mod_p;
   m_value = workspace;
   return *this;
   }

GFpElement& GFpElement::operator*=(const GFpElement& rhs)
   {
   BigInt workspace = m_value;
   workspace *= rhs.m_value;
   workspace %= mod_p;
   m_value = workspace;
   return *this;
   }

GFpElement& GFpElement::operator/=(const GFpElement& rhs)
   {
   GFpElement inv_rhs(rhs);
   inv_rhs.inverse_in_place();
   *this *= inv_rhs;
   return *this;
   }

bool GFpElement::is_zero()
   {
   return (m_value.is_zero());
   // this is correct because x_bar = x * r = x = 0 for x = 0
   }

GFpElement& GFpElement::inverse_in_place()
   {
   m_value = inverse_mod(m_value, mod_p);
   return *this;
   }

GFpElement& GFpElement::negate()
   {
   m_value = mod_p - m_value;
   assert(m_value <= mod_p);
   return *this;
   }

void GFpElement::swap(GFpElement& other)
   {
   std::swap(m_value, other.m_value);
   std::swap(mod_p, other.mod_p);
   }

std::ostream& operator<<(std::ostream& output, const GFpElement& elem)
   {
   return output << '(' << elem.get_value() << "," << elem.get_p() << ')';
   }

bool operator==(const GFpElement& lhs, const GFpElement& rhs)
   {
   return (lhs.get_p() == rhs.get_p() &&
           lhs.get_value() == rhs.get_value());
   }

GFpElement operator+(const GFpElement& lhs, const GFpElement& rhs)
   {
   // consider the case that lhs and rhs both use montgm:
   // then += returns an element which uses montgm.
   // thus the return value of op+ here will be an element
   // using montgm in this case
   // NOTE: the rhs might be transformed when using op+, the lhs never
   GFpElement result(lhs);
   result += rhs;
   return result;
   }

GFpElement operator-(const GFpElement& lhs, const GFpElement& rhs)
   {
   GFpElement result(lhs);
   result -= rhs;
   return result;
   // NOTE: the rhs might be transformed when using op-, the lhs never
   }

GFpElement operator-(const GFpElement& lhs)
   {
   return(GFpElement(lhs)).negate();
   }

GFpElement operator*(const GFpElement& lhs, const GFpElement& rhs)
   {
   // consider the case that lhs and rhs both use montgm:
   // then *= returns an element which uses montgm.
   // thus the return value of op* here will be an element
   // using montgm in this case
   GFpElement result(lhs);
   result *= rhs;
   return result;
   }

GFpElement operator*(const GFpElement& lhs, u32bit rhs)
   {
   GFpElement result(lhs);
   result *= rhs;
   return result;
   }

GFpElement operator*(u32bit lhs, const GFpElement& rhs)
   {
   return rhs*lhs;
   }

GFpElement operator/(const GFpElement& lhs, const GFpElement& rhs)
   {
   GFpElement result (lhs);
   result /= rhs;
   return result;
   }

SecureVector<byte> FE2OSP(const GFpElement& elem)
   {
   return BigInt::encode_1363(elem.get_value(), elem.get_p().bytes());
   }

GFpElement OS2FEP(MemoryRegion<byte> const& os, BigInt p)
   {
   return GFpElement(p, BigInt::decode(os.begin(), os.size()));
   }

GFpElement inverse(const GFpElement& elem)
   {
   return GFpElement(elem).inverse_in_place();
   }

}