Simplify XorBytes.
GitOrigin-RevId: d20ae3c1d44d0a88068b42979685e5b1f9df0193
This commit is contained in:
parent
44cd25afae
commit
e1515226be
@ -346,11 +346,11 @@ class Crc64Bench : public td::Benchmark {
|
|||||||
int main() {
|
int main() {
|
||||||
td::init_openssl_threads();
|
td::init_openssl_threads();
|
||||||
|
|
||||||
td::bench(AesIgeShortBench());
|
|
||||||
td::bench(AesIgeEncryptBench());
|
td::bench(AesIgeEncryptBench());
|
||||||
td::bench(AesIgeDecryptBench());
|
td::bench(AesIgeDecryptBench());
|
||||||
td::bench(AesCtrBench());
|
td::bench(AesCtrBench());
|
||||||
td::bench(AesEcbBench());
|
td::bench(AesEcbBench());
|
||||||
|
td::bench(AesIgeShortBench());
|
||||||
|
|
||||||
td::bench(Pbkdf2Bench());
|
td::bench(Pbkdf2Bench());
|
||||||
td::bench(RandBench());
|
td::bench(RandBench());
|
||||||
|
@ -75,11 +75,13 @@ struct AesBlock {
|
|||||||
}
|
}
|
||||||
|
|
||||||
AesBlock inc() const {
|
AesBlock inc() const {
|
||||||
AesBlock res;
|
AesBlock res = *this;
|
||||||
auto ptr = res.raw();
|
auto ptr = res.raw();
|
||||||
for (int i = 15; i >= 0; i--) {
|
if (++ptr[15] == 0) {
|
||||||
if (++ptr[i] != 0) {
|
for (int i = 14; i >= 0; i--) {
|
||||||
break;
|
if (++ptr[i] != 0) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return res;
|
return res;
|
||||||
@ -91,21 +93,23 @@ static_assert(sizeof(AesBlock) == AES_BLOCK_SIZE, "");
|
|||||||
class XorBytes {
|
class XorBytes {
|
||||||
public:
|
public:
|
||||||
static void run(const uint8 *a, const uint8 *b, uint8 *c, size_t n) {
|
static void run(const uint8 *a, const uint8 *b, uint8 *c, size_t n) {
|
||||||
XorBytes xorer;
|
static constexpr int BLOCK_SIZE = 16;
|
||||||
xorer.a = a;
|
auto block_cnt = n / BLOCK_SIZE;
|
||||||
xorer.b = b;
|
n -= block_cnt * BLOCK_SIZE;
|
||||||
xorer.c = c;
|
while (block_cnt-- > 0) {
|
||||||
xorer.n = n;
|
Block<BLOCK_SIZE> a_big = as<Block<BLOCK_SIZE>>(a);
|
||||||
xorer.step<16>();
|
Block<BLOCK_SIZE> b_big = as<Block<BLOCK_SIZE>>(b);
|
||||||
xorer.step<1>();
|
as<Block<BLOCK_SIZE>>(c) = a_big ^ b_big;
|
||||||
|
a += BLOCK_SIZE;
|
||||||
|
b += BLOCK_SIZE;
|
||||||
|
c += BLOCK_SIZE;
|
||||||
|
}
|
||||||
|
while (n-- > 0) {
|
||||||
|
c[n] = a[n] ^ b[n];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
const uint8 *a;
|
|
||||||
const uint8 *b;
|
|
||||||
uint8 *c;
|
|
||||||
size_t n;
|
|
||||||
|
|
||||||
template <size_t N>
|
template <size_t N>
|
||||||
struct alignas(N) Block {
|
struct alignas(N) Block {
|
||||||
uint8 data[N];
|
uint8 data[N];
|
||||||
@ -117,20 +121,6 @@ class XorBytes {
|
|||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
template <size_t N>
|
|
||||||
void step() {
|
|
||||||
auto cnt = n / N;
|
|
||||||
n -= cnt * N;
|
|
||||||
for (size_t i = 0; i < cnt; i++) {
|
|
||||||
Block<N> a_big = as<Block<N>>(a);
|
|
||||||
Block<N> b_big = as<Block<N>>(b);
|
|
||||||
as<Block<N>>(c) = a_big ^ b_big;
|
|
||||||
a += N;
|
|
||||||
b += N;
|
|
||||||
c += N;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
struct AesCtrCounterPack {
|
struct AesCtrCounterPack {
|
||||||
|
Loading…
Reference in New Issue
Block a user