From 1f31427139acdef00f24aac13b67224fa915f9e7 Mon Sep 17 00:00:00 2001 From: Richard van der Hoff Date: Tue, 24 May 2016 16:58:51 +0100 Subject: megolm_advance_to: Remove excessive optimisation There was some slightly overcomplex logic designed to save a couple of hash operations when R(0) and R(1) were advanced, but the extra code was hard to understand and didn't save much. --- src/megolm.c | 29 +++++++---------------------- 1 file changed, 7 insertions(+), 22 deletions(-) (limited to 'src') diff --git a/src/megolm.c b/src/megolm.c index 9e28f8d..6d8af08 100644 --- a/src/megolm.c +++ b/src/megolm.c @@ -106,6 +106,7 @@ void megolm_advance_to(Megolm *megolm, uint32_t advance_to) { for (j = 0; j < (int)MEGOLM_RATCHET_PARTS; j++) { int shift = (MEGOLM_RATCHET_PARTS-j-1) * 8; uint32_t mask = (~(uint32_t)0) << shift; + int k; /* how many times to we need to rehash this part? */ int steps = (advance_to >> shift) - (megolm->counter >> shift); @@ -122,30 +123,14 @@ void megolm_advance_to(Megolm *megolm, uint32_t advance_to) { steps --; } - /* on the last step (except for j=3), we need to bump at least R(j+1); - * depending on the target count, we may also need to bump R(j+2) and - * R(j+3). + /* on the last step we also need to bump R(j+1)...R(3). + * + * (Theoretically, we could skip bumping R(j+2) if we're going to bump + * R(j+1) again, but the code to figure that out is a bit baroque and + * doesn't save us much). */ - int k; - switch(j) { - case 0: - if (!(advance_to & 0xFFFF00)) { k = 3; } - else if (!(advance_to & 0xFF00)) { k = 2; } - else { k = 1; } - break; - case 1: - if (!(advance_to & 0xFF00)) { k = 3; } - else { k = 2; } - break; - case 2: - case 3: - k = 3; - break; - } - - while (k >= j) { + for (k = 3; k >= j; k--) { rehash_part(megolm->data, j, k); - k--; } megolm->counter = advance_to & mask; } -- cgit v1.2.3