Considering how big the .NET FCL (.NET Framework Class Library, sometimes – confusingly – also known as BCL, or Base Class Library) is, you would not expect glaring omissions to happen.

Naturally, everyone has it’s own pet feature and needs a very special class, but I’m talking about missing classes/types that should be there for the significant enough number of developers.

While implementing the BitTorrent’s message stream encryption protocol, I ran into a case of missing functionality – there is no class for arbitrary long integers in the .NET. Considering how portions of .NET FCL look almost exactly like their counterpart in Java and that Java does have BigInteger type, this is surprising. Before someone asks – I know that Visual J# has BigInteger, but J# is not a part of the .NET Framework (it’s an extra download) and nobody uses it anyway.

In fact, .NET FCL does have a BigInt class. Except that:

  • it’s internal and sealed in the mscorlib and System.Security (can be found in both of them)
  • it’s relatively inefficient
  • its “public” interface sucks – there is no shift-right “>>” operator nor is there a multiplication of two BigInts

So, even though you could instantiate the BigInt class using reflection (which is ugly, but works), you can’t use it for cryptography because it lacks functionality to properly implement ModPow1.

What are the alternatives? Well, IronPython has BigInteger, there is one implementation on CodeProject and there’s Mono implementation which is actually a descendant of the one on the CodeProject.

IronPython’s implementation looks the best, but it won’t work out of the box because it will treat all input coming as byte[] (which is what you’ll have, we’re talking about numbers with hundreds of decimal digits here) as signed, which is exactly the opposite of what you’d want (you want all numbers to be treated as unsigned).

CodeProject article is great, the implementation is good and even has a lot of code to generate prime and coprime numbers. But it’s 4 years old and apparently there are some bugs.

Mono implementation should have all the bugs found in the original CodeProject code fixed, but (I assume for performance reasons) they switched to using unsafe code, and I don’t want unsafe code in my app (rarely there is a justification for that).

So I’m left with a tough choice. Alternatively, I could wait till “Orcas” Visual Studio release, as it looks like Microsoft will finally add the BigInteger into the BCL.

Ugh.

1ModPow is a very common name for a function that calculates ax mod b. If a, x and b are large numbers, and I really mean large, this calculation cannot be performed naively (it’s practically impossible to calculate ax). There is an algorithm for it of course, it boils down to using shift-right (division by 2), multiplication and modulus (and by extension, division); you can find the definition of the algorithm here.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
0 Comments