ln.biginteger/ln.biginteger.test/UnitTest1.cs

382 lines
13 KiB
C#

using NUnit.Framework;
using System;
using System.Security.Cryptography;
namespace ln.biginteger.test
{
public class Tests
{
[SetUp]
public void Setup()
{
}
[Test]
public void Test_00_LowLevel()
{
UInt32[] a,b,c;
Int32 d;
a = new UInt32[]{ 0x00000000, 0x00000000 };
d = BigInteger.big_get_bitlength( a, a.Length );
Assert.AreEqual( 1 , d );
a = new UInt32[]{ 0x00000001, 0x00000000 };
d = BigInteger.big_get_bitlength( a, a.Length );
Assert.AreEqual( 2 , d );
a = new UInt32[]{ 0x00001000, 0x00000000 };
d = BigInteger.big_get_bitlength( a, a.Length );
Assert.AreEqual( 14 , d );
a = new UInt32[]{ 0xFFFF8000, 0x00000000 };
d = BigInteger.big_get_bitlength( a, a.Length );
Assert.AreEqual( 33 , d );
a = new UInt32[]{ 0xFFFF8000, 0xFFFFFFFF };
d = BigInteger.big_get_bitlength( a, a.Length );
Assert.AreEqual( 16 , d );
a = new UInt32[]{ 0xFC000000 };
d = BigInteger.big_get_bitlength( a, a.Length );
Assert.AreEqual( 27 , d );
a = new uint[]{0};
b = new uint[]{0, 0};
c = BigInteger.Extend(a, b.Length);
Assert.AreEqual(b, c);
a = new uint[]{0};
b = new uint[]{0, 0, 0, 0};
c = BigInteger.Extend(a, b.Length);
Assert.AreEqual(b, c);
a = new uint[]{1};
b = new uint[]{1, 0, 0, 0};
c = BigInteger.Extend(a, b.Length);
Assert.AreEqual(b, c);
a = new uint[]{5463782};
b = new uint[]{5463782, 0, 0, 0};
c = BigInteger.Extend(a, b.Length);
Assert.AreEqual(b, c);
a = new uint[]{ 0xFFFFEDCB };
b = new uint[]{ 0xFFFFEDCB, 0xFFFFFFFF};
c = BigInteger.Extend(a, b.Length);
Assert.AreEqual(b, c);
a = new uint[]{ 0xFFFFEDCB };
b = new uint[]{ 0xFFFFEDCB, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF};
c = BigInteger.Extend(a, b.Length);
Assert.AreEqual(b, c);
a = new uint[]{ 0xFFFFEDCB, 0xFFFFFFFF };
b = new uint[]{ 0xFFFFEDCB, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF};
c = BigInteger.Extend(a, b.Length);
Assert.AreEqual(b, c);
a = new uint[]{ 0xFFFFFFFF };
b = new uint[]{ 0x00000001 };
BigInteger.big_twos( a, a.Length );
Assert.AreEqual(b, a);
a = new uint[]{ 0xFFFFFFFF, 0xFFFFFFFF };
b = new uint[]{ 0x00000001, 0x00000000 };
BigInteger.big_twos( a, a.Length );
Assert.AreEqual(b, a);
a = new uint[]{ 0x01000020 };
b = new uint[]{ 0x00100002 };
BigInteger.big_shr( a, a.Length, 4);
Assert.AreEqual(b, a);
a = new uint[]{ 0x10000000 };
b = new uint[]{ 0x08000000 };
BigInteger.big_shr( a, a.Length, 1);
Assert.AreEqual(b, a);
a = new uint[]{ 0xbbccddee, 0x00000002 };
b = new uint[]{ 0x00000000, 0x00000002 };
Assert.IsTrue( BigInteger.big_cmp(a, b) > 0 );
}
[Test]
public void Test_01_LL_AddSub()
{
UInt32[] a,b,c;
a = new UInt32[]{ 0x00000000 };
b = new UInt32[]{ 0x00000000 };
c = new UInt32[]{ 0x00000000 };
BigInteger.big_add( a, b );
Assert.AreEqual( c, a );
a = new UInt32[]{ 0x00000123 };
b = new UInt32[]{ 0x00123000 };
c = new UInt32[]{ 0x00123123 };
BigInteger.big_add( a, b );
Assert.AreEqual( c, a );
a = new UInt32[]{ 0x00000123 };
b = new UInt32[]{ 0x00000123 };
c = new UInt32[]{ 0x00000246 };
BigInteger.big_add( a, b );
Assert.AreEqual( c, a );
a = new UInt32[]{ 0x00123456 };
b = new UInt32[]{ 0x00345678 };
c = new UInt32[]{ 0x00468ACE };
BigInteger.big_add( a, b );
Assert.AreEqual( c, a );
a = new UInt32[]{ 0x70004567, 0x00000000 };
b = new UInt32[]{ 0x10000000 };
c = new UInt32[]{ 0x80004567, 0x00000000 };
BigInteger.big_add( a, b );
Assert.AreEqual( c, a );
a = new UInt32[]{ 0x70004567, 0x00000000 };
b = new UInt32[]{ 0x20000000 };
c = new UInt32[]{ 0x90004567, 0x00000000 };
BigInteger.big_add( a, b );
Assert.AreEqual( c, a );
a = new UInt32[]{ 0x70004567, 0x00000000 };
b = new UInt32[]{ 0xA0000000 };
c = new UInt32[]{ 0x10004567, 0x00000001 };
BigInteger.big_add( a, b );
Assert.AreEqual( c, a );
a = new UInt32[]{ 0x00000000 };
b = new UInt32[]{ 0x00000001 };
c = new UInt32[]{ 0xFFFFFFFF };
BigInteger.big_sub( a, b );
Assert.AreEqual( c, a );
a = new UInt32[]{ 0x00000000, 0x00000000 };
b = new UInt32[]{ 0x00000001 };
c = new UInt32[]{ 0xFFFFFFFF, 0xFFFFFFFF };
BigInteger.big_sub( a, b );
Assert.AreEqual( c, a );
a = new UInt32[]{ 0x00000000, 0xF0000000 };
b = new UInt32[]{ 0x00000001 };
c = new UInt32[]{ 0xFFFFFFFF, 0xEFFFFFFF };
BigInteger.big_sub( a, b );
Assert.AreEqual( c, a );
a = new UInt32[]{ 0x54A5D32F, 0xD25AC351 };
b = new UInt32[]{ 0xC351425A, 0x432F54A5 };
c = new UInt32[]{ 0x915490D5, 0x8F2B6EAB };
BigInteger.big_sub( a, b );
Assert.AreEqual( c, a );
a = new UInt32[]{ 0x82345678, 0x00000013, 0x00000000 };
b = new UInt32[]{ 0x80000000, 0x0000000c, 0x00000000 };
c = new UInt32[]{ 0x02345678, 0x00000007, 0x00000000 };
BigInteger.big_sub( a, b );
Assert.AreEqual( c, a );
a = new UInt32[]{ 0x12345678, 0x00000013, 0x00000000 };
b = new UInt32[]{ 0x80000000, 0x0000000c, 0x00000000 };
c = new UInt32[]{ 0x92345678, 0x00000006, 0x00000000 };
BigInteger.big_sub( a, b );
Assert.AreEqual( c, a );
Assert.Pass();
}
[Test]
public void Test_02_LL_Mul()
{
UInt32[] a,b,c;
a = new UInt32[]{ 0x00000200 };
b = new UInt32[]{ 0x00000300 };
c = new UInt32[]{ 0x00060000 };
BigInteger.big_smul( a, b );
Assert.AreEqual( c, a );
a = new UInt32[]{ 0xD25AC351, 0x00000000 };
b = new UInt32[]{ 0x432F54A5 };
c = new UInt32[]{ 0x6B757735, 0x3734A555 };
BigInteger.big_smul( a, b );
Assert.AreEqual( c, a );
a = new UInt32[]{ 0xFFFFFFFF };
b = new UInt32[]{ 0x00000001 };
c = new UInt32[]{ 0xFFFFFFFF };
BigInteger.big_smul( a, b );
Assert.AreEqual( c, a );
a = new UInt32[]{ 0xFFFFFFFF };
b = new UInt32[]{ 0xFFFFFFFF };
c = new UInt32[]{ 0x00000001 };
BigInteger.big_smul( a, b );
Assert.AreEqual( c, a );
}
[Test]
public void Test_03_LL_Div()
{
UInt32[] a,b,c;
Int32 d;
a = new UInt32[]{ 0x12345678 };
b = new UInt32[]{ 0x00000001 };
c = new UInt32[]{ 0x12345678 };
BigInteger.big_divmod( a, b );
Assert.AreEqual( c, a );
a = new UInt32[]{ 0x12345678 };
b = new UInt32[]{ 0x00000032 };
c = new UInt32[]{ 0x005D34ED };
d = 0x2E;
BigInteger.big_divmod( a, b );
Assert.AreEqual( c, a );
Assert.AreEqual( d, b[0] );
a = new UInt32[]{ 0x12345678, 0x99AABBCC, 0x00000000 };
b = new UInt32[]{ 0x00000032 };
c = new UInt32[]{ 0xE1A4E302, 0x0312C650, 0x00000000 };
d = 0x14;
BigInteger.big_divmod( a, b );
Assert.AreEqual( c, a );
Assert.AreEqual( d, b[0] );
a = new UInt32[]{ 0xBBCCDDEE, 0x778899AA };
b = new UInt32[]{ 0x00000010 };
c = new UInt32[]{ 0xABBCCDDE, 0x0778899A };
d = 0x0E;
BigInteger.big_divmod( a, b );
Assert.AreEqual( c, a );
Assert.AreEqual( d, b[0] );
}
[Test]
public void Test_14_HL_Pow()
{
BigInteger a,b,c;
a = BigInteger.FromHexString("34");
b = a.Pow(7);
c = BigInteger.FromHexString("0EF5DD94000");
Assert.AreEqual(c, b);
a = BigInteger.FromHexString("34");
b = a.Pow(5);
c = BigInteger.FromHexString("16A97400");
Assert.AreEqual(c, b);
}
// Assert.AreEqual(1, BigInteger.Zero.GetLength());
// Assert.AreEqual(1, BigInteger.One.GetLength());
// Assert.AreEqual(2, new BigInteger(long.MaxValue).GetLength());
// Assert.AreEqual(2, new BigInteger(long.MinValue).GetLength());
// Assert.AreEqual(1, new BigInteger((long)1234).GetLength());
// Assert.AreEqual(BigInteger.One, BigInteger.MinusOne.Twos());
// Assert.AreEqual(BigInteger.MinusOne, BigInteger.One.Twos());
// Assert.AreEqual(BigInteger.Zero, BigInteger.Zero.Twos());
// Assert.IsTrue(BigInteger.Zero.IsZero);
// Assert.IsFalse(BigInteger.One.IsZero);
// Assert.AreEqual(BigInteger.One, BigInteger.Zero.Add(BigInteger.One));
// Assert.AreEqual(BigInteger.One, BigInteger.One.Add(BigInteger.Zero));
// Assert.AreNotEqual(BigInteger.Zero, BigInteger.Zero.Add(BigInteger.One));
// Assert.AreEqual(BigInteger.Zero, BigInteger.One.Sub(BigInteger.One));
// Assert.AreEqual(BigInteger.One, BigInteger.One.Sub(BigInteger.Zero));
// BigInteger bi5 = new BigInteger(5 << 24);
// BigInteger bi15 = new BigInteger(15 << 24 );
// BigInteger bi75 = new BigInteger(75L << 48 );
// BigInteger bi75b = bi5.Mul(bi15);
// Assert.AreEqual(bi75, bi75b);
// Assert.AreEqual(new BigInteger(5), bi5 >> 24);
[Test]
public void Test_20_Converter()
{
foreach (string hexdigits in new string[]{
"44BBCCDD",
"FFFFFFFF",
"1122334455667788",
"778899AABBCCDDEE"
})
TestImplHexConvert(hexdigits);
}
public void TestImplHexConvert(string hexdigits)
{
BigInteger bi = BigInteger.Parse(hexdigits, 16);
string rehexed = bi.ToString(16);
Assert.AreEqual(hexdigits, rehexed);
}
[Test]
public void PerformaceTests()
{
RandomNumberGenerator rnd = RandomNumberGenerator.Create();
byte[] rand = new byte[32];
BigInteger[] bigIntegers = new BigInteger[16];
System.Numerics.BigInteger[] dotnetIntegers = new System.Numerics.BigInteger[bigIntegers.Length];
TestContext.Error.WriteLine("Performance Test: generating random integers...");
for (int n=0;n<bigIntegers.Length;n++)
{
rnd.GetBytes(rand);
bigIntegers[n] = new BigInteger(rand);
dotnetIntegers[n] = new System.Numerics.BigInteger(rand);
TestContext.Error.WriteLine("integer #{0} = {1}", n, bigIntegers[n]);
}
DateTime start = DateTime.Now;
for (int r=0;r<10000;r++)
for (int n=0;n<bigIntegers.Length-1;n++)
bigIntegers[n].Mul(bigIntegers[n+1]);
DateTime stop = DateTime.Now;
TestContext.Error.WriteLine("[ LN ] {0} x 32 Byte multiplication needed {1}ms", 10000 * (bigIntegers.Length-1), (stop -start).TotalMilliseconds);
start = DateTime.Now;
for (int r=0;r<10000;r++)
for (int n=0;n<bigIntegers.Length-1;n++)
System.Numerics.BigInteger.Multiply(dotnetIntegers[n], dotnetIntegers[n+1]);
stop = DateTime.Now;
TestContext.Error.WriteLine("[ DOTNET ] {0} x 32 Byte multiplication needed {1}ms", 10000 * (bigIntegers.Length-1), (stop -start).TotalMilliseconds);
}
struct TestVector
{
public uint[] a,b,c;
public TestVector(uint[] a,uint[] b, uint[] c)
{
this.a = a;
this.b = b;
this.c = c;
}
}
}
}