mirror of
https://github.com/SerenityOS/serenity
synced 2024-07-03 10:53:37 +00:00
LibCrypto: Remove select individual Vector/Span::at() calls
These were showing up on profiles as quite hot (>5%), we can avoid all the unnecessary assertions by doing them once in advance and using pointers for the rest of the function. This makes AES-GCM about 6% faster.
This commit is contained in:
parent
1cc4d29d20
commit
ea15ccdae3
|
@ -94,15 +94,20 @@ ALWAYS_INLINE static void addition_with_carry(u32 a, u32 b, u32& z_carry, u32& z
|
|||
*/
|
||||
UnsignedBigInteger::Word UnsignedBigIntegerAlgorithms::montgomery_fragment(UnsignedBigInteger& z, size_t offset_in_z, UnsignedBigInteger const& x, UnsignedBigInteger::Word y_digit, size_t num_words)
|
||||
{
|
||||
VERIFY(x.m_words.size() >= num_words);
|
||||
VERIFY(z.m_words.size() >= num_words + offset_in_z);
|
||||
auto const* x_words = x.m_words.data();
|
||||
auto* z_words = z.m_words.data();
|
||||
|
||||
UnsignedBigInteger::Word carry { 0 };
|
||||
for (size_t i = 0; i < num_words; ++i) {
|
||||
UnsignedBigInteger::Word a_carry;
|
||||
UnsignedBigInteger::Word a;
|
||||
linear_multiplication_with_carry(x.m_words[i], y_digit, z.m_words[offset_in_z + i], a_carry, a);
|
||||
linear_multiplication_with_carry(x_words[i], y_digit, z_words[offset_in_z + i], a_carry, a);
|
||||
UnsignedBigInteger::Word b_carry;
|
||||
UnsignedBigInteger::Word b;
|
||||
addition_with_carry(a, carry, b_carry, b);
|
||||
z.m_words[offset_in_z + i] = b;
|
||||
z_words[offset_in_z + i] = b;
|
||||
carry = a_carry + b_carry;
|
||||
}
|
||||
return carry;
|
||||
|
|
|
@ -112,19 +112,21 @@ size_t UnsignedBigInteger::export_data(Bytes data, bool remove_leading_zeros) co
|
|||
ssize_t leading_zeros = -1;
|
||||
if (remove_leading_zeros) {
|
||||
UnsignedBigInteger::Word word = m_words[word_count - 1];
|
||||
u8 value[4] {};
|
||||
for (size_t i = 0; i < sizeof(u32); i++) {
|
||||
u8 byte = (u8)(word >> ((sizeof(u32) - i - 1) * 8));
|
||||
data[out++] = byte;
|
||||
value[i] = byte;
|
||||
if (leading_zeros < 0 && byte != 0)
|
||||
leading_zeros = (int)i;
|
||||
}
|
||||
data.overwrite(out, value, array_size(value));
|
||||
out += array_size(value);
|
||||
}
|
||||
for (size_t i = word_count - (remove_leading_zeros ? 1 : 0); i > 0; i--) {
|
||||
auto word = m_words[i - 1];
|
||||
data[out++] = (u8)(word >> 24);
|
||||
data[out++] = (u8)(word >> 16);
|
||||
data[out++] = (u8)(word >> 8);
|
||||
data[out++] = (u8)word;
|
||||
u8 value[] { (u8)(word >> 24), (u8)(word >> 16), (u8)(word >> 8), (u8)word };
|
||||
data.overwrite(out, value, array_size(value));
|
||||
out += array_size(value);
|
||||
}
|
||||
if (leading_zeros > 0)
|
||||
out -= leading_zeros;
|
||||
|
|
Loading…
Reference in New Issue
Block a user