User Tools

Site Tools


en:pfw:sha-256

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
en:pfw:sha-256 [2024-10-17 12:40] jeroenhen:pfw:sha-256 [2024-10-17 13:09] (current) jeroenh
Line 185: Line 185:
  
 \ ===== END of code \ ===== END of code
-</code> 
  
 +</code>
 + 
 + 
 + 
 + 
 + 
 **Testing the SHA-256 algorithm** **Testing the SHA-256 algorithm**
 +
  
 Testing the SHA-256 algorithm can be done with the following code. The 'abc' example comes directly from the the standard document. Testing the SHA-256 algorithm can be done with the following code. The 'abc' example comes directly from the the standard document.
 +  
 + 
 <code> <code>
 +
 \ ===== TEST routines \ ===== TEST routines
  
Line 225: Line 233:
  
 </code> </code>
- +  
 +  
 +  
 +  
 + 
 **Optimising performance of the SHA-256 hash generator - step 1 ** **Optimising performance of the SHA-256 hash generator - step 1 **
  
-Creating SHA-256 hashes is an area where time of execution is usually seen as relevant. The above version, using generic Forth, will run on most Forth systems. But is not the fastest version possible. Running on wabiForth in tests it achieves a throughput of around 2.6 MB/s. 
  
-One way of doing this is to use Forth-Macros as described by Wil Baden in Forth Dimensions (FD) Vol19No2. There is version floating around on the internet which does exactly that and here is an adapted version of that codeIt is not totally using generic Forth as I wanted to keep te original as unchanged as possible.+Creating SHA-256 hashes is an area where time of execution is usually seen as relevantThe above versionusing generic Forth, will run on most Forth systems but is rather slowRunning on wabiForth in tests it achieves throughput of around 2.6 MB/s.
  
 +One way of optimising performance is to use Forth-Macros, as described by Wil Baden in Forth Dimensions Vol. 19, No. 2. There is a version floating around on the internet which does exactly that. Here is an simplified version of that code. It is not totally using generic Forth as I wanted to keep te original as unchanged as possible. The extensive use of PICK and the 64DROP used after each block are noteworthy.
 + 
 + 
 + 
 <code> <code>
 +  
 + 
 \ ======================================================== \ ========================================================
 \ ANS Forth code for Secure Hash Algorithms SHA-256 \ ANS Forth code for Secure Hash Algorithms SHA-256
Line 448: Line 463:
  
  
-\ =====  BENCHMARKS  =====================================+\ =====  TESTING  =====================================
 : .HAHDR : .HAHDR
     ." ---h0--- ---h1--- ---h2--- ---h3--- ---h4--- ---h5--- ---h6--- ---h7---" ;     ." ---h0--- ---h1--- ---h2--- ---h3--- ---h4--- ---h5--- ---h6--- ---h7---" ;
Line 459: Line 474:
  
  
-</code\ +</code
 +  
 +  
 +  
 + 
 **Optimising performance of the SHA-256 hash generator - step 2 ** **Optimising performance of the SHA-256 hash generator - step 2 **
  
-A more thorough optimisation would be to look at often used parts of the code and optimise those.+A more thorough optimisation is possible by looking at often used parts of the code and optimising those.
  
  
-A first step to make the algorithm faster is check if BYTES>< and ROTR are available as faster primitives on your forth-system. Especially using a fast primitive for ROTR is beneficial, as most processors have a specific opcode for that function. On the Raspberry Pi4 thus change alone makes the routine 1.7 times faster.+A first step to make the algorithm faster is to check if BYTES>< and ROTR are available as faster primitives on your forth-system. Especially using a fast primitive for ROTR is beneficial, as most processors have a specific opcode for that function. On the Raspberry Pi4 this change alone makes the routine 1.7 times faster.
  
 The next step could be to use a data-array for the hash-variables H0-H7 and the temp variables. On systems with a memory-cache, this saves time by reducing writes to and reads from memory. The next step could be to use a data-array for the hash-variables H0-H7 and the temp variables. On systems with a memory-cache, this saves time by reducing writes to and reads from memory.
Line 474: Line 492:
 The nest step would be to program the subloop as a whole in assembly. This is a surprisingly short assembly routine of only 37 opcodes, including the 4 logical functions, in ARM32 assembly. The throughput is now around 25 MB/s The nest step would be to program the subloop as a whole in assembly. This is a surprisingly short assembly routine of only 37 opcodes, including the 4 logical functions, in ARM32 assembly. The throughput is now around 25 MB/s
  
-The last step tested by the author is to also program the HASH1BLOCK word in assembly. The final throughput achieved is 45 MB/s. Around 17 times faster than using generic Forth. This is the corresponding code:+The last step tested by the author is to also program the HASH1BLOCK word in assembly. The final throughput achieved is 45 MB/s. Around 17 times faster than using generic Forth.
  
  
 +The following is an example where ARM32 assembly is used for the subloop and the HASH1BLOCK word:
 + 
 + 
 <code> <code>
  
Line 755: Line 776:
  
 </code> </code>
 + 
 + 
 + 
 + 
 +Please note that this is all done using bog-standard ARM32 assembly, no SHA-256 specific opcodes are used. These  SHA_256 specific opcodes would increase the performance even more. Another option would be to use the NEON coprocessor. This is available on Raspberry Pi2 and later and would allow some parallel processing of the subloop. If this really raises throughput is as yet unproven.
  
  
- +\j2h
- +
-Please note that this is al done using normal ARM32 assembly, no SHA-256 specific opcodes were used. These  SHA_256 specific opcodes would increase the performance even more. Another option would be to use the NEON coprocessor. This is available on Raspberry Pi2 and later and would allow some parallel processing of the subloop. If this really enhances performance is as yet unproven.+
  
en/pfw/sha-256.1729161646.txt.gz · Last modified: 2024-10-17 12:40 by jeroenh