Fabcoin Core  0.16.2
P2P Digital Currency
ec2n.h
Go to the documentation of this file.
1 // ec2n.h - written and placed in the public domain by Wei Dai
2 
6 
7 
8 #ifndef CRYPTOPP_EC2N_H
9 #define CRYPTOPP_EC2N_H
10 
11 #include "cryptlib.h"
12 #include "gf2n.h"
13 #include "integer.h"
14 #include "algebra.h"
15 #include "ecpoint.h"
16 #include "eprecomp.h"
17 #include "smartptr.h"
18 #include "pubkey.h"
19 
21 
22 class CRYPTOPP_DLL EC2N : public AbstractGroup<EC2NPoint>, public EncodedPoint<EC2NPoint>
25 {
26 public:
27  typedef GF2NP Field;
29  typedef EC2NPoint Point;
30 
31  virtual ~EC2N() {}
32 
34  EC2N() {}
35 
40  EC2N(const Field &field, const Field::Element &a, const Field::Element &b)
41  : m_field(field), m_a(a), m_b(b) {}
42 
47 
50  void DEREncode(BufferedTransformation &bt) const;
51 
52  bool Equal(const Point &P, const Point &Q) const;
53  const Point& Identity() const;
54  const Point& Inverse(const Point &P) const;
55  bool InversionIsFast() const {return true;}
56  const Point& Add(const Point &P, const Point &Q) const;
57  const Point& Double(const Point &P) const;
58 
59  Point Multiply(const Integer &k, const Point &P) const
60  {return ScalarMultiply(P, k);}
61  Point CascadeMultiply(const Integer &k1, const Point &P, const Integer &k2, const Point &Q) const
62  {return CascadeScalarMultiply(P, k1, Q, k2);}
63 
64  bool ValidateParameters(RandomNumberGenerator &rng, unsigned int level=3) const;
65  bool VerifyPoint(const Point &P) const;
66 
67  unsigned int EncodedPointSize(bool compressed = false) const
68  {return 1 + (compressed?1:2)*m_field->MaxElementByteLength();}
69  // returns false if point is compressed and not valid (doesn't check if uncompressed)
70  bool DecodePoint(Point &P, BufferedTransformation &bt, size_t len) const;
71  bool DecodePoint(Point &P, const byte *encodedPoint, size_t len) const;
72  void EncodePoint(byte *encodedPoint, const Point &P, bool compressed) const;
73  void EncodePoint(BufferedTransformation &bt, const Point &P, bool compressed) const;
74 
75  Point BERDecodePoint(BufferedTransformation &bt) const;
76  void DEREncodePoint(BufferedTransformation &bt, const Point &P, bool compressed) const;
77 
78  Integer FieldSize() const {return Integer::Power2(m_field->MaxElementBitLength());}
79  const Field & GetField() const {return *m_field;}
80  const FieldElement & GetA() const {return m_a;}
81  const FieldElement & GetB() const {return m_b;}
82 
83  bool operator==(const EC2N &rhs) const
84  {return GetField() == rhs.GetField() && m_a == rhs.m_a && m_b == rhs.m_b;}
85 
86 private:
88  FieldElement m_a, m_b;
89  mutable Point m_R;
90 };
91 
94 
98 template <class EC> class EcPrecomputation;
99 
104 template<> class EcPrecomputation<EC2N> : public DL_GroupPrecomputation<EC2N::Point>
105 {
106 public:
108 
109  virtual ~EcPrecomputation() {}
110 
111  // DL_GroupPrecomputation
112  const AbstractGroup<Element> & GetGroup() const {return m_ec;}
113  Element BERDecodeElement(BufferedTransformation &bt) const {return m_ec.BERDecodePoint(bt);}
114  void DEREncodeElement(BufferedTransformation &bt, const Element &v) const {m_ec.DEREncodePoint(bt, v, false);}
115 
116  // non-inherited
117  void SetCurve(const EC2N &ec) {m_ec = ec;}
118  const EC2N & GetCurve() const {return m_ec;}
119 
120 private:
122 };
123 
125 
126 #endif
Field::Element FieldElement
Definition: ec2n.h:28
bool GetField(std::istream &is, std::string &name, std::string &value)
Definition: datatest.cpp:647
GF2NP Field
Definition: ec2n.h:27
FieldElement m_a
Definition: ec2n.h:88
uint8_t byte
Definition: Common.h:57
FieldElement m_b
Definition: ec2n.h:88
EC2N()
Construct an EC2N.
Definition: ec2n.h:34
Point CascadeMultiply(const Integer &k1, const Point &P, const Integer &k2, const Point &Q) const
Definition: ec2n.h:61
#define NAMESPACE_BEGIN(x)
Definition: config.h:200
#define CRYPTOPP_DLL_TEMPLATE_CLASS
Definition: config.h:720
#define Q(i)
Definition: cast.cpp:199
Abstract base classes that provide a uniform interface to this library.
EC2NPoint Point
Definition: ec2n.h:29
Classes for automatic resource management.
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.
void DEREncodeElement(BufferedTransformation &bt, const Element &v) const
Definition: ec2n.h:114
Integer FieldSize() const
Definition: ec2n.h:78
Classes for performing mathematics over different fields.
Interface for buffered transformations.
Definition: cryptlib.h:1352
const AbstractGroup< Element > & GetGroup() const
Definition: ec2n.h:112
void SetCurve(const EC2N &ec)
Definition: ec2n.h:117
const FieldElement & GetB() const
Definition: ec2n.h:81
Element BERDecodeElement(BufferedTransformation &bt) const
Definition: ec2n.h:113
EC2N(const Field &field, const Field::Element &a, const Field::Element &b)
Construct an EC2N.
Definition: ec2n.h:40
#define a(i)
unsigned int EncodedPointSize(bool compressed=false) const
Determines encoded point size.
Definition: ec2n.h:67
Point Multiply(const Integer &k, const Point &P) const
Definition: ec2n.h:59
EuclideanDomainOf< PolynomialMod2 >::Element Element
Definition: algebra.h:29
A pointer which can be copied and cloned.
Definition: smartptr.h:108
static Integer CRYPTOPP_API Power2(size_t e)
Exponentiates to a power of 2.
Definition: integer.cpp:3008
clonable_ptr< Field > m_field
Definition: ec2n.h:87
Multiple precision integer with arithmetic operations.
Definition: integer.h:43
Elliptic Curve over GF(2^n)
Definition: ec2n.h:24
ANONYMOUS_NAMESPACE_BEGIN const CryptoPP::GF2_32 field
Definition: ida.cpp:14
Classes and functions for schemes over GF(2^n)
#define b(i, j)
Abstract group.
Definition: algebra.h:26
Classes for precomputation in a group.
bool operator==(const EC2N &rhs) const
Definition: ec2n.h:83
GF(2^n) with Polynomial Basis.
Definition: gf2n.h:290
Abstract class for encoding and decoding ellicptic curve points.
Definition: ecpoint.h:93
bool InversionIsFast() const
Determine if inversion is fast.
Definition: ec2n.h:55
#define P
Elliptic Curve precomputation.
Definition: ec2n.h:98
#define k2
Definition: ripemd.cpp:20
#define k1
Definition: ripemd.cpp:19
Multiple precision integer with arithmetic operations.
#define NAMESPACE_END
Definition: config.h:201
Point m_R
Definition: ec2n.h:89
virtual ~EC2N()
Definition: ec2n.h:31
virtual ~EcPrecomputation()
Definition: ec2n.h:109
Elliptical Curve Point over GF(2^n)
Definition: ecpoint.h:55
const EC2N & GetCurve() const
Definition: ec2n.h:118
const Field & GetField() const
Definition: ec2n.h:79
#define CRYPTOPP_DLL
Definition: config.h:704
const FieldElement & GetA() const
Definition: ec2n.h:80