More tests and managed code updates

master
Harald Wolff 2021-03-22 23:41:33 +01:00
parent 8add210a38
commit d12dafa91b
2 changed files with 86 additions and 0 deletions

View File

@ -1,5 +1,6 @@
using NUnit.Framework;
using System;
using System.Security.Cryptography;
namespace ln.biginteger.test
{
@ -95,6 +96,9 @@ namespace ln.biginteger.test
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 );
}
@ -170,6 +174,18 @@ namespace ln.biginteger.test
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();
}
@ -223,6 +239,23 @@ namespace ln.biginteger.test
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]
@ -289,6 +322,49 @@ namespace ln.biginteger.test
}
[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;

View File

@ -1,6 +1,7 @@
using System;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
@ -50,6 +51,14 @@ namespace ln.biginteger
bits = Reduce(rawbits);
}
public BigInteger(byte[] rawbits)
{
UInt32[] rints = new UInt32[(rawbits.Length + 3) >> 2];
for (int n=0;n<rints.Length;n++)
rints[n] = BitConverter.ToUInt32(rawbits, n << 2);
bits = Reduce(rints);
}
private void Reduce() => bits = Reduce(bits);
public static UInt32[] Reduce(UInt32[] bits)
{
@ -1584,6 +1593,7 @@ namespace ln.biginteger
[DllImport("bigint")]
public static extern Int32 big_cmp(UInt32[] op_a, Int32 length_a, UInt32[] op_b, Int32 length_b);
public static Int32 big_cmp(UInt32[] op_a, UInt32[] op_b) => big_cmp(op_a, op_a.Length, op_b, op_b.Length);
[DllImport("bigint")]