262 lines
8.5 KiB
C#
262 lines
8.5 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();
|
|
}
|
|
|
|
public static void testEC()
|
|
{
|
|
UInteger yG = UInteger.fromHexString("483ADA7726A3C4655DA4FBFC0E1108A8FD17B448A68554199C47D08FFB10D4B8");
|
|
|
|
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())
|
|
}
|
|
*/
|
|
}
|
|
|
|
//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);
|
|
}
|
|
|
|
|
|
}
|
|
}
|