[FFmpeg-devel] [PATCH 3/3] xtea: precalculate part of the right side of the core equations
Michael Niedermayer
michaelni at gmx.at
Tue Sep 11 05:20:48 CEST 2012
This improves speed a bit (5% or so)
Signed-off-by: Michael Niedermayer <michaelni at gmx.at>
---
libavutil/xtea.c | 27 +++++++++++++--------------
libavutil/xtea.h | 2 +-
2 files changed, 14 insertions(+), 15 deletions(-)
diff --git a/libavutil/xtea.c b/libavutil/xtea.c
index beaa687..cdebf60 100644
--- a/libavutil/xtea.c
+++ b/libavutil/xtea.c
@@ -31,36 +31,35 @@ const int av_xtea_size = sizeof(AVXTEA);
void av_xtea_init(AVXTEA *ctx, const uint8_t key[16])
{
+ uint32_t sum = 0, delta = 0x9E3779B9;
int i;
- for (i = 0; i < 4; i++)
- ctx->key[i] = AV_RB32(key + (i << 2));
+ for (i = 0; i < 32; i++){
+ ctx->key[2*i+0] = sum + AV_RB32(key + ((sum& 3) << 2));
+ sum += delta;
+ ctx->key[2*i+1] = sum + AV_RB32(key + ((sum&6144) >> 9));
+ }
}
static void xtea_crypt_ecb(AVXTEA *ctx, uint8_t *dst, const uint8_t *src,
int decrypt)
{
uint32_t v0, v1;
+ uint32_t *key = ctx->key;
int i;
v0 = AV_RB32(src);
v1 = AV_RB32(src + 4);
if (decrypt) {
- uint32_t delta = 0x9E3779B9, sum = delta * 32;
-
- for (i = 0; i < 32; i++) {
- v1 -= (((v0 << 4) ^ (v0 >> 5)) + v0) ^ (sum + ctx->key[(sum >> 11) & 3]);
- sum -= delta;
- v0 -= (((v1 << 4) ^ (v1 >> 5)) + v1) ^ (sum + ctx->key[sum & 3]);
+ for (i=62; i>=0; i-=2) {
+ v1 -= (((v0 << 4) ^ (v0 >> 5)) + v0) ^ key[i+1];
+ v0 -= (((v1 << 4) ^ (v1 >> 5)) + v1) ^ key[i+0];
}
} else {
- uint32_t sum = 0, delta = 0x9E3779B9;
-
- for (i = 0; i < 32; i++) {
- v0 += (((v1 << 4) ^ (v1 >> 5)) + v1) ^ (sum + ctx->key[sum & 3]);
- sum += delta;
- v1 += (((v0 << 4) ^ (v0 >> 5)) + v0) ^ (sum + ctx->key[(sum >> 11) & 3]);
+ for (i=0; i<64; i+=2) {
+ v0 += (((v1 << 4) ^ (v1 >> 5)) + v1) ^ key[i+0];
+ v1 += (((v0 << 4) ^ (v0 >> 5)) + v0) ^ key[i+1];
}
}
diff --git a/libavutil/xtea.h b/libavutil/xtea.h
index 7313fb7..e68ee2a 100644
--- a/libavutil/xtea.h
+++ b/libavutil/xtea.h
@@ -39,7 +39,7 @@ extern const int av_xtea_size;
* libavutil.
*/
typedef struct AVXTEA {
- uint32_t key[16];
+ uint32_t key[64];
} AVXTEA;
/**
--
1.7.9.5
More information about the ffmpeg-devel
mailing list