summaryrefslogtreecommitdiff
path: root/ec2n.h
diff options
context:
space:
mode:
authorweidai <weidai11@users.noreply.github.com>2002-10-04 17:31:41 +0000
committerweidai <weidai11@users.noreply.github.com>2002-10-04 17:31:41 +0000
commita3b6ece7ab341b5b14135baeccea7d5e4c086771 (patch)
tree8b045309c238226c32a563b1df6b9c30a2f0e0b3 /ec2n.h
downloadcryptopp-git-a3b6ece7ab341b5b14135baeccea7d5e4c086771.tar.gz
Initial revision
Diffstat (limited to 'ec2n.h')
-rw-r--r--ec2n.h105
1 files changed, 105 insertions, 0 deletions
diff --git a/ec2n.h b/ec2n.h
new file mode 100644
index 00000000..c2177e76
--- /dev/null
+++ b/ec2n.h
@@ -0,0 +1,105 @@
+#ifndef CRYPTOPP_EC2N_H
+#define CRYPTOPP_EC2N_H
+
+#include "gf2n.h"
+#include "eprecomp.h"
+#include "smartptr.h"
+#include "pubkey.h"
+
+NAMESPACE_BEGIN(CryptoPP)
+
+//! Elliptic Curve Point
+struct EC2NPoint
+{
+ EC2NPoint() : identity(true) {}
+ EC2NPoint(const PolynomialMod2 &x, const PolynomialMod2 &y)
+ : identity(false), x(x), y(y) {}
+
+ bool operator==(const EC2NPoint &t) const
+ {return (identity && t.identity) || (!identity && !t.identity && x==t.x && y==t.y);}
+ bool operator< (const EC2NPoint &t) const
+ {return identity ? !t.identity : (!t.identity && (x<t.x || (x==t.x && y<t.y)));}
+
+ bool identity;
+ PolynomialMod2 x, y;
+};
+
+//! Elliptic Curve over GF(2^n)
+class EC2N : public AbstractGroup<EC2NPoint>
+{
+public:
+ typedef GF2NP Field;
+ typedef Field::Element FieldElement;
+ typedef EC2NPoint Point;
+
+ EC2N() {}
+ EC2N(const Field &field, const Field::Element &a, const Field::Element &b)
+ : m_field(field), m_a(a), m_b(b) {}
+ // construct from BER encoded parameters
+ // this constructor will decode and extract the the fields fieldID and curve of the sequence ECParameters
+ EC2N(BufferedTransformation &bt);
+
+ // encode the fields fieldID and curve of the sequence ECParameters
+ 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, unsigned int len) const;
+ bool DecodePoint(Point &P, const byte *encodedPoint, unsigned int 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;}
+
+private:
+ clonable_ptr<Field> m_field;
+ FieldElement m_a, m_b;
+ mutable Point m_R;
+};
+
+template <class T> class EcPrecomputation;
+
+//! .
+template<> class EcPrecomputation<EC2N> : public DL_GroupPrecomputation<EC2N::Point>
+{
+public:
+ typedef EC2N EllipticCurve;
+
+ // 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);}
+
+ // non-inherited
+ void SetCurve(const EC2N &ec) {m_ec = ec;}
+ const EC2N & GetCurve() const {return m_ec;}
+
+private:
+ EC2N m_ec;
+};
+
+NAMESPACE_END
+
+#endif