10 NAMESPACE_BEGIN(CryptoPP)
17 GFP2Element(
const Integer &c1,
const Integer &c2) : c1(c1), c2(c2) {}
18 GFP2Element(
const byte *encodedElement,
unsigned int size)
19 : c1(encodedElement, size/2), c2(encodedElement+size/2, size/2) {}
21 void Encode(byte *encodedElement,
unsigned int size)
23 c1.
Encode(encodedElement, size/2);
24 c2.
Encode(encodedElement+size/2, size/2);
27 bool operator==(
const GFP2Element &rhs)
const {
return c1 == rhs.c1 && c2 == rhs.c2;}
28 bool operator!=(
const GFP2Element &rhs)
const {
return !operator==(rhs);}
30 void swap(GFP2Element &a)
36 static const GFP2Element & Zero();
51 throw InvalidArgument(
"GFP2_ONB: modulus must be equivalent to 2 mod 3");
54 const Integer& GetModulus()
const {
return modp.GetModulus();}
58 t = modp.Inverse(modp.ConvertIn(a));
63 {
return GFP2Element(modp.ConvertIn(a.c1), modp.ConvertIn(a.c2));}
66 {
return GFP2Element(modp.ConvertOut(a.c1), modp.ConvertOut(a.c2));}
70 return modp.Equal(a.c1, b.c1) && modp.Equal(a.c2, b.c2);
75 return GFP2Element::Zero();
80 result.c1 = modp.Add(a.c1, b.c1);
81 result.c2 = modp.Add(a.c2, b.c2);
87 result.c1 = modp.Inverse(a.c1);
88 result.c2 = modp.Inverse(a.c2);
94 result.c1 = modp.Double(a.c1);
95 result.c2 = modp.Double(a.c2);
101 result.c1 = modp.Subtract(a.c1, b.c1);
102 result.c2 = modp.Subtract(a.c2, b.c2);
108 modp.Accumulate(a.c1, b.c1);
109 modp.Accumulate(a.c2, b.c2);
115 modp.Reduce(a.c1, b.c1);
116 modp.Reduce(a.c2, b.c2);
120 bool IsUnit(
const Element &a)
const
122 return a.c1.NotZero() || a.c2.NotZero();
125 const Element& MultiplicativeIdentity()
const
127 result.c1 = result.c2 = modp.Inverse(modp.MultiplicativeIdentity());
133 t = modp.Add(a.c1, a.c2);
134 t = modp.Multiply(t, modp.Add(b.c1, b.c2));
135 result.c1 = modp.Multiply(a.c1, b.c1);
136 result.c2 = modp.Multiply(a.c2, b.c2);
137 result.c1.swap(result.c2);
138 modp.Reduce(t, result.c1);
139 modp.Reduce(t, result.c2);
140 modp.Reduce(result.c1, t);
141 modp.Reduce(result.c2, t);
147 return result = Exponentiate(a, modp.GetModulus()-2);
152 const Integer &ac1 = (&a == &result) ? (t = a.c1) : a.c1;
153 result.c1 = modp.Multiply(modp.Subtract(modp.Subtract(a.c2, a.c1), a.c1), a.c2);
154 result.c2 = modp.Multiply(modp.Subtract(modp.Subtract(ac1, a.c2), a.c2), ac1);
169 result.c1.swap(result.c2);
173 void RaiseToPthPower(
Element &a)
const
181 assert(&a != &result);
183 modp.Reduce(result.c1, a.c2);
184 modp.Reduce(result.c1, a.c2);
185 modp.Reduce(result.c2, a.c1);
186 modp.Reduce(result.c2, a.c1);
193 assert(&x != &result && &y != &result && &z != &result);
194 t = modp.Add(x.c2, y.c2);
195 result.c1 = modp.Multiply(z.c1, modp.Subtract(y.c1, t));
196 modp.Accumulate(result.c1, modp.Multiply(z.c2, modp.Subtract(t, x.c1)));
197 t = modp.Add(x.c1, y.c1);
198 result.c2 = modp.Multiply(z.c2, modp.Subtract(y.c2, t));
199 modp.Accumulate(result.c2, modp.Multiply(z.c1, modp.Subtract(t, x.c2)));
exception thrown when an invalid argument is detected
static void Divide(Integer &r, Integer &q, const Integer &a, const Integer &d)
calculate r and q such that (a == d*q + r) && (0 <= r < abs(d))
interface for random number generators
multiple precision integer and basic arithmetics
void Encode(byte *output, size_t outputLen, Signedness=UNSIGNED) const
encode in big-endian format
GF(p^2), optimal normal basis.