Home » Encryption » Crypt_Xtea » Bug #8586
patch to improve the Xtea algorithm performance
Details
| Request #8586 | patch to improve the Xtea algorithm performance |
|---|---|
| Submitted | 2006-08-30 13:11 UTC |
| From | toffanin dot mauro at gmail dot com |
| Status | Bogus |
| Package | Crypt_Xtea |
| PHP Version | 5.1.4 |
| Roadmaps | (Not assigned) |
Comments
[2006-08-30 13:11 UTC] toffanin dot mauro at gmail dot com
Description:
------------
The patch below do a little code modification to improve
performances during Xtea encryption and decryption
processes.
benchmarking the new class it is possible to gain 10/20%
of speedup; more iterations, more performances are gained.
the patch also modify the constructor class, to let the
user to specify the iterations number as a constructor
parameter:
$xtea = new Crypt_Xtea(32);
so it is not necesserary to use setIter() function.
BC is not break, if no parameter is passed, the default is
always 32.
the patch also remove setIter() call from the constructor
class, because it is redundant code: constructor class
know where to store the iterations default number, so it
is save directly into $this->n_iter variable instead to
pass throught setIter() function.
Hope the patch can be useful.
Toffanin Mauro
Test script:
---------------
--- Xtea.php 2006-08-30 14:47:17.661690136 +0200
+++ Xtea.php 2006-08-30 14:57:09.567706744 +0200
@@ -142,9 +142,9 @@
* @author Jeroen Derks <jeroen@derks.it>
* @see setIter()
*/
- function Crypt_Xtea()
+ function Crypt_Xtea($n_iter=32)
{
- $this->setIter(32);
+ $this->n_iter = $n_iter;
}
// }}}
@@ -225,16 +225,19 @@
$k = array(0, 0, 0, 0);
for ($i = 0; $i < $n_data_long; ++$i) {
// get next key part of 128 bits
- if ($j + 4 <= $n_key_long) {
- $k[0] = $key_long[$j];
- $k[1] = $key_long[$j + 1];
- $k[2] = $key_long[$j + 2];
- $k[3] = $key_long[$j + 3];
- } else {
- $k[0] = $key_long[$j % $n_key_long];
- $k[1] = $key_long[($j + 1) % $n_key_long];
- $k[2] = $key_long[($j + 2) % $n_key_long];
- $k[3] = $key_long[($j + 3) % $n_key_long];
+ switch ($j + 4 <= $n_key_long) {
+ case TRUE:
+ $k[0] = $key_long[$j];
+ $k[1] = $key_long[$j + 1];
+ $k[2] = $key_long[$j + 2];
+ $k[3] = $key_long[$j + 3];
+ break;
+ case FALSE:
+ $k[0] = $key_long[$j % $n_key_long];
+ $k[1] = $key_long[($j + 1) % $n_key_long];
+ $k[2] = $key_long[($j + 2) % $n_key_long];
+ $k[3] = $key_long[($j + 3) % $n_key_long];
+ break;
}
$j = ($j + 4) % $n_key_long;
@@ -284,16 +287,19 @@
for ($i = 0; $i < $n_enc_data_long; $i += 2) {
// get next key part of 128 bits
- if ($j + 4 <= $n_key_long) {
- $k[0] = $key_long[$j];
- $k[1] = $key_long[$j + 1];
- $k[2] = $key_long[$j + 2];
- $k[3] = $key_long[$j + 3];
- } else {
- $k[0] = $key_long[$j % $n_key_long];
- $k[1] = $key_long[($j + 1) % $n_key_long];
- $k[2] = $key_long[($j + 2) % $n_key_long];
- $k[3] = $key_long[($j + 3) % $n_key_long];
+ switch ($j + 4 <= $n_key_long) {
+ case TRUE:
+ $k[0] = $key_long[$j];
+ $k[1] = $key_long[$j + 1];
+ $k[2] = $key_long[$j + 2];
+ $k[3] = $key_long[$j + 3];
+ break;
+ case FALSE:
+ $k[0] = $key_long[$j % $n_key_long];
+ $k[1] = $key_long[($j + 1) % $n_key_long];
+ $k[2] = $key_long[($j + 2) % $n_key_long];
+ $k[3] = $key_long[($j + 3) % $n_key_long];
+ break;
}
$j = ($j + 4) % $n_key_long;
[2006-08-30 13:24 UTC] toffanin dot mauro at gmail dot com
.