megolm.c: rewrite counter update
We no longer need to keep track of intermediate values of the counter, which means we can update it much more easily.
This commit is contained in:
parent
f3c0dd76d7
commit
ef8d24f483
1 changed files with 3 additions and 8 deletions
11
src/megolm.c
11
src/megolm.c
|
@ -105,26 +105,21 @@ void megolm_advance_to(Megolm *megolm, uint32_t advance_to) {
|
||||||
/* starting with R0, see if we need to update each part of the hash */
|
/* starting with R0, see if we need to update each part of the hash */
|
||||||
for (j = 0; j < (int)MEGOLM_RATCHET_PARTS; j++) {
|
for (j = 0; j < (int)MEGOLM_RATCHET_PARTS; j++) {
|
||||||
int shift = (MEGOLM_RATCHET_PARTS-j-1) * 8;
|
int shift = (MEGOLM_RATCHET_PARTS-j-1) * 8;
|
||||||
uint32_t increment = 1 << shift;
|
uint32_t mask = (~(uint32_t)0) << shift;
|
||||||
uint32_t next_counter;
|
|
||||||
|
|
||||||
/* how many times to we need to rehash this part? */
|
/* how many times to we need to rehash this part? */
|
||||||
int steps = (advance_to >> shift) - (megolm->counter >> shift);
|
int steps = (advance_to >> shift) - (megolm->counter >> shift);
|
||||||
|
|
||||||
if (steps == 0) {
|
if (steps == 0) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
megolm->counter = megolm->counter & ~(increment - 1);
|
|
||||||
next_counter = megolm->counter + increment;
|
|
||||||
|
|
||||||
/* for all but the last step, we can just bump R(j) without regard
|
/* for all but the last step, we can just bump R(j) without regard
|
||||||
* to R(j+1)...R(3).
|
* to R(j+1)...R(3).
|
||||||
*/
|
*/
|
||||||
while (steps > 1) {
|
while (steps > 1) {
|
||||||
rehash_part(megolm->data, j, j);
|
rehash_part(megolm->data, j, j);
|
||||||
megolm->counter = next_counter;
|
|
||||||
steps --;
|
steps --;
|
||||||
next_counter = megolm->counter + increment;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* on the last step (except for j=3), we need to bump at least R(j+1);
|
/* on the last step (except for j=3), we need to bump at least R(j+1);
|
||||||
|
@ -152,6 +147,6 @@ void megolm_advance_to(Megolm *megolm, uint32_t advance_to) {
|
||||||
rehash_part(megolm->data, j, k);
|
rehash_part(megolm->data, j, k);
|
||||||
k--;
|
k--;
|
||||||
}
|
}
|
||||||
megolm->counter = next_counter;
|
megolm->counter = advance_to & mask;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue