Security Scol plugin
ec2n.h
Go to the documentation of this file.
1// ec2n.h - originally written and placed in the public domain by Wei Dai
2
5
6#ifndef CRYPTOPP_EC2N_H
7#define CRYPTOPP_EC2N_H
8
9#include "cryptlib.h"
10#include "gf2n.h"
11#include "integer.h"
12#include "algebra.h"
13#include "ecpoint.h"
14#include "eprecomp.h"
15#include "smartptr.h"
16#include "pubkey.h"
17
18#if CRYPTOPP_MSC_VERSION
19# pragma warning(push)
20# pragma warning(disable: 4231 4275)
21#endif
22
23NAMESPACE_BEGIN(CryptoPP)
24
25
26class CRYPTOPP_DLL EC2N : public AbstractGroup<EC2NPoint>, public EncodedPoint<EC2NPoint>
27{
28public:
29 typedef GF2NP Field;
30 typedef Field::Element FieldElement;
31 typedef EC2NPoint Point;
32
33 virtual ~EC2N() {}
34
36 EC2N() {}
37
42 EC2N(const Field &field, const Field::Element &a, const Field::Element &b)
43 : m_field(field), m_a(a), m_b(b) {}
44
49
52 void DEREncode(BufferedTransformation &bt) const;
53
54 bool Equal(const Point &P, const Point &Q) const;
55 const Point& Identity() const;
56 const Point& Inverse(const Point &P) const;
57 bool InversionIsFast() const {return true;}
58 const Point& Add(const Point &P, const Point &Q) const;
59 const Point& Double(const Point &P) const;
60
61 Point Multiply(const Integer &k, const Point &P) const
62 {return ScalarMultiply(P, k);}
63 Point CascadeMultiply(const Integer &k1, const Point &P, const Integer &k2, const Point &Q) const
64 {return CascadeScalarMultiply(P, k1, Q, k2);}
65
66 bool ValidateParameters(RandomNumberGenerator &rng, unsigned int level=3) const;
67 bool VerifyPoint(const Point &P) const;
68
69 unsigned int EncodedPointSize(bool compressed = false) const
70 {return 1 + (compressed?1:2)*m_field->MaxElementByteLength();}
71 // returns false if point is compressed and not valid (doesn't check if uncompressed)
72 bool DecodePoint(Point &P, BufferedTransformation &bt, size_t len) const;
73 bool DecodePoint(Point &P, const byte *encodedPoint, size_t len) const;
74 void EncodePoint(byte *encodedPoint, const Point &P, bool compressed) const;
75 void EncodePoint(BufferedTransformation &bt, const Point &P, bool compressed) const;
76
77 Point BERDecodePoint(BufferedTransformation &bt) const;
78 void DEREncodePoint(BufferedTransformation &bt, const Point &P, bool compressed) const;
79
80 Integer FieldSize() const {return Integer::Power2(m_field->MaxElementBitLength());}
81 const Field & GetField() const {return *m_field;}
82 const FieldElement & GetA() const {return m_a;}
83 const FieldElement & GetB() const {return m_b;}
84
85 bool operator==(const EC2N &rhs) const
86 {return GetField() == rhs.GetField() && m_a == rhs.m_a && m_b == rhs.m_b;}
87
88private:
89 clonable_ptr<Field> m_field;
90 FieldElement m_a, m_b;
91 mutable Point m_R;
92};
93
94CRYPTOPP_DLL_TEMPLATE_CLASS DL_FixedBasePrecomputationImpl<EC2N::Point>;
95CRYPTOPP_DLL_TEMPLATE_CLASS DL_GroupPrecomputation<EC2N::Point>;
96
99template <class EC> class EcPrecomputation;
100
104template<> class EcPrecomputation<EC2N> : public DL_GroupPrecomputation<EC2N::Point>
105{
106public:
107 typedef EC2N EllipticCurve;
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
119 void SetCurve(const EC2N &ec) {m_ec = ec;}
120
124 const EC2N & GetCurve() const {return m_ec;}
125
126private:
127 EC2N m_ec;
128};
129
130NAMESPACE_END
131
132#if CRYPTOPP_MSC_VERSION
133# pragma warning(pop)
134#endif
135
136#endif
Classes for performing mathematics over different fields.
Abstract group.
Definition algebra.h:27
Interface for buffered transformations.
Definition cryptlib.h:1652
DL_FixedBasePrecomputation adapter class.
Definition eprecomp.h:127
DL_GroupPrecomputation interface.
Definition eprecomp.h:20
Elliptic Curve over GF(2^n)
Definition ec2n.h:27
unsigned int EncodedPointSize(bool compressed=false) const
Determines encoded point size.
Definition ec2n.h:69
bool InversionIsFast() const
Determine if inversion is fast.
Definition ec2n.h:57
EC2N(const Field &field, const Field::Element &a, const Field::Element &b)
Construct an EC2N.
Definition ec2n.h:42
EC2N()
Construct an EC2N.
Definition ec2n.h:36
const AbstractGroup< Element > & GetGroup() const
Retrieves AbstractGroup interface.
Definition ec2n.h:112
Element BERDecodeElement(BufferedTransformation &bt) const
Decodes element in DER format.
Definition ec2n.h:113
void SetCurve(const EC2N &ec)
Set the elliptic curve.
Definition ec2n.h:119
const EC2N & GetCurve() const
Get the elliptic curve.
Definition ec2n.h:124
void DEREncodeElement(BufferedTransformation &bt, const Element &v) const
Encodes element in DER format.
Definition ec2n.h:114
Elliptic Curve precomputation.
Definition ec2n.h:99
Abstract class for encoding and decoding ellicptic curve points.
Definition ecpoint.h:91
GF(2^n) with Polynomial Basis.
Definition gf2n.h:297
Multiple precision integer with arithmetic operations.
Definition integer.h:50
static Integer CRYPTOPP_API Power2(size_t e)
Exponentiates to a power of 2.
Definition integer.cpp:3087
Interface for random number generators.
Definition cryptlib.h:1435
A pointer which can be copied and cloned.
Definition smartptr.h:105
Abstract base classes that provide a uniform interface to this library.
Classes for Elliptic Curve points.
Classes for precomputation in a group.
Classes and functions for schemes over GF(2^n)
Multiple precision integer with arithmetic operations.
This file contains helper classes/functions for implementing public key algorithms.
Classes for automatic resource management.
Elliptical Curve Point over GF(2^n)
Definition ecpoint.h:54