ec2n.h
Go to the documentation of this file.
// ec2n.h - originally written and placed in the public domain by Wei Dai
/// \file ec2n.h
/// \brief Classes for Elliptic Curves over binary fields
#ifndef CRYPTOPP_EC2N_H
#define CRYPTOPP_EC2N_H
#include "cryptlib.h"
#include "gf2n.h"
#include "integer.h"
#include "algebra.h"
#include "ecpoint.h"
#include "eprecomp.h"
#include "smartptr.h"
#include "pubkey.h"
#if CRYPTOPP_MSC_VERSION
# pragma warning(push)
# pragma warning(disable: 4231 4275)
#endif
NAMESPACE_BEGIN(CryptoPP)
/// \brief Elliptic Curve over GF(2^n)
class CRYPTOPP_DLL EC2N : public AbstractGroup<EC2NPoint>, public EncodedPoint<EC2NPoint>
{
public:
typedef GF2NP Field;
typedef Field::Element FieldElement;
typedef EC2NPoint Point;
virtual ~EC2N() {}
/// \brief Construct an EC2N
EC2N() {}
/// \brief Construct an EC2N
/// \param field Field, GF2NP derived class
/// \param a Field::Element
/// \param b Field::Element
EC2N(const Field &field, const Field::Element &a, const Field::Element &b)
: m_field(field), m_a(a), m_b(b) {}
/// \brief Construct an EC2N from BER encoded parameters
/// \param bt BufferedTransformation derived object
/// \details This constructor will decode and extract the fields fieldID and curve of the sequence ECParameters
EC2N(BufferedTransformation &bt);
/// \brief Encode the fields fieldID and curve of the sequence ECParameters
/// \param bt BufferedTransformation derived object
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 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)*m_field->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 Integer::Power2(m_field->MaxElementBitLength());}
const Field & GetField() const {return *m_field;}
const FieldElement & GetA() const {return m_a;}
const FieldElement & GetB() const {return m_b;}
bool operator==(const EC2N &rhs) const
{return GetField() == rhs.GetField() && m_a == rhs.m_a && m_b == rhs.m_b;}
private:
clonable_ptr<Field> m_field;
FieldElement m_a, m_b;
mutable Point m_R;
};
CRYPTOPP_DLL_TEMPLATE_CLASS DL_FixedBasePrecomputationImpl<EC2N::Point>;
CRYPTOPP_DLL_TEMPLATE_CLASS DL_GroupPrecomputation<EC2N::Point>;
/// \brief Elliptic Curve precomputation
/// \tparam EC elliptic curve field
template <class EC> class EcPrecomputation;
/// \brief EC2N precomputation specialization
/// \details Implementation of <tt>DL_GroupPrecomputation<EC2N::Point></tt>
/// \sa DL_GroupPrecomputation
template<> class EcPrecomputation<EC2N> : public DL_GroupPrecomputation<EC2N::Point>
{
public:
typedef EC2N EllipticCurve;
virtual ~EcPrecomputation() {}
// DL_GroupPrecomputation
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);}
/// \brief Set the elliptic curve
/// \param ec ECP derived class
/// \details SetCurve() is not inherited
void SetCurve(const EC2N &ec) {m_ec = ec;}
/// \brief Get the elliptic curve
/// \return EC2N curve
/// \details GetCurve() is not inherited
const EC2N & GetCurve() const {return m_ec;}
private:
EC2N m_ec;
};
NAMESPACE_END
#if CRYPTOPP_MSC_VERSION
# pragma warning(pop)
#endif
#endif
algebra.h
Classes for performing mathematics over different fields.
operator==
bool operator==(const OID &lhs, const OID &rhs)
Compare two OIDs for equality.
AbstractGroup
Abstract group.
Definition: algebra.h:27
BufferedTransformation
Interface for buffered transformations.
Definition: cryptlib.h:1652
DL_FixedBasePrecomputationImpl
DL_FixedBasePrecomputation adapter class.
Definition: eprecomp.h:127
DL_GroupPrecomputation< EC2N::Point >
EC2N
Elliptic Curve over GF(2^n)
Definition: ec2n.h:27
EC2N::DecodePoint
bool DecodePoint(Point &P, const byte *encodedPoint, size_t len) const
Decodes an elliptic curve point.
EC2N::VerifyPoint
bool VerifyPoint(const Point &P) const
Verifies points on elliptic curve.
EC2N::Inverse
const Point & Inverse(const Point &P) const
Inverts the element in the group.
EC2N::EncodePoint
void EncodePoint(BufferedTransformation &bt, const Point &P, bool compressed) const
Encodes an elliptic curve point.
EC2N::EncodedPointSize
unsigned int EncodedPointSize(bool compressed=false) const
Determines encoded point size.
Definition: ec2n.h:69
EC2N::Equal
bool Equal(const Point &P, const Point &Q) const
Compare two elements for equality.
EC2N::DecodePoint
bool DecodePoint(Point &P, BufferedTransformation &bt, size_t len) const
Decodes an elliptic curve point.
EC2N::DEREncodePoint
void DEREncodePoint(BufferedTransformation &bt, const Point &P, bool compressed) const
DER Encodes an elliptic curve point.
EC2N::InversionIsFast
bool InversionIsFast() const
Determine if inversion is fast.
Definition: ec2n.h:57
EC2N::EC2N
EC2N(const Field &field, const Field::Element &a, const Field::Element &b)
Construct an EC2N.
Definition: ec2n.h:42
EC2N::EncodePoint
void EncodePoint(byte *encodedPoint, const Point &P, bool compressed) const
Encodes an elliptic curve point.
EC2N::EC2N
EC2N(BufferedTransformation &bt)
Construct an EC2N from BER encoded parameters.
EC2N::DEREncode
void DEREncode(BufferedTransformation &bt) const
Encode the fields fieldID and curve of the sequence ECParameters.
EC2N::BERDecodePoint
Point BERDecodePoint(BufferedTransformation &bt) const
BER Decodes an elliptic curve point.
EC2N::Identity
const Point & Identity() const
Provides the Identity element.
EC2N::EC2N
EC2N()
Construct an EC2N.
Definition: ec2n.h:36
EC2N::Add
const Point & Add(const Point &P, const Point &Q) const
Adds elements in the group.
EcPrecomputation< EC2N >::BERDecodeElement
Element BERDecodeElement(BufferedTransformation &bt) const
Decodes element in DER format.
Definition: ec2n.h:113
EcPrecomputation< EC2N >::SetCurve
void SetCurve(const EC2N &ec)
Set the elliptic curve.
Definition: ec2n.h:119
EcPrecomputation< EC2N >::GetGroup
const AbstractGroup< Element > & GetGroup() const
Retrieves AbstractGroup interface.
Definition: ec2n.h:112
EcPrecomputation< EC2N >::GetCurve
const EC2N & GetCurve() const
Get the elliptic curve.
Definition: ec2n.h:124
EcPrecomputation< EC2N >::DEREncodeElement
void DEREncodeElement(BufferedTransformation &bt, const Element &v) const
Encodes element in DER format.
Definition: ec2n.h:114
EcPrecomputation
Elliptic Curve precomputation.
Definition: ec2n.h:99
EncodedPoint
Abstract class for encoding and decoding ellicptic curve points.
Definition: ecpoint.h:91
GF2NP
GF(2^n) with Polynomial Basis.
Definition: gf2n.h:297
Integer
Multiple precision integer with arithmetic operations.
Definition: integer.h:50
Integer::Power2
static Integer Power2(size_t e)
Exponentiates to a power of 2.
RandomNumberGenerator
Interface for random number generators.
Definition: cryptlib.h:1435
clonable_ptr
A pointer which can be copied and cloned.
Definition: smartptr.h:105
CRYPTOPP_DLL_TEMPLATE_CLASS
#define CRYPTOPP_DLL_TEMPLATE_CLASS
Instantiate templates in a dynamic library.
Definition: config_dll.h:72
cryptlib.h
Abstract base classes that provide a uniform interface to this library.
ecpoint.h
Classes for Elliptic Curve points.
eprecomp.h
Classes for precomputation in a group.
gf2n.h
Classes and functions for schemes over GF(2^n)
integer.h
Multiple precision integer with arithmetic operations.
CryptoPP
Crypto++ library namespace.
pubkey.h
This file contains helper classes/functions for implementing public key algorithms.
smartptr.h
Classes for automatic resource management.
EC2NPoint
Elliptical Curve Point over GF(2^n)
Definition: ecpoint.h:54