Botan 3.14.1
Crypto and TLS for C&
Botan::Dilithium_Algos Namespace Reference

Functions

DilithiumInternalKeypair decode_keypair (StrongSpan< const DilithiumSerializedPrivateKey > sk, DilithiumConstants mode)
std::pair< DilithiumSeedRho, DilithiumPolyVec > decode_public_key (StrongSpan< const DilithiumSerializedPublicKey > pk, const DilithiumConstants &mode)
std::optional< std::tuple< DilithiumCommitmentHash, DilithiumPolyVec, DilithiumPolyVec > > decode_signature (StrongSpan< const DilithiumSerializedSignature > sig, const DilithiumConstants &mode)
std::pair< DilithiumPolyVec, DilithiumPolyVec > decompose (const DilithiumPolyVec &vec, const DilithiumConstants &mode)
DilithiumSerializedCommitment encode_commitment (const DilithiumPolyVec &w1, const DilithiumConstants &mode)
DilithiumSerializedPrivateKey encode_keypair (const DilithiumInternalKeypair &keypair)
DilithiumSerializedPublicKey encode_public_key (StrongSpan< const DilithiumSeedRho > rho, const DilithiumPolyVec &t1, const DilithiumConstants &mode)
DilithiumSerializedSignature encode_signature (StrongSpan< const DilithiumCommitmentHash > c, const DilithiumPolyVec &response, const DilithiumPolyVec &hint, const DilithiumConstants &mode)
DilithiumPolyMatNTT expand_A (StrongSpan< const DilithiumSeedRho > rho, const DilithiumConstants &mode)
DilithiumInternalKeypair expand_keypair (DilithiumSeedRandomness xi, DilithiumConstants mode)
DilithiumPolyVec expand_mask (StrongSpan< const DilithiumSeedRhoPrime > rhoprime, uint16_t nonce, const DilithiumConstants &mode)
std::pair< DilithiumPolyVec, DilithiumPolyVec > expand_s (StrongSpan< const DilithiumSeedRhoPrime > rhoprime, const DilithiumConstants &mode)
bool infinity_norm_within_bound (const DilithiumPolyVec &vec, size_t bound)
DilithiumPolyVec make_hint (const DilithiumPolyVec &z, const DilithiumPolyVec &r, const DilithiumConstants &mode)
std::pair< DilithiumPolyVec, DilithiumPolyVec > power2round (const DilithiumPolyVec &vec)
DilithiumPoly sample_in_ball (StrongSpan< const DilithiumCommitmentHash > seed, const DilithiumConstants &mode)
void use_hint (DilithiumPolyVec &vec, const DilithiumPolyVec &hints, const DilithiumConstants &mode)

Function Documentation

◆ decode_keypair()

DilithiumInternalKeypair Botan::Dilithium_Algos::decode_keypair ( StrongSpan< const DilithiumSerializedPrivateKey > sk,
DilithiumConstants mode )

NIST FIPS 204, Algorithm 25 (skDecode)

Because Botan's Private_Key class inherits from Public_Key, we have to derive the public key from the private key here.

TODO(Botan4): This should be refactored after PrivateKey does not inherit from PublicKey anymore.

Definition at line 403 of file dilithium_algos.cpp.

403 {
404 const auto scope = CT::scoped_poison(sk);
405
406 if(sk.size() != mode.private_key_bytes()) {
407 throw Decoding_Error("invalid size for expanded ML-DSA (or Dilithium) private key");
408 }
409
410 BufferSlicer slicer(sk);
411
414 const auto tr = slicer.copy<DilithiumHashedPublicKey>(mode.public_key_hash_bytes());
415
416 DilithiumPolyVec s1(mode.l());
417 for(auto& p : s1) {
418 poly_unpack_eta(p, slicer, mode, true /* check decoded value range */);
419 }
420
421 DilithiumPolyVec s2(mode.k());
422 for(auto& p : s2) {
423 poly_unpack_eta(p, slicer, mode, true /* check decoded value range */);
424 }
425
426 DilithiumPolyVec t0(mode.k());
427 for(auto& p : t0) {
428 poly_unpack_t0(p, slicer);
429 }
430
431 BOTAN_ASSERT_NOMSG(slicer.empty());
432
433 // Currently, Botan's Private_Key class inherits from Public_Key, forcing us
434 // to derive the public key from the private key here.
435 // TODO(Botan4): Reconsider once PrivateKey/PublicKey issue is tackled.
436
437 CT::unpoison(rho); // rho is public (used in rejection sampling of matrix A)
438
439 const auto A = expand_A(rho, mode);
440 auto [t1, t0_recomputed] = compute_t1_and_t0(A, s1, s2);
441
442 CT::unpoison(t1); // part of the public key
443
445 std::make_shared<Dilithium_PublicKeyInternal>(mode, std::move(rho), std::move(t1)),
446 std::make_shared<Dilithium_PrivateKeyInternal>(std::move(mode),
447 std::nullopt, // decoding cannot recover the private seed
448 std::move(K),
449 std::move(s1),
450 std::move(s2),
451 std::move(t0)),
452 };
453
454 CT::unpoison(tr); // hash of the public key
455
456 if(keypair.first->tr() != tr) {
457 throw Decoding_Error("Calculated dilithium public key hash does not match the one stored in the private key");
458 }
459
460 CT::unpoison(*keypair.second);
461
462 // t0 is fully determined by (rho, s1, s2), so the encoded value must agree
463 CT::unpoison(t0_recomputed);
464 if(!std::ranges::equal(keypair.second->t0().coefficients(), t0_recomputed.coefficients())) {
465 throw Decoding_Error("Decoded dilithium private key t0 does not match the value derived from s1 and s2");
466 }
467
468 return keypair;
469}
#define BOTAN_ASSERT_NOMSG(expr)
Definition assert.h:82
uint8_t l() const
dimensions of the expanded matrix A
static constexpr size_t SEED_SIGNING_KEY_BYTES
size_t public_key_hash_bytes() const
length of the public key hash 'tr' in bytes (differs between R3 and ML-DSA)
size_t private_key_bytes() const
byte length of the encoded private key
uint8_t k() const
dimensions of the expanded matrix A
static constexpr size_t SEED_RHO_BYTES
decltype(auto) size() const noexcept(noexcept(this->m_span.size()))
constexpr auto scoped_poison(const Ts &... xs)
Definition ct_utils.h:222
constexpr void unpoison(const T *p, size_t n)
Definition ct_utils.h:67
DilithiumPolyMatNTT expand_A(StrongSpan< const DilithiumSeedRho > rho, const DilithiumConstants &mode)
Strong< secure_vector< uint8_t >, struct DilithiumSeedK_ > DilithiumSigningSeedK
Private seed K used during signing.
Botan::CRYSTALS::PolynomialVector< DilithiumPolyTraits, Botan::CRYSTALS::Domain::Normal > DilithiumPolyVec
Strong< std::vector< uint8_t >, struct DilithiumPublicSeed_ > DilithiumSeedRho
Public seed to sample the polynomial matrix A from.
BOTAN_FORCE_INLINE constexpr T rho(T x)
Definition rotate.h:53
std::pair< std::shared_ptr< const Dilithium_PublicKeyInternal >, std::shared_ptr< const Dilithium_PrivateKeyInternal > > DilithiumInternalKeypair
Internal representation of a Dilithium key pair.
Strong< std::vector< uint8_t >, struct DilithiumHashedPublicKey_ > DilithiumHashedPublicKey

References BOTAN_ASSERT_NOMSG, Botan::BufferSlicer::copy(), Botan::BufferSlicer::empty(), expand_A(), Botan::DilithiumConstants::k(), Botan::DilithiumConstants::l(), Botan::DilithiumConstants::private_key_bytes(), Botan::DilithiumConstants::public_key_hash_bytes(), Botan::rho(), Botan::CT::scoped_poison(), Botan::DilithiumConstants::SEED_RHO_BYTES, Botan::DilithiumConstants::SEED_SIGNING_KEY_BYTES, Botan::StrongSpan< T >::size(), and Botan::CT::unpoison().

Referenced by Botan::Dilithium_Expanded_Keypair_Codec::decode_keypair(), and Botan::ML_DSA_Expanding_Keypair_Codec::decode_keypair().

◆ decode_public_key()

std::pair< DilithiumSeedRho, DilithiumPolyVec > Botan::Dilithium_Algos::decode_public_key ( StrongSpan< const DilithiumSerializedPublicKey > pk,
const DilithiumConstants & mode )

NIST FIPS 204, Algorithm 23 (pkDecode)

Definition at line 341 of file dilithium_algos.cpp.

342 {
343 if(pk.size() != mode.public_key_bytes()) {
344 throw Decoding_Error("Dilithium: Invalid public key length");
345 }
346
347 BufferSlicer slicer(pk);
349
350 DilithiumPolyVec t1(mode.k());
351 for(auto& p : t1) {
352 poly_unpack_t1(p, slicer);
353 }
354 BOTAN_ASSERT_NOMSG(slicer.empty());
355
356 return {std::move(rho), std::move(t1)};
357}
size_t public_key_bytes() const
byte length of the encoded public key

References BOTAN_ASSERT_NOMSG, Botan::BufferSlicer::copy(), Botan::BufferSlicer::empty(), Botan::DilithiumConstants::k(), Botan::DilithiumConstants::public_key_bytes(), Botan::rho(), Botan::DilithiumConstants::SEED_RHO_BYTES, and Botan::StrongSpan< T >::size().

Referenced by Botan::Dilithium_PublicKeyInternal::decode().

◆ decode_signature()

std::optional< std::tuple< DilithiumCommitmentHash, DilithiumPolyVec, DilithiumPolyVec > > Botan::Dilithium_Algos::decode_signature ( StrongSpan< const DilithiumSerializedSignature > sig,
const DilithiumConstants & mode )

NIST FIPS 204, Algorithm 27 (sigDecode)

Definition at line 493 of file dilithium_algos.cpp.

494 {
495 BufferSlicer slicer(sig);
496 BOTAN_ASSERT_NOMSG(slicer.remaining() == mode.signature_bytes());
497
498 auto commitment_hash = slicer.copy<DilithiumCommitmentHash>(mode.commitment_hash_full_bytes());
499
500 DilithiumPolyVec response(mode.l());
501 for(auto& p : response) {
502 poly_unpack_gamma1(p, slicer, mode);
503 }
504 BOTAN_ASSERT_NOMSG(slicer.remaining() == size_t(mode.omega()) + mode.k());
505
506 auto hint = hint_unpack(slicer, mode);
507 BOTAN_ASSERT_NOMSG(slicer.empty());
508 if(!hint.has_value()) {
509 return std::nullopt;
510 }
511
512 return std::make_tuple(std::move(commitment_hash), std::move(response), std::move(hint.value()));
513}
size_t commitment_hash_full_bytes() const
length of the entire commitment hash 'c~' in bytes (differs between R3 and ML-DSA)
size_t signature_bytes() const
byte length of the encoded signature
DilithiumOmega omega() const
maximal hamming weight of the hint polynomial vector 'h'
Strong< std::vector< uint8_t >, struct DilithiumCommitmentHash_ > DilithiumCommitmentHash
Hash of the message representative and the signer's commitment.

References BOTAN_ASSERT_NOMSG, Botan::DilithiumConstants::commitment_hash_full_bytes(), Botan::BufferSlicer::copy(), Botan::BufferSlicer::empty(), Botan::DilithiumConstants::k(), Botan::DilithiumConstants::l(), Botan::DilithiumConstants::omega(), Botan::BufferSlicer::remaining(), and Botan::DilithiumConstants::signature_bytes().

◆ decompose()

std::pair< DilithiumPolyVec, DilithiumPolyVec > Botan::Dilithium_Algos::decompose ( const DilithiumPolyVec & vec,
const DilithiumConstants & mode )

NIST FIPS 204, Algorithm 36 (Decompose) on a polynomial vector

Algorithms 37 (HighBits) and 38 (LowBits) are not implemented explicitly, simply use the first (HighBits) and second (LowBits) element of the result.

Definition at line 822 of file dilithium_algos.cpp.

822 {
824 switch(mode.gamma2()) {
825 case Gamma2::Qminus1DividedBy32:
826 return decompose_all_coefficients<Gamma2::Qminus1DividedBy32>(vec);
827 break;
828 case Gamma2::Qminus1DividedBy88:
829 return decompose_all_coefficients<Gamma2::Qminus1DividedBy88>(vec);
830 break;
831 }
832
834}
#define BOTAN_ASSERT_UNREACHABLE()
Definition assert.h:173
DilithiumGamma2 gamma2() const
low-order rounding range for decomposing the commitment from polynomial vector 'w'

References BOTAN_ASSERT_UNREACHABLE, and Botan::DilithiumConstants::gamma2().

◆ encode_commitment()

DilithiumSerializedCommitment Botan::Dilithium_Algos::encode_commitment ( const DilithiumPolyVec & w1,
const DilithiumConstants & mode )

NIST FIPS 204, Algorithm 28 (w1Encode)

Definition at line 518 of file dilithium_algos.cpp.

518 {
520 BufferStuffer stuffer(commitment);
521
522 for(const auto& p : w1) {
523 poly_pack_w1(p, stuffer, mode);
524 }
525
526 return commitment;
527}
Helper class to ease in-place marshalling of concatenated fixed-length values.
size_t serialized_commitment_bytes() const
byte length of the packed commitment polynomial vector 'w1'
Strong< std::vector< uint8_t >, struct DilithiumSerializedCommitment_ > DilithiumSerializedCommitment
Serialized representation of a commitment w1.

References Botan::DilithiumConstants::serialized_commitment_bytes().

◆ encode_keypair()

DilithiumSerializedPrivateKey Botan::Dilithium_Algos::encode_keypair ( const DilithiumInternalKeypair & keypair)

NIST FIPS 204, Algorithm 24 (skEncode)

Definition at line 362 of file dilithium_algos.cpp.

362 {
363 const auto& [pk, sk] = keypair;
366 const auto& mode = sk->mode();
367 const auto scope = CT::scoped_poison(*sk);
368
369 DilithiumSerializedPrivateKey serialization(mode.private_key_bytes());
370 BufferStuffer stuffer(serialization);
371
372 stuffer.append(pk->rho());
373 stuffer.append(sk->signing_seed());
374 stuffer.append(pk->tr());
375
376 for(const auto& p : sk->s1()) {
377 poly_pack_eta(p, stuffer, mode);
378 }
379
380 for(const auto& p : sk->s2()) {
381 poly_pack_eta(p, stuffer, mode);
382 }
383
384 for(const auto& p : sk->t0()) {
385 poly_pack_t0(p, stuffer);
386 }
387
388 BOTAN_ASSERT_NOMSG(stuffer.full());
389 CT::unpoison(serialization);
390
391 return serialization;
392}
#define BOTAN_ASSERT_NONNULL(ptr)
Definition assert.h:121
Strong< secure_vector< uint8_t >, struct DilithiumSerializedPrivateKey_ > DilithiumSerializedPrivateKey
Serialized private key data.

References Botan::BufferStuffer::append(), BOTAN_ASSERT_NOMSG, BOTAN_ASSERT_NONNULL, Botan::BufferStuffer::full(), Botan::CT::scoped_poison(), and Botan::CT::unpoison().

Referenced by Botan::Dilithium_Expanded_Keypair_Codec::encode_keypair(), Botan::ML_DSA_Expanding_Keypair_Codec::encode_keypair(), and Botan::Dilithium_PrivateKey::formatted_raw_private_key_bits().

◆ encode_public_key()

DilithiumSerializedPublicKey Botan::Dilithium_Algos::encode_public_key ( StrongSpan< const DilithiumSeedRho > rho,
const DilithiumPolyVec & t1,
const DilithiumConstants & mode )

NIST FIPS 204, Algorithm 22 (pkEncode)

Definition at line 323 of file dilithium_algos.cpp.

325 {
327 BufferStuffer stuffer(pk);
328
329 stuffer.append(rho);
330 for(const auto& p : t1) {
331 poly_pack_t1(p, stuffer);
332 }
333
334 BOTAN_ASSERT_NOMSG(stuffer.full());
335 return pk;
336}
Strong< std::vector< uint8_t >, struct DilithiumSerializedPublicKey_ > DilithiumSerializedPublicKey
Serialized public key data (result of pkEncode(pk)).

References Botan::BufferStuffer::append(), BOTAN_ASSERT_NOMSG, Botan::BufferStuffer::full(), Botan::DilithiumConstants::public_key_bytes(), and Botan::rho().

Referenced by Botan::Dilithium_PublicKeyInternal::raw_pk().

◆ encode_signature()

DilithiumSerializedSignature Botan::Dilithium_Algos::encode_signature ( StrongSpan< const DilithiumCommitmentHash > c,
const DilithiumPolyVec & response,
const DilithiumPolyVec & hint,
const DilithiumConstants & mode )

NIST FIPS 204, Algorithm 26 (sigEncode)

Definition at line 474 of file dilithium_algos.cpp.

477 {
479 BufferStuffer stuffer(sig);
480
481 stuffer.append(c);
482 for(const auto& p : response) {
483 poly_pack_gamma1(p, stuffer, mode);
484 }
485 hint_pack(hint, stuffer, mode);
486
487 return sig;
488}
Strong< std::vector< uint8_t >, struct DilithiumSerializedSignature_ > DilithiumSerializedSignature
Serialized signature data.

References Botan::BufferStuffer::append(), and Botan::DilithiumConstants::signature_bytes().

◆ expand_A()

DilithiumPolyMatNTT Botan::Dilithium_Algos::expand_A ( StrongSpan< const DilithiumSeedRho > rho,
const DilithiumConstants & mode )

NIST FIPS 204, Algorithm 32 (ExpandA)

Note that the actual concatenation of rho, s and r is done downstream in the sampling function.

Definition at line 698 of file dilithium_algos.cpp.

698 {
699 DilithiumPolyMatNTT A(mode.k(), mode.l());
700 for(uint8_t r = 0; r < mode.k(); ++r) {
701 for(uint8_t s = 0; s < mode.l(); ++s) {
702 sample_ntt_uniform(rho, A[r][s], load_le(std::array{s, r}), mode);
703 }
704 }
705 return A;
706}
constexpr auto load_le(ParamTs &&... params)
Definition loadstor.h:495
Botan::CRYSTALS::PolynomialMatrix< DilithiumPolyTraits > DilithiumPolyMatNTT

References Botan::DilithiumConstants::k(), Botan::DilithiumConstants::l(), Botan::load_le(), and Botan::rho().

Referenced by decode_keypair(), and expand_keypair().

◆ expand_keypair()

DilithiumInternalKeypair Botan::Dilithium_Algos::expand_keypair ( DilithiumSeedRandomness xi,
DilithiumConstants mode )

NIST FIPS 204, Algorithm 6 (ML-DSA.KeyGen_internal)

Lines 5-7 are extracted into a separate function, see above. The key encoding is deferred until the user explicitly invokes the encoding.

Definition at line 665 of file dilithium_algos.cpp.

665 {
667 throw Decoding_Error("Invalid ML-DSA seed size");
668 }
669 const auto& sympriv = mode.symmetric_primitives();
670 CT::poison(xi);
671
672 auto [rho, rhoprime, K] = sympriv.H(xi);
673 CT::unpoison(rho); // rho is public (seed for the public matrix A)
674
675 const auto A = Dilithium_Algos::expand_A(rho, mode);
676 auto [s1, s2] = Dilithium_Algos::expand_s(rhoprime, mode);
677 auto [t1, t0] = Dilithium_Algos::compute_t1_and_t0(A, s1, s2);
678
679 CT::unpoison(t1); // part of the public key
680
682 std::make_shared<Dilithium_PublicKeyInternal>(mode, std::move(rho), std::move(t1)),
683 std::make_shared<Dilithium_PrivateKeyInternal>(
684 std::move(mode), std::move(xi), std::move(K), std::move(s1), std::move(s2), std::move(t0)),
685 };
686
687 CT::unpoison(*keypair.second);
688
689 return keypair;
690}
Dilithium_Symmetric_Primitives_Base & symmetric_primitives() const
static constexpr size_t SEED_RANDOMNESS_BYTES
size_type size() const noexcept(noexcept(this->get().size()))
constexpr void poison(const T *p, size_t n)
Definition ct_utils.h:56
std::pair< DilithiumPolyVec, DilithiumPolyVec > expand_s(StrongSpan< const DilithiumSeedRhoPrime > rhoprime, const DilithiumConstants &mode)

References expand_A(), expand_s(), Botan::CT::poison(), Botan::rho(), Botan::DilithiumConstants::SEED_RANDOMNESS_BYTES, Botan::detail::Container_Strong_Adapter_Base< T >::size(), Botan::DilithiumConstants::symmetric_primitives(), and Botan::CT::unpoison().

Referenced by Botan::ML_DSA_Expanding_Keypair_Codec::decode_keypair(), and Botan::Dilithium_PrivateKey::Dilithium_PrivateKey().

◆ expand_mask()

DilithiumPolyVec Botan::Dilithium_Algos::expand_mask ( StrongSpan< const DilithiumSeedRhoPrime > rhoprime,
uint16_t nonce,
const DilithiumConstants & mode )

NIST FIPS 204, Algorithm 34 (ExpandMask)

Definition at line 731 of file dilithium_algos.cpp.

733 {
734 DilithiumPolyVec s(mode.l());
735 for(auto& p : s) {
736 auto xof = mode.symmetric_primitives().H(rhoprime, nonce++);
737 poly_unpack_gamma1(p, *xof, mode);
738 }
739 return s;
740}
DilithiumHashedPublicKey H(StrongSpan< const DilithiumSerializedPublicKey > pk) const

References Botan::Dilithium_Symmetric_Primitives_Base::H(), Botan::DilithiumConstants::l(), and Botan::DilithiumConstants::symmetric_primitives().

◆ expand_s()

std::pair< DilithiumPolyVec, DilithiumPolyVec > Botan::Dilithium_Algos::expand_s ( StrongSpan< const DilithiumSeedRhoPrime > rhoprime,
const DilithiumConstants & mode )

NIST FIPS 204, Algorithm 33 (ExpandS)

Definition at line 711 of file dilithium_algos.cpp.

712 {
713 auto result = std::make_pair(DilithiumPolyVec(mode.l()), DilithiumPolyVec(mode.k()));
714 auto& [s1, s2] = result;
715
716 uint16_t nonce = 0;
717 for(auto& p : s1) {
718 sample_uniform_eta(rhoprime, p, nonce++, mode);
719 }
720
721 for(auto& p : s2) {
722 sample_uniform_eta(rhoprime, p, nonce++, mode);
723 }
724
725 return result;
726}

References Botan::DilithiumConstants::k(), and Botan::DilithiumConstants::l().

Referenced by expand_keypair().

◆ infinity_norm_within_bound()

bool Botan::Dilithium_Algos::infinity_norm_within_bound ( const DilithiumPolyVec & vec,
size_t bound )

Definition at line 939 of file dilithium_algos.cpp.

939 {
940 BOTAN_DEBUG_ASSERT(bound <= (DilithiumConstants::Q - 1) / 8);
941
942 // It is ok to leak which coefficient violates the bound as the probability
943 // for each coefficient is independent of secret data but we must not leak
944 // the sign of the centralized representative.
945 for(const auto& p : vec) {
946 for(const auto c : p) {
947 const auto abs_c = c - is_negative_mask(c).if_set_return(2 * c);
948 if(CT::driveby_unpoison(abs_c >= bound)) {
949 return false;
950 }
951 }
952 }
953
954 return true;
955}
#define BOTAN_DEBUG_ASSERT(expr)
Definition assert.h:136
static constexpr T Q
modulus
constexpr decltype(auto) driveby_unpoison(T &&v)
Definition ct_utils.h:243

References BOTAN_DEBUG_ASSERT, Botan::CT::driveby_unpoison(), and Botan::DilithiumConstants::Q.

◆ make_hint()

DilithiumPolyVec Botan::Dilithium_Algos::make_hint ( const DilithiumPolyVec & z,
const DilithiumPolyVec & r,
const DilithiumConstants & mode )

NIST FIPS 204, Algorithm 39 (MakeHint)

MakeHint is specified per value in FIPS 204. This implements the algorithm for the entire polynomial vector. The specified algorithm is equivalent to the inner lambda.

TODO: This is taken from the reference implementation. We should implement it as specified in the spec, and see if that has any performance impact.

Definition at line 846 of file dilithium_algos.cpp.

846 {
847 BOTAN_DEBUG_ASSERT(z.size() == r.size());
848
849 const auto make_hint = [gamma2 = uint32_t(mode.gamma2()),
850 q_gamma2 = static_cast<uint32_t>(DilithiumConstants::Q) - uint32_t(mode.gamma2())](
851 int32_t c0, int32_t c1) -> CT::Choice {
852 BOTAN_DEBUG_ASSERT(c0 >= 0);
853 BOTAN_DEBUG_ASSERT(c1 >= 0);
854
855 const uint32_t pc0 = static_cast<uint32_t>(c0);
856 const uint32_t pc1 = static_cast<uint32_t>(c1);
857
858 return (CT::Mask<uint32_t>::is_gt(pc0, gamma2) & CT::Mask<uint32_t>::is_lte(pc0, q_gamma2) &
860 .as_choice();
861 };
862
863 DilithiumPolyVec hint(r.size());
864
865 for(size_t i = 0; i < r.size(); ++i) {
866 for(size_t j = 0; j < r[i].size(); ++j) {
867 hint[i][j] = static_cast<int>(make_hint(z[i][j], r[i][j]).as_bool());
868 }
869 }
870
871 BOTAN_DEBUG_ASSERT(hint.ct_validate_value_range(0, 1));
872
873 return hint;
874}
static constexpr Mask< T > is_lte(T x, T y)
Definition ct_utils.h:468
static constexpr Mask< T > is_equal(T x, T y)
Definition ct_utils.h:447
static constexpr Mask< T > is_gt(T x, T y)
Definition ct_utils.h:463
static constexpr Mask< T > is_zero(T x)
Definition ct_utils.h:442
DilithiumPolyVec make_hint(const DilithiumPolyVec &z, const DilithiumPolyVec &r, const DilithiumConstants &mode)

References BOTAN_DEBUG_ASSERT, Botan::CRYSTALS::PolynomialVector< Trait, D >::ct_validate_value_range(), Botan::DilithiumConstants::gamma2(), Botan::CT::Mask< T >::is_equal(), Botan::CT::Mask< T >::is_gt(), Botan::CT::Mask< T >::is_lte(), Botan::CT::Mask< T >::is_zero(), make_hint(), Botan::DilithiumConstants::Q, and Botan::CRYSTALS::PolynomialVector< Trait, D >::size().

Referenced by make_hint().

◆ power2round()

std::pair< DilithiumPolyVec, DilithiumPolyVec > Botan::Dilithium_Algos::power2round ( const DilithiumPolyVec & vec)

NIST FIPS 204, Algorithm 35 (Power2Round)

In contrast to the spec, this function takes a polynomial vector and performs the power2round operation on each coefficient in the vector. The actual Algorithm 35 as specified is actually just the inner lambda.

Definition at line 749 of file dilithium_algos.cpp.

749 {
750 // This procedure is taken verbatim from Dilithium's reference implementation.
751 const auto power2round = [d = DilithiumConstants::D](int32_t r) -> std::pair<int32_t, int32_t> {
752 const int32_t r1 = (r + (1 << (d - 1)) - 1) >> d;
753 const int32_t r0 = r - (r1 << d);
754 return {r1, r0};
755 };
756
757 auto result = std::make_pair(DilithiumPolyVec(vec.size()), DilithiumPolyVec(vec.size()));
758
759 for(size_t i = 0; i < vec.size(); ++i) {
760 for(size_t j = 0; j < vec[i].size(); ++j) {
761 std::tie(result.first[i][j], result.second[i][j]) = power2round(vec[i][j]);
762 }
763 }
764
765 return result;
766}
static constexpr T D
number of dropped bits from t (see FIPS 204 Section 5)
std::pair< DilithiumPolyVec, DilithiumPolyVec > power2round(const DilithiumPolyVec &vec)

References Botan::DilithiumConstants::D, power2round(), and Botan::CRYSTALS::PolynomialVector< Trait, D >::size().

Referenced by power2round().

◆ sample_in_ball()

DilithiumPoly Botan::Dilithium_Algos::sample_in_ball ( StrongSpan< const DilithiumCommitmentHash > seed,
const DilithiumConstants & mode )

NIST FIPS 204, Algorithm 29 (SampleInBall)

Definition at line 532 of file dilithium_algos.cpp.

532 {
533 // This generator resembles the while loop in the spec.
534 auto xof = mode.symmetric_primitives().H(seed);
536
538 uint64_t signs = load_le(bounded_xof.next<8>());
539 for(size_t i = c.size() - mode.tau(); i < c.size(); ++i) {
540 const auto j = bounded_xof.next_byte([i](uint8_t byte) { return byte <= i; });
541 c[i] = c[j];
542 c[j] = 1 - 2 * (signs & 1);
543 signs >>= 1;
544 }
545
548
549 return c;
550}
constexpr size_t hamming_weight() const noexcept
Definition pqcrystals.h:296
constexpr size_t size() const
Definition pqcrystals.h:278
constexpr bool ct_validate_value_range(T min, T max) const noexcept
Definition pqcrystals.h:291
DilithiumTau tau() const
hamming weight of the polynomial 'c' sampled from the commitment's hash
detail::Bounded_XOF< XOF &, bound > Bounded_XOF
Botan::CRYSTALS::Polynomial< DilithiumPolyTraits, Botan::CRYSTALS::Domain::Normal > DilithiumPoly

References BOTAN_DEBUG_ASSERT, Botan::CRYSTALS::Polynomial< Trait, D >::ct_validate_value_range(), Botan::Dilithium_Symmetric_Primitives_Base::H(), Botan::CRYSTALS::Polynomial< Trait, D >::hamming_weight(), Botan::load_le(), Botan::CRYSTALS::Polynomial< Trait, D >::size(), Botan::DilithiumConstants::symmetric_primitives(), and Botan::DilithiumConstants::tau().

◆ use_hint()

void Botan::Dilithium_Algos::use_hint ( DilithiumPolyVec & vec,
const DilithiumPolyVec & hints,
const DilithiumConstants & mode )

NIST FIPS 204, Algorithm 40 (UseHint)

UseHint is specified per value in FIPS 204. This implements the algorithm for the entire polynomial vector. The specified algorithm is equivalent to the inner lambdas of 'use_hint_with_coefficients'.

Definition at line 921 of file dilithium_algos.cpp.

921 {
922 BOTAN_DEBUG_ASSERT(hints.size() == vec.size());
925
927 switch(mode.gamma2()) {
928 case Gamma2::Qminus1DividedBy32:
929 use_hint_on_coefficients<Gamma2::Qminus1DividedBy32>(hints, vec);
930 break;
931 case Gamma2::Qminus1DividedBy88:
932 use_hint_on_coefficients<Gamma2::Qminus1DividedBy88>(hints, vec);
933 break;
934 }
935
937}
constexpr bool ct_validate_value_range(T min, T max) const noexcept
Definition pqcrystals.h:441

References BOTAN_DEBUG_ASSERT, Botan::CRYSTALS::PolynomialVector< Trait, D >::ct_validate_value_range(), Botan::DilithiumConstants::gamma2(), Botan::DilithiumConstants::Q, and Botan::CRYSTALS::PolynomialVector< Trait, D >::size().