diff --git a/BigIntMath.cs b/BigIntMath.cs index f4bbd7d..f7933fe 100644 --- a/BigIntMath.cs +++ b/BigIntMath.cs @@ -93,14 +93,21 @@ namespace BigInt **/ public static UInt32[] reduceSigned(UInt32[] value){ int n = value.Length; + + //Console.WriteLine("reduceSigned(): < {0}", value.getBytes().Reverse().toHexString()); + for (; n > 1; n--){ if ( - (value[n-1] != 0) || ((value[n-2] & 1<<31)!=0) + ((value[n-1] != 0) || ((value[n-2] & (1<<31))!=0)) && ((value[n-1] != 0xFFFFFFFF) || ((value[n-2] & (1<<31))==0)) ){ break; } } - return value.Segment(0,n); + value = value.Segment(0, n); + + //Console.WriteLine("reduceSigned(): > {0}", value.getBytes().Reverse().toHexString()); + + return value; } /** diff --git a/BigIntegerTest/Program.cs b/BigIntegerTest/Program.cs index 5d06e83..c55ea0f 100644 --- a/BigIntegerTest/Program.cs +++ b/BigIntegerTest/Program.cs @@ -1,4 +1,4 @@ -using System; +using System; using sharp.extensions; using BigInt; using Crypto.EC; @@ -14,6 +14,34 @@ namespace BigIntegerTest testIntegerConversion(); testUInteger(); testEC(); + + Console.WriteLine(); + + // Mögliche Punkt G: 1 / 59 , 38 / 55 + + EllipticCurve curve61 = new EllipticCurve(61, 0, 3, 38, 55, 13, 1); + Console.WriteLine("Curve61:"); + Console.WriteLine(curve61); + + CurvePoint p = curve61.G * 2; + + for (int n = 1; (n < 3) || (p != curve61.G);n++){ + p = curve61.G * n; + Console.WriteLine("{0,2} * G = {1}", n, p); + } + + ProjectiveCurvePoint pg = curve61.G; + ProjectiveCurvePoint pg2 = pg + pg; + ProjectiveCurvePoint pgb = pg2 - pg; + + Console.WriteLine("Projektiv: G = {0} => {1}",pg,pg.toCurvePoint()); + Console.WriteLine("Projektiv: G + G = {0} => {1}",pg2,pg2.toCurvePoint()); + Console.WriteLine("Projektiv: (G + G) - G = {0} => {1}",pgb,pgb.toCurvePoint()); + + for (int n = 1; (n< 3) || (pgb != ProjectiveCurvePoint.INFINITY);n++){ + pgb = pg * n; + Console.WriteLine("{0,2} * G = {1} => {2}", n, pgb, pgb.toCurvePoint() ); + } } public static void testEC() @@ -22,23 +50,26 @@ namespace BigIntegerTest EllipticCurve ec = EllipticCurve.createSecp256k1(); Console.WriteLine("{0}", ec); - UInteger yy = yG.Pow(2) % ec.Fp.FieldModulo; //ec.G.Y.Pow(2); - - Console.WriteLine("Gy^2 = {0}", yy.toHexString()); - Console.WriteLine("Y2(Gx) = {0}", ec.Y2(ec.G.X)); - Console.WriteLine(); CurvePoint G2 = ec.G + ec.G; Console.WriteLine("G + G = {0}", G2.toHexString()); Console.WriteLine("on Curve? {0}", ec.isOnCurve(G2)); Console.WriteLine(); -/* - for (int i = 1; i < 6; i++){ - CurvePoint p = ec.G * i; - Console.WriteLine("G({0}) = {1}",i,p.toHexString()) + CurvePoint p = ec.G; + + for (int i = 2; i < 16; i++){ + p = ec.G + p; + Console.WriteLine("{0,3} * G = {1}", i, p.toHexString()); + Console.WriteLine("{0,3} * G = {1}", i, (ec.G * i).toHexString()); } - */ + + CurvePoint p3 = ec.G * 3; + + Console.WriteLine("3 * G = {0}",p3.ToString()); + Console.WriteLine("(3 * G) - G = {0}",(p3 - ec.G).ToString()); + Console.WriteLine("(3 * G) - G == 2 * G ? {0}",(p3 - ec.G) == (ec.G * 2)); + } //public static void testUInt256() diff --git a/Euclid.cs b/Euclid.cs index 9d7c29f..80d6b32 100644 --- a/Euclid.cs +++ b/Euclid.cs @@ -40,6 +40,7 @@ namespace BigInt act.r - (q * next.r), act.t - (q * next.t) ); + //Console.WriteLine("EUCLID: q = {0}",q); //Console.WriteLine("EUCLID: act: r = {0} / t = {1}",act.r,act.t); //Console.WriteLine("EUCLID: next: r = {0} / t = {1}",next.r,next.t); diff --git a/IntField.cs b/IntField.cs index e679e69..89fce9a 100644 --- a/IntField.cs +++ b/IntField.cs @@ -5,22 +5,47 @@ namespace BigInt public class IntField { public static IntField Default { get; set; } = null; // new IntField(UBigInteger.ZERO.Resize(256) - 1); + public static IntField INFINITY { get; private set; } = new IntField(); - public UInteger FieldModulo { get; private set; } - public int FieldWidth { get; private set; } + public Integer FieldModulo { get; private set; } - public IntField(UInteger p,int width){ - this.FieldWidth = width; - this.FieldModulo = p; - } public IntField(UInteger p){ this.FieldModulo = p; - this.FieldWidth = p.RawValue.Length << 5; + } + public IntField(Integer p){ + this.FieldModulo = p; } - public UInteger Fit(UInteger value){ - return value % FieldModulo; - } + private IntField(){ + this.FieldModulo = 0; + } + + public Integer Fit(Integer value) + { + if (FieldModulo.isZero()){ + return value; + } + + value %= FieldModulo; + if (value.Sign()) + { + value += FieldModulo; + } + return value; + } + public UInteger Fit(UInteger value) + { + if (FieldModulo.isZero()){ + return value; + } + + value %= (UInteger)FieldModulo; + return value; + } + + public Integer AdditiveInverse(Integer value){ + return FieldModulo - value; + } public override string ToString(){ return String.Format("[IntField p={0}]",this.FieldModulo); diff --git a/Integer.cs b/Integer.cs index ca8d892..7249e49 100644 --- a/Integer.cs +++ b/Integer.cs @@ -120,7 +120,8 @@ namespace BigInt private Integer __op_mod(Integer b) { - return null; + UInt32[] result = BigIntMath.smod(rawValue, b.RawValue); + return new Integer(result); } private Integer __op_mul(Integer b) @@ -171,11 +172,6 @@ namespace BigInt { return a.__op_mul(b); } - public static Integer operator *(Integer a, UInt32 b) - { - Integer bb = new Integer(new UInt32[] { b }); - return a.__op_mul(bb); - } public static Integer operator /(Integer a, Integer b) { diff --git a/UInteger.cs b/UInteger.cs index 38c9e48..7f210d0 100644 --- a/UInteger.cs +++ b/UInteger.cs @@ -175,11 +175,6 @@ namespace BigInt { return a.__op_mul(b); } - public static UInteger operator *(UInteger a, UInt32 b) - { - UInteger bb = a.__op_new(new UInt32[] { b }); - return a.__op_mul(bb); - } public static UInteger operator /(UInteger a, UInteger b) {