Fabcoin Core  0.16.2
P2P Digital Currency
ecp.h
Go to the documentation of this file.
1 // ecp.h - written and placed in the public domain by Wei Dai
2 
5 
6 #ifndef CRYPTOPP_ECP_H
7 #define CRYPTOPP_ECP_H
8 
9 #include "cryptlib.h"
10 #include "integer.h"
11 #include "algebra.h"
12 #include "modarith.h"
13 #include "ecpoint.h"
14 #include "eprecomp.h"
15 #include "smartptr.h"
16 #include "pubkey.h"
17 
19 
20 class CRYPTOPP_DLL ECP : public AbstractGroup<ECPPoint>, public EncodedPoint<ECPPoint>
23 {
24 public:
27  typedef ECPPoint Point;
28 
29  virtual ~ECP() {}
30 
32  ECP() {}
33 
38  ECP(const ECP &ecp, bool convertToMontgomeryRepresentation = false);
39 
44  ECP(const Integer &modulus, const FieldElement &a, const FieldElement &b)
45  : m_fieldPtr(new Field(modulus)), m_a(a.IsNegative() ? modulus+a : a), m_b(b) {}
46 
51 
54  void DEREncode(BufferedTransformation &bt) const;
55 
56  bool Equal(const Point &P, const Point &Q) const;
57  const Point& Identity() const;
58  const Point& Inverse(const Point &P) const;
59  bool InversionIsFast() const {return true;}
60  const Point& Add(const Point &P, const Point &Q) const;
61  const Point& Double(const Point &P) const;
62  Point ScalarMultiply(const Point &P, const Integer &k) const;
63  Point CascadeScalarMultiply(const Point &P, const Integer &k1, const Point &Q, const Integer &k2) const;
64  void SimultaneousMultiply(Point *results, const Point &base, const Integer *exponents, unsigned int exponentsCount) const;
65 
66  Point Multiply(const Integer &k, const Point &P) const
67  {return ScalarMultiply(P, k);}
68  Point CascadeMultiply(const Integer &k1, const Point &P, const Integer &k2, const Point &Q) const
69  {return CascadeScalarMultiply(P, k1, Q, k2);}
70 
71  bool ValidateParameters(RandomNumberGenerator &rng, unsigned int level=3) const;
72  bool VerifyPoint(const Point &P) const;
73 
74  unsigned int EncodedPointSize(bool compressed = false) const
75  {return 1 + (compressed?1:2)*GetField().MaxElementByteLength();}
76  // returns false if point is compressed and not valid (doesn't check if uncompressed)
77  bool DecodePoint(Point &P, BufferedTransformation &bt, size_t len) const;
78  bool DecodePoint(Point &P, const byte *encodedPoint, size_t len) const;
79  void EncodePoint(byte *encodedPoint, const Point &P, bool compressed) const;
80  void EncodePoint(BufferedTransformation &bt, const Point &P, bool compressed) const;
81 
82  Point BERDecodePoint(BufferedTransformation &bt) const;
83  void DEREncodePoint(BufferedTransformation &bt, const Point &P, bool compressed) const;
84 
85  Integer FieldSize() const {return GetField().GetModulus();}
86  const Field & GetField() const {return *m_fieldPtr;}
87  const FieldElement & GetA() const {return m_a;}
88  const FieldElement & GetB() const {return m_b;}
89 
90  bool operator==(const ECP &rhs) const
91  {return GetField() == rhs.GetField() && m_a == rhs.m_a && m_b == rhs.m_b;}
92 
93 private:
95  FieldElement m_a, m_b;
96  mutable Point m_R;
97 };
98 
101 
105 template <class EC> class EcPrecomputation;
106 
112 template<> class EcPrecomputation<ECP> : public DL_GroupPrecomputation<ECP::Point>
113 {
114 public:
116 
117  virtual ~EcPrecomputation() {}
118 
119  // DL_GroupPrecomputation
120  bool NeedConversions() const {return true;}
121  Element ConvertIn(const Element &P) const
122  {return P.identity ? P : ECP::Point(m_ec->GetField().ConvertIn(P.x), m_ec->GetField().ConvertIn(P.y));};
123  Element ConvertOut(const Element &P) const
124  {return P.identity ? P : ECP::Point(m_ec->GetField().ConvertOut(P.x), m_ec->GetField().ConvertOut(P.y));}
125  const AbstractGroup<Element> & GetGroup() const {return *m_ec;}
126  Element BERDecodeElement(BufferedTransformation &bt) const {return m_ec->BERDecodePoint(bt);}
127  void DEREncodeElement(BufferedTransformation &bt, const Element &v) const {m_ec->DEREncodePoint(bt, v, false);}
128 
129  // non-inherited
130  void SetCurve(const ECP &ec)
131  {
132  m_ec.reset(new ECP(ec, true));
133  m_ecOriginal = ec;
134  }
135  const ECP & GetCurve() const {return *m_ecOriginal;}
136 
137 private:
139 };
140 
142 
143 #endif
bool operator==(const ECP &rhs) const
Definition: ecp.h:90
FieldElement m_a
Definition: ecp.h:95
virtual ~ECP()
Definition: ecp.h:29
bool GetField(std::istream &is, std::string &name, std::string &value)
Definition: datatest.cpp:647
uint8_t byte
Definition: Common.h:57
Element ConvertIn(const Element &P) const
Definition: ecp.h:121
Elliptical Curve Point over GF(p), where p is prime.
Definition: ecpoint.h:21
bool InversionIsFast() const
Determine if inversion is fast.
Definition: ecp.h:59
Elliptic Curve over GF(p), where p is prime.
Definition: ecp.h:22
#define NAMESPACE_BEGIN(x)
Definition: config.h:200
#define CRYPTOPP_DLL_TEMPLATE_CLASS
Definition: config.h:720
const FieldElement & GetA() const
Definition: ecp.h:87
#define Q(i)
Definition: cast.cpp:199
Abstract base classes that provide a uniform interface to this library.
void DEREncodeElement(BufferedTransformation &bt, const Element &v) const
Definition: ecp.h:127
Classes for automatic resource management.
unsigned int EncodedPointSize(bool compressed=false) const
Determines encoded point size.
Definition: ecp.h:74
Ring of congruence classes modulo n.
Definition: modarith.h:34
Interface for random number generators.
Definition: cryptlib.h:1188
int Add(word *C, const word *A, const word *B, size_t N)
Definition: integer.cpp:2143
Classes for Elliptic Curve points.
Classes for performing mathematics over different fields.
Interface for buffered transformations.
Definition: cryptlib.h:1352
virtual ~EcPrecomputation()
Definition: ecp.h:117
FieldElement m_b
Definition: ecp.h:95
const AbstractGroup< Element > & GetGroup() const
Definition: ecp.h:125
#define a(i)
Element BERDecodeElement(BufferedTransformation &bt) const
Definition: ecp.h:126
const ECP & GetCurve() const
Definition: ecp.h:135
A pointer which can be copied and cloned.
Definition: smartptr.h:108
Multiple precision integer with arithmetic operations.
Definition: integer.h:43
clonable_ptr< Field > m_fieldPtr
Definition: ecp.h:94
const FieldElement & GetB() const
Definition: ecp.h:88
#define b(i, j)
Abstract group.
Definition: algebra.h:26
Integer FieldSize() const
Definition: ecp.h:85
Element ConvertOut(const Element &P) const
Definition: ecp.h:123
Classes for precomputation in a group.
Abstract class for encoding and decoding ellicptic curve points.
Definition: ecpoint.h:93
bool identity
Definition: ecpoint.h:47
#define P
Elliptic Curve precomputation.
Definition: ec2n.h:98
#define k2
Definition: ripemd.cpp:20
ECP()
Construct an ECP.
Definition: ecp.h:32
#define k1
Definition: ripemd.cpp:19
Multiple precision integer with arithmetic operations.
#define NAMESPACE_END
Definition: config.h:201
Class file for performing modular arithmetic.
const Field & GetField() const
Definition: ecp.h:86
value_ptr< ECP > m_ecOriginal
Definition: ecp.h:138
#define CRYPTOPP_DLL
Definition: config.h:704
ECP(const Integer &modulus, const FieldElement &a, const FieldElement &b)
Construct an ECP.
Definition: ecp.h:44
Point m_R
Definition: ecp.h:96
ModularArithmetic Field
Definition: ecp.h:25
bool NeedConversions() const
Definition: ecp.h:120
void SetCurve(const ECP &ec)
Definition: ecp.h:130
Point CascadeMultiply(const Integer &k1, const Point &P, const Integer &k2, const Point &Q) const
Definition: ecp.h:68
Integer FieldElement
Definition: ecp.h:26
Integer x
Definition: ecpoint.h:46
ECPPoint Point
Definition: ecp.h:27
Integer y
Definition: ecpoint.h:46
Point Multiply(const Integer &k, const Point &P) const
Definition: ecp.h:66