summary |
shortlog |
log |
commit | commitdiff |
tree
raw |
patch |
inline | side by side (from parent 1:
0c9c04e)
ctr_add came directly from 2012, and it was always broken.
The original purpose is to add two very large integers.
Still, there is no much to worry about: Threefish 64 bit integers
make it very hard to trigger the old buggy code, and it was only
used to rewind the counter. But it is time to finally fix it.
memset(usctr, 0, TF_BLOCK_SIZE);
memcpy(usctr, sctr, sctrsz > TF_BLOCK_SIZE ? TF_BLOCK_SIZE : sctrsz);
memset(usctr, 0, TF_BLOCK_SIZE);
memcpy(usctr, sctr, sctrsz > TF_BLOCK_SIZE ? TF_BLOCK_SIZE : sctrsz);
- ctr_add(uctr, usctr, TF_NR_BLOCK_UNITS);
+ ctr_add(uctr, TF_NR_BLOCK_UNITS, usctr, TF_NR_BLOCK_UNITS);
data_to_words(uctr, TF_BLOCK_SIZE);
memset(usctr, 0, TF_BLOCK_SIZE);
}
data_to_words(uctr, TF_BLOCK_SIZE);
memset(usctr, 0, TF_BLOCK_SIZE);
}
-static inline void ctr_inc(TF_UNIT_TYPE *x, size_t l)
+static inline void ctr_inc(TF_UNIT_TYPE *x, size_t xl)
- for (i = 0; i < l; i++) {
- x[i] = ((x[i] + (TF_UNIT_TYPE)1) & ((TF_UNIT_TYPE)~0));
- if (x[i]) break;
+ for (z = 0; z < xl; z++) {
+ x[z] = ((x[z] + (TF_UNIT_TYPE)1) & ((TF_UNIT_TYPE)~0));
+ if (x[z]) break;
-static inline void ctr_add(TF_UNIT_TYPE *x, const TF_UNIT_TYPE *y, size_t l)
+static inline void ctr_add(TF_UNIT_TYPE *x, size_t xl, const TF_UNIT_TYPE *y, size_t yl)
- for (i = 0; i < l; i++) {
- t = x[i];
- x[i] += y[i]; x[i] &= ((TF_UNIT_TYPE)~0);
- if (x[i] < t) {
-_again: f++;
- t = x[f-i];
- x[f-i]++;
- if (x[f-i] < t) goto _again;
- else f = 0;
- }
+ for (z = 0, cf = 0; z < xl; z++) {
+ t = x[z] + (z >= yl ? (TF_UNIT_TYPE)0 : y[z]) + cf;
+ if (cf) cf = (x[z] >= t ? 1 : 0);
+ else cf = (x[z] > t ? 1 : 0);
+ x[z] = t;