sharp-crypto/EC/EllipticCurve.cs

147 lines
3.8 KiB
C#

using System;
using BigInt;
using System.Text;
using sharp.extensions;
namespace Crypto.EC
{
public class EllipticCurve
{
public UInteger a { get; private set; }
public UInteger b { get; private set; }
public UInteger n { get; private set; }
public int h { get; private set; }
public CurvePoint G { get; private set; }
public IntField Fp { get; private set; }
public EllipticCurve(UInteger p,UInteger a,UInteger b,UInteger xG,UInteger yG,UInteger n,int h)
{
this.Fp = new IntField(p);
this.a = a;
this.b = b;
this.n = n;
this.h = h;
this.G = new CurvePoint(this, xG, yG);
if (!isOnCurve(this.G)){
throw new ArgumentException("G is not part of the curve");
}
}
public UInteger Y2(UInteger x){
UInteger yy = x;
Console.WriteLine("Y(x): X = {0}",x.toHexString());
Console.WriteLine("Y(x): x^2 = {0}",yy.Pow(2).toHexString());
yy = yy.Pow(3);
Console.WriteLine("Y(x): x^3 = {0}",yy.toHexString());
yy += (a * x);
Console.WriteLine("Y(x): x^3 + ax = {0}",yy.toHexString());
yy += b;
Console.WriteLine("Y(x) x^3 + ax + b = {0}",yy.toHexString());
yy %= this.Fp.FieldModulo;
Console.WriteLine("Y(x) mod p = {0}",yy.toHexString());
return yy;
}
public bool isOnCurve(CurvePoint p){
UInteger py2 = p.Y.Pow(2);
UInteger pcy2 = Y2(p.X);
Console.WriteLine("CHECK A: {0}",py2.toHexString());
Console.WriteLine("CHECK B: {0}",pcy2.toHexString());
return (py2 == pcy2);
}
public CurvePoint Add(CurvePoint p1, CurvePoint p2)
{
if (((object)p1 == null) || ((object)p2 == null))
{
throw new ArgumentException("CurvePoints to be added must not be null");
}
if (p1.Curve != p2.Curve)
{
throw new ArgumentException("Curvepoints to be added must belong to the same curve!");
}
if (p1 == p2)
{
UInteger s = p1.X.Pow(2);
s *= 3;
s += p1.Curve.a;
s /= (p1.Y << 1);
//(((p1.X.Pow(2) * 3) + p1.Curve.a) / (p1.Y << 1));
UInteger xR = (s.Pow(2) - (p1.X << 1));
UInteger yR = ((s * (p1.X - xR)) - p1.Y);
return new CurvePoint(p1.Curve, xR, yR);
}
if (p1.X == p2.X)
{
throw new NotImplementedException("Eternity point not implemented");
}
else
{
UInteger s = (((p1.Y - p2.Y) / (p1.X - p2.X)));
UInteger xR = (s.Pow(2) - p1.X - p2.X);
UInteger yR = ((s * (p1.X - xR)) - p1.Y);
return new CurvePoint(p1.Curve, xR, yR);
}
throw new NotImplementedException("Point addition not yet implemented");
}
/* public CurvePoint getPoint(T x){
x = new T(x, this.Fp);
return new CurvePoint(this,x,Y(x));
}
*/
public override string ToString()
{
StringBuilder sb = new StringBuilder();
sb.AppendFormat("[EllipticCurve] Fp={0}\n",(Fp == null) ? "" : Fp.ToString());
sb.AppendFormat("[EllipticCurve] a={0}\n",a.toHexString());
sb.AppendFormat("[EllipticCurve] b={0}\n",b.toHexString());
sb.AppendFormat("[EllipticCurve] Gx={0}\n",G.X.toHexString());
sb.AppendFormat("[EllipticCurve] Gy={0}\n",G.Y.toHexString());
sb.AppendFormat("[EllipticCurve] G on curve? {0}",isOnCurve(G));
return sb.ToString();
}
public static EllipticCurve createSecp256k1(){
UInteger p = (UInteger.ONE << 256);
p -= UInteger.ONE;
p -= (UInteger.ONE << 32);
p -= (UInteger.ONE << 9);
p -= (UInteger.ONE << 8);
p -= (UInteger.ONE << 7);
p -= (UInteger.ONE << 6);
p -= (UInteger.ONE << 4);
UInteger xG = UInteger.fromHexString("79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798");
UInteger yG = UInteger.fromHexString("483ADA7726A3C4655DA4FBFC0E1108A8FD17B448A68554199C47D08FFB10D4B8");
UInteger n = UInteger.fromHexString("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141");
return new EllipticCurve(p, new UInteger(0), new UInteger(7), xG, yG, n, 1);
}
}
}