22 GFP2Element(
const byte *encodedElement,
unsigned int size)
23 : c1(encodedElement, size/2), c2(encodedElement+size/2, size/2) {}
25 void Encode(
byte *encodedElement,
unsigned int size)
27 c1.
Encode(encodedElement, size/2);
28 c2.
Encode(encodedElement+size/2, size/2);
56 throw InvalidArgument(
"GFP2_ONB: modulus must be equivalent to 2 mod 3");
59 const Integer& GetModulus()
const {
return modp.GetModulus();}
63 t = modp.Inverse(modp.ConvertIn(a));
68 {
return GFP2Element(modp.ConvertIn(a.c1), modp.ConvertIn(a.c2));}
71 {
return GFP2Element(modp.ConvertOut(a.c1), modp.ConvertOut(a.c2));}
75 return modp.Equal(a.c1, b.c1) && modp.Equal(a.c2, b.c2);
78 const Element& Identity()
const
80 return GFP2Element::Zero();
85 result.c1 = modp.Add(a.c1, b.c1);
86 result.c2 = modp.Add(a.c2, b.c2);
92 result.c1 = modp.Inverse(a.c1);
93 result.c2 = modp.Inverse(a.c2);
97 const Element& Double(
const Element &a)
const
99 result.c1 = modp.Double(a.c1);
100 result.c2 = modp.Double(a.c2);
104 const Element& Subtract(
const Element &a,
const Element &b)
const
106 result.c1 = modp.Subtract(a.c1, b.c1);
107 result.c2 = modp.Subtract(a.c2, b.c2);
111 Element& Accumulate(Element &a,
const Element &b)
const
113 modp.Accumulate(a.c1, b.c1);
114 modp.Accumulate(a.c2, b.c2);
118 Element& Reduce(Element &a,
const Element &b)
const
120 modp.Reduce(a.c1, b.c1);
121 modp.Reduce(a.c2, b.c2);
127 return a.c1.NotZero() || a.c2.NotZero();
130 const Element& MultiplicativeIdentity()
const
132 result.c1 = result.c2 = modp.Inverse(modp.MultiplicativeIdentity());
138 t = modp.Add(a.c1, a.c2);
139 t = modp.Multiply(t, modp.Add(b.c1, b.c2));
140 result.c1 = modp.Multiply(a.c1, b.c1);
141 result.c2 = modp.Multiply(a.c2, b.c2);
142 result.c1.
swap(result.c2);
143 modp.Reduce(t, result.c1);
144 modp.Reduce(t, result.c2);
145 modp.Reduce(result.c1, t);
146 modp.Reduce(result.c2, t);
152 return result = Exponentiate(a, modp.GetModulus()-2);
155 const Element&
Square(
const Element &a)
const
157 const Integer &ac1 = (&a == &result) ? (t = a.c1) : a.c1;
158 result.c1 = modp.Multiply(modp.Subtract(modp.Subtract(a.c2, a.c1), a.c1), a.c2);
159 result.c2 = modp.Multiply(modp.Subtract(modp.Subtract(ac1, a.c2), a.c2), ac1);
163 Element Exponentiate(
const Element &a,
const Integer &e)
const
167 Element b = PthPower(a);
171 const Element & PthPower(
const Element &a)
const
174 result.c1.
swap(result.c2);
178 void RaiseToPthPower(Element &a)
const
184 const Element & SpecialOperation1(
const Element &a)
const
186 assert(&a != &result);
188 modp.Reduce(result.c1, a.c2);
189 modp.Reduce(result.c1, a.c2);
190 modp.Reduce(result.c2, a.c1);
191 modp.Reduce(result.c2, a.c1);
196 const Element & SpecialOperation2(
const Element &x,
const Element &y,
const Element &z)
const
198 assert(&x != &result && &y != &result && &z != &result);
199 t = modp.Add(x.c2, y.c2);
200 result.c1 = modp.Multiply(z.c1, modp.Subtract(y.c1, t));
201 modp.Accumulate(result.c1, modp.Multiply(z.c2, modp.Subtract(t, x.c1)));
202 t = modp.Add(x.c1, y.c1);
203 result.c2 = modp.Multiply(z.c2, modp.Subtract(y.c2, t));
204 modp.Accumulate(result.c2, modp.Multiply(z.c1, modp.Subtract(t, x.c2)));