From d24594976686983c7186cbe4e78153888a13b6e4 Mon Sep 17 00:00:00 2001 From: Marcus Brinkmann Date: Thu, 6 Jul 2017 13:52:24 +0200 Subject: tofu: Compare squares instead of square roots. * g10/Makefile.am (tofu_source) [USE_TOFU]: Remove sqrtu32.h and sqrtu32.c. * g10/sqrtu32.h, g10/sqrtu32.c: Removed files. * g10/tofu.c: Compare squares instead of square roots. -- The original code is a factor 11.5 slower than using libm's sqrt(), which in turn is a factor 3.5 slower than using one multiplication on the other side of the comparison. Also, it's much simpler now. Signed-off-by: Marcus Brinkmann --- g10/tofu.c | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) (limited to 'g10/tofu.c') diff --git a/g10/tofu.c b/g10/tofu.c index ba46244b0..c183fc665 100644 --- a/g10/tofu.c +++ b/g10/tofu.c @@ -42,7 +42,6 @@ #include "../common/mkdir_p.h" #include "gpgsql.h" #include "../common/status.h" -#include "sqrtu32.h" #include "tofu.h" @@ -2922,19 +2921,18 @@ write_stats_status (estream_t fp, { int summary; int validity; - unsigned long days; + unsigned long days_sq; /* Use the euclidean distance (m = sqrt(a^2 + b^2)) rather then the sum of the magnitudes (m = a + b) to ensure a balance between verified signatures and encrypted messages. */ - days = sqrtu32 (signature_days * signature_days - + encryption_days * encryption_days); + days_sq = signature_days * signature_days + encryption_days * encryption_days; - if (days < 1) + if (days_sq < 1) validity = 1; /* Key without history. */ - else if (days < 2 * BASIC_TRUST_THRESHOLD) + else if (days_sq < (2 * BASIC_TRUST_THRESHOLD) * (2 * BASIC_TRUST_THRESHOLD)) validity = 2; /* Key with too little history. */ - else if (days < 2 * FULL_TRUST_THRESHOLD) + else if (days_sq < (2 * FULL_TRUST_THRESHOLD) * (2 * FULL_TRUST_THRESHOLD)) validity = 3; /* Key with enough history for basic trust. */ else validity = 4; /* Key with a lot of history. */ @@ -3232,9 +3230,9 @@ show_statistics (tofu_dbs_t dbs, " one message to this key!\n")); /* Cf. write_stats_status */ - if (sqrtu32 (encryption_count * encryption_count - + signature_count * signature_count) - < 2 * BASIC_TRUST_THRESHOLD) + if ((encryption_count * encryption_count + + signature_count * signature_count) + < ((2 * BASIC_TRUST_THRESHOLD) * (2 * BASIC_TRUST_THRESHOLD))) show_warning = 1; } } -- cgit v1.2.3