00001
#ifndef CRYPTOPP_ECP_H
00002
#define CRYPTOPP_ECP_H
00003
00004
#include "modarith.h"
00005
#include "eprecomp.h"
00006
#include "smartptr.h"
00007
#include "pubkey.h"
00008
00009 NAMESPACE_BEGIN(CryptoPP)
00010
00011
00012 struct
ECPPoint
00013 {
00014 ECPPoint() : identity(
true) {}
00015 ECPPoint(
const Integer &x,
const Integer &y)
00016 : identity(
false), x(x), y(y) {}
00017
00018
bool operator==(
const ECPPoint &t)
const
00019
{
return (identity && t.identity) || (!identity && !t.identity && x==t.x && y==t.y);}
00020
bool operator< (
const ECPPoint &t)
const
00021
{
return identity ? !t.identity : (!t.identity && (x<t.x || (x==t.x && y<t.y)));}
00022
00023
bool identity;
00024
Integer x, y;
00025 };
00026
00027
00028 class ECP :
public AbstractGroup<ECPPoint>
00029 {
00030
public:
00031
typedef ModularArithmetic Field;
00032
typedef Integer FieldElement;
00033
typedef ECPPoint Point;
00034
00035
ECP() {}
00036
ECP(
const ECP &ecp,
bool convertToMontgomeryRepresentation =
false);
00037
ECP(
const Integer &modulus,
const FieldElement &a,
const FieldElement &b)
00038 : m_fieldPtr(
new Field(modulus)), m_a(a.IsNegative() ? modulus+a : a), m_b(b) {}
00039
00040
00041
ECP(
BufferedTransformation &bt);
00042
00043
00044
void DEREncode(
BufferedTransformation &bt)
const;
00045
00046
bool Equal(
const Point &P,
const Point &Q)
const;
00047
const Point& Identity()
const;
00048
const Point& Inverse(
const Point &P)
const;
00049
bool InversionIsFast()
const {
return true;}
00050
const Point& Add(
const Point &P,
const Point &Q)
const;
00051
const Point& Double(
const Point &P)
const;
00052
Point ScalarMultiply(
const Point &P,
const Integer &k)
const;
00053
Point CascadeScalarMultiply(
const Point &P,
const Integer &k1,
const Point &Q,
const Integer &k2)
const;
00054
void SimultaneousMultiply(
Point *results,
const Point &base,
const Integer *exponents,
unsigned int exponentsCount)
const;
00055
00056
Point Multiply(
const Integer &k,
const Point &P)
const
00057
{
return ScalarMultiply(P, k);}
00058
Point CascadeMultiply(
const Integer &k1,
const Point &P,
const Integer &k2,
const Point &Q)
const
00059
{
return CascadeScalarMultiply(P, k1, Q, k2);}
00060
00061
bool ValidateParameters(
RandomNumberGenerator &rng,
unsigned int level=3)
const;
00062
bool VerifyPoint(
const Point &P)
const;
00063
00064
unsigned int EncodedPointSize(
bool compressed =
false)
const
00065
{
return 1 + (compressed?1:2)*GetField().
MaxElementByteLength();}
00066
00067
bool DecodePoint(
Point &P,
BufferedTransformation &bt,
unsigned int len)
const;
00068
bool DecodePoint(
Point &P,
const byte *encodedPoint,
unsigned int len)
const;
00069
void EncodePoint(byte *encodedPoint,
const Point &P,
bool compressed)
const;
00070
void EncodePoint(
BufferedTransformation &bt,
const Point &P,
bool compressed)
const;
00071
00072
Point BERDecodePoint(
BufferedTransformation &bt)
const;
00073
void DEREncodePoint(
BufferedTransformation &bt,
const Point &P,
bool compressed)
const;
00074
00075
Integer FieldSize()
const {
return GetField().
GetModulus();}
00076
const Field & GetField()
const {
return *m_fieldPtr;}
00077
const FieldElement & GetA()
const {
return m_a;}
00078
const FieldElement & GetB()
const {
return m_b;}
00079
00080
private:
00081 clonable_ptr<Field> m_fieldPtr;
00082
FieldElement m_a, m_b;
00083
mutable Point m_R;
00084 };
00085
00086
template <
class T>
class EcPrecomputation;
00087
00088
00089 template<>
class EcPrecomputation<ECP> :
public DL_GroupPrecomputation<ECP::Point>
00090 {
00091
public:
00092
typedef ECP
EllipticCurve;
00093
00094
00095
bool NeedConversions()
const {
return true;}
00096 Element ConvertIn(
const Element &P)
const
00097
{
return P.identity ? P :
ECP::Point(m_ec->GetField().ConvertIn(P.x), m_ec->GetField().ConvertIn(P.y));};
00098 Element ConvertOut(
const Element &P)
const
00099
{
return P.identity ? P :
ECP::Point(m_ec->GetField().ConvertOut(P.x), m_ec->GetField().ConvertOut(P.y));}
00100
const AbstractGroup<Element> & GetGroup()
const {
return *m_ec;}
00101 Element BERDecodeElement(
BufferedTransformation &bt)
const {
return m_ec->BERDecodePoint(bt);}
00102
void DEREncodeElement(
BufferedTransformation &bt,
const Element &v)
const {m_ec->DEREncodePoint(bt, v,
false);}
00103
00104
00105
void SetCurve(
const ECP &ec);
00106
const ECP & GetCurve()
const {
return *m_ecOriginal;}
00107
00108
private:
00109 value_ptr<ECP> m_ec, m_ecOriginal;
00110 };
00111
00112 NAMESPACE_END
00113
00114
#endif