sharp-biginteger/BigIntegerTest/Program.cs

293 lines
9.6 KiB
C#

using System;
using sharp.extensions;
using BigInt;
using Crypto.EC;
using System.Runtime.CompilerServices;
namespace BigIntegerTest
{
class MainClass
{
public static void Main(string[] args)
{
testIntegerBase();
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()
{
UInteger yG = UInteger.fromHexString("483ADA7726A3C4655DA4FBFC0E1108A8FD17B448A68554199C47D08FFB10D4B8");
EllipticCurve ec = EllipticCurve.createSecp256k1();
Console.WriteLine("{0}", ec);
CurvePoint G2 = ec.G + ec.G;
Console.WriteLine("G + G = {0}", G2.toHexString());
Console.WriteLine("on Curve? {0}", ec.isOnCurve(G2));
Console.WriteLine();
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()
//{
// UInt256 a, b, c;
// a = UInt256.fromHexString("8844220000000000112244880000004444");
// b = 2;
// c = UInt256.fromHexString("8844220000000000000000000000000000");
// Console.WriteLine("UInt256: a = {0}", a);
// Console.WriteLine("UInt256: a << 32 = {0}", a << 32);
// Console.WriteLine("UInt256: a >> 32 = {0}", a >> 32);
// Console.WriteLine("UInt256: b = {0}", b);
// Console.WriteLine("UInt256: a * b = {0}", a * b);
// Console.WriteLine("UInt256: a / b = {0}", a / b);
// Console.WriteLine("UInt256: c = {0}", c);
// Console.WriteLine("UInt256: a % c = {0}", a % c);
// Console.WriteLine("UInt256: a * a = {0}", a * a);
// Console.WriteLine("UInt256: a ^ 0 = {0}", a.Pow(0));
// Console.WriteLine("UInt256: a ^ 1 = {0}", a.Pow(1));
// Console.WriteLine("UInt256: a ^ 2 = {0}", a.Pow(2));
// Console.WriteLine();
//}
public static void testInverseMod(UInteger a,UInteger m){
Console.WriteLine("UInteger: a = {0}",a);
Console.WriteLine("UInteger: m = {0}",m);
UInteger inv = Euclid.inverse(a, m);
if (inv == null){
Console.WriteLine("UInteger: no inverse found for a [ mod m ]");
} else {
Console.WriteLine("UInteger: inv(a) [ mod m] = {0}", inv);
Console.WriteLine("UInteger: inv(a) * a = {0}", inv * a);
Console.WriteLine("UInteger: inv(a) * a [ mod m] = {0}", (inv * a) % m);
}
Console.WriteLine();
}
public static void testIntegerConversion(){
UInteger ua, ub;
Integer ia, ib;
ua = UInteger.fromHexString("12345678ABCDEF00");
ub = UInteger.fromHexString("9876543210FEDCBA");
Console.WriteLine("Unsigned a = {0}",ua);
Console.WriteLine("Unsigned b = {0}",ub);
ia = ua;
ib = ub;
Console.WriteLine("Signed a = {0}",ia);
Console.WriteLine("Signed b = {0}",ib);
ua = ia;
ub = ib;
Console.WriteLine("Unsigned a = {0}",ua);
Console.WriteLine("Unsigned b = {0}",ub);
Console.WriteLine("");
}
public static void testIntegerBase(){
Integer a, b, c, d;
a = Integer.fromHexString("08432100");
b = Integer.fromHexString("00000100");
c = Integer.fromHexString("FFFFFFFFFFFFEF22");
d = Integer.fromHexString("80010080");
Console.WriteLine("Integer: a = {0}", a);
Console.WriteLine("Integer: b = {0}", b);
Console.WriteLine("Integer: c = {0}", c);
Console.WriteLine("Integer: log2(a) = {0}", a.Log2());
Console.WriteLine("Integer: log2(b) = {0}", b.Log2());
Console.WriteLine("Integer: log2(c) = {0}", c.Log2());
Console.WriteLine("Integer: c - a = {0}", c - a);
Console.WriteLine("Integer: c + a = {0}", c + a);
Console.WriteLine("Integer: c - b = {0}", c - b);
Console.WriteLine("Integer: c + b = {0}", c + b);
Console.WriteLine("Integer: a * b = {0}", a * b);
Console.WriteLine("Integer: b * a = {0}", b * a);
Console.WriteLine("Integer: b * c = {0}", b * c);
Console.WriteLine("Integer: c * b = {0}", c * b);
Console.WriteLine();
Console.WriteLine("Integer: a << 12 = {0}", a << 12);
Console.WriteLine("Integer: b << 12 = {0}", b << 12);
Console.WriteLine("Integer: c << 12 = {0}", c << 12);
Console.WriteLine("Integer: a >> 12 = {0}", a >> 12);
Console.WriteLine("Integer: b >> 12 = {0}", b >> 12);
Console.WriteLine("Integer: c >> 12 = {0}", c >> 12);
Console.WriteLine("Integer: d = {0}", d);
Console.WriteLine("Integer: d >> 12 = {0}", d >> 12);
Console.WriteLine();
Console.WriteLine("Integer: a = {0}", a);
Console.WriteLine("Integer: -a = {0}", a.Twos());
Console.WriteLine("Integer: b = {0}", b);
Console.WriteLine("Integer: -b = {0}", b.Twos());
Console.WriteLine("Integer: c = {0}", c);
Console.WriteLine("Integer: -c = {0}", c.Twos());
Console.WriteLine("Integer: c / b = {0}", c / b);
}
public static void testUInteger()
{
UInteger a, b, c, d;
a = UInteger.fromHexString("08432100");
b = UInteger.fromHexString("00000100");
c = UInteger.fromHexString("FFFFFFFFFFFFEF22");
d = UInteger.fromHexString("80010080");
Console.WriteLine("UInteger: a = {0}", a);
Console.WriteLine("UInteger: b = {0}", b);
Console.WriteLine("UInteger: c = {0}", c);
Console.WriteLine("UInteger: log2(a) = {0}", a.Log2());
Console.WriteLine("UInteger: log2(b) = {0}", b.Log2());
Console.WriteLine("UInteger: log2(c) = {0}", c.Log2());
Console.WriteLine("UInteger: c - a = {0}", c - a);
Console.WriteLine("UInteger: c + a = {0}", c + a);
Console.WriteLine("UInteger: c - b = {0}", c - b);
Console.WriteLine("UInteger: c + b = {0}", c + b);
Console.WriteLine("UInteger: a * b = {0}", a* b);
Console.WriteLine("UInteger: b * a = {0}", b* a);
Console.WriteLine("UInteger: b * c = {0}", b* c);
Console.WriteLine("UInteger: c * b = {0}", c* b);
Console.WriteLine();
Console.WriteLine("UInteger: a << 12 = {0}", a << 12);
Console.WriteLine("UInteger: b << 12 = {0}", b << 12);
Console.WriteLine("UInteger: c << 12 = {0}", c << 12);
Console.WriteLine("UInteger: a >> 12 = {0}", a >> 12);
Console.WriteLine("UInteger: b >> 12 = {0}", b >> 12);
Console.WriteLine("UInteger: c >> 12 = {0}", c >> 12);
Console.WriteLine("UInteger: d = {0}", d);
Console.WriteLine("UInteger: d >> 12 = {0}", d >> 12);
Console.WriteLine();
Console.WriteLine("UInteger: a = {0}", a);
Console.WriteLine("UInteger: -a = {0}", a.Twos());
Console.WriteLine("UInteger: b = {0}", b);
Console.WriteLine("UInteger: -b = {0}", b.Twos());
Console.WriteLine("UInteger: c = {0}", c);
Console.WriteLine("UInteger: -c = {0}", c.Twos());
Console.WriteLine("UInteger: c / b = {0}", c / b);
a = new UInteger(5);
b = new UInteger(17);
testInverseMod(a, b);
a = new UInteger(34);
b = new UInteger(73);
testInverseMod(a,b);
a = UInteger.fromHexString("01dd53fb");
b = UInteger.fromHexString("9FFFEFF3");
testInverseMod(a,b);
a = UInteger.fromHexString("F1dd53fb");
b = UInteger.fromHexString("FFFFEFF3");
testInverseMod(a, b);
a = UInteger.fromHexString("FFF1dd53fb");
b = UInteger.fromHexString("FFFFFFEFF3");
testInverseMod(a, b);
a = UInteger.fromHexString("029BFCDB2DCE28D959F2815B16F81798");
b = UInteger.fromHexString("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F");
testInverseMod(a,b);
a = UInteger.fromHexString("79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798");
b = UInteger.fromHexString("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F");
testInverseMod(a,b);
}
}
}