mirror of
https://github.com/itzmeanjan/ml-kem.git
synced 2026-01-07 23:04:00 -05:00
reformat source tree with increased *ColumnLimit*
Signed-off-by: Anjan Roy <hello@itzmeanjan.in>
This commit is contained in:
@@ -81,7 +81,7 @@ BreakBeforeTernaryOperators: true
|
||||
BreakConstructorInitializers: BeforeComma
|
||||
BreakInheritanceList: BeforeComma
|
||||
BreakStringLiterals: true
|
||||
ColumnLimit: 120
|
||||
ColumnLimit: 172
|
||||
CommentPragmas: '^ IWYU pragma:'
|
||||
CompactNamespaces: false
|
||||
ConstructorInitializerIndentWidth: 2
|
||||
|
||||
@@ -48,7 +48,7 @@ public:
|
||||
inline constexpr zq_t operator+(const zq_t rhs) const
|
||||
{
|
||||
const uint32_t t0 = this->v + rhs.v;
|
||||
const uint32_t mask = (-static_cast<uint32_t>(t0 >= Q));
|
||||
const auto mask = -static_cast<uint32_t>(t0 >= Q);
|
||||
const uint32_t t1 = t0 - (mask & Q);
|
||||
|
||||
return zq_t(t1);
|
||||
@@ -143,8 +143,8 @@ private:
|
||||
const uint32_t t2 = t1 * Q;
|
||||
const uint32_t t = v - t2;
|
||||
|
||||
const bool flg = t >= Q;
|
||||
const uint32_t t_prime = t - flg * Q;
|
||||
const auto mask = -static_cast<uint32_t>(t >= Q);
|
||||
const uint32_t t_prime = t - (mask & Q);
|
||||
|
||||
return t_prime;
|
||||
}
|
||||
|
||||
@@ -144,8 +144,7 @@ encapsulate(std::span<const uint8_t, 32> m,
|
||||
// https://pq-crystals.org/kyber/data/kyber-specification-round3-20210804.pdf
|
||||
template<size_t k, size_t eta1, size_t eta2, size_t du, size_t dv>
|
||||
static inline shake256::shake256_t
|
||||
decapsulate(std::span<const uint8_t, kyber_utils::get_kem_secret_key_len<k>()> seckey,
|
||||
std::span<const uint8_t, kyber_utils::get_kem_cipher_len<k, du, dv>()> cipher)
|
||||
decapsulate(std::span<const uint8_t, kyber_utils::get_kem_secret_key_len<k>()> seckey, std::span<const uint8_t, kyber_utils::get_kem_cipher_len<k, du, dv>()> cipher)
|
||||
requires(kyber_params::check_decap_params(k, eta1, eta2, du, dv))
|
||||
{
|
||||
constexpr size_t sklen = k * 12 * 32;
|
||||
|
||||
@@ -28,10 +28,7 @@ constexpr size_t CIPHER_LEN = kyber_utils::get_kem_cipher_len<k, du, dv>();
|
||||
// secret key is 3168 -bytes, given 32 -bytes seed d ( used in CPA-PKE ) and 32
|
||||
// -bytes seed z ( used in CCA-KEM ).
|
||||
inline void
|
||||
keygen(std::span<const uint8_t, 32> d,
|
||||
std::span<const uint8_t, 32> z,
|
||||
std::span<uint8_t, PKEY_LEN> pubkey,
|
||||
std::span<uint8_t, SKEY_LEN> seckey)
|
||||
keygen(std::span<const uint8_t, 32> d, std::span<const uint8_t, 32> z, std::span<uint8_t, PKEY_LEN> pubkey, std::span<uint8_t, SKEY_LEN> seckey)
|
||||
{
|
||||
kem::keygen<k, η1>(d, z, pubkey, seckey);
|
||||
}
|
||||
@@ -44,9 +41,7 @@ keygen(std::span<const uint8_t, 32> d,
|
||||
//
|
||||
// Returned KDF can be used for deriving shared key of arbitrary bytes length.
|
||||
inline shake256::shake256_t
|
||||
encapsulate(std::span<const uint8_t, 32> m,
|
||||
std::span<const uint8_t, PKEY_LEN> pubkey,
|
||||
std::span<uint8_t, CIPHER_LEN> cipher)
|
||||
encapsulate(std::span<const uint8_t, 32> m, std::span<const uint8_t, PKEY_LEN> pubkey, std::span<uint8_t, CIPHER_LEN> cipher)
|
||||
{
|
||||
return kem::encapsulate<k, η1, η2, du, dv>(m, pubkey, cipher);
|
||||
}
|
||||
|
||||
@@ -28,10 +28,7 @@ constexpr size_t CIPHER_LEN = kyber_utils::get_kem_cipher_len<k, du, dv>();
|
||||
// key is 1632 -bytes, given 32 -bytes seed d ( used in CPA-PKE ) and 32 -bytes
|
||||
// seed z ( used in CCA-KEM ).
|
||||
inline void
|
||||
keygen(std::span<const uint8_t, 32> d,
|
||||
std::span<const uint8_t, 32> z,
|
||||
std::span<uint8_t, PKEY_LEN> pubkey,
|
||||
std::span<uint8_t, SKEY_LEN> seckey)
|
||||
keygen(std::span<const uint8_t, 32> d, std::span<const uint8_t, 32> z, std::span<uint8_t, PKEY_LEN> pubkey, std::span<uint8_t, SKEY_LEN> seckey)
|
||||
{
|
||||
kem::keygen<k, η1>(d, z, pubkey, seckey);
|
||||
}
|
||||
@@ -44,9 +41,7 @@ keygen(std::span<const uint8_t, 32> d,
|
||||
//
|
||||
// Returned KDF can be used for deriving shared key of arbitrary bytes length.
|
||||
inline shake256::shake256_t
|
||||
encapsulate(std::span<const uint8_t, 32> m,
|
||||
std::span<const uint8_t, PKEY_LEN> pubkey,
|
||||
std::span<uint8_t, CIPHER_LEN> cipher)
|
||||
encapsulate(std::span<const uint8_t, 32> m, std::span<const uint8_t, PKEY_LEN> pubkey, std::span<uint8_t, CIPHER_LEN> cipher)
|
||||
{
|
||||
return kem::encapsulate<k, η1, η2, du, dv>(m, pubkey, cipher);
|
||||
}
|
||||
|
||||
@@ -27,10 +27,7 @@ constexpr size_t CIPHER_LEN = kyber_utils::get_kem_cipher_len<k, du, dv>();
|
||||
// key is 2400 -bytes, given 32 -bytes seed d ( used in CPA-PKE ) and 32 -bytes
|
||||
// seed z ( used in CCA-KEM ).
|
||||
inline void
|
||||
keygen(std::span<const uint8_t, 32> d,
|
||||
std::span<const uint8_t, 32> z,
|
||||
std::span<uint8_t, PKEY_LEN> pubkey,
|
||||
std::span<uint8_t, SKEY_LEN> seckey)
|
||||
keygen(std::span<const uint8_t, 32> d, std::span<const uint8_t, 32> z, std::span<uint8_t, PKEY_LEN> pubkey, std::span<uint8_t, SKEY_LEN> seckey)
|
||||
{
|
||||
kem::keygen<k, η1>(d, z, pubkey, seckey);
|
||||
}
|
||||
@@ -43,9 +40,7 @@ keygen(std::span<const uint8_t, 32> d,
|
||||
//
|
||||
// Returned KDF can be used for deriving shared key of arbitrary bytes length.
|
||||
inline shake256::shake256_t
|
||||
encapsulate(std::span<const uint8_t, 32> m,
|
||||
std::span<const uint8_t, PKEY_LEN> pubkey,
|
||||
std::span<uint8_t, CIPHER_LEN> cipher)
|
||||
encapsulate(std::span<const uint8_t, 32> m, std::span<const uint8_t, PKEY_LEN> pubkey, std::span<uint8_t, CIPHER_LEN> cipher)
|
||||
{
|
||||
return kem::encapsulate<k, η1, η2, du, dv>(m, pubkey, cipher);
|
||||
}
|
||||
|
||||
@@ -28,9 +28,7 @@ namespace pke {
|
||||
// benchmarking underlying PKE's key generation implementation.
|
||||
template<size_t k, size_t eta1>
|
||||
static inline void
|
||||
keygen(std::span<const uint8_t, 32> d,
|
||||
std::span<uint8_t, k * 12 * 32 + 32> pubkey,
|
||||
std::span<uint8_t, k * 12 * 32> seckey)
|
||||
keygen(std::span<const uint8_t, 32> d, std::span<uint8_t, k * 12 * 32 + 32> pubkey, std::span<uint8_t, k * 12 * 32> seckey)
|
||||
requires(kyber_params::check_keygen_params(k, eta1))
|
||||
{
|
||||
// step 2
|
||||
@@ -171,9 +169,7 @@ encrypt(std::span<const uint8_t, k * 12 * 32 + 32> pubkey,
|
||||
// https://pq-crystals.org/kyber/data/kyber-specification-round3-20210804.pdf
|
||||
template<size_t k, size_t du, size_t dv>
|
||||
static inline void
|
||||
decrypt(std::span<const uint8_t, k * 12 * 32> seckey,
|
||||
std::span<const uint8_t, k * du * 32 + dv * 32> enc,
|
||||
std::span<uint8_t, 32> dec)
|
||||
decrypt(std::span<const uint8_t, k * 12 * 32> seckey, std::span<const uint8_t, k * du * 32 + dv * 32> enc, std::span<uint8_t, 32> dec)
|
||||
requires(kyber_params::check_decrypt_params(k, du, dv))
|
||||
{
|
||||
constexpr size_t encoff = k * du * 32;
|
||||
|
||||
@@ -124,8 +124,7 @@ cbd(std::span<const uint8_t, 64 * eta> prf, std::span<field::zq_t, ntt::N> poly)
|
||||
const size_t boff = i * 3;
|
||||
const size_t poff = i << 2;
|
||||
|
||||
const uint32_t word = (static_cast<uint32_t>(prf[boff + 2]) << 16) | (static_cast<uint32_t>(prf[boff + 1]) << 8) |
|
||||
static_cast<uint32_t>(prf[boff + 0]);
|
||||
const uint32_t word = (static_cast<uint32_t>(prf[boff + 2]) << 16) | (static_cast<uint32_t>(prf[boff + 1]) << 8) | static_cast<uint32_t>(prf[boff + 0]);
|
||||
|
||||
const uint32_t t0 = (word >> 0) & mask24;
|
||||
const uint32_t t1 = (word >> 1) & mask24;
|
||||
|
||||
@@ -26,14 +26,10 @@ encode(std::span<const field::zq_t, ntt::N> poly, std::span<uint8_t, 32 * l> arr
|
||||
|
||||
for (size_t i = 0; i < itr_cnt; i++) {
|
||||
const size_t off = i << 3;
|
||||
arr[i] = (static_cast<uint8_t>(poly[off + 7].raw() & one) << 7) |
|
||||
(static_cast<uint8_t>(poly[off + 6].raw() & one) << 6) |
|
||||
(static_cast<uint8_t>(poly[off + 5].raw() & one) << 5) |
|
||||
(static_cast<uint8_t>(poly[off + 4].raw() & one) << 4) |
|
||||
(static_cast<uint8_t>(poly[off + 3].raw() & one) << 3) |
|
||||
(static_cast<uint8_t>(poly[off + 2].raw() & one) << 2) |
|
||||
(static_cast<uint8_t>(poly[off + 1].raw() & one) << 1) |
|
||||
(static_cast<uint8_t>(poly[off + 0].raw() & one) << 0);
|
||||
arr[i] = (static_cast<uint8_t>(poly[off + 7].raw() & one) << 7) | (static_cast<uint8_t>(poly[off + 6].raw() & one) << 6) |
|
||||
(static_cast<uint8_t>(poly[off + 5].raw() & one) << 5) | (static_cast<uint8_t>(poly[off + 4].raw() & one) << 4) |
|
||||
(static_cast<uint8_t>(poly[off + 3].raw() & one) << 3) | (static_cast<uint8_t>(poly[off + 2].raw() & one) << 2) |
|
||||
(static_cast<uint8_t>(poly[off + 1].raw() & one) << 1) | (static_cast<uint8_t>(poly[off + 0].raw() & one) << 0);
|
||||
}
|
||||
} else if constexpr (l == 4) {
|
||||
constexpr size_t itr_cnt = ntt::N >> 1;
|
||||
@@ -65,11 +61,9 @@ encode(std::span<const field::zq_t, ntt::N> poly, std::span<uint8_t, 32 * l> arr
|
||||
const auto t7 = poly[poff + 7].raw();
|
||||
|
||||
arr[boff + 0] = (static_cast<uint8_t>(t1 & mask3) << 5) | (static_cast<uint8_t>(t0 & mask5) << 0);
|
||||
arr[boff + 1] = (static_cast<uint8_t>(t3 & mask1) << 7) | (static_cast<uint8_t>(t2 & mask5) << 2) |
|
||||
static_cast<uint8_t>((t1 >> 3) & mask2);
|
||||
arr[boff + 1] = (static_cast<uint8_t>(t3 & mask1) << 7) | (static_cast<uint8_t>(t2 & mask5) << 2) | static_cast<uint8_t>((t1 >> 3) & mask2);
|
||||
arr[boff + 2] = (static_cast<uint8_t>(t4 & mask4) << 4) | static_cast<uint8_t>((t3 >> 1) & mask4);
|
||||
arr[boff + 3] = (static_cast<uint8_t>(t6 & mask2) << 6) | (static_cast<uint8_t>(t5 & mask5) << 1) |
|
||||
static_cast<uint8_t>((t4 >> 4) & mask1);
|
||||
arr[boff + 3] = (static_cast<uint8_t>(t6 & mask2) << 6) | (static_cast<uint8_t>(t5 & mask5) << 1) | static_cast<uint8_t>((t4 >> 4) & mask1);
|
||||
arr[boff + 4] = (static_cast<uint8_t>(t7 & mask5) << 3) | static_cast<uint8_t>((t6 >> 2) & mask3);
|
||||
}
|
||||
} else if constexpr (l == 10) {
|
||||
@@ -201,16 +195,12 @@ decode(std::span<const uint8_t, 32 * l> arr, std::span<field::zq_t, ntt::N> poly
|
||||
const size_t boff = i * 5;
|
||||
|
||||
const auto t0 = static_cast<uint16_t>(arr[boff + 0] & mask5);
|
||||
const auto t1 =
|
||||
static_cast<uint16_t>((arr[boff + 1] & mask2) << 3) | static_cast<uint16_t>((arr[boff + 0] >> 5) & mask3);
|
||||
const auto t1 = static_cast<uint16_t>((arr[boff + 1] & mask2) << 3) | static_cast<uint16_t>((arr[boff + 0] >> 5) & mask3);
|
||||
const auto t2 = static_cast<uint16_t>((arr[boff + 1] >> 2) & mask5);
|
||||
const auto t3 =
|
||||
static_cast<uint16_t>((arr[boff + 2] & mask4) << 1) | static_cast<uint16_t>((arr[boff + 1] >> 7) & mask1);
|
||||
const auto t4 =
|
||||
static_cast<uint16_t>((arr[boff + 3] & mask1) << 4) | static_cast<uint16_t>((arr[boff + 2] >> 4) & mask4);
|
||||
const auto t3 = static_cast<uint16_t>((arr[boff + 2] & mask4) << 1) | static_cast<uint16_t>((arr[boff + 1] >> 7) & mask1);
|
||||
const auto t4 = static_cast<uint16_t>((arr[boff + 3] & mask1) << 4) | static_cast<uint16_t>((arr[boff + 2] >> 4) & mask4);
|
||||
const auto t5 = static_cast<uint16_t>((arr[boff + 3] >> 1) & mask5);
|
||||
const auto t6 =
|
||||
static_cast<uint16_t>((arr[boff + 4] & mask3) << 2) | static_cast<uint16_t>((arr[boff + 3] >> 6) & mask2);
|
||||
const auto t6 = static_cast<uint16_t>((arr[boff + 4] & mask3) << 2) | static_cast<uint16_t>((arr[boff + 3] >> 6) & mask2);
|
||||
const auto t7 = static_cast<uint16_t>((arr[boff + 4] >> 3) & mask5);
|
||||
|
||||
poly[poff + 0] = field::zq_t(t0);
|
||||
@@ -258,12 +248,10 @@ decode(std::span<const uint8_t, 32 * l> arr, std::span<field::zq_t, ntt::N> poly
|
||||
|
||||
const auto t0 = (static_cast<uint16_t>(arr[boff + 1] & mask3) << 8) | static_cast<uint16_t>(arr[boff + 0]);
|
||||
const auto t1 = (static_cast<uint16_t>(arr[boff + 2] & mask6) << 5) | static_cast<uint16_t>(arr[boff + 1] >> 3);
|
||||
const auto t2 = (static_cast<uint16_t>(arr[boff + 4] & mask1) << 10) |
|
||||
(static_cast<uint16_t>(arr[boff + 3]) << 2) | static_cast<uint16_t>(arr[boff + 2] >> 6);
|
||||
const auto t2 = (static_cast<uint16_t>(arr[boff + 4] & mask1) << 10) | (static_cast<uint16_t>(arr[boff + 3]) << 2) | static_cast<uint16_t>(arr[boff + 2] >> 6);
|
||||
const auto t3 = (static_cast<uint16_t>(arr[boff + 5] & mask4) << 7) | static_cast<uint16_t>(arr[boff + 4] >> 1);
|
||||
const auto t4 = (static_cast<uint16_t>(arr[boff + 6] & mask7) << 4) | static_cast<uint16_t>(arr[boff + 5] >> 4);
|
||||
const auto t5 = (static_cast<uint16_t>(arr[boff + 8] & mask2) << 9) |
|
||||
(static_cast<uint16_t>(arr[boff + 7]) << 1) | static_cast<uint16_t>(arr[boff + 6] >> 7);
|
||||
const auto t5 = (static_cast<uint16_t>(arr[boff + 8] & mask2) << 9) | (static_cast<uint16_t>(arr[boff + 7]) << 1) | static_cast<uint16_t>(arr[boff + 6] >> 7);
|
||||
const auto t6 = (static_cast<uint16_t>(arr[boff + 9] & mask5) << 6) | static_cast<uint16_t>(arr[boff + 8] >> 2);
|
||||
const auto t7 = (static_cast<uint16_t>(arr[boff + 10]) << 3) | static_cast<uint16_t>(arr[boff + 9] >> 5);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user