libgmp implementation

master
Harald Wolff 2021-03-24 10:55:17 +01:00
parent d12dafa91b
commit 50d9de279c
5 changed files with 2363 additions and 1805 deletions

View File

@ -1,5 +1,6 @@
using NUnit.Framework;
using System;
using System.Numerics;
using System.Security.Cryptography;
namespace ln.biginteger.test
@ -11,266 +12,266 @@ namespace ln.biginteger.test
{
}
[Test]
public void Test_00_LowLevel()
{
UInt32[] a,b,c;
Int32 d;
// [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[]{ 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[]{ 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[]{ 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, 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[]{ 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 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};
// 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[]{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[]{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[]{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};
// 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 };
// 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[]{ 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 };
// 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[]{ 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[]{ 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[]{ 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 );
// 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;
// [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[]{ 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[]{ 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[]{ 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[]{ 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[]{ 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[]{ 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[]{ 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 };
// 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, 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[]{ 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[]{ 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[]{ 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 );
// 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();
}
// Assert.Pass();
// }
[Test]
public void Test_02_LL_Mul()
{
UInt32[] a,b,c;
// [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[]{ 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[]{ 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[]{ 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 );
// 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;
// [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[]{ 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 };
// 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[]{ 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] );
// 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");
a = BigInteger.Parse("34", 16);
b = a.Pow(7);
c = BigInteger.FromHexString("0EF5DD94000");
c = BigInteger.Parse("0EF5DD94000", 16);
Assert.AreEqual(c, b);
a = BigInteger.FromHexString("34");
a = BigInteger.Parse("34", 16);
b = a.Pow(5);
c = BigInteger.FromHexString("16A97400");
c = BigInteger.Parse("16A97400", 16);
Assert.AreEqual(c, b);
}
@ -318,15 +319,27 @@ namespace ln.biginteger.test
{
BigInteger bi = BigInteger.Parse(hexdigits, 16);
string rehexed = bi.ToString(16);
Assert.AreEqual(hexdigits, rehexed);
Assert.AreEqual(hexdigits.ToLower(), rehexed.ToLower());
}
[Test]
public void PerformaceTests()
public void PerformaceTest_Mul()
{
for (int n=0;n<4;n++)
{
PT_Mul(4, 100000);
PT_Mul(8, 100000);
PT_Mul(16, 100000);
PT_Mul(32, 100000);
PT_Mul(64, 100000);
}
}
public void PT_Mul(int bytes, int repeat)
{
RandomNumberGenerator rnd = RandomNumberGenerator.Create();
byte[] rand = new byte[32];
byte[] rand = new byte[bytes];
BigInteger[] bigIntegers = new BigInteger[16];
System.Numerics.BigInteger[] dotnetIntegers = new System.Numerics.BigInteger[bigIntegers.Length];
@ -334,33 +347,104 @@ namespace ln.biginteger.test
for (int n=0;n<bigIntegers.Length;n++)
{
rnd.GetBytes(rand);
bigIntegers[n] = new BigInteger(rand);
bigIntegers[n] = BigInteger.FromByteArray(rand);
dotnetIntegers[n] = new System.Numerics.BigInteger(rand);
TestContext.Error.WriteLine("integer #{0} = {1}", n, bigIntegers[n]);
//TestContext.Error.WriteLine("integer #{0} = {1}", n, bigIntegers[n]);
}
DateTime start = DateTime.Now;
BigInteger bi;
for (int r=0;r<10000;r++)
for (int r=0;r<repeat;r++)
for (int n=0;n<bigIntegers.Length-1;n++)
bigIntegers[n].Mul(bigIntegers[n+1]);
bi = bigIntegers[n] * 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);
TestContext.Error.WriteLine("[ LN ] {0} x {2} Byte multiplication needed {1}ms", repeat * (bigIntegers.Length-1), (stop -start).TotalMilliseconds, bytes);
// start = DateTime.Now;
// for (int r=0;r<10000;r++)
// for (int n=0;n<bigIntegers.Length-1;n++)
// bigIntegers[n].MMul(bigIntegers[n+1]);
// stop = DateTime.Now;
// TestContext.Error.WriteLine("[ LNC# ] {0} x {2} Byte multiplication needed {1}ms", 10000 * (bigIntegers.Length-1), (stop -start).TotalMilliseconds, bytes);
start = DateTime.Now;
for (int r=0;r<10000;r++)
for (int r=0;r<repeat;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);
TestContext.Error.WriteLine("[ DOTNET ] {0} x {2} Byte multiplication needed {1}ms", repeat * (bigIntegers.Length-1), (stop -start).TotalMilliseconds, bytes);
}
[Test]
public void PerformaceTest_Add()
{
for (int n=0;n<4;n++)
{
PT_Add(4, 100000);
PT_Add(8, 100000);
PT_Add(16, 100000);
PT_Add(32, 100000);
}
}
public void PT_Add(int bytes, int repeat)
{
RandomNumberGenerator rnd = RandomNumberGenerator.Create();
byte[] rand = new byte[bytes];
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] = BigInteger.FromByteArray(rand);
dotnetIntegers[n] = new System.Numerics.BigInteger(rand);
//TestContext.Error.WriteLine("integer #{0} = {1}", n, bigIntegers[n]);
}
DateTime start = DateTime.Now;
BigInteger bi;
for (int r=0;r<repeat;r++)
for (int n=0;n<bigIntegers.Length-1;n++)
bi = bigIntegers[n] + bigIntegers[n+1];
DateTime stop = DateTime.Now;
TestContext.Error.WriteLine("[ LN ] {0} x {2} Byte additions needed {1}ms", repeat * (bigIntegers.Length-1), (stop -start).TotalMilliseconds, bytes);
// start = DateTime.Now;
// for (int r=0;r<repeat;r++)
// for (int n=0;n<bigIntegers.Length-1;n++)
// bigIntegers[n].MAdd(bigIntegers[n+1]);
// stop = DateTime.Now;
// TestContext.Error.WriteLine("[ LNC# ] {0} x {2} Byte additions needed {1}ms", repeat * (bigIntegers.Length-1), (stop -start).TotalMilliseconds, bytes);
start = DateTime.Now;
for (int r=0;r<repeat;r++)
for (int n=0;n<bigIntegers.Length-1;n++)
System.Numerics.BigInteger.Add(dotnetIntegers[n], dotnetIntegers[n+1]);
stop = DateTime.Now;
TestContext.Error.WriteLine("[ DOTNET ] {0} x {2} Byte additions needed {1}ms", repeat * (bigIntegers.Length-1), (stop -start).TotalMilliseconds, bytes);
}

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,42 @@
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Threading;
namespace ln.biginteger
{
public static class BigIntegerAllocator
{
static int _mpz_size = 24;
static int _allocate_count = 40960000;
static Stack<IntPtr> pool = new Stack<IntPtr>();
public static IntPtr Pop()
{
lock (pool)
{
if (!pool.TryPop(out IntPtr ptr))
{
ptr = Marshal.AllocHGlobal(_mpz_size * _allocate_count);
for (int n=1;n<_allocate_count;n++)
pool.Push(ptr + (n*_mpz_size));
}
return ptr;
}
}
public static void Push(IntPtr ptr)
{
lock (pool)
{
pool.Push(ptr);
}
}
}
}

View File

@ -2,12 +2,16 @@
<PropertyGroup>
<TargetFramework>netcoreapp5.0</TargetFramework>
<!--AllowUnsafeBlocks>true</AllowUnsafeBlocks-->
</PropertyGroup>
<ItemGroup>
<Content Include="libbigint.*">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<!--PackageReference Include="AdvancedDLSupport" Version="3.2.0" /-->
</ItemGroup>
<ItemGroup>
<None Include="x32\**" CopyToOutputDirectory="PreserveNewest" LinkBase="config\" />
<None Include="x64\**" CopyToOutputDirectory="PreserveNewest" LinkBase="config\" />
</ItemGroup>
</Project>

Binary file not shown.