149 lines
2.7 KiB
C#
149 lines
2.7 KiB
C#
using System;
|
|
using BigInt;
|
|
using System.Text;
|
|
using System.Runtime.CompilerServices;
|
|
|
|
namespace Crypto.EC
|
|
{
|
|
public class CurvePoint
|
|
{
|
|
public static readonly CurvePoint INFINITY = new CurvePoint();
|
|
|
|
public CurvePoint(EllipticCurve curve,Integer x,Integer y){
|
|
Curve = curve;
|
|
X = x;
|
|
Y = y;
|
|
}
|
|
|
|
private CurvePoint(){
|
|
this.Curve = null;
|
|
this.X = 0;
|
|
this.Y = 0;
|
|
}
|
|
|
|
public string toHexString()
|
|
{
|
|
StringBuilder sb = new StringBuilder();
|
|
sb.Append(((UInteger)X).toHexString());
|
|
sb.Append(".");
|
|
sb.Append(((UInteger)Y).toHexString());
|
|
return sb.ToString();
|
|
}
|
|
|
|
public override string ToString()
|
|
{
|
|
if ((object)this == (object)INFINITY){
|
|
return String.Format("[CurvePoint INFINITY]");
|
|
}
|
|
return String.Format("[CurvePoint X={0} Y={1}]",X.ToString(),Y.ToString());
|
|
}
|
|
|
|
public EllipticCurve Curve { get; private set; }
|
|
public Integer X { get; private set; }
|
|
public Integer Y { get; private set; }
|
|
|
|
public bool isOnCurve(){
|
|
return this.Curve.isOnCurve(this);
|
|
}
|
|
|
|
public bool isInfinity(){
|
|
return ((object)this == (object)INFINITY);
|
|
}
|
|
|
|
public CurvePoint Negated(){
|
|
return new CurvePoint(Curve, X, Curve.Fp.AdditiveInverse(Y));
|
|
}
|
|
|
|
public static CurvePoint operator +(CurvePoint p1, CurvePoint p2)
|
|
{
|
|
if ((object)p1 == (object)INFINITY){
|
|
return p2;
|
|
}
|
|
if ((object)p2 == (object)INFINITY){
|
|
return p1;
|
|
}
|
|
|
|
return p1.Curve.Add(p1, p2);
|
|
}
|
|
|
|
public static CurvePoint operator -(CurvePoint p1, CurvePoint p2)
|
|
{
|
|
if ((object)p1 == (object)INFINITY){
|
|
return p2;
|
|
}
|
|
if ((object)p2 == (object)INFINITY){
|
|
return p1;
|
|
}
|
|
return p1.Curve.Add(p1, p2.Negated());
|
|
}
|
|
|
|
public static CurvePoint operator *(CurvePoint p1, Integer n)
|
|
{
|
|
if ((object)p1 == (object)INFINITY){
|
|
return INFINITY;
|
|
}
|
|
|
|
if (p1.Y == Integer.ZERO){
|
|
return INFINITY;
|
|
}
|
|
|
|
if (n.isZero()){
|
|
throw new NotImplementedException("CurevPoint * 0");
|
|
}
|
|
|
|
CurvePoint result = null;
|
|
CurvePoint p = p1;
|
|
|
|
int i;
|
|
int bitwidth = n.Log2();
|
|
|
|
if (bitwidth < 0){
|
|
bitwidth = -bitwidth;
|
|
}
|
|
|
|
for (i = 0; i < bitwidth; i++)
|
|
{
|
|
if (n[i])
|
|
break;
|
|
|
|
p += p;
|
|
}
|
|
|
|
result = p;
|
|
|
|
for (i++; i <= bitwidth; i++)
|
|
{
|
|
p += p;
|
|
if (n[i]){
|
|
result = p + result;
|
|
}
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
public static bool operator ==(CurvePoint p1, CurvePoint p2)
|
|
{
|
|
if (((object)p1 == null)||((object)p2==null)){
|
|
return false;
|
|
}
|
|
return (p1.X == p2.X) && (p1.Y == p2.Y);
|
|
}
|
|
public static bool operator !=(CurvePoint p1, CurvePoint p2)
|
|
{
|
|
return (p1.X != p2.X) || (p1.Y != p2.Y);
|
|
}
|
|
|
|
public override bool Equals(object obj)
|
|
{
|
|
if (this.GetType().IsInstanceOfType(obj)){
|
|
return (this == (CurvePoint)obj);
|
|
} else {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
|
|
}
|
|
}
|