secp256k1/group.h

113 lines
2.5 KiB
C
Raw Normal View History

2013-03-08 00:20:41 +00:00
#ifndef _SECP256K1_GROUP_
#define _SECP256K1_GROUP_
2013-03-16 14:51:55 +00:00
#include <string>
#include "num.h"
2013-03-08 00:20:41 +00:00
#include "field.h"
namespace secp256k1 {
2013-03-09 21:47:40 +00:00
class GroupElemJac;
2013-03-08 00:20:41 +00:00
/** Defines a point on the secp256k1 curve (y^2 = x^3 + 7) */
class GroupElem {
protected:
bool fInfinity;
FieldElem x;
FieldElem y;
public:
/** Creates the point at infinity */
2013-03-16 14:51:55 +00:00
GroupElem();
2013-03-08 00:20:41 +00:00
/** Creates the point with given affine coordinates */
2013-03-16 14:51:55 +00:00
GroupElem(const FieldElem &xin, const FieldElem &yin);
2013-03-08 00:20:41 +00:00
/** Checks whether this is the point at infinity */
2013-03-16 14:51:55 +00:00
bool IsInfinity() const;
2013-03-08 00:20:41 +00:00
2013-03-16 14:51:55 +00:00
void SetNeg(const GroupElem &p);
2013-03-08 00:20:41 +00:00
2013-03-16 14:51:55 +00:00
void GetX(FieldElem &xout);
2013-03-10 04:34:04 +00:00
2013-03-16 14:51:55 +00:00
void GetY(FieldElem &yout);
2013-03-10 04:34:04 +00:00
2013-03-16 14:51:55 +00:00
std::string ToString() const;
2013-03-08 00:20:41 +00:00
void SetJac(GroupElemJac &jac);
2013-03-09 21:47:40 +00:00
2013-03-08 00:20:41 +00:00
friend class GroupElemJac;
};
/** Represents a point on the secp256k1 curve, with jacobian coordinates */
2013-03-10 00:49:42 +00:00
class GroupElemJac : private GroupElem {
2013-03-08 00:20:41 +00:00
protected:
FieldElem z;
public:
/** Creates the point at infinity */
2013-03-16 14:51:55 +00:00
GroupElemJac();
2013-03-08 00:20:41 +00:00
/** Creates the point with given affine coordinates */
2013-03-16 14:51:55 +00:00
GroupElemJac(const FieldElem &xin, const FieldElem &yin);
2013-03-08 00:20:41 +00:00
2013-03-16 14:51:55 +00:00
GroupElemJac(const GroupElem &in);
2013-03-09 21:47:40 +00:00
2013-03-18 01:41:01 +00:00
void SetJac(const GroupElemJac &jac);
void SetAffine(const GroupElem &aff);
2013-03-09 21:47:40 +00:00
2013-03-08 00:20:41 +00:00
/** Checks whether this is a non-infinite point on the curve */
2013-03-16 14:51:55 +00:00
bool IsValid() const;
2013-03-08 00:20:41 +00:00
/** Returns the affine coordinates of this point */
2013-03-16 14:51:55 +00:00
void GetAffine(GroupElem &aff);
void GetX(FieldElem &xout);
void GetY(FieldElem &yout);
bool IsInfinity() const;
void SetNeg(const GroupElemJac &p);
2013-03-09 21:47:40 +00:00
2013-03-08 00:20:41 +00:00
/** Sets this point to have a given X coordinate & given Y oddness */
2013-03-16 14:51:55 +00:00
void SetCompressed(const FieldElem &xin, bool fOdd);
2013-03-08 00:20:41 +00:00
/** Sets this point to be the EC double of another */
2013-03-16 14:51:55 +00:00
void SetDouble(const GroupElemJac &p);
2013-03-08 00:20:41 +00:00
/** Sets this point to be the EC addition of two others */
2013-03-16 14:51:55 +00:00
void SetAdd(const GroupElemJac &p, const GroupElemJac &q);
2013-03-08 00:20:41 +00:00
/** Sets this point to be the EC addition of two others (one of which is in affine coordinates) */
2013-03-16 14:51:55 +00:00
void SetAdd(const GroupElemJac &p, const GroupElem &q);
std::string ToString() const;
2013-03-10 03:24:00 +00:00
void SetMulLambda(const GroupElemJac &p);
2013-03-08 00:20:41 +00:00
};
2013-03-09 21:47:40 +00:00
class GroupConstants {
private:
const FieldElem g_x;
const FieldElem g_y;
public:
const Number order;
const GroupElem g;
2013-03-10 03:24:00 +00:00
const FieldElem beta;
const Number lambda, a1b2, b1, a2;
2013-03-09 21:47:40 +00:00
2013-03-16 14:51:55 +00:00
GroupConstants();
2013-03-09 21:47:40 +00:00
};
2013-03-16 14:51:55 +00:00
const GroupConstants &GetGroupConst();
2013-03-10 03:24:00 +00:00
2013-03-16 14:51:55 +00:00
void SplitExp(const Number &exp, Number &exp1, Number &exp2);
2013-03-10 03:24:00 +00:00
2013-03-08 00:20:41 +00:00
}
#endif