sharp-crypto/EC/CurvePoint.cs

90 lines
1.6 KiB
C#

using System;
using BigInt;
using System.Text;
using System.Runtime.CompilerServices;
namespace Crypto.EC
{
public class CurvePoint
{
public CurvePoint(EllipticCurve curve,UInteger x,UInteger y){
Curve = curve;
X = x;
Y = y;
}
public string toHexString(){
StringBuilder sb = new StringBuilder();
sb.Append(X.toHexString());
sb.Append(".");
sb.Append(Y.toHexString());
return sb.ToString();
}
public EllipticCurve Curve { get; private set; }
public UInteger X { get; private set; }
public UInteger Y { get; private set; }
public bool isOnCurve(){
return this.Curve.isOnCurve(this);
}
public static CurvePoint operator +(CurvePoint p1, CurvePoint p2)
{
return p1.Curve.Add(p1,p2);
}
public static CurvePoint operator *(CurvePoint p1, UInteger n)
{
if (p1.Y == UInteger.ZERO){
throw new NotImplementedException("Eternity point not implemented");
}
CurvePoint result = null;
CurvePoint p = p1;
int i;
for (i = 0; i < 256; i++)
{
if (n[i]){
result = p;
break;
}
p += p;
}
for (; i < 256; i++)
{
p += p;
if (n[i]){
result += p;
}
}
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;
}
}
}
}