2017-01-27 12:05:45 +00:00
|
|
|
// ecp.h - originally written and placed in the public domain by Wei Dai
|
2015-11-18 20:32:28 +00:00
|
|
|
|
2017-11-29 15:54:33 +00:00
|
|
|
/// \file ecp.h
|
|
|
|
/// \brief Classes for Elliptic Curves over prime fields
|
2015-11-18 20:32:28 +00:00
|
|
|
|
2015-11-05 06:59:46 +00:00
|
|
|
#ifndef CRYPTOPP_ECP_H
|
|
|
|
#define CRYPTOPP_ECP_H
|
|
|
|
|
|
|
|
#include "cryptlib.h"
|
|
|
|
#include "integer.h"
|
2015-12-04 23:18:34 +00:00
|
|
|
#include "algebra.h"
|
2015-11-05 06:59:46 +00:00
|
|
|
#include "modarith.h"
|
2016-11-04 15:13:07 +00:00
|
|
|
#include "ecpoint.h"
|
2015-11-05 06:59:46 +00:00
|
|
|
#include "eprecomp.h"
|
|
|
|
#include "smartptr.h"
|
|
|
|
#include "pubkey.h"
|
|
|
|
|
2017-06-02 09:18:52 +00:00
|
|
|
#if CRYPTOPP_MSC_VERSION
|
|
|
|
# pragma warning(push)
|
|
|
|
# pragma warning(disable: 4231 4275)
|
|
|
|
#endif
|
|
|
|
|
2015-11-05 06:59:46 +00:00
|
|
|
NAMESPACE_BEGIN(CryptoPP)
|
|
|
|
|
2017-11-29 15:54:33 +00:00
|
|
|
/// \brief Elliptic Curve over GF(p), where p is prime
|
2016-11-04 15:13:07 +00:00
|
|
|
class CRYPTOPP_DLL ECP : public AbstractGroup<ECPPoint>, public EncodedPoint<ECPPoint>
|
2015-11-05 06:59:46 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
typedef ModularArithmetic Field;
|
|
|
|
typedef Integer FieldElement;
|
|
|
|
typedef ECPPoint Point;
|
|
|
|
|
2016-10-18 02:00:31 +00:00
|
|
|
virtual ~ECP() {}
|
|
|
|
|
2017-11-29 15:54:33 +00:00
|
|
|
/// \brief Construct an ECP
|
2015-11-05 06:59:46 +00:00
|
|
|
ECP() {}
|
2016-10-18 23:39:33 +00:00
|
|
|
|
2017-11-29 15:54:33 +00:00
|
|
|
/// \brief Copy construct an ECP
|
|
|
|
/// \param ecp the other ECP object
|
|
|
|
/// \param convertToMontgomeryRepresentation flag indicating if the curve should be converted to a MontgomeryRepresentation
|
|
|
|
/// \sa ModularArithmetic, MontgomeryRepresentation
|
2015-11-05 06:59:46 +00:00
|
|
|
ECP(const ECP &ecp, bool convertToMontgomeryRepresentation = false);
|
2016-10-18 23:39:33 +00:00
|
|
|
|
2017-11-29 15:54:33 +00:00
|
|
|
/// \brief Construct an ECP
|
|
|
|
/// \param modulus the prime modulus
|
|
|
|
/// \param a Field::Element
|
|
|
|
/// \param b Field::Element
|
2015-11-05 06:59:46 +00:00
|
|
|
ECP(const Integer &modulus, const FieldElement &a, const FieldElement &b)
|
|
|
|
: m_fieldPtr(new Field(modulus)), m_a(a.IsNegative() ? modulus+a : a), m_b(b) {}
|
2016-10-18 23:39:33 +00:00
|
|
|
|
2017-11-29 15:54:33 +00:00
|
|
|
/// \brief Construct an ECP from BER encoded parameters
|
|
|
|
/// \param bt BufferedTransformation derived object
|
|
|
|
/// \details This constructor will decode and extract the the fields fieldID and curve of the sequence ECParameters
|
2015-11-05 06:59:46 +00:00
|
|
|
ECP(BufferedTransformation &bt);
|
|
|
|
|
2017-11-29 15:54:33 +00:00
|
|
|
/// \brief Encode the fields fieldID and curve of the sequence ECParameters
|
|
|
|
/// \param bt BufferedTransformation derived object
|
2015-11-05 06:59:46 +00:00
|
|
|
void DEREncode(BufferedTransformation &bt) const;
|
|
|
|
|
|
|
|
bool Equal(const Point &P, const Point &Q) const;
|
|
|
|
const Point& Identity() const;
|
|
|
|
const Point& Inverse(const Point &P) const;
|
|
|
|
bool InversionIsFast() const {return true;}
|
|
|
|
const Point& Add(const Point &P, const Point &Q) const;
|
|
|
|
const Point& Double(const Point &P) const;
|
|
|
|
Point ScalarMultiply(const Point &P, const Integer &k) const;
|
|
|
|
Point CascadeScalarMultiply(const Point &P, const Integer &k1, const Point &Q, const Integer &k2) const;
|
|
|
|
void SimultaneousMultiply(Point *results, const Point &base, const Integer *exponents, unsigned int exponentsCount) const;
|
|
|
|
|
|
|
|
Point Multiply(const Integer &k, const Point &P) const
|
|
|
|
{return ScalarMultiply(P, k);}
|
|
|
|
Point CascadeMultiply(const Integer &k1, const Point &P, const Integer &k2, const Point &Q) const
|
|
|
|
{return CascadeScalarMultiply(P, k1, Q, k2);}
|
|
|
|
|
|
|
|
bool ValidateParameters(RandomNumberGenerator &rng, unsigned int level=3) const;
|
|
|
|
bool VerifyPoint(const Point &P) const;
|
|
|
|
|
|
|
|
unsigned int EncodedPointSize(bool compressed = false) const
|
|
|
|
{return 1 + (compressed?1:2)*GetField().MaxElementByteLength();}
|
|
|
|
// returns false if point is compressed and not valid (doesn't check if uncompressed)
|
|
|
|
bool DecodePoint(Point &P, BufferedTransformation &bt, size_t len) const;
|
|
|
|
bool DecodePoint(Point &P, const byte *encodedPoint, size_t len) const;
|
|
|
|
void EncodePoint(byte *encodedPoint, const Point &P, bool compressed) const;
|
|
|
|
void EncodePoint(BufferedTransformation &bt, const Point &P, bool compressed) const;
|
|
|
|
|
|
|
|
Point BERDecodePoint(BufferedTransformation &bt) const;
|
|
|
|
void DEREncodePoint(BufferedTransformation &bt, const Point &P, bool compressed) const;
|
|
|
|
|
|
|
|
Integer FieldSize() const {return GetField().GetModulus();}
|
|
|
|
const Field & GetField() const {return *m_fieldPtr;}
|
|
|
|
const FieldElement & GetA() const {return m_a;}
|
|
|
|
const FieldElement & GetB() const {return m_b;}
|
|
|
|
|
|
|
|
bool operator==(const ECP &rhs) const
|
|
|
|
{return GetField() == rhs.GetField() && m_a == rhs.m_a && m_b == rhs.m_b;}
|
2016-09-10 08:57:48 +00:00
|
|
|
|
2015-11-05 06:59:46 +00:00
|
|
|
private:
|
|
|
|
clonable_ptr<Field> m_fieldPtr;
|
|
|
|
FieldElement m_a, m_b;
|
|
|
|
mutable Point m_R;
|
|
|
|
};
|
|
|
|
|
|
|
|
CRYPTOPP_DLL_TEMPLATE_CLASS DL_FixedBasePrecomputationImpl<ECP::Point>;
|
|
|
|
CRYPTOPP_DLL_TEMPLATE_CLASS DL_GroupPrecomputation<ECP::Point>;
|
|
|
|
|
2017-11-29 15:54:33 +00:00
|
|
|
/// \brief Elliptic Curve precomputation
|
|
|
|
/// \tparam EC elliptic curve field
|
2016-10-18 23:39:33 +00:00
|
|
|
template <class EC> class EcPrecomputation;
|
2015-11-05 06:59:46 +00:00
|
|
|
|
2017-11-29 15:54:33 +00:00
|
|
|
/// \brief ECP precomputation specialization
|
|
|
|
/// \details Implementation of <tt>DL_GroupPrecomputation<ECP::Point></tt> with input and output
|
|
|
|
/// conversions for Montgomery modular multiplication.
|
|
|
|
/// \sa DL_GroupPrecomputation, ModularArithmetic, MontgomeryRepresentation
|
2015-11-05 06:59:46 +00:00
|
|
|
template<> class EcPrecomputation<ECP> : public DL_GroupPrecomputation<ECP::Point>
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
typedef ECP EllipticCurve;
|
2016-09-10 08:57:48 +00:00
|
|
|
|
2016-10-18 02:00:31 +00:00
|
|
|
virtual ~EcPrecomputation() {}
|
|
|
|
|
2015-11-05 06:59:46 +00:00
|
|
|
// DL_GroupPrecomputation
|
|
|
|
bool NeedConversions() const {return true;}
|
|
|
|
Element ConvertIn(const Element &P) const
|
|
|
|
{return P.identity ? P : ECP::Point(m_ec->GetField().ConvertIn(P.x), m_ec->GetField().ConvertIn(P.y));};
|
|
|
|
Element ConvertOut(const Element &P) const
|
|
|
|
{return P.identity ? P : ECP::Point(m_ec->GetField().ConvertOut(P.x), m_ec->GetField().ConvertOut(P.y));}
|
|
|
|
const AbstractGroup<Element> & GetGroup() const {return *m_ec;}
|
|
|
|
Element BERDecodeElement(BufferedTransformation &bt) const {return m_ec->BERDecodePoint(bt);}
|
|
|
|
void DEREncodeElement(BufferedTransformation &bt, const Element &v) const {m_ec->DEREncodePoint(bt, v, false);}
|
|
|
|
|
2018-01-23 21:08:46 +00:00
|
|
|
/// \brief Set the elliptic curve
|
|
|
|
/// \param ec ECP derived class
|
|
|
|
/// \details SetCurve() is not inherited
|
2015-11-05 06:59:46 +00:00
|
|
|
void SetCurve(const ECP &ec)
|
|
|
|
{
|
|
|
|
m_ec.reset(new ECP(ec, true));
|
|
|
|
m_ecOriginal = ec;
|
|
|
|
}
|
2018-01-23 21:08:46 +00:00
|
|
|
|
|
|
|
/// \brief Get the elliptic curve
|
|
|
|
/// \returns ECP curve
|
|
|
|
/// \details GetCurve() is not inherited
|
2015-11-05 06:59:46 +00:00
|
|
|
const ECP & GetCurve() const {return *m_ecOriginal;}
|
2016-09-10 08:57:48 +00:00
|
|
|
|
2015-11-05 06:59:46 +00:00
|
|
|
private:
|
|
|
|
value_ptr<ECP> m_ec, m_ecOriginal;
|
|
|
|
};
|
|
|
|
|
|
|
|
NAMESPACE_END
|
|
|
|
|
2017-06-02 09:18:52 +00:00
|
|
|
#if CRYPTOPP_MSC_VERSION
|
|
|
|
# pragma warning(pop)
|
|
|
|
#endif
|
|
|
|
|
2015-11-05 06:59:46 +00:00
|
|
|
#endif
|