Skip to content

[AUD-026][Medium] Handle or reject the one-point DCT #323

Description

@gabrielsantosphilips

Audit finding

  • ID: AUD-026
  • Status: Verified defect
  • Severity: Medium
  • Confidence: High
  • Audited revision: 2f479320d805a1f9f35ebe4afaaeeded48913a94

Problem

Length == 1 satisfies the DCT power-of-two assertion, but the reorder loop executes zero times. Forward([x]) transforms the initialized zero buffer and returns zero instead of the mathematically valid one-point DCT [x].

Source:

class DiscreteCosineTransform
{
static_assert((Length & (Length - 1)) == 0, "DiscreteCosineTransform size must be a power of 2");
static_assert(math::is_qnumber<QNumberType>::value || std::is_floating_point_v<QNumberType>,
"DiscreteCosineTransform can only be instantiated with math::QNumber types or floating point.");
public:
using VectorReal = typename FastFourierTransform<QNumberType>::VectorReal;
using VectorComplex = typename FastFourierTransform<QNumberType>::VectorComplex;
explicit DiscreteCosineTransform(FastFourierTransform<QNumberType>& fft);
VectorReal& Forward(VectorReal& input);
VectorReal& Inverse(VectorReal& input);
private:
FastFourierTransform<QNumberType>& fft;
typename infra::BoundedVector<QNumberType>::template WithMaxSize<Length> output;
typename infra::BoundedVector<QNumberType>::template WithMaxSize<Length> reordered;
typename VectorComplex::template WithMaxSize<Length> complexBuffer;
};
// Implementation //
template<typename QNumberType, std::size_t Length>
DiscreteCosineTransform<QNumberType, Length>::DiscreteCosineTransform(FastFourierTransform<QNumberType>& fft)
: fft(fft)
{
output.resize(Length);
reordered.resize(Length);
complexBuffer.resize(Length);
}
template<typename QNumberType, std::size_t Length>
OPTIMIZE_FOR_SPEED
typename DiscreteCosineTransform<QNumberType, Length>::VectorReal&
DiscreteCosineTransform<QNumberType, Length>::Forward(VectorReal& input)
{
for (std::size_t n = 0; n < Length / 2; ++n)
{
reordered[n] = input[2 * n];
reordered[Length - 1 - n] = input[2 * n + 1];
}
auto& fftResult = fft.Forward(reordered);
output[0] = QNumberType(math::ToFloat(fftResult[0].Real()) / math::Sqrt(static_cast<float>(Length)));
for (std::size_t k = 1; k < Length; ++k)
{

Acceptance criteria

  • Explicitly implement the one-point transform, or require Length >= 2 at compile time.
  • Forward and inverse behavior agree for the minimum supported length.
  • Add a float-only TEST_F minimum-length round-trip/reference case.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions