Lựa chọn

 Danh mục

 Chi tiết tài nguyên
ThexCS - TTH (tiger tree hash) maker in C#
   Đăng bởi: host | Ngày : 01:43 05/12/09 | Xem: 607 |        
ThexCS - TTH (tiger tree hash) maker in C#
I wrote this code for a project called SharpConnect - a C# client of the DirectConnect protocol.
The updated version was released for all of those who asked for the option to get the whole TTH tree. This version is thread based, I decided using threads to improve the CPU usage and algorithm speed. After analyzing the performance of the ThexOptimized version, I found out that the stage which consumes the most of the time was the data pulling from the file into the buffer. Also, I found that the "tree compress" (the part that computes the internal hash) is very fast and almost doesn't take any time, so I decided to add threads and split the file into parts for each thread to handle simultaneously. The outcome was that the CPU usage is much better (the more threads used, the less CPU is needed) but the speed wasn't improved, but got slower (not buy much). Problems that came up along with the threads usage were threads colliding in the FileStream.Read stage which throws an exception that was caused from two or more threads reading the file at the same time. Then I tried fixing it up with Monitor which made it thread safe but it was slower and overloaded the CPU. Finally, a FileStream was added for each thread which required a rewrite of the code. I also decided to update the way data is pulled out, and it's now being read in blocks instead of leaf by leaf (which improved the I/O reads number dramatically).
For conclusion in this version, you can set it to your needs by changing the ThreadCount const; the bigger it is, the slower it gets, but saves some CPU, or you can set it to a low threads count like 1-3 which will get faster results with high CPU usage.
Tiger Tree Hash is constructed from two parts, the Tiger Hash Algorithm and Merkle Tree. The original Tiger Hash code was taken from Tiger.Net which is written in Visual Basic. NET.
Using the code
I added two new functions:
  • GetTTH_Value returns the root TTH value in byte[] array.
  • GetTTH_Tree returns a byte[][][] array of the full tree.
You can check LevelCount properties for getting how many levels are in the tree.
ThexCS doesn't require much, all you have to do is basically:
ThexThreaded TTH = new ThexThreaded();
Points of Interest
Algorithm is not fun.
  • First version only calculated an algorithm for getting TTH values very slowly.
  • Second version was based on byte arrays instead of ArrayList, and compressed the block to make the byte arrays smaller which got the values faster and was called ThexOptimized.
  • Third version is based on threads that were supposed to make the code much faster, but this turned out to be almost the same but the CPU usage is much better.  
Bình luận

Thêm nhận xét