5 #ifndef CRYPTOPP_IMPORTS 11 #include "algebra.cpp" 12 #include "eprecomp.cpp" 14 ANONYMOUS_NAMESPACE_BEGIN
18 #if defined(HAVE_GCC_INIT_PRIORITY) 19 const EC2N::Point g_identity __attribute__ ((init_priority (CRYPTOPP_INIT_PRIORITY + 50))) =
EC2N::Point();
20 #elif defined(HAVE_MSC_INIT_PRIORITY) 21 #pragma warning(disable: 4075) 22 #pragma init_seg(".CRT$XCU") 24 #pragma warning(default: 4075) 25 #elif defined(HAVE_XLC_INIT_PRIORITY) 30 ANONYMOUS_NAMESPACE_END
35 : m_field(BERDecodeGF2NP(bt))
38 m_field->BERDecodeElement(seq, m_a);
39 m_field->BERDecodeElement(seq, m_b);
41 if (!seq.EndReached())
52 m_field->DEREncode(bt);
54 m_field->DEREncodeElement(seq, m_a);
55 m_field->DEREncodeElement(seq, m_b);
68 if (encodedPointLen < 1 || !bt.
Get(type))
83 P.x.Decode(bt, m_field->MaxElementByteLength());
87 P.y = m_field->SquareRoot(m_b);
91 FieldElement z = m_field->Square(P.x);
93 P.y = m_field->Divide(m_field->Add(m_field->Multiply(z, m_field->Add(P.x, m_a)), m_b), z);
94 CRYPTOPP_ASSERT(P.x == m_field->Subtract(m_field->Divide(m_field->Subtract(m_field->Multiply(P.y, z), m_b), z), m_a));
95 z = m_field->SolveQuadraticEquation(P.y);
97 z.SetCoefficient(0, type & 1);
99 P.y = m_field->Multiply(z, P.x);
107 unsigned int len = m_field->MaxElementByteLength();
124 bt.
Put((byte)(2U + (!P.x ? 0U : m_field->Divide(P.y, P.x).GetBit(0))));
125 P.x.Encode(bt, m_field->MaxElementByteLength());
129 unsigned int len = m_field->MaxElementByteLength();
162 CRYPTOPP_UNUSED(rng);
164 pass = pass && m_a.CoefficientCount() <= m_field->MaxElementBitLength();
165 pass = pass && m_b.CoefficientCount() <= m_field->MaxElementBitLength();
168 pass = pass && m_field->GetModulus().IsIrreducible();
175 const FieldElement &x = P.x, &y = P.y;
177 (x.CoefficientCount() <= m_field->MaxElementBitLength()
178 && y.CoefficientCount() <= m_field->MaxElementBitLength()
179 && !(((x+m_a)*x*x+m_b-(x+y)*y)%m_field->GetModulus()));
184 if (P.identity && Q.identity)
187 if (P.identity && !Q.identity)
190 if (!P.identity && Q.identity)
193 return (m_field->Equal(P.x,Q.x) && m_field->Equal(P.y,Q.y));
198 #if defined(HAVE_GCC_INIT_PRIORITY) || defined(HAVE_MSC_INIT_PRIORITY) || defined(HAVE_XLC_INIT_PRIORITY) 200 #elif defined(CRYPTOPP_CXX11_DYNAMIC_INIT) 214 m_R.identity =
false;
215 m_R.y = m_field->Add(P.x, P.y);
223 if (P.identity)
return Q;
224 if (Q.identity)
return P;
226 if (m_field->Equal(P.x, Q.x) && m_field->Equal(P.y, m_field->Add(Q.x, Q.y)))
return Identity();
228 FieldElement t = m_field->Add(P.y, Q.y);
229 t = m_field->Divide(t, m_field->Add(P.x, Q.x));
230 FieldElement x = m_field->Square(t);
231 m_field->Accumulate(x, t);
232 m_field->Accumulate(x, Q.x);
233 m_field->Accumulate(x, m_a);
234 m_R.y = m_field->Add(P.y, m_field->Multiply(t, x));
235 m_field->Accumulate(x, P.x);
236 m_field->Accumulate(m_R.y, x);
239 m_R.identity =
false;
245 if (P.identity)
return P;
246 if (!m_field->IsUnit(P.x))
return Identity();
248 FieldElement t = m_field->Divide(P.y, P.x);
249 m_field->Accumulate(t, P.x);
250 m_R.y = m_field->Square(P.x);
251 m_R.x = m_field->Square(t);
252 m_field->Accumulate(m_R.x, t);
253 m_field->Accumulate(m_R.x, m_a);
254 m_field->Accumulate(m_R.y, m_field->Multiply(t, m_R.x));
255 m_field->Accumulate(m_R.y, m_R.x);
257 m_R.identity =
false;
bool VerifyPoint(const Point &P) const
Verifies points on elliptic curve.
Restricts the instantiation of a class to one static object without locks.
unsigned int EncodedPointSize(bool compressed=false) const
Determines encoded point size.
void DEREncodePoint(BufferedTransformation &bt, const Point &P, bool compressed) const
DER Encodes an elliptic curve point.
Interface for random number generators.
void EncodePoint(byte *encodedPoint, const Point &P, bool compressed) const
Encodes an elliptic curve point.
Classes for Elliptic Curves over binary fields.
Copy input to a memory buffer.
size_t BERDecodeOctetString(BufferedTransformation &bt, SecByteBlock &str)
BER decode octet string.
const Point & Inverse(const Point &P) const
Inverts the element in the group.
Elliptic Curve over GF(2^n)
Point BERDecodePoint(BufferedTransformation &bt) const
BER Decodes an elliptic curve point.
const Point & Identity() const
Provides the Identity element.
String-based implementation of Store interface.
#define CRYPTOPP_ASSERT(exp)
Debugging and diagnostic assertion.
void BERDecodeError()
Raises a BERDecodeErr.
Classes and functions for working with ANS.1 objects.
Implementation of BufferedTransformation's attachment interface.
bool DecodePoint(Point &P, BufferedTransformation &bt, size_t len) const
Decodes an elliptic curve point.
size_t DEREncodeOctetString(BufferedTransformation &bt, const byte *str, size_t strLen)
DER encode octet string.
Multiple precision integer with arithmetic operations.
bool Equal(const Point &P, const Point &Q) const
Compare two elements for equality.
size_t BERDecodeBitString(BufferedTransformation &bt, SecByteBlock &str, unsigned int &unusedBits)
DER decode bit string.
void DEREncode(BufferedTransformation &bt) const
Encode the fields fieldID and curve of the sequence ECParameters.
Crypto++ library namespace.
Elliptical Curve Point over GF(2^n)
const Point & Add(const Point &P, const Point &Q) const
Adds elements in the group.
size_type size() const
Provides the count of elements in the SecBlock.
lword TotalPutLength()
Provides the number of bytes written to the Sink.
const Point & Double(const Point &P) const
Doubles an element in the group.